我想在Keras中使用InceptionV3来利用瓶颈进行转移学习 . 我已经使用了一些关于创建,加载和使用瓶颈的技巧来自https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
我的问题是我不知道如何使用瓶颈(numpy数组)作为具有新顶层的InceptionV3的输入 .
I get the following error:
ValueError:检查输入时出错:期望input_3有形状(None,None,None,3)但是有形状的数组(248,8,8,2048)
248指的是这种情况下的图像总数 .
我知道这行是错的,但我不知道如何纠正它:
model = Model(inputs = base_model.input,outputs = predictions)
将瓶颈输入InceptionV3的正确方法是什么?
Creating the InceptionV3 bottlenecks:
def create_bottlenecks():
datagen = ImageDataGenerator(rescale=1. / 255)
model = InceptionV3(include_top=False, weights='imagenet')
# Generate bottlenecks for all training images
generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
nb_train_samples = len(generator.filenames)
bottlenecks_train = model.predict_generator(generator, int(math.ceil(nb_train_samples / float(batch_size))), verbose=1)
np.save(open(train_bottlenecks_file, 'w'), bottlenecks_train)
# Generate bottlenecks for all validation images
generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
nb_validation_samples = len(generator.filenames)
bottlenecks_validation = model.predict_generator(generator, int(math.ceil(nb_validation_samples / float(batch_size))), verbose=1)
np.save(open(validation_bottlenecks_file, 'w'), bottlenecks_validation)
Loading the bottlenecks:
def load_bottlenecks(src_dir, bottleneck_file):
datagen = ImageDataGenerator(rescale=1. / 255)
generator = datagen.flow_from_directory(
src_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=False)
num_classes = len(generator.class_indices)
# load the bottleneck features saved earlier
bottleneck_data = np.load(bottleneck_file)
# get the class lebels for the training data, in the original order
bottleneck_class_labels = generator.classes
# convert the training labels to categorical vectors
bottleneck_class_labels = to_categorical(bottleneck_class_labels, num_classes=num_classes)
return bottleneck_data, bottleneck_class_labels
Starting training:
def start_training():
global nb_train_samples, nb_validation_samples
create_bottlenecks()
train_data, train_labels = load_bottlenecks(train_data_dir, train_bottlenecks_file)
validation_data, validation_labels = load_bottlenecks(validation_data_dir, validation_bottlenecks_file)
nb_train_samples = len(train_data)
nb_validation_samples = len(validation_data)
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 2 classes
predictions = Dense(2, activation='softmax')(x)
# What is the correct input? Obviously not base_model.input.
model = Model(inputs=base_model.input, outputs=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
model.compile(optimizer=optimizers.SGD(lr=0.01, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])
# train the model on the new data for a few epochs
history = model.fit(train_data, train_labels,
epochs=epochs,
batch_size=batch_size,
validation_data=(validation_data, validation_labels),
)
任何帮助,将不胜感激!
1 回答
当您尝试使用与模型支持的形状不同的输入数据训练模型时,会发生此错误 .
您的模型支持
(None, None, None, 3)
,意思是:任意数量的图像
任何高度
任何宽度
3个 Channels
因此,您必须确保
train_data
(和validation_data
)与此形状匹配 .系统告诉
train_data.shape = (248,8,8,2048)
我看到
train_data
来自load_botlenecks
. 真的应该是从那里来的吗?什么是火车数据应该是什么?一个图像?别的什么?什么是瓶颈?您的模型从Inception模型开始,Inception模型采用图像 .
但是,如果瓶颈已经是Inception模型的结果,并且您想要提供 only 瓶颈,则Inception模型不应该参与任何事情 .
从...开始:
创建模型:
这个想法是:
初始模型:图像 - >初始 - >瓶颈
您的型号:瓶颈 - >型号 - >标签
只有当您没有预装瓶颈时才需要组合使用这两种模型,但是您有自己的图像需要先预测瓶颈 . (当然你也可以使用不同的模型)
然后你将只输入图像(瓶颈将由Inception创建并传递给你的模型,内部的一切):
为了那个原因: