【发布时间】:2021-12-12 01:36:15
【问题描述】:
我实际上是在尝试对图像进行表面缺陷检测(检查墙壁上的缺陷,如裂缝……)当我尝试拟合模型时,它会抛出错误 logits 并且标签必须是 broadcastable: logits_size=[32,198] labels_size=[32,3]
我尝试了几种方法,但都没有奏效。我该如何克服这个错误,或者我选择的方法有什么问题? 我正在使用的数据是未标记的图像数据(所有图像都在一个文件夹中)
from keras.preprocessing.image import ImageDataGenerator
train_model = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_model = ImageDataGenerator(rescale = 1./255)
training_data = train_model.flow_from_directory('/Users/nm2/Public/ai-dataset-training-100/5/23_463_DISTACCO_DEL_COPRIFERRO_Q100_training_dataset',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
testing_data = test_model.flow_from_directory('/Users/nm2/Public/ai-dataset-training-100/5/23_463_DISTACCO_DEL_COPRIFERRO_Q100_training_dataset',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
IMAGE_SIZE = [224, 224]
#Import the Vgg 16 and add the preprocessing layer to front of the VGG16 Here we will use ImageNet PreTrained Weights
vgg_model = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
for layer in vgg_model.layers:
layer.trainable = False
x = Flatten()(vgg_model.output)
#We use glob function to find out how many files are there in the working directory and count the number of classes they belong to.
folder_count = glob('/Users/nm2/Public/ai-dataset-training-`100/5/23_493_PANORAMICA_LIVELLO_BASE_ISPEZIONE_Q100_training_dataset/*')`
prediction = Dense(len(folder_count), activation='softmax')(x)
#Create a Model
model = Model(inputs=vgg_model.input, outputs=prediction)
model.summary()
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
post_run = model.fit(training_data,
validation_data=testing_data,
epochs=10,
steps_per_epoch=len(training_data),
validation_steps=len(testing_data))
InvalidArgumentError: logits and labels must be broadcastable: logits_size=[32,198] labels_size=[32,3]
[[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at var/folders/3b/tfwxbsyd41j64kbrjghzrvcm0000gq/T/ipykernel_1068/3441923959.py:5) ]] [Op:__inference_train_function_1205]
Function call stack:
train_function
【问题讨论】:
标签: tensorflow deep-learning computer-vision object-detection transfer-learning