Error while testing model using referenceModel
Closed this issue · 2 comments
Mikey-889 commented
import cv2
import typing
import numpy as np
from mltu.inferenceModel import OnnxInferenceModel
from mltu.utils.text_utils import ctc_decoder, get_cer, get_wer
from mltu.transformers import ImageResizer
class ImageToWordModel(OnnxInferenceModel):
def init(self, char_list: typing.Union[str, list], *args, **kwargs):
super().init(*args, **kwargs)
self.char_list = char_list
def predict(self, image: np.ndarray):
image = ImageResizer.resize_maintaining_aspect_ratio(image, *self.input_shape[:2][::-1])
image_pred = np.expand_dims(image, axis=0).astype(np.float32)
preds = self.model.run(None, {self.input_name: image_pred})[0]
text = ctc_decoder(preds, self.char_list)[0]
return text
if name == "main":
import pandas as pd
from tqdm import tqdm
from mltu.configs import BaseModelConfigs
configs = BaseModelConfigs.load("Models/04_sentence_recognition/202301131202/configs.yaml")
model = ImageToWordModel(model_path=configs.model_path, char_list=configs.vocab)
df = pd.read_csv("Models/04_sentence_recognition/202301131202/val.csv").values.tolist()
accum_cer, accum_wer = [], []
for image_path, label in tqdm(df):
image = cv2.imread(image_path)
prediction_text = model.predict(image)
cer = get_cer(prediction_text, label)
wer = get_wer(prediction_text, label)
print("Image: ", image_path)
print("Label:", label)
print("Prediction: ", prediction_text)
print(f"CER: {cer}; WER: {wer}")
accum_cer.append(cer)
accum_wer.append(wer)
cv2.imshow(prediction_text, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(f"Average CER: {np.average(accum_cer)}, Average WER: {np.average(accum_wer)}")
When i run this code i get this error this code is the same as your tutorial on sentence recognition (inferenceModel.py)
pythonlessons commented
check what image path you use, it seems you are not reading image from the right path
Mikey-889 commented
Thanks bro solved it