JanPalasek / ulozto-captcha-breaker

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

ImportError: cannot import name 'all_correct_acc' from 'metrics' #15

Closed OrsonFun closed 1 year ago

OrsonFun commented 1 year ago

Hi,

if i want to tain my own model i keep getting the error message: ImportError: cannot import name 'all_correct_acc' from 'metrics' can you give me a hint why i get this error message?

i m on windows 10 and Python 3.9.13

EDIT: fixed it with: pip install metrics getting the code from ulozto-captcha-breaker-master\src\ulozto_captcha_breaker\metric.py and replaced it in my python folder and there \Lib\site-packages\metrics\metrics.py looks now like this:


import tensorflow as tf

def all_correct_acc(y_true: tf.Tensor, y_pred: tf.Tensor):
    """
    Computes accuracy between y_true and y_pred in the following manner:

    - If i-th sample has all values on y_pred same as on y_true, then 1.
    - Otherwise 0.

    It is hence more restricting then a typical accuracy.

    Args:
        y_true (tf.Tensor): 2D tensor of shape (N, L), where N is the number of samples and L is length of the vector (number of characters).
        y_pred: 2D tensor of shape (N, L), where N is the number of samples and L is length of the vector (number of characters)

    Returns:
        Accuracy: number between [0, 1] denoting how many codes were predicted correctly.
    """
    if y_true.shape[0] is None and y_true.shape[1] is None and y_true.shape[2] is None:
        return tf.convert_to_tensor(0)

    # cast to int64 so we can compare it
    y_true = tf.cast(y_true, tf.dtypes.int64)

    if len(y_pred.shape) <= 2:
        y_pred = tf.expand_dims(y_pred, axis=1)
    if len(y_true.shape) <= 1:
        y_true = tf.expand_dims(y_true, axis=1)
    y_pred = tf.argmax(y_pred, axis=2)
    correct = y_true == y_pred
    # tf.print(f"Pred shape: {y_true.shape}", output_stream=sys.stdout)

    all_correct = tf.reduce_all(correct, axis=1)
    all_correct = tf.cast(all_correct, tf.dtypes.float32)

    return tf.reduce_mean(all_correct)

now i got a new error message when i want to train my own model: image

the code in image_preprocessors.py:

import numpy as np
from PIL import Image

class ConvertToGrayscalePreprocessor:
    """
    Converts image to grayscale.
    """
    def __call__(self, img: np.ndarray):
        r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
        output = 0.299 * r + 0.587 * g + 0.114 * b
        return output

class ImageCutPreprocessor:
    def __init__(self, pieces_count: int):
        self._pieces_count = pieces_count

    def __call__(self, image: np.ndarray):
        images = np.split(image, self._pieces_count, axis=1)

        return np.array(images)

class NormalizeImagePreprocessor:
    """
    Converts image from byte format (values are integers in {0, ..., 255} to normalized float format (values are
    floats in the interval [0, 1].
    """
    def __init__(self):
        pass

    def __call__(self, image):
        image = image.astype(np.float32) / 255
        image = np.expand_dims(image, axis=len(image.shape))
        return image

class ResizePreprocessor:
    """
    Resizes image to target width and height.
    """
    def __init__(self, target_height, target_width):
        self._target_height = target_height
        self._target_width = target_width

    def __call__(self, img: np.ndarray):
        return img.resize(img, (self._target_width, self._target_height))
JanPalasek commented 1 year ago

Hi @OrsonFun ,

I've fixed the problem. Can you try it again with the updated version of master branch?

JanPalasek commented 1 year ago

Stale. Closing.