我试图建立一个模型来帮助我识别多标签分类问题的图像,例如,如果我有猫,狗和牛的照片。 我运行了一个CNN模型,但它根本没有捕捉到(精度为33%)。 谁能分享一个可行的模型(即使准确性只是合理的)? 在此先感谢大家! [附上我上面提到的代码]
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D,
BatchNormalization
from keras.callbacks import LearningRateScheduler
from keras.optimizers import adam, SGD
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import VGG16
# 2 - Create network layers
image_width = 200
image_height = 200
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(3,3),
activation='relu',input_shape=(
(image_width,image_height,3)))
model.add(BatchNormalization())
model.add(Conv2D(filters=16, kernel_size=(3,3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPool2D(strides=(2,2)))
model.add(Dropout(0.25))
# Stage II = make it more compex with 'filters = 32'
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
model.add(BatchNormalization())
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPool2D(strides=(2,2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))
# We'll Randomize the training set (shuffle), to avoid overfitting
(augmentation)
datagen = ImageDataGenerator(zoom_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
rotation_range = 10)
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=
['accuracy'])
# automatically retrieve images and their classes for train and validation
train_generator = datagen.flow_from_directory(
train_dataset,
target_size=(image_width, image_height),
batch_size=32,
class_mode='categorical')
validation_generator = datagen.flow_from_directory(
validation_dataset,
target_size=(image_width, image_height),
batch_size=32,
class_mode='categorical')
# Now let's fit the model on the validation set
model.fit_generator(
train_generator,
steps_per_epoch=50,
epochs=500,
validation_data=validation_generator,
validation_steps=15)
答案 0 :(得分:0)
我在您的代码中看到的问题之一是flow_from_directory
不支持多标签分类。它将仅基于子目录返回单个标签。链接到docs
这可能是一个巨大的问题,因为您的模型甚至没有执行多标签分类。