2017-07-15 3 views
1

ここにあるTensorflowオートエンコーダーコード(https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/autoencoder.py)に自分のトレーニング例を適用しようとしています。私の訓練の例は、シングルチャンネルの29 * 29(グレイレベル)の画像で、UINT8値としてバイナリファイルに保存されています。トレーニングを指導するdata_batchesを作成するモジュールを作成しました。Tensorflowバイナリファイルのカスタムトレーニング例を含むオートエンコーダー

import tensorflow as tf 

# various initialization variables 
BATCH_SIZE = 128 
N_FEATURES = 9 

def batch_generator(filenames, record_bytes): 
""" filenames is the list of files you want to read from. 
In this case, it contains only heart.csv 
""" 

record_bytes = 29**2 # 29x29 images per record 
filename_queue = tf.train.string_input_producer(filenames) 
reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) # skip the first line in the file 
_, value = reader.read(filename_queue) 
print(value) 

# record_defaults are the default values in case some of our columns are empty 
# This is also to tell tensorflow the format of our data (the type of the decode result) 
# for this dataset, out of 9 feature columns, 
# 8 of them are floats (some are integers, but to make our features homogenous, 
# we consider them floats), and 1 is string (at position 5) 
# the last column corresponds to the lable is an integer 

#record_defaults = [[1.0] for _ in range(N_FEATURES)] 
#record_defaults[4] = [''] 
#record_defaults.append([1]) 

# read in the 10 columns of data 
content = tf.decode_raw(value, out_type=tf.uint8) 
#print(content) 

# convert the 5th column (present/absent) to the binary value 0 and 1 
#condition = tf.equal(content[4], tf.constant('Present')) 
#content[4] = tf.where(condition, tf.constant(1.0), tf.constant(0.0)) 

# pack all UINT8 values into a tensor 
features = tf.stack(content) 
#print(features) 

# assign the last column to label 
#label = content[-1] 

# The bytes read represent the image, which we reshape 
# from [depth * height * width] to [depth, height, width]. 
depth_major = tf.reshape(
    tf.strided_slice(content, [0], 
        [record_bytes]), 
    [1, 29, 29]) 

# Convert from [depth, height, width] to [height, width, depth]. 
uint8image = tf.transpose(depth_major, [1, 2, 0]) 

# minimum number elements in the queue after a dequeue, used to ensure 
# that the samples are sufficiently mixed 
# I think 10 times the BATCH_SIZE is sufficient 
min_after_dequeue = 10 * BATCH_SIZE 

# the maximum number of elements in the queue 
capacity = 20 * BATCH_SIZE 

# shuffle the data to generate BATCH_SIZE sample pairs 
data_batch = tf.train.shuffle_batch([uint8image], batch_size=BATCH_SIZE, 
            capacity=capacity, min_after_dequeue=min_after_dequeue) 

return data_batch 

私はその後、私の入力バッチ送りコードからbatch_xsをロードするためのオートエンコーダコード適応: ValueErrorを:コードを実行するときに

from __future__ import division, print_function, absolute_import 

# Various initialization variables 
DATA_PATH1 = 'data/building_extract_train.bin' 

import tensorflow as tf 
import numpy as np 
import matplotlib.pyplot as plt 

# custom imports 
import data_reader 

# Import MNIST data 
from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets("MNIST_data", one_hot=True) 

# Parameters 
learning_rate = 0.01 
training_epochs = 20 
batch_size = 256 
display_step = 1 
examples_to_show = 10 

# Network Parameters 
n_hidden_1 = 256 # 1st layer num features 
n_hidden_2 = 128 # 2nd layer num features 
#n_input = 784 # edge-data input (img shape: 28*28) 
n_input = 841 # edge-data input (img shape: 29*29) 

# tf Graph input (only pictures) 
X = tf.placeholder("float", [None, n_input]) 

# create the data batches (queue) 
# Accepts two parameters. The tensor containing the binary files and the size of a record 
data_batch = data_reader.batch_generator([DATA_PATH1],29**2) 

weights = { 
'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), 
'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 
'decoder_h1': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])), 
'decoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_input])), 
} 
biases = { 
'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1])), 
'encoder_b2': tf.Variable(tf.random_normal([n_hidden_2])), 
'decoder_b1': tf.Variable(tf.random_normal([n_hidden_1])), 
'decoder_b2': tf.Variable(tf.random_normal([n_input])), 
} 

# Building the encoder 
def encoder(x): 
# Encoder Hidden layer with sigmoid activation #1 
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']), 
           biases['encoder_b1'])) 
