EasyOCR
EasyOCR copied to clipboard
How to use custom CRAFT model with EasyOCR
I trained the CRAFT detection model using a custom dataset and got the new weights as a .pth file. Now, how can I use this model with EasyOCR?
I tried to pass the model directory to the model_storage_directory
parameter but it doesn't seem to work.
import easyocr
reader = easyocr.Reader(['en'],
download_enabled=False,
model_storage_directory='custom_example/1/model')
when I run the above script I get: FileNotFoundError: Missing custom_example/1/model/craft_mlt_25k.pth and downloads disabled
Go to config.py
in easyocr folder, you have to change filename and md5sum of the detector model there.
@rkcosmos I got RuntimeError: Error(s) in loading state_dict for CRAFT: Missing key(s) in state_dict: "basenet.slice1.0.weight", "basenet.slice1.0.bias", "basenet.slice1.1.weight", ..
@gmuffiness
may be when train use the vgg backbone not the craft
- update solve is by adding
-
net.load_state_dict(copyStateDict(torch.load(trained_model, map_location=device)['cuda']))
- in this line
@Shidhani Can you tell me how to train your own CRAFT model ? I need lightweight CRAFT model.
Go to
config.py
in easyocr folder, you have to change filename and md5sum of the detector model there.
How about url?
'craft' : {
'filename': 'craft_mlt_25k.pth',
'url': 'C:/Users/res/source/sheet_recognition/test_code/text/EasyOCR-1.6.2/trainer/craft/exp/custom_data_train/CRAFT_clr_amp_200.pth',
'md5sum': '50D96F50EDD083B92CC5CCFE9E9C0A5B0D476D8BCF4F4194DC75CC71DC6EEF44'
},
@rkcosmos I got
RuntimeError: Error(s) in loading state_dict for CRAFT: Missing key(s) in state_dict: "basenet.slice1.0.weight", "basenet.slice1.0.bias", "basenet.slice1.1.weight", ..
When you use train.py to train the CRAFT model, the .pth file will be dict_state of 3 item { "iter": train_step, "craft": craft.state_dict(), "optimizer": optimizer.state_dict(), } You only need craft.state_dict(), so
- load .pth into torch. ex
save_pth= torch.load(''path_to_pth_file)
- extract only craft. ex
model = save_pth["craft"]
- save the model into .pth. ex
torch.save(model , "craft_model_save_location")
@Shidhani Can you tell me how to train your own CRAFT model ? I need lightweight CRAFT model.
I also need guide on how to annotate images on specific format for craft
Go to
config.py
in easyocr folder, you have to change filename and md5sum of the detector model there.
Just in case someone does not feel like editing the source code of the environment, you can use this:
# for craft:
import easyocr
from easyocr.detection import get_detector, get_textbox
# or for dbnet
# from .detection_db import get_detector, get_textbox
reader easyocr.Reader(
lang_list=["fr"],
detector=False,
)
reader.get_detector, reader.get_textbox = get_detector, get_textbox
reader.detector = reader.initDetector('path_to_the_filtered_model')
path_to_the_filtered_model
is for the model obtained using @kidserge-yong method (thank you btw!).