Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pretrained-model loading with errors #29

Closed
Ao-Lee opened this issue May 30, 2022 · 8 comments
Closed

pretrained-model loading with errors #29

Ao-Lee opened this issue May 30, 2022 · 8 comments

Comments

@Ao-Lee
Copy link

Ao-Lee commented May 30, 2022

Hello,
I used single GPU env with python == 3.8, torch==1.8.1 and torchvision==0.9.1
I followed the github hint with the following command:

python3 infer.py --gpu --image demo_image/demo_2.jpg --model vitstr_small_patch16_224.pth

It returned an error with

AttributeError: 'collections.OrderedDict' object has no attribute 'to'

it seems that the function model = torch.load(checkpoint) in infer.py returns an ordered dict instead of the model object.
One way to solve the problem is:

ordered_dict = torch.load(checkpoint)
model.load(ordered_dict )

But I do not know the hyper params of vitstr_small_patch16_224.pth when it is training. so it is very hard form me to initialize the model object with correct hyper params.
I would like to ask would it possible to may the hyper params of the pretrained models public?

I also tried the pt models

python3 infer.py --gpu --image demo_image/demo_2.jpg --model vitstr_small_patch16_jit.pt

it gives the following error:

  File "E:\ProgramFiles\anaconda3\envs\vitstr\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "e:\projects\deep-text-recognition-benchmark-master\infer.py", line 147, in <module>
    data = infer(args)

  File "e:\projects\deep-text-recognition-benchmark-master\infer.py", line 121, in infer
    model = torch.load(checkpoint)

  File "E:\ProgramFiles\anaconda3\envs\vitstr\lib\site-packages\torch\serialization.py", line 591, in load
    return torch.jit.load(opened_file)

  File "E:\ProgramFiles\anaconda3\envs\vitstr\lib\site-packages\torch\jit\_serialization.py", line 163, in load
    cpp_module = torch._C.import_ir_module_from_buffer(

RuntimeError: 
Unknown type name 'NoneType':
Serialized   File "code/__torch__/modules/vitstr.py", line 12
  embed_dim : int
  num_tokens : int
  dist_token : NoneType
               ~~~~~~~~ <--- HERE
  head_dist : NoneType
  patch_embed : __torch__.timm.models.layers.patch_embed.PatchEmbed

any way to load the model correctly please?
may thanks

@roatienza
Copy link
Owner

roatienza commented May 30, 2022

I just tried. Everything seems fine. Both on cpu and gpu inference.

(base) rowel@atienza-G190-G30:\~/github/roatienza/deep-text-recognition-benchmark$ python3 infer.py --image demo_image/demo_2.jpg --model https://github.com/roatienza/deep-text-recognition-benchmark/releases/download/v0.1.0/vitstr_small_patch16_jit.pt
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 82.1M/82.1M [00:01<00:00, 84.0MB/s]
/home/rowel/anaconda3/lib/python3.7/site-packages/torch/serialization.py:709: UserWarning: 'torch.load' received a zip file that looks like a TorchScript archive dispatching to 'torch.jit.load' (call 'torch.jit.load' directly to silence this warning)
  " silence this warning)", UserWarning)
demo_image/demo_2.jpg   :  SHAKESHACK
(base) rowel@atienza-G190-G30:\~/github/roatienza/deep-text-recognition-benchmark$ python3 infer.py --image demo_image/demo_2.jpg --model vitstr_small_patch16_jit.pt
/home/rowel/anaconda3/lib/python3.7/site-packages/torch/serialization.py:709: UserWarning: 'torch.load' received a zip file that looks like a TorchScript archive dispatching to 'torch.jit.load' (call 'torch.jit.load' directly to silence this warning)
  " silence this warning)", UserWarning)
demo_image/demo_2.jpg   :  SHAKESHACK
(base) rowel@atienza-G190-G30:\~/github/roatienza/deep-text-recognition-benchmark$ python3
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
1.11.0+cu113
>>> 
(base) rowel@atienza-G190-G30:\~/github/roatienza/deep-text-recognition-benchmark$ python3 infer.py --image demo_image/demo_2.jpg --gpu --model vitstr_small_patch16_jit.pt
/home/rowel/anaconda3/lib/python3.7/site-packages/torch/serialization.py:709: UserWarning: 'torch.load' received a zip file that looks like a TorchScript archive dispatching to 'torch.jit.load' (call 'torch.jit.load' directly to silence this warning)
  " silence this warning)", UserWarning)
demo_image/demo_2.jpg   :  SHAKESHACK

@Ao-Lee
Copy link
Author

Ao-Lee commented May 30, 2022

thx. I can update torch from 1.8 to 1.11.0 and try again

@Ao-Lee
Copy link
Author

Ao-Lee commented May 30, 2022

would kindly like to ask if requirements.txt can be update to your current environment setting please?

@Ao-Lee
Copy link
Author

Ao-Lee commented May 31, 2022

all problems are solved with pytorch 1.11.0, many thanks, plz close this issue

@raisinbl
Copy link

@Ao-Lee

it seems that the function model = torch.load(checkpoint) in infer.py returns an ordered dict instead of the model object.
One way to solve the problem is:

ordered_dict = torch.load(checkpoint)  
model.load(ordered_dict )

How can you create model in infer.py. Can you show me how to that? I want to infer from my checkpoint but I was stuck there
Thank you so much.

@roatienza
Copy link
Owner

In test.py,

# https://pytorch.org/tutorials/beginner/saving_loading_models.html

It is triggered by --infer-model option. The pytorch doc for JIT is also indicated.

@raisinbl
Copy link

In test.py,

# https://pytorch.org/tutorials/beginner/saving_loading_models.html

It is triggered by --infer-model option. The pytorch doc for JIT is also indicated.

Thank you so much! My problem is solved

@siddagra
Copy link

siddagra commented Jul 9, 2022

Should probably update requirements.txt to torch=1.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants