JanPalasek / ulozto-captcha-breaker

Deep learning model using Tensorflow that breaks ulozto captcha codes.
MIT License
81 stars 11 forks source link

Something bad when creating .tflite file #17

Closed jdvorak23 closed 11 months ago

jdvorak23 commented 1 year ago

Hi, I spend whole weekend trying to train simple 0-9 captcha solver, but I think I got stuck on the last step. I already passed test with high success, but final .tflite model does not work properly. Can you please help a little? Steps to reproduce:

git clone git@github.com:JanPalasek/ulozto-captcha-breaker.git
cd ulozto-captcha-breaker
python -m venv "venv"
source venv/bin/activate
python -m pip install --upgrade pip
python -m pip install --upgrade wheel setuptools pip-tools
python -m piptools sync
python -m pip install -e .

python bin/simple_captcha_generate.py --height=70 --width=175 --available_chars="0123456789" --captcha_length=4 --dataset_size=10000
python bin/captcha_annotate.py --val_split=0.1 --test_split=0.1

python bin/train.py --available_chars="0123456789" --captcha_length=4

In this moment, I stopped script after a while, 7th epoch had val_all_correct_acc: 0.9453, so for testing purposes enough.

Then I ran test.py:

python bin/test.py --available_chars="0123456789" --captcha_length=4 --weights_file=/home/manas/Projects/2.Other/ulozto-captcha-breaker/out/logs/train.py-2023-09-10_163320-ac=0123456789,bs=32,cl=4,e=1500,fl=0,l=1e-05,od=out,pm=None,rl=False,smp=None,s=42,tih=None,tiw=None,wf=None/cp-07.h5

with result :

32/32 [==============================] - 2s 40ms/step
Test acc: 0.92

And .csv file with results, 92% succesfully as written. After that I have model folder in out directory. I did:

python bin/create_tflite.py --pretrained_model=out/model

with output to terminal (I dont know if it is useful):

2023-09-11 18:07:01.736468: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:362] Ignored output_format.
2023-09-11 18:07:01.736497: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:365] Ignored drop_control_dependency.
2023-09-11 18:07:01.737129: I tensorflow/cc/saved_model/reader.cc:45] Reading SavedModel from: out/model
2023-09-11 18:07:01.753180: I tensorflow/cc/saved_model/reader.cc:89] Reading meta graph with tags { serve }
2023-09-11 18:07:01.753227: I tensorflow/cc/saved_model/reader.cc:130] Reading SavedModel debug info (if present) from: out/model
2023-09-11 18:07:01.785731: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:354] MLIR V1 optimization pass is not enabled
2023-09-11 18:07:01.796363: I tensorflow/cc/saved_model/loader.cc:229] Restoring SavedModel bundle.
2023-09-11 18:07:01.967260: I tensorflow/cc/saved_model/loader.cc:213] Running initialization op on SavedModel bundle at path: out/model
2023-09-11 18:07:02.019981: I tensorflow/cc/saved_model/loader.cc:305] SavedModel load for tags { serve }; Status: success: OK. Took 282855 microseconds.
2023-09-11 18:07:02.140917: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.

After this, model.tflite was created in out directory. I copied randomly a few images from data directory to base directory, renamed them test1.jpg - test5.jpg (which are: 0336, 3185, 7881, 8388, 9977) and then tried that .tflite model:

python bin/predict.py --model_path=out/model.tflite --available_chars="0123456789" --image_path=test1.png
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Decoded label is the following:
1117

python bin/predict.py --model_path=out/model.tflite --available_chars="0123456789" --image_path=test2.png
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Decoded label is the following:
1197

python bin/predict.py --model_path=out/model.tflite --available_chars="0123456789" --image_path=test3.png
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Decoded label is the following:
1147

python bin/predict.py --model_path=out/model.tflite --available_chars="0123456789" --image_path=test4.png
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Decoded label is the following:
1147

python bin/predict.py --model_path=out/model.tflite --available_chars="0123456789" --image_path=test5.png
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Decoded label is the following:
1147

So this seems I do something wrong when I try to create .tflite file, can you please little help?

JanPalasek commented 11 months ago

@jdvorak23 Sorry for the long response :-(. I found the bug: when using predict, image was loaded in a different way than when using test and train. I'll post a fix soon.