Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问ctc_2019中input_length=16, label_length=4的具体使用方法? #46

Open
chajnoven opened this issue Nov 5, 2019 · 8 comments
Labels
base_model 与 model 为什么会有两个模型

Comments

@chajnoven
Copy link

我已经完成原版keras的使用,但ctc版本有所不同,关于生成器参数多出来两个,请问这两个参数的具体用途是?请问这两个参数该如何理解?

@wuyangdut
Copy link

我也有这个疑惑,看其他地方都没有用到啊

@ypwhs
Copy link
Owner

ypwhs commented Nov 5, 2019

数据生成器和 CNN 的差不多,这里需要多几个矩阵,一个是 input_length,代表序列长度,一个是 label_length,代表验证码长度,还有一个 np.ones,没有意义,只是为了适配 Keras 训练需要的矩阵输入。

from tensorflow.keras.utils import Sequence

class CaptchaSequence(Sequence):
    def __init__(self, characters, batch_size, steps, n_len=4, width=128, height=64, 
                 input_length=16, label_length=4):
        self.characters = characters
        self.batch_size = batch_size
        self.steps = steps
        self.n_len = n_len
        self.width = width
        self.height = height
        self.input_length = input_length
        self.label_length = label_length
        self.n_class = len(characters)
        self.generator = ImageCaptcha(width=width, height=height)
    
    def __len__(self):
        return self.steps

    def __getitem__(self, idx):
        X = np.zeros((self.batch_size, self.height, self.width, 3), dtype=np.float32)
        y = np.zeros((self.batch_size, self.n_len), dtype=np.uint8)
        input_length = np.ones(self.batch_size)*self.input_length
        label_length = np.ones(self.batch_size)*self.label_length
        for i in range(self.batch_size):
            random_str = ''.join([random.choice(self.characters) for j in range(self.n_len)])
            X[i] = np.array(self.generator.generate_image(random_str)) / 255.0
            y[i] = [self.characters.find(x) for x in random_str]
        return [X, y, input_length, label_length], np.ones(self.batch_size)

input_length 和 label_length 都在计算 loss 的地方用到了:

  • y_pred 是模型的输出,是按顺序输出的37个字符的概率,因为我们这里用到了循环神经网络,所以需要一个空白字符的概念;
  • labels 是验证码,是四个数字,每个数字代表字符在字符集里的位置
  • input_length 表示 y_pred 的长度,我们这里是16
  • label_length 表示 labels 的长度,我们这里是4
import tensorflow.keras.backend as K

def ctc_lambda_func(args):
    y_pred, labels, input_length, label_length = args
    return K.ctc_batch_cost(labels, y_pred, input_length, label_length)

@ypwhs ypwhs added the base_model 与 model 为什么会有两个模型 label Nov 9, 2019
@alex337
Copy link

alex337 commented Nov 20, 2019

请问一下这边的input_length是啥

@ypwhs
Copy link
Owner

ypwhs commented Nov 20, 2019

请问一下这边的input_length是啥

input_length,代表序列长度,我们这里是4

@lijiajun3029
Copy link

请问序列长度input_length选择16的原因是什么呢,最后一张特征图宽度16有什么关系呢?16是这里的最大取值吗,小一点有什么缺点与优点呢,请赐教

@ypwhs
Copy link
Owner

ypwhs commented Nov 25, 2019

请问序列长度input_length选择16的原因是什么呢,最后一张特征图宽度16有什么关系呢?16是这里的最大取值吗,小一点有什么缺点与优点呢,请赐教

input_length 表示 y_pred 的长度,也就是 RNN 输出的序列长度,它和 CNN 输出的宽度是相等的,不能随意修改。

@lijiajun3029
Copy link

请问序列长度input_length选择16的原因是什么呢,最后一张特征图宽度16有什么关系呢?16是这里的最大取值吗,小一点有什么缺点与优点呢,请赐教

input_length 表示 y_pred 的长度,也就是 RNN 输出的序列长度,它和 CNN 输出的宽度是相等的,不能随意修改。谢谢

@ctzhang2008
Copy link

请问序列长度input_length选择16的原因是什么呢,最后一张特征图宽度16有什么关系呢?16是这里的最大取值吗,小一点有什么缺点与优点呢,请赐教

input_length 表示 y_pred 的长度,也就是 RNN 输出的序列长度,它和 CNN 输出的宽度是相等的,不能随意修改。谢谢

它和 CNN 输出的宽度是相等的,不能随意修改。不理解CNN输出的宽度指的是什么?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
base_model 与 model 为什么会有两个模型
Projects
None yet
Development

No branches or pull requests

6 participants