def prepare_model(self, x_train, y_train, x_val, y_val, params): conv_base = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(150, 150, 3)) model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add(layers.Dense(256, activation=params['activation'])) model.add(layers.Dense(len(self.classes), activation='softmax')) conv_base.trainable = False model.compile(loss=params['losses'], optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) validation_data = [x_val, y_val] history = model.fit(x_train, y_train, steps_per_epoch=params['batch_size'], epochs=params['epochs'], validation_data=validation_data, validation_steps=params['batch_size']) #self.history = history #self.model = model return history, model
TEST_SIZE = [192, 256, 320] for size in TEST_SIZE: print('### Use Pict size ' + str(size)) trainImg, clearTrainLabel = load_image(size, segment=True) X_train, X_valid, y_train, y_valid = train_test_split( trainImg, clearTrainLabel, test_size=0.2, random_state=33, stratify=clearTrainLabel) datagen = ImageDataGenerator() datagen.fit(X_train) base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(size, size, 3)) x = base_model.output x = Flatten()(x) x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(256, activation='relu')(x) predictions = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) base_model.trainable = False for layer in base_model.layers: layer.trainable = False
def get_category_number(name, config): return config['category_map'][name.split('_')[0]] if __name__ == '__main__': # Load configuration. with open('config.yaml') as f: config = yaml.load(f) mode = config['mode'] if mode == 'train_classify': # Use a new model. input_layer = Input(shape=(299, 299, 3)) rn_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) rn_model.trainable = False x = rn_model(input_layer) x = Flatten()(x) x = Dense(512, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(512, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(10, activation='softmax')(x) model = Model(inputs=input_layer, outputs=x) sgd = SGD(0.0002) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc']) # Prepare data. image_root = config['image_root']
# from tensorflow.python.keras.applications.vgg16 import VGG16 from keras.applications import InceptionV3 from keras.applications import InceptionResNetV2 # conv_base = Xception(weights='imagenet', include_top=False, # input_shape=(150,150,3)) # conv_base = InceptionV3() # (224, 224, 3) conv_base = InceptionResNetV2() conv_base.summary() # conv_base.summary() from keras import models, layers model = models.Sequential() model.add(conv_base) # model.add(layers.Flatten()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.summary()
print('****************') classes_name = [0 for i in range(103)] for cls, idx in train_batches.class_indices.items(): print('Class #{} = {}'.format(idx, cls)) classes_name[idx] = cls print(classes_name) print('****************') # build our classifier model based on pre-trained InceptionResNetV2: # 1. we don't include the top (fully connected) layers of InceptionResNetV2 # 2. we add a DropOut layer followed by a Dense (fully connected) # layer which generates softmax class score for each class # 3. we compile the final model using an Adam optimizer, with a # low learning rate (since we are 'fine-tuning') base_model = InceptionResNetV2(include_top=False, weights='imagenet', input_tensor=None, input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3)) for layer in base_model.layers: layer.trainable = True # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dropout(0.5)(x) # and a logistic layer -- let's say we have 200 classes predictions = Dense(NUM_CLASSES, activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions)
def main(cropped): train_generator, val_generator, test_generator = get_generators(cropped) conv_base = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(100, 100, 3)) model = models.Sequential() model.add(conv_base) model.add(layers.Flatten()) model.add( layers.Dense(256, activation='relu', kernel_initializer='orthogonal', bias_initializer='zeros')) model.add( layers.Dense(27, activation='softmax', kernel_initializer='orthogonal', bias_initializer='zeros')) print(model.summary()) model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(learning_rate=0.001, momentum=0.9), metrics=['acc']) keras_callbacks = [ EarlyStopping(monitor='val_loss', patience=3, mode='min', min_delta=0.0001, restore_best_weights=True) ] train_stpep = train_generator.n // train_generator.batch_size val_stpep = val_generator.n // val_generator.batch_size history = model.fit_generator( train_generator, steps_per_epoch=train_stpep, epochs=30, validation_data=val_generator, validation_steps=val_stpep, callbacks=keras_callbacks, ) test_acc, test_f1 = acc_and_f1(model, test_generator) print("Final Test Accuracy: " + str(test_acc)) print("Final Test F1: " + str(test_f1)) # serialize model to JSON model_json = model.to_json() if args.cropped: folder = 'models/cropped_resnet' else: folder = 'models/uncropped_resnet' if not os.path.exists(folder): os.mkdir(folder) with open(folder + "/model_resnet.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights(folder + "/model.h5") print("Saved model to disk")
test_image_gen = image_gen.flow_from_dataframe(dataframe = df_detailed, directory= './train', x_col = 'patientId', y_col = 'class', target_size=(256,256), color_mode='rgb', classes= None, class_mode='categorical', batch_size=16, shuffle=True, subset = 'validation' ) orig_net = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(256,256,3)) filters = GlobalAveragePooling2D()(orig_net.output) classifiers = Dense(3, activation='softmax', bias_initializer='ones')(filters) model = Model(inputs=orig_net.inputs, outputs=classifiers) model.compile(loss='categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) from keras.callbacks import ModelCheckpoint, EarlyStopping
if __name__ == '__main__': save_dir = os.path.join(os.getcwd(), 'saved_models') model_name = 'ResNetV2_wood_model.hdf5' weight_name = 'ResNetV2_wood_weight.hdf5' model_path = os.path.join(save_dir, model_name) weight_path = os.path.join(save_dir, weight_name) x_train, y_train, x_test, y_test = image_crop.read_data() x_train_resized = image_crop.resize_imgs(x_train) y = to_categorical(y_train, num_classes=34) model = InceptionResNetV2(include_top=True, weights=None, classes=34) checkpoint = ModelCheckpoint(filepath=os.path.join( save_dir, 'ResNetV2_weight.{epoch:02d}-{loss:.2f}.hdf5'), verbose=1) opt = RMSprop(lr=2e-5) model.compile(optimizer=opt, loss=losses.categorical_crossentropy, metrics=[metrics.categorical_accuracy]) model.fit(x_train_resized, y, epochs=10, batch_size=10, callbacks=[checkpoint])
from keras.utils import to_categorical from sklearn import metrics from Utilities.Metrics import MetricsWithGenerator train_dir = 'C:/Users/Federico/PycharmProjects/Image-Classification/Datasets/cats-dogs/training_set/' validation_dir = 'C:/Users/Federico/PycharmProjects/Image-Classification/Datasets/cats-dogs/test_set/' # train_dir = 'C:/Users/Federico/PycharmProjects/Image-Classification/Datasets/fruits/Training/' # validation_dir = 'C:/Users/Federico/PycharmProjects/Image-Classification/Datasets/fruits/Test/' image_size = 200 epochs = 10 # Load the VGG model inceptResNet_conv = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3)) # Freeze all the layers except last 4 for layer in inceptResNet_conv.layers[:-4]: layer.trainable = False # Create the model model = models.Sequential() # Add the vgg convolutional base model model.add(inceptResNet_conv) # Add new layers model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu'))
def design_model(train, test, ytrain, ytest, epoch, nclas, value, val): #intialize time for training and testing tr = 0.0 tt = 0.0 #start time st = time.time() #input shape image_input = Input(shape=(None, None, 3)) #adding a lambda layer to reshape to size 224 by 224 #this make sure that image are converted to 224,224 before entering the model prep = Lambda(lambda x: tf.image.resize(x, (224, 224)))(image_input) #using pretained weights from imagnet for transfer learning #using InceptionResNetV2 model dmodel = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(224, 224, 3))(prep) dx = GlobalAveragePooling2D()(dmodel) i_model = Model(image_input, dx) i_model.summary() #get the deep feature from InceptionResNetV2, feature extraction from InceptionResNetV2 model #get training deep features df_train_i = i_model.predict(train, batch_size=32, workers=50, use_multiprocessing=True, verbose=1) #get testing deep features df_test_i = i_model.predict(test, batch_size=32, workers=50, use_multiprocessing=True, verbose=1) #using pretained weights from imagnet for transfer learning #using ResNet152V2 model vmodel = ResNet152V2(include_top=False, weights='imagenet', input_shape=(224, 224, 3))(prep) vx = GlobalAveragePooling2D()(vmodel) r_model = Model(image_input, vx) r_model.summary() #get deep features from ResNet152V2, extraction of deep features #get training features df_train_r = r_model.predict(train, batch_size=32, workers=50, use_multiprocessing=True, verbose=1) #get testing features df_test_r = r_model.predict(test, batch_size=32, workers=50, use_multiprocessing=True, verbose=1) #combining of deep features from both the InceptionResNetV2 and ResNet152V2 models, Feature fusion or feature combination #fusion of deep features extracted from InceptionResNetV2 and ResNet152V2 model and creating final_train and final_test set of features final_train = tf.keras.layers.Concatenate()([df_train_i, df_train_r]) final_test = tf.keras.layers.Concatenate()([df_test_i, df_test_r]) #my own fully connected classifier(ANN) classifier = Sequential([ Dense(4096, activation='relu', input_shape=final_train[0].shape), Dropout(0.5), Dense(nclas, activation='softmax') ]) #optimizer opt = Adam(lr=1e-4, decay=1e-4 / 50) #compile my own classifier classifier.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) #reduce_lr method is used to reduce the learning rate if the learning rate is stagnant or if there are no major improvements during training reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.2, patience=5, min_lr=0.001) #early stopping method is used to montior the loss if there are no significant reductions in loss then halts the training es = EarlyStopping(monitor='loss', patience=10) #fit the model history = classifier.fit(final_train, ytrain, epochs=epoch, batch_size=32, shuffle=True, verbose=1, workers=50, use_multiprocessing=True, callbacks=[reduce_lr, es]) #total time for training tr = time.time() - st #plot the graph of accuracy and loss for training plotgraph(history, value, val) #start time for testing st = time.time() #evalute the model (loss, accuracy) = classifier.evaluate(final_test, ytest, batch_size=32, verbose=1, workers=50, use_multiprocessing=True) #total time for testing tt = time.time() - st #training and testing accuracy train_acc = history.history["accuracy"][-1:][0] test_acc = accuracy #free memory del i_model, df_train_i, df_test_i, r_model, df_train_r, df_test_r, final_train, final_test, classifier, history return tr, tt, train_acc, test_acc
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
""" if epoch > 500: lr *= 0.5e-3 elif epoch > 200: lr *= 1e-3 elif epoch > 100: lr *= 1e-2 elif epoch > 50: lr *= 1e-1 print('Learning rate: ', lr) return lr model_ir2 = InceptionResNetV2(include_top=False, weights="imagenet", input_shape=(XSIZE, YSIZE, ZSIZE)) for layer in model_ir2.layers: layer.trainable = True model = GlobalAveragePooling2D(name='GlobalAverage')(model_ir2.output) model = Dense(256, activation='relu', kernel_regularizer=l2(0.04))(model) model = Dropout(0.5)(model) model = Dense(1, activation='sigmoid')(model) model_ir2_sp = Model(model_ir2.input, model) model_ir2_sp.compile(loss='binary_crossentropy', optimizer=Adam(lr=lr_schedule(0)), metrics=['acc']) model_ir2_sp.summary() # Prepare model model saving directory. save_dir = os.path.join(os.getcwd(), 'saved_models')
def __init__(self, name): if name.lowwer() == 'resnet50': self.model = ResNet50() elif name.lowwer() == 'inceptionresnetv2': self.model = InceptionResNetV2()
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, BatchNormalization, Activation from keras.applications import MobileNet, MobileNetV2, DenseNet121, DenseNet169, DenseNet201 from keras.applications import NASNetLarge, NASNetMobile from keras.applications import Xception, ResNet101, InceptionResNetV2 from keras.optimizers import Adam # model = VGG19() # model = Xception() # model = ResNet101() # model = ResNet101V2() # model = ResNet152() # model = ResNet152V2() # model = ResNet50() # model = ResNet50V2() # model = InceptionV3() model = InceptionResNetV2() # model = MobileNet() # model = MobileNetV2() # model = DenseNet121() # model = DenseNet169() # model = DenseNet201() # model = NASNetLarge() # model = NASNetMobile() """ vgg16 = VGG16( weights='imagenet', include_top=False, classes=10, input_shape=(224,224,3), # classifier_activation="softmax" )
def build_network(self, model_name, fine_tune): if model_name.lower() == 'vgg16': if fine_tune: base_model = VGG16(include_top=False, input_shape=(None, None, 3), pooling='avg') for layer in base_model.layers: if layer.name.startswith('block5'): layer.trainable = True else: layer.trainable = False x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(512, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) model.summary() return model else: base_model = VGG16(include_top=False, input_shape=(None, None, 3), pooling='avg') for layer in base_model.layers: layer.trainable = True x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(512, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) model.summary() return model elif model_name.lower() == 'resnet50': base_model = ResNet50(include_top=False, input_shape=(None, None, 3), pooling='avg', backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model # elif model_name.lower()=='resnet34': # base_model=ResNet34(include_top=False, input_shape=self.shape, pooling='avg') # x = base_model.output # x = Dense(1024, activation='relu')(x) # x = Dropout(0.5)(x) # x = Dense(1024, activation='relu')(x) # predictions = Dense(1, activation='sigmoid')(x) # model = Model(base_model.input, predictions) # if fine_tune: # for layer in base_model.layers: # layer.trainable = False # model.summary() # return model elif model_name.lower() == 'resnet101': base_model = ResNet101(include_top=False, input_shape=(None, None, 3), pooling='avg', backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model elif model_name.lower() == 'resnet152': base_model = ResNet152(include_top=False, input_shape=(None, None, 3), pooling='avg', backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model elif model_name.lower() == 'inceptionresnetv2': base_model = InceptionResNetV2(include_top=False, input_shape=(None, None, 3), pooling='avg') x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model elif model_name.lower() == 'xception': base_model = Xception(include_top=False, input_shape=(None, None, 3), pooling='avg') x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model elif model_name.lower() == 'densenet121': base_model = DenseNet121(include_top=False, input_shape=(None, None, 3), pooling='avg') x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model elif model_name.lower() == 'densenet169': base_model = DenseNet169(include_top=False, input_shape=(None, None, 3), pooling='avg') x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model elif model_name.lower() == 'densenet201': base_model = DenseNet201(include_top=False, input_shape=(None, None, 3), pooling='avg') x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model elif model_name.lower() == 'nasnetlarge': base_model = NASNetLarge(include_top=False, input_shape=(None, None, 3), pooling='avg') x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model elif model_name.lower() == 'vgg19': base_model = VGG19(include_top=False, input_shape=(None, None, 3), pooling='avg') x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model else: base_model = NASNetMobile(include_top=False, input_shape=(None, None, 3), pooling='avg') x = base_model.output x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(base_model.input, predictions) if fine_tune: for layer in base_model.layers: layer.trainable = False model.summary() return model
from keras.layers import Dense, Flatten, MaxPooling2D, Conv2D, Dropout from keras.applications import InceptionResNetV2 from keras import optimizers from keras.preprocessing.image import ImageDataGenerator ##################### # STEP - 2: The Model ##################### # Making the model model = Sequential() # For this model I'm using InceptionResNetV2 # I'll use imagenet weights here also inception = InceptionResNetV2(include_top=False, weights="imagenet", input_shape=(128, 128, 3), pooling="max") # Adding the inception model and configuting the output layer model.add(inception) model.add(Dense(units=102, activation="softmax")) # Compiling the model model.compile(loss='categorical_crossentropy', optimizer=optimizers.RMSprop(lr=2e-5), metrics=['accuracy']) # Printing the summary of the model print(model.summary()) ############################
from keras.models import Sequential from keras.layers import Dense, Flatten from keras import initializers from keras.optimizers import Adam from tensorflow.keras import layers import resnet_prac as rp import cv2 import os import numpy as np import matplotlib.pyplot as plt model = Sequential() model.add( InceptionResNetV2(include_top=False, input_shape=(75, 75, 3), weights='imagenet')) model.add(Flatten()) model.add(Dense(units=rp.class_num, activation='softmax')) print(model.summary()) # 신기하게도 multi-classify인데도 loss function을 binary-crossentropy를 쓰는 것이 효과적. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) hist = model.fit(rp.X_train, rp.Y_train, batch_size=rp.batch_size, epochs=rp.epochs,
def classifier(self) -> Model: """ Returns the model of this configuration """ model = InceptionResNetV2(include_top=True, weights=None, input_shape=self.data_shape, pooling='avg', classes=self.number_of_classes) model.compile(self.get_optimizer(), loss="categorical_crossentropy", metrics=["accuracy"]) return model
image += 128 return image.clip(min=0, max=255).astype("uint8") def plot_layer_filters(model, layer=1, interpolate=True): # Evaluate layer of convolutions for trained CNN cnn_first_layer = model.get_layer(index=layer).get_weights() cnn_first_layer = np.asanyarray(cnn_first_layer)[0, :, :, :] for i in range(32): plt.subplot(4, 8, i + 1) plt.axis("off") if interpolate: plt.imshow( imresize(rescale(cnn_first_layer[:, :, :, i]), (25, 25), interp="bicubic")) else: plt.imshow(rescale(cnn_first_layer[:, :, :, i])) plt.show() if __name__ == "__main__": # Load models of interest model = load_model("") model_imagenet = InceptionResNetV2(weights="imagenet") plot_layer_filters(model, interpolate=False)
model = Model(dataset) ''' dataset.get_train_length() : 5866 dataset.get_all_validation_data(): 1956 ''' print('dataset.get_train_length()',dataset.get_train_length()) print('dataset.get_validation_length()',dataset.get_validation_length()) x_train, y_train , x_val, y_val =dataset.get_all_processor_data() ''' 实现自己的网络机构 ''' # sqeue = ResNet50( weights=None, include_top=True, input_shape=(300, 300, 3),classes=num_classes) base_model = InceptionResNetV2(weights=weights_path, include_top=False) # 冻结不打算训练的层。这里我冻结了前5层。 for layer in base_model.layers: layer.trainable = False # 增加定制层 x = base_model.output # x = Flatten()(x) x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(1024)(x) x = Dropout(0.5)(x) # Classification block x = GlobalAveragePooling2D(name='avg_pool')(x) predictions = Dense(num_classes, activation='softmax', name='predictions')(x)
def generate_model_base(preset, width, height, channel, weights_init): ''' モデルを作成する # Arguments preset: プリセットモデルの名前 width: 入力画像の幅 height: 入力画像の高さ channel: 入力画像のチャンネル数 class_num: 分類クラス数 weights_init: 初期値(None, imagenet) # Returns keras.models.Model オブジェクト ''' os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # os.environ["CUDA_VISIBLE_DEVICES"] = "-1" from keras.layers import Dense, BatchNormalization, Dropout, Input, Conv2D # from keras.layers import GlobalAveragePooling2D from keras.models import Model input_tensor = Input(shape=(width, height, channel)) conv_base = None # output_layer = None prediction_layer = None if preset.upper() == "bench".upper(): conv_base = create_bench_model(input_tensor) prediction_layer = conv_base elif preset.upper() == "VGG16".upper(): from keras.applications import VGG16 conv_base = None if channel == 3: conv_base = VGG16(weights=weights_init, include_top=True, input_shape=(width, height, channel) ) else: conv_base = VGG16(weights=weights_init, include_top=True, input_shape=(width, height, 3) ) conv_base.layers.pop(0) conv_base.layers.pop(0) input_layer = Input(shape=(width, height, channel), name='multi_input') block1_conv1_new = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform', name='block1_conv1_new') conv_base = insert_intermediate_layer_in_keras(conv_base, [0, 0], [input_layer, block1_conv1_new]) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.get_output_at(-1) # x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.2, name='fc_dropout')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "VGG19".upper(): from keras.applications import VGG19 conv_base = None if channel == 3: conv_base = VGG19(weights=weights_init, include_top=True, input_shape=(width, height, channel) ) else: conv_base = VGG19(weights=weights_init, include_top=True, input_shape=(width, height, 3) ) conv_base.layers.pop(0) conv_base.layers.pop(0) input_layer = Input(shape=(width, height, channel), name='multi_input') block1_conv1_new = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform', name='block1_conv1_new') conv_base = insert_intermediate_layer_in_keras(conv_base, [0, 0], [input_layer, block1_conv1_new]) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.get_output_at(-1) # x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.2, name='fc_dropout')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "VGG16BN".upper(): from model import VGG16BN conv_base = None if channel == 3: conv_base = VGG16BN(weights=weights_init, include_top=False, pooling='avg', kernel_initializer='glorot_uniform', input_shape=(width, height, channel) ) else: conv_base = VGG16BN(weights=weights_init, include_top=False, pooling='avg', kernel_initializer='glorot_uniform', input_shape=(width, height, 3) ) conv_base.layers.pop(0) conv_base.layers.pop(0) input_layer = Input(shape=(width, height, channel), name='multi_input') block1_conv1_new = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform', name='block1_conv1_new') conv_base = insert_intermediate_layer_in_keras(conv_base, [0, 0], [input_layer, block1_conv1_new]) # conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.get_output_at(-1) # x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.2, name='fc_dropout')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "VGG19BN".upper(): from model import VGG19BN conv_base = None if channel == 3: conv_base = VGG19BN(weights=weights_init, include_top=False, pooling='avg', kernel_initializer='glorot_uniform', input_shape=(width, height, channel) ) else: conv_base = VGG19BN(weights=weights_init, include_top=False, pooling='avg', kernel_initializer='glorot_uniform', input_shape=(width, height, 3) ) conv_base.layers.pop(0) conv_base.layers.pop(0) input_layer = Input(shape=(width, height, channel), name='multi_input') block1_conv1_new = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='glorot_uniform', name='block1_conv1_new') conv_base = insert_intermediate_layer_in_keras(conv_base, [0, 0], [input_layer, block1_conv1_new]) # conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.get_output_at(-1) # x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.2, name='fc_dropout')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNet20".upper(): # from keras.applications import ResNet50 from model.resnet import ResNet20 conv_base = ResNet20(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNet50".upper(): # from keras.applications import ResNet50 from model.resnet import ResNet50 conv_base = ResNet50(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNet101".upper(): from model.resnet import ResNet101 conv_base = ResNet101(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNet152".upper(): from model.resnet import ResNet152 conv_base = ResNet152(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNet50V2".upper(): from model.resnet_v2 import ResNet50V2 conv_base = ResNet50V2(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNet101V2".upper(): from model.resnet_v2 import ResNet101V2 conv_base = ResNet101V2(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNet152V2".upper(): from model.resnet_v2 import ResNet152V2 conv_base = ResNet152V2(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNeXt50".upper(): from model.resnext import ResNeXt50 conv_base = ResNeXt50(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "ResNeXt101".upper(): from model.resnext import ResNeXt101 conv_base = ResNeXt101(weights=weights_init, include_top=True, input_shape=(width, height, channel), input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) x = Dropout(0.5, name='fc_dropout')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "InceptionV3".upper(): from keras.applications import InceptionV3 conv_base = InceptionV3(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "InceptionResNetV2".upper(): from keras.applications import InceptionResNetV2 conv_base = InceptionResNetV2(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "DenseNet121".upper(): from keras.applications import DenseNet121 conv_base = DenseNet121(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "DenseNet169".upper(): from keras.applications import DenseNet169 conv_base = DenseNet169(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "DenseNet201".upper(): from keras.applications import DenseNet201 conv_base = DenseNet201(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "Xception".upper(): from keras.applications import Xception conv_base = Xception(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEDenseNetImageNet121".upper(): from model import SEDenseNetImageNet121 conv_base = SEDenseNetImageNet121(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEDenseNetImageNet169".upper(): from model import SEDenseNetImageNet169 conv_base = SEDenseNetImageNet169(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEDenseNetImageNet201".upper(): from model import SEDenseNetImageNet201 conv_base = SEDenseNetImageNet201(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEDenseNetImageNet264".upper(): from model import SEDenseNetImageNet264 conv_base = SEDenseNetImageNet264(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEDenseNetImageNet161".upper(): from model import SEDenseNetImageNet161 conv_base = SEDenseNetImageNet161(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEInceptionResNetV2".upper(): from model import SEInceptionResNetV2 conv_base = SEInceptionResNetV2(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEInceptionV3".upper(): from model import SEInceptionV3 conv_base = SEInceptionV3(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEMobileNet".upper(): from model import SEMobileNet conv_base = SEMobileNet(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNet6".upper(): from model import SEResNet6 conv_base = SEResNet6(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNet8".upper(): from model import SEResNet8 conv_base = SEResNet8(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNet10".upper(): from model import SEResNet10 conv_base = SEResNet10(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNet18".upper(): from model import SEResNet18 conv_base = SEResNet18(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNet34".upper(): from model import SEResNet34 conv_base = SEResNet34(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNet50".upper(): from model import SEResNet50 conv_base = SEResNet50(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNet101".upper(): from model import SEResNet101 conv_base = SEResNet101(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNet154".upper(): from model import SEResNet154 conv_base = SEResNet154(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) elif preset.upper() == "SEResNext".upper(): from model import SEResNext conv_base = SEResNext(weights=weights_init, include_top=True, input_tensor=input_tensor ) conv_base.layers.pop() output_layer = conv_base.layers[-1] x = output_layer.output x = BatchNormalization(name='fc_bachnorm')(x) # x = GlobalAveragePooling2D(name='avg_pool')(x) prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) else: raise ValueError('unknown model name : {}'.format(preset)) # x = output_layer.output # # x = Flatten()(x) # # x = Dense(512, activation='relu', kernel_initializer='glorot_uniform')(x) # # # x = Dropout(0.7)(x) # # x = BatchNormalization(name='fc_bachnorm')(x) # prediction_layer = Dense(class_num, activation='softmax', kernel_initializer='glorot_uniform', name='prediction')(x) model = Model(inputs=conv_base.input, outputs=prediction_layer, name='classification_model') # #weights_filepath = 'work/test/vgg19_weights_tf_dim_ordering_tf_kernels.h5' # weights_filepath = 'work/test/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5' # model.load_weights(weights_filepath, by_name=True, skip_mismatch=True) return model
import cv2 import numpy as np from keras.models import load_model from keras.applications import VGG16, InceptionResNetV2 import sys import argparse import tensorflow as tf import numpy as np import detect_face # In[2]: EMOTION_DICT = {1:"ANGRY", 2:"DISGUST", 3:"FEAR", 4:"HAPPY", 5:"NEUTRAL", 6:"SAD", 7:"SURPRISE"} model_VGG = InceptionResNetV2(weights='imagenet', include_top=False) model_top = load_model("../Data/Model_Save/model.h5") def face_detection(img, pnet, rnet, onet): minsize = 40 # minimum size of face threshold = [ 0.6, 0.7, 0.9 ] # three steps's threshold factor = 0.709 # scale factor img_rgb=cv2.cvtColor(img,cv2.COLOR_BGR2RGB) bounding_boxes, points = detect_face.detect_face(img_rgb, minsize, pnet, rnet, onet, threshold, factor) # for b in bounding_boxes: # cv2.rectangle(img, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 0)) # print(b)
import os import sys from argparse import ArgumentParser from keras.applications import InceptionResNetV2 from keras.applications.imagenet_utils import decode_predictions from train.utils import inception_process_img input_size = 299 image_ext = ['jpg', 'jpeg', 'png', 'gif'] model = InceptionResNetV2(include_top=True) def main(dataset, out_dir): count = 0 results = [] for root, dirs, files in os.walk(dataset): for im_f in files: split = im_f.split(os.extsep) fname = split[0] ext = split[-1] count += 1 if count % 500 == 0: print("{} done.".format(count)) if ext in image_ext: image_file = os.path.join(dataset, im_f) im = inception_process_img(image_file, 299) pred = model.predict(im) p = decode_predictions(pred, top=3)
def get_model_inception(img_shape, img_input, weights, version): if version == 'V2': return InceptionResNetV2(include_top=False, weights=weights, input_tensor=img_input, input_shape=img_shape, pooling=None) elif version == 'V3': return InceptionV3(include_top=False, weights=weights, input_tensor=img_input, input_shape=img_shape, pooling=None)
# plt.legend() # plt.show() if __name__ == "__main__": # config = tf.ConfigProto() # config.gpu_options.allow_growth = True #让TensorFlow在运行过程中动态申请显存,需要多少就申请多少 # session = tf.Session(config=config) # set_session(session) gt_data = load_wider_face_gt_boxes("E:/Document/Datasets/Wider Face/wider_face_split/wider_face_train_bbx_gt.txt") wh = get_imgs_w_h("w_h.csv") model = Model(inputs=[feature_map_tile], outputs=[output_scores, output_deltas]) model.compile(optimizer='adam', loss={'scores1':loss_cls, 'deltas1':smoothL1}) pretrained_model = InceptionResNetV2(include_top=False) # VGG16(include_top=False) # img=load_img("E:/Share/ILSVRC2014_train_00010391.JPEG") x = img_to_array(img) x = np.expand_dims(x, axis=0) not_used=pretrained_model.predict(x) count = 0 sub_gt_data_1 = {} sub_gt_data_2 = {} sub_gt_data_3 = {} sub_gt_data_4 = {} for path, gt_boxes in gt_data.items(): if count < 3220: sub_gt_data_1[path] = gt_boxes elif count < 3220 * 2:
if IMG_SIZE !=224: model = DenseNet169(weights='imagenet',include_top=False,input_shape=(224, 224,3)) new_model = change_model(model,new_input_shape=(None, IMG_SIZE, IMG_SIZE, 3)) else: new_model = DenseNet169(weights='imagenet',include_top=False,input_shape=(224, 224,3)) elif themodel=='InceptionV3': from keras.applications import InceptionV3 if IMG_SIZE !=224: model = InceptionV3(weights='imagenet',include_top=False,input_shape=(224, 224,3)) new_model = change_model(model,new_input_shape=(None, IMG_SIZE, IMG_SIZE, 3)) else: new_model = InceptionV3(weights='imagenet',include_top=False,input_shape=(224, 224,3)) elif themodel=='InceptionResNetV2': from keras.applications import InceptionResNetV2 if IMG_SIZE !=224: model = InceptionResNetV2(weights='imagenet',include_top=False,input_shape=(224, 224,3)) new_model = change_model(model,new_input_shape=(None, IMG_SIZE, IMG_SIZE, 3)) else: new_model = InceptionResNetV2(weights='imagenet',include_top=False,input_shape=(224, 224,3)) elif themodel=='DenseNet201': from keras.applications import DenseNet201 if IMG_SIZE !=224: model = DenseNet201(weights='imagenet',include_top=False,input_shape=(224, 224,3)) new_model = change_model(model,new_input_shape=(None, IMG_SIZE, IMG_SIZE, 3)) else: new_model = DenseNet201(weights='imagenet',include_top=False,input_shape=(224, 224,3)) elif themodel =='Xception': from keras.applications.xception import Xception if IMG_SIZE !=224: model = Xception(weights='imagenet',include_top=False,input_shape=(224, 224,3)) new_model = change_model(model,new_input_shape=(None, IMG_SIZE, IMG_SIZE, 3))
# from data import data_input input_tensor = Input(shape=(75, 75, 3)) factory = { # 'vgg16': lambda: VGG16(include_top=False, input_tensor=input_tensor, weights='imagenet'), 'vgg19': lambda: VGG19( include_top=False, input_tensor=input_tensor, weights='imagenet'), 'xception': lambda: Xception( include_top=False, input_tensor=input_tensor, weights='imagenet'), 'InceptionV3': lambda: InceptionV3( include_top=False, input_tensor=input_tensor, weights='imagenet'), 'InceptionResNetV2': lambda: InceptionResNetV2( include_top=False, input_tensor=input_tensor, weights='imagenet') } def get_model(name='simple', train_base=True, use_angle=False, dropout=0.8, layers=(512, 256)): base = factory[name]() inputs = [base.input] x = GlobalMaxPooling2D()(base.output) if use_angle: angle_in = Input(shape=(5, ))
def train(x_target, x_ref, y_ref, epoch_num): print("Model build...") # mobile = VGG16(include_top=False, input_shape=input_shape, weights='imagenet', pooling= 'avg') mobile = InceptionResNetV2(include_top=False, input_shape=input_shape, weights='imagenet') # mobile = MobileNetV2(include_top=True, input_shape=input_shape, alpha=alpha, # weights='imagenet') mobile.layers.pop() ''' Last layer : - block_13_expand : Mobilenet v2 - block5_conv1 : VGG16 - mixed_7a : Inception resnetv2 ''' # for layer in mobile.layers: # print(layer.name) # if layer.name == "block_13_expand": # "block5_conv1": for VGG16 # if layer.name == "block5_conv1": # if layer.name == "mixed_7a": # break # else: # layer.trainable = False # exit() flat = GlobalAveragePooling2D()(mobile.layers[-1].output) model_t = Model(inputs=mobile.input, outputs=flat) model_r = Network(inputs=model_t.input, outputs=model_t.output, name="shared_layer") prediction = Dense(classes, activation='softmax')(model_t.output) model_r = Model(inputs=model_r.input, outputs=prediction) optimizer = SGD(lr=5e-5, decay=0.00005) model_r.compile(optimizer=optimizer, loss="categorical_crossentropy") model_t.compile(optimizer=optimizer, loss=original_loss) model_t.summary() ref_samples = np.arange(x_ref.shape[0]) loss, loss_c = [], [] epochs = [] print("training...") for epochnumber in range(epoch_num): x_r, y_r, lc, ld = [], [], [], [] np.random.shuffle(x_target) np.random.shuffle(ref_samples) for i in range(len(x_target)): x_r.append(x_ref[ref_samples[i]]) y_r.append(y_ref[ref_samples[i]]) x_r = np.array(x_r) y_r = np.array(y_r) for i in range(int(len(x_target) / batchsize)): batch_target = x_target[i * batchsize:i * batchsize + batchsize] batch_ref = x_r[i * batchsize:i * batchsize + batchsize] batch_y = y_r[i * batchsize:i * batchsize + batchsize] #target data lc.append( model_t.train_on_batch(batch_target, np.zeros((batchsize, feature_out)))) #reference data ld.append(model_r.train_on_batch(batch_ref, batch_y)) loss.append(np.mean(ld)) loss_c.append(np.mean(lc)) epochs.append(epochnumber) print("epoch : {} ,Descriptive loss : {}, Compact loss : {}".format( epochnumber + 1, loss[-1], loss_c[-1])) if epochnumber % 10 == 0: model_t.save_weights('model/model_t_smd_{}.h5'.format(epochnumber)) model_r.save_weights('model/model_r_smd_{}.h5'.format(epochnumber))
loss2 = hist2.history['loss'] plt.subplot(2,1,1) plt.plot(epochs2, accuracy2) plt.title("CNN for Accuracy and Loss (Mask vs No Mask)") plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.subplot(2,1,2) plt.plot(epochs2, loss2) plt.xlabel('Epochs') plt.ylabel('Loss') plt.show() #Loading pre-trained weights from ImageNet to save training time, thank you Transfer Learning base=InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(150, 150, 3)) model3 = models.Sequential() model3.add(base) model3.add(layers.Flatten()) model3.add(layers.Dense(256, activation='relu')) model3.add(layers.Dense(2, activation='softmax')) base.trainable = False print(model3.summary()) batch_size = 32 epochs = 20 model3.compile(loss='sparse_categorical_crossentropy', optimizer='adam',
def get_inception_resnet_v2_unet_softmax(input_shape, weights='imagenet'): inp = Input(input_shape + (4, )) # Stem block: 35 x 35 x 192 x = conv2d_bn(inp, 32, 3, strides=2, padding='same') x = conv2d_bn(x, 32, 3, padding='same') x = conv2d_bn(x, 64, 3) conv1 = x x = MaxPooling2D(3, strides=2, padding='same')(x) x = conv2d_bn(x, 80, 1, padding='same') x = conv2d_bn(x, 192, 3, padding='same') conv2 = x x = MaxPooling2D(3, strides=2, padding='same')(x) # Mixed 5b (Inception-A block): 35 x 35 x 320 branch_0 = conv2d_bn(x, 96, 1) branch_1 = conv2d_bn(x, 48, 1) branch_1 = conv2d_bn(branch_1, 64, 5) branch_2 = conv2d_bn(x, 64, 1) branch_2 = conv2d_bn(branch_2, 96, 3) branch_2 = conv2d_bn(branch_2, 96, 3) branch_pool = AveragePooling2D(3, strides=1, padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1) branches = [branch_0, branch_1, branch_2, branch_pool] channel_axis = 1 if K.image_data_format() == 'channels_first' else 3 x = Concatenate(axis=channel_axis, name='mixed_5b')(branches) # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320 for block_idx in range(1, 11): x = inception_resnet_block(x, scale=0.17, block_type='block35', block_idx=block_idx) conv3 = x # Mixed 6a (Reduction-A block): 17 x 17 x 1088 branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='same') branch_1 = conv2d_bn(x, 256, 1) branch_1 = conv2d_bn(branch_1, 256, 3) branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='same') branch_pool = MaxPooling2D(3, strides=2, padding='same')(x) branches = [branch_0, branch_1, branch_pool] x = Concatenate(axis=channel_axis, name='mixed_6a')(branches) # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088 for block_idx in range(1, 21): x = inception_resnet_block(x, scale=0.1, block_type='block17', block_idx=block_idx) conv4 = x # Mixed 7a (Reduction-B block): 8 x 8 x 2080 branch_0 = conv2d_bn(x, 256, 1) branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='same') branch_1 = conv2d_bn(x, 256, 1) branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='same') branch_2 = conv2d_bn(x, 256, 1) branch_2 = conv2d_bn(branch_2, 288, 3) branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='same') branch_pool = MaxPooling2D(3, strides=2, padding='same')(x) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='mixed_7a')(branches) # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080 for block_idx in range(1, 10): x = inception_resnet_block(x, scale=0.2, block_type='block8', block_idx=block_idx) x = inception_resnet_block(x, scale=1., activation=None, block_type='block8', block_idx=10) # Final convolution block: 8 x 8 x 1536 x = conv2d_bn(x, 1536, 1, name='conv_7b') conv5 = x conv6 = conv_block(UpSampling2D()(conv5), 320) conv6 = concatenate([conv6, conv4], axis=-1) conv6 = conv_block(conv6, 320) conv7 = conv_block(UpSampling2D()(conv6), 256) conv7 = concatenate([conv7, conv3], axis=-1) conv7 = conv_block(conv7, 256) conv8 = conv_block(UpSampling2D()(conv7), 128) conv8 = concatenate([conv8, conv2], axis=-1) conv8 = conv_block(conv8, 128) conv9 = conv_block(UpSampling2D()(conv8), 96) conv9 = concatenate([conv9, conv1], axis=-1) conv9 = conv_block(conv9, 96) conv10 = conv_block(UpSampling2D()(conv9), 64) conv10 = conv_block(conv10, 64) res = Conv2D(3, (1, 1), activation='softmax')(conv10) model = Model(inp, res) if weights == 'imagenet': inception_resnet_v2 = InceptionResNetV2(weights=weights, include_top=False, input_shape=input_shape + (3, )) for i in range(2, len(inception_resnet_v2.layers) - 1): model.layers[i].set_weights( inception_resnet_v2.layers[i].get_weights()) model.layers[i].trainable = False return model