gpt4 book ai didi

python-3.x - 训练后将数据输入 tensorflow 模型

转载 作者:行者123 更新时间:2023-11-30 08:46:14 24 4
gpt4 key购买 nike

我制作了一个简单的图表,以便我可以测试恢复图表并输入数据以进行评估。我使用 CIFAR10 作为测试集,并且我构建的模型仅包含两个卷积层,后面是一个全连接层。数据通过队列加载,由图形处理并应用反向传播。

模型的代码:

# Libraries
# Standard Libraries
import os
import re
import sys

# Third Party Libraries
import numpy as np
import tensorflow as tf

# Custom Paths
PACKAGE_PARENT = ".."
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(),
os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

# User Defined Libraries
# User Defined Libraries
from helper_scripts.synset_cifar import *

# Directories
tf.app.flags.DEFINE_string("data_dir", "~/Documents/CIFAR/data/",
"The directory containing the training images")
tf.app.flags.DEFINE_string("save_dir", "./save/", "Checkpoints save directory")

# Model Paramaters
tf.app.flags.DEFINE_integer("num_classes", 10, "The number of classes")
tf.app.flags.DEFINE_integer("batch_size", 64, "The batch size")
tf.app.flags.DEFINE_integer('num_epochs', 3, "The number of training steps")

FLAGS = tf.app.flags.FLAGS

def load_data(data_dir):
data = []
file_list = []

for root, dirs, files in os.walk(data_dir, topdown=False):
for file in files:
label_name = re.search(r'(n\d+)', file).group(1)
img_path = "{}{}/{}".format(data_dir, label_name, file)
file_list.append(img_path)


for img_fn in file_list:
ext = os.path.splitext(img_fn)[1] # Gets the extensions of the files in the filelist
if ext != '.png':
continue

label_name = re.search(r'(n\d+)', img_fn).group(1) # Synset index

fn = os.path.join(data_dir, img_fn)

label_index = synset_map[label_name]["index"]

data.append({
"filename": fn,
"label_name": label_name, #n\d+
"label_index": label_index,
"desc": synset[label_index],
})

return data

def decode_jpeg(image_buffer):
# with tf.name_scope("decode_jpeg", values=[image_buffer]):
image = tf.image.decode_jpeg(image_buffer, channels=3)
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = tf.cast(image, tf.float32)
image.set_shape([32, 32, 3])

return image

def distorted_inputs():
data = load_data(FLAGS.data_dir)
images_and_labels = []

filenames = [d['filename'] for d in data]
label_indexes_ = [d['label_index'] for d in data]

label_indexes = tf.one_hot(label_indexes_, depth=FLAGS.num_classes,
on_value=1.0, off_value=0.0, axis=-1, dtype=tf.float32)

# with tf.variable_scope('InputProducer'):
filename, label_index = tf.train.slice_input_producer(
[filenames, label_indexes],
num_epochs=FLAGS.num_epochs,
seed=22,
capacity=32,
shuffle=True)

image_buffer = tf.read_file(filename)

image = decode_jpeg(image_buffer)
images_and_labels.append([image, label_index])

images, label_index_batch = tf.train.batch_join(images_and_labels,
batch_size=FLAGS.batch_size,
capacity=2 * FLAGS.batch_size,
dynamic_pad=False,
allow_smaller_final_batch=True)

return images, label_index_batch

def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)

return tf.Variable(initial)

def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def conv_layer(input, shape):
W = weight_variable(shape)
b = bias_variable([shape[3]])

return tf.nn.relu(conv2d(input, W) + b)

def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

def full_layer(input, size):
in_size = int(input.get_shape()[1])
W = weight_variable([in_size, size])
b = bias_variable([size])

return tf.matmul(input, W) + b

def loss(labels, logits):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
labels=labels,
logits=logits))

return loss

def accuracy(labels, logits):
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

return accuracy

def inference(images):
with tf.variable_scope("conv1x"):
conv1 = conv_layer(images, shape=[5, 5, 3, 32])
conv1_pool = max_pool_2x2(conv1)

with tf.variable_scope("conv2x"):
conv2 = conv_layer(conv1_pool, shape=[5, 5, 32, 64])
conv2_pool = max_pool_2x2(conv2)

