pytorch 使用预训练的模型

import torch
import torchvision.models  as models
import MyDataLoader
from torch.autograd import Variable


alexnet = models.alexnet(pretrained = False)
alexnet.load_state_dict(torch.load('/home/alexa/Facial/alexnet-owt-4df8aa71.pth'))
#print(alexnet)
pre_dict = alexnet.state_dict()
#print((k,v) for k ,v in pre_dict.items()) 
for k ,v in pre_dict.items():  #打印模型每层命名
    print(k)


#pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
data_domain = "human"
USE_CUDA = True
train_dataset = MyDataLoader.ImgDataSet( "/home/%s_mini_train.csv" % data_domain, data_domain )
trainloader = torch.utils.data.DataLoader( train_dataset, batch_size = 32, shuffle = True )
for inputs, labels in trainloader:
    inputs = inputs.type(torch.FloatTensor)
    if USE_CUDA: 
        inputs = inputs.cuda()
        net = alexnet.cuda()
        
    inputs = Variable(inputs)
#    print(inputs)
    with torch.no_grad():
        out = alexnet(inputs)
    print(out.shape)

 

你可能感兴趣的:(pytorch)