我是深度学习和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"])
我遇到了这个错误:
回溯(最近通话最近): 文件“ train_mask.py”,第130行,在 epochs = EPOCHS) tensorflow.python.framework.errors_impl.InvalidArgumentError:不兼容的形状:[10,7]与[10] [[{{节点指标/ acc /等于}}]] [[{{node> ConstantFoldingCtrl / loss / dense_1_loss / broadcast_weights / assert_broadcastable / AssertGuard / Switch_0}}]]
注意:当我在第77行注释时,训练效果很好,但结果非常差,准确度= {1.2xx
答案 0 :(得分:0)
您可以保留第77行,并从以下位置更改编译功能:
model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["categorical_accuracy"])
或者您可以删除第77行并将编译功能更改为:
model.compile(loss="sparse_categorical_crossentropy", optimizer=opt,metrics=["sparse_categorical_accuracy"])