Tensorflow 训练自己的数据集tfrecords问题

c5395348 2017-04-14 11:30:43

import tensorflow as tf
import numpy as np
def read_and_decode(filename):
filename_queue = tf.train.string_input_producer(['test_train.tfrecords'])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
}
)

img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [28, 28, 3])
img = tf.cast(img, tf.float32) * (1./ 255) - 0.5
label = tf.cast(features['label'], tf.int32)
return img, label

img, label = read_and_decode("test_train.tfrecords")

img_batch, label_batch = tf.train.shuffle_batch([img, label],
batch_size=100, capacity=2000,
min_after_dequeue=1000)
x = tf.placeholder("float",[None,784])

w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x,w)+b)

y_ = tf.placeholder("float",[None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

img_batch, label_batch = tf.train.shuffle_batch([img, label],
batch_size=100, capacity=2000,
min_after_dequeue=1000)
threads = tf.train.start_queue_runners(sess=sess)
for i in range(1000):
img_xs,label_xs = sess.run([img_batch,label_batch])
sess.run(train_step, feed_dict={x: img_xs,y_:label_xs})

按照博客上softmax实现MNIST数据集的例子改的 运行时出现
ValueError: Cannot feed value of shape (100, 28, 28, 3) for Tensor u'Placeholder:0', which has shape '(?, 784)'
这样的错误 不知道该怎么修改
...全文
11497 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
小蜗牛跑啊跑 2019-03-27
  • 打赏
  • 举报
回复
转换完成后如何区分训练集与测试集呢
HowieeY 2018-03-07
  • 打赏
  • 举报
回复
我知道了,你的图片本张数大于2000,所以在创建队列的时候capacity容量太小,放不下你的全部图片,所以要把capacity调整到大于你图片总数,下面的min_after_dequeue要小于capacity的值。
Simplicity_ 2017-09-28
  • 打赏
  • 举报
回复
请问楼上的问题解决了吗?我也遇到了这样的问题
lutongdrr 2017-07-27
  • 打赏
  • 举报
回复
博主问题解决了吗 我的也出现了这种情况,网络参数更改之后,出现了新的错误!
yokey1219 2017-07-19
  • 打赏
  • 举报
回复
建议改成如下试试:

import tensorflow as tf
import numpy as np
def read_and_decode(filename):
    filename_queue = tf.train.string_input_producer(['test_train.tfrecords'])
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)   
    features = tf.parse_single_example(
        serialized_example,
    features={
        'label': tf.FixedLenFeature([], tf.int64),
            'img_raw' : tf.FixedLenFeature([], tf.string),
        }
    )
 
    img = tf.decode_raw(features['img_raw'], tf.uint8)
    img = tf.reshape(img, [28*28*3])
    img = tf.cast(img, tf.float32) * (1./ 255) - 0.5
    label = tf.cast(features['label'], tf.int32)
    return img, label
 
img, label = read_and_decode("test_train.tfrecords")
 
img_batch, label_batch = tf.train.shuffle_batch([img, label],
                                                batch_size=100, capacity=2000,
                                                min_after_dequeue=1000)
x = tf.placeholder("float",[None,28*28*3])
 
w = tf.Variable(tf.zeros([28*28*3,10]))
b = tf.Variable(tf.zeros([10]))
 
y = tf.nn.softmax(tf.matmul(x,w)+b)
 
y_ = tf.placeholder("float",[None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
 
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
 
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
 
img_batch, label_batch = tf.train.shuffle_batch([img, label],
                                                   batch_size=100, capacity=2000,
                                                min_after_dequeue=1000)
threads = tf.train.start_queue_runners(sess=sess)
for i in range(1000):
    img_xs,label_xs = sess.run([img_batch,label_batch])
    sess.run(train_step, feed_dict={x: img_xs,y_:label_xs})
CQ_Liu 2017-04-20
  • 打赏
  • 举报
回复
看了下你的代码,应该是你的数据尺寸和网络需要的尺寸不一样。你网络输入的数据尺寸应该是[None,784],而不是【None,28,28】

6,721

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 Google技术社区
社区管理员
  • Google技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