AlexNet Keras预训练模型

简介

因为Keras没有Alexnet预训练模型,我将Pytroch上的AlexNet预训练模型转成了Keras,供大家使用。因为AlexNet很简单,Keras和Pytroch代码都很容易理解,因此不做详细解释。

下载链接

链接: https://pan.baidu.com/s/1oAsVNANDdb8w4Lqw-1hIIA 提取码: y8xr

AlexNet代码-Keras

# import tensorflow as tf
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D

# import keras.backend.tensorflow_backend as K
# K.set_image_dim_ordering('th')
def AlexNet(num_classses=1000):
    model = Sequential()

    model.add(ZeroPadding2D((2, 2), input_shape=(227, 227, 3)))
    model.add(Convolution2D(64, (11, 11), strides=(4, 4), activation='relu'))
    model.add(MaxPooling2D((3, 3), strides=(2, 2)))

    model.add(ZeroPadding2D((2, 2)))
    model.add(Convolution2D(192, (5, 5), activation='relu'))
    model.add(MaxPooling2D((3, 3), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(384, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, (3, 3), activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, (3, 3), activation='relu'))
    model.add(MaxPooling2D((3, 3), strides=(2, 2)))

    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classses, activation='softmax'))

    return model

model = AlexNet(num_classses=1000)
print(model)
# model.save_weights('alexnet_weights.h5')
model.load_weights('alexnet_weights_pytorch.h5')
for layer in model.layers:
    for weight in layer.weights:
        print(weight.name, weight.shape)

AlexNet代码-Pytroch

import torch.nn as nn
import torch.utils.model_zoo as model_zoo


__all__ = ['AlexNet', 'alexnet']


model_urls = {
    'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
}


class AlexNet(nn.Module):

    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), 256 * 6 * 6)
        x = self.classifier(x)
        return x

验证参数

测试代码
import h5py
import torch

#读取pytorch的Alexnet模型参数
pthfile = '/Users/wanglei/Downloads/alexnet-owt-4df8aa71.pth'
model = torch.load(pthfile)
print(model['features.0.bias'][:10]) # conv2d_1的bias中64个值的前10个,便于好看

#读取keras的Alexnet模型参数
f = h5py.File('/Users/wanglei/Downloads/alexnet_weights_pytorch.h5', 'r')
subGroup1 = f.get('conv2d_1')
subGroup2 = subGroup1.get('conv2d_1')
print(subGroup2['bias:0'][:10])
测试结果

在这里插入图片描述

参考

https://github.com/pytorch/vision/blob/master/torchvision/models/alexnet.py

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值