对截止至2020.8.1的top5方案进行总结归纳
比赛链接:dog-breed-identification
1、数据形式
2、top1总结
3、top2总结
4、top3总结
……
1、数据形式
120个类别,训练集共10222张图片,测试集10357张
具体:train和test根目录下为图片,labels为每个图片对应label,sample_submission为测试集和最终给出的csv格式范例。具体如下
import pandas as pd
labelfile = os.path.join(dirname,'labels.csv')
dataf = pd.read_csv(labelfile)
print('csv文件长度:', len(dataf))
dataf
# 载入测试集
df2 = pd.read_csv(dirname + 'sample_submission.csv')
df2.head()
2、top1方案总结
top1传送门
方案:
1、使用4个提取特征器提取特征(fc层之前的特征)
2、将特征融合
3、使用简单的cnn网络,包括dense层和dropout层进行训练
没有使用数据增强
结果:验证集最高为0.9413,loss最低为0.1982。
# 提取特征网络
def get_features(model_name, data_preprocessor, input_size, data):
'''
1- Create a feature extractor to extract features from the data.
2- Returns the extracted features and the feature extractor.
'''
#Prepare pipeline.
input_layer = Input(input_size)
preprocessor = Lambda(data_preprocessor)(input_layer)
base_model = model_name(weights='imagenet', include_top=False,
input_shape=input_size)(preprocessor)
avg = GlobalAveragePooling2D()(base_model)
feature_extractor = Model(inputs = input_layer, outputs = avg)
#Extract feature.
feature_maps = feature_extractor.predict(data, batch_size=64, verbose=1)
print('Feature maps shape: ', feature_maps.shape)
return feature_maps
# 使用# Extract features using InceptionV3 as extractor.
from keras.applications.inception_v3 import InceptionV3, preprocess_input
inception_preprocessor = preprocess_input
inception_features = get_features(InceptionV3,
inception_preprocessor,
img_size, X)
...
# 依次提取了InceptionV3、xception、NASNetLarge 、InceptionResNetV2 四个网络特征
# 将特征concate在一起,大小为(10222、9664)
final_features = np.concatenate([inception_features,
xception_features,
nasnet_features,
inc_resnet_features,], axis=-1)
print('Final feature maps shape', final_features.shape)
#Prepare DNN model
dnn = keras.models.Sequential([
InputLayer(final_features.shape[1:]),
Dropout(0.7),
Dense(n_classes, activation='softmax')
])
dnn.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
注意:测试时也同样需要提取并concate特征后再predict
3、top2方案总结
top2传送门
方案:和上面一样,只不过换了提取特征网络为InceptionV3、xception、NASNetLarge、InceptionResNetV2
4、top3方案总结
top3传送门
受到欺骗,方案和上面一样
5、top4方案总结
top4传送门
1、数据预处理及数据增强
2、构建模型:在InceptionResNetV2基础上,去除原fc层,pool后添加三层fc层
结果:train_acc=75.623%, train_loss=0.87319,val_acc=89.232%, val_loss=0.09762
norm_factor = 1 / 255
#
# Augmentation Ranges
transform_params = {
'featurewise_center': False,
'featurewise_std_normalization': False,
'samplewise_center': False,
'samplewise_std_normalization': False,
'rotation_range': 30,
'width_shift_range': 0.15,
'height_shift_range': 0.15,
'horizontal_flip': True,
'rescale': norm_factor
}
# the generator used for training - gives augmented images
img_gen = ImageDataGenerator(**transform_params)
值得借鉴的代码:将数据增强后的数据plot出来,及写一个plotter的class,将每个epoch的loss、acc等描绘出来
# a Fully connected layer with activation, batchnorm and dropout
def dense_block(x, neurons, layer_no):
x = Dense(neurons, kernel_initializer=he_normal(layer_no), name=f'topDense{layer_no}')(x)
x = Activation('relu', name=f'Relu{layer_no}')(x)
x = BatchNormalization(name=f'BatchNorm{layer_no}')(x)
x = Dropout(0.5, name=f'Dropout{layer_no}')(x)
return x
def create_model(shape):
input_layer = Input(shape, name='input_layer') # input layer with given shape
# load InceptionResNetV2 with initialized weights and remove final dense layers - frozen layers
incep_res = InceptionResNetV2(include_top=False, weights='imagenet', input_tensor=input_layer)
for layer in incep_res.layers:
layer.trainable = False
# pooling to reduce dimensionality of each feature map
pool = MaxPooling2D(pool_size=[3, 3], strides=[3, 3], padding='same')(incep_res.output)
flat1 = Flatten(name='Flatten1')(pool)
flat1_bn = BatchNormalization(name='BatchNormFlat')(flat1)
# dense layers after the InceptionResNetV2 initialized layers
dens1 = dense_block(flat1_bn, neurons=512, layer_no=1)
dens2 = dense_block(dens1, neurons=512, layer_no=2)
dens3 = dense_block(dens2, neurons=1024, layer_no=3)
dens_final = Dense(classes_num, name='Dense4')(dens3)
output_layer = Activation('softmax', name='Softmax')(dens_final)
model = Model(inputs=[input_layer], outputs=[output_layer])
return model
6、top5方案总结
top5传送门
使用fastai,resnet101网络及预训练
1、resnet101 baseline,acc为0.869
2、precompute+data augmentation, acc为0.889
3、扩大输入尺寸,acc为0.891