RelTR icon indicating copy to clipboard operation
RelTR copied to clipboard

annotation files

Open Turkishvein opened this issue 2 years ago • 2 comments

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

Turkishvein avatar Apr 03 '23 13:04 Turkishvein

+1

Maelic avatar Aug 04 '23 13:08 Maelic

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}

yrcong avatar Nov 10 '23 13:11 yrcong