pytorch转换onnx,再转换caffe 测试caffe, pytorch 模型结果是否一致


def pytorch_out(input):
    model = model_res() #model.eval
    # input = input.cuda()
    # model.cuda()
    torch.no_grad()
    t1 = time.time()
    output = model(input)
    print("一张图片torch 模型前向运行时间",time.time()-t1)
    # print output[0].flatten()[70:80]
    return output

def caffe_output(input,input_layer_name="data",out_layer_name="fc1"):
    import sys
    # sys.path.insert(0, "/nfs-data/xingwg/deep_learning/NVCaffe/python")   #python3.6
    sys.path.insert(0, "/home/shiyy/nas/NVCaffe/python")  #python2.7
    import caffe
    caffe.set_device(0)

    deploy = "./transform_model/succed_res50.prototxt"
    weight = "./transform_model/succed_res50.caffemodel"
    caffe_model = caffe.Net(deploy, weight, caffe.TEST)
    # reshape network inputs
    blobs = {}
    blobs["0"] = input.data.numpy() #add dict

    t1=time.time()
    caffe_model.blobs[input_layer_name].reshape(*blobs["0"].shape)  #'input.1'  prototxt  name
    print("caffe 模型测试一张图片前向时间:",time.time()-t1)

    # do forward
    forward_kwargs = {input_layer_name: blobs['0'].astype(np.float32, copy=False)}  #input data name
    output_blobs = caffe_model.forward_all(**forward_kwargs)

    return output_blobs[out_layer_name]

def pytorch_caffe_test():
    # 测试数据
    torch.manual_seed(66)
    dummy_input = torch.randn(1, 3, 112, 112, device='cpu')

    print("================>")
    caffe_out = caffe_output(dummy_input)
    print(caffe_out)
    print(caffe_out.shape)

    print("================>")
    torch_out_res = pytorch_out(dummy_input).detach().numpy()
    print(torch_out_res)
    print(torch_out_res.shape)

    print("===================================>")
    print("输出结果验证小数点后五位是否正确,都变成一维np")
    torch_out_res = torch_out_res.flatten()
    caffe_out = caffe_out.flatten()

    pytor = np.array(torch_out_res,dtype="float32") #need to float32
    caff=np.array(caffe_out,dtype="float32")  ##need to float32
    np.testing.assert_almost_equal(pytor,caff, decimal=5)
    print("恭喜你 ^^,caffe 和 pytorch结果一致, Exported model has been executed decimal=5 and the result looks good!")
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值