gpt4 book ai didi

python - 在 TensorFlow 中读取与 mnist 数据集相同格式的新数据集

转载 作者:行者123 更新时间:2023-12-05 06:26:54 25 4
gpt4 key购买 nike

我有一个深度学习模型,我必须在其中输入尺寸为 100X100 的图像。我有的数据是

训练图像 - x_train (530,100,100),火车标签 - y_train (530,4),

测试图像 - x_test(89,100,100),测试标签 - y_test(89,4)

使用-读取数据集mnist

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

它会生成这样的东西-

Datasets(train=tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x7fd64cae76a0, 
validation=tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x7fd64cae7be0,
test=tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet object at 0x7fd64cae7400)

我必须以相同的格式转换我的数据,这样才能与我拥有的代码一起使用。请帮忙

    epochs = 20
batch_size = 100
image_vector = 28*28


for i in range(epochs):
training_accuracy = []
epoch_loss = []

for ii in tqdm(range(mnist.train.num_examples // batch_size)):

batch = mnist.train.next_batch(batch_size)

images = batch[0].reshape((-1, 28, 28))
targets = batch[1]

c, _, a = session.run([model.cost, model.opt, model.accuracy], feed_dict={model.inputs: images, model.targets:targets})

epoch_loss.append(c)
training_accuracy.append(a)

print("Epoch: {}/{}".format(i, epochs), " | Current loss: {}".format(np.mean(epoch_loss)),
" | Training accuracy: {:.4f}%".format(np.mean(training_accuracy)))

编辑 1: 按照建议,我做了以下操作-

num_examples=271
batch_size=10
buffer_size=271
num_cpu_cores=4
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset = dataset.shuffle(buffer_size, reshuffle_each_iteration=True).repeat()
#dataset = dataset.apply(tf.data.batch( batch_size=batch_size, num_parallel_batches=num_cpu_cores))

#batch1=dataset.batch(10)

iterator = dataset.make_one_shot_iterator()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for ii in tqdm(range(num_examples // batch_size)):
batch = iterator.get_next()
images = batch[0]
targets = batch[1]

c, _, a = sess.run([model.cost, model.opt, model.accuracy])

epoch_loss.append(c)
training_accuracy.append(a)

print("Epoch: {}/{}".format(i, epochs), " | Current loss: {}".format(np.mean(epoch_loss)),
" | Training accuracy: {:.4f}%".format(np.mean(training_accuracy)))

图像和目标不是批处理,而是单个图像和标签。

(, )

请建议如何批量处理数据并输入 sess.run

编辑 2:这是算法的完整代码-

def LSTM_layer(lstm_cell_units, number_of_layers, batch_size, dropout_rate=0.8):
'''
This method is used to create LSTM layer/s for PixelRNN

Input(s): lstm_cell_unitis - used to define the number of units in a LSTM layer
number_of_layers - used to define how many of LSTM layers do we want in the network
batch_size - in this method this information is used to build starting state for the network
dropout_rate - used to define how many cells in a layer do we want to 'turn off'

Output(s): cell - lstm layer
init_state - zero vectors used as a starting state for the network
'''


#layer = tf.contrib.rnn.BasicLSTMCell(lstm_cell_units)
layer = tf.nn.rnn_cell.LSTMCell(lstm_cell_units,name='basic_lstm_cell')

if dropout_rate != 0:
layer = tf.contrib.rnn.DropoutWrapper(layer, dropout_rate)

cell = tf.contrib.rnn.MultiRNNCell([layer]*number_of_layers)

init_size = cell.zero_state(batch_size, tf.float32)

return cell, init_size

def rnn_output(lstm_outputs, input_size, output_size):
'''
Output layer for the lstm netowrk

Input(s): lstm_outputs - outputs from the RNN part of the network
input_size - in this case it is RNN size (number of neuros in RNN layer)
output_size - number of neuros for the output layer == number of classes

Output(s) - logits,
'''


outputs = lstm_outputs[:, -1, :]

weights = tf.Variable(tf.random_uniform([input_size, output_size]), name='rnn_out_weights')
bias = tf.Variable(tf.zeros([output_size]), name='rnn_out_bias')

output_layer = tf.matmul(outputs, weights) + bias
return output_layer

def loss_optimizer(rnn_out, targets, learning_rate):
'''
Function used to calculate loss and minimize it

Input(s): rnn_out - logits from the fully_connected layer
targets - targets used to train network
learning_rate/step_size


Output(s): optimizer - optimizer of choice
loss - calculated loss function
'''
loss = tf.nn.softmax_cross_entropy_with_logits(logits=rnn_out, labels=targets)
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)
return optimizer, loss
class PixelRNN(object):

def __init__(self, learning_rate=0.001, batch_size=10, classes=4, img_size = (129, 251), lstm_size=64,
number_of_layers=1, dropout_rate=0.6,clip_rate=None):

'''
PixelRNN - call this class to create whole model

Input(s): learning_rate - how fast are we going to move towards global minima
batch_size - how many samples do we feed at ones
classes - number of classes that we are trying to recognize
img_size - width and height of a single image
lstm_size - number of neurons in a LSTM layer
number_of_layers - number of RNN layers in the PixelRNN
dropout_rate - % of cells in a layer that we are stopping gradients to flow through
'''

#This placeholders are just for images
self.inputs = tf.placeholder(tf.float32, [None, img_size[0], img_size[1]], name='inputs')
self.targets = tf.placeholder(tf.int32, [None, classes], name='targets')

cell, init_state = LSTM_layer(lstm_size, number_of_layers, batch_size, dropout_rate)

outputs, states = tf.nn.dynamic_rnn(cell, self.inputs, initial_state=init_state)

rnn_out = rnn_output(outputs, lstm_size, classes)

self.opt, self.cost = loss_optimizer(rnn_out, self.targets, learning_rate)

predictions = tf.nn.softmax(rnn_out)

currect_pred = tf.equal(tf.cast(tf.round(tf.argmax(predictions, 1)), tf.int32), tf.cast(tf.argmax(self.targets, 1), tf.int32))
self.accuracy = tf.reduce_mean(tf.cast(currect_pred, tf.float32))

self.predictions = tf.argmax(tf.nn.softmax(rnn_out), 1)


tf.reset_default_graph()
model = PixelRNN()
num_examples=271
batch_size=10
buffer_size=271
num_cpu_cores=4
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset = dataset.shuffle(buffer_size, reshuffle_each_iteration=True).repeat()
#dataset = dataset.apply(tf.data.batch( batch_size=batch_size, num_parallel_batches=num_cpu_cores))

dataset = dataset.batch(10) # apply batch to dataset

iterator = dataset.make_one_shot_iterator() # create iterator
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for ii in tqdm(range(num_examples // batch_size)):
batch = iterator.get_next() #run iterator
images = batch[0]
targets = batch[1]

c, _, a = sess.run([model.cost, model.opt, model.accuracy],feed_dict={model.inputs: images, model.targets:targets})

epoch_loss.append(c)
training_accuracy.append(a)

print("Epoch: {}/{}".format(i, epochs), " | Current loss: {}".format(np.mean(epoch_loss)),
" | Training accuracy: {:.4f}%".format(np.mean(training_accuracy)))

当我按照建议操作时,出现以下错误-

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.For reference, the tensor object was Tensor("IteratorGetNext:0", shape=(?, 129, 251), dtype=float32) which was passed to the feed with key Tensor("inputs:0", shape=(?, 129, 251), dtype=float32).

无法弄清楚这里出了什么问题

最佳答案

如果您的数据集是 numpy 数组或图像文件列表,请使用 from_tensor_slices。定义解析函数,如果使用文件名列表,请使用 read_filedecode_image,否则只需应用任何预处理

def parse_image(filename, label):
file = tf.read_file(filename)
image = tf.image.decode_image(file)
#do any image/label preprocessing here
return image, label

然后定义数据集对象。通常,使用数据集的长度作为随机缓冲区,但这可能取决于大小。重复函数将控制时期(无值传递 = 无限迭代)。如果您不需要任何预处理,请将 dataset.apply 行替换为 dataset = dataset.batch(batch_size)

dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
dataset = dataset_train.shuffle(buffer_size, reshuffle_each_iteration=True).repeat()
dataset = dataset.apply(tf.data.batch( batch_size=batch_size, num_parallel_batches=num_cpu_cores))

创建迭代器。一般来说,没有必要使用 feed dict,只要你的输入数组不会导致 2GB graphdef 限制。

iterator = dataset.make_one_shot_iterator()
batch = iterator.get_next()

编辑:您需要从数据集中创建迭代器,这是整体结构:

dataset = dataset.batch(10) # apply batch to dataset 
iterator = dataset.make_one_shot_iterator() # create iterator
batch = iterator.get_next() #run iterator
images = batch[0]
targets = batch[1]

logits = Model_function(images)
loss = loss_function(logits, targets)
train_op = optimizer.minimize()

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(steps):
sess.run(train_op)

关于python - 在 TensorFlow 中读取与 mnist 数据集相同格式的新数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55725074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com