blog icon indicating copy to clipboard operation
blog copied to clipboard

TensorFlow 2.0 (五) - mnist手写数字识别(CNN卷积神经网络) | 极客兔兔

Open geektutu opened this issue 5 years ago • 31 comments

https://geektutu.com/post/tensorflow2-mnist-cnn.html

tensorflow入门系列文章,mnist手写数字识别(五,CNN卷积神经网络)。

geektutu avatar Jun 15 '19 16:06 geektutu

请问为什么我训练后直接从数据集读取数据时测试的成功率是99%,而用make_data_set生成图片后再测试时成功率只有83%

ICE-GB avatar Jun 21 '19 10:06 ICE-GB

@ICE-GB

from make_npy_data_set import DataSet

dataset = DataSet()
test_x, test_y = dataset.get_test_set()

# 784 reshaple 为 28 x 28 x 1
test_x = test_x.reshape((-1, 28, 28, 1)) 
# 独热编码转为 label
test_y = np.array([np.argmax(arr) for arr in test_y])

self.cnn.model.evaluate(test_x, test_y)

这是我的验证代码,结果是这样的,准确率99.5%

1037/1037 [=======] - loss: 0.0097 - accuracy: 0.9952

不过,我生成了约5000张图片,测试集有1037张图片。

我上传到Github的代码默认生成200张,测试集只有20*0.2=40张,图片太少,容易有误差。你多生成一点图片试一试?

geektutu avatar Jun 21 '19 15:06 geektutu

@ICE-GB

你描述的问题,我已经回复你邮件了。读取出来的图片,值范围是[0, 255],训练集的值范围是[0, 1],所以你需要使用PIL读取后,除以 255,如下所示。

img = Image.open(image_path).convert('L')
flatten_img = np.reshape(img, (28, 28, 1)) / 255

geektutu avatar Jun 23 '19 13:06 geektutu

感谢大佬解答我的问题(。・ω・。)ノ♡

ICE-GB avatar Jun 23 '19 16:06 ICE-GB

您好,我试运行了一下您的代码,发现当data_set_tf2里已经有.npz文件时,仍然会自动下载,不知道应该怎样修改呢?

ReiZhao avatar Jul 26 '19 12:07 ReiZhao

学习完了前五篇,作者代码简洁,思路清晰,让我收获颇多,在此深表感谢。

ringfa11 avatar May 23 '20 14:05 ringfa11

请问为什么做预测时,输入图片像素不做归一化?就是class Predict(object)第24行: x = np.array([1 - flatten_img])

为何不是 x = np.array([1 - flatten_img]) / 255.0

训练集做了归一化,预测时的输入数据不做也可以得到正确预测,这是为什么呢? 这两种操作有什么区别呢?

LiZeng001 avatar May 25 '20 06:05 LiZeng001

请问为什么做预测时,输入图片像素不做归一化?就是class Predict(object)第24行: x = np.array([1 - flatten_img])

为何不是 x = np.array([1 - flatten_img]) / 255.0

训练集做了归一化,预测时的输入数据不做也可以得到正确预测,这是为什么呢? 这两种操作有什么区别呢?

似乎是作者写错了,实际上作者的predict预测准确率并不高,改成x = np.array([1 - flatten_img]) / 255.0之后就对了。我猜测训练的时候不归一化,预测的时候也许也不用归一化,但是并没有实验,如果你测试了可以告诉我一下结果。@LiZeng001

ringfa11 avatar May 25 '20 06:05 ringfa11

@Linf-p @LiZeng001 抱歉,这里应该是写错了,预测和训练时的数据是需要匹配上的。训练时 /255,预测时也应该 /255。

geektutu avatar May 25 '20 07:05 geektutu

@Linf-p @geektutu 谢谢回复。 进一步做了些测试,这里是否归一化对最终结果(argmax)影响不大,但是明显影响各个分类的概率分布。尝试了 (/2, 4, 9, 16, 127, 200, 255)等归一化系数,影响softmax的分布。如:未归一化的预测结果为6,对应softmax向量第6个值的大小落在[0.6, 1.0]区间内。不做归一化时,一般为1.0, 归一化系数越大(→255.0),则softmax对应值越小,但是最终结果都是6.

=============================================================== 目前觉得不明白的是这一步: x = np.array([1 - flatten_img])

1-flatten_img是什么意思呢?

我用实际图片进行了测试,和用np生成的矩阵结果相差很大,具体如下: 图片大小(8,8,1),采用“img = Image.open(image_path).convert('L')”读入,并转为ndarray后为 [[249 249 249 249 248 248 249 241] [254 255 254 244 103 143 254 245] [254 254 237 78 50 86 220 245] [254 253 116 146 237 162 153 245] [254 214 113 254 253 144 170 245] [254 200 141 235 150 147 249 245] [254 223 53 68 181 251 255 245] [254 254 245 248 254 255 255 245]] 经过x = np.array([1 - flatten_img])后,值为(这里把(1, 8, 8, 1)中的(8, 8)取出): [[ 8 8 8 8 9 9 8 16] [ 3 2 3 13 154 114 3 12] [ 3 3 20 179 207 171 37 12] [ 3 4 141 111 20 95 104 12] [ 3 43 144 3 4 113 87 12] [ 3 57 116 22 107 110 8 12] [ 3 34 204 189 76 6 2 12] [ 3 3 12 9 3 2 2 12]] -----------------(A) 显然,对应结果是(257 - flatten_img),不是1 - flatten_img

