2016-11-13 10 views
1

私は現在、MNIST for ML Beginners codeを少し変更してニューラルネットワークを作成しようとしています。Tensorflowニューラルネットワークの入力データとしてCSVを使用するにはどうすればよいですか?

Image_Name |Nevus? |Dysplastic Nevus?| Melanoma? asdfgjkgdsl.png |1 |0 |0

イメージ名、およびそれがワンホット結果だ:私はこのように編成だCSVを持っています。各画像は1022 x 767で、各ピクセルの色を入力として使いたいと思います。そのため、私はMNISTコードを2,351,622個の入力(1022ピクセル幅* 767ピクセル高* 1ピクセルあたり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})) 

コメント行は、データをニューラルネットワークにロードするために置き換えなければならない行です。

from PIL import Image 
import numpy as np 

list(np.array(Image.open('asdfgjkgdsl.png')).ravel().flatten()) 

どのようにニューラルネットワークを訓練するために使用されるtensorflowに、このデータセットをロードすることができます(私が見つけたことを)各画像の2.3M入力を取得する最も簡単な方法はにありますか?

答えて

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を使用することができます行ごとに代わりに。

関連する問題