Last active
June 11, 2018 10:30
-
-
Save mkocabas/68108714db6fbe7743ef14271ca30de7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################### retinanet.py | |
def default_null_model( | |
pyramid_feature_size=256, | |
name='null_submodel' | |
): | |
options = { | |
'kernel_size' : 3, | |
'strides' : 1, | |
'padding' : 'same', | |
} | |
inputs = keras.layers.Input(shape=(None, None, pyramid_feature_size)) | |
outputs = inputs | |
return keras.models.Model(inputs=inputs, outputs=outputs, name=name) | |
def default_null_submodel(): | |
return [('null', default_null_model())] | |
def retinanet_null( | |
inputs, | |
backbone, | |
create_pyramid_features = __create_pyramid_features, | |
submodels = None, | |
name = 'retinanet' | |
): | |
if submodels is None: | |
submodels = default_null_submodel() | |
image = inputs | |
_, C3, C4, C5 = backbone.outputs # we ignore C2 | |
# compute pyramid features as per https://arxiv.org/abs/1708.02002 | |
features = create_pyramid_features(C3, C4, C5) | |
# for all pyramid levels, run available submodels | |
pyramid = __build_pyramid(submodels, features) | |
return keras.models.Model(inputs=inputs, outputs=[pyramid], name=name) | |
def retinanet_null_bbox(inputs, name='retinanet-null-bbox', *args, **kwargs): | |
model = retinanet_null(inputs=inputs, *args, **kwargs) | |
# we expect the anchors, regression and classification values as first output | |
regression = model.outputs[0] | |
# construct the model | |
return keras.models.Model(inputs=inputs, outputs=[regression], name=name) | |
####################### resnet.py | |
def ResNet50RetinaNetNull(inputs, weights='imagenet', *args, **kwargs): | |
image = inputs | |
# load pretrained imagenet weights? | |
if weights == 'imagenet': | |
weights_path = keras.applications.imagenet_utils.get_file( | |
'ResNet-50-model.keras.h5', | |
WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='1e511c75e9ab5c16900652ad1f6044ce' | |
) | |
else: | |
weights_path = weights | |
resnet = keras_resnet.models.ResNet50(image, include_top=False, freeze_bn=True) | |
model = keras_retinanet.models.retinanet.retinanet_null_bbox(inputs=inputs, backbone=resnet, *args, **kwargs) | |
model.load_weights(weights_path, by_name=True) | |
return model | |
def create_model(weights='imagenet'): | |
image = keras.layers.Input((640, 640, 3)) | |
return ResNet50RetinaNetNull(image, weights=weights) | |
# ValueError: `Concatenate` layer requires inputs with matching shapes except for the concat axis. | |
# Got inputs shapes: [(None, 80, 80, 256), (None, 40, 40, 256), (None, 20, 20, 256), (None, 10, 10, 256), (None, 5, 5, 256)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment