Jdka1 / KanjiNet

✍️🇯🇵 Predicting handwritten Japanese characters with OCR 🈳
2 stars 0 forks source link

How to train the model and run it correctly ? #1

Open tranquoc1301 opened 2 weeks ago

tranquoc1301 commented 2 weeks ago

i tried to train the model again in machine_learning.ipynb and run vision.py but it still couldn't recognize kanji characters. One more thing, how and where to use the python code block you noted in README.md. Can you help me to solve above problems. Thanks you so much !

Jdka1 commented 1 week ago

What was your training process? Usually it's best to start with the pretrained model then fine-tune from there. Make sure you are loading the right weights though, it seems the name of the file and the name in the code are different. Make sure the learning rate is not too high as well.

As for using the code block in README.md, if you clone the repository then make a python file inside of it, you will be able to run the code.

If you are on Mac/Linux do: git clone https://github.com/Jdka1/KanjiNet.git cd KanjiNet touch main.py

then open main.py and paste in the code block.

Lmk if it works!

tranquoc1301 commented 1 week ago

i run the code block successfully but the 'len_kanji_dict' argument in KanjiNet() is fixed with 10, so can i change that value ?. When i ran vision.py for the first time, it told that no such file or directory: 'Machine_Learning/model.pth', so i changed num_samples = 100 and retrained the model. Did i miss any step before retraining the model ?. One more thing, i didn't find Network from architecture.py imported in vision.py, did you mean KanjiNet ?. Thank for your support !

Jdka1 commented 1 week ago

Ah yes, num_samples = 10 currently in the vision.py. If you want to add more kanji characters, just increase that number and it will take the first n in the list, but then you will have to retrain the model and input your custom trained weights file because the model architecture changes based on that number.

If you want to retrain the model, you'll have to run through the machine_learning.ipynb notebook. Also, if you want to identify all characters then set num_samples = len(imgs) when retraining.

As for the no such file or directory error, instead of model.pth it should be weights.pth

Also, yes, that's correct. It should be KanjiNet instead of Network—thanks for poiting that out.

tranquoc1301 commented 1 week ago

In machine_learning.ipynb , at cell 15, 16 [15] torch.save(model.state_dict(), 'weights.pth')

[16] model = KanjiNet(len_kanji_dict=len(kanji_dict)) model.load_state_dict(torch.load('weights.pth')) model.eval()

Do i have to change to 'weights.pth' ? In your repo, what is file 'data.zip' used for ? Thanks for your support !

Jdka1 commented 1 week ago

data.zip is the training data used for the model—ie all the pdfs iirc also, yes, it should be weights.pth

tranquoc1301 commented 6 days ago

Can i use 'data' folder containing png files only to train the model ?. I tried training the model with the 'data' folder and tested it in the last cell of machine_learning.ipynb. The result of test_img and the prediction were the same, but it printed 'Wrong'. After training the model, I ran vision.py, but it couldn't recognize the characters. Sometimes, it misrecognized even though I didn't show any Kanji in front of the camera. So, I tried cloning a new repo and running your pre-trained model (weights.pth) with vision.py, but it still had the same issue. Thanks for your support !