def test_transfer_weights(): # Prepare Model src_param = ResNetParams(n=2, version=2) dest_param = ResNetParams(n=6, version=2) # Create src model: ResNet20v2 print("Src model type: %s" % src_param.model_type) src_model = resnet_v2(input_shape=input_shape, depth=src_param.depth, num_classes=num_classes) # Restore src model's weights # MODEL_CKPT_FILE = "ResNet20v2.020-auc-0.9736.h5" # filepath = os.path.join(src_param.saves_dir, MODEL_CKPT_FILE) # print("loading weights from: %s..." % filepath) # src_model.load_weights(filepath) # src_weights_list = src_model.get_weights() # Create dest model: ResNet56v2 print("Dest model type: %s" % dest_param.model_type) dest_model = resnet_v2(input_shape=input_shape, depth=dest_param.depth, num_classes=num_classes) transfer_weights(src_model, dest_model) print("Saving new model...") filename = f"{dest_param.model_type}.h5" dest_model.save_weights(filename)
def construct_original_network(dataset_name, model_name, train): data_model = dataset_name + model_name # Define the model input_size = 32 num_classes = 10 channel = 3 assert model_name == 'resnet' from resnet import resnet_v2, lr_schedule, lr_schedule_sgd model, image_ph, preds = resnet_v2(input_shape=(input_size, input_size, channel), depth=20, num_classes = num_classes) optimizer = optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) grads = [] for c in range(num_classes): grads.append(tf.gradients(preds[:,c], image_ph)) grads = tf.concat(grads, axis = 0) approxs = grads * tf.expand_dims(image_ph, 0) logits = [layer.output for layer in model.layers][-2] print(logits) sess = K.get_session() return image_ph, preds, grads, approxs, sess, model, num_classes, logits
def full_res_net_model_voxceleb(shape=(32, 1024), num_classes=500, n=1, feature_length=100, l2_sm=15, **kwargs): input_array = keras.Input(shape, name='input') three_d_input = keras.layers.Reshape(target_shape=(*shape, 1))(input_array) resnet_output = resnet_v2(inputs=three_d_input, n=n) resnet_output = BatchNormalization()(resnet_output) mid = keras.layers.Dense(feature_length, activation='sigmoid', name="feature_layer")(resnet_output) mid = BatchNormalization()(mid) output = keras.layers.Dense(num_classes, activation='softmax', name="output_layer")(mid) model = Model(inputs=input_array, outputs=output) model.compile(loss=l2_softmax(l2_sm), optimizer="adam", metrics=['accuracy']) model.summary() return model
def full_res_net_model(shape=(32, 1024), num_classes=500, n=1, feature_length=100, l2_sm=15, **kwargs): input_array = keras.Input(shape, name='input') # three_d_input = keras.layers.Reshape(target_shape=(*shape, 1))(input_array) three_d_input = SpatialDropout2D(0.1)(input_array) resnet_output = resnet_v2(inputs=three_d_input, n=n) resnet_output = BatchNormalization()(resnet_output) resnet_output = Dropout(0.1)(resnet_output) mid = keras.layers.Dense(feature_length, activation=relu_advanced, name="feature_layer")(resnet_output) mid = Dropout(0.05)(mid) mid = BatchNormalization()(mid) output = keras.layers.Dense(num_classes, activation='softmax', name="output_layer")(mid) model = Model(inputs=input_array, outputs=output) model.compile(loss=l2_softmax(l2_sm), optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.summary() return model
def cnn_model_fn(features, labels, mode): if FLAGS.net == 'resnet_v2': logits = resnet_v2(features['x'], mode == tf.estimator.ModeKeys.TRAIN) print('use', FLAGS.net, '------------------') elif FLAGS.net == 'cnnnet': logits = cnn_net(features['x'], mode) print('use', FLAGS.net, '------------------') else: raise ValueError('Not a valid parmeter:', FLAGS.net) # logits = resnet_v2(features['x'],mode == tf.estimator.ModeKeys.TRAIN) predictions = { # Generate predictions (for PREDICT and EVAL mode) "classes": tf.argmax(input=logits, axis=1), # Add `softmax_tensor` to the graph. It is used for PREDICT and by the # `logging_hook`. "probabilities": tf.nn.softmax(logits, name="softmax_tensor") } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) # Calculate Loss (for both TRAIN and EVAL modes) onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=10) loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits) # Configure the Training Op (for TRAIN mode) if mode == tf.estimator.ModeKeys.TRAIN: learning_rate = tf.train.exponential_decay(0.001, tf.train.get_global_step(), 100, 0.95, staircase=True) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) #optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001) train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step()) return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op) # Add evaluation metrics (for EVAL mode) eval_metric_ops = { "accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"]) } return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def main(): # TODO record the learning rate vs epoch cifar10 = keras.datasets.cifar10 (train_images, train_labels), (test_images, test_labels) = cifar10.load_data() train_labels = keras.utils.to_categorical(train_labels) input_shape = train_images.shape[1:] batch_size = 32 num_classes = 10 epochs = 100 data_augmentation = True num_predictions = 20 # model type selected_model = "ResNet20v2" # selected_model = "keras.applications.ResNet50V2" n = 2 # order of ResNetv2, 2 or 6 version = 2 depth = model_depth(n, version) model_type = 'ResNet%dv%d' % (depth, version) save_dir = os.path.join(os.getcwd(), 'saved_models') model_name = 'keras_cifar10_trained_model.h5' if version == 2: model = resnet_v2(input_shape=input_shape, depth=depth) else: model = resnet_v1(input_shape=input_shape, depth=depth) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=lr_schedule(0)), metrics=['accuracy']) # callbacks logdir = os.path.join( "logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S")) csv_logger = CSVLogger(os.path.join( logdir, "training.log.csv"), append=True) tensorboard_callback = tf.keras.callbacks.TensorBoard( logdir, histogram_freq=1) callbacks = [csv_logger, tensorboard_callback] model.fit(train_images, train_labels, epochs=epochs, validation_data=(test_images, test_labels), batch_size=batch_size, verbose=1, workers=4, callbacks=callbacks)
def model_fn_new(img_shape, num_classes, learning_rate): # Subtracting pixel mean improves accuracy # Model parameter # ---------------------------------------------------------------------------- # | | 200-epoch | Orig Paper| 200-epoch | Orig Paper| sec/epoch # Model | n | ResNet v1 | ResNet v1 | ResNet v2 | ResNet v2 | GTX1080Ti # |v1(v2)| %Accuracy | %Accuracy | %Accuracy | %Accuracy | v1 (v2) # ---------------------------------------------------------------------------- # ResNet20 | 3 (2)| 92.16 | 91.25 | ----- | ----- | 35 (---) # ResNet32 | 5(NA)| 92.46 | 92.49 | NA | NA | 50 ( NA) # ResNet44 | 7(NA)| 92.50 | 92.83 | NA | NA | 70 ( NA) # ResNet56 | 9 (6)| 92.71 | 93.03 | 93.01 | NA | 90 (100) # ResNet110 |18(12)| 92.65 | 93.39+-.16| 93.15 | 93.63 | 165(180) # ResNet164 |27(18)| ----- | 94.07 | ----- | 94.54 | ---(---) # ResNet1001| (111)| ----- | 92.39 | ----- | 95.08+-.14| ---(---) # --------------------------------------------------------------------------- n = 3 # Model version # Orig paper: version = 1 (ResNet v1), Improved ResNet: version = 2 (ResNet v2) version = 1 # Computed depth from supplied model parameter n if version == 1: depth = n * 6 + 2 elif version == 2: depth = n * 9 + 2 from resnet import resnet_v1, resnet_v2 if version == 2: model = resnet_v2(input_shape=img_shape, depth=depth) else: model = resnet_v1(input_shape=img_shape, depth=depth) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=metrics) info('ResNet%dv%d' % (depth, version)) return model
def res_plus_transformer_model(shape=(32, 1024), num_classes=500): input_array = keras.Input(shape) three_d_input = keras.layers.Reshape(target_shape=(*shape, 1))(input_array) transformer_output = keras.layers.Flatten()(get_transformer( transformer_input=input_array, transformer_depth=3)) resnet_output = resnet_v2(inputs=three_d_input, n=1) mid = keras.layers.concatenate([resnet_output, transformer_output]) output = keras.layers.Dense(num_classes, activation='relu', kernel_initializer='he_normal')(mid) model = Model(inputs=input_array, outputs=output) model.compile(loss=l2_softmax(5), optimizer="sgd", metrics=['accuracy']) model.summary() # plot_model(model, to_file='model.png') return model
def main(): # data fashion_mnist = keras.datasets.fashion_mnist (train_images, train_labels), \ (test_images, test_labels) = fashion_mnist.load_data() num_classes = np.max(train_labels) + 1 # 10 padded = True if padded: train_images = np.load("./mnist_train_images_padded.npy") train_labels = np.eye(num_classes)[train_labels] # model selected_model = "ResNet20v2" # selected_model = "keras.applications.ResNet50V2" n = 2 # order of ResNetv2, 2 or 6 version = 2 depth = model_depth(n, version) model_type = 'ResNet%dv%d' % (depth, version) metrics = [Accuracy()] train_images = np.expand_dims(train_images, -1) input_shape = train_images.shape[1:] if selected_model == "ResNet20v2": model = resnet_v2(input_shape=input_shape, depth=depth, num_classes=num_classes) elif selected_model == "keras.applications.ResNet50V2": model = tf.keras.applications.ResNet50V2(include_top=True, weights=None, input_shape=input_shape, classes=num_classes) else: return # plot model plot_model(model, to_file=f"{selected_model}.png", show_shapes=True) model.compile( loss='categorical_crossentropy', optimizer=Adam(), # learning_rate=lr_schedule(0) metrics=metrics) # checkpoint = ModelCheckpoint(filepath=filepath, monitor="acc",verbose=1) logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S")) csv_logger = CSVLogger(os.path.join(logdir, "training.log.csv"), append=True) tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1) callbacks = [csv_logger, tensorboard_callback] # makedir_exist_ok() # fit epochs = 100 batch_size = 32 history = model.fit(train_images, train_labels, epochs=epochs, batch_size=batch_size, callbacks=callbacks)
def create_model(input_shape): model = Sequential() model.add(resnet_v2(input_shape, 11)) model.add(Dense(10, activation="softmax")) model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy']) return model
def construct_original_network(dataset_name, model_name, train): data_model = dataset_name + model_name if dataset_name == 'mnist': input_size = 28 num_classes = 10 channel = 1 elif dataset_name == 'cifar10': # Define the model input_size = 32 num_classes = 10 channel = 3 elif dataset_name == 'cifar100': # Define the model input_size = 32 num_classes = 100 channel = 3 if model_name == 'scnn': image_ph = Input(shape=(input_size, input_size, channel), dtype='float32') net = Convolution2D(32, kernel_size=(5, 5), padding='same', activation='relu', name='conv1')(image_ph) net = MaxPooling2D(pool_size=(2, 2))(net) net = Convolution2D(64, (5, 5), padding='same', activation='relu', name='conv2')(net) net = MaxPooling2D(pool_size=(2, 2))(net) net = Flatten()(net) net = Dense(1024, activation='relu', name='fc1')(net) net = Dense(num_classes, activation='softmax', name='fc2')(net) preds = Activation('softmax')(net) model = Model(image_ph, preds) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) elif model_name == 'cnn': image_ph = Input(shape=(input_size, input_size, channel), dtype='float32') net = Convolution2D(48, (3, 3), padding='same', input_shape=(32, 32, 3))(image_ph) net = Activation('relu')(net) net = Convolution2D(48, (3, 3))(net) net = Activation('relu')(net) net = MaxPooling2D(pool_size=(2, 2))(net) net = Dropout(0.25)(net) net = Convolution2D(96, (3, 3), padding='same')(net) net = Activation('relu')(net) net = Convolution2D(96, (3, 3))(net) net = Activation('relu')(net) net = MaxPooling2D(pool_size=(2, 2))(net) net = Dropout(0.25)(net) net = Convolution2D(192, (3, 3), padding='same')(net) net = Activation('relu')(net) net = Convolution2D(192, (3, 3))(net) net = Activation('relu')(net) net = MaxPooling2D(pool_size=(2, 2))(net) net = Dropout(0.25)(net) net = Flatten()(net) net = Dense(512)(net) net = Activation('relu')(net) net = Dropout(0.5)(net) net = Dense(256)(net) net = Activation('relu')(net) net = Dropout(0.5)(net) net = Dense(num_classes, activation=None)(net) preds = Activation('softmax')(net) model = Model(image_ph, preds) sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc']) # Compile the model elif model_name == 'fc': image_ph = Input(shape=(input_size, input_size, channel), dtype='float32') net = Flatten()(image_ph) net = Dense(256)(net) net = Activation('relu')(net) net = Dense(256)(net) net = Activation('relu')(net) net = Dense(256)(net) net = Activation('relu')(net) preds = Dense(num_classes, activation='softmax')(net) model = Model(image_ph, preds) sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc']) elif model_name == 'resnet': from resnet import resnet_v2, lr_schedule, lr_schedule_sgd model, image_ph, preds = resnet_v2(input_shape=(input_size, input_size, channel), depth=20, num_classes=num_classes) optimizer = optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) elif model_name == 'densenet': from densenet import DenseNet nb_filter = -1 #12 if dataset_name == 'cifar100' else -1 image_ph = Input(shape=(input_size, input_size, channel), dtype='float32') model, preds = DenseNet((input_size, input_size, channel), classes=num_classes, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=nb_filter, dropout_rate=0.0, weights=None, input_tensor=image_ph) optimizer = optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=["accuracy"]) grads = [] for c in range(num_classes): grads.append(tf.gradients(preds[:, c], image_ph)) grads = tf.concat(grads, axis=0) approxs = grads * tf.expand_dims(image_ph, 0) logits = [layer.output for layer in model.layers][-2] print(logits) sess = K.get_session() return image_ph, preds, grads, approxs, sess, model, num_classes, logits
def main(): """ Use tensorflow version 2 """ assert tf.__version__[0] == "2" """ Load Config """ with open('./config/config.json', 'r') as f: CONFIG = json.load(f) BATCH_SIZE = 32 # CONFIG["BATCH_SIZE"] ROOT_PATH = CONFIG["ROOT_PATH"] TRAIN_DATA_DIR = CONFIG["TRAIN_DATA_DIR"] VALIDATION_DATA_DIR = CONFIG["VALIDATION_DATA_DIR"] TRAIN_DATA_DIR = os.path.join(ROOT_PATH, TRAIN_DATA_DIR) VALIDATION_DATA_DIR = os.path.join(ROOT_PATH, VALIDATION_DATA_DIR) MODEL_CKPT = CONFIG["MODEL_CKPT"] """ Prepare Model """ n = 6 # order of ResNetv2 version = 2 depth = model_depth(n, version) MODEL_TYPE = 'ResNet%dv%d' % (depth, version) SAVES_DIR = "models-%s/" % MODEL_TYPE SAVES_DIR = os.path.join(ROOT_PATH, SAVES_DIR) if not os.path.exists(SAVES_DIR): os.mkdir(SAVES_DIR) model = resnet_v2(input_shape=INPUT_SHAPE, depth=depth, num_classes=2) model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=lr_schedule(TRAINING_EPOCHS)), metrics=METRICS) # model.summary() print(MODEL_TYPE) """ Prepare Testing Data """ val_filenames = os.listdir(os.path.join(VALIDATION_DATA_DIR, "bad_1")) val_bad_df = pd.DataFrame({'filename': val_filenames}) n_bad_samples = val_bad_df.shape[0] """ Prepare good samples """ val_filenames = os.listdir(os.path.join(VALIDATION_DATA_DIR, "good_0")) val_good_df = pd.DataFrame({'filename': val_filenames}) n_good_samples = val_good_df.shape[0] """ Create bad sample validation generator """ valid_bad_datagen = ImageDataGenerator(rescale=1. / 255) valid_bad_generator = valid_bad_datagen.flow_from_dataframe( val_bad_df, os.path.join(VALIDATION_DATA_DIR, "bad_1"), x_col='filename', y_col=None, class_mode=None, target_size=IMAGE_SIZE, color_mode="grayscale", batch_size=BATCH_SIZE, shuffle=False) """ Create good sample validation generator """ valid_good_datagen = ImageDataGenerator(rescale=1. / 255) valid_good_generator = valid_good_datagen.flow_from_dataframe( val_good_df, os.path.join(VALIDATION_DATA_DIR, "good_0"), x_col='filename', y_col=None, class_mode=None, target_size=IMAGE_SIZE, color_mode="grayscale", batch_size=BATCH_SIZE, shuffle=False) """ Load Weights """ model_ckpt_file = os.path.join(SAVES_DIR, MODEL_CKPT) if os.path.exists(model_ckpt_file): print("Model ckpt found! Loading...:%s" % model_ckpt_file) model.load_weights(model_ckpt_file) """ Predict """ import time start = time.perf_counter() # print("Start validating bad samples...") print("Start validating samples...") predict = model.predict_generator(valid_good_generator, steps=np.ceil(N_VAL_GOOD / BATCH_SIZE), workers=4, verbose=1) elapsed = (time.perf_counter() - start) print("Prediction time used:", elapsed) np.save(MODEL_TYPE + "-predict.npy", predict) # predict 第 1 列,是 bad_1 的概率 val_good_df['predict'] = predict[:, 0] print("Predict Samples: ") print(type(val_good_df)) print(val_good_df.head(10)) """ Submit prediction """ submission_df = val_good_df.copy() # submission_df.drop(['filename', 'predict'], axis=1, inplace=True) submission_df.to_csv('./submissions/submission-%s.csv' % MODEL_CKPT, index=False)
def teacher(input_images, keep_prob, lambda_decay=FLAGS.lambda_decay, is_training=True, weight_decay=0.00004, batch_norm_decay=0.99, batch_norm_epsilon=0.001): with tf.variable_scope("Teacher_model"): net, endpoints = resnet.resnet_v2(inputs=input_images, lambda_decay=lambda_decay, num_classes=FLAGS.num_class, is_training=True, scope='resnet_v2_50') # co_trained layers var_scope = 'Teacher_model/resnet_v2_50/' co_list_0 = slim.get_model_variables(var_scope + 'Conv2d_0') # co_list_1 = slim.get_model_variables(var_scope +'InvertedResidual_16_') # co_list_2 = slim.get_model_variables(var_scope +'InvertedResidual_24_') t_co_list = co_list_0 base_var_list = slim.get_variables() # for _ in range(2): # base_var_list.pop() lambda_c_list = slim.get_variables_by_name('lambda_c') lambda_b_list = slim.get_variables_by_name('lambda_b') t_lambda_list = lambda_c_list + lambda_b_list # print(lambda_b_list) # exit() t_net_var_list =[] for v in base_var_list: if v not in t_co_list and v not in t_lambda_list: t_net_var_list.append(v) # feature & attention t_g0 = endpoints["InvertedResidual_{}_{}".format(256, 2)] t_at0 = tf.nn.l2_normalize(tf.reduce_sum(tf.square(t_g0), -1), axis=0, name='t_at0') t_g1 = endpoints["InvertedResidual_{}_{}".format(512, 3)] t_at1 = tf.nn.l2_normalize(tf.reduce_sum(tf.square(t_g1), -1), axis=0, name='t_at1') part_feature = endpoints["InvertedResidual_{}_{}".format(1024, 5)] t_at2 = tf.nn.l2_normalize(tf.reduce_sum(tf.square(part_feature), -1), axis=0, name='t_at2') object_feature = endpoints["InvertedResidual_{}_{}".format(2048, 2)] t_at3 = tf.nn.l2_normalize(tf.reduce_sum(tf.square(object_feature), -1), axis=0, name='t_at3') # print(t_at1.get_shape().as_list()) # exit() t_g = (t_g0, t_g1, part_feature, object_feature) t_at = (t_at0, t_at1, t_at2, t_at3) fc_obj = slim.max_pool2d(object_feature, (6, 8), scope="GMP1") batch_norm_params = { 'center': True, 'scale': True, 'decay': batch_norm_decay, 'epsilon': batch_norm_epsilon, } fc_obj = slim.conv2d(fc_obj, M, [1, 1], activation_fn=None, weights_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), biases_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), scope='fc_obj') fc_obj = tf.nn.dropout(fc_obj, keep_prob=keep_prob) fc_obj = slim.flatten(fc_obj) # fc_part = slim.conv2d(part_feature, M * k, #卷积核个数 [1, 1], #卷积核高宽 activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, # 标准化器设置为BN normalizer_params=batch_norm_params, weights_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), biases_regularizer=tf.contrib.layers.l2_regularizer(weight_decay) ) # print('part',fc_part.get_shape()) fc_part = slim.max_pool2d(fc_part, (12, 16), scope="GMP2") ft_list = tf.split(fc_part, num_or_size_splits=FLAGS.num_class, axis=-1) #最后一维度(C) cls_list = [] for i in range(M): ft = tf.transpose(ft_list[i], [0, 1, 3, 2]) cls = layers_lib.pool(ft, [1, 10], "AVG") cls = layers.flatten(cls) cls_list.append(cls) fc_ccp = tf.concat(cls_list, axis=-1) #cross_channel_pooling (N, M) fc_part = slim.conv2d(fc_part, FLAGS.num_class, [1, 1], activation_fn=None, weights_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), biases_regularizer=tf.contrib.layers.l2_regularizer(weight_decay), scope="fc_part") fc_part = tf.nn.dropout(fc_part, keep_prob=keep_prob) fc_part = slim.flatten(fc_part) t_var_list = slim.get_model_variables() t_fc_var_list = [] for var in t_var_list: if var not in base_var_list: t_fc_var_list.append(var) return t_g, t_at, fc_obj, fc_part, fc_ccp, t_co_list, t_net_var_list, t_fc_var_list, t_lambda_list, t_var_list
lr (float32): learning rate """ lr = 0.001 epoch += 1 if epoch >= 90: lr *= 5e-2 elif epoch >= 60: lr *= 1e-1 elif epoch >= 30: lr *= 5e-1 print('Learning rate: ', lr) return lr if version == 2: model = resnet_v2(input_shape=input_shape, depth=depth) else: model = resnet_v1(input_shape=input_shape, depth=depth) model.compile(loss='categorical_crossentropy', optimizer=AdaBound(lr=lr_schedule(0), final_lr=adabound_final_lr, gamma=adabound_gamma, weight_decay=weight_decay, amsbound=amsbound), metrics=['accuracy']) model.summary() print(model_type) # Prepare model model saving directory. save_dir = os.path.join(os.getcwd(), 'weights')
validation_ids = pd.read_csv(validation_file_names)["0"] num_train_examples = train_ids.shape[0] num_val_examples = validation_ids.shape[0] print("Number of training examples: {}".format(num_train_examples)) print("Number of validation examples: {}".format(num_val_examples)) partition = {'train': train_ids.values.tolist(), 'validation': validation_ids.values.tolist()} # Generators training_generator = DataGenerator(partition['train'], is_training=True, **gen_params) validation_generator = DataGenerator(partition['validation'], is_training=False,**gen_params) res_output,img_input = re.resnet_v2(params["img_shape"], 1) outputs = mt.decoder(res_output, n_filters=16, dropout=0.05, batchnorm=True) model = Model(inputs=img_input, outputs=[outputs]) '''Compile model''' adam = adam(lr=params["learning_rate"], beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(optimizer=adam, loss=custom_mae, metrics=[custom_mae,percentual_deviance]) model.summary() '''train and save model''' save_model_path = os.path.join(params["save_path"], "weights.hdf5") cp = tf.keras.callbacks.ModelCheckpoint(filepath=save_model_path, monitor='val_percentual_deviance', save_best_only=True, verbose=1, save_weights_only=True) es = tf.keras.callbacks.EarlyStopping(monitor='val_acc', mode='max', verbose=1, patience=50)