def init_model(self): """Compile and build the model""" base_model = Backbone(weights='imagenet', include_top=False, input_shape=(self._img_size, self._img_size, 3)) base_model.trainable = False self._model = keras.models.Sequential([ base_model, GlobalAveragePooling2D(), Dense(256, activation='relu'), Dropout(0.3), Dense(len(self._classes), activation='softmax') ]) sgd = SGD(lr=0.0005) self._model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) self._model.summary()
color_mode='rgb', batch_size=VALID_IMG_COUNT)) # one big batch print(valid_x.shape, valid_y.shape) # In[ ]: t_x, t_y = next(train_gen) print('x', t_x.shape, t_x.dtype, t_x.min(), t_x.max()) print('y', t_y.shape, t_y.dtype, t_y.min(), t_y.max()) # In[ ]: base_pretrained_model = PTModel(input_shape=t_x.shape[1:], include_top=False, weights='imagenet') base_pretrained_model.trainable = False # In[ ]: from keras import models, layers from keras.optimizers import Adam img_in = layers.Input(t_x.shape[1:], name='Image_RGB_In') img_noise = layers.GaussianNoise(GAUSSIAN_NOISE)(img_in) pt_features = base_pretrained_model(img_noise) pt_depth = base_pretrained_model.get_output_shape_at(0)[-1] bn_features = layers.BatchNormalization()(pt_features) feature_dropout = layers.SpatialDropout2D(DROPOUT)(bn_features) gmp_dr = layers.GlobalMaxPooling2D()(feature_dropout) dr_steps = layers.Dropout(DROPOUT)(layers.Dense(DENSE_COUNT, activation='relu')(gmp_dr)) out_layer = layers.Dense(1, activation='sigmoid')(dr_steps)
def compile_model(loss, opt, metrics, shape, weights=None, conv_base='DenseNet121'): if conv_base == 'VGG16': from keras.applications.vgg16 import VGG16 as BASE elif conv_base == 'ResNet152': from keras_applications.resnet import ResNet152 as BASE elif conv_base == 'DenseNet121': from keras.applications.densenet import DenseNet121 as BASE elif conv_base == 'NASNetMobile': from keras.applications.nasnet import NASNetMobile as BASE elif conv_base == 'MobileNetV2': from keras.applications.mobilenet_v2 import MobileNetV2 as BASE elif conv_base == 'Xception': from keras.applications.xception import Xception as BASE else: raise ValueError('Unknown model: {}'.format(conv_base)) #load the base layer with everything if weights: conv_base = BASE(include_top=False, input_shape=shape, pooling=max) # conv_base = BASE(include_top=True) #pop the top layer off # conv_base.layers.pop() # print(conv_base.summary()) #set all the layers to trainable conv_base.trainable = True for layer in conv_base.layers: layer.trainable = True model = models.Sequential() model.add(conv_base) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dense(1, activation='sigmoid')) model.load_weights(weights) model.compile(loss=loss, optimizer=opt, metrics=metrics) else: conv_base = BASE(include_top=False, weights=None, input_shape=shape, pooling=max) #pop the top layer off # conv_base.layers.pop() # print(conv_base.summary()) #set all the layers to trainable conv_base.trainable = True for layer in conv_base.layers: layer.trainable = True model = models.Sequential() model.add(conv_base) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dense(1, activation='sigmoid')) model.compile(loss=loss, optimizer=opt, metrics=metrics) return model
# batch_size = VALIDATION_BATCH_SIZE) # we can use much larger batches for evaluation # used a fixed dataset for evaluating the algorithm valid_X, valid_Y = next( flow_from_dataframe(img_gen, valid_df, path_col='path', y_col='class_vec', target_size=IMG_SIZE, color_mode='rgb', batch_size=TEST_SAMPLES)) # one big batch t_x, t_y = next(train_gen) base_pretrained_model = PTModel(input_shape=t_x.shape[1:], include_top=False, weights='imagenet') base_pretrained_model.trainable = PRETRAIN_MODEL_TRAINABLE #Model Supplements #Here we add a few other layers to the model to make it better suited for the classification problem. from keras.layers import GlobalAveragePooling2D, Dense, Dropout, Flatten, Input, Conv2D, multiply, LocallyConnected2D, Lambda, AvgPool2D from keras.models import Model from keras.optimizers import Adam # catch the output as feature from picked pre-trained model pt_features = Input(base_pretrained_model.get_output_shape_at(0)[1:], name='feature_input') pt_depth = base_pretrained_model.get_output_shape_at(0)[-1] # Normalize the activations of the previous layer at each batch, # i.e. applies a transformation that maintains the mean activation close to 0 and the activation standard deviation close to 1. from keras.layers import BatchNormalization
def train_classifier(fold, model_name, x_train, y_train, x_valid, y_valid, id_valid): print(model_name) x_train_act = x_train x_val_act = x_valid # x_train_act = x_train.astype(np.float32) # for idx in range(len(x_train_act)): # x_train_act[idx] = preprocess_input(x_train_act[idx]) # print(x_train_act.max()) # x_val_act = x_valid.astype(np.float32) # for idx in range(len(x_val_act)): # x_val_act[idx] = preprocess_input(x_val_act[idx]) inputshape = x_train_act.shape[1:] PTModel, preprocess_input = get_model(model_name) base_pretrained_model = PTModel(input_shape = inputshape, include_top = False, weights = 'imagenet') base_pretrained_model.trainable = False from keras import models, layers from keras.optimizers import Adam img_in = layers.Input(inputshape, name='Image_RGB_In') x = base_pretrained_model(img_in) x = layers.Flatten(name='flatten')(x) x = Dense(256)(x) x = BatchActivate(x) x = Dropout(0.5)(x) x = Dense(64)(x) x = BatchActivate(x) x = Dropout(0.5)(x) out_layer = layers.Dense(1, activation = 'sigmoid')(x) class_model = models.Model(inputs = [img_in], outputs = [out_layer], name = 'full_model') class_model.compile(optimizer = Adam(lr=0.01), loss = 'binary_crossentropy', metrics = ['binary_accuracy']) batch_size = 32 base_name = '{}_{}'.format(model_name, fold) save_model_name = '../model/classifier/{}.model'.format(base_name) submission_file = '../result/classifier/{}.csv'.format(base_name) oof_file = '../result/classifier/{}_oof.csv'.format(base_name) print(save_model_name) print(submission_file) print(oof_file) board = keras.callbacks.TensorBoard(log_dir='log/classifier/{}'.format(base_name), histogram_freq=0, write_graph=True, write_images=False) early_stopping = EarlyStopping(monitor='val_binary_accuracy', mode = 'max',patience=5, verbose=1) model_checkpoint = ModelCheckpoint(save_model_name,monitor='val_binary_accuracy', mode = 'max', save_best_only=True, verbose=1) reduce_lr = ReduceLROnPlateau(monitor='val_binary_accuracy', mode = 'max',factor=0.5, patience=2, min_lr=0.00001, verbose=1) epochs = 200 history = class_model.fit(x_train, y_train, validation_data=[x_valid, y_valid], epochs=epochs, batch_size=batch_size, callbacks=[board, early_stopping, reduce_lr, model_checkpoint], verbose=1) model = load_model(save_model_name) oof = model.predict(x_valid) df_oof = pd.DataFrame() df_oof['id'] = id_valid df_oof['target'] = oof df_oof.to_csv(oof_file, index=False) files = os.listdir('../input/test/images/') x_test = np.array([(np.array(load_img("../input/test/images/{}".format(idx), grayscale = False))) for idx in files]).reshape(-1, img_size_target, img_size_target, 3) preds_test = model.predict(x_test) df_result = pd.DataFrame() df_result['id'] = files df_result['pre'] = preds_test.reshape(len(files)) df_result.to_csv(submission_file, index=False)
y_col="label", class_mode="binary", color_mode="rgb", target_size=TARGET_SIZE, batch_size=BATCH_SIZE) # -----------------------------COMPILE THE MODEL---------------------------------- # conv_base = BASE(include_top=True, input_shape=train_generator.image_shape, pooling=max) conv_base.layers.pop() # print(conv_base.summary()) conv_base.trainable = True for layer in conv_base.layers: layer.trainable = True model = models.Sequential() model.add(conv_base) model.add(layers.Dense(1, activation='sigmoid')) if WEIGHTS: model.load_weights(WEIGHTS) model.compile(loss='binary_crossentropy', optimizer=OPT, metrics=['accuracy']) print(model.summary()) # -----------------------------ADD SOME CHECKPOINTS---------------------------------- #