y_train = tf.keras.utils.to_categorical(y_train, num_classes) y_test = tf.keras.utils.to_categorical(y_test, num_classes) model = Sequential() model.add( Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax')) # ============ Multi-GPU ============ model = to_multi_gpu(model, n_gpus=2) # =================================== model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer=tf.keras.optimizers.Adadelta(), metrics=['accuracy']) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])
def run_experiment(x): """ Runs a single experiment with the given learning rate and weight decay parameters """ learning_rate = x[0] weight_decay = x[1] global log_path if args.optimize: global step step += 1 # pylint: disable=E0602 log_path = os.path.join('log', args.dataset, args.ml_method, args.init, str(args.labeled_ratio), str(args.corruption_ratio), str(step)) if not os.path.exists(log_path): os.makedirs(log_path) with open(os.path.join(log_path, 'his.txt'), 'a') as f: f.write('Learning rate: ' + str(x[0]) + '\n') f.write('Weight decay: ' + str(x[1]) + '\n') K.clear_session() model = resnet101(dh.no_classes[args.dataset], initialization=args.init, weight_decay=weight_decay) if params.n_gpus > 1: model = to_multi_gpu(model, n_gpus=params.n_gpus) sgd = SGD(lr=learning_rate, momentum=0.9, decay=0.0, nesterov=True) model.compile(loss=loss_function, optimizer=sgd, metrics=[loss_function]) ind_lr_step = 0 lowest_loss = np.inf while ind_lr_step < params.no_lr_steps: model_checkpoint = ModelCheckpoint(os.path.join( log_path, str(ind_lr_step) + '_cp.h5'), monitor='val_loss', save_best_only=True) early_stopper = EarlyStopping(monitor='val_loss', patience=params.lr_patience) loss_plotter = LossPlotter( os.path.join(log_path, str(ind_lr_step) + '_losses.png')) his = model.fit_generator( generator=dh.generator('train_labeled', aug=True), steps_per_epoch=len(dh.inds_labeled) / params.batch_size, epochs=params.max_epoch, callbacks=[model_checkpoint, early_stopper, loss_plotter], validation_data=dh.generator('val', aug=False), validation_steps=len(dh.val_images) / params.batch_size / 10, verbose=2) with open(os.path.join(log_path, str(ind_lr_step) + '_his.p'), 'wb') as f: pickle.dump(his.history, f, pickle.HIGHEST_PROTOCOL) with open(os.path.join(log_path, 'his.txt'), 'a') as f: f.write('Training history:\n') f.write(str(his.history) + '\n') if np.min(his.history['val_loss']) < lowest_loss: lowest_loss = np.min(his.history['val_loss']) else: model.load_weights(os.path.join(log_path, str(ind_lr_step - 1) + '_cp.h5'), by_name=True) break model.load_weights(os.path.join(log_path, str(ind_lr_step) + '_cp.h5'), by_name=True) learning_rate /= 10 K.set_value(model.optimizer.lr, learning_rate) ind_lr_step += 1 model.save_weights(os.path.join(log_path, 'best_cp.h5')) res_metrics = test_model(model) with open(os.path.join(log_path, 'metrics.p'), 'wb') as f: pickle.dump(res_metrics, f, pickle.HIGHEST_PROTOCOL) with open(os.path.join(log_path, 'metrics.txt'), 'w') as f: f.write(str(res_metrics) + '\n') return -res_metrics[ 'f1c_top3'] # negative because gp_minimize tries to minimize the result
def run_experiment(x): """ Runs a single experiment with the given learning rate and weight decay parameters """ learning_rate = x[0] weight_decay = x[1] global log_path if args.optimize: global step step += 1 # pylint: disable=E0602 log_path = os.path.join('log2', args.dataset, args.ml_method, args.init, str(args.labeled_ratio), str(args.corruption_ratio), str(step)) if not os.path.exists(log_path): os.makedirs(log_path) with open(os.path.join(log_path, 'his.txt'), 'w') as f: f.write('Learning rate: ' + str(x[0]) + '\n') f.write('Weight decay: ' + str(x[1]) + '\n') K.clear_session() # load pretrained model for label propagation if args.ml_method == 'robust_warp': model_path = os.path.join('log', args.dataset, 'robust_warp_sup', args.init, str(args.labeled_ratio), str(args.corruption_ratio), 'best_cp.h5') else: model_path = os.path.join('log', args.dataset, args.ml_method, args.init, str(args.labeled_ratio), str(args.corruption_ratio), 'best_cp.h5') model_orig = resnet101(dh.no_classes[args.dataset], initialization='random', weight_decay=weight_decay) if params.n_gpus > 1: model_orig = to_multi_gpu(model_orig, n_gpus=params.n_gpus) model_orig.load_weights(model_path) update_mixed_labels(model_orig) model = resnet101(dh.no_classes[args.dataset] + 1, initialization='random', weight_decay=weight_decay) if params.n_gpus > 1: model_orig = to_single_gpu(model_orig) for ind_layer in range(len(model.layers)): if model.layers[ind_layer].name == model_orig.layers[ind_layer].name: model.layers[ind_layer].set_weights( model_orig.layers[ind_layer].get_weights()) if params.n_gpus > 1: model = to_multi_gpu(model, n_gpus=params.n_gpus) sgd = SGD(lr=learning_rate, momentum=0.9, decay=0.0, nesterov=True) model.compile(loss=loss_function, optimizer=sgd, metrics=[loss_function]) ind_lr_step = 0 train_losses = [] val_losses = [] patience_losses = [] for ind_epoch in range(params.max_epoch): if ind_epoch % 20 == 0 and not ind_epoch == 0: update_mixed_labels(model) his = model.fit_generator( generator=dh.generator('train_mixed', aug=True), steps_per_epoch=dh.mixed_labels.shape[0] / params.batch_size, validation_data=dh.generator('val', aug=False), validation_steps=len(dh.val_images) / params.batch_size / 10, verbose=2) train_losses.append(his.history['loss'][0]) val_losses.append(his.history['val_loss'][0]) patience_losses.append(his.history['val_loss'][0]) if min(patience_losses) == patience_losses[-1]: model.save_weights( os.path.join(log_path, str(ind_lr_step) + '_cp.h5')) elif np.argmin(np.array(patience_losses) ) < len(patience_losses) - 1 - params.lr_patience: with open(os.path.join(log_path, 'his.txt'), 'a') as f: f.write('loss: ' + str(train_losses) + '\n') f.write('val_loss: ' + str(val_losses) + '\n') if ind_lr_step == params.no_lr_steps - 1: model.load_weights(os.path.join(log_path, str(ind_lr_step) + '_cp.h5'), by_name=True) break else: model.load_weights(os.path.join(log_path, str(ind_lr_step) + '_cp.h5'), by_name=True) learning_rate /= 10 K.set_value(model.optimizer.lr, learning_rate) ind_lr_step += 1 model.save_weights( os.path.join(log_path, str(ind_lr_step) + '_cp.h5')) train_losses = [] val_losses = [] model.save_weights(os.path.join(log_path, 'best_cp.h5')) res_metrics = test_model(model) with open(os.path.join(log_path, 'metrics.p'), 'wb') as f: pickle.dump(res_metrics, f, pickle.HIGHEST_PROTOCOL) with open(os.path.join(log_path, 'metrics.txt'), 'w') as f: f.write(str(res_metrics) + '\n') return -res_metrics[ 'f1c_top3'] # negative because gp_minimize tries to minimize the result
def update_mixed_labels(model): """ Propagates labels to unlabeled examples using the features extracted with the model """ if params.n_gpus > 1: feat_model = to_single_gpu(model) feat_model = keras.Model(feat_model.layers[0].input, feat_model.layers[-2].output) # pylint: disable=E1101 if params.n_gpus > 1: feat_model = to_multi_gpu(feat_model, n_gpus=params.n_gpus) no_batches = int(np.ceil(float(len(dh.inds_labeled)) / params.batch_size)) l_feats = feat_model.predict_generator( dh.generator('train_labeled_sorted', aug=False, shuffle_batches=False), no_batches) l_feats = np.concatenate( (l_feats[:(no_batches - 1) * params.batch_size], l_feats[-(len(dh.inds_labeled) - (no_batches - 1) * params.batch_size):])) no_batches = int(np.ceil( float(len(dh.inds_unlabeled)) / params.batch_size)) ul_feats = feat_model.predict_generator( dh.generator('train_unlabeled', aug=False, shuffle_batches=False), no_batches) ul_feats = np.concatenate( (ul_feats[:(no_batches - 1) * params.batch_size], ul_feats[-(len(dh.inds_unlabeled) - (no_batches - 1) * params.batch_size):])) min_dists = np.zeros(ul_feats.shape[0], dtype=np.float32) min_dist_inds = np.zeros(ul_feats.shape[0], dtype=np.int) for ind_unlabeled in range(ul_feats.shape[0]): min_dists[ind_unlabeled] = np.Inf for ind_labeled in range(l_feats.shape[0]): dist = np.linalg.norm(l_feats[ind_labeled] - ul_feats[ind_unlabeled]) if dist < min_dists[ind_unlabeled]: min_dists[ind_unlabeled] = dist min_dist_inds[ind_unlabeled] = ind_labeled no_labeled = l_feats.shape[0] no_unlabeled = ul_feats.shape[0] no_mixed = no_labeled + no_unlabeled mean_dist = np.mean(min_dists) * (float(no_unlabeled) / no_mixed) similarity_scores = np.exp(-min_dists / mean_dist) mixed_labels = np.zeros((no_mixed, dh.no_classes[dh.dataset] + 1), dtype=np.float32) mixed_labels[dh.inds_labeled_sorted, :-1] = dh.train_labels[ dh.inds_labeled_sorted] mixed_labels[dh.inds_labeled_sorted, -1] = 5 for ind_unlabeled in range(no_unlabeled): mixed_labels[dh.inds_unlabeled[ind_unlabeled], :-1] = dh.train_labels[ dh.inds_labeled_sorted[min_dist_inds[ind_unlabeled]]] mixed_labels[dh.inds_unlabeled[ind_unlabeled], -1] = similarity_scores[ind_unlabeled] dh.mixed_labels = mixed_labels """for ind in range(no_mixed):
if args.load_weights: model.load_weights(args.load_weights, by_name=True) model.summary() if args.suffix is None: suffix = "__rgb" if args.use_background: suffix += "X" if args.use_coarse: suffix += "CO" else: suffix = "__" + args.suffix if args.gpus != 1: model = to_multi_gpu(model, n_gpus=args.gpus) if args.test: model_basename = args.load_model.split('/')[-1] mkdir_p('test_' + model_basename) test_model(model, ids, X, CO, patches_per_image=args.test_patches_per_image, batch_size=args.batch_size, csv_filename=model_basename + '.csv', save_pngs_to_folder='test_' + model_basename, input_channels=input_channels) else: