def create_models(num_classes, weights='imagenet', multi_gpu=0, snapshot_start=None): # create "base" model (no NMS) image = keras.layers.Input((None, None, 3)) # 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'): if snapshot_start == None: model = ResNet50RetinaNet(image, num_classes=num_classes, weights=weights, nms=False) else: model = snapshot_start training_model = multi_gpu_model(model, gpus=multi_gpu) else: if snapshot_start == None: model = ResNet50RetinaNet(image, num_classes=num_classes, weights=weights, nms=False) else: model = snapshot_start training_model = model # append NMS for prediction only classification = model.outputs[1] detections = model.outputs[2] boxes = keras.layers.Lambda(lambda x: x[:, :, :4])(detections) detections = keras_retinanet.layers.NonMaximumSuppression(name='nms')( [boxes, classification, detections]) prediction_model = keras.models.Model(inputs=model.inputs, outputs=model.outputs[:2] + [detections]) # Kai - check if we still need to compile if restarting from a snapshot # compile model training_model.compile(loss={ 'regression': keras_retinanet.losses.smooth_l1(), 'classification': keras_retinanet.losses.focal() }, optimizer=keras.optimizers.adam(lr=1e-5, clipnorm=0.001)) return model, training_model, prediction_model
def create_models(num_classes, p): # create "base" model (no NMS) image = keras.layers.Input((None, None, 3)) if p['resnet'] == 101: model = ResNet101RetinaNet(image, num_classes=num_classes, weights=p['weights'], nms=False) elif p['resnet'] == 152: model = ResNet152RetinaNet(image, num_classes=num_classes, weights=p['weights'], nms=False) else: # 50 model = ResNet50RetinaNet(image, num_classes=num_classes, weights=p['weights'], nms=False) training_model = model # append NMS for prediction only classification = model.outputs[1] detections = model.outputs[2] boxes = keras.layers.Lambda(lambda x: x[:, :, :4])(detections) detections = layers.NonMaximumSuppression(name='nms')([boxes, classification, detections]) prediction_model = keras.models.Model(inputs=model.inputs, outputs=model.outputs[:2] + [detections]) # compile model training_model.compile( loss={ 'regression' : losses.smooth_l1(), 'classification': losses.focal() }, optimizer=keras.optimizers.adam(lr = p['learning-rate'], clipnorm=0.001), metrics = [] ) return model, training_model, prediction_model
def create_models(num_classes): # create "base" model (no NMS) image = keras.layers.Input((None, None, 3)) model = ResNet50RetinaNet(image, num_classes=num_classes, weights='imagenet', nms=False) training_model = model # append NMS for prediction only classification = model.outputs[1] detections = model.outputs[2] boxes = keras.layers.Lambda(lambda x: x[:, :, :4])(detections) detections = keras_retinanet.layers.NonMaximumSuppression(name='nms')( [boxes, classification, detections]) prediction_model = keras.models.Model(inputs=model.inputs, outputs=model.outputs[:2] + [detections]) # compile model training_model.compile(loss={ 'regression': keras_retinanet.losses.smooth_l1(), 'classification': keras_retinanet.losses.focal() }, optimizer=keras.optimizers.adam(lr=1e-5, clipnorm=0.001)) return model, training_model, prediction_model
def create_models(num_classes, weights='imagenet', multi_gpu=0): # create "base" model (no NMS) image = keras.layers.Input((None, None, 3)) # 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 args.multi_gpu > 1: with tf.device('/cpu:0'): model = ResNet50RetinaNet(image, num_classes=num_classes, weights=weights, nms=False) training_model = multi_gpu_model(model, gpus=args.multi_gpu) else: model = ResNet50RetinaNet(image, num_classes=num_classes, weights=weights, nms=False) training_model = model # append NMS for prediction detections = keras_retinanet.layers.NonMaximumSuppression(name='nms')( model.outputs) prediction_model = keras.models.Model(inputs=model.inputs, outputs=model.outputs[:2] + [detections]) # compile model training_model.compile(loss={ 'regression': keras_retinanet.losses.smooth_l1(), 'classification': keras_retinanet.losses.focal() }, optimizer=keras.optimizers.adam(lr=1e-5, clipnorm=0.001)) return model, training_model, prediction_model
def create_model(weights='imagenet'): image = keras.layers.Input((None, None, 3)) return ResNet50RetinaNet(image, num_classes=20, weights=weights)