我正在尝试在TensorFlow中实现一个学习预测单应的网络(
Wiki
). 我的网络将输出一个4维向量,这将被用来翻译2个图像水平和垂直。然后,我用一个中心作物计算地面真实值的误差(
tf.image.central_crop
)这些扭曲的图像。我试着实现它,对于翻译部分,我使用
tf.contrib.image.translate
. 但是,梯度并没有流向网络的变量。我怎样才能解决这个问题?这是我得到的错误:
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).
下面是获取这个变换向量的代码。
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
这是翻译图像和计算损失的代码。
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)