def main(args=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # create the generator generator = create_generator(args) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # 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): pass else: run(generator, args, 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()) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # 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 = models.load_model(args.model, backbone_name=args.backbone) # 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, binarize_threshold=args.binarize_threshold, 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 args.weighted_average: print('mAP: {:.4f}'.format(sum([a * b for a, b in zip(total_instances, precisions)]) / sum(total_instances))) else: print('mAP: {:.4f}'.format(sum(precisions) / 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) # create object that stores backbone information backbone = models.backbone(args.backbone) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # create the generators train_generator = create_generators(args, backbone.preprocess_image) # create the model if args.snapshot is not None: print('Loading model') model = models.load_model(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: weights = args.weights if weights is None and args.imagenet_weights: weights = backbone.download_imagenet() print('Creating model') 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) # this lets the generator compute backbone layer shapes using the actual backbone model if 'densenet' in args.backbone: train_generator.compute_shapes = make_shapes_callback(model) # create the callbacks callbacks = create_callbacks(model, training_model, prediction_model, args) # start training return training_model.fit_generator( generator=train_generator, steps_per_epoch=train_generator.size() // args.batch_size, epochs=args.epochs, verbose=1, callbacks=callbacks)
def test_config_read(): config = read_config_file('tests/test-data/config/config.ini') assert 'anchor_parameters' in config assert 'sizes' in config['anchor_parameters'] assert 'strides' in config['anchor_parameters'] assert 'ratios' in config['anchor_parameters'] assert 'scales' in config['anchor_parameters'] assert config['anchor_parameters']['sizes'] == '32 64 128 256 512' assert config['anchor_parameters']['strides'] == '8 16 32 64 128' assert config['anchor_parameters']['ratios'] == '0.5 1 2 3' assert config['anchor_parameters']['scales'] == '1 1.2 1.6'
def test_config_read(): config = read_config_file("tests/test-data/config/config.ini") assert "anchor_parameters" in config assert "sizes" in config["anchor_parameters"] assert "strides" in config["anchor_parameters"] assert "ratios" in config["anchor_parameters"] assert "scales" in config["anchor_parameters"] assert config["anchor_parameters"]["sizes"] == "32 64 128 256 512" assert config["anchor_parameters"]["strides"] == "8 16 32 64 128" assert config["anchor_parameters"]["ratios"] == "0.5 1 2 3" assert config["anchor_parameters"]["scales"] == "1 1.2 1.6"
def get_joint_detection_model(model_path, model_type): """ Input -> Model path for the object detection model Model type-> Foot or Hand Output -> Inference model for getting the predictions on test images """ # config_file_path = '/usr/local/bin/config' if model_type == 'Foot_detection': # with open('/usr/local/bin/src/config.ini','w') as f: # f.write('[anchor_parameters]\nsizes = 32 64 128 256 512 1024\nstrides = 8 16 32 64 128 256\nratios = 1.2 1.5 2 2.5 3\nscales =1 1.5 2\n') model, training_model, prediction_model = create_models( backbone_retinanet=backbone('resnet50').retinanet, num_classes=5, weights=None, multi_gpu=False, freeze_backbone=True, lr=1e-3, config=read_config_file('/usr/local/bin/Config files/config_foot.ini')) training_model.load_weights(model_path) infer_model = convert_model(training_model, anchor_params = parse_anchor_parameters(read_config_file('/usr/local/bin/Config files/config_foot.ini'))) elif model_type == 'Hand_detection': # with open('/usr/local/bin/src/config.ini','w') as f: # f.write('[anchor_parameters]\nsizes = 32 64 128 256 512 1024\nstrides = 8 16 32 64 128 256\nratios = 1 1.5 2 2.5 3\nscales = 1 1.2 1.6\n') model, training_model, prediction_model = create_models( backbone_retinanet=backbone('resnet50').retinanet, num_classes=6, weights=None, multi_gpu=False, freeze_backbone=True, lr=1e-3, config=read_config_file('/usr/local/bin/Config files/config_hand.ini')) training_model.load_weights(model_path) infer_model = convert_model(training_model, anchor_params = parse_anchor_parameters(read_config_file('/usr/local/bin/Config files/config_hand.ini'))) return infer_model
def from_conf(cls, conf_path: Optional[str] = None) -> 'AnchorParametersWrap': ret: Optional['AnchorParametersWrap'] = None if conf_path: config = read_config_file(conf_path) anchors = parse_anchor_parameters(config) ret = cls(anchors) log.info(f'loaded anchors from {conf_path}') else: log.info(f'using default anchors') ret = cls() return ret
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()) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # create the generators generator = create_generators(args) # create the model print('Loading model, this may take a second...') model = models.load_model(args.snapshot, backbone_name=args.backbone) 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) # print model summary prediction_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: generator.compute_shapes = make_shapes_callback(model) # inference boxes, scores, labels = prediction_model.predict_generator( generator=generator, verbose=1, ) # generate COCO annotations create_coco_annotations(boxes, scores, labels, generator.image_ids)
def run(args=None): args = parse_args(args) backbone = models.backbone(args.backbone) if args.config: args.config = read_config_file(args.config) _, generator = create_generators(args, backbone.preprocess_image) weights = os.path.join(args.snapshot_path, 'checkpoint.h5') _, _, model = create_models( backbone_retinanet=backbone.retinanet, num_classes=10, weights=weights, multi_gpu=args.multi_gpu, freeze_backbone=args.freeze_backbone, lr=args.lr, config=args.config ) infer(generator, model)
class args: batch_size =4 config = read_config_file('config.ini') random_transform = True # Image augmentation annotations = "C:\\Users\\Pawan\\Documents\\ML\\annotations_train_modified2.csv" val_annotations = "C:\\Users\\Pawan\\Documents\\ML\\annotations_test_modified2.csv" no_resize=False classes = "C:\\Users\\Pawan\\Documents\\ML\\classes_train_modified2.csv" image_min_side = 672 image_max_side = 672 dataset_type = 'csv' tensorboard_dir = 'C:\\Users\\Pawan\\Documents\\Tensorboard' evaluation = True snapshots = True snapshot_path = "C:\\Users\\Pawan\\Documents\\ML\\snapshots12" backbone = 'resnet50' epochs = 100 steps = 10755//(batch_size) gpu=0 resize=True
def __init__(self, csv_data_file, csv_class_file, base_dir=None, transform_generator=None, batch_size=2, group_method='none', # one of 'none', 'random', 'ratio' shuffle_groups=True, image_min_side=480, image_max_side=640, transform_parameters=None, compute_anchor_targets=anchor_targets_bbox, compute_shapes=guess_shapes, preprocess_image=preprocess_image, config=None ): """ Initialize a CSV data generator. Args for csv csv_data_file: Path to the CSV annotations file. csv_class_file: Path to the CSV classes file. base_dir: Directory w.r.t. where the files are to be searched (defaults to the directory containing the csv_data_file). Args for generator transform_generator : A generator used to randomly transform images and annotations. batch_size : The size of the batches to generate. group_method : Determines how images are grouped together (defaults to 'ratio', one of ('none', 'random', 'ratio')). shuffle_groups : If True, shuffles the groups each epoch. image_min_side : After resizing the minimum side of an image is equal to image_min_side. image_max_side : If after resizing the maximum side is larger than image_max_side, scales down further so that the max side is equal to image_max_side. transform_parameters : The transform parameters used for data augmentation. compute_anchor_targets : Function handler for computing the targets of anchors for an image and its annotations. compute_shapes : Function handler for computing the shapes of the pyramid for a given input. preprocess_image : Function handler for preprocessing an image (scaling / normalizing) for passing through a network. """ self.image_names = [] self.image_data = {} self.base_dir = base_dir self.layout_2_group_table = {} # take base_dir from annotations file if not explicitly specified. if self.base_dir is None: self.base_dir = os.path.dirname(csv_data_file) # parse the provided class file try: with _open_for_csv(csv_class_file) as file: self.classes = _read_classes(csv.reader(file, delimiter=',')) except ValueError as e: raise_from(ValueError('invalid CSV class file: {}: {}'.format(csv_class_file, e)), None) self.labels = {} for key, value in self.classes.items(): self.labels[value] = key # csv with img_path, x1, y1, x2, y2, class_name try: with _open_for_csv(csv_data_file) as file: self.image_data = _read_annotations(csv.reader(file, delimiter=','), self.classes) except ValueError as e: raise_from(ValueError('invalid CSV annotations file: {}: {}'.format(csv_data_file, e)), None) self.image_names = list(self.image_data.keys()) self.transform_generator = transform_generator self.batch_size = int(batch_size) self.group_method = group_method self.shuffle_groups = shuffle_groups self.image_min_side = image_min_side self.image_max_side = image_max_side self.transform_parameters = transform_parameters or TransformParameters() self.compute_anchor_targets = compute_anchor_targets self.compute_shapes = compute_shapes self.preprocess_image = preprocess_image if config is not None: self.config = read_config_file(config) else: self.config = None self.group_index = 0 self.lock = threading.Lock() self.group_images() self.layout_lookup()
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.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) 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: 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 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, eval_batch_size=args.val_batch_size) # Use multiprocessing if workers > 0 if args.workers > 0: use_multiprocessing = True else: use_multiprocessing = False # start training training_model.fit_generator(generator=train_generator, steps_per_epoch=args.steps, epochs=args.epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=use_multiprocessing, max_queue_size=args.max_queue_size)
def main(args=None, model_filename=None): # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) # make sure keras and tensorflow are the minimum required version check_keras_version() check_tf_version() # optionally choose specific GPU if args.gpu: setup_gpu(args.gpu) # 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 backbone = models.backbone(args.backbone) generator = create_generator(args, backbone.preprocess_image) # 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...') if args.continual_learning_model == 'dual_memory': # Continual learning dual-memory modelling treatment base_models = LoadModels(args.historical_snapshots_folder, args.backbone, args.day_number) all_models = [] for model in base_models: generator.compute_shapes = make_shapes_callback(model) if args.convert_model: model = models.convert_model(model, anchor_params=anchor_params) all_models.append(model) (average_precisions, inference_time, detections_per_model, final_detections) = evaluate_dual_memory_model( generator, all_models, iou_threshold=args.iou_threshold, score_threshold=args.score_threshold, max_detections=args.max_detections, save_path=args.save_path) # bbox_savepath given, save bounding box coordinates from dual-memory model predictions: if args.bbox_savepath: detections_per_model = [[ [class_predictions.tolist() for class_predictions in image] for image in model_predictions ] for model_predictions in detections_per_model] detections_with_filenames = { 'final_detections': final_detections, 'annotations': args.annotations, 'detections_per_model': detections_per_model } with open(args.bbox_savepath, 'wt') as outf: json.dump(detections_with_filenames, outf) print("Finished dual memory model") print(average_precisions, inference_time) else: if model_filename is None: model_filename = args.model model = models.load_model(model_filename, backbone_name=args.backbone) generator.compute_shapes = make_shapes_callback(model) # 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, inference_time = 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 = [] #labels = [] 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)) #labels.append(label) total_instances.append(num_annotations) precisions.append(average_precision) if sum(total_instances) == 0: print('No test instances found.') return print('Inference time for {:.0f} images: {:.4f}'.format( generator.size(), inference_time)) 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(labels) print(precisions) print(total_instances) # Save mAP and other accuracy statistics to mAP_savepath: mAP = sum(precisions) / sum(x > 0 for x in total_instances) date = datetime.now().strftime("%Y%m%d%H%M") with open(args.mAP_savepath, 'a') as outf: outf.write( f"{date}, {mAP}, {precisions}, {total_instances}, {model_filename}, {args.continual_learning_model}" + "\n") return mAP
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 = load_model(args.model, backbone_name=args.backbone) # optionally convert the model if args.convert_model: model = convert_model(model=model, nms_threshold=args.nms_threshold, score_threshold=args.nms_score, max_detections=args.nms_detections, 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, pr_curves = 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 = [] f1_scores = [] mean_ious = [] 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) f1_scores.append(max(pr_curves[label]['f1_score'])) mean_ious.append(np.mean(pr_curves[label]['average_iou'])) 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))) mean_ap = sum(precisions) / sum(x > 0 for x in total_instances) mean_f1 = sum(f1_scores) / sum(x > 0 for x in total_instances) mean_iou = sum(mean_ious) / sum(x > 0 for x in total_instances) for label in range(generator.num_classes()): class_label = generator.label_to_name(label) instances = int(total_instances[label]) predictions = len(pr_curves[label]['precision']) true_positives = int(pr_curves[label]['TP'][-1]) if len( pr_curves[label]['TP']) > 0 else 0 false_positives = int(pr_curves[label]['FP'][-1]) if len( pr_curves[label]['FP']) > 0 else 0 print( '\nClass {}: Instances: {} | Predictions: {} | False positives: {} | True positives: {}' .format(class_label, instances, predictions, false_positives, true_positives)) print('mAP: {:.4f}'.format(mean_ap), 'mF1-score: {:.4f}'.format(mean_f1), 'mIoU: {:.4f}'.format(mean_iou)) print(args.save_path) print('Saving results: ') f = open(args.save_path + "result.txt", "w+") f.write('mAP: {:.4f}; '.format(mean_ap)) f.write('mF1-score: {:.4f}; '.format(mean_f1)) f.write('mIoU: {:.4f}'.format(mean_iou)) f.close() # save stats if args.logs: makedirs(args.logs) np.save(os.path.join(args.logs, 'pr_curves'), pr_curves)
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() # create object that stores backbone information backbone = models.backbone(args.backbone) # optionally choose specific GPU if args.gpu: setup_gpu(args.gpu) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # create the generators train_generator, validation_generator = create_generators(args) # 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) training_model = model prediction_model = model else: weights = args.weights # default to imagenet if nothing else is specified if weights is None and args.imagenet_weights: weights = backbone.download_imagenet() anchor_params = None if args.config and 'anchor_parameters' in args.config: anchor_params = parse_anchor_parameters(args.config) print('Creating model, this may take a second...') model, training_model, prediction_model = create_models( backbone_retinanet=backbone.maskrcnn, num_classes=train_generator.num_classes(), weights=weights, freeze_backbone=args.freeze_backbone, class_specific_filter=args.class_specific_filter, anchor_params=anchor_params ) # print model summary print(model.summary()) # 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 # start training training_model.fit_generator( generator=train_generator, steps_per_epoch=args.steps, epochs=args.epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=use_multiprocessing, max_queue_size=args.max_queue_size )
def main(forest_object, args=None, input_type="fit_generator", list_of_tfrecords=None, comet_experiment=None): """ Main Training Loop Args: forest_object: a deepforest class object args: Keras retinanet argparse list_of_tfrecords: list of tfrecords to parse input_type: "fit_generator" or "tfrecord" input type """ # 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: setup_gpu(args.gpu) # optionally load config parameters if args.config: args.config = read_config_file(args.config) # data input if input_type == "fit_generator": # create the generators train_generator, validation_generator = create_generators( args, backbone.preprocess_image) # placeholder target tensor for creating models targets = None elif input_type == "tfrecord": # Create tensorflow iterators iterator = tfrecords.create_dataset(list_of_tfrecords, args.batch_size) next_element = iterator.get_next() # Split into inputs and targets inputs = next_element[0] targets = [next_element[1], next_element[2]] validation_generator = None else: raise ValueError( "{} input type is invalid. Only 'tfrecord' or 'for_generator' " "input types are accepted for model training".format(input_type)) # 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) 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: 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...') if input_type == "fit_generator": num_of_classes = train_generator.num_classes() else: # Add background class num_of_classes = len(forest_object.labels.keys()) model, training_model, prediction_model = create_models( backbone_retinanet=backbone.retinanet, num_classes=num_of_classes, weights=weights, multi_gpu=args.multi_gpu, freeze_backbone=args.freeze_backbone, lr=args.lr, config=args.config, targets=targets, freeze_layers=args.freeze_layers) # 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, comet_experiment) if not args.compute_val_loss: validation_generator = None # start training if input_type == "fit_generator": history = training_model.fit_generator( generator=train_generator, steps_per_epoch=args.steps, 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) elif input_type == "tfrecord": # Fit model history = training_model.fit(x=inputs, steps_per_epoch=args.steps, epochs=args.epochs, callbacks=callbacks) else: raise ValueError( "{} input type is invalid. Only 'tfrecord' or 'for_generator' " "input types are accepted for model training".format(input_type)) # Assign history to deepforest model class forest_object.history = history # return trained model return model, prediction_model, training_model
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 and tensorflow are the minimum required version check_keras_version() check_tf_version() # optionally choose specific GPU if args.gpu is not None: setup_gpu(args.gpu) # 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) 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: 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, regularisation=args.regularisation ) # 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, 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, initial_epoch=args.initial_epoch )
def train(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) 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: 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, config=args.config) # print model summary print(model.summary()) # create the callbacks callbacks = create_callbacks( model, training_model, prediction_model, validation_generator, args, ) # start training history = training_model.fit_generator( generator=train_generator, validation_data=validation_generator, validation_steps=args.vsteps, steps_per_epoch=args.steps, epochs=args.epochs, verbose=1, callbacks=callbacks, ) return history
train_gen,valid_gen = create_generators(args,b.preprocess_image) model, training_model, prediction_model = create_models( backbone_retinanet=b.retinanet, num_classes=train_gen.num_classes(), weights=None, multi_gpu=True, freeze_backbone=True, lr=1e-9, config=args.config ) training_model.load_weights("C:\\Users\\Pawan\\Documents\\ML\\snapshots12\\resnet50_csv_07.h5") infer_model = convert_model(training_model,anchor_params=parse_anchor_parameters(read_config_file('C:\\Users\\Pawan\\Documents\\config.ini'))) def test_gen(image_ids, bs = 2, size=672,test = True): imgs = [] scale = None idx = 0 if test: path = 'C:\\Users\\Pawan\\Downloads\\dataset_test_rgb\\rgb\\test\\' else: path = 'C:\\Users\\Pawan\\Downloads\\dataset_test_rgb\\rgb\\test\\' while idx < len(image_ids): if len(imgs) < bs: imgs.append(resize_image(preprocess_image(read_image_bgr(path + image_ids[idx] + '.png')),min_side=size,max_side=size)[0]) if scale is None:
def main(args=None): import json with open( os.path.expanduser('~') + '/.maskrcnn-modanet/' + 'savedvars.json') as f: savedvars = json.load(f) # parse arguments if args is None: print( '\n\n\nExample usage: maskrcnn-modanet train --epochs 15 --workers 0 --batch-size 1 coco\n\n\n' ) args = ['-h'] args = parse_args(args, savedvars) # make sure keras is the minimum required version check_keras_version() # create object that stores backbone information backbone = models.backbone(args.backbone) # 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) # 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) training_model = model prediction_model = model else: weights = args.weights # default to imagenet if nothing else is specified if weights is None and args.imagenet_weights: weights = backbone.download_imagenet() anchor_params = None if args.config and 'anchor_parameters' in args.config: anchor_params = parse_anchor_parameters(args.config) print('Creating model, this may take a second...') model, training_model, prediction_model = create_models( backbone_retinanet=backbone.maskrcnn, num_classes=train_generator.num_classes(), weights=weights, freeze_backbone=args.freeze_backbone, class_specific_filter=args.class_specific_filter, anchor_params=anchor_params) # print model summary print(model.summary()) # 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 # start training training_model.fit_generator(generator=train_generator, steps_per_epoch=args.steps, epochs=args.epochs, verbose=1, callbacks=callbacks, workers=args.workers, use_multiprocessing=use_multiprocessing, max_queue_size=args.max_queue_size)