有,如何给您?
import numpy as np import cv2 import torch import os import torch_mlu.core.mlu_quantize as mlu_quantize import torch_mlu.core.mlu_model as ct import torch.nn as nn from nets.facenet import Facenet as facenet from torchsummary import summary from utils.utils import preprocess_input, resize_image from PIL import Image #device = torch.device("cpu") def predict(): input_shape = [160, 160, 3] image_1_path = '/data/daixiahu/facenet-pytorch/img/1_001.jpg' image_1 = Image.open(image_1_path) image_1 = resize_image(image_1, [input_shape[1], input_shape[0]],letterbox_image=True) photo_1 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_1, np.float32)), (2, 0, 1)), 0)) # 读取量化模型 model_net = facenet(backbone='inception_resnetv1', mode="predict") model_net.float().eval() qconfig = {'use_avg': False, 'data_scale': 1.0, 'firstconv': True, 'per_channel': False} quantized_net = mlu_quantize.quantize_dynamic_mlu(model_net, qconfig, dtype='int8') state_dict = torch.load("facenet_int8.pth") quantized_net.load_state_dict(state_dict, strict=False) quantized_net.eval() #res = quantized_net(photo_1) #print(res) device = ct.mlu_device() quantized_net.to(device) # 模型融合 photo_1 = photo_1.to(ct.mlu_device()) #quantized_net.eval().float().to(ct.mlu_device()) #quantized_net = torch.jit.trace(quantized_net, input_mlu_data, check_trace=False) quantized_net.to(ct.mlu_device()) ct.save_as_cambricon('facenet_model_int8') torch.set_grad_enabled(False) ct.set_core_number(4) # 模型融合 trace_input = torch.randn(1, 3, 160, 160, dtype=torch.float32) input_mlu_data = trace_input.to(ct.mlu_device()) quantized_net = torch.jit.trace(quantized_net, input_mlu_data, check_trace=False) print('wwww') #print(quantized_net) #pred = quantized_net(photo_1).data.cpu().type(torch.FloatTensor) #print(pred) if __name__ == "__main__": predict()
请登录后评论