Pytorch預訓練模型

Pytorch預訓練模型

Pytorch支持預訓練的模型如下

From torchvision package:

  • ResNet (resnet18resnet34resnet50resnet101resnet152)
  • DenseNet (densenet121densenet169densenet201densenet161)
  • Inception v3 (inception_v3)
  • VGG (vgg11vgg11_bnvgg13vgg13_bnvgg16vgg16_bnvgg19vgg19_bn)
  • SqueezeNet (squeezenet1_0squeezenet1_1)
  • AlexNet (alexnet)

From Pretrained models for PyTorch package:

  • ResNeXt (resnext101_32x4dresnext101_64x4d)
  • NASNet-A Large (nasnetalarge)
  • NASNet-A Mobile (nasnetamobile)
  • Inception-ResNet v2 (inceptionresnetv2)
  • Dual Path Networks (dpn68dpn68bdpn92dpn98dpn131dpn107)
  • Inception v4 (inception_v4)
  • Xception (xception)
  • Squeeze-and-Excitation Networks (senet154se_resnet50se_resnet101se_resnet152se_resnext50_32x4dse_resnext101_32x4d)
  • PNASNet-5-Large (pnasnet5large)
  • PolyNet (polynet)

Pytorch預訓練運行環境

  • Python 3.5+
  • PyTorch 0.3+

安裝方法

pip install cnn_finetune

用例

from cnn_finetune import make_model
from torch import optim
model = make_model(model_name='resnext101_64x4d', num_classes=10, pretrained=True)
# optimizer
optimizer = optim.SGD(model.parameters(), lr=0.0001)

導入包,將make_model中的num_classes改成你項目中分類類別數。如果你選擇的model是VGG系列或者其他需要設置固定輸入格式大小的模型還會有input_size需要設置。需要將輸入通過cv.resize完成或者特徵提取成相應大小。

簡單的完整示例

"""
!/usr/bin/env python
-*- coding:utf-8 -*-
Author: eric.lai
Created on 2019/7/22 9:48
"""
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
from cnn_finetune import make_model
from torch import optim
import torch.nn as nn
import torch

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
model = make_model(model_name='resnext101_64x4d', num_classes=10, pretrained=True)
# optimizer
optimizer = optim.SGD(model.parameters(), lr=0.0001)

def load_data():
    model.train()
    criterion = nn.MSELoss() # CrossEntropyLoss does not expect a one-hot encoded vector as the target
    total_loss = 0
    total_size = 0

    for i in range(100):
        optimizer.zero_grad()
        batch_xs, batch_ys = mnist.train.next_batch(10)
        batch_xs = list(batch_xs)*3
        batch_xs = np.array(batch_xs)
        batch_xs = np.reshape(batch_xs, [-1, 3, 28, 28])
        batch_xs = torch.from_numpy(batch_xs)
        batch_ys = torch.from_numpy(batch_ys)
        output = model(batch_xs)
        loss = criterion(output,target=batch_ys.float())
        total_loss += loss.item()
        total_size += batch_xs.shape[0]
        loss.backward()
        optimizer.step()
        print(loss)
        test()

def test():
    model.eval()
    with torch.no_grad():
        batch_xs, batch_ys = mnist.test.next_batch(100)
        batch_xs = list(batch_xs) * 3
        batch_xs = np.array(batch_xs)
        batch_xs = np.reshape(batch_xs, [-1, 3, 28, 28])
        batch_xs = torch.from_numpy(batch_xs)
        output = model(batch_xs)
        correct = list(np.equal(np.argmax(output, axis=1),np.argmax(batch_ys, axis=1)))
        nums = correct.count(1)
    print('test accuracy: ', nums / len(batch_xs))
    return output

if __name__ == '__main__':
    load_data()

需要注意的幾點:

1.Pytorch是channel_first,在reshape的時候要把通道數放在第一個位置

2.輸入的數據要通過torch_from_numpy轉換成Tensor

參考來源

https://github.com/Laicheng0830/pytorch-cnn-finetune#from-torchvision-package

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章