我正在尝试构建一个可以识别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
答案 0 :(得分:0)
错误在于表示网络输出的形状和标签的形状不匹配。当您使用softmax和分类交叉熵损失时,您的标签应编码为单热矢量。
幸运的是,keras提供了一种方法:
from keras.utils import to_categorical
labels = to_categorical(labels)
然后,如果检查标签的形状,它应该是(样本,62),与网络输出相同,它应该可以正常工作。