I am trying to implement a network in TensorFlow that learns to predict homography (Wiki). My network will output a 4-dimensional vector, which will be used to translate 2 images horizontally and vertically. Then, I calculate the error with a ground truth using a central crop (tf.image.central_crop
)of these warped images. I tried implementing it, and for the translation part, I used tf.contrib.image.translate
. But, the gradients are not flowing to the variables of the network. How can I fix this problem? This is the error that I am getting:
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'get_tr/w1:0' shape=(3, 3, 6, 64) dtype=float32_ref>", "<tf.Variable 'get_tr/b1:0' shape=(20, 298, 298, 64) dtype=float32_ref>", "<tf.Variable 'get_tr/w2:0' shape=(3, 3, 64, 64) dtype=float32_ref>", "<tf.Variable 'get_tr/b2:0' shape=(20, 296, 296, 64) dtype=float32_ref>", "<tf.Variable 'get_tr/w3:0' shape=(3, 3, 64, 128) dtype=float32_ref>", "<tf.Variable 'get_tr/b3:0' shape=(20, 147, 147, 128) dtype=float32_ref>", "<tf.Variable 'get_tr/w4:0' shape=(3, 3, 128, 128) dtype=float32_ref>", "<tf.Variable 'get_tr/b4:0' shape=(20, 73, 73, 128) dtype=float32_ref>", "<tf.Variable 'get_tr/w5:0' shape=(5, 5, 128, 128) dtype=float32_ref>", "<tf.Variable 'get_tr/fc1/kernel:0' shape=(609408, 512) dtype=float32_ref>", "<tf.Variable 'get_tr/fc1/bias:0' shape=(512,) dtype=float32_ref>", "<tf.Variable 'get_tr/fc2/kernel:0' shape=(512, 1024) dtype=float32_ref>", "<tf.Variable 'get_tr/fc2/bias:0' shape=(1024,) dtype=float32_ref>", "<tf.Variable 'get_tr/fc_o/kernel:0' shape=(1024, 4) dtype=float32_ref>", "<tf.Variable 'get_tr/fc_o/bias:0' shape=(4,) dtype=float32_ref>"] and loss Tensor("mean_squared_error/value:0", shape=(), dtype=float32).
Here is the code for getting this transformation vector.
def get_transform_vectors(self):
# Start of transformation prediction network
image = tf.concat((self.img_train_1, self.img_train_2), 3)
with tf.variable_scope('get_tr'):
w1 = tf.Variable(tf.truncated_normal(shape=[3, 3, 6, 64], stddev=0.1),
name='w1')
conv1 = tf.nn.conv2d(image,
filter=w1,
strides=(1,1,1,1),
padding="VALID",
name='conv1')
b1 = tf.Variable(tf.truncated_normal(shape=tf.shape(conv1), stddev=0.1),
name='b1')
conv1_ = tf.nn.relu(conv1+b1, name='conv1_')
w2 = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 64], stddev=0.1),
name='w2')
conv2 = tf.nn.conv2d(conv1_,
filter=w2,
strides=(1,1,1,1),
padding="VALID",
name='conv2')
b2 = tf.Variable(tf.truncated_normal(shape=tf.shape(conv2), stddev=0.1),
name='b2')
conv2_ = tf.nn.relu(conv2+b2, name='conv2_')
w3 = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 128], stddev=0.1),
name='w3')
conv3 = tf.nn.conv2d(conv2_,
filter=w3,
strides=(1,2,2,1),
padding="VALID",
name='conv3')
b3 = tf.Variable(tf.truncated_normal(shape=tf.shape(conv3), stddev=0.1),
name='b3')
conv3_ = tf.nn.relu(conv3+b3, name='conv3_')
w4 = tf.Variable(tf.truncated_normal(shape=[3, 3, 128, 128], stddev=0.1),
name='w4')
conv4 = tf.nn.conv2d(conv3_,
filter=w4,
strides=(1,2,2,1),
padding="VALID",
name='conv4')
b4 = tf.Variable(tf.truncated_normal(shape=tf.shape(conv4), stddev=0.1),
name='b4')
conv4_ = tf.nn.relu(conv4+b4, name='conv4_')
w5 = tf.Variable(tf.truncated_normal(shape=[5, 5, 128, 128], stddev=0.1),
name='w5')
conv5 = tf.nn.conv2d(conv4_,
filter=w5,
strides=(1,1,1,1),
padding="VALID",
name='conv5')
conv5_ = tf.contrib.layers.flatten(conv5)
fc1 = tf.layers.dense(conv5_, 512, activation=tf.nn.relu, name='fc1')
fc2 = tf.layers.dense(fc1, 1024, activation=tf.nn.relu, name='fc2')
fc_o = tf.layers.dense(fc2, 4, name='fc_o')
return fc_o
This is the code for translating the image and calculating the loss.
self.img_o = tf.contrib.image.translate(self.img_train_1,
tf.cast(tf.reshape(self.tr_vector[:, 0:2], [self.batch_size,2]),
dtype=tf.float32)) +
tf.contrib.image.translate(self.img_train_2,
tf.cast(tf.reshape(self.tr_vector[:, 2:4], [self.batch_size,2]),
dtype=tf.float32))
self.loss = tf.losses.mean_squared_error(self.img_o, self.img_label)
self.optim = tf.train.AdamOptimizer().minimize(self.loss)
See Question&Answers more detail:
os