in FasterRCNNDetection/data/iwildcam_dataset.py [0:0]
def __init__(self, root, ann_file, max_images=None):
self.root = root
print('Loading annotations from: ' + os.path.basename(ann_file))
with open(ann_file) as data_file:
ann_data = json.load(data_file)
# set up the filenames and annotations
self.impaths = np.array([aa['file_name'] for aa in ann_data['images']])
self.image_ids = np.array([aa['id'] for aa in ann_data['images']])
# This loop reads the bboxes and corresponding labels and assigns them
# the correct image. Kind of slow at the moment...
self.bboxes = [[] for _ in self.image_ids]
self.labels = [[] for _ in self.image_ids]
# To speed up the loop, creating mapping for image_id to list index
image_id_to_idx = {id:idx for idx, id in enumerate(self.image_ids)}
for ann in ann_data['annotations']:
idx = image_id_to_idx[ann['image_id']]
#check that the image contains an animal, if not, don't append a box or label to the
#image list
if 'bbox' in ann:
# Bboxes should have ('ymin', 'xmin', 'ymax', 'xmax') format
self.bboxes[idx].append([ann['bbox'][1], ann['bbox'][0],
ann['bbox'][1] + ann['bbox'][3],
ann['bbox'][0] + ann['bbox'][2]])
# Currently we take the label from the annotation file, non-consecutive-
# label-support would be great
self.labels[idx].append(ann['category_id'])
else:
#self.bboxes[idx].append([-1.,-1.,0.,0.])
#self.labels[idx].append(30)
self.bboxes[idx].append([])
self.labels[idx].append(30)
# load classes
self.classes = np.unique(sorted([cat['id'] for cat in ann_data['categories']]))
if opt.dataset == 'oneclass':
self.class_names = ['Animal']
else:
self.class_names = ['' for _ in range(self.get_class_count())]
for cat in ann_data['categories']:
self.class_names[cat['id']] = '{}'.format(cat['name'])
# print out some stats
print("The dataset has {} images containing {} classes".format(
len(self.image_ids),
len(self.classes)))
if max_images is not None:
print('Selecting a subset of {} images from training and validation'.
format(max_images))
self.impaths = self.impaths[:max_images]
self.image_ids = self.image_ids[:max_images]
self.bboxes = self.bboxes[:max_images]
self.labels = self.labels[:max_images]
if opt.dataset == 'oneclass':
print('Merging all classes to one category')
for ll in self.labels:
for l_idx in range(len(ll)):
ll[l_idx] = 0