Skip to content

Instantly share code, notes, and snippets.

@ronghanghu
Last active May 1, 2018 02:46
Show Gist options
  • Save ronghanghu/3592682c466086aaaa838786648e05ac to your computer and use it in GitHub Desktop.
Save ronghanghu/3592682c466086aaaa838786648e05ac to your computer and use it in GitHub Desktop.
CLEVR train
import tensorflow as tf
from glob import glob
import numpy as np
sess = tf.Session(config=tf.ConfigProto(
gpu_options=tf.GPUOptions(allow_growth=True)))
N = 100 # batch_size
H_f = 14
W_f = 14
D_f = 1024
weight_decay = 5e-4
train_file_A = sorted(glob('./exp_clevr_snmn/data/resnet101_c4/valA/*.npy'))
train_file_B = sorted(glob('./exp_clevr_snmn/data/resnet101_c4/valB/*.npy'))
test_file_A = sorted(glob('./exp_clevr_snmn/data/resnet101_c4/testA/*.npy'))
test_file_B = sorted(glob('./exp_clevr_snmn/data/resnet101_c4/testB/*.npy'))
max_epoch = 20
def model(image_features, map_dim=256):
conv1 = tf.layers.conv2d(
image_features, filters=map_dim, kernel_size=1, strides=1)
pool1 = tf.reduce_max(conv1, axis=[1, 2])
scores = tf.layers.dense(pool1, 1)
return tf.squeeze(scores, axis=1)
_feature_buffer = np.zeros((N, H_f, W_f, D_f), np.float32)
def load_files(file_list): # NoQA
for n, p in enumerate(file_list):
_feature_buffer[n] = np.load(p)
return _feature_buffer
image_features = tf.placeholder(tf.float32, [N, H_f, W_f, D_f])
labels = tf.placeholder(tf.float32, [None])
scores = model(image_features)
loss_cls = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
logits=scores, labels=labels))
loss_reg = weight_decay * tf.add_n(
[tf.nn.l2_loss(v) for v in tf.global_variables()])
solver_step = tf.train.AdamOptimizer().minimize(loss_cls + loss_reg)
train_files = train_file_A + train_file_B
train_labels = [0] * len(train_file_A) + [1] * len(train_file_B)
# randomly permute the training files
np.random.seed(3)
perm_idx = np.random.permutation(len(train_files))
train_files = [train_files[n] for n in perm_idx]
train_labels = [train_labels[n] for n in perm_idx]
test_files = test_file_A + test_file_B
test_labels = [0] * len(test_file_A) + [1] * len(test_file_B)
print('got %d training files and %d test files' %
(len(train_files), len(test_files)))
sess.run(tf.global_variables_initializer())
for n_epoch in range(max_epoch):
for n_batch in range(len(train_files) // N):
F = load_files(train_files[n_batch*N:(n_batch+1)*N])
L = np.array(train_labels[n_batch*N:(n_batch+1)*N], np.float32)
loss_val, _ = sess.run((loss_cls, solver_step), feed_dict={
image_features: F, labels: L})
print('epoch %d batch %d, loss = %f' % (n_epoch, n_batch, loss_val))
correct = 0
for n_batch in range(len(test_files) // N):
F = load_files(test_files[n_batch*N:(n_batch+1)*N])
L = np.array(test_labels[n_batch*N:(n_batch+1)*N], np.float32)
scores_val = sess.run(scores, feed_dict={
image_features: F})
correct += np.sum((scores_val >= 0) == L)
print('epoch %d test %d' % (n_epoch, n_batch))
accuracy = correct * 1. / len(test_labels)
print('epoch %d, test accuracy = %f' % (n_epoch, accuracy))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment