python - 如何将自己的数据集提供给keras image_ocr

标签 python machine-learning artificial-intelligence keras ocr

我知道 keras image_ocr 模型。它使用图像生成器来生成图像,但是,我面临一些困难,因为我试图将自己的数据集提供给模型进行训练。vi

存储库链接是:https://github.com/fchollet/keras/blob/master/examples/image_ocr.py

我创建了数组:x 和 y。我的图像路径及其相应的 gt 位于 csv 文件中。

x 的图像尺寸为: [nb_samples,w,h,c]

y 被赋予标签,它是一个字符串,即 gt。

这是我用来预处理的代码:

for i in range(0,len(read_file)):
    path = read_file['path'][i]
    label = read_file['gt'][i]
    path = path.strip('\n')
    img = cv2.imread(path,0)
    #Re-sizing the images
    #height = 64, width = 128
    #res_img = cv2.resize(img, (128,64))
    #cv2.imwrite(i,res_img)
    h,w =  img.shape
    x.append(img)
    y.append(label)
    size = img.size
    """
    print "Height: ", h #Height
    print "Width: ", w #Width
    print "Channel: ", c #Channel
    print "Size: ", size
    print "\n"
    """
print "H: ", h
print "W: ", w
print "S: ", size

x = np.array(x).astype(np.float32)
y = np.array(y)

x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.3,random_state=42)

x_train = np.array(x_train).astype(np.float32)
y_train = np.array(y_train)
x_train = np.array(x_train)
x_test = np.array(x_test)
y_test = np.array(y_test)

print "Printing the shapes. \n"
print "X_train shape: ", x_train.shape
print "Y_train shape: ", y_train.shape
print "X_test shape: ", x_test.shape
print "Y_test shape: ", y_test.shape
print "\n"

后面是 keras image_ocr 代码。总代码在这里: https://gist.github.com/kjanjua26/b46388bbde9ded5cf1f077a9f0dedc4f

运行时的错误是:

`Traceback (most recent call last):
 File "preprocess.py", line 323, in <module>
 train(run_name, 0, 20, w)
 File "preprocess.py", line 314, in train
 model.fit(next_train(x_train), y_train, batch_size=7, epochs=20,       verbose=1, validation_split=0.1, shuffle=True, initial_epoch=0)
 File "/home/kamranjanjua/anaconda2/lib/python2.7/site-  packages/keras/engine/training.py", line 1358, in fit
batch_size=batch_size)
 File "/home/kamranjanjua/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1234, in _standardize_user_data
exception_prefix='input')
 File "/home/kamranjanjua/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 100, in _standardize_input_data
'Found: ' + str(data)[:200] + '...')
 TypeError: Error when checking model input: data should be a Numpy array, or list/dict of Numpy arrays. Found: <generator object next_train at 0x7f8752671640>...`

如有任何帮助,我们将不胜感激。

最佳答案

如果仔细查看代码,您将能够看到该模型需要字典作为其输入。

inputs = {'the_input': X_data,'the_labels': labels, 'input_length': input_length,'label_length': label_length,'source_str': source_str}

outputs = {'ctc': np.zeros([size])}  # dummy data for dummy loss function

对于输入: 1)X_data是训练样本 2)Labels为对应训练样例的Label 3) label_length是标签的长度 4)Input_Length是您输入的长度 5) 源字符串是 不是强制的,只是用于解码

输出是 CTC 损失函数的虚拟数据

现在在您的代码中,您仅生成了 X_train、y_train,但缺少其他输入。您需要根据模型的预期输入和输出准备数据集。

关于python - 如何将自己的数据集提供给keras image_ocr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45566412/

相关文章:

python - KNN 归一化的精度差异

r - 多个连续变量之间的关联规则

Java微服务训练AI模型问题

python - 从 Google App Engine 模型迁移到纯 Django 系统

python - TensorFlow - 值错误 : features should be a dictionary of `Tensor` s

algorithm - 寻找启发式传教士和食人者

java - 我的带有 alpha-beta 剪枝的 Negamax 算法有问题吗?

python - 过拟合问题,验证成功率远低于训练。我能改变什么?

python - RS485 Modbus-RTU 设备给出的错误是什么

python - 同时点击 URL 并处理结果