[英]Tensorflow Autoencoder with custom training examples from binary file
I'm trying to adapt the Tensorflow Autoencoder code found here ( https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/autoencoder.py ) to use my own training examples. 我正在尝试调整此处( https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/autoencoder.py )中的Tensorflow Autoencoder代码以使用我自己的训练示例。 My training examples are single channel 29*29 (gray level) images saved as UINT8 values continuously in a binary file.
我的训练示例是将单通道29 * 29(灰度)图像连续保存为UINT8值的二进制文件。 I have created a module which creates data_batches which will guide the training.
我创建了一个模块,该模块创建用于指导培训的data_batches。 This is the module:
这是模块:
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
I then adapt the Autoencoder code to load batch_xs from my input batch feeding code: 然后,我将自动编码器代码改编为从输入的批处理供稿代码中加载batch_xs:
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!")
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)' 不幸的是,运行代码时出现以下错误: ValueError:无法为Tensor'Placeholder:0'输入形状为((,, 841)'的形状(1,128,29,29,1)的值
My first question is why do I have Tensors of shape (1, 128, 29, 29, 1) when I was expecting (128,29,29,1)? 我的第一个问题是,为什么在我期望(128,29,29,1)时具有形状为(1,128,29,29,1)的张量? Am I missing something here?
我在这里想念什么吗?
I also don't understand the following code and how can I alter it in order to compare it with my dataset: 我也不理解以下代码,以及如何更改它以便与我的数据集进行比较:
# Applying encode and decode over test set
encode_decode = sess.run(
y_pred, feed_dict={X: mnist.test.images[:examples_to_show]})
As I understand it, this code executes the y_pred part of the graph and passes the first 10 test images to the placeholder X, previously defined. 据我了解,此代码执行图形的y_pred部分,并将前10个测试图像传递到先前定义的占位符X。 If I were to use a second data queue for my test images (29x29) how would I input these into the above dictionary?
如果我将第二个数据队列用于测试图像(29x29),我如何将它们输入上述字典中?
For example, using my code I could define a data_batch_eval as follows: 例如,使用我的代码,我可以如下定义data_batch_eval:
data_batch_eval = data_reader.batch_generator([DATA_PATH_EVAL],29**2) # eval set
Nonetheless, how would I extract the first 10 test images to feed the dictionary? 尽管如此,我将如何提取前10张测试图像来填充字典?
My first question is why do I have Tensors of shape (1, 128, 29, 29, 1) when I was expecting (128,29,29,1)?
我的第一个问题是,为什么在我期望(128,29,29,1)时具有形状为(1,128,29,29,1)的张量? Am I missing something here?
我在这里想念什么吗?
You need to remove the bracket in sess.run: 您需要删除sess.run中的括号:
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)'
不幸的是,运行代码时出现以下错误:ValueError:无法为Tensor'占位符:0'输入形状为((,, 841)'的形状(1,128,29,29,1)的值
You have declared your placeholder X as which is of [None, 841] and feeding an input [128, 29, 29, 1]: 您已将占位符X声明为[None,841],并提供了输入[128,29,29,1]:
X = tf.placeholder("float", [None, n_input])
Either change your feed input or your placeholder, so that both have the same size. 更改您的供稿输入或占位符,以使两者的大小相同。
Note: Your handling of queues are inefficient, you directly pass the data_batch
as input to your network
and not through the feed in
mechanism. 注意:队列的处理效率低下,您直接将
data_batch
作为输入传递到network
而不是通过feed in
机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.