一、pth2caffe
Net:定义的网络结构,pth存储的模型路径
def pth2caffe(Net,pth):from pytorch2caffe import pytorch2caffename = 'yolov3_2'model = Net#将权重加载到网络中model_dict = model.state_dict()pretrained_dict = torch.load(pth, map_location=torch.device('cpu'))pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)}model_dict.update(pretrained_dict)model.load_state_dict(model_dict)model.eval()#定义转换格式后的输入数据大小dummy_input = torch.ones([1, 3, 40, 40])pytorch2caffe.trans_net(model, dummy_input, name)pytorch2caffe.save_prototxt('{}.prototxt'.format(name))pytorch2caffe.save_caffemodel('{}.caffemodel'.format(name))
二、测试模型生成
import numpy as npimport torchimport torch.nn as nn#定义网络结构class YoloBody(nn.Module):def __init__(self, anchors_mask, num_classes):super(YoloBody, self).__init__()self.last_layer0 = nn.Conv2d(3, len(anchors_mask[0]) * (num_classes + 5), kernel_size=1, stride=1)self.last_layer1 = nn.Conv2d(3, len(anchors_mask[0]) * (num_classes + 5), kernel_size=1, stride=1)self.last_layer2 = nn.Conv2d(3, len(anchors_mask[0]) * (num_classes + 5), kernel_size=1, stride=1)def forward(self, x):out0 = self.last_layer0(x)out1 = self.last_layer1(x)out2 = self.last_layer2(x)return out0, out1, out2#网络参数初始化def weights_init(net, init_type='kaiming', init_gain = 0.02):def init_func(m):classname = m.__class__.__name__if hasattr(m, 'weight') and classname.find('Conv') != -1:if init_type == 'normal':torch.nn.init.normal_(m.weight.data, 0.0, init_gain)elif init_type == 'xavier':torch.nn.init.xavier_normal_(m.weight.data, gain=init_gain)elif init_type == 'kaiming':torch.nn.init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')elif init_type == 'orthogonal':torch.nn.init.orthogonal_(m.weight.data, gain=init_gain)else:raise NotImplementedError('initialization method [%s] is not implemented' % init_type)elif classname.find('BatchNorm2d') != -1:torch.nn.init.normal_(m.weight.data, 1.0, 0.02)torch.nn.init.constant_(m.bias.data, 0.0)print('initialize network with %s type' % init_type)net.apply(init_func)def pth2caffe(Net,pth):from pytorch2caffe import pytorch2caffename = 'yolov3_2'model = Netmodel_dict = model.state_dict()pretrained_dict = torch.load(pth, map_location=torch.device('cpu'))pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)}model_dict.update(pretrained_dict)model.load_state_dict(model_dict)model.eval()#转换格式后的输入dummy_input = torch.ones([1, 3, 40, 40])pytorch2caffe.trans_net(model, dummy_input, name)pytorch2caffe.save_prototxt('{}.prototxt'.format(name))pytorch2caffe.save_caffemodel('{}.caffemodel'.format(name))if __name__ == "__main__":anchors_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]#网络结构定义Net = YoloBody(anchors_mask,20)#自定义模型路径modelpath = "xx/xx/yolo.pth"weights_init(Net)Net.train()#保存pytorch模型torch.save(Net.state_dict(),modelpath)#转换格式pth2caffe(Net,modelpath)
