gpt4 book ai didi

python - Tensorflow 3 channel 颜色输入顺序

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:40 26 4
gpt4 key购买 nike

我正在使用 tensorflow 通过卷积神经网络处理彩色图像。下面是一段代码。

我的代码可以运行,所以我认为我得到了正确的 channel 数量。我的问题是,如何正确排序 rgb 数据?它的形式是 rgbrgbrgb 还是 rrrgggbbb?目前我正在使用后者。谢谢。任何帮助将不胜感激。

    c_output = 2
c_input = 784 * 3

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 conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

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

self.c_x = tf.placeholder(tf.float32, shape=[None, c_input])
self.c_y_ = tf.placeholder(tf.float32, shape=[None, c_output])

self.W_conv1 = weight_variable([5, 5, 3, 32])
self.b_conv1 = bias_variable([32])
self.x_image = tf.reshape(self.c_x, [-1, 28, 28 , 3])
self.h_conv1 = tf.nn.relu(conv2d(self.x_image, self.W_conv1) + self.b_conv1)
self.h_pool1 = max_pool_2x2(self.h_conv1)

self.W_conv2 = weight_variable([5, 5, 32, 64])
self.b_conv2 = bias_variable([64])

self.h_conv2 = tf.nn.relu(conv2d(self.h_pool1, self.W_conv2) + self.b_conv2)
self.h_pool2 = max_pool_2x2(self.h_conv2)

self.W_fc1 = weight_variable([7 * 7 * 64, 1024])
self.b_fc1 = bias_variable([1024])

self.h_pool2_flat = tf.reshape(self.h_pool2, [-1, 7 * 7 * 64 ])
self.h_fc1 = tf.nn.relu(tf.matmul(self.h_pool2_flat, self.W_fc1) + self.b_fc1)

self.keep_prob = tf.placeholder(tf.float32)
self.h_fc1_drop = tf.nn.dropout(self.h_fc1, self.keep_prob)

self.W_fc2 = weight_variable([1024, c_output])
self.b_fc2 = bias_variable([c_output])

self.y_conv = tf.matmul(self.h_fc1_drop, self.W_fc2) + self.b_fc2

self.c_cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(self.y_conv, self.c_y_))
self.c_train_step = tf.train.AdamOptimizer(1e-4).minimize(self.c_cross_entropy)
self.c_correct_prediction = tf.equal(tf.argmax(self.y_conv, 1), tf.argmax(self.c_y_, 1))
self.c_accuracy = tf.reduce_mean(tf.cast(self.c_correct_prediction, tf.float32))

最佳答案

TL;DR:对于您当前的程序,数据在内存中的布局应该是 R-G-B-R-G-B-R-G-B-R-G-B...

我从这一行假设您正在传递 28x28 像素的 RGB 图像:

self.x_image = tf.reshape(self.c_x, [-1, 28, 28, 3])

我们可以将self.x_image 的维度称为“batch”、“height”、“width”和“channel”。这与 tf.nn.conv_2d() 的默认数据格式匹配和 tf.nn.max_pool() .

在 TensorFlow 中,张量的内存表示是 row-major order (或“C”排序,因为这是 C 编程语言中数组的表示)。本质上这意味着最右边的维度变化最快,张量的元素按以下顺序打包在内存中(其中 ? 代表未知的批量大小,减 1):

[0,  0,  0,  0]
[0, 0, 0, 1]
[0, 0, 0, 2]
[0, 0, 1, 0]
...
[?, 27, 27, 1]
[?, 27, 27, 2]

因此您的程序可能没有正确解释图像数据。至少有两种选择:

  1. reshape 您的数据以匹配其真实顺序(“batch”、“channels”、“height”、“width”):

    self.x_image = tf.reshape(self.c_x, [-1, 3, 28, 28])

    事实上,这种格式有时对卷积更有效。您可以通过传递可选参数 data_format="NCHW"指示 tf.nn.conv2d()tf.nn.max_pool() 在不转置的情况下使用它,但您还需要更改偏置变量的形状以匹配。

  2. 转置图像数据以匹配使用 tf.transpose() 的程序结果:

    self.x_image = tf.transpose(tf.reshape(self.c_x, [-1, 3, 28, 28]), [0, 2, 3, 1])

关于python - Tensorflow 3 channel 颜色输入顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41396970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com