Torch.load() raise an error

Hi, when i run the following code, an error raises. Can anybody help? Thanks!

my code:

if len(args.test_model) != 0:
    assert os.path.exists(args.test_model), \
        "file not found: {}".format(args.test_model)

    checkpoint_ = torch.load(args.test_model)
    # FIXME
    key_m, key_u = net.load_state_dict(checkpoint_['model'], strict=False)

    if key_u:
        print('Unexpected keys :')

    if key_m:
        print('Missing keys :')
        raise KeyError

Traceback (most recent call last):
File “/home4/user_from_home1/wangyufei/dc/dsd/”, line 625, in
File “/home4/user_from_home1/wangyufei/dc/dsd/”, line 621, in main
File “/home4/user_from_home1/wangyufei/dc/dsd/”, line 516, in test
checkpoint_ = torch.load(args.test_model)
File “/home1/wangyufei/anaconda3/envs/py3.8bindtr/lib/python3.8/site-packages/torch/”, line 585, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File “/home1/wangyufei/anaconda3/envs/py3.8bindtr/lib/python3.8/site-packages/torch/”, line 765, in _legacy_load
result = unpickler.load()
ModuleNotFoundError: No module named ‘metrics’

The recommended way to serialize a model would be to store its state_dict as described here. Based on the error you are seeing it seems you’ve stored the entire model via, path). If that’s the case, you would have to make sure the code structure while loading the model is equal to the one used during the saving.

Thanks for your reply!
The point in your reply may be a case that caused the problem. But I found that I got the above error because I defined a metrics package that has same name with built-in package. Add from future import absolute_import can solve it.