OverFeat keras代码实现
对模型结构有疑问详见原论文
# -*- coding: utf-8 -*-
"""
Created on 2022/8/16 14:49
@author: Troye Jcan
"""
import os
os.environ['CUDA_VISIBLE_DEVICES'] = "1" # use GPU 0
import keras
from keras.layers import Conv2D, Dense, MaxPooling2D, Flatten
# define a overfeat model
def overfeat_model(model_type='accurate'):
if model_type == 'accurate':
# accurate model
model = keras.models.Sequential()
model.add(Conv2D(input_shape=(221, 221, 3), filters=96, kernel_size=7, strides=2, padding='valid',
activation='relu', kernel_initializer='uniform'))
model.add(MaxPooling2D(pool_size=3, strides=3))
model.add(Conv2D(filters=256, kernel_size=7, strides=1, kernel_initializer='uniform'))
model.add(MaxPooling2D(pool_size=2, strides=2))
model.add(Conv2D(filters=512, kernel_size=3, strides=1, padding='same', kernel_initializer='uniform'))
model.add(Conv2D(filters=512, kernel_size=3, strides=1, padding='same', kernel_initializer='uniform'))
model.add(Conv2D(filters=1024, kernel_size=3, strides=1, padding='same', kernel_initializer='uniform'))
model.add(Conv2D(filters=1024, kernel_size=3, strides=1, padding='same', kernel_initializer='uniform'))
model.add(MaxPooling2D(pool_size=3, strides=3))
model.add(Conv2D(filters=4096, kernel_size=5, strides=1, kernel_initializer='uniform'))
model.add(Conv2D(filters=4096, kernel_size=1, strides=1, kernel_initializer='uniform'))
model.add(Conv2D(filters=1000, kernel_size=1, strides=1, kernel_initializer='uniform'))
model = keras.models.Model(inputs=model.input, outputs=model.output)
else:
# fast model
model = keras.models.Sequential()
model.add(Conv2D(input_shape=(231, 231, 3), filters=96, kernel_size=11, strides=4))
model.add(MaxPooling2D(pool_size=2, strides=2))
model.add(Conv2D(filters=256, kernel_size=5, strides=1))
model.add(MaxPooling2D(pool_size=2, strides=2))
model.add(Conv2D(filters=512, kernel_size=3, strides=1, padding='same'))
model.add(Conv2D(filters=1024, kernel_size=3, strides=1, padding='same'))
model.add(Conv2D(filters=1024, kernel_size=3, strides=1, padding='same'))
model.add(MaxPooling2D(pool_size=2, strides=2))
model.add(Conv2D(filters=3072, kernel_size=(6, 6), strides=1))
model.add(Conv2D(filters=4096, kernel_size=1, strides=1))
model.add(Conv2D(filters=1000, kernel_size=1, strides=1))
model = keras.models.Model(inputs=model.input, outputs=model.output)
return model
overfeat = overfeat_model('accurate')
overfeat.summary()
OverFeat推荐博客阅读:
https://www.cnblogs.com/liaohuiqiang/p/9348276.html
https://yufeigan.github.io/2014/11/23/OverFeat-Integrated-Recognition-Localization-and-Detection-using-Convolutional-Networks/