def get_model(self): """ Create and compile the DenseNet121 model :return: DenseNet121 Model """ # DenseNet121 expects number of channels to be 3 input = Input(shape=(self.input_size, self.input_size, 3)) base_pretrained_model = densenet.DenseNet121( input_shape=(self.input_size, self.input_size, 3), input_tensor=input, include_top=False, weights='imagenet') x = GlobalAveragePooling2D()(base_pretrained_model.layers[-1].output) x = Dense(self.output_classes, activation='sigmoid')(x) self.model = Model(input=input, output=x) # Using weighted binary loss has been suggested in the paper loss = WeightedBinaryLoss(self.w_class0, self.w_class1) # Note: default learning rate of 'adam' is 0.001 as required by the paper self.model.compile(optimizer='adam', loss=loss.compute_loss) return self.model
def custom_dn121(input_tensor, num_classes, weights=None, optimizer=keras.optimizers.SGD(), fc=0): model_base = densenet.DenseNet121(include_top=False, input_tensor=input_tensor, weights='imagenet', pooling='avg') for ll in model_base.layers[1:]: ll.trainable = False ll.name += '_dn121' inp_tensor = model_base.input x = model_base.output for i in range(fc): x = Dense(256, activation='relu', name='top_dense'+str(i)+'_dn121')(x) x = Dropout(0.1)(x) x = Dense(num_classes, activation='softmax', name='top_predictions_dn121')(x) model = keras.Model(inputs=inp_tensor, outputs=x) try: model.load_weights(weights, by_name=False) except: print("Weight file {} could not be loaded. Using ImageNet weights.".format(weights)) model.preprocessor = densenet.preprocess_input model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) return model
def build_base_model(self, inputs, blocks=None): inputs = keras.layers.Lambda( lambda x: keras_densenet.preprocess_input(x))(inputs) if self.backbone_name == 'densenet121': densenet = keras_densenet.DenseNet121(include_top=False, weights='imagenet', input_tensor=inputs) elif self.backbone_name == 'densenet169': densenet = keras_densenet.DenseNet169(include_top=False, input_tensor=inputs, weights='imagenet') elif self.backbone_name == 'densenet201': densenet = keras_densenet.DenseNet201(include_top=False, input_tensor=inputs, weights='imagenet') elif self.backbone_name == 'densenet': if blocks is None: raise ValueError( 'blocks must be specified to use custom densenet backbone') densenet = keras_densenet.DenseNet(blocks=blocks, include_top=False, input_tensor=inputs, weights='imagenet') else: raise ValueError("Backbone '{}' not recognized.".format( self.backbone_name)) return densenet
def generate_transfer_model(self, input_shape, num_classes): # imports the pretrained model and discards the fc layer base_model = densenet.DenseNet121( include_top=False, weights='imagenet', input_tensor=None, input_shape=input_shape, pooling='max') #using max global pooling, no flatten required # add fc layers x = base_model.output #x = Dense(256, activation="relu")(x) x = Dense(256, activation="elu", kernel_regularizer=regularizers.l2(0.1))(x) x = Dropout(0.6)(x) x = BatchNormalization()(x) predictions = Dense(num_classes, activation="softmax")(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # compile model using accuracy to measure model performance and adam optimizer #optimizer = optimizers.Adam(lr=0.0001) optimizer = optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy', 'mse']) return model
def pretrained_model(model): if model == 'densenet': base_model = densenet.DenseNet121(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 3)) elif model == 'inception': base_model = inception_v3.InceptionV3(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 3)) elif model == 'mobilenet': base_model = mobilenet.MobileNet(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 3)) elif model == 'vgg': base_model = vgg19.VGG19(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 3)) elif model == 'resnet': base_model = resnet50.ResNet50(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 3)) elif model == 'xception': base_model = xception.Xception(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 3)) for layer in base_model.layers: layer.trainable = True x = base_model.output x = Flatten()(x) x = Dense(150, activation='relu')(x) x = Dropout(0.2)(x) predictions = Dense(1, activation='sigmoid')(x) return models.Model(base_model.input, predictions)
def get_model(img): model = densenet.DenseNet121(include_top=False, input_tensor=img, weights=None, pooling=None) layer_out_put = [model.get_layer(name='conv{}_block{}_concat'.format(idx + 3, block_num)).output for idx, block_num in enumerate( [12, 24, 16])] return layer_out_put
def load_model(self, h5modelpath, model_file_name): sess = tf.Session(graph=K.get_session().graph) K.set_session(sess) model = densenet.DenseNet121(weights='imagenet',classes=1000) # loads both architecture & weights model.save(h5modelpath, model_file_name) # saving the model (graph + weights) in a single .h5 file logits = model.outputs[0].op.inputs[0] print("Model loaded..") return sess, model, logits
def compute_embeddings_pretrained(parameters, train_X, val_X, test=None): if parameters['cnn'] == 'vgg': cnn_model = vgg19.VGG19(weights='imagenet', pooling='avg') cnn = Model(inputs=cnn_model.input, outputs=cnn_model.get_layer('fc2').output) if parameters['cnn'] == 'resnet': #cnn = resnet50.ResNet50(weights='imagenet',include_top=False,pooling=parameters['pool']) #cnn = Model(inputs=cnn_model.input, outputs=cnn_model.get_layer('').output) cnn = resnet.ResNet101(include_top=False, weights='imagenet', pooling='avg') if parameters['cnn'] == 'inc_resnet': cnn = inception_resnet_v2.InceptionResNetV2(weights='imagenet', include_top=False, pooling=parameters['pool']) #cnn = Model(inputs=cnn_model.input, outputs=cnn_model.get_layer('').output) if parameters['cnn'] == 'densenet': cnn = densenet.DenseNet121(weights='imagenet', include_top=False, pooling=parameters['pool']) if parameters['cnn'] == 'xception': cnn = xception.Xception(weights='imagenet', include_top=False, pooling=parameters['pool']) ''' if parameters['freeze']: for layer in cnn.layers: layer.trainable = False print ('TRAINABLE LAYERS: ') for layer in cnn.layers: print(layer, layer.trainable) ''' embeddings_train = cnn.predict(train_X) embeddings_val = cnn.predict(val_X) print('Dim embeddings train: %s', str(embeddings_train.shape)) print('Dim embeddings val: %s', str(embeddings_val.shape)) if test is not None: embeddings_test = {} for k, v in test.items(): if parameters['cnn'] == 'resnet': embeddings_test[k] = cnn.predict(v) #.reshape((2048,-1)) elif parameters['cnn'] == 'inc_resnet': embeddings_test[k] = cnn.predict(v) #.reshape((1536,-1)) elif parameters['cnn'] == 'xception': embeddings_test[k] = cnn.predict(v) #.reshape((,-1)) else: embeddings_test[k] = cnn.predict(v) #.reshape((1024,-1)) with open(parameters['embeddings_pretrained_test'], 'wb') as f: pickle.dump(embeddings_test, f, protocol=pickle.HIGHEST_PROTOCOL) print('Dim embeddings test: %s', str(len(embeddings_test))) return embeddings_train.squeeze(), embeddings_val.squeeze()
def build_model(model_name, input_shape, num_classes, transfer_learning): from keras.layers import Dense, Dropout, Activation, Flatten, GlobalAveragePooling2D, Conv2D, MaxPooling2D, BatchNormalization from keras.models import Sequential from keras.applications import densenet from keras.applications import vgg16 from keras.applications import InceptionV3 from keras.models import Model weights = 'imagenet' if transfer_learning else None if model_name == 'MLP': model = Sequential() model.add(Dense(1024, activation='relu', input_shape=input_shape)) model.add(Dropout(0.5)) model.add(Dense(1024, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) elif model_name == 'ShallowCNN': model = Sequential() model.add( Conv2D(filters=32, kernel_size=[11, 5], strides=[3, 3], input_shape=input_shape)) model.add(MaxPooling2D(pool_size=[3, 3], strides=[2, 2])) model.add(Dropout(0.2)) model.add(BatchNormalization(momentum=0.9)) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(num_classes, activation='softmax')) elif model_name == 'VGG16': base_model = vgg16.VGG16(weights=weights, include_top=False, input_shape=input_shape) x = Flatten()(base_model.output) x = Dense(4096, activation='relu')(x) x = Dense(4096, activation='relu')(x) predictions = Dense(num_classes, activation="softmax")(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name == 'InceptionV3': base_model = InceptionV3(weights=weights, include_top=False, input_shape=input_shape) x = GlobalAveragePooling2D()(base_model.output) x = Dense(4096, activation='relu')(x) predictions = Dense(num_classes, activation="softmax")(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name == 'DenseNet': model = densenet.DenseNet121(weights='imagenet', include_top=False, input_shape=input_shape) x = Flatten()(model.output) predictions = Dense(num_classes, activation="softmax")(x) model = Model(inputs=model.input, outputs=predictions) return model
def keras_pretrained_model(netname): #import pdb; pdb.set_trace() if netname == 'VGG16': model = vgg16.VGG16(weights='imagenet') elif netname == 'VGG19': model = vgg19.VGG19(weights='imagenet') elif netname == 'ResNet50': model = resnet50.ResNet50(weights='imagenet') elif netname == 'DenseNet': model = densenet.DenseNet121(weights='imagenet') elif netname == 'Inception_v3': model = inception_v3.InceptionV3(weights='imagenet') return _maybe_save(netname, model)
def configure_simple_model(): """ loads a pretrained model and replaces the last layer with a layer to finetune""" base_model = densenet.DenseNet121(input_shape=(224, 224, 3), include_top=False) x = Flatten()(base_model.output) x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) predictions = Dense(1, activation='softmax')(x) # create graph of your new model head_model = Model(input=base_model.input, output=predictions) head_model.compile(loss=keras.losses.binary_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) return head_model
def dense_net121(self, percent2retrain): 'Returns a Densenet121 architecture NN' dense_model = densenet.DenseNet121(input_shape=self.input_dim, weights='imagenet', include_top=False) # freeze base layers if percent2retrain < 1: for layer in dense_model.layers[:-int(len(dense_model.layers)*percent2retrain)]: layer.trainable = False # add classification top layer model = Sequential() model.add(dense_model) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(self.n_classes, activation='sigmoid')) return model
def load_dcn_model(model_name = 'VGG16'): """ Returns a pretrained keras DCN model. For each model we return complete activation (last layer) and intermediate activation (penultimate layer). model_name can be: - VGG16 - VGG19 - xception - inception_resnet_v2 - resnet50 - inceptionV3 - densenet """ print "Loading model "+model_name if(model_name == 'VGG16'): model = vgg16.VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) int_model = Model(inputs=model.input, outputs = model.get_layer('fc2').output) elif(model_name == 'VGG19'): model = vgg19.VGG19(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) int_model = Model(inputs=model.input, outputs = model.get_layer('fc2').output) elif(model_name == 'xception'): model = xception.Xception(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) int_model = Model(inputs=model.input, outputs = model.get_layer('avg_pool').output) elif(model_name == 'inception_resnet_v2'): model = inception_resnet_v2.InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) int_model = Model(inputs=model.input, outputs = model.get_layer('avg_pool').output) elif(model_name == 'resnet50'): model = resnet50.ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) int_model = Model(inputs=model.input, outputs = model.get_layer('flatten_1').output) elif(model_name == 'inceptionV3'): model = inception_v3.InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) int_model = Model(inputs=model.input, outputs = model.get_layer('avg_pool').output) elif(model_name == 'densenet'): model = densenet.DenseNet121(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) int_model = Model(inputs=model.input, outputs = model.get_layer('avg_pool').output) else: print "Model not found." return None print "Model loaded." return (model, int_model)
def pretrained_model(model): if model == 'densenet': base_model = densenet.DenseNet121(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3)) elif model == 'inception': base_model = inception_v3.InceptionV3(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3)) elif model == 'mobilenet': base_model = mobilenet.MobileNet(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3)) elif model == 'vgg': base_model = vgg19.VGG19(include_top=True,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3)) elif model == 'resnet': base_model = resnet50.ResNet50(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3)) elif model == 'xception': base_model = xception.Xception(include_top=False,weights='imagenet',input_shape = (IMG_SIZE,IMG_SIZE,3)) for layer in base_model.layers: layer.trainable = False base_model = Model(inputs = base_model.input, outputs = base_model.get_layer('fc2').output) #x = Dense(2048,activation='relu')(x) #x = Dropout(0.2)(x) predictions = Dense(1108,activation='softmax')(base_model.output) return models.Model(base_model.input,predictions)
def generate_transfer_model(input_shape, num_classes): # imports the pretrained model and discards the fc layer base_model = densenet.DenseNet121( include_top=False, weights='imagenet', input_tensor=None, input_shape=input_shape, pooling='max') #using max global pooling, no flatten required # train only the top layers, i.e. freeze all convolutional layers # for layer in base_model.layers: # layer.trainable = False # unfreeze last convolutional block # train_last_conv_layer = True # if train_last_conv_layer: # n_layers_unfreeze = 7*16+3 # for layer in base_model.layers[-n_layers_unfreeze:]: # layer.trainable = True # add fc layers x = base_model.output #x = Dense(256, activation="relu")(x) x = Dense(256, activation="relu", kernel_regularizer=regularizers.l2(0.01))(x) x = Dropout(0.6)(x) x = BatchNormalization()(x) predictions = Dense(num_classes, activation="softmax")(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # compile model using accuracy to measure model performance and adam optimizer optimizer = optimizers.Adam(lr=0.001) #optimizer = optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) return model
def get_model(self): """ Create and compile the DenseNet121 model :return: DenseNet121 Model """ # DenseNet121 expects number of channels to be 3 input = Input(shape=(self.input_size, self.input_size, 3)) base_pretrained_model = densenet.DenseNet121(input_shape=(self.input_size, self.input_size, 3), input_tensor=input, include_top=False, weights='imagenet') x = GlobalAveragePooling2D()(base_pretrained_model.layers[-1].output) x = Dense(self.output_classes, activation='softmax')(x) self.model = Model(input=input, output=x) # Note: default learning rate of 'adam' is 0.001 as required by the paper self.model.compile(optimizer='adam', loss='categorical_crossentropy') return self.model
def Load_DenseNet121_Lstm_IMG_Model(self): base_model = DenseNet.DenseNet121(include_top=False, weights=None, input_shape=(38, 38, self.date_size)) model = Sequential() model.add(base_model) # model.add(Dense(512)) # model.add(LeakyReLU(alpha=0.2)) # model.add(Dense(256)) # model.add(LeakyReLU(alpha=0.2)) # model.add(Dense(128)) # model.add(LeakyReLU(alpha=0.2)) # model.add(Dense(64, activation='linear')) if self.CATEGORICAL: model.add(GlobalAveragePooling2D(name='avg_pool')) model.add(Dense(self.num_classes, activation='softmax')) else: model.add(Dense(1, activation='linear')) model.compile(loss='mse', optimizer='rmsprop', metrics=['acc']) return model
def transfer_densenet_model(input_shape, n_classes): base_model = densenet.DenseNet121(input_shape=input_shape, weights='imagenet', include_top=False, pooling='avg') for layer in base_model.layers: layer.trainable = True x = base_model.output x = Dense(1000,\ kernel_regularizer=regularizers.l1_l2(0.01),\ activity_regularizer=regularizers.l2(0.01))(x) x = Activation('relu')(x) x = Dense(500,\ kernel_regularizer=regularizers.l1_l2(0.01),\ activity_regularizer=regularizers.l2(0.01))(x) x = Activation('relu')(x) predictions = Dense(n_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) return model
def densenet_fcn( resize=(224, 224), pretrained="imagenet", freezeUpto=141, onFcLayers=True): """ pretrained = imagenet / path to the saved weights """ densenett = densenet.DenseNet121(input_shape=(*resize, 3), include_top=False, weights=pretrained, pooling='avg') if freezeUpto > 0: for layer in densenett.layers[:freezeUpto]: layer.trainable = False densenet_model = Sequential() densenet_model.add(densenett) if onFcLayers: densenet_model.add(Dropout(0.5)) densenet_model.add(Dense(512)) densenet_model.add(Dropout(0.5)) densenet_model.add(Dense(15, activation='sigmoid')) return densenet_model
def __init__(self, model_name, transfer=False, input_shape=None): ''' Load pre-trained model. model_name: one of available models. transfer: whether to transfer learning. if True, exclude the fully-connected layer at the top of the network. default False input_shape: : Transfer learning input shape. If None, default input shape. Default None. https://keras.io/applications/ ''' self.name = model_name # vgg16 if model_name == 'vgg16': import keras.applications.vgg16 as vgg16 self.lib = vgg16 if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = vgg16.VGG16(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = vgg16.VGG16(include_top=(not transfer)) # vgg 19 elif model_name == 'vgg19': import keras.applications.vgg19 as vgg19 self.lib = vgg19 if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = vgg19.VGG19(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = vgg19.VGG19(include_top=(not transfer)) # resnet50 elif model_name == 'resnet50': import keras.applications.resnet50 as resnet50 self.lib = resnet50 if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = resnet50.ResNet50(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = resnet50.ResNet50(include_top=(not transfer)) # xception elif model_name == 'xception': import keras.applications.xception as xception self.lib = xception if transfer: if input_shape == None: self.input_size = (299, 299) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = xception.Xception(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (299, 299) self.model = xception.Xception(include_top=(not transfer)) # densenet121 elif model_name == 'densenet121': import keras.applications.densenet as densenet self.lib = densenet if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = densenet.DenseNet121(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = densenet.DenseNet121(include_top=(not transfer)) # densenet169 elif model_name == 'densenet169': import keras.applications.densenet as densenet self.lib = densenet if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = densenet.DenseNet169(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = densenet.DenseNet169(include_top=(not transfer)) # densenet201 elif model_name == 'densenet201': import keras.applications.densenet as densenet self.lib = densenet if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = densenet.DenseNet201(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = densenet.DenseNet201(include_top=(not transfer)) # inceptionResnetV2 elif model_name == 'inception_resnet_v2': import keras.applications.inception_resnet_v2 as inception_resnet_v2 self.lib = inception_resnet_v2 if transfer: if input_shape == None: self.input_size = (299, 299) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = self.lib.InceptionResNetV2(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (299, 299) self.model = self.lib.InceptionResNetV2(include_top=(not transfer)) # inceptionV3 elif model_name == 'inception_v3': import keras.applications.inception_v3 as inception_v3 self.lib = inception_v3 if transfer: if input_shape == None: self.input_size = (299, 299) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = self.lib.InceptionV3(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (299, 299) self.model = self.lib.InceptionV3(include_top=(not transfer)) # nasnet mobile elif model_name == 'nasnet_mobile': import keras.applications.nasnet as nasnet self.lib = nasnet if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = self.lib.NASNetMobile(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = self.lib.NASNetMobile(include_top=(not transfer)) # nasnet large elif model_name == 'nasnet_large': import keras.applications.nasnet as nasnet self.lib = nasnet if transfer: if input_shape == None: self.input_size = (331, 331) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = self.lib.NASNetLarge(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (331, 331) self.model = self.lib.NASNetLarge(include_top=(not transfer)) # mobilenet elif model_name == 'mobilenet': import keras.applications.mobilenet as mobilenet self.lib = mobilenet if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = self.lib.MobileNet(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = self.lib.MobileNet(include_top=(not transfer)) # mobilenet v2 elif model_name == 'mobilenet_v2': import keras.applications.mobilenet_v2 as mobilenet_v2 self.lib = mobilenet_v2 if transfer: if input_shape == None: self.input_size = (224, 224) input_shape = (self.input_size[0], self.input_size[1], 3) else: self.input_size = (input_shape[0], input_shape[1]) self.model = self.lib.MobileNetV2(include_top=(not transfer), input_shape=input_shape) else: self.input_size = (224, 224) self.model = self.lib.MobileNetV2(include_top=(not transfer))
# list all CPUs and GPUs device_list = K.get_session().list_devices() # number of GPUs gpu_number = np.count_nonzero( ['device:GPU' in str(x) for x in device_list]) # instantiate model with tf.device('/cpu:0'): # we start with the DenseNet without the final Dense layer, because it has a softmax activation, and we only # want to classify 1 class. So then we manually add an extra Dense layer with a sigmoid activation as final # output # # DenseNet121: blocks=[6, 12, 24, 16] base_model = densenet.DenseNet121(include_top=False, weights=None, input_shape=(401, 401, 3), pooling='avg') x = Dense(units=1, activation='sigmoid', name='fc1')(base_model.output) model = Model(inputs=base_model.input, outputs=x) del base_model saved_model_filename = os.path.join( saved_models_dir, experiment_id + '_model_fold_' + str(i_fold) + '.h5') # the number of training images has to be a multiple of the batch_size. Otherwise, BatchNormalization # produces NaNs num_train_onecell_im_to_use = int( np.floor(train_onecell_im.shape[0] / batch_size) * batch_size) train_onecell_im = train_onecell_im[0:num_train_onecell_im_to_use, :, :, :] train_onecell_dice = train_onecell_dice[0:num_train_onecell_im_to_use]
def get_model(): num_classes = 10 input_shape = (MODELS[MODEL]['size'], MODELS[MODEL]['size'], 3) #preprocess = imagenet_utils.preprocess_input input_image = Input(shape=input_shape) if MODEL == "densenet121": base_model = densenet.DenseNet121(include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) elif MODEL == "densenet169": base_model = densenet.DenseNet169(include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) elif MODEL == "densenet201": base_model = densenet.DenseNet201(include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) elif MODEL == "inceptionresnet": base_model = inception_resnet_v2.InceptionResNetV2( include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) elif MODEL == "inception": base_model = inception_v3.InceptionV3(include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) elif MODEL == "mobilenet": base_model = mobilenet.MobileNet(include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) elif MODEL == "resnet": base_model = resnet50.ResNet50(include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) elif MODEL == "vgg16": base_model = vgg16.VGG16(include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) elif MODEL == "vgg19": base_model = vgg19.VGG19(include_top=False, pooling=None, weights='imagenet', input_shape=input_shape) else: print("Bad model type:", MODEL) sys.exit(-1) x = input_image x = base_model(x) x = Reshape((-1, ))(x) #x = Dropout(rate=?)(x) x = Dense(512, activation='relu', name='fc1')(x) x = Dropout(0.3, name='dropout_fc1')(x) x = Dense(128, activation='relu', name='fc2')(x) x = Dropout(0.3, name='dropout_fc2')(x) prediction = Dense(nclass, activation="softmax", name="predictions")(x) # this is the model we will train my_model = Model(inputs=(input_image), outputs=prediction) # compile the model (should be done *after* setting layers to non-trainable) opt = optimizers.Adam(lr=1e-4) my_model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['acc']) my_model.summary() return my_model
imgNumber = 1 smooth = True nPatches = 9 #can be None if patches should not be considered weightImage = 9 weightHeatmap = 2 weightPatches = 3 filepath = "D:\\DataSetImage\\All\\" #data = pd.read_excel("D:\\DataSetImage\\All\\ImageDataset_Mos.xlsx", index_col=0).T data = pd.read_excel( "D:\\DataSetImage\\All\\Copy of ImageDataset_DMOS(1).xlsx", index_col=0).T mos = data[fileName][7] img_path = filepath + fileName plotLocalPredictions(model, densenet.DenseNet121, densenet.preprocess_input, img_path, mos=mos, smooth=smooth, nPatches=nPatches, img_weights=(weightImage, weightHeatmap, weightPatches), filename=fileName + "_localPreds") #%% ----------------------------------------- Count layers ----------------------------------------------------------- model = densenet.DenseNet121(weights=None) countLayers(model, 7)
#%% [markdown] # ## Extract features using ResNet50 model #%% print("%s ResNet50 feature extraction - start" % (datetime.datetime.now())) model = resnet50.ResNet50() features = extract_features(directory, model, processor=resnet50.preprocess_input) c.SaveFeatures(features, "resnet_features.pkl") print('Extracted Features: %d' % len(features)) print("%s Feature extraction - end" % (datetime.datetime.now())) # Save to file #%% [markdown] # ## Extract features using DenseNet121 model #%% print("%s DenseNet121 feature extraction - start" % (datetime.datetime.now())) model = densenet121.DenseNet121() features = extract_features(directory, model, processor=densenet121.preprocess_input) c.SaveFeatures(features, "densenet_features.pkl") print('Extracted Features: %d' % len(features)) print("%s Feature extraction - end" % (datetime.datetime.now()))
decoded_preds[0][0][2]), (20, y_pos), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 255), 2) # Path of the input image to be classified: img_path = 'car.jpg' # Load some available models: model_inception_v3 = inception_v3.InceptionV3(weights='imagenet') model_vgg_16 = vgg16.VGG16(weights='imagenet') model_vgg_19 = vgg19.VGG19(weights='imagenet') model_resnet_50 = resnet50.ResNet50(weights='imagenet') model_mobilenet = mobilenet.MobileNet(weights='imagenet') model_xception = xception.Xception(weights='imagenet') model_nasnet_mobile = nasnet.NASNetMobile(weights='imagenet') model_densenet_121 = densenet.DenseNet121(weights='imagenet') # Prepare the image for the corresponding architecture: x_inception_v3 = preprocessing_image(img_path, (299, 299), inception_v3) x_vgg_16 = preprocessing_image(img_path, (224, 224), vgg16) x_vgg_19 = preprocessing_image(img_path, (224, 224), vgg19) x_resnet_50 = preprocessing_image(img_path, (224, 224), resnet50) x_mobilenet = preprocessing_image(img_path, (224, 224), mobilenet) x_xception = preprocessing_image(img_path, (299, 299), xception) x_nasnet_mobile = preprocessing_image(img_path, (224, 224), nasnet) x_densenet_121 = preprocessing_image(img_path, (224, 224), densenet) # Get the predicted probabilities: preds_inception_v3 = model_inception_v3.predict(x_inception_v3) preds_vgg_16 = model_vgg_16.predict(x_vgg_16) preds_vgg_19 = model_vgg_19.predict(x_vgg_19)
model = load_model(best_epoch_filename) # load model and run bleu test, it also returns the tokenizer and max_length to be used later tokenizer, test_features, test_descriptions, max_length = prepare_evaluate_params( c, model, feature_file_name) # prepare the photo input demo_path = c.flickr_images_directory + "/" img_filepath = demo_path + "1015118661_980735411b.jpg" #img_filepath = "user/mutaz/car1.jpg" import keras.applications.densenet as densenet121 # load a cnn model to examine the classification dense_model = densenet121.DenseNet121() # load a cnn feature representation model, this one is used for producing input to the captioning model dense_model_features = densenet121.DenseNet121() dense_model_features.layers.pop() dense_model_features = Model(inputs=dense_model_features.inputs, outputs=dense_model_features.layers[-1].output) predict_img(model, tokenizer, max_length, img_filepath, dense_model, dense_model_features, densenet121, target_size=(224, 224))
batch_size = 128 steps_per_epoch = len(train_label) // batch_size # from denseid import DenseID from keras.utils.np_utils import to_categorical from keras import backend as K from keras.models import Model from keras.optimizers import SGD, Adam, Nadam, RMSprop from keras.layers import Dense from keras.applications import densenet # model = DenseID(classes = people_num) denseid = 2048 classes = people_num base_model = densenet.DenseNet121(include_top=False, pooling="avg") x = base_model.output x = Dense(denseid, activation="relu", name="denseid")(x) x = Dense(classes, activation="softmax", name="fc")(x) model = Model(inputs=base_model.input, outputs=x, name="DenseID") for layer in base_model.layers: layer.trainable = False opt = Adam(lr=0.001) model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["categorical_accuracy"]) np.random.seed(1024) for _ in range(epochs):
analyzers0 = [ 'lrp.sequential_preset_a_flat', 'gradient', 'guided_backprop' ] model = models0[modelind] sanalyzer = analyzers0[analyzerind] if model == 'vgg16': import keras.applications.vgg16 as vgg16 #Get model model, preprocess = vgg16.VGG16(), vgg16.preprocess_input size = 224 elif model == 'dense121': import keras.applications.densenet as nnnnet model, preprocess = nnnnet.DenseNet121(), nnnnet.preprocess_input size = 224 elif model == 'inception_v3': import keras.applications.inception_v3 as inception_v3 model, preprocess = inception_v3.InceptionV3( ), inception_v3.preprocess_input size = 299 else: print('err') exit() image = load_image(fn, size) # Code snippet. plt.imshow(image / 255)
def get_models(model_name, NUM_CLASSES): #region Normal Inceptionv3,Xception,InceptionResnetV2 if model_name == 'InceptionV3': model = InceptionV3(include_top=True, input_shape=(299, 299, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 # BATCH_SIZE_TRAIN = 64 # 增加GPU之后 elif model_name == 'InceptionV4': from LIBS.Neural_Networks.classification import inception_v4 model = inception_v4.create_inception_v4(nb_classes=NUM_CLASSES, load_weights=False) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 16 elif model_name == 'Xception': model = Xception(include_top=True, input_shape=(299, 299, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 28 # 增加GPU之后 elif model_name == 'InceptionResNetV2': # following keras implemantation don't use dropout # 标准的不用dropout,收敛快了许多, train准确率提高 model = InceptionResNetV2(include_top=True, input_shape=(299, 299, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 # 增加GPU之后 elif model_name == 'MobileNetV2': #Total params: 2,261,827 # model = MobileNetV2(include_top=True, input_shape=(256, 256, 3), weights=None, classes=NUM_CLASSES) model = MobileNetV2(include_top=True, input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 64 elif model_name == 'My_Xception': model = my_xception.Xception(classes=NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 # 增加GPU之后 elif model_name == 'DRN_A18': #params:20,631,682 from LIBS.Neural_Networks.classification import my_DRN_A model = my_DRN_A.DRN_A_Builder.build_DRN_A_18(input_shape=(256, 256, 3), num_classes=NUM_CLASSES) IMAGE_SIZE = 256 BATCH_SIZE_TRAIN = 32 elif model_name == 'DRN_A34': from LIBS.Neural_Networks.classification import my_DRN_A_1 # model = my_DRN_A.DRN_A_Builder.build_DRN_A_34(input_shape=(256, 256, 3), num_classes=NUM_CLASSES) model = my_DRN_A_1.DRN_A_Builder.build_DRN_A_34( input_shape=(256, 256, 3), num_classes=NUM_CLASSES) IMAGE_SIZE = 256 BATCH_SIZE_TRAIN = 32 elif model_name == 'DRN_A50': from LIBS.Neural_Networks.classification import my_DRN_A model = my_DRN_A.DRN_A_Builder.build_DRN_A_50(input_shape=(256, 256, 3), num_classes=NUM_CLASSES) IMAGE_SIZE = 256 BATCH_SIZE_TRAIN = 32 elif model_name == 'DRN_A101': from LIBS.Neural_Networks.classification import my_DRN_A model = my_DRN_A.DRN_A_Builder.build_DRN_A_101(input_shape=(256, 256, 3), num_classes=NUM_CLASSES) IMAGE_SIZE = 256 BATCH_SIZE_TRAIN = 32 elif model_name == 'DRN_C26': #params:20,631,682 from LIBS.Neural_Networks.classification import my_DRN_C model = my_DRN_C.DRN_C_Builder.build_DRN_C_26(input_shape=(224, 224, 3), num_classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 32 elif model_name == 'DRN_C42': # params:30,750,978 from LIBS.Neural_Networks.classification import my_DRN_C model = my_DRN_C.DRN_C_Builder.build_DRN_C_42(input_shape=(224, 224, 3), num_classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 32 elif model_name == 'DPN92': #DPN92,DPN98,DPN107,DPN137 from LIBS.Neural_Networks.classification import my_dpn model = my_dpn.DPN92(input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 16 # GTX1080 ti 32 exhausted elif model_name == 'DPN98': from LIBS.Neural_Networks.classification import my_dpn model = my_dpn.DPN92(input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 16 # GTX1080 ti 32 exhausted elif model_name == 'DPN107': from LIBS.Neural_Networks.classification import my_dpn model = my_dpn.DPN107(input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 16 # GTX1080 ti 32 exhausted elif model_name == 'DPN137': from LIBS.Neural_Networks.classification import my_dpn model = my_dpn.DPN137(input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 16 # GTX1080 ti 32 exhausted elif model_name == 'DenseNet121': #121,169,201 model = densenet.DenseNet121(input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 32 elif model_name == 'DenseNet169': model = densenet.DenseNet169(input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 32 elif model_name == 'DenseNet201': model = densenet.DenseNet201(input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 16 elif model_name == 'NasnetMobile': model = nasnet.NASNetMobile(input_shape=(224, 224, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 32 elif model_name == 'my_Mnasnet': from LIBS.Neural_Networks.classification import my_Mnasnet model = my_Mnasnet.MnasNet(input_shape=(224, 224, 3), classes=NUM_CLASSES) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 64 elif model_name == "Mnasnet": from LIBS.Neural_Networks.classification import my_Mnasnet model = my_Mnasnet.MnasNet(classes=NUM_CLASSES, input_shape=(224, 224, 3)) IMAGE_SIZE = 224 BATCH_SIZE_TRAIN = 64 elif model_name == 'NasnetMedium': #Trainable params: 22,695,624 model = nasnet.NASNet( input_shape=(299, 299, 3), penultimate_filters=1920, num_blocks=7, stem_block_filters=80, # Large:96, Mobile:32 classes=NUM_CLASSES, default_size=299) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 16 elif model_name == 'NasnetLarge': model = nasnet.NASNetLarge(input_shape=(331, 331, 3), weights=None, classes=NUM_CLASSES) IMAGE_SIZE = 331 BATCH_SIZE_TRAIN = 8 #endregion #region All kinds of ResNet, ResNeXt # 7*32=224, 256, 288, 320, 352, 384 elif model_name == 'ResNet50': from LIBS.Neural_Networks.classification import my_resnet model = my_resnet.ResnetBuilder.build_resnet_50((256, 256, 3), NUM_CLASSES) IMAGE_SIZE = 256 BATCH_SIZE_TRAIN = 32 elif model_name == 'ResNet50_288': from LIBS.Neural_Networks.classification import my_resnet model = my_resnet.ResnetBuilder.build_resnet_50((288, 288, 3), NUM_CLASSES) IMAGE_SIZE = 288 BATCH_SIZE_TRAIN = 32 elif model_name == 'ResNet101': from LIBS.Neural_Networks.classification import my_resnet model = my_resnet.ResnetBuilder.build_resnet_101((256, 256, 3), NUM_CLASSES) IMAGE_SIZE = 256 BATCH_SIZE_TRAIN = 32 elif model_name == 'ResNet448': from LIBS.Neural_Networks.classification import my_resnet # model = my_resnet.ResnetBuilder.build_resnet_mymodel_34_64_5((448, 448, 3), NUM_CLASSES) model = my_resnet.ResnetBuilder.build_resnet_448_1((448, 448, 3), NUM_CLASSES) IMAGE_SIZE = 448 BATCH_SIZE_TRAIN = 32 # 增加GPU之后 elif model_name == 'ResNext448': from LIBS.Neural_Networks.classification import resNeXt model = resNeXt.my_ResNext(input_shape=(448, 448, 3), classes=NUM_CLASSES) IMAGE_SIZE = 448 BATCH_SIZE_TRAIN = 16 # 增加GPU之后 #endregion #region multi label (Inceptionv3, Xception, InceptionResnetV2) elif model_name == 'Multi_label_InceptionV3': base_model = InceptionV3(include_top=False, weights=None) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_label_SE_InceptionV3': from LIBS.Neural_Networks.classification import my_se_inception_v3 base_model = my_se_inception_v3.SE_InceptionV3((299, 299, 3), include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_label_Xception': base_model = Xception(include_top=False, weights=None) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 # 增加GPU之后 elif model_name == 'Multi_label_my_Xception': from LIBS.Neural_Networks.classification import my_xception model = my_xception.Xception(classes=NUM_CLASSES, multi_labels=True) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 # 增加GPU之后 elif model_name == 'Multi_label_InceptionResNetV2': base_model = InceptionResNetV2(include_top=False, weights=None) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_label_DRN_A_Xception': from LIBS.Neural_Networks import DRN_A_Xception_Builder base_model = DRN_A_Xception_Builder.build_DRN_A_xception( input_shape=(288, 288, 3), num_classes=29, include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 288 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_NasnetMedium': base_model = nasnet.NASNet( input_shape=(299, 299, 3), penultimate_filters=1920, num_blocks=7, stem_block_filters=80, # Large:96, Mobile:32 classes=NUM_CLASSES, default_size=299, include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 16 elif model_name == 'Multi_DRN_A18': from LIBS.Neural_Networks.classification import my_DRN_A #original number of parameters, resnet34 :21.8M, resnet50:25.6M base_model = my_DRN_A.DRN_A_Builder.build_DRN_A_18( input_shape=(288, 288, 3), num_classes=NUM_CLASSES, include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 288 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_DRN_A34': from LIBS.Neural_Networks.classification import my_DRN_A base_model = my_DRN_A.DRN_A_Builder.build_DRN_A_34( input_shape=(288, 288, 3), num_classes=NUM_CLASSES, include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 288 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_DRN_A50': from LIBS.Neural_Networks.classification import my_DRN_A base_model = my_DRN_A.DRN_A_Builder.build_DRN_A_50( input_shape=(288, 288, 3), num_classes=NUM_CLASSES, include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 288 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_DRN_C26': #params:20,631,682 from LIBS.Neural_Networks.classification import my_DRN_C base_model = my_DRN_C.DRN_C_Builder.build_DRN_C_26( input_shape=(288, 288, 3), num_classes=NUM_CLASSES, include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 288 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_DRN_C42': # params:30,750,978 from LIBS.Neural_Networks.classification import my_DRN_C base_model = my_DRN_C.DRN_C_Builder.build_DRN_C_42( input_shape=(288, 288, 3), num_classes=NUM_CLASSES, include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 288 BATCH_SIZE_TRAIN = 32 elif model_name == 'Multi_DRN_C58': from LIBS.Neural_Networks.classification import my_DRN_C base_model = my_DRN_C.DRN_C_Builder.build_DRN_C_58( input_shape=(288, 288, 3), num_classes=NUM_CLASSES, include_top=False) model = add_multilabels_top(base_model, NUM_CLASSES) IMAGE_SIZE = 288 BATCH_SIZE_TRAIN = 32 # endregion #region SE Net(Se_InceptionV3, Se_InceptionResNetV2等) elif model_name == 'Se_InceptionV3': from LIBS.Neural_Networks.classification import my_se_inception_v3 model = my_se_inception_v3.SE_InceptionV3((299, 299, 3), classes=NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 48 elif model_name == 'Se_InceptionResNetV2': from LIBS.Neural_Networks.classification import my_se_inception_resnet_v2 model = my_se_inception_resnet_v2.SE_InceptionResNetV2( (299, 299, 3), classes=NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 elif model_name == 'Se_Resnext': from LIBS.Neural_Networks import my_se_resnext model = my_se_resnext.SEResNextImageNet((299, 299, 3), classes=NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 24 elif model_name == 'Se_Resnet50': from LIBS.Neural_Networks import my_se_resnet model = my_se_resnet.SEResNet50((299, 299, 3), classes=NUM_CLASSES) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 32 #endregion '''other models #region multi label SE_NET elif model_name == 'Multi_label_Se_InceptionV3': from Neural_Networks import my_se_inception_v3 base_model = my_se_inception_v3.SE_InceptionV3((299, 299, 3), include_top=False, classes=29, weights=None) x = base_model.output from keras.layers import GlobalAveragePooling2D, Dense from keras.models import Model x = GlobalAveragePooling2D()(x) predictions = Dense(NUM_CLASSES, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=predictions) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 48 elif model_name == 'Multi_label_Se_InceptionResNetV2': from Neural_Networks import my_se_inception_resnet_v2 base_model = my_se_inception_resnet_v2.SE_InceptionResNetV2((299, 299, 3), include_top=False, classes=29, weights=None) x = base_model.output from keras.layers import GlobalAveragePooling2D, Dense from keras.models import Model x = GlobalAveragePooling2D()(x) predictions = Dense(NUM_CLASSES, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=predictions) IMAGE_SIZE = 299 BATCH_SIZE_TRAIN = 24 BATCH_SIZE_TRAIN = 32 # 增加GPU之后 #endregion ''' return model, IMAGE_SIZE, BATCH_SIZE_TRAIN
def build_cnn(img_height=None, num_channel=None, reg=None, latent_fea=None, num_normal_class=None, cnn_type=None): """Build CNN or OSRNET. :param img_height: Input image height (width should be equal to this). :param num_channel: Number of image channels. :param reg: Decay of regularization terms. :param latent_fea: Number of latent features. :param num_normal_class: Number of known classes. :param cnn_type: The name of the CNN used as a backbone: modified_vgg, alexnet, mlp, densenet, etc. :return: A list of models. """ # ==================== Constants Definition ==================== acti_func = 'linear' clf_acti = 'softmax' acti_alpha = 0.2 set_bias = False weights_init = tn(mean=0, stddev=0.01) # weights_init = 'glorot_uniform' bn_eps = 1e-3 bn_m = 0.99 logits_layer = None # ==================== General Input Layer ==================== input_layer = Input(shape=(img_height, img_height, num_channel), name='input_layer') # ==================== CNN Pool ==================== if cnn_type == 'alexnet': # ==================== AlexNet ==================== conv_1 = Conv2D(filters=96, kernel_size=(5, 5), activation=acti_func, name='conv_1', dilation_rate=(4, 4), kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(input_layer) lrelu_1 = LeakyReLU(alpha=acti_alpha)(conv_1) pool_1 = MaxPooling2D(pool_size=(2, 2), name='pool_1')(lrelu_1) bn_1 = BatchNormalization(name='bn_1')(pool_1) conv_2 = Conv2D(filters=256, kernel_size=(3, 3), activation=acti_func, name='conv_2', dilation_rate=(2, 2), padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_1) lrelu_2 = LeakyReLU(alpha=acti_alpha)(conv_2) pool_2 = MaxPooling2D(pool_size=(2, 2), name='pool_2')(lrelu_2) bn_2 = BatchNormalization(name='bn_2')(pool_2) conv_3 = Conv2D(filters=384, kernel_size=(3, 3), activation=acti_func, name='conv_3', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_2) lrelu_3 = LeakyReLU(alpha=acti_alpha)(conv_3) bn_3 = BatchNormalization(name='bn_3')(lrelu_3) conv_4 = Conv2D(filters=384, kernel_size=(3, 3), activation=acti_func, name='conv_4', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_3) lrelu_4 = LeakyReLU(alpha=acti_alpha)(conv_4) bn_4 = BatchNormalization(name='bn_4')(lrelu_4) conv_5 = Conv2D(filters=256, kernel_size=(3, 3), activation=acti_func, name='conv_5', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_4) lrelu_5 = LeakyReLU(alpha=acti_alpha)(conv_5) # pool_6 = MaxPooling2D(pool_size=(2, 2), name='pool_6')(lrelu_5) # # flt_7 = Flatten(name='flt_7')(pool_6) flt_7 = GlobalAveragePooling2D()(lrelu_5) dense_8 = Dense(units=4096, activation=acti_func, name='dense_8', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(flt_7) lrelu_8 = LeakyReLU(alpha=acti_alpha)(dense_8) drop_8 = Dropout(rate=0.5)(lrelu_8) dense_9 = Dense(units=latent_fea, activation=acti_func, name='dense_9', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(drop_8) lrelu_9 = LeakyReLU(alpha=acti_alpha)(dense_9) drop_9 = Dropout(rate=0.5)(lrelu_9) dense_10 = Dense(units=num_normal_class, activation='softmax', name='dense_10', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(drop_9) # dense_10 = RBFLayer(output_dim=num_normal_class)(drop_9) top_layer = Reshape(target_shape=(-1, ), name='top_layer')(dense_10) latent_layer = Reshape(target_shape=(-1, ), name='latent_layer')(dense_9) # latent_layer = Reshape(target_shape=(-1,), name='latent_layer')(lrelu_9) elif cnn_type == 'modified_vgg': conv_1 = Conv2D(filters=32, kernel_size=(3, 3), activation=acti_func, name='conv_1', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(input_layer) conv_11 = Conv2D(filters=32, kernel_size=(3, 3), activation=acti_func, name='conv_11', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(conv_1) conv_1 = Concatenate()([conv_1, conv_11]) # 32x32x64 lrelu_1 = LeakyReLU(alpha=acti_alpha)(conv_1) pool_1 = AveragePooling2D(pool_size=(2, 2), name='pool_1')(lrelu_1) # 16x16 / 14x14 bn_1 = BatchNormalization(momentum=bn_m, epsilon=bn_eps, name='bn_1')(pool_1) conv_2 = Conv2D(filters=64, kernel_size=(3, 3), activation=acti_func, name='conv_2', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_1) conv_22 = Conv2D(filters=64, kernel_size=(3, 3), activation=acti_func, name='conv_22', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(conv_2) conv_2 = Concatenate()([conv_2, conv_22]) # 16x16x128 lrelu_2 = LeakyReLU(alpha=acti_alpha)(conv_2) pool_2 = AveragePooling2D(pool_size=(2, 2), name='pool_2')(lrelu_2) # 8x8 / 7x7 if img_height == 28: pool_2 = ZeroPadding2D(padding=(1, 1))( pool_2) # zero-padding if mnist or fashion-mnist bn_2 = BatchNormalization(momentum=bn_m, epsilon=bn_eps, name='bn_2')(pool_2) conv_3 = Conv2D(filters=128, kernel_size=(3, 3), activation=acti_func, name='conv_3', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_2) conv_33 = Conv2D(filters=128, kernel_size=(3, 3), activation=acti_func, name='conv_33', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(conv_3) conv_3 = Concatenate()([conv_3, conv_33]) # 8x8x256 lrelu_3 = LeakyReLU(alpha=acti_alpha)(conv_3) pool_3 = AveragePooling2D(pool_size=(2, 2), name='pool_3')(lrelu_3) # 4x4 bn_3 = BatchNormalization(momentum=bn_m, epsilon=bn_eps, name='bn_3')(pool_3) conv_4 = Conv2D(filters=256, kernel_size=(3, 3), activation=acti_func, name='conv_4', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_3) conv_44 = Conv2D(filters=256, kernel_size=(3, 3), activation=acti_func, name='conv_44', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(conv_4) conv_4 = Concatenate()([conv_4, conv_44]) # 4x4x512 lrelu_4 = LeakyReLU(alpha=acti_alpha)(conv_4) pool_4 = AveragePooling2D(pool_size=(2, 2), name='pool_4')(lrelu_4) # 2x2 bn_4 = BatchNormalization(momentum=bn_m, epsilon=bn_eps, name='bn_4')(pool_4) conv_5 = Conv2D(filters=256, kernel_size=(1, 1), activation=acti_func, name='conv_5', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_4) # 2x2 conv_55 = Conv2D(filters=256, kernel_size=(1, 1), activation=acti_func, name='conv_55', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_4) conv_5 = Concatenate()([conv_5, conv_55]) # 2x2x512 lrelu_5 = LeakyReLU(alpha=acti_alpha)(conv_5) bn_5 = BatchNormalization(name='bn_5')(lrelu_5) # flt_7 = GlobalAveragePooling2D()(bn_5) flt_7 = Flatten()(bn_5) dense_8 = Dense(units=256, activation=acti_func, name='dense_8', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(flt_7) lrelu_8 = LeakyReLU(alpha=acti_alpha)(dense_8) drop_8 = Dropout(rate=0.5)(lrelu_8) # drop_8 = BatchNormalization()(lrelu_8) dense_9 = Dense(units=latent_fea, activation=acti_func, name='dense_9', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(drop_8) lrelu_9 = LeakyReLU(alpha=acti_alpha)(dense_9) drop_9 = Dropout(rate=0.5)(lrelu_9) # drop_9 = BatchNormalization()(lrelu_9) dense_10 = Dense(units=num_normal_class, activation='linear', name='dense_10', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(drop_9) sf_10 = Softmax()(dense_10) top_layer = Reshape(target_shape=(-1, ), name='top_layer')(sf_10) latent_layer = Reshape(target_shape=(-1, ), name='latent_layer')(lrelu_9) logits_layer = Reshape(target_shape=(-1, ), name='logits_layer')(dense_10) elif cnn_type == 'logits_cnn': conv_1 = Conv2D(filters=32, kernel_size=(7, 7), activation=acti_func, name='conv_1', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(input_layer) conv_11 = Conv2D(filters=32, kernel_size=(7, 7), activation=acti_func, name='conv_11', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(conv_1) conv_1 = Concatenate()([conv_1, conv_11]) lrelu_1 = LeakyReLU(alpha=acti_alpha)(conv_1) pool_1 = AveragePooling2D(pool_size=(2, 2), name='pool_1')(lrelu_1) # 16x16 / 14x14 bn_1 = BatchNormalization(name='bn_1')(pool_1) conv_2 = Conv2D(filters=64, kernel_size=(3, 3), activation=acti_func, name='conv_2', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_1) conv_22 = Conv2D(filters=64, kernel_size=(3, 3), activation=acti_func, name='conv_22', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(conv_2) conv_2 = Concatenate()([conv_2, conv_22]) lrelu_2 = LeakyReLU(alpha=acti_alpha)(conv_2) pool_2 = AveragePooling2D(pool_size=(2, 2), name='pool_2')(lrelu_2) # 8x8 / 7x7 if img_height == 28: pool_2 = ZeroPadding2D(padding=(1, 1))(pool_2) bn_2 = BatchNormalization(name='bn_2')(pool_2) conv_3 = Conv2D(filters=128, kernel_size=(3, 3), activation=acti_func, name='conv_3', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_2) conv_33 = Conv2D(filters=128, kernel_size=(3, 3), activation=acti_func, name='conv_33', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(conv_3) conv_3 = Concatenate()([conv_3, conv_33]) lrelu_3 = LeakyReLU(alpha=acti_alpha)(conv_3) pool_3 = AveragePooling2D(pool_size=(2, 2), name='pool_3')(lrelu_3) # 4x4 bn_3 = BatchNormalization(name='bn_3')(pool_3) conv_4 = Conv2D(filters=256, kernel_size=(3, 3), activation=acti_func, name='conv_4', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_3) conv_44 = Conv2D(filters=256, kernel_size=(3, 3), activation=acti_func, name='conv_44', padding='same', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(conv_4) conv_4 = Concatenate()([conv_4, conv_44]) lrelu_4 = LeakyReLU(alpha=acti_alpha)(conv_4) pool_4 = AveragePooling2D(pool_size=(2, 2), name='pool_4')(lrelu_4) # 2x2 bn_4 = BatchNormalization(name='bn_4')(pool_4) conv_5 = Conv2D(filters=256, kernel_size=(1, 1), activation=acti_func, name='conv_5', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_4) # 2x2 conv_55 = Conv2D(filters=256, kernel_size=(1, 1), activation=acti_func, name='conv_55', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_4) conv_5 = Concatenate()([conv_5, conv_55]) lrelu_5 = LeakyReLU(alpha=acti_alpha)(conv_5) bn_5 = BatchNormalization(name='bn_5')(lrelu_5) # flt_7 = GlobalAveragePooling2D()(bn_5) flt_7 = Flatten()(bn_5) dense_8 = Dense(units=256, activation=acti_func, name='dense_8', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(flt_7) lrelu_8 = LeakyReLU(alpha=acti_alpha)(dense_8) drop_8 = Dropout(rate=0.5)(lrelu_8) # drop_8 = BatchNormalization()(lrelu_8) dense_9 = Dense(units=latent_fea, activation=acti_func, name='dense_9', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(drop_8) lrelu_9 = LeakyReLU(alpha=acti_alpha)(dense_9) drop_9 = Dropout(rate=0.5)(lrelu_9) # drop_9 = BatchNormalization()(lrelu_9) dense_10 = Dense(units=num_normal_class, activation='linear', name='dense_10', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(drop_9) clf_layer = Softmax(name='softmax')(dense_10) top_layer = Reshape(target_shape=(-1, ), name='top_layer')(clf_layer) latent_layer = Reshape(target_shape=(-1, ), name='latent_layer')(dense_10) logits_layer = Reshape(target_shape=(-1, ), name='latent_layer')(dense_10) elif cnn_type == 'mlp': conv_1 = Conv2D(filters=256, kernel_size=(5, 5), activation=acti_func, name='conv_1', dilation_rate=(2, 2), kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(input_layer) lrelu_1 = LeakyReLU(alpha=acti_alpha)(conv_1) bn_1 = BatchNormalization(name='bn_1')(lrelu_1) conv_2 = Conv2D(filters=latent_fea, kernel_size=(5, 5), activation=acti_func, name='conv_2', dilation_rate=(2, 2), kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(bn_1) lrelu_2 = LeakyReLU(alpha=acti_alpha)(conv_2) latent_layer = GlobalAveragePooling2D()(lrelu_2) top_layer = Dense(units=num_normal_class, activation='softmax', name='dense_10', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(latent_layer) elif cnn_type == 'vgg16': if img_height < 48: img_size = 2 * img_height ups = UpSampling2D((2, 2))(input_layer) else: ups = input_layer img_size = img_height if num_channel != 3: conc = Concatenate()([ups, ups, ups]) else: conc = ups latent_layer = vgg16.VGG16(include_top=False, weights=None, input_shape=(img_size, img_size, 3), pooling='avg')(conc) logits_layer = Dense(units=num_normal_class, activation='linear', name='logits_layer', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(latent_layer) top_layer = Softmax()(logits_layer) elif cnn_type == 'vgg19': if img_height < 48: img_size = 2 * img_height ups = UpSampling2D((2, 2))(input_layer) else: ups = input_layer img_size = img_height if num_channel != 3: conc = Concatenate()([ups, ups, ups]) else: conc = ups latent_layer = vgg19.VGG19(include_top=False, weights=None, input_shape=(img_size, img_size, 3), pooling='avg')(conc) top_layer = Dense(units=num_normal_class, activation='softmax', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(latent_layer) elif cnn_type == 'densenet': if img_height == 28: zero_padding = ZeroPadding2D((2, 2))(input_layer) conc = Concatenate()([zero_padding, zero_padding, zero_padding]) else: conc = input_layer latent_layer = densenet.DenseNet121(include_top=False, weights=None, input_shape=(32, 32, 3), pooling='avg')(conc) top_layer = Dense(units=num_normal_class, activation='softmax', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(latent_layer) elif cnn_type == 'xception': img_size = 3 * img_height ups = UpSampling2D((3, 3))(input_layer) if num_channel != 3: conc = Concatenate()([ups, ups, ups]) else: conc = ups latent_layer = xception.Xception(include_top=False, weights=None, input_shape=(img_size, img_size, 3), pooling='avg')(conc) top_layer = Dense(units=num_normal_class, activation='softmax', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(latent_layer) elif cnn_type == 'resnet': if img_height == 28: ups = UpSampling2D((8, 8))(input_layer) conc = Concatenate()([ups, ups, ups]) else: ups = UpSampling2D((7, 7))(input_layer) conc = ups latent_layer = resnet50.ResNet50(include_top=False, weights=None, input_shape=(224, 224, 3), pooling='avg')(conc) top_layer = Dense(units=num_normal_class, activation='softmax', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(latent_layer) elif cnn_type == 'inception': img_size = 5 * img_height ups = UpSampling2D((5, 5))(input_layer) if num_channel != 3: conc = Concatenate()([ups, ups, ups]) else: conc = ups latent_layer = inception_v3.InceptionV3(include_top=False, weights=None, input_shape=(img_size, img_size, 3), pooling='avg')(conc) top_layer = Dense(units=num_normal_class, activation='softmax', kernel_regularizer=regularizers.l2(reg), use_bias=set_bias, kernel_initializer=weights_init)(latent_layer) else: raise ValueError('No suitable CNN architecture...') cnn = Model(inputs=input_layer, outputs=top_layer, name=cnn_type) cnn_latent = Model(inputs=input_layer, outputs=latent_layer, name=cnn_type + '_latent') cnn_logits = Model(inputs=input_layer, outputs=logits_layer, name=cnn_type + '_logits') # ==================== Intra-Class Networks ==================== input_1 = Input(shape=(img_height, img_height, num_channel), name='input_1') input_2 = Input(shape=(img_height, img_height, num_channel), name='input_2') lat_1 = cnn_latent(input_1) lat_2 = cnn_latent(input_2) latent_dist = Subtract(name='latent_dist')([lat_1, lat_2]) dense_ly = Dense(units=1, activation='sigmoid', name='dense_ly', kernel_regularizer=regularizers.l2(reg))(latent_dist) ic_network = Model(inputs=[input_1, input_2], outputs=dense_ly) # ==================== Joint layers ==================== dense_11 = Dense(units=num_normal_class - 1, activation='softmax', name='dense_joint', kernel_regularizer=regularizers.l2(reg), use_bias=not set_bias, kernel_initializer=weights_init)(latent_layer) joint_layer = Reshape(target_shape=(-1, ), name='joint_layer')(dense_11) joint_cnn = Model(inputs=input_layer, outputs=[top_layer, joint_layer]) cnn.summary() ic_network.summary() return cnn, cnn_latent, joint_cnn, cnn_logits