我目前正试图通过略微改变MNIST for ML Beginners code来编写神经网络。我有一个像这样组织的CSV:
Image_Name |Nevus? |Dysplastic Nevus?| Melanoma?
asdfgjkgdsl.png |1 |0 |0
图像名称,它是一个热门的结果。每张图像都是1022 x 767,我也希望将每个像素的颜色用作输入。因此,我将MNIST代码更改为2,351,622个输入(1022像素宽* 767像素高*每像素3种颜色)和3个输出。
# from tensorflow.examples.tutorials.mnist import input_data
# mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
def main():
x = tf.placeholder(tf.float32, [None, 2351622])
W = tf.Variable(tf.zeroes([2351622, 3]))
b = tf.Variable(tf.zeroes([3]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 3])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
example, label = sess.run([features, col5])
# batch_xs, batch_ys = mnist.train.next_batch(100)
# sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
注释行是我必须替换以将我的数据加载到神经网络中的行。为每张图像(我已经找到)获取2.3M输入的最简单方法是:
from PIL import Image
import numpy as np
list(np.array(Image.open('asdfgjkgdsl.png')).ravel().flatten())
如何将此数据集加载到tensorflow中以用于训练神经网络?
答案 0 :(得分:1)
推荐的方法可能是准备一系列tf_records
个文件。 MNIST就是这样做的一个例子。然后,您创建一个队列。为了节省空间,最好将输入保持为png格式并在运行时使用decode_png
。
简而言之,首先转换(你应该写多个文件):
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def convert():
writer = tf.python_io.TFRecordWriter(output_filename)
for filename, nv, dnv, mn in parse_csv(...):
fs = {}
png_data = read_image_as_np_array(filename)
image_name = 'data/image/png'
fs['png_data'] = _bytes_feature(png_data)
fs['label'] = _bytes_feature([nv, dnv, mn])
example = tf.train.Example(features=tf.train.Features(feature=fs))
writer.write(example.SerializeToString())
writer.close()
然后,阅读它:(把它放在队列中)
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_from_queue)
features_def = {
'png_data': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([3], tf.uint8)
}
features = tf.parse_single_example(serialized_example, features=feature_def)
image = tf.image.decode_png(features['png_data'])
...
您也可以使用tf.TextLineReader
逐行阅读。