当前位置:  开发笔记 > 编程语言 > 正文

Keras开始v3重新训练和微调错误

如何解决《Keras开始v3重新训练和微调错误》经验,为你挑选了1个好方法。

我试图从这里(https://keras.io/applications/)起作用的例子,因为它不起作用我有点疯狂...如果有人有一个我会非常感激想法我能尝试什么!这是我的示例代码:

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Input

# dimensions of our images.
img_width, img_height = 150, 150

train_data_dir = '/Users/michael/testdata/train' #contains two classes cats      and dogs
validation_data_dir = '/Users/michael/testdata/validation' #contains two classes cats and dogs

nb_train_samples = 1200
nb_validation_samples = 800
nb_epoch = 50

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(200, activation='softmax')(x)

# this is the model we will train
model = Model(input=base_model.input, output=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
#model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=    ['accuracy'])

# prepare data augmentation configuration
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(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=16,
    class_mode='categorical'
)

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=16,
    class_mode='categorical'
)

print "start history model"
history = model.fit_generator(
    train_generator,
    nb_epoch=nb_epoch,
    samples_per_epoch=128,
    validation_data=validation_generator,
    nb_val_samples=nb_validation_samples) #1020

当我运行这个时,我得到以下错误.我已经尝试将枕头更新到最新版本,但仍然是同样的错误:

#
Found 1199 images belonging to 2 classes.
Found 800 images belonging to 2 classes.
start history model
Epoch 1/50
Traceback (most recent call last):
  File "/Users/michael/PycharmProjects/keras-imaging/fine-tune-v3-new-    classes.py", line 75, in 
    nb_val_samples=nb_validation_samples) #1020
  File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line     1508, in fit_generator
    class_weight=class_weight)
  File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line     1261, in train_on_batch
    check_batch_dim=True)
  File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line     985, in _standardize_user_data
    exception_prefix='model target')
  File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line     113, in standardize_input_data
    str(array.shape))
ValueError: Error when checking model target: expected dense_2 to have shape     (None, 200) but got array with shape (16, 2)
Exception in thread Thread-1:
Traceback (most recent call last):
  File     "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/pytho    n2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File     "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/pytho    n2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/site-packages/keras/engine/training.py", line     409, in data_generator_task
    generator_output = next(generator)
  File "/usr/local/lib/python2.7/site-packages/keras/preprocessing/image.py", line 691, in next
    target_size=self.target_size)
  File "/usr/local/lib/python2.7/site-packages/keras/preprocessing/image.py", line     191, in load_img
    img = img.convert('RGB')
      File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 844, in             convert
        self.load()
          File "/usr/local/lib/python2.7/site-packages/PIL/ImageFile.py", line     248, in     load
        return Image.Image.load(self)
    AttributeError: 'NoneType' object has no attribute 'Image'

Sergii Grysh.. 5

预期的类数与实际类之间存在不匹配,从错误消息中可以清楚地看出:

ValueError: Error when checking model target: expected dense_2 to have shape     (None, 200) but got array with shape (16, 2)

在这里,您指定您的模型需要200个类,但实际上您只有2个类.

# and a logistic layer -- let's say we have 200 classes
predictions = Dense(200, activation='softmax')(x)

将其更改为 predictions = Dense(2, activation='softmax')(x)



1> Sergii Grysh..:

预期的类数与实际类之间存在不匹配,从错误消息中可以清楚地看出:

ValueError: Error when checking model target: expected dense_2 to have shape     (None, 200) but got array with shape (16, 2)

在这里,您指定您的模型需要200个类,但实际上您只有2个类.

# and a logistic layer -- let's say we have 200 classes
predictions = Dense(200, activation='softmax')(x)

将其更改为 predictions = Dense(2, activation='softmax')(x)

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有