切换版块
×
基础软件平台
PyTorch开发
TensorFlow开发
编解码及SDK开发
BANG语言与计算库
开发工具链
MagicMind开发
软件栈百科
云平台集成
硬件产品专区
MLU370系列AI加速卡
MLU270系列AI加速卡
MLU220系列AI加速产品
经验方案交流区
经验案例与实践分享
开发者服务
开发者活动
公告与版务
高校支持
《智能计算系统》
签到
版块
社区
文档
SDK下载
370系列
200系列
开发平台
官网首页
注册
登录
全部版块
基础软件平台
硬件产品专区
经验方案交流区
开发者服务
高校支持
发布新帖
登录/注册
LV.1
大大大鸡扒
20
积分
1
赞
2
帖子
12
回复
1
收藏
TA的动态
TA的帖子
TA的回复
运行融合模式出错
我的回复:#3YukSing回复检查过代码,mlu在线逐层模式可以运行,在线融合就出错。解决了吗?我也遇见了同样的问题
0
torch.jit.trace生成离线模型报错
我的回复:希望官方大大能看过来,帮我解决这个问题
0
torch.jit.trace生成离线模型报错
我的回复:import osimport sysimport argparseimport numpy as npfrom PIL import Imageimport torchimport torch.nn as nnimport torch.nn.functional as Fimport torchvision.transforms as transformsimport torch_mluimport torch_mlu.core.mlu_quantize as mlu_quantizeimport torch_mlu.core.mlu_model as ctfrom src.models.modnet import MODNetif __name__ == '__main__': # define cmd arguments parser = argparse.ArgumentParser() parser.add_argument('--input-path', type=str,default='./input-img', help='path of input images') parser.add_argument('--output-path', type=str, default='./output-img', help='path of output images') parser.add_argument('--ckpt-path', type=str, default='./exportmodel/modnet_people_8bit.pth', help='path of pre-trained MODNet') args = parser.parse_args() # check input arguments if not os.path.exists(args.input_path): print('Cannot find input path: {0}'.format(args.input_path)) exit() if not os.path.exists(args.output_path): print('Cannot find output path: {0}'.format(args.output_path)) exit() if not os.path.exists(args.ckpt_path): print('Cannot find ckpt path: {0}'.format(args.ckpt_path)) exit() # define hyper-parameters ref_size = 512 # define image to tensor transform im_transform = transforms.Compose( [ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ] ) # create MODNet and load the pre-trained ckpt modnet = MODNet(backbone_pretrained=False) modnet = nn.DataParallel(modnet) qconfig = {'use_avg': False, 'data_scale': 1.0, 'firstconv': False, 'per_channel': False} modnet = mlu_quantize.quantize_dynamic_mlu(modnet.float(), qconfig_spec=qconfig, dtype='int8') modnet.load_state_dict(torch.load(args.ckpt_path)) modnet.eval() #torch.save(modnet.state_dict(), "./exportmodel/modnet_people_unzip.pth", _use_new_zipfile_serialization=False) #modnet.eval() # 注:程序中的数字仅表示某张输入图片尺寸,如1080x1440,此处只为记住其转换过程。 # inference images device = ct.mlu_device() modnet.to(device) ct.save_as_cambricon('modnet_people_cambricon') torch.set_grad_enabled(False) ct.set_core_number(1) trace_input = torch.randn(1, 3, 512, 512, dtype=torch.float32) input_mlu_data = trace_input.to(ct.mlu_device()) quantized_net = torch.jit.trace(modnet, input_mlu_data, check_trace=False) print('LXY---------------------=========================================') im_names = os.listdir(args.input_path) for im_name in im_names: print('Process image: {0}'.format(im_name)) # read image im = Image.open(os.path.join(args.input_path, im_name)) # unify image channels to 3 im = np.asarray(im) if len(im.shape) == 2: im = im[:, :, None] if im.shape[2] == 1: im = np.repeat(im, 3, axis=2) elif im.shape[2] == 4: im = im[:, :, 0:3] im_org = im # 保存numpy原始数组 (1080,1440,3) # convert image to PyTorch tensor im = Image.fromarray(im) im = im_transform(im) # add mini-batch dim im = im[None, :, :, :] # resize image for input im_b, im_c, im_h, im_w = im.shape if max(im_h, im_w) < ref_size or min(im_h, im_w) > ref_size: if im_w >= im_h: im_rh = ref_size im_rw = int(im_w / im_h * ref_size) elif im_w < im_h: im_rw = ref_size im_rh = int(im_h / im_w * ref_size) else: im_rh = im_h im_rw = im_w im_rw = im_rw - im_rw % 32 im_rh = im_rh - im_rh % 32 im = F.interpolate(im, size=(im_rh, im_rw), mode='area') #im.to(torch.device("cpu")) im = im.to(ct.mlu_device()) print(type(im)) #print('LXY---------------------=========================================') _, _, matte = quantized_net(im) # inference #_, _, matte = modnet(im.cuda(), True) # 从模型获得的 matte ([1,1,512, 672]) # resize and save matte,foreground picture matte = F.interpolate(matte, size=(im_h, im_w), mode='area') # 内插,扩展到([1,1,1080,1440]) 范围[0,1] matte = matte[0][0].data.cpu().numpy() # torch 张量转换成numpy (1080, 1440) matte_name = im_name.split('.')[0] + '_matte.png' Image.fromarray(((matte * 255).astype('uint8')), mode='L').save(os.path.join(args.output_path, matte_name)) matte_org = np.repeat(np.asarray(matte)[:, :, None], 3, axis=2) # 扩展到 (1080, 1440, 3) 以便和im_org计算 foreground = im_org * matte_org + np.full(im_org.shape, 255) * (1 - matte_org) # 计算前景,获得抠像 fg_name = im_name.split('.')[0] + '_fg.png' Image.fromarray(((foreground).astype('uint8')), mode='RGB').save(os.path.join(args.output_path, fg_name))
0
torch.jit.trace生成离线模型报错
我的回复:报错信息如上
0
生成MLU离线模型没有保存模型
我的回复:#4zhangyanting回复你好,有没有包括依赖框架在内的完整代码,以及你使用的facenet_int8.pth?我好复现一下问题。有,如何给您?
1
生成MLU离线模型没有保存模型
我的回复:我把模型中的normalize层删除后没有报错了,但是模型跑完quantized_net = torch.jit.trace(quantized_net, input_mlu_data, check_trace=False)还是没有保存,并且在推理阶段报错
1
生成MLU离线模型没有保存模型
我的回复:并且在生成离线模型后进行推理时报错。模型输出为128维度特征向量。
1
生成MLU离线模型没有保存模型
我的回复: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()
1
Github
开发平台
文档中心
新手必读
官方微信
版权所有 © 2024 寒武纪 Cambricon.com 备案/许可证号:
京ICP备17003415号-1
关闭