def build_ssd_resnet50(input_shape, n_classes, load_pretrained=None): """Create a SSD model with resnet50 as the base network When using with pretrained weights, they must come from a keras ResNet50 model or from models.resnet.build_resnet50(). Parameters ---------- input_shape : tuple Input shape for the model in the form (w, h, c). n_classes : int Number of classes to detect by the model. load_pretrained : str or None If None, no pretrained weights are used. Otherwise, it is assumed the value is a path to a hdf5 file with the weights. """ base_model = resnet.build_resnet50(img_shape=input_shape, n_classes=n_classes, freeze_layers_from=None, include_top=False) if load_pretrained: base_model.load_weights(load_pretrained, by_name=True) resnet50 = {} resnet50['input'] = base_model.input resnet50['block2'] = base_model.get_layer('res3a_branch2a').input resnet50['block3'] = base_model.get_layer('res4a_branch2a').input resnet50['block4'] = base_model.get_layer('res5a_branch2a').input resnet50['block5'] = base_model.get_layer('avg_pool').input # Take as base layers the resnet merge layers from blocks 2, 3, 4 and 5 variances = [.1, .1, .2, .2] priors = [ create_priors('block2', 10., None, [1.25, 1.5], variances), create_priors('block3', 82., 157., [1.5], variances), create_priors('block4', 157., 235., [1.5], variances), create_priors('block5', 235., 320., [1.5], variances) ] ssd = build_ssd(input_shape, n_classes, resnet50, priors) return ssd
def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer): # Create the *Keras* model if cf.model_name == 'fcn8': model = build_fcn8(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=cf.load_imageNet) elif cf.model_name == 'unet': model = build_unet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'segnet_basic': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None, basic=True) elif cf.model_name == 'segnet_vgg': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None, basic=False) elif cf.model_name == 'resnetFCN': model = build_resnetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=cf.load_imageNet) elif cf.model_name == 'densenetFCN': model = build_densenetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'lenet': model = build_lenet(in_shape, cf.dataset.n_classes, cf.weight_decay) elif cf.model_name == 'alexNet': model = build_alexNet(in_shape, cf.dataset.n_classes, cf.weight_decay) elif cf.model_name == 'vgg16': model = build_vgg(in_shape, cf.dataset.n_classes, 16, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'vgg19': model = build_vgg(in_shape, cf.dataset.n_classes, 19, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'resnet50': model = build_resnet50(in_shape, cf.dataset.n_classes, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'InceptionV3': model = build_inceptionV3(in_shape, cf.dataset.n_classes, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=False) elif cf.model_name == 'tiny-yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=True) else: raise ValueError('Unknown model') # Load pretrained weights if cf.load_pretrained: print(' loading model weights from: ' + cf.weights_file + '...') model.load_weights(cf.weights_file, by_name=True) # Compile model model.compile(loss=loss, metrics=metrics, optimizer=optimizer) # Show model structure if cf.show_model: model.summary() plot(model, to_file=os.path.join(cf.savepath, 'model.png')) # Output the model print (' Model: ' + cf.model_name) # model is a keras model, Model is a class wrapper so that we can have # other models (like GANs) made of a pair of keras models, with their # own ways to train, test and predict return One_Net_Model(model, cf, optimizer)
def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer): # Assertions if 'tiramisu' in cf.model_name: input_rows, input_cols = cf.target_size_train[0], cf.target_size_train[1] multiple = 2 ** 5 # 5 transition blocks if input_rows is not None: if input_rows % multiple != 0: raise ValueError('The number of rows of the input data must be a multiple of {}'.format(multiple)) if input_cols is not None: if input_cols % multiple != 0: raise ValueError( 'The number of columns of the input data must be a multiple of {}'.format(multiple)) # Create the *Keras* model if cf.model_name == 'fcn8': model = build_fcn8(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, # path_weights='weights/pascal-fcn8s-dag.mat') path_weights=None) elif cf.model_name == 'dilation': model = build_dilation(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, # path_weights='weights/pascal-fcn8s-dag.mat') path_weights=None) elif cf.model_name == 'segnet_basic': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None, basic=True) elif cf.model_name == 'segnet_vgg': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None, basic=False) elif cf.model_name == 'densenetFCN': model = build_densenetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'vgg16': model = build_vgg(in_shape, cf.dataset.n_classes, 16, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'vgg19': model = build_vgg(in_shape, cf.dataset.n_classes, 19, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'resnet50': model = build_resnet50(in_shape, cf.dataset.n_classes, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=False) elif cf.model_name == 'tiny-yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=True) elif cf.model_name == 'ssd300': model = build_ssd300(in_shape, cf.dataset.n_classes + 1, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'deeplabV2': model = build_deeplabv2(in_shape, nclasses=cf.dataset.n_classes, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, weight_decay=cf.weight_decay) elif cf.model_name == 'ssd300': model = build_ssd300(in_shape, cf.dataset.n_classes + 1, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'tiramisu_fc56': model = build_tiramisu_fc56(in_shape, cf.dataset.n_classes, cf.weight_decay, compression=0, dropout=0.2, nb_filter=48, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'tiramisu_fc67': model = build_tiramisu_fc67(in_shape, cf.dataset.n_classes, cf.weight_decay, compression=0, dropout=0.2, nb_filter=48, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'tiramisu_fc103': model = build_tiramisu_fc103(in_shape, cf.dataset.n_classes, cf.weight_decay, compression=0, dropout=0.2, nb_filter=48, freeze_layers_from=cf.freeze_layers_from) else: raise ValueError('Unknown model') # Load pretrained weights if cf.load_pretrained: print(' loading model weights from: ' + cf.weights_file + '...') model.load_weights(cf.weights_file, by_name=True) # Compile model model.compile(loss=loss, metrics=metrics, optimizer=optimizer) # Show model structure if cf.show_model: model.summary() plot(model, to_file=os.path.join(cf.savepath, 'model.png')) # Output the model print (' Model: ' + cf.model_name) # model is a keras model, Model is a class wrapper so that we can have # other models (like GANs) made of a pair of keras models, with their # own ways to train, test and predict return One_Net_Model(model, cf, optimizer)
def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer): # Create the *Keras* model if cf.model_name == 'fcn8': model = build_fcn8(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=cf.load_imageNet) elif cf.model_name == 'unet': model = build_unet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'segnet_basic': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, basic=True) elif cf.model_name == 'segnet_vgg': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, basic=False) elif cf.model_name == 'resnetFCN': model = build_resnetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'inceptionFCN': model = build_inceptionFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'densenet': model = build_densenet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'lenet': model = build_lenet(in_shape, cf.dataset.n_classes, cf.weight_decay) elif cf.model_name == 'alexNet': model = build_alexNet(in_shape, cf.dataset.n_classes, cf.weight_decay) elif cf.model_name == 'vgg16': model = build_vgg(in_shape, cf.dataset.n_classes, 16, cf.weight_decay, load_imageNet=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'vgg19': model = build_vgg(in_shape, cf.dataset.n_classes, 19, cf.weight_decay, load_imageNet=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'resnet50': model = build_resnet50(in_shape, cf.dataset.n_classes, cf.weight_decay, load_imageNet=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'InceptionV3': model = build_inceptionV3(in_shape, cf.dataset.n_classes, cf.weight_decay, load_imageNet=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_imageNet=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=False) elif cf.model_name == 'tiny-yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_imageNet=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=True) elif cf.model_name == 'ssd': model = build_ssd(in_shape, cf.dataset.n_classes+1, cf.dataset.n_priors, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'densenet_segmentation': model = build_densenet_segmentation(in_shape, cf.dataset.n_classes, weight_decay = cf.weight_decay, freeze_layers_from = cf.freeze_layers_from, path_weights = cf.load_imageNet) else: raise ValueError('Unknown model') # Load pretrained weights if cf.load_pretrained: print(' loading model weights from: ' + cf.weights_file + '...') # If the weights are from different datasets if cf.different_datasets: if cf.freeze_layers_from == 'base_model': raise TypeError('Please, enter the layer id instead of "base_model"' ' for the freeze_layers_from config parameter') croppedmodel = model_from_json(model.to_json()) # Remove not frozen layers for i in range(len(model.layers[cf.freeze_layers_from:])): croppedmodel.layers.pop() # Load weights only for the frozen layers croppedmodel.load_weights(cf.weights_file, by_name=True) model.set_weights(croppedmodel.get_weights()) else: model.load_weights(cf.weights_file, by_name=True) # Compile model model.compile(loss=loss, metrics=metrics, optimizer=optimizer) # Show model structure if cf.show_model: model.summary() plot(model, to_file=os.path.join(cf.savepath, 'model.png')) # Output the model print (' Model: ' + cf.model_name) # model is a keras model, Model is a class wrapper so that we can have # other models (like GANs) made of a pair of keras models, with their # own ways to train, test and predict return One_Net_Model(model, cf, optimizer)
def make_one_net_model(self, cf, in_shape, loss, metrics, optimizer): # Create the *Keras* model if cf.model_name == 'fcn8': model = build_fcn8(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=('weights/pascal-fcn8s-dag.mat' if cf.load_pascalVOC else None)) elif cf.model_name == 'unet': model = build_unet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'segnet_basic': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None, basic=True) elif cf.model_name == 'segnet_vgg': model = build_segnet(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None, basic=False) elif cf.model_name == 'resnetFCN': model = build_resnetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'densenetFCN': model = build_densenetFCN(in_shape, cf.dataset.n_classes, cf.weight_decay, freeze_layers_from=cf.freeze_layers_from, path_weights=None) elif cf.model_name == 'lenet': model = build_lenet(in_shape, cf.dataset.n_classes, cf.weight_decay) elif cf.model_name == 'alexNet': model = build_alexNet(in_shape, cf.dataset.n_classes, cf.weight_decay) elif cf.model_name == 'vgg16': model = build_vgg(in_shape, cf.dataset.n_classes, 16, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'vgg19': model = build_vgg(in_shape, cf.dataset.n_classes, 19, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'resnet50': model = build_resnet50(in_shape, cf.dataset.n_classes, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'InceptionV3': model = build_inceptionV3(in_shape, cf.dataset.n_classes, cf.weight_decay, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from) elif cf.model_name == 'densenet': model = build_densenet( in_shape, cf.dataset.n_classes, layers_in_dense_block=cf.layers_in_dense_block, initial_filters=cf.initial_filters, growth_rate=cf.growth_rate, n_bottleneck=cf.n_bottleneck, compression=cf.compression, dropout=cf.dropout, weight_decay=cf.weight_decay) elif cf.model_name == 'yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=False) elif cf.model_name == 'tiny-yolo': model = build_yolo(in_shape, cf.dataset.n_classes, cf.dataset.n_priors, load_pretrained=cf.load_imageNet, freeze_layers_from=cf.freeze_layers_from, tiny=True) elif cf.model_name == 'ssd300': model = build_ssd300(in_shape, cf.dataset.n_classes + 1) # TODO: find best parameters ssd_utils.initialize_module(model, in_shape, cf.dataset.n_classes + 1, overlap_threshold=0.5, nms_thresh=0.45, top_k=400) elif cf.model_name == 'tiramisu': model = build_tiramisu( in_shape, cf.dataset.n_classes, layers_in_dense_block=cf.layers_in_dense_block, initial_filters=cf.initial_filters, growth_rate=cf.growth_rate, n_bottleneck=cf.n_bottleneck, compression=cf.compression, dropout=cf.dropout, weight_decay=cf.weight_decay) elif cf.model_name == 'ssd300_pretrained': model = build_ssd300_pretrained(in_shape, cf.dataset.n_classes + 1) # TODO: find best parameters ssd_utils.initialize_module(model, in_shape, cf.dataset.n_classes + 1, overlap_threshold=0.5, nms_thresh=0.45, top_k=400) elif cf.model_name == 'ssd_resnet50': model = build_ssd_resnet50(in_shape, cf.dataset.n_classes + 1) # TODO: find best parameters ssd_utils.initialize_module(model, in_shape, cf.dataset.n_classes + 1, overlap_threshold=0.5, nms_thresh=0.45, top_k=400) else: raise ValueError('Unknown model') # Load pretrained weights if cf.load_pretrained: print(' loading model weights from: ' + cf.weights_file + '...') model.load_weights(cf.weights_file, by_name=True) # Compile model model.compile(loss=loss, metrics=metrics, optimizer=optimizer) # Show model structure if cf.show_model: model.summary() plot(model, to_file=os.path.join(cf.savepath, 'model.png')) # Output the model print(' Model: ' + cf.model_name) # model is a keras model, Model is a class wrapper so that we can have # other models (like GANs) made of a pair of keras models, with their # own ways to train, test and predict return One_Net_Model(model, cf, optimizer)