def save_model_to_file(filename, architecture, weights='imagenet', input_shape=(299, 299, 3)): base_path = os.getcwd() model_path = os.path.join(base_path, filename) # Create and save network file if not os.path.exists(model_path): # Initialise Keras object if architecture == 'inceptionv3': mymodel = applications.InceptionV3( weights=weights, input_tensor=Input(shape=input_shape)) elif architecture == 'resnet152v2': mymodel = applications.ResNet152V2( weights=weights, input_tensor=Input(shape=input_shape)) elif architecture == 'inceptionresnetv2': mymodel = applications.InceptionResNetV2( weights=weights, input_tensor=Input(shape=input_shape)) else: print('Invalid architecture') return mymodel.save(os.path.join(base_path, filename))
def create_model(nclass): base_model = applications.ResNet152V2(weights='imagenet', include_top=False, input_shape=input_shape) base_model.trainable = False model = Sequential() model.add(base_model) model.add(Flatten()) model.add(BatchNormalization()) model.add(Dense(2048, activation='relu')) model.add(BatchNormalization()) model.add(Dense(1024, activation='relu')) model.add(BatchNormalization()) model.add(Dense(nclass, activation='softmax')) return model
def create_resnet_model(): base = applications.ResNet152V2(include_top=False, weights=None, input_shape=(112, 112, 3), pooling='max') x = base.output x = Dropout(0.4)(x) x = Dense(2048, activation='relu')(x) x = Dense(2048, activation='relu')(x) x = Dense(1024, activation='relu')(x) x = Dense(1024, activation='relu')(x) x = Dense(512, activation='relu')(x) x = Dense(512, activation='relu')(x) x = Dense(256, activation='relu')(x) x = Dense(256, activation='relu')(x) output = Dense(1, activation='linear')(x) model = Model(inputs=base.input, outputs=output) return model
x_train, y_train = load_data(os.getcwd() + "/archive/seg_train/") x_test, y_test = load_data(os.getcwd() + "/archive/seg_test/") x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=0.15, random_state=0) # %--------------------------------------------- Training ---------------------------------------------------------- # Resnet BATCH_SIZE = 512 EPOCHS = 10 LR = 1e-3 res = applications.ResNet152V2(input_shape=(150, 150, 3), weights='imagenet', include_top=False) res.trainable = False print('Resnet pre trained model is loaded ....') model = Sequential([ res, Flatten(), Dense(400, activation='tanh'), Dropout(0.5), BatchNormalization(), Dense(6, activation='softmax') ]) early_stopping_callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)