Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Add example for EfficientNet and upgrade PyTorch in Dockerfile #1497

Merged
merged 14 commits into from
Sep 9, 2019
1 change: 1 addition & 0 deletions examples/trials/efficientnet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EfficientNet-PyTorch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EfficientNet-PyTorch is with Apache license and cannot be included into our repo. So user has to clone this repo ahead. Add it to gitignore to prevent this directory to be included in our repo.

33 changes: 33 additions & 0 deletions examples/trials/efficientnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# EfficientNet

[arXiv](https://arxiv.org/abs/1905.11946)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please change arxiv to other name such as the name of this paper?


We provide here:

* Search space and tuners for finding the best tuple (alpha, beta, gamma) for EfficientNet-B1 with
grid search, as discussed in Section 3.3 in [paper](https://arxiv.org/abs/1905.11946).
* Search space for EfficientNet transfer learning on datasets other than ImageNet;
settings are borrowed from Appendix A.5 from
"[Do Better ImageNet Models Transfer Better?](https://arxiv.org/abs/1805.08974)".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the work in second paper (https://arxiv.org/abs/1805.08974) is not specific to EfficientNet. What we provided in this example for 2nd paper is just learning rate search (which we already have a lot of examples).
Can we remove the second part? so that this exmaple will more concise and clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2nd paper is cited by EfficientNet, for the settings of transfer learning. And yes, it's not important in the big picture. The main reason I show this here is to provide an example on how to use transfer learning with EfficientNet-PyTorch. I'm fine with removing this part. What about @QuanluZhang ?

## Instructions

1. Clone our modified version of [EfficientNet-PyTorch](https://github.com/ultmaster/EfficientNet-PyTorch) in this directory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between the modified version and the original version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documented

2. Resolve all the dependencies following the README.
3. Run `nnictl create --config config_lr.yml` if you want to do transfer learning;
or `nnictl create --config config_net.yml` if you want to find the best EfficientNet-B1.
Adjust the training service (PAI/local/remote), batch size in the config files according to your environment.

For training on ImageNet, you should read `EfficientNet-PyTorch/train_imagenet.sh`. Download ImageNet beforehand
and extract it adhering to [PyTorch format](https://pytorch.org/docs/stable/torchvision/datasets.html#imagenet)
and then replace `/mnt/data/imagenet` in with the location of your ImageNet storage. This file should also be a good example
in case you want to mount ImageNet into the container on OpenPAI.

*Backward compatibility instructions: Grid Search quniform is changed in NNI v1.0. Use `search_net_old.json` if you are
still using NNI <= 0.9.*

## Results

Our experiments show the relationship between acc@1 and alpha, beta, gamma.

![](assets/search_result.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions examples/trials/efficientnet/config_lr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
authorName: unknown
experimentName: lr_efficient_net
trialConcurrency: 4
maxExecDuration: 48h
maxTrialNum: 140
trainingServicePlatform: pai
searchSpacePath: search_lr.json
useAnnotation: false
tuner:
builtinTunerName: GridSearch
trial:
codeDir: EfficientNet-PyTorch
command: python3 main.py cifar10 -j 4 --epochs 20 -a efficientnet-b0 --pretrained --num-classes 10 --batch-size 64 --optimizer sgd --request-from-nni
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this code(main.py)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is in EfficientNet-PyTorch repo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make clear in docs..

cpuNum: 4
memoryMB: 16384
gpuNum: 1
shmMB: 8192
virtualCluster: nni
image: ultmaster/nni:latest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use the image from nni?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, PyTorch in NNI official image needs to be upgraded to at least v1.1; and nfs-common and cifs-utils need to be installed. Refer to Dockerfile for all the changes I've done to make it work.

nniManagerIp: <nni_manager_ip>
paiConfig:
userName: <username>
passWord: <password>
host: <host>
28 changes: 28 additions & 0 deletions examples/trials/efficientnet/config_net.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
authorName: unknown
experimentName: example_efficient_net
trialConcurrency: 8
maxExecDuration: 48h
maxTrialNum: 100
trainingServicePlatform: pai
searchSpacePath: search_net.json
useAnnotation: false
tuner:
codeDir: .
classFileName: tuner.py
className: FixedProductTuner
classArgs:
product: 2
trial:
codeDir: EfficientNet-PyTorch
command: sh train_imagenet.sh
cpuNum: 4
memoryMB: 25000
shmMB: 25000
gpuNum: 1
virtualCluster: nni
image: ultmaster/nni:latest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

nniManagerIp: <nni_manager_ip>
paiConfig:
userName: <username>
passWord: <password>
host: <host>
10 changes: 10 additions & 0 deletions examples/trials/efficientnet/search_lr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"lr": {
"_type": "qloguniform",
"_value": [0.00001, 0.1, 7]
},
"wd": {
"_type": "qloguniform",
"_value": [0.000001, 0.001, 7]
}
}
14 changes: 14 additions & 0 deletions examples/trials/efficientnet/search_net.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"alpha": {
"_type": "quniform",
"_value": [1.0, 2.0, 0.1]
},
"beta": {
"_type": "quniform",
"_value": [1.0, 1.5, 0.1]
},
"gamma": {
"_type": "quniform",
"_value": [1.0, 1.5, 0.1]
}
}
14 changes: 14 additions & 0 deletions examples/trials/efficientnet/search_net_old.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"alpha": {
"_type": "quniform",
"_value": [1.0, 2.0, 11]
},
"beta": {
"_type": "quniform",
"_value": [1.0, 1.5, 11]
},
"gamma": {
"_type": "quniform",
"_value": [1.0, 1.5, 11]
}
}
19 changes: 19 additions & 0 deletions examples/trials/efficientnet/tuner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from nni.gridsearch_tuner.gridsearch_tuner import GridSearchTuner


class FixedProductTuner(GridSearchTuner):

def __init__(self, product):
super().__init__()
self.product = product

def expand_parameters(self, para):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better adding some docstring here?

para = super().expand_parameters(para)
if all([key in para[0] for key in ["alpha", "beta", "gamma"]]):
ret_para = []
for p in para:
prod = p["alpha"] * (p["beta"] ** 2) * (p["gamma"] ** 2)
if abs(prod - self.product) < 0.1:
ret_para.append(p)
return ret_para
return para