python – Tensorflow 3通道颜色输入顺序

我正在使用张量流来处理彩色图像和卷积神经网络.下面是一段代码片段.

我的代码运行所以我认为我的通道数正确.我的问题是,如何正确订购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 …

我假设你在这行中传递了28×28像素的RGB图像:

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

我们可以将self.x_image的维度称为“批处理”,“高度”,“宽度”和“通道”.这与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]

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

>重塑您的数据以匹配其真实订单(“批次”,“渠道”,“高度”,“宽度”):

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

实际上,这种格式有时对卷积更有效.您可以通过传递可选参数data_format =“NCHW”来指示tf.nn.conv2d()和tf.nn.max_pool()使用它而不进行转置,但您还需要更改偏置变量的形状以匹配.
>使用tf.transpose()转置图像数据以匹配程序的结果:

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

相关文章

MNIST数据集可以说是深度学习的入门,但是使用模型预测单张M...
1、新建tensorflow环境(1)打开anacondaprompt,输入命令行...
这篇文章主要介绍“张量tensor是什么”,在日常操作中,相信...
tensorflow中model.fit()用法model.fit()方法用于执行训练过...
https://blog.csdn.net/To_be_little/article/details/12443...
根据身高推测体重const$=require('jquery');const...