예제 #1
0
파일: train_tnar.py 프로젝트: uuujf/TNAR
        x.append(xi)
    x = tf.stack(x, axis=0)
    x_ul = []
    for i in range(args.batch_size_ul):
        xi = tf.pad(x_ul_raw[i,:,:,:], [[2,2],[2,2],[0,0]])
        xi = tf.random_crop(xi, [32,32,3])
        xi = tf.image.random_flip_left_right(xi)
        x_ul.append(xi)
    x_ul = tf.stack(x_ul, axis=0)
else:
    x = x_raw
    x_ul = x_ul_raw

vae = VAE(args.latent_dim)
net = Net()
out = net.classifier('net', x, keep_prob=args.keep_prob, is_training=True, update_batch_stats=True)
out_ul = net.classifier('net', x_ul, keep_prob=args.keep_prob, is_training=True, update_batch_stats=False)
mu, logvar = vae.encode(x_ul, False)
z = vae.reparamenterize(mu, logvar, False)
x_recon = vae.decode(z, False)

r0 = tf.zeros_like(z, name='zero_holder')
x_recon_r0 = vae.decode(z+r0, False)
diff2 = 0.5 * tf.reduce_sum((x_recon - x_recon_r0)**2, axis=[1,2,3])
diffJaco = tf.gradients(diff2, r0)[0]
def normalizevector(r):
    shape = tf.shape(r)
    r = tf.reshape(r, [shape[0],-1])
    r /= (1e-12+tf.reduce_max(tf.abs(r), axis=1, keepdims=True))
    r / tf.sqrt(tf.reduce_sum(r**2, axis=1, keepdims=True)+1e-6)
    return tf.reshape(r, shape)
예제 #2
0
args = parser.parse_args()
print(args)

# dataset
test_set = np.load(args.datadir + '/test.npz')
X_test = test_set['image'].reshape(-1, 32, 32, 3)
Y_test = (np.eye(10)[test_set['label']]).astype(np.float32)

# tensor graph
net = Net()
x_test = tf.placeholder(tf.float32, [None, 32, 32, 3], name='x_test')
y_test = tf.placeholder(tf.float32, [None, 10], name='y_test')
out_test = net.classifier('net',
                          x_test,
                          keep_prob=1.0,
                          is_training=False,
                          update_batch_stats=False)
correct_prediction = tf.equal(tf.argmax(y_test, 1), tf.argmax(out_test, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

weight_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='net')
saver = tf.train.Saver(var_list=weight_list)

gpu_options = tf.GPUOptions(allow_growth=True)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
    # sess.run(tf.global_variables_initializer())
    saver.restore(sess, args.resume)
    print('restored', args.resume)
    acc = 0
    for j in range(20):