[英]ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)
[英]ValueError when fitting my model. (ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224,3,3))
我是机器学习的新手,我正在使用 kaggle 的笔记本进行编码。 我正在制作具有多个类别的分类模型。 我使用efficientnet 来制作我的模型的架构,但是我尝试过的所有其他模型都会出现这个问题。 待分类的图像在数据集中分为 train 和 val 文件夹。 在这些文件夹中,它们位于各自班级的文件夹中。
代码运行良好,直到 fit_generator,它给了我一个 valueError “ValueError:无法将输入数组从形状(224,224,3)广播到形状(224,224,3,3)”
我附上了完整的代码、数据集和错误消息的图像。
我不知道代码或数据有什么问题? 请帮助我并感谢您阅读此问题,如果缺少更多上下文,我深表歉意。
#!pip install -U efficientnet
import pandas as pd
import numpy as np
import efficientnet.tfkeras as efn # Convolutional Neural Network architecture
import IPython.display as ipd
import librosa.display
import matplotlib.pyplot as plt
from efficientnet.keras import preprocess_input
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from sklearn.utils import class_weight
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
IM_SIZE = (224, 224, 3)
train=pd.read_csv("../input/birdclef-2022/train_metadata.csv")
BIRDS = os.listdir("../input/mel-split-mark17/mel_spectrogram/train")
BATCH_SIZE = 16
train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.1,
fill_mode="nearest",
)
train_batches = train_datagen.flow_from_directory(
"../input/mel-split-mark17/mel_spectrogram/train",
classes=BIRDS,
target_size=IM_SIZE,
class_mode="categorical",
shuffle=True,
batch_size=BATCH_SIZE,
)
valid_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
valid_batches = valid_datagen.flow_from_directory(
"../input/mel-split-mark17/mel_spectrogram/val",
classes=BIRDS,
target_size=IM_SIZE,
class_mode="categorical",
shuffle=False,
batch_size=BATCH_SIZE,
)
# Define CNN's architecture
net = efn.EfficientNetB3(
include_top=False, weights="imagenet", input_tensor=None, input_shape=IM_SIZE
)
x = net.output
x = Flatten()(x)
x = Dropout(0.5)(x)
output_layer = Dense(len(BIRDS), activation="softmax", name="softmax")(x)
net_final = Model(inputs=net.input, outputs=output_layer)
net_final.compile(
optimizer=Adam(), loss="categorical_crossentropy", metrics=["accuracy"]
)
print(net_final.summary())
# Estimate class weights for unbalanced dataset
class_weights = class_weight.compute_class_weight(
class_weight = "balanced",
classes= np.unique(train_batches.classes),
y=train_batches.classes
)
# Define callbacks
ModelCheck = ModelCheckpoint(
"models/efficientnet_checkpoint.h5",
monitor="val_loss",
verbose=0,
save_best_only=True,
save_weights_only=True,
mode="auto",
period=1,
)
ReduceLR = ReduceLROnPlateau(monitor="val_loss", factor=0.2, patience=5, min_lr=3e-4)
# Train the model
net_final.fit_generator(
train_batches,
validation_data=valid_batches,
epochs=30,
steps_per_epoch=1596,
class_weight=class_weights,
callbacks=[ModelCheck, ReduceLR],
)
我认为问题出在.flow_from_directory方法中。 该方法中图像的形状不应包括图像通道,您可以通过将附加参数“color_mode”设置为“rgb”来指定使用 3 个通道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.