Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

annotation files #24

Open
Turkishvein opened this issue Apr 3, 2023 · 2 comments
Open

annotation files #24

Turkishvein opened this issue Apr 3, 2023 · 2 comments

Comments

@Turkishvein
Copy link

hey can you share how to create annotations files for custom dataset, thanks.

@Maelic
Copy link

Maelic commented Aug 4, 2023

+1

@yrcong
Copy link
Owner

yrcong commented Nov 10, 2023

We create the .json files in COCO format.

You can refer to the code we used:

from dataloaders.visual_genome_coco import VGDataLoader, VG
import numpy as np
from torch import optim
import torch
from PIL import Image
import time
import json

train, val, test = VG.splits(num_val_im=5000, filter_duplicate_rels=True,
                             use_proposals=False,
                            filter_non_overlap=True)
rel_categories = train.ind_to_predicates

counter = 0

images = []
annotations = []
categories = []
train_rel = {}

for idx, i in enumerate(train.ind_to_classes):
    if idx == 0:
        continue
    else:
        category = {'supercategory': i, 'id': idx, 'name': i}
        categories.append(category)


train_triplets = np.zeros([151,151,51]) #sub obj rel

for idx, i in enumerate(train.filenames):
    file_name = i.split('/')[-1]
    w, h = Image.open(i).size
    image_id = int(i.split('/')[-1].split('.')[0])
    image = {'file_name': file_name,
              'height': h,
              'width': w,
              'id': image_id}
    images.append(image)
    train_rel[image_id] = [triplet.tolist() for triplet in np.unique(train.relationships[idx],axis=0)]

    for idx2, j in enumerate(train.gt_boxes[idx]):
        j = j*max(w, h)/1024
        bbox = [int(j[0]), int(j[1]), int(j[2] - j[0] + 1), int(j[3] - j[1] + 1)]
        area = int((j[3] - j[1] + 1) * (j[2] - j[0] + 1))
        anno_id = counter
        counter = counter + 1
        annotation = {'segmentation': None,
                      'area': area,
                      'bbox': bbox,
                      'iscrowd': 0,
                      'image_id': image_id,
                      'id': anno_id,
                      'category_id': int(train.gt_classes[idx][idx2])}
        annotations.append(annotation)

    for relation in train_rel[image_id]:
        train_triplets[train.gt_classes[idx][relation[0]], train.gt_classes[idx][relation[1]], relation[2]] += 1


train_database = {'images': images,
                  'annotations': annotations,
                  'categories': categories}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants