图片识别代码问题
创建了四个独立的.py文件合起来实现手写数字识别有没有大佬能帮着看一下下面的问题,因为1.x版本的Tensorflow已经下架,但是这里面很多编码都是1.X版本的Tensorflow,所以可能导致错误,可以帮着改一下不,就是不要改变大致目标
第一个文件:mnist_forward.py
-源代码:
import tensorflow as tf
INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500
def get_weight(shape, regularizer):
w = tf.Variable(tf.random.truncated_normal(shape, stddev=0.1))
if regularizer != None:
tf.add_to_collection("losses", tf.keras.regularizers.l2(regularizer)(w))
return w
def get_bias(shape):
b = tf.Variable(tf.zeros(shape))
return b
def forward(x, regularizer):
w1 = get_weight(, regularizer)
b1 = get_bias()
y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
w2 = get_weight(, regularizer)
b2 = get_bias()
y = tf.matmul(y1, w2) + b2
return y
运行结果:无报错信息,无任何执行结果,执行后一段时间自动跳出
第二个文件:mnist_backward.py
-源代码:
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from mnist_forward import INPUT_NODE, OUTPUT_NODE# 修正导入错误
import os
BATCH_SIZE = 200
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
STEPS = 50000
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH = "./model/"
MODEL_NAME = "mnist_model"
def backward(dataset):
x = tf.placeholder(tf.float32, )# 使用修正后的导入
y_ = tf.placeholder(tf.float32, )# 使用修正后的导入
y = mnist_forward.forward(x, REGULARIZER)
global_step = tf.Variable(0, trainable=False)
logits = y# 将神经网络输出 y 作为 logits
labels = tf.argmax(y_, 1)# 将类别索引作为 labels
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels)# 修改损失计算方式
cem = tf.reduce_mean(ce)
loss = cem + tf.add_n(tf.get_collection("losses"))
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
dataset.train.num_examples / BATCH_SIZE,
LEARNING_RATE_DECAY,
staircase=True)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies():
train_op = tf.no_op(name="train")
saver = tf.train.Saver()
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
for i in range(STEPS):
xs, ys = dataset.train.next_batch(BATCH_SIZE)
_, lossvalue, step = sess.run(, feed_dict={x: xs, y_: ys})
if i % 1000 == 0:
print("After %d training step(s), loss on training batch is %g." % (step, lossvalue))
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def main():
(x_train, y_train), _ = mnist.load_data()
x_train = x_train.reshape(x_train.shape, -1) / 255.0
y_train = tf.keras.utils.to_categorical(y_train, 10)
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(BATCH_SIZE)
backward(dataset)
if __name__ == "__main__":
main()
-运行结果:无报错信息,执行以后只要你点了IDLE的弹窗口就会卡死然后你必须手动退出,如果你让它往下执行,它就会出现很多字符串,想下载器一样,时不时会卡死一段时间,然后接着执行,运行大概五分钟以后,就会彻底卡死,需要手动退出,下面给一个运行截图:
https://i.postimg.cc/1zmXvXWN/Snipaste-2024-04-30-07-18-24.jpg
第三个文件:mnist_test.py
-源代码:
import tensorflow as tf
import numpy as np
import mnist_forward
import os
TEST_INTERVAL_SECS = 5
def test(mnist):
x = tf.placeholder(tf.float32, )
y_ = tf.placeholder(tf.float32, )
y = mnist_forward.forward(x, None)
ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
ema_restore = ema.variables_to_restore()
saver = tf.train.Saver(ema_restore)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
while True:
latest_checkpoint = tf.train.latest_checkpoint(mnist_backward.MODEL_SAVE_PATH)
if latest_checkpoint:
saver.restore(tf.compat.v1.get_default_session(), latest_checkpoint)
global_step = int(latest_checkpoint.split("-")[-1])
accuracy_score = accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})
print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
else:
print("No checkpoint file found")
return
time.sleep(TEST_INTERVAL_SECS)
def main():
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_test = np.reshape(x_test, (-1, mnist_forward.INPUT_NODE))
y_test = tf.keras.utils.to_categorical(y_test, mnist_forward.OUTPUT_NODE)
mnist.test = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(10000)
test(mnist)
if __name__ == "__main__":
main()
-运行结果:
同样没有报错信息,但是结果跟mnist.backward.py的结果一样,非常卡顿,运行一段时间后会自动退出,出现的数据也是一抹一样
第四个文件:mnist_app.py
-源代码:
import tensorflow.compat.v1 as tf
import numpy as np
from PIL import Image
import mnist_backward
import mnist_forward
import os
tf.compat.v1.disable_v2_behavior()
def restore_model(testPicArr):
with tf.Graph().as_default() as tg:
x = tf.compat.v1.placeholder(tf.float32, )
y = mnist_forward.forward(x, None)
preValue = tf.argmax(y, 1)
variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.compat.v1.train.Saver(variables_to_restore)
with tf.compat.v1.Session() as sess:
ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
print("Checkpoint file path:", ckpt.model_checkpoint_path)
saver.restore(sess, ckpt.model_checkpoint_path)
preValue = sess.run(preValue, feed_dict={x: testPicArr})
return preValue
else:
print("No checkpoint file found in", mnist_backward.MODEL_SAVE_PATH)
return -1
def pre_pic(picName):
img = Image.open(picName)
reIm = img.resize((28, 28), Image.LANCZOS)
im_arr = np.array(reIm.convert('L'))
threshold = 50
for i in range(28):
for j in range(28):
im_arr = 255 - im_arr
if im_arr < threshold:
im_arr = 0
else:
im_arr = 255
nm_arr = im_arr.reshape()
nm_arr = nm_arr.astype(np.float32)
img_ready = np.multiply(nm_arr, 1.0 / 255.0)
return img_ready
def application():
testNum = int(input("input the number of test pictures:"))
for i in range(testNum):
testPic = input("the path of test picture:")
testPicArr = pre_pic(testPic)
preValue = restore_model(testPicArr)
print("The prediction number is:", preValue)
def main():
application()
if __name__ == "__main__":
main()
-运行结果:
(1)警告信息:
WARNING:tensorflow:From D:\卢天恩文件夹\3.编程\0.Tendsorflow学习\2.代码实践\手写数字识别\源代码\mnist_app.py:8: The name tf.disable_v2_behavior is deprecated. Please use tf.compat.v1.disable_v2_behavior instead.
WARNING:tensorflow:From C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\compat\v2_compat.py:98: disable_resource_variables (from tensorflow.python.ops.resource_variables_toggle) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term(2)运行时无法查找节点,无法识别图片:
input the number of test pictures:3
the path of test picture:pic/3.jpg
WARNING:tensorflow:From D:\卢天恩文件夹\3.编程\0.Tendsorflow学习\2.代码实践\手写数字识别\源代码\mnist_app.py:18: The name tf.train.Saver is deprecated. Please use tf.compat.v1.train.Saver instead.
No checkpoint file found in ./model/
The prediction number is: -1
the path of test picture:pic/10.jpg
No checkpoint file found in ./model/
The prediction number is: -1
the path of test picture:pic/4.jpg
No checkpoint file found in ./model/
The prediction number is: -1
请求帮我看一下,可能是这四个文件都有问题,都互相关联,向前传播的过程没报错没结果,一段时间就退出了,向后传播和优化训练都会卡死
问题可能出在两个地方:
1. TensorFlow版本不兼容:
首先,你提到的这些代码是基于TensorFlow 1.x版本编写的,而你可能在使用TensorFlow 2.x版本。在TensorFlow 2.x版本中,一些API和函数名称发生了变化,所以代码可能无法正常运行。
解决方法:
将`import tensorflow.compat.v1 as tf`替换为`import tensorflow as tf`,然后将所有使用`tf.compat.v1`前缀的函数和类名改为使用`tf`即可。
2. 模型文件缺失或路径错误:
在`mnist_app.py`文件中,你尝试从模型文件中恢复模型权重以进行预测。可能是由于模型文件丢失或路径设置不正确,导致无法找到模型文件。
解决方法:
确保模型文件`.ckpt`位于指定的模型保存路径`MODEL_SAVE_PATH`下,并且命名为`mnist_model.ckpt`。另外,请确保`mnist_test.py`文件中的`TEST_INTERVAL_SECS`变量设置的时间足够长,以允许模型训练完成并生成模型文件。
针对以上问题的解决方法,你可以按照以下步骤进行:
步骤1:修改`mnist_forward.py`文件
将代码中的`get_weight`和`get_bias`函数的实现修改如下:
def get_weight(shape, regularizer):
w = tf.Variable(tf.random.truncated_normal(shape, stddev=0.1))
if regularizer != None:
tf.add_to_collection("losses", tf.keras.regularizers.l2(regularizer)(w))
return w
def get_bias(shape):
b = tf.Variable(tf.zeros(shape))
return b
步骤2:修改`mnist_test.py`文件
将代码中的`test`函数和`main`函数的实现修改如下:
def test(mnist):
x = tf.placeholder(tf.float32, shape=(None, mnist_forward.INPUT_NODE))
y_ = tf.placeholder(tf.float32, shape=(None, mnist_forward.OUTPUT_NODE))
y = mnist_forward.forward(x, None)
ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
ema_restore = ema.variables_to_restore()
saver = tf.train.Saver(ema_restore)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
while True:
latest_checkpoint = tf.train.latest_checkpoint(mnist_backward.MODEL_SAVE_PATH)
if latest_checkpoint:
with tf.compat.v1.Session() as sess:
saver.restore(sess, latest_checkpoint)
global_step = int(latest_checkpoint.split("-")[-1])
accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
print("After %d training step(s), test accuracy = %g" % (global_step, accuracy_score))
else:
print("No checkpoint file found")
return
def main():
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_test = x_test.reshape(-1, mnist_forward.INPUT_NODE) / 255.0
y_test = tf.keras.utils.to_categorical(y_test, mnist_forward.OUTPUT_NODE)
mnist.test = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(10000)
test(mnist)
if __name__ == "__main__":
main()
步骤3:修改`mnist_app.py`文件
将代码中的`restore_model`函数和`pre_pic`函数的实现修改如下:
def restore_model(testPicArr):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, shape=(None, mnist_forward.INPUT_NODE))
y = mnist_forward.forward(x, None)
preValue = tf.argmax(y, 1)
variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
with tf.compat.v1.Session(graph=g) as sess:
latest_checkpoint = tf.train.latest_checkpoint(mnist_backward.MODEL_SAVE_PATH)
if latest_checkpoint:
saver.restore(sess, latest_checkpoint)
preValue = sess.run(preValue, feed_dict={x: })
return preValue
else:
print("No checkpoint file found in", mnist_backward.MODEL_SAVE_PATH)
return -1
def pre_pic(picName):
img = Image.open(picName).convert('L')
reIm = img.resize((28, 28), Image.LANCZOS)
im_arr = np.array(reIm)
threshold = 50
im_arr = np.where(im_arr < threshold, 0, 255)
nm_arr = im_arr.reshape(1, -1).astype(np.float32)
img_ready = nm_arr / 255.0
return img_ready
def application():
testNum = int(input("input the number of test pictures:"))
for i in range(testNum):
testPic = input("the path of test picture:")
testPicArr = pre_pic(testPic)
preValue = restore_model(testPicArr)
print("The prediction number is:", preValue)
def main():
application()
if __name__ == "__main__":
main()
最后,确保所有的模型文件和图片路径都正确设置,然后尝试运行代码。希望这些修改能够解决你的问题!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]