【问题标题】:Error while building OCR构建 OCR 时出错
【发布时间】:2018-03-29 10:18:54
【问题描述】:

我试图构建一个可以识别 A-Z、a-z 和 0-9 的 ocr。 那是总共62个类别的认可。 这是我的代码:-

ap = argparse.ArgumentParser() #aaaaaaaaaaaaaaaaaaaaaaaaaaa
ap.add_argument("-d", "--dataset", required=True, help="path to input dataset")
ap.add_argument("-m", "--model", required=True, help="path to output model")
ap.add_argument("-p", "--plot", type=str, default="plot.png", help="path to output accuracy/loss plot")
args = vars(ap.parse_args())

print("Loading images.....")
data = []
labels = []

imagePaths = sorted(list(paths.list_images(args["dataset"])))
random.seed(42) #aaaaaaaaaaaaaaaaaaaaaaaaaaa
random.shuffle(imagePaths)

for imagePath in imagePaths:
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (28, 28))
    image = img_to_array(image)
    data.append(image)
    label = imagePath.split(os.path.sep)[-2]
    labels.append(label)

data = np.array(data, dtype = "float")/255.0
labels = np.array(labels)

(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size = 0.25, random_state = 42)

aug = ImageDataGenerator(rotation_range = 30, width_shift_range = 0.1, height_shift_range = 0.1, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = False, fill_mode = "nearest")

print("Compiling model.....")
model = ConvNet.build(width = 28, height = 28, depth = 3, classes = 62)
model.compile(loss = 'categorical_crossentropy', optimizer = 'Adam', metrics = ['accuracy'])

print("Training model.....")
# aug.fit(trainX)
H = model.fit_generator(aug.flow(trainX, trainY, batch_size = 10), validation_data = (testX, testY), steps_per_epoch = len(trainX)//10, epochs = 10)

print("Saving the model.....")
model.save(args["model"])

当我运行时出现以下错误:-

ValueError: Error when checking target: expected activation_4 to have shape (None, 62) but got array with shape (853, 1)

请帮我理解activation_4是什么?为什么我得到了一个形状数组 (843, 1)

这是我的 ConvNet 代码:-

from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
from keras import backend as K

class ConvNet:
    @staticmethod
    def build(width, height, depth, classes):
        model = Sequential()
        inputShape = (height, width, depth)

        if K.image_data_format() == "channels_first":
            inputShape = (depth, height, width)
        model.add(Conv2D(20, (5, 5), padding="same", input_shape=inputShape))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(Conv2D(50, (5, 5), padding="same"))
        model.add(Activation("relu"))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(Flatten())
        model.add(Dense(500))
        model.add(Activation("relu"))
        model.add(Dense(classes))
        model.add(Activation("softmax"))

    return model

【问题讨论】:

  • 很可能,activation_4 是你的最后一个,softmax - 但可以肯定的是,在拟合之前添加一个 model.summary() 语句,并在此处包含 fresh 的输出重新运行

标签: python-3.x machine-learning keras ocr


【解决方案1】:

错误是网络输出的形状和标签的形状不匹配。当您使用 softmax 和分类交叉熵损失时,您的标签应编码为 one-hot 向量。

幸运的是 keras 提供了一种方法来做到这一点:

from keras.utils import to_categorical
labels = to_categorical(labels)

然后,如果您检查标签的形状,它应该是(样本,62),与网络输出相同,它应该可以正常工作。

【讨论】:

  • 我得到了你说的方法的输出。谢谢。还有一个疑问,我有 62 个文件夹,分别是 0-9、A-Z 和 a-z,从 0 到 61 标记。每个文件夹有 55 个图像样本。你能告诉我,如果我没有应用 one-hot 编码,我是如何得到 (853, 1) 的形状的?
猜你喜欢
  • 1970-01-01
  • 2018-11-26
  • 2012-03-30
  • 2011-10-11
  • 2012-03-23
  • 2016-11-20
  • 2012-04-08
  • 2017-12-20
  • 2016-05-27
相关资源
最近更新 更多