【环境搭建】cambricon测试

# -*- coding: utf-8 -*-
import torch_mlu
import torch_mlu.core.mlu_model as ct
import torch_mlu.core.mlu_quantize as mlu_quantize
import torch
import numpy as np
import torchvision.transforms as transforms
from torchvision.models.resnet import resnet50
from PIL import Image

torch.set_grad_enabled(False)

def get_torch_image(path, resize=256, crop=224, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
    transform = transforms.Compose([
        transforms.Resize(resize),
        transforms.CenterCrop(crop),
        transforms.ToTensor(),
        transforms.Normalize(mean=mean, std=std)
    ])
    img_mat = Image.open(path)
    img_mat = transform(img_mat)
    im_tensor = torch.unsqueeze(img_mat, 0)
    return im_tensor


if __name__ == '__main__':
    net = resnet50(pretrained=True)
    net.eval().float()

    im_tensor = get_torch_image("./imagenet/3.jpg")
    outputs = net(im_tensor).detach().numpy().reshape(-1)
    classify = np.argmax(outputs)
    print(classify)

    # combricon quant
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    qconfig = {"iteration": 1, "use_avg": False, "data_scale": 1.0, "firstconv": True, "mean": mean, "std": std,
               "per_channel": False}
    quantized_net = torch_mlu.core.mlu_quantize.quantize_dynamic_mlu(net.eval().float(), qconfig_spec=qconfig,
                                                                     dtype="int8", gen_quant=True)
    quantized_net = quantized_net.eval().float()  # model -> model.eval
    im_tensor = get_torch_image("./imagenet/3.jpg")
    outputs = quantized_net(im_tensor).detach().numpy().reshape(-1)
    classify = np.argmax(outputs)
    print(classify)

    # combricon quant save combricon
    mean = [0, 0, 0]
    std = [1 / 255, 1 / 255, 1 / 255]
    torch.save(quantized_net.state_dict(), "resnet50_quant.pth")
    state_dict = torch.load("resnet50_quant.pth")
    quantized_net = torch_mlu.core.mlu_quantize.quantize_dynamic_mlu(net)
    quantized_net.load_state_dict(state_dict)
    quantized_net_mlu = quantized_net.to(ct.mlu_device())

    ct.set_core_number(1)
    ct.set_core_version("MLU220")
    ct.save_as_cambricon("resnet50_1batch_1core") # 设置保存路径

    im_tensor = get_torch_image("imagenet/3.jpg", mean=mean, std=std)
    im_tensor = im_tensor.to(ct.mlu_device())
    net_traced = torch.jit.trace(quantized_net_mlu, im_tensor, check_trace=False)

    outputs = net_traced(im_tensor).cpu().detach().numpy().reshape(-1)
    classify = np.argmax(outputs)
    print(classify)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值