|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
创建了四个独立的.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([INPUT_NODE, LAYER1_NODE], regularizer)
- b1 = get_bias([LAYER1_NODE])
- y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
- w2 = get_weight([LAYER1_NODE, OUTPUT_NODE], regularizer)
- b2 = get_bias([OUTPUT_NODE])
- 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, [None, INPUT_NODE]) # 使用修正后的导入
- y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE]) # 使用修正后的导入
- 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_step, ema_op]):
- 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([train_op, loss, global_step], 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[0], -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的弹窗口就会卡死然后你必须手动退出,如果你让它往下执行,它就会出现很多字符串,想下载器一样,时不时会卡死一段时间,然后接着执行,运行大概五分钟以后,就会彻底卡死,需要手动退出,下面给一个运行截图:
第三个文件: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, [None, mnist_forward.INPUT_NODE])
- y_ = tf.placeholder(tf.float32, [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:
- 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, [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.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[i][j] = 255 - im_arr[i][j]
- if im_arr[i][j] < threshold:
- im_arr[i][j] = 0
- else:
- im_arr[i][j] = 255
- nm_arr = im_arr.reshape([1, 784])
- 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
复制代码
请求帮我看一下,可能是这四个文件都有问题,都互相关联,向前传播的过程没报错没结果,一段时间就退出了,向后传播和优化训练都会卡死
|
|