Skip to main content

transfer learning toolkits for finetune deep learning models

Project description

PaddleTransfer

Introduction

PaddleTransfer, a transfer learning tool of PaddlePaddle, provides a variety of algorithms to transfer the knowledge learned from source tasks to target task. It enables users to use the state-of-the-art transfer learning algorithms on main-stream model archtechtures.

PaddleTransfer provides various transfer learning algorithms, including MMD(JMLR'12),L2-SP(ICML'18), DELTA(ICLR'19), RIFLE(ICML'20), Co-Tuning(NIPS'20) and supports many main-stream model archtechtures, including ResNet, MobileNet and ViT. With several lines of codes, users can apply these algorithms on our predifined model or their own model easily.

Contents

Key Highlights

High Accuracy

PaddleTransfer provides various transfer learning algorithms, users can conveniently apply them on their models, and select the most appropriate one with high accuracy for further usage.

Easy to Use

PaddleTransfer provides unified API to invoke different algorithms, users can easily apply them on their models with several lines of codes.

Fully Support

PaddleTransfer supports most commonly used models including Resnet, MobileNet and ViT, and will iterates rapidly to support more model archtectures.

Installation

Install by pip

python -m pip install paddletransfer

Dependencies

if you want to use our package in your own code, the following dependencies are required

  • python >= 3.7
  • numpy >= 1.21
  • paddlepaddle >= 2.2 (with suitable CUDA and cuDNN version)

If you want to run our demo script, please make sure the following packages are installed correctly on your machine.

  • visualdl

Usage Guideline

Quick Start

Users can run our demo code for a quick start

python finetune.py --name [experiment_name] --train_dir [path_to_train_dir] --eval_dir [path_to_eval_dir] --model_arch [model_archtechture] --algo [finetune_algorithm] --gpu [device_for_experiment]

For model_arch argument, please choose one from "resnet18", "resnet34", "resnet50", "resnet101", "resnet152", "mobilenet_v2" and "vit", mistyping may lead to unexpected behavior.

If you want to finetune the ViT model, please make sure you have set the configuration file and pretrained parameters file correctly, and remember to add the corresponding argumengts(--cfg and --model_path) in your command. You can get the configuration file and pretrained model from PaddleVit.

Use PaddleTransfer in Your Own Code

Import dependencies

from paddletransfer.finetuning.img_cls import *

We strongly recommand you to use the model architechtures we provide in backbones by following codes. The parameters setting are the same as the implementations in paddle.vision.models, remember to set pretrained=True for finetuning.

from backbones import resnet18, resnet34, resnet50, resnet101, resnet152, mobilenet_v2, build_vit

You can only import the model archtechtures that you need.

If you want to use ViT model, please put the config.py file either from our project or PaddleVit under your working directory and set other files correctly as described in Quick Start

If you want to use your self-defined model, please make sure that the name of layers are consistent with the implementations in paddle.vision.models and your model has two outputs: The first one is the original output of the network and the second one is the features(the intermediate output before average pooling layer and fc layer).

Initialize the algorithm object

algo = FinetuneDELTA(model, model_arch)
  • To customize the hyperparameter for finetune algorithm, please add the following arguments to the initializer, for details about hyperparameter setting, please refer to provided algorithms
confs = confs # a directory of the hyperparameter setting

Get the parameters list which need to update and pass them to optimizer

params = algo.params()
...
optimizer = paddle.optimizer.Optimizer(...,parameters = params,...)

In most cases, it is equal to model.parameters() and you may not want to invoke this method. But remember to do this if you are using Co-tuning algorithm since it has extra parameters to update

Get the regularization loss and merge it to the classification loss

loss_cls = paddle.nn.CrossEntropyLoss(y_data,logits)
loss_all = {'loss_cls': loss_cls}
loss_reg = algo.loss(x_data, y_data, logits, features, epoch, batch_id)
loss_all.update(loss_reg)
loss = sum(loss_all.values())
...
loss.backward()

Provided Algorithms

So far we have provided 5 algorithms for finetune, which are MMD, L2-SP, DELTA, RIFLE and Co-Tuning. If you do not want to use any finetune algorithms, just use the following code for vanilla finetune, the corresponding code for invoking different algorithms are in the respective sections.

algo = FinetuneBASE(model,model_arch)

MMD

Use the following code for invoking MMD algorithm

algo = FinetuneMMD(model,model_arch,confs=_confs)

The default hyperparameters for MMD algorithms is as follows, if you want to modify them, please pass your own confs object to the initializer.

_confs = {'reg_weight': 0.1, 'kernel_mul': 2.0, 'kernel_num': 5}

L2-SP

Use the following code for invoking L2-SP algorithm

algo = FinetuneL2SP(model,model_arch,confs=_confs)

The default hyperparameters for L2SP algorithms is as follows, if you want to modify them, please pass your own confs object to the initializer.

_confs = {'reg_weight': 0.01}

DELTA

Use the following code for invoking DELTA algorithm

algo = FinetuneDELTA(model,model_arch,confs=_confs)

The default hyperparameters for DELTA algorithms is as follows, if you want to modify them, please pass your own confs object to the initializer.

_confs = {'reg_weight': 0.01}

RIFLE

Use the following code for invoking RIFLE algorithm

algo = FinetuneRIFLE(model,model_arch,confs=_confs)

The default hyperparameters for RIFLE algorithms is as follows, if you want to modify them, please pass your own confs object to the initializer.

_confs = {'reinit_epochs': [10,20,30]} # with total epochs = 40

Co-Tuning

Use the following code for invoking Co-Tuning algorithm. The data_loader is used for relationship learning.

algo = FinetuneCOT(model, model_arch, data_loader=data_loader, confs=_confs)

If you want to use Co-Tuning algorithm on ViT, use the following code. The vit_config is used for auxillary imagenet classifier construction. Please make sure that your vit_config object passed to the initializer has the same structure with the official implementation in PaddleVit, you can use the get_config() function they provide to generate one easily.

algo = FinetuneCOT(model, model_arch, data_loader=data_loader, vit_config=config, confs=_confs)

The default hyperparameters for Co-Tuning algorithms is as follows, if you want to modify them, please pass your own confs object to the initializer.

 _confs = {'reg_weight': 2.3}

Algorithm Performance

We have conducted some experiments on several dataset(CUB_200_2011, indoorCVPR_09 and dtd) using algorithms provided by PaddleTransfer. Most of experiments use the default hyper parameter setting in finetune.py, except Co-Tuning. For Co-Tuning, We use default hyper parameters on bird classification task, lr = 0.002 and wd = 0.0005 on scene classification task, lr = 0.001 and wd = 0.0005 on texture classification task. The outcomes are as follows.

Bird Scene Texture
BASE 80.25 77.34 62.07
MMD 80.41 78.27 63.15
L2SP 80.39 77.57 65.34
DELTA 81.04 78.79 67.47
RIFLE 80.74 78.08 63.15
CoTuning 81.07 78.00 69.39

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

paddletransfer-0.1.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

paddletransfer-0.1.0-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file paddletransfer-0.1.0.tar.gz.

File metadata

  • Download URL: paddletransfer-0.1.0.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for paddletransfer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6fae242e4ca426ed7c11ab5494f29cd97abf1711636172806042494e998f3a15
MD5 1ff73aea1f4db8e72ba7f6d0ec19a5d1
BLAKE2b-256 008f80b4a33978e1b977ae94bd4ba924efb630317a44f33ecf2739fca15d1291

See more details on using hashes here.

File details

Details for the file paddletransfer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for paddletransfer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fba0fca56e02848a8ec025f785697d38c4ad87775c91ddf06223ad378e1b7167
MD5 0fdbf136e206636f26848bf54069d97c
BLAKE2b-256 8a3d94a5c203ca3bf5e407342f439c0947151c84cdc6e0483ffb25db1ecd66e1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page