初始V3微调:为什么我使用InceptionV3微调获得非常低的(.37)精度?

时间:2017-07-03 07:30:14

标签: python machine-learning neural-network deep-learning keras

我尝试用我的自定义数据集(由2个类组成)来微调InceptionV3模型,但是我对训练和验证的准确度都很低。我该怎么做才能提高准确度?或者您是否有其他网络创意/实现用于此目的?

我的代码:

from keras.datasets import cifar10
from keras.utils import *
from keras.optimizers import SGD
from keras.layers import  Input,Dense,Flatten,Dropout,GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.models import Model
from keras.applications.inception_v3 import InceptionV3
import numpy as np
import cv2

epochs = 10
steps_per_epoch  = 300
validation_steps = 300
input_shape=(64, 64, 3)
image_rows=64
image_cols=64

train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    'dataset/train',
    target_size=(image_rows, image_cols),
    batch_size=32,
    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    'dataset/evaluate',
    target_size=(image_rows, image_cols),
    batch_size=32,
    class_mode='categorical')


inputs = Input(shape=input_shape)

base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(2, activation='softmax')(x)
model = Model(input=base_model.input, output=predictions)


for layer in base_model.layers:
  layer.trainable = False


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


model.fit_generator(
        train_generator,
        steps_per_epoch=steps_per_epoch,
        epochs=epochs,
        validation_data=validation_generator,
        validation_steps=validation_steps)

1 个答案:

答案 0 :(得分:3)

你的问题在于根据Keras InceptionV3 documentation - 最小输入大小为139这一事实。因此 - 由于您的网络输入大小为64 - 您的网络无法正常工作好。要克服这个问题:

  • 将输入大小更改为n,其中n > 139
  • 在每个flow_from_directory中 - 将target_size更改为(n, n)