繁体   English   中英

具有不同尺寸图像的张量流卷积神经网络

[英]Tensorflow Convolution Neural Network with different sized images

我正在尝试创建一个可以对图像中的每个像素进行分类的深度CNN。 我复制从图像架构下面取自这个文件。 在论文中提到使用去卷积使得任何大小的输入都是可能的。 这可以在下图中看到。

Github存储库

在此输入图像描述

目前,我已经硬编码我的模型接受大小为32x32x7的图像,但我想接受任何大小的输入。 我需要对我的代码进行哪些更改以接受可变大小的输入?

 x = tf.placeholder(tf.float32, shape=[None, 32*32*7])
 y_ = tf.placeholder(tf.float32, shape=[None, 32*32*7, 3])
 ...
 DeConnv1 = tf.nn.conv3d_transpose(layer1, filter = w, output_shape = [1,32,32,7,1], strides = [1,2,2,2,1], padding = 'SAME')
 ...
 final = tf.reshape(final, [1, 32*32*7])
 W_final = weight_variable([32*32*7,32*32*7,3])
 b_final = bias_variable([32*32*7,3])
 final_conv = tf.tensordot(final, W_final, axes=[[1], [1]]) + b_final

动态占位符

Tensorflow允许在占位符中具有多个动态(即None )维度。 在构建图形时,引擎将无法确保正确性,因此客户端负责提供正确的输入,但它提供了很大的灵活性。

所以我要去......

x = tf.placeholder(tf.float32, shape=[None, N*M*P])
y_ = tf.placeholder(tf.float32, shape=[None, N*M*P, 3])
...
x_image = tf.reshape(x, [-1, N, M, P, 1])

至...

# Nearly all dimensions are dynamic
x_image = tf.placeholder(tf.float32, shape=[None, None, None, None, 1])
label = tf.placeholder(tf.float32, shape=[None, None, 3])

既然您打算将输入重新x_image为5D,那么为什么不从一开始就在x_image使用5D。 此时, label的第二维是任意的,但我们保证它将与x_image匹配的张量x_image

反卷积中的动态形状

接下来,关于tf.nn.conv3d_transpose是它的输出形状可以是动态的。 所以不是这样的:

# Hard-coded output shape
DeConnv1 = tf.nn.conv3d_transpose(layer1, w, output_shape=[1,32,32,7,1], ...)

... 你可以这样做:

# Dynamic output shape
DeConnv1 = tf.nn.conv3d_transpose(layer1, w, output_shape=tf.shape(x_image), ...)

这样,转置卷积可以应用于任何图像,结果将采用在运行时实际传入的x_image的形状。

注意, x_image静态形状是(?, ?, ?, ?, 1)

全卷积网络

这个难题的最后和最重要的部分是使整个网络卷积,并且包括你的最终密集层。 密集层必须静态定义其尺寸,这迫使整个神经网络修复输入图像尺寸。

对我们来说幸运的是,Springenberg在“努力实现简单:全面卷积网”论文中描述了用CONV层取代FC层的方法。 我将使用带有3个1x1x1滤镜的卷积(另请参阅此问题 ):

final_conv = conv3d_s1(final, weight_variable([1, 1, 1, 1, 3]))
y = tf.reshape(final_conv, [-1, 3])

如果我们确保final具有相同尺寸DeConnv1 (及其他),它会让y正确的形状,我们希望: [-1, N * M * P, 3]

将它们结合在一起

您的网络非常庞大,但所有解卷积基本上都遵循相同的模式,因此我将概念验证代码简化为一个解卷积。 目标只是展示哪种网络能够处理任意大小的图像。 最后再说一句:图像尺寸可以批次之间有所不同,但一个批次内,他们必须是相同的。

完整代码:

sess = tf.InteractiveSession()

def conv3d_dilation(tempX, tempFilter):
  return tf.layers.conv3d(tempX, filters=tempFilter, kernel_size=[3, 3, 1], strides=1, padding='SAME', dilation_rate=2)

