def show_annotations(): generator = PascalVocGenerator(data_dir='datasets/voc_trainval/VOC0712', set_name='val') for image_group, annotation_group, targets in generator: locations = targets[0] batch_regr_targets = targets[1] batch_cls_targets = targets[2] batch_centerness_targets = targets[3] for image, annotation, regr_targets, cls_targets, centerness_targets in zip( image_group, annotation_group, batch_regr_targets, batch_cls_targets, batch_centerness_targets): gt_boxes = annotation['bboxes'] for gt_box in gt_boxes: gt_xmin, gt_ymin, gt_xmax, gt_ymax = gt_box cv2.rectangle(image, (int(gt_xmin), int(gt_ymin)), (int(gt_xmax), int(gt_ymax)), (0, 255, 0), 2) pos_indices = np.where(centerness_targets[:, 1] == 1)[0] for pos_index in pos_indices: cx, cy = locations[pos_index] l, t, r, b, *_ = regr_targets[pos_index] xmin = cx - l ymin = cy - t xmax = cx + r ymax = cy + b class_id = np.argmax(cls_targets[pos_index]) centerness = centerness_targets[pos_index][0] # cv2.putText(image, '{:.2f}'.format(centerness), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (255, 0, 255), 2) cv2.putText(image, str(class_id), (xmin, ymin), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 0, 0), 3) cv2.circle(image, (round(cx), round(cy)), 5, (255, 0, 0), -1) cv2.rectangle(image, (round(xmin), round(ymin)), (round(xmax), round(ymax)), (0, 0, 255), 2) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', image) cv2.waitKey(0)
def verify_no_negative_regr(): transform_generator = random_transform_generator( min_rotation=-0.1, max_rotation=0.1, min_translation=(-0.1, -0.1), max_translation=(0.1, 0.1), min_shear=-0.1, max_shear=0.1, min_scaling=(0.9, 0.9), max_scaling=(1.1, 1.1), flip_x_chance=0.5, flip_y_chance=0.5, ) visual_effect_generator = random_visual_effect_generator( contrast_range=(0.9, 1.1), brightness_range=(-.1, .1), hue_range=(-0.05, 0.05), saturation_range=(0.95, 1.05) ) common_args = { 'batch_size': 1, 'image_min_side': 800, 'image_max_side': 1333, 'preprocess_image': preprocess_image, } generator = PascalVocGenerator( 'datasets/voc_trainval/VOC0712', 'trainval', transform_generator=transform_generator, visual_effect_generator=visual_effect_generator, skip_difficult=True, **common_args ) i = 0 for image_group, targets in generator: i += 1 if i > 20000: break
def create_generators(args, preprocess_image): """ Create generators for training and validation. Args args: parseargs object containing configuration for generators. preprocess_image: Function that preprocesses an image for the network. """ common_args = { 'batch_size': args.batch_size, 'config': args.config, 'image_min_side': args.image_min_side, 'image_max_side': args.image_max_side, 'preprocess_image': preprocess_image, } # create random transform generator for augmenting training data if args.random_transform: transform_generator = random_transform_generator( min_rotation=-0.1, max_rotation=0.1, min_translation=(-0.1, -0.1), max_translation=(0.1, 0.1), min_shear=-0.1, max_shear=0.1, min_scaling=(0.9, 0.9), max_scaling=(1.1, 1.1), flip_x_chance=0.5, flip_y_chance=0.5, ) visual_effect_generator = random_visual_effect_generator( contrast_range=(0.9, 1.1), brightness_range=(-.1, .1), hue_range=(-0.05, 0.05), saturation_range=(0.95, 1.05) ) else: transform_generator = random_transform_generator(flip_x_chance=0.5) visual_effect_generator = None if args.dataset_type == 'pascal': train_generator = PascalVocGenerator( args.pascal_path, 'trainval', transform_generator=transform_generator, visual_effect_generator=visual_effect_generator, skip_difficult=True, **common_args ) validation_generator = PascalVocGenerator( args.pascal_path, 'val', shuffle_groups=False, skip_difficult=True, **common_args ) elif args.dataset_type == 'csv': train_generator = CSVGenerator( args.annotations_path, args.classes_path, transform_generator=transform_generator, visual_effect_generator=visual_effect_generator, **common_args ) if args.val_annotations_path: validation_generator = CSVGenerator( args.val_annotations_path, args.classes_path, shuffle_groups=False, **common_args ) else: validation_generator = None elif args.dataset_type == 'coco': # import here to prevent unnecessary dependency on cocoapi from generators.coco_generator import CocoGenerator train_generator = CocoGenerator( args.coco_path, 'train2017', transform_generator=transform_generator, visual_effect_generator=visual_effect_generator, **common_args ) validation_generator = CocoGenerator( args.coco_path, 'val2017', shuffle_groups=False, **common_args ) else: raise ValueError('Invalid data type received: {}'.format(args.dataset_type)) return train_generator, validation_generator
'image_min_side': 800, 'image_max_side': 1333, 'preprocess_image': preprocess_image, } # generator = PascalVocGenerator( # 'datasets/voc_trainval/VOC0712', # 'val', # shuffle_groups=False, # skip_truncated=False, # skip_difficult=True, # **common_args # ) generator = PascalVocGenerator( 'datasets/voc_test/VOC2007', 'test', shuffle_groups=False, skip_truncated=False, skip_difficult=True, **common_args ) model_path = '/home/adam/workspace/github/xuannianz/carrot/fsaf/snapshots/2019-10-05/resnet101_pascal_47_0.7652.h5' # load retinanet model # import keras.backend as K # K.set_learning_phase(1) from models.resnet import resnet_fsaf from models.retinanet import fsaf_bbox fsaf = resnet_fsaf(num_classes=20, backbone='resnet101') model = fsaf_bbox(fsaf) model.load_weights(model_path, by_name=True) average_precisions = evaluate(generator, model, visualize=False) # compute per class average precision total_instances = []
'batch_size': 1, 'image_min_side': 800, 'image_max_side': 1333, 'preprocess_image': preprocess_image, } # generator = PascalVocGenerator( # 'datasets/voc_trainval/VOC0712', # 'val', # shuffle_groups=False, # skip_truncated=False, # skip_difficult=True, # **common_args # ) generator = PascalVocGenerator('datasets/VOC2007', 'test', shuffle_groups=False, skip_truncated=False, skip_difficult=True, **common_args) model_path = 'snapshots/2019-08-25/resnet101_pascal_07_0.7352.h5' # load retinanet model import keras.backend as K K.clear_session() K.set_learning_phase(1) model = models.load_model(model_path, backbone_name='resnet101') # if the model is not converted to an inference model, use the line below # see: https://github.com/fizyr/keras-retinanet#converting-a-training-model-to-inference-model model = models.convert_model(model) average_precisions = evaluate(generator, model, epoch=0) # compute per class average precision total_instances = [] precisions = []