tensorlayer / TensorLayer

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

πŸš€πŸš€ Support network architecture that can be easily use #367

Closed auroua closed 6 years ago

auroua commented 6 years ago

Could you support the network architecture like vgg, resnet easily to use. If I want to use resnet in Pytorch, I only need to write the following two lines code:

from torchvision import models
model_ft = models.resnet18(pretrained=True)

But in tensorlayer, I have to define the network architecture by myself and then load the pretrained parameters.

zsdonghao commented 6 years ago

πŸ†• Hi, we support this with tl.models now , see here

================== Hi, TL provides' SlimNetsLayer that can use all Google TF-Slim's pre-tained model here. This is an example for Inception V3 (click)

zsdonghao commented 6 years ago

@luomai @lgarithm

I am thinking to release an API that help people import any type of model without knowing anything about slim or other resources.

Something like:

Any idea about how to design the API ?

lgarithm commented 6 years ago

Shall we also provide the following syntax sugar if it is possible to do so in python?

net = tl.models["inceptionv3"][:-1]
net = tl.models["inceptionv3"]["conv3_2"]
zsdonghao commented 6 years ago

related discussion: https://github.com/tensorlayer/tensorlayer/issues/380

auroua commented 6 years ago

I have implemented Resent in tensorlayer which can be created by

nets = get_resnet(x, 1000, 50, sess)

The code can load the tf-slim pre-trained ckpt file, but I found the inference results have some differences with the tf-slim model. The code can be found here.

luomai commented 6 years ago

@zsdonghao The model API can allow model specific parameter as follows:

net = tl.models.inceptionv3(**args)  # import inception v3 model with args

The API shall also allow "partially reusing the model based on layers" for transfer learning, for example.

net = tl.models.inceptionv3(**args)[:-1]  # reuse model and remove last layer
net = tl.models.inceptionv3(**args)[`conv3_2`]  # reuse model until conv3_2 layer
lgarithm commented 6 years ago

451

zsdonghao commented 6 years ago

Usage examples for image classification models

Classify ImageNet classes with VGG16

x = tf.placeholder(tf.float32, [None, 224, 224, 3])

# get the whole model
vgg = tl.models.VGG16(x)

# restore pre-trained VGG parameters
vgg.restore_params(sess)

# use pre-trained VGG
softmax = tf.nn.softmax(vgg.outputs)
...

Extract features with VGG16 and Train a classifier with 100 classes

x = tf.placeholder(tf.float32, [None, 224, 224, 3])

# get VGG without the last layer
x = tf.placeholder(tf.float32, [None, 224, 224, 3])

# get VGG without the last layer
vgg = tl.models.VGG16(x, end_with='fc2_relu')

# add one more layer
net = tl.layers.DenseLayer(vgg, 100, name='out')

# initialize all parameters
sess = tf.InteractiveSession()
tl.layers.initialize_global_variables(sess)

# restore pre-trained VGG parameters
vgg.restore_params(sess)

# train your own classifier (only update the last layer)
train_params = tl.layers.get_variables_with_name('out')
…

Reuse model

x1 = tf.placeholder(tf.float32, [None, 224, 224, 3])

x2 = tf.placeholder(tf.float32, [None, 224, 224, 3])

# get VGG without the last layer
vgg1 = tl.models.VGG16(x1, end_with='fc2_relu')

# reuse parameters of vgg1, but use different input 
vgg2 = tl.models.VGG16(x2, end_with='fc2_relu', reuse=True)

# restore pre-trained VGG parameters (as they share parameters, don’t need to restore vgg2)
sess = tf.InteractiveSession()
vgg1.restore_params(sess)

ref: https://keras.io/applications/#applications

zsdonghao commented 6 years ago

Other well-known models (TODO) 🌺

TensorLayer already have some implementations, just wrap them up!

then provide examples in documentation along with the APIs, and tutorial_models_xxx.py in here

zsdonghao commented 6 years ago

https://github.com/tensorlayer/tensorlayer/issues/394

We can save the model structure into TL graph, and the parameters into TL npz file.

TF .ckpt --> TL .npz
TF .pb --> TL graph

lgarithm commented 6 years ago

what kind of data format will be used for TL graph?

lgarithm commented 6 years ago

We might need to use pickle if we want to restore the network from saved file.

zsdonghao commented 6 years ago

Release SqueezeNet Model

https://github.com/tensorlayer/tensorlayer/pull/461 tl.models.SqueezeNetV1 which uses ConcatLayer @lgarithm

zsdonghao commented 6 years ago

Release MobileNetV1 Model

https://github.com/tensorlayer/tensorlayer/pull/465 tl.models.MobileNetV1 which uses DepthwiseConv2d @lgarithm

zsdonghao commented 6 years ago

Reference Slim Endpoint

final_endpoint https://github.com/tensorflow/models/blob/master/research/slim/nets/inception_resnet_v2.py#L125

zsdonghao commented 6 years ago

@luomai TODO list : https://github.com/tensorlayer/pretrained-models/blob/master/README.md

zsdonghao commented 6 years ago

hi, I close this issue, feel free to reopen it if you want to discuss.