提高角膜上多类别图像分类的准确性

时间:2018-07-24 15:50:16

标签: python machine-learning keras multiclass-classification

我在keras(https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html-版本2)上使用猫和狗的例子来训练我自己的数据,以进行多类分类(11个类)。当我仅对两个类使用相同的控件时,其准确度就可以达到86%,但是,当我对所有11个类进行调整并使用相同的控件时,准确度确实很差。 有趣的是,跨历元的值始终为.0909(即1/11),这使我怀疑好像所有数据都仅被归为一种类型。

关于我做错了什么或如何提高准确性的任何建议。

img_width, img_height = 150, 150

top_model_weights_path = 'bottleneck_fc_model.h5'
train_data_dir = 'birdimages/train'
validation_data_dir = 'birdimages/validation'
nb_train_samples = 825
nb_validation_samples = 165
num_classes = 11
num_samples_per_class = nb_train_samples/num_classes
num_validate_per_class = nb_validation_samples/num_classes
epochs = 50
batch_size = 1


def save_bottlebeck_features():
    datagen = ImageDataGenerator(rescale=1. / 255)

    # build the VGG16 network
    model = applications.VGG16(include_top=False, weights='imagenet')

    generator = datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode=None,
        shuffle=False)
    bottleneck_features_train = model.predict_generator(generator, nb_train_samples // batch_size)

    np.save(open('bottleneck_features_train.npy', 'wb'),bottleneck_features_train)

    generator = datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode=None,
        shuffle=False)

    bottleneck_features_validation = model.predict_generator(generator, nb_validation_samples // batch_size)

    np.save(open('bottleneck_features_validation.npy', 'wb'),bottleneck_features_validation)


def train_top_model():
    train_data = np.load(open('bottleneck_features_train.npy'))
    train_data = to_categorical(train_data)

    train_labels = np.array( [0] * num_samples_per_class + [1] * num_samples_per_class + [2] * num_samples_per_class + [3] * num_samples_per_class + [4] * num_samples_per_class + [5] * num_samples_per_class + [6] * num_samples_per_class + [7] * num_samples_per_class + [8] * num_samples_per_class + [9] * num_samples_per_class + [10] * num_samples_per_class )
    train_labels = to_categorical(train_labels)

    validation_data = np.load(open('bottleneck_features_validation.npy'))
    validation_data = to_categorical(validation_data)

    validation_labels = np.array([0] * num_validate_per_class + [1] * num_validate_per_class + [2] * num_validate_per_class + [3] * num_validate_per_class + [4] * num_validate_per_class + [5] * num_validate_per_class + [6] * num_validate_per_class + [7] * num_validate_per_class + [8] * num_validate_per_class + [9] * num_validate_per_class + [10] * num_validate_per_class)
    validation_labels = to_categorical(validation_labels)

    model = Sequential()
    model.add(Flatten(input_shape=train_data.shape[1:]))
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))

    model.add(Dense(num_classes, activation='softmax'))

    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy', metrics=['accuracy'])

    model.fit(train_data, train_labels,
              epochs=epochs,
              batch_size=batch_size,
              validation_data=(validation_data, validation_labels))
    model.save_weights(top_model_weights_path)


save_bottlebeck_features()
train_top_model()

0 个答案:

没有答案