|
pytorch转caffe成功之后的验证
思路:用同一张图片作为输入,经过相同的预处理
各自模型的前向计算,比较输出的结果是否相同?
- import torch
- import numpy as np
- from config import get_config_lite
- from Learner import face_learner
- import torch.nn.functional as F
- from torch.nn import CrossEntropyLoss
- from torchvision import transforms as trans
- import os
- import cv2
- os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
- # 选择哪一块gpu,如果是-1,就是调用cpu
- os.environ['CUDA_VISIBLE_DEVICES'] = "0"
- # 看同一张图片 输入后 torch与caffe的输出的512维度向量
- img_path = "/home/jingyun/py2_caffe_face_works/onnx2caffe-master/model/10.jpg"
- # conf = get_config_dfy(training=False)
- conf = get_config_lite(training=False)
- # conf = get_config_dfy(training=False)
- print("easydict的配置参数:", conf)
- learner = face_learner(conf, inference=True)
- fixed_str = "2020-08-09-11-00_accuracy:0.9094341157427415_step:1324780_final.pth"
- learner.load_state(conf, fixed_str, model_only=True, from_save_folder=True)
- learner.model.eval()
- test_transform = trans.Compose([
- trans.ToTensor(),
- trans.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
- ])
- img = cv2.imread(img_path)
- print("cv2.imread之后:", img.shape)
- # 图片的预处理
- img = test_transform(img)
- print("torch预处理后:", img.size())
- img = torch.unsqueeze(img, dim=0)
- print("torch 扩展维度后:", img.size(), type(img))
- with torch.no_grad():
- result = learner.model(img.to(conf.device))
- print("result_512:", result.size())
- result = result.cpu().numpy()[0]
- print(type(result), result.shape, result)
- # result是否归一化了: 25.143478
- print("result是否归一化了:", np.linalg.norm(result))
- # numpy l2_norm()的实现
- result = np.divide(result, np.linalg.norm(result))
- print(result)
- # torch的结果:
- # 4.18923460e-02 -4.78874184e-02 -2.89320014e-02 -5.47427386e-02
- # -2.47906484e-02 2.65899673e-03 -5.64723015e-02 -1.81384571e-02
- # -5.86921759e-02 -1.66410357e-02 5.65116517e-02 -5.67275733e-02
- # 2.84908544e-02 -3.57359536e-02 -2.56096441e-02 -1.55471719e-03
- # caffe的结果:
- # 4.18923870e-02 -4.78875041e-02 -2.89320182e-02 -5.47428243e-02
- # -2.47906558e-02 2.65889615e-03 -5.64723164e-02 -1.81384161e-02
- # -5.86921349e-02 -1.66409668e-02 5.65117151e-02 -5.67275919e-02
- # 2.84908172e-02 -3.57360132e-02 -2.56096572e-02 -1.55465060e-03]
复制代码
- # -*- coding: utf-8 -*-
- from __future__ import print_function, division
- __author__ = u'东方耀 微信:dfy_88888'
- __date__ = '2020/8/10 上午10:34'
- __product__ = 'PyCharm'
- __filename__ = 'caffe_inference_extract_512_feature'
- import caffe
- import os
- import cv2
- import numpy as np
- os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
- # 选择哪一块gpu,如果是-1,就是调用cpu
- os.environ['CUDA_VISIBLE_DEVICES'] = "0"
- deploy = "./caffe_model/FaceRecog_dfy06-sim.prototxt"
- trained_model = "./caffe_model/FaceRecog_dfy06-sim.caffemodel"
- caffe.set_mode_gpu()
- caffe.set_device(0)
- net_caffe = caffe.Net(deploy, 1, weights=trained_model)
- # 看同一张图片 输入后 torch与caffe的输出的512维度向量
- img_path = "/home/jingyun/py2_caffe_face_works/onnx2caffe-master/model/10.jpg"
- img = cv2.imread(img_path)
- print("cv2.imread after", img.shape)
- # brg (0-255)
- # test_transform = trans.Compose([
- # trans.ToTensor(),
- # trans.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
- # ])
- # 图片的预处理
- img2 = img.copy()
- # img = test_transform(img)
- img2 = img2 / 255.0
- img2 = (img2 - 0.5) / 0.5
- img2 = np.transpose(img2, axes=[2, 0, 1])
- # cv2.imread after (112, 112, 3)
- # torch预处理后: (3, 112, 112)
- # print("torch预处理后:", img.size(), img)
- # [-0.7333, -0.8588, -0.8824, ..., -0.2863, 0.4196, 0.8588],
- # [-0.7176, -0.8431, -0.8745, ..., -0.3569, 0.3569, 0.8588],
- # [-0.7176, -0.8431, -0.8745, ..., -0.4667, 0.2471, 0.8196],
- # ...,
- # [ 0.5765, 0.5765, 0.5529, ..., 0.6941, 0.7020, 0.7176],
- # [ 0.5765, 0.5686, 0.5529, ..., 0.7020, 0.7020, 0.7255],
- # [ 0.5686, 0.5686, 0.5529, ..., 0.7020, 0.7098, 0.7255]
- img2 = np.expand_dims(img2, axis=0)
- print("numpy预处理后:", img2.shape)
- # img = torch.unsqueeze(img, dim=0)
- # print("torch 扩展维度后:", img.size(), type(img))
- net_caffe.blobs["input0"].data[...] = img2
- result = net_caffe.forward()['output0'][0]
- print("result_caffe:", result.shape, type(result))
- # result是否归一化了: 25.14348
- print("result是否归一化了:", np.linalg.norm(result))
- result = np.divide(result, np.linalg.norm(result))
- print(result)
- # 4.18923870e-02 -4.78875041e-02 -2.89320182e-02 -5.47428243e-02
- # -2.47906558e-02 2.65889615e-03 -5.64723164e-02 -1.81384161e-02
- # -5.86921349e-02 -1.66409668e-02 5.65117151e-02 -5.67275919e-02
- # 2.84908172e-02 -3.57360132e-02 -2.56096572e-02 -1.55465060e-03]
复制代码
|
|