如果我直接用np生成同样的数据,则1 - flatten_img的结果为 [[[-248 -248 -248 -248 -247 -247 -248 -240] [-253 -254 -253 -243 -102 -142 -253 -244] [-253 -253 -236 -77 -49 -85 -219 -244] [-253 -252 -115 -145 -236 -161 -152 -244] [-253 -213 -112 -253 -252 -143 -169 -244] [-253 -199 -140 -234 -149 -146 -248 -244] [-253 -222 -52 -67 -180 -250 -254 -244] [-253 -253 -244 -247 -253 -254 -254 -244]]] -----------------(B)

请问为什么会得到结果(A)呢?x = np.array([1 - flatten_img])这一步的作用是什么?

LiZeng001 avatar May 25 '20 08:05 LiZeng001

x = np.array([1 - flatten_img])建议改为 x = np.array([255 - flatten_img]) 灰度反转

LiZeng001 avatar May 25 '20 11:05 LiZeng001

为什么我自己写的数字,拍下来改成28*28,用来验证很容易错啊?还有输出的one_hot和需要为1吗?厦门是我的输出。 ''' ../test_images/qrh_0.png [0.10820425 0.06075585 0.05405012 0.08265415 0.0839475 0.07603313 0.11496077 0.10453177 0.09218799 0.2226745 ] -> Predict digit 9 ../test_images/qrh_1.png [0.10367583 0.17859878 0.06843033 0.07650375 0.13739616 0.06804659 0.07628669 0.12138759 0.10303307 0.06664125] -> Predict digit 1 ../test_images/qrh_3.png [0.0340519 0.11628386 0.07826916 0.13755722 0.06508617 0.0535523 0.02320397 0.25788862 0.1083305 0.12577625] -> Predict digit 7 ../test_images/qrh_4.png [0.0467831 0.08102739 0.03599969 0.05296399 0.22805345 0.12213521 0.05424373 0.15700966 0.09909225 0.12269157] -> Predict digit 4 ../test_images/qrh_7.png [0.07227767 0.1610153 0.1259185 0.10038598 0.07952032 0.03625101 0.03909528 0.22072537 0.09698366 0.06782683] -> Predict digit 7 ../test_images/qrh_9.png [0.06446036 0.16687377 0.08327802 0.09353368 0.08186979 0.05212609 0.03528493 0.19081159 0.13146625 0.10029551] -> Predict digit 7

Process finished with exit code 0 '''

FrontierQ avatar Sep 25 '20 04:09 FrontierQ

博主你好,predict.py中 flatten_img = np.reshape(img, (28, 28, 1))中flatten_img 应该不是被打平了吧,这样命名容易误导啊

FrontierQ avatar Sep 28 '20 11:09 FrontierQ

感谢 @LiZeng001 @FrontierQ ,已经修正为

img = np.reshape(img, (28, 28, 1)) / 255.
x = np.array([1 - img])

geektutu avatar Oct 10 '20 13:10 geektutu

Traceback (most recent call last): File "", line 1, in File "D:\Apply\PyCharm\PyCharm 2020.2.2\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "D:\Apply\PyCharm\PyCharm 2020.2.2\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:/File/pythonworkplace/homework/new_model_predict.py", line 37, in app = Predict() File "D:/File/pythonworkplace/homework/new_model_predict.py", line 18, in init self.cnn.model.load_weights(latest) File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 2172, in load_weights if _is_hdf5_filepath(filepath): File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 2785, in _is_hdf5_filepath return (filepath.endswith('.h5') or filepath.endswith('.keras') or AttributeError: 'NoneType' object has no attribute 'endswith'

请问一下为什么会出现这样的问题呀?谢谢

plateau1 avatar Oct 18 '20 01:10 plateau1

报错信息是模型文件没有生成,应该是还训练过吧?需要先训练再预测,先执行train.py,再执行 predict.py。

geektutu avatar Oct 18 '20 02:10 geektutu

您好!首先非常感觉你对我的回复。因为最近在做通过对人脸的照片进行识别来实现性别预测的功能,因为我是刚刚接触这个,所以我想询问一下,就是在训练的时候accuracy 都很高最少都是0.8几,但是在用训练好的模型进行预测的时候,只有0.6几,我想问一下 ,就是有什么办法可以改进吗?真的非常感谢!

报错信息是模型文件没有生成,应该是还训练过吧?需要先训练再预测,先执行train.py,再执行 predict.py。

plateau1 avatar Oct 18 '20 07:10 plateau1