def conv3d(tempX, tempW):
  return tf.nn.conv3d(tempX, tempW, strides=[1, 2, 2, 2, 1], padding='SAME')

def conv3d_s1(tempX, tempW):
  return tf.nn.conv3d(tempX, tempW, strides=[1, 1, 1, 1, 1], padding='SAME')

def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)

def max_pool_3x3(x):
  return tf.nn.max_pool3d(x, ksize=[1, 3, 3, 3, 1], strides=[1, 2, 2, 2, 1], padding='SAME')

x_image = tf.placeholder(tf.float32, shape=[None, None, None, None, 1])
label = tf.placeholder(tf.float32, shape=[None, None, 3])

W_conv1 = weight_variable([3, 3, 1, 1, 32])
h_conv1 = conv3d(x_image, W_conv1)
# second convolution
W_conv2 = weight_variable([3, 3, 4, 32, 64])
h_conv2 = conv3d_s1(h_conv1, W_conv2)
# third convolution path 1
W_conv3_A = weight_variable([1, 1, 1, 64, 64])
h_conv3_A = conv3d_s1(h_conv2, W_conv3_A)
# third convolution path 2
W_conv3_B = weight_variable([1, 1, 1, 64, 64])
h_conv3_B = conv3d_s1(h_conv2, W_conv3_B)
# fourth convolution path 1
W_conv4_A = weight_variable([3, 3, 1, 64, 96])
h_conv4_A = conv3d_s1(h_conv3_A, W_conv4_A)
# fourth convolution path 2
W_conv4_B = weight_variable([1, 7, 1, 64, 64])
h_conv4_B = conv3d_s1(h_conv3_B, W_conv4_B)
# fifth convolution path 2
W_conv5_B = weight_variable([1, 7, 1, 64, 64])
h_conv5_B = conv3d_s1(h_conv4_B, W_conv5_B)
# sixth convolution path 2
W_conv6_B = weight_variable([3, 3, 1, 64, 96])
h_conv6_B = conv3d_s1(h_conv5_B, W_conv6_B)
# concatenation
layer1 = tf.concat([h_conv4_A, h_conv6_B], 4)
w = tf.Variable(tf.constant(1., shape=[2, 2, 4, 1, 192]))
DeConnv1 = tf.nn.conv3d_transpose(layer1, filter=w, output_shape=tf.shape(x_image), strides=[1, 2, 2, 2, 1], padding='SAME')

final = DeConnv1
final_conv = conv3d_s1(final, weight_variable([1, 1, 1, 1, 3]))
y = tf.reshape(final_conv, [-1, 3])
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=y))

print('x_image:', x_image)
print('DeConnv1:', DeConnv1)
print('final_conv:', final_conv)

def try_image(N, M, P, B=1):
  batch_x = np.random.normal(size=[B, N, M, P, 1])
  batch_y = np.ones([B, N * M * P, 3]) / 3.0

  deconv_val, final_conv_val, loss = sess.run([DeConnv1, final_conv, cross_entropy],
                                              feed_dict={x_image: batch_x, label: batch_y})
  print(deconv_val.shape)
  print(final_conv.shape)
  print(loss)
  print()

tf.global_variables_initializer().run()
try_image(32, 32, 7)
try_image(16, 16, 3)
try_image(16, 16, 3, 2)

从理论上讲,它是可能的。 您需要将输入和标签图像占位符的图像大小设置为none ,并让图形从输入数据动态推断图像大小。

但是,定义图形时必须小心。 需要使用tf.shape而不是tf.get_shape() 前者仅在session.run时动态推断形状,后者可以在定义图形时获得形状。 但是当输入大小设置为none ,后者不会得到真正的重塑(可能只返回None)。

并且为了使事情变得复杂,如果你使用tf.layers.conv2dupconv2d ,有时这些高级函数不喜欢tf.shape ,因为它们似乎假设形状信息在图形构造期间可用。

我希望我有更好的工作实例来展示上述要点。 我会把这个答案作为占位符,如果有机会我会回来添加更多东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM