Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Vložit
- čas přidán 4. 07. 2024
- In this video we are putting the theory into practice. We design a neural network which recognizes handwritten digits. For this we use Tensorflow!
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: www.neuralnine.com/books/
🐍 The Python Bible Book: www.neuralnine.com/books/
👕 Programming Merch: www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine.com/
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/NeuralNine
🖥️ My Coding Setup 🖥️
⌨️ Keyboard: hyperurl.co/neuralkeyboard
🖱️ Mouse: hyperurl.co/neuralmouse
🖥️ Monitor: hyperurl.co/neuralmonitor
🎙️ Microphone: hyperurl.co/neuralmicrophone
✏️ Drawing Tablet: hyperurl.co/neuraldraw
🎵 Outro Music From: www.bensound.com/ - Věda a technologie
Just watched the whole series and it was great !
You made a great series. Thank You.
Clearly explained. Great. Please add images and flow charts like diagrams , so that it will be useful to beginners like me.Thanks
hello! I really appreciate the effort and time that goes into this video and I am sure I can answer for most when I say we are grateful for such a tutorial. I have one question that is the data used for training was not our own dataset but how did it look like if we are preparing our own dataset. the labeling techniques, grouping classes, and such data organization steps.
Thank you man, it was amazing. Could you do it for a livestream camera. I mean by putting a picture in front of camera and showing the result in computer?
Fantastic series. The predictions were off, so I suspected my writing was different to the training images. If you use:
img = x_train
for i in range(0,30):
plt.imshow(img[i], cmap=plt.cm.binary)
plt.show()
to flick through the images then you can see how the numbers look like. For me numbers were too fat, if you can write with line width 2, it works better.
Just cant get the number 4 to get predicted correctly. 😁
thanks man , it is amazing video
Amazing video! Could you do also explain, how you would do this if you have a PDF document with 1 handwritten digit (not always at the very same place). How would you achieve for it to only read the handwritten digit and ignore the rest of the document?
do model.predict takes array of various images as input or can take a single image also? I am facing error when i pass single image.
Which images are you using? From urself or from a dataset? Created with paint? Nice Video btw.
this video help me with my study, i like your video dude! btw is this using CNN? what kind of model for this code??
I am getting this eror when reading the images.
TypeError: 'NoneType' object is not subscriptable
what to do?
Hey, great video! I am getting a very low accuracy and high loss ( 0.098, 2.3025851249694824 respectively). What could be the reason for this?
is it possible for you to make a video to recognize double digits or any no. of digits?
13:05
Bro writes his 5 like a serial killer 💀
Hi, im using google collab but it wont let me load the csv file of the images is there anyway to solve this?
Great video! Everything was well explained. I have one question though. I have included model.save('digits.model') in the code but somehow it is not creating the folder. Any ideas on how to fix this?
It doesnt create a folder. It just saves it in tf.models . you can import your model by "from tf.models import digits" and use again.
Hey great tutorial!
What should I do if I get this error: AttributeError: module 'keras.api._v2.keras' has no attribute 'Flatten'
Good morning
Thanks really for your video
I just have 2 or 3 questions i hope you answer me
Well i'm trying to make ocr for my company and we are scanning documents into tiff images so i need to read texts like batch_number and get the numbers after it
So how can i do that?
Why you choose 28*28?
Thanks
As a beginner in machine learning, loved the tutorial.
i just have a question, why normalize the data along the axis 1? seems kinda random, isn't the axis 1 the column of the images? like if a column has a lot of dark pixels, each pixels are gonna get grey, where as if it has only 1 grey pixel, it's gonna get darker.
Wouldn't it be better to divide every elements of the x_train tensor by the biggest element in the tensor (ie 255)?
if i want traine the models with leters , what do you recomended?
Can i use for image which greater than 28x28?
Can we apply this to text images. Can the whole text be extracted?
Hi sir when I excute the code ihave problem that error name sequential is not defined why?Thanks advance
can we do this in jupyter notebook ????? how can we save the pictures ????
mine is getting it wrong quite a bit (like its right only 20% of the time) when I check it but when it is training it says that it is 97.5% accurate
Just wondering how to install cv on a mac... Ill figure it out hopefully
Hey...How to use dedicated gpu for tensorflow?
Anyone has any advice in how to improve the results by using the same type of ANN but maybe different hyperparameters?
First of all, thank you for this nice tutorial !
But i do have a question, I followed you along but the time I trained the neural network it just hat 1875 images instead of 60000. Do you know what I did wrong ?
maybe you sliced the data set too much in the beginning
@@NeuralNine I just double checked my code and it's exactly the one you wrote, just with a bunch of comments to remember what every line of code has to do. And I checked the length of the x_train (60000) and x_test(10000). do you know any other possible problem ?
@@NeuralNine I get this output : Epoch 1/3
1875/1875 [==============================] - 1s 737us/step - loss: 0.2605 - accuracy: 0.9242
Epoch 2/3
1875/1875 [==============================] - 1s 743us/step - loss: 0.1072 - accuracy: 0.9667
Epoch 3/3
1875/1875 [==============================] - 1s 758us/step - loss: 0.0732 - accuracy: 0.9763
313/313 [==============================] - 0s 415us/step - loss: 0.1064 - accuracy: 0.9671
0.10637900233268738
0.9671000242233276
INFO:tensorflow:Assets written to: digits.model\assets
@@iloveme9664 yeah, i'm having the same, did u figure out what it was ?
ty
@@iloveme9664 maybe you dont need so much data anymore.
even with 1875 we get more than 97% accuracy
ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 1024 but received input with shape (None, 16128) I am getting this error...how can I solve it
same
what to do if the data is already flattened
What IDE is used here?
I copied your code but i cant install cv2 module in my programm do you know how can i fix it ... I have 3.6.5 python and if i go to 3.7 or 3.8 i cant install both cv2 and tensorflow... Those version problems are pissing me off!! I really need your help!!!Thank you!!!!
try this for python 3
pip3 install opencv-python
this is a great video, however, I am getting the following error.
"ValueError: Data cardinality is ambiguous:
x sizes: 60000
y sizes: 10000
Make sure all arrays contain the same number of samples."
This happens after it runs all 3 epochs and gets to a 97.5% accuracy. It points to "accuracy, loss = model.evaluate(x_test, y_test)"
You've probably figured it out by now, but if not it's because you have made a mistake in the normalization of the data. Make sure the lines 9 and 10 are correct.
Thank you man this has really helped but i'm getting an error here
prediction = model.predict(img)
can you help me out please. many thanks
I cannot help you if you don't post the error you get
May be you are getting error because your image shape.
Deep learning model accept the the image shape which they are trained on.
@@deepakraj008 Hello can you tell what is the shape or size of the model? or how do i make my canvas as such? Thank you
Which neural network algorithm is used here ?
He used the Adam optimizer
Thank you for this. I do have one question which is i wanted to know how to can load my dataset which i already have in my laptop. ( how can i load it via folder )
AH YES İ NEED THİS TOO
@@emren2415 .
Hi you can use roboflow which will convert your folder into a link which you can download and add the link to your coe
*code
not coming giving error img = np.invert(np.array([img]))
TypeError: bad operand type for unary ~: 'NoneType'
Hi do you have ppt for this project?? Its urgent🙏🙏🙏🙏🙏🙏?
Hello, I am getting this error:
img = cv.imread(F'{i}.png')[:,:,0]
TypeError: 'NoneType' object is not subscriptable
I have the same code as shown in the video. Can someone please help me?
what numbers/filenames did u use? -> 1,2,3,4. For example 1,3,6,9 would not be correct, since there is no 2.png file -> NoneType
I'm new to python and coding in general, would this also work with general handwriting recognition?
no
you would have to make some changes but it should if you increase the outputs to 26 (assuming ur using just the enlgish alphabet. you could use 36 for englihs alphabet and digits, etc) and you could label it so that a=1 b=1 and so on and it'll output a number and then you can just convert that number back to a b c etc
I got a valueError on model.fit(x_train,y_train,epochs=3)
how to fix?
I tried : "pip install tensorflow==2.0.0"
Then it worked!
While giving it a scanned image how can we convert it to 28*28 pixels. My image is 500*500
use photoshop or paint....
No need to use paint or photoshop.
Use cv2 to resize the image.
Cv2.resize() check the documentation.
Why the process just print out 3/3 with 1875/1875 instead of 60000?
same question arise
Because the program is doing it in batches of 32
I tried this but when i put in my own 28x28 digital drawn images, it got every single prediction wrong.
1~>2
2~>5
3~>0
4~>2
5~>3
6~>2
7~>3
after the training and before the prediction it said loss was 0.099 and accuracy was 0.9674
i even doubled the epochs to 6 but it still didn't work.
I am thoroughly confused, any help would be greatly appreciated!
Perhaps the color or line thickness is different from the training data?
exactly!did you solve it?
How do you "correct" the mistakes?
what do you mean exactly? :)
NeuralNine that I put a Numbers and it’s wrong. How can I tell the AI that this is wrong and it’s another Number? Should I train again the model with also this photo or there’s another way more instant?
I receive an error can you solve this ?
File "C:/Users/MUHTANCI/Desktop/SayıOkuma/SayıOkuma.py", line 32, in
img = cv2.imread(f'{x}.jpg')[:, :, 0]
TypeError: 'NoneType' object is not subscriptable
I am getting the same error. Have u find any solution?
@@gauravdesai6676 I couldn't find any solution so I didn't continue with this project.
same error
@NeuralNine
Did anyone found some solution for this error??
I am getting an error of failed to load native TensorFlow runtime
do you maybe have a 32 bit processor?
@@NeuralNine it's 64 bit one
Hello, thank you for nice video! Very clear BTW!
Well, my system gives a loss coef : 0.0960269495844841 and an accuracy coef : 0.9697999954223633 which are pretty good even if a little worse than yours but not by much. Here is the thing : I made the files with paint in 28x28 px and the results were not that good. I made 12 files and out of the twelve, it gave me 3 good answers. Seems quite random. Strangely, at some point, a 3 was considered as a 4, a 5 as a 7, a 2 as a 7 and an 8 as a 4 and a 1 as an 8!!! Even if I took the mnist dataset as you explained..
What would be the issue according to your expert point of view?
probably your images look quite different from those in the dataset. Maybe you need to invert the colors or use another tool (brush, pencil) in Paint.
@@NeuralNine Thanks for your fast answer. I tried something else : first of all, I modified the width of my pencil from 1 to 2px and it gave better results. The fact is that I tried in a first place with the digits 3 and 8 directly written with text font calibri size 12 and the results were wrong. Then I changed the size of the characters from 12 to 24 : the 3 was considered as a 3 but the 8 was considered as a 3 as well. It was close to the left border of the picture and I centered it and then the detection was right.
@@NeuralNine By the way, you import cv2, numpy and matplotlib (pyplot) but the compiler in Spyder tells that these imports are not used further. Was I missing something? And cv2 is the same as opencv (I ask because the library was not found in my environment and neither on the internet at a first glance)?
can this model read multiple of numbers in one picture????
no
yes but you have to make your own dataset and resize the image
I am receiving the following error can any one help me with this
Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape (None, 2352)
I am recieving the same error. Did you find the solution?
@@rockit14 solved? because i got the same tho
@@FinFlare44 Never got a solution
harika
Dear NeuralNine
I Have been following along with all your tutorial videos, they are excellent! THANK YOU!
I am have trouble with is one though. I have copied your code word for word but I am getting errors with tensorflow. I have installed the tensorflow module while my Anaconda environment is activated, the way you said.
Can you please help? I have pasted my error below.
C:\Users\alexs\anaconda3\envs\stokes\python.exe C:/Users/alexs/PycharmProjects/pythonProject/main.py
2020-08-20 14:42:45.693778: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-08-20 14:42:45.694028: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-08-20 14:42:48.505726: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-08-20 14:42:49.241714: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:02:00.0 name: GeForce GTX 1050 computeCapability: 6.1
coreClock: 1.493GHz coreCount: 5 deviceMemorySize: 2.00GiB deviceMemoryBandwidth: 104.43GiB/s
2020-08-20 14:42:49.242972: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-08-20 14:42:49.244146: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-08-20 14:42:49.245140: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2020-08-20 14:42:49.246222: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2020-08-20 14:42:49.247201: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
2020-08-20 14:42:49.248186: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-08-20 14:42:49.249153: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2020-08-20 14:42:49.249477: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1753] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-08-20 14:42:49.250467: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-08-20 14:42:49.546377: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x292f2b9d760 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-20 14:42:49.546651: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-08-20 14:42:49.565431: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-08-20 14:42:49.565632: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]
Epoch 1/3
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2660 - accuracy: 0.9217
Epoch 2/3
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1062 - accuracy: 0.9669
Epoch 3/3
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0724 - accuracy: 0.9777
313/313 [==============================] - 0s 701us/step - loss: 0.0895 - accuracy: 0.9723
0.9722999930381775
0.08949561417102814
WARNING:tensorflow:From C:\Users\alexs\anaconda3\envs\stokes\lib\site-packages\tensorflow\python\training\tracking\tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
2020-08-20 14:42:57.027305: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
WARNING:tensorflow:From C:\Users\alexs\anaconda3\envs\stokes\lib\site-packages\tensorflow\python\training\tracking\tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
It seems like you don't have a GPU that is supported by Tensorflow
@@NeuralNine OK thank you. Is their any thing I can do. Without replacing my graphics card?
Thank you very much Sir!
I'm a student and i work for my final year project Detecting and Recognizing mathematical expressions from image.
can you help me!
Hi, I am doing a similar project. Were you able to succeed with the project?
TypeError Traceback (most recent call last)
in ()
27
28 for x in range (1,6):
---> 29 img = cv.imread(f'{x}.png')[:,:,0]
30 img = np.invert(np.array([img]))
31 plt.imshow(img[0], cmap=plt.cm.binary)
TypeError: 'NoneType' object is not subscriptable
Any ideea on how to fix this?
Great tutorial, thank you!!
If the file path to where your python program is has any special characters it won't work, I just moved mine to desktop and it worked
@@user-bm2ml6up1b can you please tell me more clearly about this
@@user-bm2ml6up1bim facing same issue
How can I install cv2. It is showing error while executing the program showing that no modules exist with the name
Go to cmd and type:
pip install opencv-python
Use search engine nex time, please. It'll be a lot faster :)
12:05
plzz say something
bruh can you help me with my problem
i tried this with tensorflow 1.15.0 , but training is not taking place , can you help me to solve this problem
@Py Coding Pi 3.6
dude im not getting 60000 results im getting 1875
it's 1875 batches, 1875*32 = 60'000. So you are allright I guess
@@peschebichsu How do I change the batch size?
Your code doesn't work, aside form that it's a great tutorial