使用特征向量

使用特征向量

学习出处

我认为的缺点:将train和test一并喂入预训练模型生成各自对应的特征向量,比较费时

注:三个模型的输出图为卷积层输出的激活特征图

生成代码实现概要

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def write_gap(MODEL, image_size, lambda_func=None):
width = image_size[0]
height = image_size[1]
input_tensor = Input((height, width, 3))
x = input_tensor
if lambda_func:
x = Lambda(lambda_func)(x)

base_model = MODEL(input_tensor=x, weights='imagenet', include_top=False)
#include_top: 是否包括顶层的全连接层
model = Model(base_model.input, GlobalAveragePooling2D()(base_model.output))

gen = ImageDataGenerator()
train_generator = gen.flow_from_directory("train2", image_size, shuffle=False,
batch_size=16)
test_generator = gen.flow_from_directory("test2", image_size, shuffle=False,
batch_size=16, class_mode=None)

train = model.predict_generator(train_generator, train_generator.nb_sample)
test = model.predict_generator(test_generator, test_generator.nb_sample)
with h5py.File("gap_%s.h5"%MODEL.func_name) as h:
h.create_dataset("train", data=train)
h.create_dataset("test", data=test)
h.create_dataset("label", data=train_generator.classes)

write_gap(ResNet50, (224, 224))
write_gap(InceptionV3, (299, 299), inception_v3.preprocess_input)
write_gap(Xception, (299, 299), xception.preprocess_input)

使用模型概要

1
2
3
4
5
6
7
8
9
10
11
12
13
from keras.models import *
from keras.layers import *

np.random.seed(2017)

input_tensor = Input(X_train.shape[1:])
x = Dropout(0.5)(input_tensor)
x = Dense(1, activation='sigmoid')(x)
model = Model(input_tensor, x)

model.compile(optimizer='adadelta',
loss='binary_crossentropy',
metrics=['accuracy'])

使用numpy的

面向小数据集构建图像分类模型

思想: 利用网络的卷积层部分,把全连接以上的部分抛掉。然后在我们的训练集和测试集上跑一遍,将得到的输出(即“bottleneck feature”,网络在全连接之前的最后一层激活的feature map)记录在两个numpy array里。然后我们基于记录下来的特征训练一个全连接网络

1
2
3
4
bottleneck_features_train = model.predict_generator(generator, 2000)
# save the output as a Numpy array
np.save(open('bottleneck_features_train.npy', 'w'), bottleneck_features_train)
train_data = np.load(open('bottleneck_features_train.npy'))
1
2