def densenet_retinanet(num_classes, backbone='densenet121', inputs=None, modifier=None, **kwargs): """ Constructs a retinanet model using a densenet backbone. Args num_classes: Number of classes to predict. backbone: Which backbone to use (one of ('densenet121', 'densenet169', 'densenet201')). inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)). modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example). Returns RetinaNet model with a DenseNet backbone. """ # choose default input if inputs is None: inputs = keras.layers.Input((None, None, 3)) blocks = allowed_backbones[backbone] densenet = DenseNet(blocks=blocks, input_tensor=inputs, include_top=False, pooling=None, weights=None) # get last conv layer from the end of each dense block layer_outputs = [ densenet.get_layer( name='conv{}_block{}_concat'.format(idx + 2, block_num)).output for idx, block_num in enumerate(blocks) ] # create the densenet backbone densenet = keras.models.Model(inputs=inputs, outputs=layer_outputs[1:], name=densenet.name) # invoke modifier if given if modifier: densenet = modifier(densenet) # create the full model model = retinanet.retinanet(inputs=inputs, num_classes=num_classes, backbone_layers=densenet.outputs, **kwargs) return model
def densenet_retinanet(num_classes, backbone='densenet121', modifier=None, inputs=None, **kwargs): # choose default input if inputs is None: inputs = keras.layers.Input((None, None, 3)) blocks = allowed_backbones[backbone] densenet = DenseNet(blocks=blocks, input_tensor=inputs, include_top=False, pooling=None, weights=None) # get last conv layer from the end of each dense block outputs = [ densenet.get_layer( name='conv{}_block{}_concat'.format(idx + 2, block_num)).output for idx, block_num in enumerate(blocks) ] # create the densenet backbone densenet = keras.models.Model(inputs=inputs, outputs=outputs, name=densenet.name) # invoke modifier if given if modifier: densenet = modifier(densenet) # create the full model model = retinanet.retinanet_bbox(inputs=inputs, num_classes=num_classes, backbone=densenet, **kwargs) return model
def classifier(self) -> Model: """ Returns the model of this configuration """ base_model = DenseNet([6, 12, 24, 16], include_top=False, weights='imagenet', input_shape=self.data_shape, pooling=None) x = base_model.output x = Flatten()(x) x = Dense(500)(x) x = Dropout(0.5)(x) x = Dense(500)(x) x = Dropout(0.5)(x) x = Dense(8, activation='linear', name='output_class')(x) model = Model(inputs=base_model.inputs, outputs=x) model.compile(self.get_optimizer(), loss="mean_squared_error", metrics=["mae"]) return model
input_tensor = Conv2D(3, (1, 1))(input_tensor) if use_vgg: base_model_imagenet = VGG(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) base_model = VGG(include_top=False, weights=None, input_tensor=input_tensor) for i, layer in enumerate(base_model_imagenet.layers): # we must skip input layer, which has no weights if i == 0: continue base_model.layers[i+1].set_weights(layer.get_weights()) else: base_model_imagenet = DenseNet(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) base_model = DenseNet(include_top=False, weights=None, input_tensor=input_tensor) for i, layer in enumerate(base_model_imagenet.layers): # we must skip input layer, which has no weights if i == 0: continue base_model.layers[i+1].set_weights(layer.get_weights()) # serialize model to JSON base_model_json = base_model.to_json() with open("model_base.json", "w") as json_file: json_file.write(base_model_json) # serialize weights to HDF5
'SeaLake': 9 } num_classes = len(class_indices) # contruct path path_to_home = os.path.expanduser("~") path_to_split_datasets = path_to_split_datasets.replace("~", path_to_home) path_to_train = os.path.join(path_to_split_datasets, "train") path_to_validation = os.path.join(path_to_split_datasets, "validation") # parameters for CNN if use_vgg: base_model = VGG(include_top=False, weights=None, input_shape=(64, 64, 13)) else: base_model = DenseNet(include_top=False, weights=None, input_shape=(64, 64, 13)) # add a global spatial average pooling layer top_model = base_model.output top_model = GlobalAveragePooling2D()(top_model) # or just flatten the layers # top_model = Flatten()(top_model) # let's add a fully-connected layer if use_vgg: # only in VGG19 a fully connected nn is added for classfication # DenseNet tends to overfitting if using additionally dense layers top_model = Dense(2048, activation='relu')(top_model) top_model = Dense(2048, activation='relu')(top_model) # and a logistic layer predictions = Dense(num_classes, activation='softmax')(top_model)
# ![vgg16](images_for_notebook/vgg16.png "Original VGG") # ### 1. Pretrained network model without top layers # ![vgg16_no_top](images_for_notebook/vgg16_no_top.png "VGG no top") # In[62]: # parameters for CNN if use_vgg: base_model = VGG(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) else: base_model = DenseNet(include_top=False, weights='imagenet', input_shape=(64, 64, 3)) # ### 2. define new top layers # ![vgg16_sentinel_rgb](images_for_notebook/vgg16_sentinel_rgb.png "VGG RGB Sentinel") # In[63]: # add a global spatial average pooling layer top_model = base_model.output top_model = GlobalAveragePooling2D()(top_model) # or just flatten the layers # top_model = Flatten()(top_model) # let's add a fully-connected layer if use_vgg:
# load the input image using the Keras helper utility while ensuring # that the image is resized to 224x224 pxiels, the required input # dimensions for the network -- then convert the PIL image to a # NumPy array print("[INFO] loading and preprocessing image...") image = image_utils.load_img(args["image"], target_size=(224, 224)) image = image_utils.img_to_array(image) # our image is now represented by a NumPy array of shape (3, 224, 224), # but we need to expand the dimensions to be (1, 3, 224, 224) so we can # pass it through the network -- we'll also preprocess the image by # subtracting the mean RGB pixel intensity from the ImageNet dataset image = np.expand_dims(image, axis=0) image = preprocess_input(image) # load the VGG16 network print("[INFO] loading network...") model = DenseNet(4, weights="imagenet") # classify the image print("[INFO] classifying image...") preds = model.predict(image) label=decode_predictions(preds, top=3)[0] print('Predicted:' , label) # display the predictions to our screen cv2.putText(orig, "Label: {}".format(label), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) cv2.imshow("Classification", orig) cv2.waitKey(0)