【发布时间】:2020-07-12 07:45:11
【问题描述】:
我是深度学习和 Keras 库的新手,我正在尝试使用 Adrian Rosebrock 的教程(在 X 射线图像中检测 COVID-19)使用二进制分类。 我的目标是训练两个以上的课程。我进行了一些更改以应用类别分类。 (7类检测面部情绪)。 我最近发布了一个培训错误: link
我已经解决了,但我还有另一个错误:
标签形状和数据形状的输出:
(981, 7, 2) # labels shape
(981, 224, 224, 3) # data shape
我正在尝试使用此脚本训练数据集(在应用了一些修改之后)。
INIT_LR = 1e-3
EPOCHS = 100
BS = 10
print("[INFO] loading images...")
imagePaths = list(paths.list_images(args["dataset"]))
data = []
labels = []
for imagePath in imagePaths:
label = imagePath.split(os.path.sep)[-2]
image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (224, 224))
data.append(image)
labels.append(label)
data = np.array(data) / 255.0
labels = np.array(labels)
stratifylabels=np.array(labels)
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels) # line 77
print(labels.shape)
print(data.shape)
(trainX, testX, trainY, testY) = train_test_split(data, labels,
test_size=0.20, random_state=42,)
trainAug = ImageDataGenerator(
rotation_range=15,
fill_mode="nearest")
baseModel = VGG16(weights="imagenet", include_top=False,
input_tensor=Input(shape=(224, 224, 3)))
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(64, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(7, activation="softmax")(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
for layer in baseModel.layers:
layer.trainable = False
print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])
我遇到了这个错误:
Traceback(最近一次调用最后一次): 文件“train_mask.py”,第 130 行,在 纪元=EPOCHS) tensorflow.python.framework.errors_impl.InvalidArgumentError:不兼容的形状:[10,7] vs. [10] [[{{节点指标/acc/Equal}}]] [[{{node >ConstantFoldingCtrl/loss/dense_1_loss/broadcast_weights/assert_broadcastable/AssertGuard/Switch_0}}]]
注意:当我评论第 77 行时,训练工作正常,但结果非常糟糕,准确率=1.2xx
【问题讨论】:
标签: python tensorflow keras