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

Can not load pertrained weights in VGG19 #1022

Closed
2 tasks done
se7enXF opened this issue Jul 17, 2019 · 4 comments
Closed
2 tasks done

Can not load pertrained weights in VGG19 #1022

se7enXF opened this issue Jul 17, 2019 · 4 comments

Comments

@se7enXF
Copy link

se7enXF commented Jul 17, 2019

New Issue Checklist

Issue Description

When I use VGG19 model with pretrained weights, some errors arose. Maybe it is mismatching of
numpy and tensorlayer2 that cause the problem. The weight download process works well.

Reproducible Code

  • OS:Windows10
  • Python3.6
  • numpy1.16.4
  • tensorlayer2.1.0
import tensorlayer as tl
cnn = tl.models.vgg19(pretrained=True, end_with='conv5_4')

Here is the error:

Traceback (most recent call last):
  File "D:/TAM_Net/model.py", line 197, in <module>
    test = THZoom([None, image_size, image_size, 3])
  File "D:/TAM_Net/model.py", line 181, in __init__
    self.encoder = tl.models.vgg19(pretrained=True, end_with='conv5_4', mode='static')
  File "C:\Users\dell\Miniconda3\envs\tf2\lib\site-packages\tensorlayer\models\vgg.py", line 317, in vgg19
    restore_model(model, layer_type='vgg19')
  File "C:\Users\dell\Miniconda3\envs\tf2\lib\site-packages\tensorlayer\models\vgg.py", line 169, in restore_model
    npz = np.load(os.path.join('models', model_saved_name[layer_type]), encoding='latin1').item()
  File "C:\Users\dell\Miniconda3\envs\tf2\lib\site-packages\numpy\lib\npyio.py", line 447, in load
    pickle_kwargs=pickle_kwargs)
  File "C:\Users\dell\Miniconda3\envs\tf2\lib\site-packages\numpy\lib\format.py", line 696, in read_array
    raise ValueError("Object arrays cannot be loaded when "
ValueError: Object arrays cannot be loaded when allow_pickle=False
@JingqingZ
Copy link
Member

This issue is mentioned by #1019 and has been fixed in PR#1021. Please pull the latest TensorLayer code and modify your code accordingly. Thanks!

@JingqingZ JingqingZ reopened this Jul 17, 2019
@se7enXF
Copy link
Author

se7enXF commented Jul 18, 2019

I use pip installing the TL2 and the version is the latest 2.1.0.
I edit the TF2 source code in python with 'allow_pickle=True' and it works well.
Hope you can add some more useful model in TL, such as ResNet, YOLO. TL is very useful and thank you.

@se7enXF se7enXF closed this as completed Jul 18, 2019
@rinabell
Copy link

same problem. and my version is the latest 2.1.1.
but what puzzles me is when I ran codes in tensorlayer/examples/pretrained_cnn/, tutorial_models_vgg16.py worked prefect and tutorial_models_vgg19.py did't.
I guess it is the problem of tensorlayer/tensorlayer/models/vgg.py, cauz http://www.cs.toronto.edu/~frossard/post/vgg16/ can be acceessed and link to vgg19 cannot.
maybe change the link to a more stable one will help.
also hope codes in tensorlayer/examples/ could show how to load local vgg*.npz.
thanks a lot.

@se7enXF
Copy link
Author

se7enXF commented Jul 29, 2019

@rinabell

About vgg16 and vgg19

The pretrained weights of VGG16 and VGG19 are in different encodings which causes the different loading code in TL source code.

    if layer_type == 'vgg16':
        npz = np.load(os.path.join('models', model_saved_name[layer_type]))
        ...
    elif layer_type == 'vgg19':
        # npz = np.load(os.path.join('models', model_saved_name[layer_type]), encoding='latin1').item()
        npz = np.load(os.path.join('models', model_saved_name[layer_type]), encoding='latin1', allow_pickle=True).item()
        ...

Now I think you know why VGG16 works well. If you have downloaded the pretrained weights of VGG19 but failed to loading, please change the TL code tensorlayer/models/vgg.py in python site-packages as shown upper.

About downloading error

If you are in China might cause some TIMEOUT error. I suggest you to try more because I did it and I am in China.
To load the local vgg*.npz, you can find out by reading the source code and here is my submission:

  • The pertrained *.npz will be downloaded into $PROJECT_DIR/models.
  • Make a directory in your project root directory and download the pretrained *.npz into it. (Make sure that your project root is python execute root)
  • The TL code will load *.npz from $PROJECT_DIR/models

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

3 participants