pythonlessons / mltu

Machine Learning Training Utilities (for TensorFlow and PyTorch)
MIT License
160 stars 100 forks source link

Error while testing model using referenceModel #17

Closed Mikey-889 closed 1 year ago

Mikey-889 commented 1 year ago

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)

Screenshot 2023-06-22 210027

pythonlessons commented 1 year ago

check what image path you use, it seems you are not reading image from the right path

Mikey-889 commented 1 year ago

Thanks bro solved it