def process_single_epoch(): print('**************') parser = argparse.ArgumentParser() parser.add_argument('-d', type=int, default=0, help='Which gpu to use') args = parser.parse_args() DEVICE = torch.device('cuda:{}'.format(args.d)) torch.backends.cudnn.benchmark = True net = create_network() net.to(DEVICE) nat_val = load_test_dataset(10000, natural=True) adv_val = load_test_dataset(10000, natural=False) AttackMethod = config.create_evaluation_attack_method(DEVICE) filename = '../ckpts/6leaf-epoch29.checkpoint' print(filename) if os.path.isfile(filename): load_checkpoint(filename, net) print('Evaluating Natural Samples') clean_acc, adv_acc = my_eval_one_epoch(net, nat_val, DEVICE, AttackMethod) print('clean acc -- {} adv acc -- {}'.format(clean_acc, adv_acc)) print('Evaluating Adversarial Samples') clean_acc, adv_acc = my_eval_one_epoch(net, adv_val, DEVICE, AttackMethod) print('clean acc -- {} adv acc -- {}'.format(clean_acc, adv_acc))
def test(model, sess, exp_folder, epoch, stage): # test dataset x, side_length, channels = load_test_dataset(args.dataset, args.root_folder) np.random.shuffle(x) x = x[0:10000] # reconstruction and generation img_recons = model.reconstruct(sess, x) img_gens1 = model.generate(sess, 10000, 1) img_gens2 = model.generate(sess, 10000, 2) img_recons_sample = stich_imgs(img_recons) img_gens1_sample = stich_imgs(img_gens1) img_gens2_sample = stich_imgs(img_gens2) plt.imsave( os.path.join(exp_folder, 'recon_sample_stage_{}_epoch_{}.jpg'.format(stage, epoch)), img_recons_sample) plt.imsave( os.path.join(exp_folder, 'gen1_sample_stage_{}_epoch_{}.jpg'.format(stage, epoch)), img_gens1_sample) plt.imsave( os.path.join(exp_folder, 'gen2_sample_stage_{}_epoch_{}.jpg'.format(stage, epoch)), img_gens2_sample) # calculating FID score tf.reset_default_graph() fid_recon = evaluate_fid_score(img_recons.copy(), args.dataset, args.root_folder, True) fid_gen1 = evaluate_fid_score(img_gens1.copy(), args.dataset, args.root_folder, True) fid_gen2 = evaluate_fid_score(img_gens2.copy(), args.dataset, args.root_folder, True) print('Reconstruction Results:') print('FID = {:.4F}\n'.format(fid_recon)) print('Generation Results (First Stage):') print('FID = {:.4f}\n'.format(fid_gen1)) print('Generation Results (Second Stage):') print('FID = {:.4f}\n'.format(fid_gen2))
def evaluate_fid_score(fake_images, dataset, root_folder, norm=True): real_images, _, _ = load_test_dataset(dataset, root_folder) np.random.shuffle(real_images) real_images = real_images[0:10000] real_images = preprocess_real_images(real_images) fake_images = preprocess_fake_images(fake_images, norm) inception_path = check_or_download_inception() create_inception_graph(inception_path) sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) print('calculating tf features...') real_out = get_activations_tf(real_images, sess) fake_out = get_activations_tf(fake_images, sess) fid_result = fid_score(real_out, fake_out) return fid_result
def main(): tf.reset_default_graph() # exp info exp_folder = os.path.join(args.output_path, args.dataset, args.exp_name) if not os.path.exists(exp_folder): os.makedirs(exp_folder) model_path = os.path.join(exp_folder, 'model') if not os.path.exists(model_path): os.makedirs(model_path) # dataset x, side_length, channels = load_dataset(args.dataset, args.root_folder) input_x = tf.placeholder( tf.uint8, [args.batch_size, side_length, side_length, channels], 'x') num_sample = np.shape(x)[0] print('Num Sample = {}.'.format(num_sample)) # model if args.network_structure != 'Resnet': model = eval(args.network_structure)(input_x, args.latent_dim, args.second_depth, args.second_dim) else: model = Resnet(input_x, args.num_scale, args.block_per_scale, args.depth_per_block, args.kernel_size, args.base_dim, args.fc_dim, args.latent_dim, args.second_depth, args.second_dim) sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter(exp_folder, sess.graph) saver = tf.train.Saver() # train model iteration_per_epoch = num_sample // args.batch_size if not args.val: # first stage for epoch in range(args.epochs): np.random.shuffle(x) lr = args.lr if args.lr_epochs <= 0 else args.lr * math.pow( args.lr_fac, math.floor(float(epoch) / float(args.lr_epochs))) epoch_loss = 0 for j in range(iteration_per_epoch): image_batch = x[j * args.batch_size:(j + 1) * args.batch_size] loss = model.step(1, image_batch, lr, sess, writer, args.write_iteration) epoch_loss += loss epoch_loss /= iteration_per_epoch print('Date: {date}\t' 'Epoch: [Stage 1][{0}/{1}]\t' 'Loss: {2:.4f}.'.format( epoch, args.epochs, epoch_loss, date=time.strftime('%Y-%m-%d %H:%M:%S'))) saver.save(sess, os.path.join(model_path, 'stage1')) # second stage for epoch in range(args.epochs2): np.random.shuffle(x) lr = args.lr2 if args.lr_epochs2 <= 0 else args.lr2 * math.pow( args.lr_fac2, math.floor( float(epoch) / float(args.lr_epochs2))) epoch_loss = 0 for j in range(iteration_per_epoch): image_batch = x[j * args.batch_size:(j + 1) * args.batch_size] loss = model.step(2, image_batch, lr, sess, writer, args.write_iteration) epoch_loss += loss epoch_loss /= iteration_per_epoch print('Date: {date}\t' 'Epoch: [Stage 2][{0}/{1}]\t' 'Loss: {2:.4f}.'.format( epoch, args.epochs2, epoch_loss, date=time.strftime('%Y-%m-%d %H:%M:%S'))) saver.save(sess, os.path.join(model_path, 'stage2')) else: saver.restore(sess, os.path.join(model_path, 'stage2')) # test dataset x, side_length, channels = load_test_dataset(args.dataset, args.root_folder) np.random.shuffle(x) x = x[0:10000] # reconstruction and generation img_recons = model.reconstruct(sess, x) img_gens1 = model.generate(sess, 10000, 1) img_gens2 = model.generate(sess, 10000, 2) img_recons_sample = stich_imgs(img_recons) img_gens1_sample = stich_imgs(img_gens1) img_gens2_sample = stich_imgs(img_gens2) plt.imsave(os.path.join(exp_folder, 'recon_sample.jpg'), img_recons_sample) plt.imsave(os.path.join(exp_folder, 'gen1_sample.jpg'), img_gens1_sample) plt.imsave(os.path.join(exp_folder, 'gen2_sample.jpg'), img_gens2_sample)
def run_training(): with tf.Graph().as_default(): with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.0005), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.0005), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.0005), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.0005), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.0005), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.0005), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.0005), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.0005), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.0005), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.0005), 'out': _variable_with_weight_decay('wout_finetune', [4096, C.n_actions], 0.0005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.000), 'bc2': _variable_with_weight_decay('bc2', [128], 0.000), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.000), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.000), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.000), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.000), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.000), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.000), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.000), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.000), 'out': _variable_with_weight_decay('bout_finetune', [C.n_actions], 0.000), } # crop_mean = np.load(C.crop_mean_fpath) # crop_mean = crop_mean.reshape([C.n_frames_per_clip, C.crop_size, C.crop_size, 3]) # Build model train_model = build_train_model(weights, biases) test_model = build_test_model(weights, biases) # Create a session for running Ops on the Graph. config = tf.ConfigProto() config.allow_soft_placement = True config.gpu_options.allow_growth = True sess = tf.Session(config=config) # Create a saver for writing training checkpoints. saver = tf.train.Saver(list(weights.values()) + list(biases.values())) # Load train dataset train_dataset = load_train_dataset(C.train_data_fpath, N_GPU * C.batch_size) train_iterator = train_dataset.make_initializable_iterator() train_next_batch = train_iterator.get_next() sess.run(train_iterator.initializer) # Load test dataset test_dataset = load_test_dataset(C.test_data_fpath, N_GPU * C.batch_size, shuffle=True, repeat=True) test_iterator = test_dataset.make_initializable_iterator() test_next_batch = test_iterator.get_next() sess.run(test_iterator.initializer) # Load a pretrained model (if exists) if C.use_pretrained_model: variables = list(weights.values()) + list(biases.values()) restorer = tf.train.Saver(train_model["varlist_stable"]) restorer.restore(sess, C.pretrained_model_fpath) # Initialize init = tf.global_variables_initializer() sess.run(init) # Train for step in range(1, C.n_iterations + 1): train_clips, train_labels = sess.run(train_next_batch) sess.run(train_model["train_op"], feed_dict={ train_model["images_placeholder"]: train_clips, train_model["labels_placeholder"]: train_labels, }) # Log train if step % C.train_log_every == 0: train_clips, train_labels = sess.run(train_next_batch) preds, acc, loss_val = sess.run( [train_model["logits"], train_model["accuracy"], train_model["loss"]], feed_dict={ train_model["images_placeholder"]: train_clips, train_model["labels_placeholder"]: train_labels, }) loss_val = np.mean(loss_val) print("Train acc.: {:.3f}\tloss: {:.3f}".format(acc, loss_val)) summary_writer.scalar("train/accuracy", acc, step) summary_writer.scalar("train/loss", loss_val, step) train_log(train_clips, preds, train_labels, step) # Log test if step % C.test_log_every == 0: test_clips, test_labels, test_frames = sess.run(test_next_batch) preds, acc, loss_val = sess.run( [test_model["logits"], test_model["accuracy"], test_model["loss"]], feed_dict={ test_model["images_placeholder"]: test_clips, test_model["labels_placeholder"]: test_labels, }) loss_val = np.mean(loss_val) print("Test acc.: {:.3f}\t loss: {:.3f}".format(acc, loss_val)) summary_writer.scalar("test/accuracy", acc, step) summary_writer.scalar("test/loss", loss_val, step) test_log(test_clips, preds, test_labels, step) # Save a checkpoint if step % C.save_every == 0: if not os.path.exists(os.path.dirname(C.model_fpath)): os.makedirs(os.path.dirname(C.model_fpath)) saver.save(sess, C.model_fpath, global_step=step)
from keras.callbacks import ModelCheckpoint, EarlyStopping # global variables IMG_SIZE = 60 EPOCHS = 5 BATCH_SIZE = 64 df_train = pd.read_csv('data/labels.csv') df_test = pd.read_csv('data/sample_submission.csv') targets_series = pd.Series(df_train['breed']) one_hot = pd.get_dummies(targets_series, sparse=True) one_hot_labels = np.asarray(one_hot) x_train = load_train_dataset(df_train, IMG_SIZE) x_test = load_test_dataset(df_test, IMG_SIZE) y_train = load_train_labels(df_train, one_hot_labels) x_train_raw = np.array(x_train, np.float32) / 255. x_test = np.array(x_test, np.float32) / 255. y_train_raw = np.array(y_train, np.uint8) NUM_CLASS = y_train_raw.shape[1] print(x_train_raw.shape) print(y_train_raw.shape) print(x_test.shape) X_train, X_valid, Y_train, Y_valid = train_test_split(x_train_raw, y_train_raw, test_size=0.3,
def run_test(): # Get the sets of images and labels for training, validation, and images_placeholder, labels_placeholder = placeholder_inputs() with tf.variable_scope('var_name') as var_scope: weights = { 'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00), 'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00), 'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00), 'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00), 'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00), 'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00), 'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00), 'wd1': _variable_with_weight_decay('wd1', [8192, 4096], 0.04, 0.001), 'wd2': _variable_with_weight_decay('wd2', [4096, 4096], 0.04, 0.002), 'out': _variable_with_weight_decay('wout_finetune', [4096, C.n_actions], 0.04, 0.005) } biases = { 'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0), 'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0), 'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0), 'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0), 'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0), 'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0), 'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0), 'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0), 'bd1': _variable_with_weight_decay('bd1', [4096], 0.04, 0.0), 'bd2': _variable_with_weight_decay('bd2', [4096], 0.04, 0.0), 'out': _variable_with_weight_decay('bout_finetune', [C.n_actions], 0.04, 0.0), } logits = [] for i, gpu_index in enumerate(GPU_LIST): with tf.device('/gpu:%d' % gpu_index): logit, _ = model.inference( images_placeholder[i * C.batch_size:(i + 1) * C.batch_size,:,:,:,:], 1., C.batch_size, weights, biases) logits.append(logit) logits = tf.concat(logits, 0) norm_scores = tf.nn.sigmoid(logits) config = tf.ConfigProto() config.allow_soft_placement=True config.gpu_options.allow_growth = True sess = tf.Session(config=config) init = tf.global_variables_initializer() sess.run(init) saver = tf.train.Saver() saver.restore(sess, C.model_fpath) os.makedirs(C.prediction_dpath, exist_ok=True) os.makedirs(C.integration_dpath, exist_ok=True) pbar = tqdm(total=sum([ len(episodes) for episodes in C.episodes_list ])) for season, episodes in zip(C.seasons, C.episodes_list): for episode in episodes: pbar.set_description("Generating prediction results of S{:02d}_EP{:02d}...".format(season, episode)) list_file_fpath = C.list_fpath_tpl.format(season, episode) demo_results = [] integration_results = [] # Load train dataset dataset = load_test_dataset(list_file_fpath, N_GPU * C.batch_size, shuffle=False, repeat=False) iterator = dataset.make_initializable_iterator() next_batch = iterator.get_next() sess.run(iterator.initializer) while True: try: clips, labels, frames = sess.run(next_batch) except tf.errors.OutOfRangeError: break frames = frames.tolist() predict_scores = norm_scores.eval( session=sess, feed_dict={ images_placeholder: clips }) topk_idxs = np.argsort(predict_scores, axis=1)[:, -C.topk:] topk_scores = np.take(predict_scores, topk_idxs) topk_scores = topk_scores.tolist() for frame, labels, topk_idx, topk_score in zip(frames, labels, topk_idxs, topk_scores): result1 = build_result_for_demo(frame, labels, topk_idx, topk_score) demo_results.append(result1) result2 = build_results_for_integration(frame, labels, topk_idx, topk_score) integration_results += result2 # For demo videos episode_id = "S{:02d}_EP{:02d}".format(season, episode) result = { "file_name": "{}.json".format(episode_id), "registed_name": "{}.json".format(episode_id), "prediction_results": demo_results, } result_fpath = C.prediction_fpath_tpl.format(season, episode) with open(result_fpath, 'w') as fout: json.dump(result, fout, indent=2, sort_keys=True) # For integration integration_fpath = C.integration_fpath_tpl.format(season, episode) with jsonlines.open(integration_fpath, mode='w') as writer: writer.write_all(integration_results) pbar.update(1)