def create_generators(args): """ 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, 'phi': args.phi, } # create random transform generator for augmenting training data if args.random_transform: misc_effect = MiscEffect() visual_effect = VisualEffect() else: misc_effect = None visual_effect = None if args.dataset_type == 'pascal': from generators.pascal import PascalVocGenerator train_generator = PascalVocGenerator(args.pascal_path, 'trainval', skip_difficult=True, misc_effect=misc_effect, visual_effect=visual_effect, **common_args) validation_generator = PascalVocGenerator(args.pascal_path, 'val', skip_difficult=True, shuffle_groups=False, **common_args) elif args.dataset_type == 'csv': from generators.csv_ import CSVGenerator train_generator = CSVGenerator(args.annotations_path, args.classes_path, misc_effect=misc_effect, visual_effect=visual_effect, **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 else: raise ValueError('Invalid data type received: {}'.format( args.dataset_type)) return train_generator, validation_generator
def main(args=None): if len(sys.argv) is 3: model_path = str(sys.argv[1]) dataset_path = str(sys.argv[2]) else: print( "Pass model path and dataset path in respectively as command line argument" ) exit() from generators.pascal import PascalVocGenerator from model import efficientdet import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 4 weighted_bifpn = False common_args = { 'batch_size': 1, 'phi': phi, } test_generator = PascalVocGenerator(dataset_path, 'test', shuffle_groups=False, skip_truncated=False, skip_difficult=True, **common_args) model_path = model_path input_shape = (test_generator.image_size, test_generator.image_size) anchors = test_generator.anchors num_classes = test_generator.num_classes() model, prediction_model = efficientdet(phi=phi, num_classes=num_classes, weighted_bifpn=weighted_bifpn) prediction_model.load_weights(model_path, by_name=True) average_precisions = evaluate(test_generator, prediction_model, visualize=False) # compute per class average precision total_instances = [] precisions = [] for label, (average_precision, num_annotations) in average_precisions.items(): print('{:.0f} instances of class'.format(num_annotations), test_generator.label_to_name(label), 'with average precision: {:.4f}'.format(average_precision)) total_instances.append(num_annotations) precisions.append(average_precision) mean_ap = sum(precisions) / sum(x > 0 for x in total_instances) print('mAP: {:.4f}'.format(mean_ap))
def create_generators(train=False): train_generator = PascalVocGenerator(image_dir=cfg.IMAGE_DIR, annotation_dir=cfg.ANNOTATION_DIR, text_file=cfg.TRAIN_TEXT, skip_difficult=True, classes=cfg.CLASSES, multi_scale=cfg.MULTI_SCALE, **cfg.COMMON_ARGS) validation_generator = PascalVocGenerator( image_dir=cfg.IMAGE_DIR, annotation_dir=cfg.ANNOTATION_DIR, text_file=cfg.VAL_TEXT, skip_difficult=True, classes=cfg.CLASSES, shuffle_groups=False, **cfg.COMMON_ARGS, train_data=False) return train_generator, validation_generator
def create_generators(args): """ 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, 'phi': args.phi, 'detect_text': args.detect_text, 'detect_quadrangle': args.detect_quadrangle } # create random transform generator for augmenting training data if args.random_transform: misc_effect = MiscEffect() visual_effect = VisualEffect() else: misc_effect = None visual_effect = None if args.dataset_type == 'pascal': from generators.pascal import PascalVocGenerator train_generator = PascalVocGenerator( args.pascal_path, 'trainval', skip_difficult=True, misc_effect=misc_effect, visual_effect=visual_effect, image_extension=".png", **common_args ) validation_generator = PascalVocGenerator( args.pascal_path, 'val', skip_difficult=True, shuffle_groups=False, image_extension=".png", **common_args ) elif args.dataset_type == 'csv': from generators.csv_ import CSVGenerator train_generator = CSVGenerator( args.annotations_path, args.classes_path, misc_effect=misc_effect, visual_effect=visual_effect, **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 import CocoGenerator train_generator = CocoGenerator( args.coco_path, 'train2017', misc_effect=misc_effect, visual_effect=visual_effect, group_method='random', **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
if __name__ == '__main__': from generators.pascal import PascalVocGenerator from model import efficientdet import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 1 weighted_bifpn = False common_args = { 'batch_size': 1, 'phi': phi, } test_generator = PascalVocGenerator('datasets/VOC2007', 'test', shuffle_groups=False, skip_truncated=False, skip_difficult=True, **common_args) model_path = 'checkpoints/2019-12-03/pascal_05_0.6283_1.1975_0.8029.h5' input_shape = (test_generator.image_size, test_generator.image_size) anchors = test_generator.anchors num_classes = test_generator.num_classes() model, prediction_model = efficientdet(phi=phi, num_classes=num_classes, weighted_bifpn=weighted_bifpn) prediction_model.load_weights(model_path, by_name=True) average_precisions = evaluate(test_generator, prediction_model, visualize=False) # compute per class average precision total_instances = []
def main(): generator = PascalVocGenerator(cfg.IMAGE_DIR, cfg.ANNOTATION_DIR, cfg.TEST_TEXT, classes=cfg.CLASSES, skip_difficult=True, train_data=False) num_classes = generator.num_classes() classes = list(generator.classes.keys()) colors = [ np.random.randint(0, 256, 3).tolist() for i in range(num_classes) ] model = centernet(num_classes, score_threshold=cfg.SCORE_THRESHOLD, nms=cfg.NMS, flip_test=cfg.FLIP_TEST, training=False) model.load_weights(model_path, by_name=True, skip_mismatch=True) for i in range(10): image = generator.load_image(i) #cv2.imwrite("./results/{}_o.jpg".format(i), image) src_image = image.copy() c = np.array([image.shape[1] / 2., image.shape[0] / 2.], dtype=np.float32) s = max(image.shape[0], image.shape[1]) * 1.0 tgt_w = generator.input_size tgt_h = generator.input_size image = generator.preprocess_image(image, c, s, tgt_w=tgt_w, tgt_h=tgt_h) if cfg.FLIP_TEST: flipped_image = image[:, ::-1] inputs = np.stack([image, flipped_image], axis=0) else: inputs = np.expand_dims(image, axis=0) detections = model.predict_on_batch(inputs)[0] scores = detections[:, 4] indices = np.where(scores > cfg.SCORE_THRESHOLD)[0] detections = detections[indices] detections_copy = detections.copy() detections = detections.astype(np.float64) trans = get_affine_transform(c, s, (tgt_w // 4, tgt_h // 4), inv=1) for j in range(detections.shape[0]): detections[j, 0:2] = affine_transform(detections[j, 0:2], trans) detections[j, 2:4] = affine_transform(detections[j, 2:4], trans) detections[:, [0, 2]] = np.clip(detections[:, [0, 2]], 0, src_image.shape[1]) detections[:, [1, 3]] = np.clip(detections[:, [1, 3]], 0, src_image.shape[0]) for detection in detections: xmin = int(round(detection[0])) ymin = int(round(detection[1])) xmax = int(round(detection[2])) ymax = int(round(detection[3])) score = '{:.4f}'.format(detection[4]) class_id = int(detection[5]) color = colors[class_id] class_name = classes[class_id] label = '-'.join([class_name, score]) ret, baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1) cv2.rectangle(src_image, (xmin, ymin), (xmax, ymax), color, 1) cv2.rectangle(src_image, (xmin, ymax - ret[1] - baseline), (xmin + ret[0], ymax), color, -1) cv2.putText(src_image, label, (xmin, ymax - baseline), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1) #cv2.imwrite("./results/{}_r.jpg".format(i), src_image) cv2.imshow('image', src_image) cv2.waitKey(0)
self.crop_prob = crop_prob self.translate_prob = translate_prob def __call__(self, image, boxes): image, boxes = rotate(image, boxes, prob=self.rotate_prob) image, boxes = crop(image, boxes, prob=self.crop_prob) image, boxes = translate(image, boxes, prob=self.translate_prob) return image, boxes if __name__ == '__main__': from generators.pascal import PascalVocGenerator train_generator = PascalVocGenerator('datasets/VOC0712', 'trainval', skip_difficult=True, anchors_path='voc_anchors_416.txt', batch_size=1) misc_effect = MiscEffect() for i in range(train_generator.size()): image = train_generator.load_image(i) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) annotations = train_generator.load_annotations(i) boxes = annotations['bboxes'] for box in boxes.astype(np.int32): cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 2) src_image = image.copy() cv2.namedWindow('src_image', cv2.WINDOW_NORMAL) cv2.imshow('src_image', src_image) image, boxes = misc_effect(image, boxes)
from generators.pascal import PascalVocGenerator from model import efficientdet import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 1 weighted_bifpn = False common_args = { 'batch_size': 1, 'phi': phi, } test_generator = PascalVocGenerator( 'datasets/VOC2007', 'test', shuffle_groups=False, skip_truncated=False, skip_difficult=True, **common_args ) model_path = 'checkpoints/2019-12-03/pascal_05_0.6283_1.1975_0.8029.h5' input_shape = (test_generator.image_size, test_generator.image_size) anchors = test_generator.anchors num_classes = test_generator.num_classes() model, prediction_model = efficientdet(phi=phi, num_classes=num_classes, weighted_bifpn=weighted_bifpn) prediction_model.load_weights(model_path, by_name=True) average_precisions = evaluate(test_generator, prediction_model, visualize=False) # compute per class average precision total_instances = [] precisions = [] for label, (average_precision, num_annotations) in average_precisions.items(): print('{:.0f} instances of class'.format(num_annotations), test_generator.label_to_name(label),
border_value=self.border_value) image, boxes = flipx(image, boxes, prob=self.flip_prob) image, boxes = crop(image, boxes, prob=self.crop_prob) image, boxes = translate(image, boxes, prob=self.translate_prob, border_value=self.border_value) return image, boxes if __name__ == '__main__': from generators.pascal import PascalVocGenerator train_generator = PascalVocGenerator('datasets/VOC0712', 'trainval', skip_difficult=True, batch_size=1, shuffle_groups=False) misc_effect = MiscEffect() for i in range(train_generator.size()): image = train_generator.load_image(i) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) annotations = train_generator.load_annotations(i) boxes = annotations['bboxes'] for box in boxes.astype(np.int32): cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 2) src_image = image.copy() # cv2.namedWindow('src_image', cv2.WINDOW_NORMAL) cv2.imshow('src_image', src_image) # image, boxes = misc_effect(image, boxes)
from generators.pascal import PascalVocGenerator from models.resnet import centernet import cv2 import os import numpy as np import time from generators.utils import affine_transform, get_affine_transform import os.path as osp os.environ['CUDA_VISIBLE_DEVICES'] = '0' generator = PascalVocGenerator( 'datasets/VOC2007', 'test', shuffle_groups=False, skip_truncated=False, skip_difficult=True, ) model_path = 'checkpoints/2019-11-10/pascal_81_1.5415_3.0741_0.6860_0.7057_0.7209_0.7290.h5' num_classes = generator.num_classes() classes = list(generator.classes.keys()) flip_test = True nms = True keep_resolution = False score_threshold = 0.1 colors = [np.random.randint(0, 256, 3).tolist() for i in range(num_classes)] model, prediction_model, debug_model = centernet( num_classes=num_classes, nms=nms, flip_test=flip_test, freeze_bn=True, score_threshold=score_threshold)
hm_pred, wh_pred, reg_pred, hm_true, wh_true, reg_true, reg_mask, indices = args hm_loss = focal_loss(hm_pred, hm_true) wh_loss = 0.1 * reg_l1_loss(wh_pred, wh_true, indices, reg_mask) reg_loss = reg_l1_loss(reg_pred, reg_true, indices, reg_mask) total_loss = hm_loss + wh_loss + reg_loss return total_loss if __name__ == '__main__': from generators.pascal import PascalVocGenerator from models.resnet import centernet import numpy as np generator = PascalVocGenerator('datasets/VOC0712', 'train', skip_difficult=True, shuffle_groups=False, batch_size=1) model, prediction_model, debug_model = centernet(num_classes=20) debug_model.load_weights( 'checkpoints/2019-11-06/pascal_31_1.0370_10.7204.h5', by_name=True, skip_mismatch=True) sess = tf.Session() for inputs, targets in generator: image_input, hm_true, wh_true, reg_true, reg_mask, indices = inputs hm_pred, wh_pred, reg_pred = debug_model.predict(image_input) np.save('debug/1106/hm_true', hm_true) np.save('debug/1106/wh_true', wh_true) np.save('debug/1106/reg_true', reg_true)
def create_generators(args): """ 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, "phi": args.phi, "detect_text": args.detect_text, "detect_quadrangle": args.detect_quadrangle, } # create random transform generator for augmenting training data if args.random_transform: misc_effect = MiscEffect() visual_effect = VisualEffect() else: misc_effect = None visual_effect = None if args.dataset_type == "pascal": from generators.pascal import PascalVocGenerator train_generator = PascalVocGenerator( args.pascal_path, "trainval", skip_difficult=True, misc_effect=misc_effect, visual_effect=visual_effect, **common_args, ) validation_generator = PascalVocGenerator( args.pascal_path, "val", skip_difficult=True, shuffle_groups=False, **common_args, ) elif args.dataset_type == "csv": from generators.csv_ import CSVGenerator train_generator = CSVGenerator( args.annotations_path, args.classes_path, misc_effect=misc_effect, visual_effect=visual_effect, **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 import CocoGenerator train_generator = CocoGenerator( args.coco_path, "train2017", misc_effect=misc_effect, visual_effect=visual_effect, group_method="random", **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
from generators.pascal import PascalVocGenerator from models.resnet import centernet import cv2 import os import numpy as np import time from generators.utils import affine_transform, get_affine_transform import os.path as osp os.environ['CUDA_VISIBLE_DEVICES'] = '0' generator = PascalVocGenerator( r'D:\datasets\voc_dataset', 'trainval', shuffle_groups=False, skip_truncated=False, skip_difficult=True, ) model_path = r'C:\work\keras-CenterNet\checkpoints\2021-04-21\pascal_01_153.2941_16.5124.h5' num_classes = generator.num_classes() classes = list(generator.classes.keys()) flip_test = True nms = True keep_resolution = False score_threshold = 0.1 colors = [np.random.randint(0, 256, 3).tolist() for i in range(num_classes)] model, prediction_model, debug_model = centernet(num_classes=num_classes, nms=nms, flip_test=flip_test, freeze_bn=True, score_threshold=score_threshold) prediction_model.load_weights(model_path, by_name=True, skip_mismatch=True)
from generators.csv import CSVGenerator from model import efficientdet import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 1 weighted_bifpn = False common_args = { 'batch_size': 1, 'phi': phi, } test_generator = PascalVocGenerator( 'datasets/config_folder/dataset/test.csv', 'datasets/config_folder/dataset/classes.csv', base_dir="D:/Study\Masters Data/Masters Thesis/HSU/EfficientDet-master", detect_quadrangle=False, detect_text=False, **kwargs) model_path = 'checkpoints/2020-06-21 (MD B1)/csv_100_0.1140_0.7599.h5' input_shape = (test_generator.image_size, test_generator.image_size) anchors = test_generator.anchors num_classes = test_generator.num_classes() model, prediction_model = efficientdet(phi=phi, num_classes=num_classes, weighted_bifpn=weighted_bifpn) prediction_model.load_weights(model_path, by_name=True) average_precisions = evaluate(test_generator, prediction_model, visualize=False) # compute per class average precision