[英]Do X_train and y_train match? Convolutional Neural Network - Segmentation free character recognition
I am trying to train a convolutional neural.network for character recognition on images.我正在尝试训练一个卷积神经网络来识别图像上的字符。 One image contains 7 characters (0-9, AZ, blank space => 37 possibilities for characters).
一张图像包含 7 个字符(0-9、AZ、空格 => 字符的 37 种可能性)。 In total there are 646 images.
总共有 646 张图片。 These images are the X_train data.
这些图像是 X_train 数据。
X_train.shape
(646, 600, 1200, 1)
I also have a datasheet with all labels of the images.我还有一个包含图像所有标签的数据表。 I one-hot-encoded these labels in order to get arrays.
我对这些标签进行了单热编码以获得 arrays。
y_train.shape
(646, 7, 37)
646 is the number of images, so the row dimension. 646是图像的数量,所以是行维度。 7 is the length of the label, the column dimension.
7是label的长度,列维度。 37 is the number of possible characters, the depth dimension.
37是可能的字符数,深度维度。
I would like my convolutional neural.network to recognize the characters on the image automatically without any segmentation or spatial specifications of the characters as the authors in this paper did: https://ieeexplore.ieee.org/abstract/document/8078501我希望我的卷积神经网络能够自动识别图像上的字符,而无需像本文作者那样对字符进行任何分割或空间规范: https://ieeexplore.ieee.org/abstract/document/8078501
I am now wondering if X_train and y_train fit together for the purpose of my neural.network?我现在想知道 X_train 和 y_train 是否适合我的神经网络? Maybe I have to reshape the dimension of X_train or y_train, because when I am running this code:
也许我必须重塑 X_train 或 y_train 的维度,因为当我运行这段代码时:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), input_shape = (600, 1200, 1), activation = "relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64, activation = "relu"))
model.add(Dense(7, activation = "relu"))
model.add(Dense(37, activation = "softmax"))
model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(X_train, y_train, batch_size = 32, epochs = 10, shuffle = True)
I got the following error:我收到以下错误:
ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (646, 7, 37)
Thank you for all advices!谢谢大家的建议!
I redefined the.networks in order to match y output shape, I can say anything about the performances我重新定义了 .networks 以匹配 y output 形状,我可以对表演说些什么
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), input_shape = (600, 1200, 1), activation = "relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Lambda(lambda x: tf.expand_dims(x,1)))
model.add(Dense(64, activation = "relu"))
model.add(Dense(7, activation = "relu"))
model.add(Permute((2, 1)))
model.add(Dense(37, activation = "softmax"))
the summary:摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_6 (Conv2D) (None, 598, 1198, 32) 320
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 299, 599, 32) 0
_________________________________________________________________
dropout_6 (Dropout) (None, 299, 599, 32) 0
_________________________________________________________________
flatten_5 (Flatten) (None, 5731232) 0
_________________________________________________________________
lambda_3 (Lambda) (None, 1, 5731232) 0
_________________________________________________________________
dense_15 (Dense) (None, 1, 64) 366798912
_________________________________________________________________
dense_16 (Dense) (None, 1, 7) 455
_________________________________________________________________
permute (Permute) (None, 7, 1) 0
_________________________________________________________________
dense_17 (Dense) (None, 7, 37) 74
=================================================================
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.