问题描述
我执行 class_mode='categorical' 并且我的文件夹名称是 0 到 1801 的整数,但是当我运行代码时它返回此错误:
ValueError:检查目标时出错:预期dense_4具有形状(无,1082)但得到形状为(16,1802)的数组
我的模型是:
def Create_Model(conv_base):
model = models.Sequential()
model.add(conv_base)
model.add(layers.Dropout(0.5))
model.add(layers.Flatten())
model.add(layers.Dense(14,activation='relu'))
# model.add(layers.Dense(1,activation='sigmoid'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1082,activation='softmax'))
# Compile Model
model.compile(optimizer=optimizers.Adam(),loss=losses.categorical_crossentropy,# sparse_categorical_crossentropy: beacuse the labels are integer and aren't one-hot (help:https://stackoverflow.com/questions/54232549/multi-class-and-variable-size-image-classification-with-flow-from-directory)
metrics=['acc'])
# model.build((None,197,275,3))
# Print Model
print(model.summary())
return model
而且,我的代码是:
#%% Call Pretrain VGG16 with ImageNet
conv_base = VGG16(weights='imagenet',include_top=False,input_shape=(197,3))
#%% Create Model
model = Create_Model(conv_base)
#%% Freeze Conv_base(VGG-19) For Train Top layers
# Freezing just top layers
conv_base.trainable = False
# Generate Data
train_datagen = ImageDataGenerator(
rescale=1./255,rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,featurewise_center=True,# set input mean to 0 over the dataset
samplewise_center=True,# set each sample mean to 0
featurewise_std_normalization=True,# divide inputs by std of the dataset
samplewise_std_normalization=True,# divide each input by its std
fill_mode='nearest'
)
# preprocessing_function = preprocess,test_datagen = ImageDataGenerator(
rescale=1./255,# divide each input by its std
)
train_generator = train_datagen.flow_from_directory(train_dir,target_size=(197,275),batch_size=16,seed=124000,# If the out of memory,reduce this parameter and increse "steps_per_epoch" while manitaining ratio
class_mode='categorical') # sparse: beacuse labels is integer and is not one-hot (help:https://stackoverflow.com/questions/54232549/multi-class-and-variable-size-image-classification-with-flow-from-directory)
validation_generator = test_datagen.flow_from_directory(validation_dir,reduce this parameter and increse "validation_step" while manitaining ratio
class_mode='categorical') # sparse: beacuse labels is integer and is not one-hot (help:https://stackoverflow.com/questions/54232549/multi-class-and-variable-size-image-classification-with-flow-from-directory)
#%% Checkpoint
file_path = "weights.best.hdf5"
checkpoint = ModelCheckpoint(file_path,monitor='val_acc',verbose=1,save_best_only=True,mode='max')
callbacks_list = [checkpoint]
#%% Fit Model
'''
# Train Data: 176303
# Test Data: 48974
# Validation Data: 19590
'''
# history = model.fit_generator(train_generator,steps_per_epoch=5510,# Warning: for batch_size = 32
# validation_data=validation_generator,# validation_steps=613,epochs=100,# callbacks=callbacks_list)
history = model.fit_generator(train_generator,steps_per_epoch=11020,# Warning: for batch_size = 16
validation_data=validation_generator,validation_steps=1251,callbacks=callbacks_list)
# history = model.fit_generator(train_generator,steps_per_epoch=2,# Warning: for Test
# validation_data=validation_generator,# validation_steps=1,epochs=5,# callbacks=callbacks_list)
#%% Test model
test_generator = test_datagen.flow_from_directory(test_dir,batch_size=20,class_mode='categorical') # sparse: beacuse labels is integer and is not one-hot (help:https://stackoverflow.com/questions/54232549/multi-class-and-variable-size-image-classification-with-flow-from-directory)
请帮帮我。
ValueError:检查目标时出错:预期dense_4具有形状(无,1082)但得到形状为(16,1802)的数组
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)