# Decoder Hidden layer with sigmoid activation #2 
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']), 
           biases['encoder_b2'])) 
return layer_2 


# Building the decoder 
def decoder(x): 
# Encoder Hidden layer with sigmoid activation #1 
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']), 
           biases['decoder_b1'])) 
# Decoder Hidden layer with sigmoid activation #2 
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,  weights['decoder_h2']), 
           biases['decoder_b2'])) 
return layer_2 

# Construct model 
encoder_op = encoder(X) 
decoder_op = decoder(encoder_op) 

# Prediction 
y_pred = decoder_op 
# Targets (Labels) are the input data. 
y_true = X 

# Define loss and optimizer, minimize the squared error 
cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2)) 
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost) 

# Initializing the variables 
init = tf.global_variables_initializer() 

# Launch the graph 
with tf.Session() as sess: 
coord = tf.train.Coordinator() 
threads = tf.train.start_queue_runners(coord=coord) 
sess.run(init) 
total_batch = int(mnist.train.num_examples/batch_size) 
# Training cycle 
for epoch in range(training_epochs): 
    # Loop over all batches 
    for i in range(total_batch): 
     #batch_xs, batch_ys = mnist.train.next_batch(batch_size) 
     batch_xs = sess.run([data_batch]) 
     #print(batch_xs) 
     #batch_xs = tf.reshape(batch_xs, [-1, n_input]) 
     # Run optimization op (backprop) and cost op (to get loss value) 
     _, c = sess.run([optimizer, cost], feed_dict={X: batch_xs}) 
    # Display logs per epoch step 
    if epoch % display_step == 0: 
     print("Epoch:", '%04d' % (epoch+1), 
       "cost=", "{:.9f}".format(c)) 
coord.request_stop() 
coord.join(threads) 
print("Optimization Finished!") 

は、残念ながら、私はこのエラーを取得するこれはモジュールです。テンソル 'プレースホルダー:0'、形 '(?、841)'の形の値を供給できません。

私の最初の質問は、なぜテンソルの形をしているのですか? (1,128,29,29,1)のとき期待している(128,29,29,1)?私はここに何かを逃していますか

私はまた、次のコードを理解していないとどのように私は私のデータセットと比較するために、それを変更することができます。

# Applying encode and decode over test set 
encode_decode = sess.run(
    y_pred, feed_dict={X: mnist.test.images[:examples_to_show]}) 

私はそれを理解するように、このコードは、グラフのy_pred一部を実行し、前に定義したプレースホルダXに最初の10個のテスト画像を渡します。私がテスト画像(29x29)に2番目のデータキューを使用する場合、これらを上記の辞書にどのように入力すればよいでしょうか?例えば

、次のように私はdata_batch_evalを定義することができ、私のコードを使用して:

それでも
data_batch_eval = data_reader.batch_generator([DATA_PATH_EVAL],29**2) # eval set 

を、私は辞書を養うために最初の10枚のテスト画像を抽出する方法を?

+0

あなたの '' 'features''変数の目的はあなたが作成したようですが使用していないようです。 – Anis

+0

コメントするのを忘れました。それは削除する必要があります。 – divined

答えて

0

My first question is why do I have Tensors of shape (1, 128, 29, 29, 1) when I was expecting (128,29,29,1)? Am I missing something here?

あなたはsess.runにブラケットを削除する必要があります:あなたは、あなたのプレースホルダのXを宣言した

batch_xs = sess.run(data_batch) 

Unfortunately, when running the code I get this error: ValueError: Cannot feed value of shape (1, 128, 29, 29, 1) for Tensor 'Placeholder:0', which has shape '(?, 841)'

どのようなし、841]のものであり、入力[128を供給し、 29,29、1]:

フィード入力またはプレースホルダを変更して、両方のサイズが同じになるようにします。

注:キューの処理が非効率的な場合は、networkに入力としてdata_batchを直接渡し、feed inメカニズムを使用しないでください。

+0

ありがとうございました。ブラケットを外して[29 ** 2]に再成形しました。残念ながら、このようなシンプルなネットワークでは辛抱強く遅いです。私はfeed_inメカニズムを使用する必要があるようです。 – divined

+0

はい、私がメモしたように、あなたのキューの扱いは間違っています。 'data_batch'を直接encoder_op = encoder(data_batch)に渡します。両方のキューとfeed_dict(推論中)の両方を使いたいなら、 'tf.placeholder_with_default()'を使うことができます。とにかく、その別の問題は全く新しい問題を開く、まだ問題がある場合は、私はそれに対処します。 –

関連する問題