def extract_DenseNet(path): import densenet from keras.preprocessing import image import numpy as np img = image.load_img(path, target_size=(224, 224)) x = image.img_to_array(img) img_tensor = densenet.preprocess_input(np.expand_dims(x, axis=0)) image_dim = (224, 224, 3) DenseNet_model = densenet.DenseNetImageNet161(input_shape=image_dim, include_top=False) DenseNet_output = DenseNet_model.predict(img_tensor) return DenseNet_output
def cnn_model(): input_tensor = Input(shape=(params.target_img_size[0],params.target_img_size[1],params.num_channels)) baseModel = densenet.DenseNetImageNet161(input_shape=(params.target_img_size[0], params.target_img_size[1], params.num_channels), include_top=False, input_tensor=input_tensor) modelStruct = baseModel.layers[-1].output modelStruct = Dense(params.cnn_last_layer_length, activation='relu', name='fc1')(modelStruct) modelStruct = Dropout(0.5)(modelStruct) modelStruct = Dense(params.cnn_last_layer_length, activation='relu', name='fc2')(modelStruct) modelStruct = Dropout(0.5)(modelStruct) predictions = Dense(params.num_labels, activation='softmax')(modelStruct) model = Model(inputs=[baseModel.input], outputs=predictions) for i,layer in enumerate(model.layers[-5:]): layer.trainable = True for i,layer in enumerate(model.layers[:-5]): layer.trainable = False return model
https://github.com/titu1994/DenseNet The imported |densenet| library is from this repo. This script was RUN IN THAT REPO, and the files moved over. """ import densenet import keras from keras.models import Model import json image_dim = (224, 224, 3) # Tensorflow backend print("Image dimensions set to %s." % str(image_dim)) print("Loading denseNet model...") densenet_model = densenet.DenseNetImageNet161(input_shape=image_dim, include_top=False) print("Extracting a subset of denseNet to use as an encoder.") n_final_layer = 393 print("Final layer is %d." % n_final_layer) inp = densenet_model.input res56 = densenet_model.layers[49] print("res56 layer is %s" % str(res56)) out_res56 = res56.output res28 = densenet_model.layers[137] print("res28 layer is %s" % str(res28)) out_res28 = res28.output
def get_cnn_model(params): """ Load base CNN model and add metadata fusion layers if 'use_metadata' is set in params.py :param params: global parameters, used to find location of the dataset and json file :return model: CNN model with or without depending on params """ input_tensor = Input(shape=(params.target_img_size[0], params.target_img_size[1], params.num_channels)) baseModel = densenet.DenseNetImageNet161( input_shape=(params.target_img_size[0], params.target_img_size[1], params.num_channels), include_top=False, input_tensor=input_tensor) modelStruct = baseModel.layers[-1].output if params.use_nlm: modelStruct = baseModel.layers[-2].output modelStruct = non_local_block(modelStruct, computation_compression=1, mode='embedded') modelStruct = Conv2D(params.cnn_lstm_layer_length, [3, 3], name='conv_nlm')(modelStruct) modelStruct = Flatten()(modelStruct) modelStruct = Dense(params.cnn_lstm_layer_length, activation='relu', name='fc_nlm')(modelStruct) modelStruct = Dropout(0.5)(modelStruct) if params.use_spp: modelStruct = baseModel.layers[-2].output modelStruct = SpatialPyramidPooling([1, 2, 4], name='spp')(modelStruct) modelStruct = Dense(params.cnn_lstm_layer_length, activation='relu', name='fc_spp')(modelStruct) modelStruct = Dropout(0.5)(modelStruct) if params.use_deform: modelStruct = baseModel.layers[-2].output modelStruct = ConvOffset2D(params.cnn_lstm_layer_length, name='deform')(modelStruct) modelStruct = Conv2D(params.cnn_lstm_layer_length, [3, 3], name='conv_deform')(modelStruct) modelStruct = Flatten()(modelStruct) modelStruct = Dense(params.cnn_lstm_layer_length, activation='relu', name='fc_deform')(modelStruct) modelStruct = Dropout(0.5)(modelStruct) if params.use_metadata: auxiliary_input = Input(shape=(params.metadata_length, ), name='aux_input') modelStruct = merge([modelStruct, auxiliary_input], 'concat') modelStruct = Dense(params.cnn_last_layer_length, activation='relu', name='fc1')(modelStruct) modelStruct = Dropout(0.5)(modelStruct) modelStruct = Dense(params.cnn_last_layer_length, activation='relu', name='fc2')(modelStruct) modelStruct = Dropout(0.5)(modelStruct) predictions = Dense(params.num_labels, activation='softmax')(modelStruct) if not params.use_metadata: model = Model(input=[baseModel.input], output=predictions) else: model = Model(input=[baseModel.input, auxiliary_input], output=predictions) for i, layer in enumerate(model.layers): layer.trainable = True return model
bn_weights[-1][1] = v_w[1] continue v = f[name] v_w = v[v.attrs['weight_names'][0]][:] conv_weights.append(v_w) count_layers = 1 # for dense matrix count_layers += len(conv_weights) count_layers += len(bn_weights) print('Copying %d weights. (%d layers)' % (count_layers, count_layers // 2)) import densenet model = densenet.DenseNetImageNet161((224, 224, 3), weights=None) conv_layer_ids = [] bn_layer_ids = [] for i, layer in enumerate(model.layers): if layer.__class__.__name__ == 'Input': continue if layer.__class__.__name__ == 'Activation': continue if layer.__class__.__name__ == 'MaxPooling2D': continue if layer.__class__.__name__ == 'AveragePooling2D':