gpt4 book ai didi

python-2.7 - Keras inception v3 重新训练和微调错误

转载 作者:行者123 更新时间:2023-12-03 09:39:18 27 4
gpt4 key购买 nike

我试图从这里( 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 <module>
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'

最佳答案

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

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)

关于python-2.7 - Keras inception v3 重新训练和微调错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41883800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com