@plateau1 这个模型过于简单了,训练人脸估计是不够的,效果差是正常的,你需要修改网络结构,试着添加几层网络,或者加入池化等方式多试一试。

geektutu avatar Oct 18 '20 10:10 geektutu

博主你好!请问,这个卷积网络的模型定义有没有原型或者参考?

Jaccelerator avatar Nov 08 '20 08:11 Jaccelerator

@Jaccelerator 用来识别 mnist 的模型相当于是卷积网络中的 hello world,这个模型无论怎么定义,识别率都是很高的。如果想了解更多的话,可以参考两本书,一本是国内周志华老师的西瓜书,另一本是被奉为深度学习的圣经的国外的花书,后面这本难度比较大。

geektutu avatar Nov 08 '20 09:11 geektutu

@geektutu 报错信息是模型文件没有生成,应该是还训练过吧?需要先训练再预测,先执行train.py,再执行 predict.py。

lz为啥运行train.py后并没有生成ckpt文件夹, WARNING:tensorflow:Callbacks method on_train_batch_end is slow compared to the batch time (batch time: 0.0100s vs on_train_batch_end time: 0.0661s). Check your callbacks. 报这样的警告

xiongli520 avatar Nov 23 '20 06:11 xiongli520

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 221: invalid start byte

dongyangyang621719 avatar Feb 02 '21 08:02 dongyangyang621719

请问,怎么修改生成图片数目啊

wmrenr avatar Apr 08 '21 03:04 wmrenr

请问最后预测的时候,测试的图片从哪来的啊

MR-chenliang avatar Sep 06 '21 13:09 MR-chenliang

为什么我用你的模型预测mnist数据集中的图片都没预测对呢

MR-chenliang avatar Sep 07 '21 02:09 MR-chenliang

我用tensorflow2.2为啥跑不了predict.py文件呢 加载不到权重

Traceback (most recent call last):
  File "C:/Users/312/Desktop/pythonProject/filed_depth/CNN/predict.py", line 27, in <module>
    app = Predict()
  File "C:/Users/312/Desktop/pythonProject/filed_depth/CNN/predict.py", line 12, in __init__
    self.cnn.model.load_weights(latest)
  File "C:\Users\312\Desktop\pythonProject\filed_depth\CNN\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 250, in load_weights
    return super(Model, self).load_weights(filepath, by_name, skip_mismatch)
  File "C:\Users\312\Desktop\pythonProject\filed_depth\CNN\venv\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1227, in load_weights
    if _is_hdf5_filepath(filepath):
  File "C:\Users\312\Desktop\pythonProject\filed_depth\CNN\venv\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1616, in _is_hdf5_filepath
    return (filepath.endswith('.h5') or filepath.endswith('.keras') or
AttributeError: 'NoneType' object has no attribute 'endswith'

wangshuo2020 avatar Dec 03 '21 13:12 wangshuo2020

我先训练了啊 而且拿到了权重文件夹

wangshuo2020 avatar Dec 03 '21 13:12 wangshuo2020

@plateau1 Traceback (most recent call last): File "", line 1, in File "D:\Apply\PyCharm\PyCharm 2020.2.2\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "D:\Apply\PyCharm\PyCharm 2020.2.2\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:/File/pythonworkplace/homework/new_model_predict.py", line 37, in app = Predict() File "D:/File/pythonworkplace/homework/new_model_predict.py", line 18, in init self.cnn.model.load_weights(latest) File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 2172, in load_weights if _is_hdf5_filepath(filepath): File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 2785, in _is_hdf5_filepath return (filepath.endswith('.h5') or filepath.endswith('.keras') or AttributeError: 'NoneType' object has no attribute 'endswith'

请问一下为什么会出现这样的问题呀?谢谢

能问下你是怎么解决的吗

wangshuo2020 avatar Dec 03 '21 13:12 wangshuo2020

你看一下你的NoneType是怎么来的,

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2021年12月3日(星期五) 晚上9:17 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [geektutu/blog] TensorFlow 2.0 (五) - mnist手写数字识别(CNN卷积神经网络) | 极客兔兔 (#9)

@plateau1 Traceback (most recent call last): File "", line 1, in File "D:\Apply\PyCharm\PyCharm 2020.2.2\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "D:\Apply\PyCharm\PyCharm 2020.2.2\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:/File/pythonworkplace/homework/new_model_predict.py", line 37, in app = Predict() File "D:/File/pythonworkplace/homework/new_model_predict.py", line 18, in init self.cnn.model.load_weights(latest) File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 2172, in load_weights if _is_hdf5_filepath(filepath): File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\training.py", line 2785, in _is_hdf5_filepath return (filepath.endswith('.h5') or filepath.endswith('.keras') or AttributeError: 'NoneType' object has no attribute 'endswith'

请问一下为什么会出现这样的问题呀?谢谢

能问下你是怎么解决的吗

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

wmrenr avatar Dec 03 '21 13:12 wmrenr

predict的最后测试图像014是需要自己输入吗

lucy285 avatar Dec 28 '21 14:12 lucy285