python – Tensorflow 3通道颜色输入顺序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Tensorflow 3通道颜色输入顺序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3079字,纯文字阅读大概需要5分钟。
内容图文
我正在使用张量流来处理彩色图像和卷积神经网络.下面是一段代码片段.
我的代码运行所以我认为我的通道数正确.我的问题是,如何正确订购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])
内容总结
以上是互联网集市为您收集整理的python – Tensorflow 3通道颜色输入顺序全部内容,希望文章能够帮你解决python – Tensorflow 3通道颜色输入顺序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。