tensorlayer / TensorLayer

Deep Learning and Reinforcement Learning Library for Scientists and Engineers
http://tensorlayerx.com
Other
7.33k stars 1.61k forks source link

Can not load pertrained weights in VGG19 #1022

Closed se7enXF closed 5 years ago

se7enXF commented 5 years ago

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

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 commented 5 years ago

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!

se7enXF commented 5 years ago

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.

rinabell commented 5 years ago

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 commented 5 years ago

@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: