def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # optionally load config parameters if args.config: args.config = read_config_file(args, 'debug') # make sure keras is the minimum required version check_keras_version() # create the generator generator = create_generator(args) # optionally load anchor parameters anchor_params = None if args.config and 'anchor_parameters' in args.config: anchor_params = parse_anchor_parameters(args.config) # create the display window #cv2.namedWindow('Image', cv2.WINDOW_NORMAL) if args.loop: while run(generator, args, anchor_params=anchor_params): pass else: run(generator, args, anchor_params=anchor_params)
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu keras.backend.tensorflow_backend.set_session(get_session()) # make save path if it doesn't exist if args.save_path is not None and not os.path.exists(args.save_path): os.makedirs(args.save_path) # create the generator generator = create_generator(args) # load the model print('Loading model, this may take a second...') model = keras.models.load_model(args.model, custom_objects=custom_objects) # print model summary print(model.summary()) loss = {'regression': losses.smooth_l1(), 'classification': losses.focal()} # start evaluation average_precisions = evaluate( generator, model, # loss, iou_threshold=args.iou_threshold, score_threshold=args.score_threshold, max_detections=args.max_detections, save_path=args.save_path) # print evaluation for label, average_precision in average_precisions.items(): print(generator.label_to_name(label), '{:.4f}'.format(average_precision)) print('mAP: {:.4f}'.format( sum(average_precisions.values()) / len(average_precisions)))
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu keras.backend.tensorflow_backend.set_session(get_session()) # create the model print('Loading model, this may take a second...') model = keras.models.load_model(args.model, custom_objects=custom_objects) # create a generator for testing data test_generator = CocoGenerator(args.coco_path, args.set) evaluate_coco(test_generator, model, args.score_threshold)
def main(config_file=None): cwd = os.getcwd() # parse configuration file if config_file is None: config_file = sys.argv[-1] config_file = os.path.join(cwd, config_file) config_file_name = config_file.split('/')[-1] configs = parse_config(config_file) # save config file if configs['Train']['save_configs']: # confirm save dir config_save_path = 'logs/' + configs['Name'] config_save_path = os.path.join(cwd, config_save_path) if not os.path.exists(config_save_path): os.mkdir(os.path.join(cwd, config_save_path)) # copy config file config_dst_name = configs['Name'] + '.json' config_file_dst = os.path.join(config_save_path, config_dst_name) shutil.copy(config_file, config_file_dst) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if configs['Train']['gpu']: os.environ['CUDA_VISIBLE_DEVICES'] = configs['Train']['gpu'] keras.backend.tensorflow_backend.set_session(get_session()) # create the generators train_generator, validation_generator = create_generators(configs) # create the model if configs['Train']['load_snapshot'] is not None: print('Loading model, this may take a second...') model = models.load_model(configs['Train']['load_snapshot'], backbone=configs['Train']['backbone']) training_model = prediction_model = model else: weights = configs['Train']['weights'] # default to imagenet if nothing else is specified if weights is None and configs['Train']['imagenet_weights']: weights = models.download_imagenet(configs['Train']['backbone']) print('Creating model, this may take a second...') backbone = configs['Train']['backbone'] num_classes = train_generator.num_classes() multi_gpu = configs['Train']['multi_gpu'] freeze_backbone = configs['Train']['freeze_backbone'] modifier = freeze_model if freeze_backbone else None # Keras recommends initialising a multi-gpu model on the CPU to ease weight sharing, and to prevent OOM errors. # optionally wrap in a parallel model if multi_gpu > 1: with tf.device('/cpu:0'): retinanet = models.retinanet_backbone( configs['Train']['backbone'])(num_classes, backbone=backbone, modifier=modifier) model = model_with_weights(retinanet, weights=weights, skip_mismatch=True) training_model = multi_gpu_model(model, gpus=multi_gpu) else: retinanet = models.retinanet_backbone( configs['Train']['backbone'])(num_classes, backbone=backbone, modifier=modifier) training_model = model = model_with_weights(retinanet, weights=weights, skip_mismatch=True) # make prediction model prediction_model = retinanet_bbox(model=model, anchor_param=configs['Anchors']) # compile model subnet_loss = { 'regression': losses.smooth_l1(), 'classification': losses.focal() } # run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) # run_metadata = tf.RunMetadata() if configs['Train']['lr_multiplier_layer']: optimizer = AdamWithLRMult( lr=configs['Train']['init_lr'], lr_multipliers=configs['Train']['lr_multiplier_layer'], debug_verbose=False, clipnorm=0.001) else: optimizer = keras.optimizers.adam(configs['Train']['init_lr'], clipnorm=0.001) training_model.compile(loss=subnet_loss, optimizer=optimizer) # training_model.compile(loss=subnet_loss, optimizer=keras.optimizers.adam(configs['Train']['init_lr'], clipnorm=0.001)) # this lets the generator compute backbone layer shapes using the actual backbone model if 'vgg' in configs['Train']['backbone'] or 'densenet' in configs['Train'][ 'backbone']: compute_anchor_targets = functools.partial( anchor_targets_bbox, shapes_callback=make_shapes_callback(model)) train_generator.compute_anchor_targets = compute_anchor_targets if validation_generator is not None: validation_generator.compute_anchor_targets = compute_anchor_targets # create the callbacks callbacks = create_callbacks( model, training_model, prediction_model, validation_generator, configs, ) # start training training_model.fit_generator( train_generator, validation_data=validation_generator, validation_steps=39, steps_per_epoch=configs['Train']['steps'], epochs=configs['Train']['epochs'], verbose=1, callbacks=callbacks, )
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # create object that stores backbone information backbone = models.backbone(args.backbone) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu keras.backend.tensorflow_backend.set_session(get_session()) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # create the generators train_generator, validation_generator = create_generators(args, backbone.preprocess_image) # create the model if args.snapshot is not None: print('Loading model, this may take a second...') # model = models.load_model(args.snapshot, backbone_name=args.backbone) model = model_with_weights(backbone.fsaf(train_generator.num_classes(), modifier=None), weights=args.snapshot, skip_mismatch=True) training_model = model prediction_model = fsaf_bbox(model=model) # compile model training_model.compile( loss={ 'cls_loss': lambda y_true, y_pred: y_pred, 'regr_loss': lambda y_true, y_pred: y_pred, }, # optimizer=keras.optimizers.sgd(lr=1e-5, momentum=0.9, nesterov=True, decay=1e-6) optimizer=keras.optimizers.adam(lr=1e-5) ) else: weights = args.weights # default to imagenet if nothing else is specified if weights is None and args.imagenet_weights: weights = backbone.download_imagenet() print('Creating model, this may take a second...') model, training_model, prediction_model = create_models( # backbone_retinanet=backbone.retinanet, backbone_retinanet=backbone.fsaf, num_classes=train_generator.num_classes(), weights=weights, num_gpus=args.num_gpus, freeze_backbone=args.freeze_backbone, lr=args.lr, config=args.config ) # print model summary # print(model.summary()) # this lets the generator compute backbone layer shapes using the actual backbone model if 'vgg' in args.backbone or 'densenet' in args.backbone: train_generator.compute_shapes = make_shapes_callback(model) if validation_generator: validation_generator.compute_shapes = train_generator.compute_shapes # create the callbacks callbacks = create_callbacks( model, training_model, prediction_model, validation_generator, args, ) if not args.compute_val_loss: validation_generator = None # start training return training_model.fit_generator( generator=train_generator, steps_per_epoch=args.steps, initial_epoch=9, epochs=args.epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=args.multiprocessing, max_queue_size=args.max_queue_size, validation_data=validation_generator )
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # optionally load config parameters if args.config: args.config = read_config_file(args, 'evaluation') #print("----------------------------------") #print("ARGUMENTS IN CONFIG FILE:") #for sec in args.config.sections(): #print(sec, "=", dict(args.config.items(sec))) #print("----------------------------------") # for arg in vars(args): # print(arg, "=", getattr(args, arg)) # exit() # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu keras.backend.tensorflow_backend.set_session(get_session()) # make save path if it doesn't exist if args.save_path is not None and not os.path.exists(args.save_path): os.makedirs(args.save_path) # create the generator generator = create_generator(args) # optionally load anchor parameters anchor_params = None if args.config and 'anchor_parameters' in args.config: anchor_params = parse_anchor_parameters(args.config) # load the model print('Loading model, this may take a second...') model = models.load_model(args.model, backbone_name=args.backbone) # optionally convert the model if args.convert_model: model = models.convert_model(model, anchor_params=anchor_params) # print model summary # print(model.summary()) # layer_outputs = [] # layer_names = ['res2c_relu', # C2 # 'res3b3_relu', # C3 # 'res4b22_relu', # C4 # 'P2', # P2 # 'P3', # P3 # 'P4', # P4 # # 'regression_submodel', # Subreg # # 'classification_submodel', # SubClas # 'regression', # Regression # 'classification'] # Classification # # for layer in model.layers: # if layer.name in layer_names: # print('------------------------------------------------------------------------------------------------------------------') # print('Layer found: ', layer.name) # print('\tOutput:', layer.output) # print('------------------------------------------------------------------------------------------------------------------') # layer_outputs.append(layer.output) # # image = preprocess_image(generator.load_image(0)) # image, scale = resize_image(image, args.image_min_side, args.image_max_side) # # activation_model = keras.Model(inputs=model.input, outputs=layer_outputs) # activations = activation_model.predict(np.expand_dims(image, axis=0)) # # def display_activation(activations, col_size, row_size, act_index): # activation = activations[act_index] # activation_index=0 # fig, ax = plt.subplots(row_size, col_size, figsize=(row_size*2.5,col_size*1.5)) # for row in range(0,row_size): # for col in range(0,col_size): # ax[row][col].imshow(activation[0, :, :, activation_index], cmap='gray') # activation_index += 1 # plt.savefig('layer_{}.png'.format(layer_names[act_index])) # # display_activation(activations, 8, 8, 0) # display_activation(activations, 8, 8, 1) # display_activation(activations, 8, 8, 2) # display_activation(activations, 8, 8, 3) # display_activation(activations, 8, 8, 4) # display_activation(activations, 8, 8, 5) # # exit() # start evaluation if args.dataset_type == 'coco': from ..utils.coco_eval import evaluate_coco evaluate_coco(generator, model, args.score_threshold) else: average_precisions = evaluate(generator, model, iou_threshold=args.iou_threshold, score_threshold=args.score_threshold, max_detections=args.max_detections, save_path=args.save_path, mask_base_path=args.mask_folder) # print evaluation total_instances = [] precisions = [] F1s = [] for label, (recall, precision, F1, average_precision, num_annotations) in average_precisions.items(): print('{:.0f} instances of class'.format(num_annotations), generator.label_to_name(label), 'with average precision: {:.4f}'.format(average_precision), 'precision: {:.4f}'.format(precision), 'recall: {:.4f}'.format(recall), 'and F1-score: {:.4f}'.format(F1)) total_instances.append(num_annotations) precisions.append(average_precision) F1s.append(F1) if sum(total_instances) == 0: print('No test instances found.') return print( 'mAP using the weighted average of precisions among classes: {:.4f}' .format( sum([a * b for a, b in zip(total_instances, precisions)]) / sum(total_instances))) print('mAP: {:.4f}'.format( sum(precisions) / sum(x > 0 for x in total_instances))) print('mF1: {:.4f}'.format( sum(F1s) / sum(x > 0 for x in total_instances)))
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu keras.backend.tensorflow_backend.set_session(get_session()) # make save path if it doesn't exist if args.save_path is not None and not os.path.exists(args.save_path): os.makedirs(args.save_path) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # create the generator generator = create_generator(args) # optionally load anchor parameters anchor_params = None if args.config and 'anchor_parameters' in args.config: anchor_params = parse_anchor_parameters(args.config) # load the model print('Loading model, this may take a second...') model = models.load_model(args.model, backbone_name=args.backbone) # optionally convert the model if args.convert_model: model = models.convert_model(model, anchor_params=anchor_params) # print model summary # print(model.summary()) # start evaluation if args.dataset_type == 'coco': from ..utils.coco_eval import evaluate_coco evaluate_coco(generator, model, args.score_threshold) else: average_precisions = evaluate(generator, model, iou_threshold=args.iou_threshold, score_threshold=args.score_threshold, max_detections=args.max_detections, save_path=args.save_path) # print evaluation total_instances = [] precisions = [] for label, (average_precision, num_annotations) in average_precisions.items(): print('{:.0f} instances of class'.format(num_annotations), generator.label_to_name(label), 'with average precision: {:.4f}'.format(average_precision)) total_instances.append(num_annotations) precisions.append(average_precision) if sum(total_instances) == 0: print('No test instances found.') return print( 'mAP using the weighted average of precisions among classes: {:.4f}' .format( sum([a * b for a, b in zip(total_instances, precisions)]) / sum(total_instances))) print('mAP: {:.4f}'.format( sum(precisions) / sum(x > 0 for x in total_instances))) return precisions, total_instances
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # create object that stores backbone information #backbone = models.backbone(args.backbone) backbone = models.backbone(args.backbone) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu keras.backend.tensorflow_backend.set_session(get_session()) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # create the generators #print(args) train_generator, validation_generator = create_generators( args, backbone.preprocess_image) # Log configs #run.log('batch-size', args.batch_size) #run.log('gamma', args.fl_gamma) #run.log('alpha', args.fl_alpha) #run.log('lr', args.lr) #run.log('neg-overlap', args.neg_overlap) #run.log('pos-overlap', args.pos_overlap) #run.log('fpn-layers', args.fpn_layers) if args.class_weights is not None: if args.class_weights == "cw1": polyp_weight = 0.25 #class_weights = {'classification': {0:a_w, 1:a_w, 2:a_w, 3:a_w, 4:a_w, 5:a_w, 6:a_w, 7:polyp_weight}, 'regression': {0:0.25, 1:0.25, 2:0.25, 3:0.25}} #class_weights = {'classification': [polyp_weight, a_w, a_w, a_w, a_w, a_w, a_w, a_w]} elif args.class_weights == "cw2": polyp_weight = 0.5 elif args.class_weights == "cw3": polyp_weight = 0.75 a_w = (1 - polyp_weight) / 7 #class_weights = {'classification': [polyp_weight, a_w, a_w, a_w, a_w, a_w, a_w, a_w]} class_weights = [polyp_weight, a_w, a_w, a_w, a_w, a_w, a_w, a_w] else: class_weights = None if args.loss_weights is None: loss_weights = [1, 1] elif args.loss_weights == "lw0": loss_weights = [1, 1, 1] elif args.loss_weights == "lw1": loss_weights = [1, 1, 3] elif args.loss_weights == "lw2": loss_weights = [1, 1, 10] elif args.loss_weights == "lw3": loss_weights = [1, 1, 20] # create the model if args.snapshot is not None: print('Loading model, this may take a second...') model = models.load_model(os.path.join(args.data_dir, args.snapshot), backbone_name=args.backbone) training_model = model anchor_params = None if args.config and 'anchor_parameters' in args.config: anchor_params = parse_anchor_parameters(args.config) prediction_model = retinanet_bbox(model=model, anchor_params=anchor_params) else: if args.weights is None and args.imagenet_weights: weights = backbone.download_imagenet() else: weights = args.weights # default to imagenet if nothing else is specified ## SO the file that is downloaded is actually only the weights ## this means that I should be able to use --weights to give it my own model sample_test = np.array([[0.25, 0.25, 0.25, 0.25, 0, 0, 0, 0], [10, 10, 10, 10, 10, 10, 10, 10]]) print('Creating model, this may take a second...') model, training_model, prediction_model = create_models( backbone_retinanet=backbone.retinanet, num_classes=train_generator.num_classes(), weights=weights, class_weights=class_weights, loss_weights=loss_weights, multi_gpu=args.multi_gpu, freeze_backbone=args.freeze_backbone, lr=args.lr, config=args.config, fl_gamma=args.fl_gamma, fl_alpha=args.fl_alpha, c_weight=args.c_weight, r_weight=args.r_weight, p_weight=args.p_weight, train_type=args.train_type, sample_t=sample_test) # print model summary #print(model.summary()) # this lets the generator compute backbone layer shapes using the actual backbone model if 'vgg' in args.backbone or 'densenet' in args.backbone: train_generator.compute_shapes = make_shapes_callback(model) if validation_generator: validation_generator.compute_shapes = train_generator.compute_shapes # create the callbacks callbacks = create_callbacks( model, training_model, prediction_model, validation_generator, args, ) # Use multiprocessing if workers > 0 if args.workers > 0: use_multiprocessing = True else: use_multiprocessing = False temp_df = pd.read_csv( os.path.join(args.data_dir, args.annotations), names=["image_path", "x1", "y1", "x2", "y2", "object_id"]) im_count = len(set(list(temp_df.image_path))) # start training training_model.fit_generator(generator=train_generator, steps_per_epoch=int(im_count / args.batch_size), epochs=args.epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=use_multiprocessing, max_queue_size=args.max_queue_size #class_weight=class_weights )
def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # optionally load config parameters if args.config: args.config = read_config_file(args, 'training') # print("----------------------------------") # print("ARGUMENTS IN CONFIG FILE:") # for sec in args.config.sections(): # print(sec, "=", dict(args.config.items(sec))) # print("----------------------------------") # # for arg in vars(args): # print(arg, "=", getattr(args, arg)) # exit() # create object that stores backbone information backbone = models.backbone(args.backbone) # make sure keras is the minimum required version check_keras_version() # optionally choose specific GPU if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu keras.backend.tensorflow_backend.set_session(get_session()) # create the generators train_generator, validation_generator = create_generators( args, backbone.preprocess_image) # # Debuging # for i in range(1): # inputs, targets = train_generator.__getitem__(i) # exit() # create the model if args.snapshot is not None: print('Loading model, this may take a second...') model = models.load_model(args.snapshot, backbone_name=args.backbone) # When using as a second step for fine-tuning for layer in model.layers: layer.trainable = True training_model = model anchor_params = None if args.config and 'anchor_parameters' in args.config: anchor_params = parse_anchor_parameters(args.config) prediction_model = retinanet_bbox(model=model, anchor_params=anchor_params) ###################################################################################### BRUNO # compile model training_model.compile(loss={ 'regression': losses.smooth_l1(), 'classification': losses.focal() }, optimizer=keras.optimizers.adam(lr=args.lr, clipnorm=0.001)) else: weights = args.weights # default to imagenet if nothing else is specified if weights is None and args.imagenet_weights: weights = backbone.download_imagenet() print('Creating model, this may take a second...') model, training_model, prediction_model = create_models( backbone_retinanet=backbone.retinanet, num_classes=train_generator.num_classes(), weights=weights, multi_gpu=args.multi_gpu, freeze_backbone=args.freeze_backbone, lr=args.lr, config=args.config) # Print model design # print(model.summary()) # print(training_model.summary()) # plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True) # exit() # Get the number of samples in the training and validations datasets. train_size = train_generator.size() val_size = validation_generator.size() print('Train on {} samples, val on {} samples, with batch size {}.'.format( train_size, val_size, args.batch_size)) # this lets the generator compute backbone layer shapes using the actual backbone model if 'vgg' in args.backbone or 'densenet' in args.backbone: train_generator.compute_shapes = make_shapes_callback(model) if validation_generator: validation_generator.compute_shapes = train_generator.compute_shapes # create the callbacks callbacks = create_callbacks( model, training_model, prediction_model, validation_generator, train_size, [1e-6, 1e-4], args, ) # Use multiprocessing if workers > 0 if args.workers > 0: use_multiprocessing = True else: use_multiprocessing = False # check to see if we are attempting to find an optimal learning rate # before training for the full number of epochs if args.find_lr: # initialize the learning rate finder and then train with learning # rates ranging from 1e-10 to 1e+1 print("[INFO] Finding learning rate...") lrf = LearningRateFinder(training_model) lrf.find(train_generator, 1e-10, 1e+1, stepsPerEpoch=np.ceil((train_size / float(args.batch_size))), batchSize=args.batch_size) # plot the loss for the various learning rates and save the # resulting plot to disk lrf.plot_loss() plt.savefig("lrfind_plot.png") # save values into a csv file lrf.save_csv("lr_loss.csv") # gracefully exit the script so we can adjust our learning rates # in the config and then train the network for our full set of # epochs print("[INFO] Learning rate finder complete") print("[INFO] Examine plot and adjust learning rates before training") sys.exit(0) # Number of epochs and steps for training new layers n_epochs = 350 steps = train_size // args.batch_size # start training training_model.fit_generator(generator=train_generator, validation_data=validation_generator, steps_per_epoch=steps, epochs=n_epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=use_multiprocessing, max_queue_size=args.max_queue_size) # Unfreeze and continue training, to fine-tune. # Train longer if the result is not good. if True: # for layer in model.layers: # if layer.name is 'bn_conv1': # print("Before\t-> Trainable: {}, Freeze: {}".format(layer.trainable, layer.freeze)) for layer in model.layers: layer.trainable = True # recompile to apply the change model.compile( loss={ 'regression': losses.smooth_l1(), 'classification': losses.focal() }, # Learning rate must be lower for training the entire network optimizer=keras.optimizers.adam(lr=args.lr * 0.1, clipnorm=0.001), metrics=['accuracy']) if args.multi_gpu > 1: from keras.utils import multi_gpu_model with tf.device('/gpu:1'): training_model = multi_gpu_model(model, gpus=args.multi_gpu) else: training_model = model # recompile to apply the change training_model.compile( loss={ 'regression': losses.smooth_l1(), 'classification': losses.focal() }, # Learning rate must be lower for training the entire network optimizer=keras.optimizers.adam(lr=args.lr * 0.1, clipnorm=0.001), metrics=['accuracy']) print('Unfreezing all layers.') # for layer in model.layers: # if layer.name is 'bn_conv1': # print("After\t-> Trainable: {}, Freeze: {}".format(layer.trainable, layer.freeze)) # Print training_model design # print(model.summary()) # print(training_model.summary()) # create the callbacks callbacks = create_callbacks( model, training_model, prediction_model, validation_generator, train_size, [1e-8, 1e-6], args, ) batch_size = 2 # note that more GPU memory is required after unfreezing the body steps = train_size // batch_size print('Train on {} samples, val on {} samples, with batch size {}.'. format(train_size, val_size, batch_size)) training_model.fit_generator(generator=train_generator, validation_data=validation_generator, steps_per_epoch=steps, epochs=args.epochs, initial_epoch=n_epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=use_multiprocessing, max_queue_size=args.max_queue_size)
def main(args=None): iou_thres_vec = [] if sys.argv[1:] == []: print('Please give at least one value for IoU threshold.') exit(0) for v in sys.argv[1:]: iou_thres_vec.append(float(v)) print('Doing job for IoU values:', iou_thres_vec) # make sure keras is the minimum required version check_keras_version() # define base paths model_base_path = '/path/snapshots/' data_base_path = '/path/dataset/' out_imgs_base_path = '/path/out_imgs/' # define number of experiments and k-folds experiments = ['1'] #,'2','3','4'] kfolds = ['1'] #,'2','3','4','5'] # loop for each experiment and k-fold for exp in experiments: # set experiment name if exp == '1': data_name = 'cns_stratified' elif exp == '2': data_name = 'cns_unseen_split' elif exp == '3': data_name = 'all_stratified' elif exp == '4': data_name = 'all_unseen_split' for kf in kfolds: # set names model_name = 'resnet101_fpn4_1000_sc4_ar3_cycLRexp-8_allDataAugm_350-450_exp' + exp + '_k' + kf model_path = model_base_path + model_name + '/resnet101_ivm.h5' data_path = data_base_path + 'image_sets/' + data_name + '/k_' + kf save_path = out_imgs_base_path + model_name # copy data image_set to data_base_path files = os.listdir(data_path) for f in files: if f.endswith('.txt'): f = data_path + '/' + f dst = data_base_path + 'image_sets/' # print('File copied\n\tFROM:', f, '\n\tTO:', dst) shutil.copy(f, dst) # optionally choose specific GPU os.environ['CUDA_VISIBLE_DEVICES'] = '0' keras.backend.tensorflow_backend.set_session(get_session()) # load the model print('Loading model, this may take a second...') model = models.load_model(model_path, backbone_name='resnet101') # convert the model model = models.convert_model(model) print('Loaded.') # create the generator generator = IVMGenerator(data_base_path, 'val', image_min_side=1000, image_max_side=1400) # # Grid search # md = 500 nms_thres_vec = np.arange(.05, 1., .05) score_thres_vec = np.arange(.05, 1., .05) # output values outputs = [] # varying params for it in iou_thres_vec: for nt in nms_thres_vec: for st in score_thres_vec: evaluate_model(generator=generator, model=model, nt=nt, it=it, st=st, md=md, save_path=None, mask='/path/dataset/all/masks', output=outputs) # save in data frame format df = pd.DataFrame(data=outputs) # ensure directory created first and save file makedirs(save_path) out_path = save_path + '/params_search.csv' df.to_csv(out_path, index=None, header=True) # # Get best set of parameters # # compute measure avg_measure = (df['f1_score'] + df['average_precision']) / 2 df['avg_measure'] = avg_measure # set best param values best = df.iloc[df['avg_measure'].idxmax()] md = best['max_detections'].astype(int) it = best['iou_threshold'] nt = best['nms_threshold'] st = best['score_threshold'] # create the generator for test dataset generator = IVMGenerator(data_base_path, 'test', image_min_side=1000, image_max_side=1400) # perform inference in test dataset outputs = [] # varying params evaluate_model(generator=generator, model=model, nt=nt, it=it, st=st, md=md, save_path=save_path, mask='/path/dataset/all/masks', output=outputs) # save in data frame format df = pd.DataFrame(data=outputs) out_path = save_path + '/best_output.csv' df.to_csv(out_path, index=None, header=True)