microsoft / MMdnn

MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.
MIT License
5.78k stars 968 forks source link

mxnet to pytorch, 'NpzFile' object has no attribute 'item' #905

Closed Russell565 closed 3 years ago

Russell565 commented 3 years ago

Platform: win10

Python version: 3.8.3

Source framework with version: mxnet-cu102 1.6.0

Destination framework with version: pytorch 1.6.0

Pre-trained model path: link extraction code:x2vu

Running scripts: $ mmtoir -f mxnet -n model-symbol.json -w model-0000.params --inputShape 3,112,112 -o IRmxnet $ mmtocode -f pytorch -n IRmxnet.pb -w IRmxnet.npy -o IRpytorch.py -dw IRpytorch.npy $ mmtomodel -f pytorch -in IRpytorch.py -iw IRpytorch.npy -o Res_50.pth

I use these scripts to get the .pth but when i run the code:

def load_weights(weight_file): if weight_file == None: return

try:
    weights_dict = np.load(weight_file, allow_pickle=True).item()
except:
    weights_dict = np.load(weight_file, allow_pickle=True, encoding='bytes').item()

return weights_dict 

weight_file is the Res_50.pth , and then, i got this:

Traceback (most recent call last): File "D:\Program Files (x86)\JetBrains\PyCharm 2020.2.1\plugins\python\helpers\pydev\pydevd.py", line 1448, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "D:\Program Files (x86)\JetBrains\PyCharm 2020.2.1\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:\PythonProject\mycode\pytorch_test.py", line 16, in model = KitModel('./Res_50.pth').to(device) File "D:\PythonProject\mycode\IRpytorch.py", line 26, in init _weights_dict = load_weights(weight_file) File "D:\PythonProject\mycode\IRpytorch.py", line 16, in load_weights weights_dict = np.load(weight_file, allow_pickle=True, encoding='bytes').item() AttributeError: 'NpzFile' object has no attribute 'item'

please help me !

Russell565 commented 3 years ago

Platform: ubuntu18.04

Python version: 3.6.12

Source framework with version: mxnet-cu102 1.6.0

Destination framework with version: pytorch 1.3.1

Running scripts: $ mmtoir -f mxnet -n model-symbol.json -w model-0000.params --inputShape 3,112,112 -o IRmxnet $ mmtocode -f pytorch -n IRmxnet.pb -w IRmxnet.npy -o Resnet50.py -dw Resnet50.npy $ mmtomodel -f pytorch -in Resnet50.py -iw Resnet50.npy -o Resnet_50.pth

I changed my computer today. Now i got different error

this is my used scrpts to load .pth:

import torch from Resnet50 import KitModel

if name == 'main':

device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
# Load Model
model = KitModel('./Resnet_50.pth').to(device)

model.eval()

and this is the Resnet50.py

import numpy as np import torch import torch.nn as nn import torch.nn.functional as F import math

__weights_dict = dict()

def load_weights(weight_file): if weight_file == None: return

try:
    weights_dict = np.load(weight_file, allow_pickle=True).item()
except:
    weights_dict = np.load(weight_file, allow_pickle=True, encoding='bytes').item()

return weights_dict

class KitModel(nn.Module):

def __init__(self, weight_file):
    super(KitModel, self).__init__()
    global __weights_dict
    __weights_dict = load_weights(weight_file)
   '''network...'''
def forward(self, x):
  ''' forward '''

when program run the model = KitModel('./Resnet_50.pth').to(device), it will run into function load_weights. And this time, weights_dict = np.load(weight_file, allow_pickle=True).item() prompts for an error message

Traceback (most recent call last): File "/home/wwgz-cbm/pycharm-professional-2020.2.3/pycharm-2020.2.3/plugins/python/helpers/pydev/pydevd.py", line 1448, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/home/wwgz-cbm/pycharm-professional-2020.2.3/pycharm-2020.2.3/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/wwgz-cbm/PycharmProjects/pythonProject/mmdnn-test/pytorch_val.py", line 15, in model = KitModel('./Resnet_50.pth').to(device) File "/home/wwgz-cbm/PycharmProjects/pythonProject/mmdnn-test/Resnet50.py", line 26, in init __weights_dict = load_weights(weight_file) File "/home/wwgz-cbm/PycharmProjects/pythonProject/mmdnn-test/Resnet50.py", line 16, in load_weights weights_dict = np.load(weight_file, allow_pickle=True, encoding='bytes').item() AttributeError: 'int' object has no attribute 'item'

please help me !(There seems to be no relu or prelu in the the .pth file generated by mmdnn ?)

Russell565 commented 3 years ago

problem solved . loading model like #862