def val(): num_test = 10000 data_dir = '/home/rong/something_for_deep/cifar-10-batches-bin' train_log_dir = './logs/train' image_batch, label_batch = input_data.read_cifar10(data_dir, is_train=False, batch_size=BATCH_SIZE, shuffle=False) vgg16 = model.VGG16() logits = vgg16.build(image_batch, NUM_CLASSES, False) saver = tf.train.Saver() correct_per_batch = tools.num_correct_prediction(logits, label_batch) with tf.Session() as sess: print('Reading checkpoints') ckpt = tf.train.get_checkpoint_state(train_log_dir) if ckpt and ckpt.model_checkpoint_path: global_step = ckpt.model_checkpoint_path.split('/')[-1].split( '-')[-1] saver.restore(sess, ckpt.model_checkpoint_path) print('Loading success, global_step is %s' % global_step) else: print('No checkpoint file found') return saver.restore(sess, './logs/train/model.ckpt-8000') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: print('\nEvaluating......') num_step = int(math.floor(num_test / BATCH_SIZE)) num_sample = num_step * BATCH_SIZE step = 0 total_correct = 0 while step < num_step and not coord.should_stop(): batch_correct = sess.run(correct_per_batch) total_correct += np.sum(batch_correct) step += 1 if step % 10 == 0: print('Testing samples: %d' % (step * BATCH_SIZE)) print('Correct predictions: %d' % total_correct) print('Average accuracy: %.2f%%' % (total_correct / (step * BATCH_SIZE))) print('Total testing samples: %d' % num_sample) print('Total correct predictions: %d' % total_correct) print('Average accuracy: %.2f%%' % (100 * total_correct / num_sample)) except Exception as e: coord.request_stop(e) finally: coord.request_stop() coord.join(threads)
def main(args, model_name, trainloader, testloader): n_classes = args["dataset"]["n_classes"] net = model.VGG16(n_classes, True) optimizer = torch.optim.SGD(params=net.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay) criterion = nn.CrossEntropyLoss(reduction="none").cuda() net = torch.nn.DataParallel(net).to(device) train_dp(model_name, model_path, mode, net, criterion, optimizer, trainloader, testloader, n_epochs, noise)