with tf.variable_scope("fc_layer"):
conv2_flat = tf.reshape(conv2_pool, [-1, 8 * 8 * 64])
full_1 = tf.nn.relu(full_layer(conv2_flat, 1024))
# full1_drop = tf.nn.dropout(full_1, keep_prob=keep_prob)
y_conv = full_layer(full_1, 10)

return y_conv

def train(logits, labels, length=300):
with tf.variable_scope("loss"):
loss_ = loss(labels, logits)
accu_ = accuracy(logits, labels)

global_step = tf.get_variable('global_step', [],
initializer=tf.constant_initializer(0),
trainable=False)

optimizer = tf.train.AdamOptimizer(1e-3)
train_op = optimizer.minimize(loss_, global_step=global_step)

init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())

saver = tf.train.Saver(tf.global_variables(), max_to_keep=2)

with tf.Session() as sess:
writer = tf.summary.FileWriter("./graphs", sess.graph)

sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

try:
for i in range(length):
if coord.should_stop():
break

o = sess.run([loss_, train_op, accu_, global_step])
print("Loss: {:05f}, Accuracy: {:04f}, Global Step: {:04d}".\
format(o[0], o[2], int(o[3])))

# Save the model checkpoints periodically:
if o[-1] > 1 and o[-1] % 100 == 0:
checkpoint_path = os.path.join(FLAGS.save_dir, "model_cifar.ckpt")
saver.save(sess, checkpoint_path, global_step=global_step)

except Exception as e:
print("Error: {}".format(e))

finally:
writer.close()
coord.request_stop()
coord.join(threads)

def main(*args, **kwargs):
images, labels = distorted_inputs()
y_conv = inference(images)
train(y_conv, labels, length=400)

if __name__ == "__main__":
tf.app.run()

由于其简单性,该模型似乎确实按照人们的预期工作。现在,当涉及到恢复模型时,我已经了解了:

Tensorflow: restoring a graph and model then running evaluation on a single image

Tensorflow: how to save/restore a model?

其中第一个链接似乎是最有帮助的具体 bigdata2s 解决方案,但我无法使其工作。我的代码:

def forward():
images = tf.placeholder(tf.float32, (1, 32, 32, 3), name='imgs')
loc_test_img = "./images/test.png"
img = mpimage.imread(loc_test_img)

sess = tf.Session('', tf.Graph())

with sess.graph.as_default() as graph:
# Read meta graph and checkpoint to restore tf session
saver = tf.train.import_meta_graph("./save/model_cifar.ckpt-301.meta")
saver.restore(sess, "./save/model_cifar.ckpt-301")

# images = tf.placeholder(tf.float32, (1, 32, 32, 3), name='imgs')

# Read a single image from a file.
img = np.expand_dims(img, axis=0)

# Start the queue runners. If they are not started the program will hang
# see e.g. https://www.tensorflow.org/programmers_guide/reading_data
coord = tf.train.Coordinator()
threads = []
for qr in graph.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
threads.extend(qr.create_threads(sess, coord=coord, daemon=True,
start=True))

# In the graph created above, feed "is_training" and "imgs"
# placeholders. Feeding them will disconnect the path from queue
# runners to the graph and enable a path from the placeholder
# instead. The "img" placeholder will be fed with the image that
# was read above.

# o = sess.run(pred, feed_dict={'images': img})

# Prints classifiction results
sess.close()
# print(logits)

我已经尝试了其他发布的解决方案,但仍然没有运气。

最佳答案

您是否尝试过取消注释该行:

 o = sess.run(pred, feed_dict={'images': img})

无论是训练还是测试,都必须执行 Tensorflow session 才能获得输出(据我所知)。

然后需要打印o的值。

再问一点,你定义了pred吗?简单查看后无法在代码中看到它。与 sess.run 一样,您正在执行的变量需要定义。

您可能想要使用accuracyaccu_loss来代替,它们已在恢复的模型中定义并且应该仍然存在。有了这些,您将需要根据训练代码定义提供 logits, labels 作为 feed_dict 输入。

这还有一个额外的好处,就是可以与训练阶段使用的指标进行比较。

关于python-3.x - 训练后将数据输入 tensorflow 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46286614/

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