Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#41 from LielinJiang/fix-bugs-from-qa
Browse files Browse the repository at this point in the history
release some image classification models
  • Loading branch information
LielinJiang authored Apr 16, 2020
2 parents 5ed8fa8 + ec0eba2 commit 8a312a9
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 67 deletions.
9 changes: 7 additions & 2 deletions examples/image_classification/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --arch
### 单卡预测
执行如下命令进行预测
```bash
python -u main.py --arch resnet50 -d --evaly-only /path/to/imagenet
python -u main.py --arch resnet50 -d --eval-only /path/to/imagenet
```

### 多卡预测
执行如下命令进行多卡预测
```bash
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --arch resnet50 --evaly-only /path/to/imagenet
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --arch resnet50 --eval-only /path/to/imagenet
```


Expand All @@ -71,12 +71,17 @@ CUDA_VISIBLE_DEVICES=0,1,2,3 python -m paddle.distributed.launch main.py --arch
* **weight-decay**: 模型权重正则化系数,默认值:1e-4
* **momentum**: SGD优化器的动量,默认值:0.9

注意:使用```--resume```恢复训练时,假如你的模型路径为```./output/118.pdparams```,你输入的路径不需要带后缀,即```--resume ./output/118```即可。

## 模型

| 模型 | top1 acc | top5 acc |
| --- | --- | --- |
| [ResNet18](https://paddle-hapi.bj.bcebos.com/models/resnet18.pdparams) | 71.72 | 90.60 |
| [ResNet34](https://paddle-hapi.bj.bcebos.com/models/resnet34.pdparams) | 75.02 | 92.31 |
| [ResNet50](https://paddle-hapi.bj.bcebos.com/models/resnet50.pdparams) | 76.27 | 93.03 |
| [ResNet101](https://paddle-hapi.bj.bcebos.com/models/resnet101.pdparams) | 78.33 | 94.04 |
| [ResNet152](https://paddle-hapi.bj.bcebos.com/models/resnet152.pdparams) | 78.78 | 94.40 |
| [vgg16](https://paddle-hapi.bj.bcebos.com/models/vgg16.pdparams) | 71.92 | 90.65 |
| [mobilenet_v1](https://paddle-hapi.bj.bcebos.com/models/mobilenet_v1_x1.0.pdparams) | 71.16 | 89.89 |
| [mobilenet_v2](https://paddle-hapi.bj.bcebos.com/models/mobilenet_v2_x1.0.pdparams) | 72.30 | 90.74 |
Expand Down
13 changes: 11 additions & 2 deletions examples/image_classification/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def main():
device = set_device(FLAGS.device)
fluid.enable_dygraph(device) if FLAGS.dynamic else None

model_list = [x for x in models.__dict__["__all__"]]
assert FLAGS.arch in model_list, "Expected FLAGS.arch in {}, but received {}".format(
model_list, FLAGS.arch)
model = models.__dict__[FLAGS.arch](pretrained=FLAGS.eval_only and
not FLAGS.resume)

Expand All @@ -94,7 +97,13 @@ def main():
len(train_dataset) * 1. / FLAGS.batch_size / ParallelEnv().nranks),
parameter_list=model.parameters())

model.prepare(optim, CrossEntropy(), Accuracy(topk=(1, 5)), inputs, labels)
model.prepare(
optim,
CrossEntropy(),
Accuracy(topk=(1, 5)),
inputs,
labels,
FLAGS.device)

if FLAGS.eval_only:
model.evaluate(
Expand Down Expand Up @@ -152,7 +161,7 @@ def main():
type=str,
help="checkpoint path to resume")
parser.add_argument(
"--eval-only", action='store_true', help="enable dygraph mode")
"--eval-only", action='store_true', help="only evaluate the model")
parser.add_argument(
"--lr-scheduler",
default='piecewise',
Expand Down
13 changes: 13 additions & 0 deletions examples/image_classification/scripts/mobilenet_v1_x1.0.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export CUDA_VISIBLE_DEVICES=0,1,2,3

# 默认imagenet数据存储在data/ILSVRC2012/下,去除-d便使用静态图模式运行
python -m paddle.distributed.launch main.py \
--arch mobilenet_v1 \
--epoch 120 \
--batch-size 64 \
--learning-rate 0.1 \
--lr-scheduler piecewise \
--milestones 30 60 90 \
--weight-decay 3e-5 \
-d \
data/ILSVRC2012/
12 changes: 12 additions & 0 deletions examples/image_classification/scripts/mobilenet_v2_x1.0.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export CUDA_VISIBLE_DEVICES=0,1,2,3

# 默认imagenet数据存储在data/ILSVRC2012/下,去除-d便使用静态图模式运行
python -m paddle.distributed.launch main.py \
--arch mobilenet_v2 \
--epoch 240 \
--batch-size 64 \
--learning-rate 0.1 \
--lr-scheduler cosine \
--weight-decay 4e-5 \
-d \
data/ILSVRC2012/
10 changes: 10 additions & 0 deletions examples/image_classification/scripts/resnet101.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export CUDA_VISIBLE_DEVICES=0,1,2,3

# 默认imagenet数据存储在data/ILSVRC2012/下,去除-d便使用静态图模式运行
python -m paddle.distributed.launch main.py \
--arch resnet101 \
--epoch 90 \
--batch-size 64 \
--learning-rate 0.1 \
-d \
data/ILSVRC2012/
10 changes: 10 additions & 0 deletions examples/image_classification/scripts/resnet152.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export CUDA_VISIBLE_DEVICES=0,1,2,3

# 默认imagenet数据存储在data/ILSVRC2012/下,去除-d便使用静态图模式运行
python -m paddle.distributed.launch main.py \
--arch resnet152 \
--epoch 90 \
--batch-size 64 \
--learning-rate 0.1 \
-d \
data/ILSVRC2012/
11 changes: 11 additions & 0 deletions examples/image_classification/scripts/resnet18.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export CUDA_VISIBLE_DEVICES=0,1,2,3

# 默认imagenet数据存储在data/ILSVRC2012/下,去除-d便使用静态图模式运行
python -m paddle.distributed.launch main.py \
--arch resnet18 \
--epoch 120 \
--batch-size 64 \
--learning-rate 0.1 \
--lr-scheduler cosine \
-d \
data/ILSVRC2012/
11 changes: 11 additions & 0 deletions examples/image_classification/scripts/resnet34.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export CUDA_VISIBLE_DEVICES=0,1,2,3

# 默认imagenet数据存储在data/ILSVRC2012/下,去除-d便使用静态图模式运行
python -m paddle.distributed.launch main.py \
--arch resnet34 \
--epoch 120 \
--batch-size 64 \
--learning-rate 0.1 \
--lr-scheduler cosine \
-d \
data/ILSVRC2012/
10 changes: 10 additions & 0 deletions examples/image_classification/scripts/resnet50.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export CUDA_VISIBLE_DEVICES=0,1,2,3

# 默认imagenet数据存储在data/ILSVRC2012/下,去除-d便使用静态图模式运行
python -m paddle.distributed.launch main.py \
--arch resnet50 \
--epoch 90 \
--batch-size 64 \
--learning-rate 0.1 \
-d \
data/ILSVRC2012/
11 changes: 11 additions & 0 deletions examples/image_classification/scripts/vgg16.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export CUDA_VISIBLE_DEVICES=0,1,2,3

# 默认imagenet数据存储在data/ILSVRC2012/下,去除-d便使用静态图模式运行
python -m paddle.distributed.launch main.py \
--arch vgg16 \
--epoch 90 \
--batch-size 64 \
--learning-rate 0.01 \
--lr-scheduler cosine \
-d \
data/ILSVRC2012/
50 changes: 27 additions & 23 deletions hapi/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def _check_match(key, param):
"{} receives a shape {}, but the expected shape is {}.".
format(key, list(state.shape), list(param.shape)))
return param, state

def _strip_postfix(path):
path, ext = os.path.splitext(path)
assert ext in ['', '.pdparams', '.pdopt', '.pdmodel'], \
Expand Down Expand Up @@ -936,35 +936,35 @@ def fit(
Args:
train_data (Dataset|DataLoader): An iterable data loader is used for
train. An instance of paddle paddle.io.Dataset or
paddle.io.Dataloader is recomended.
paddle.io.Dataloader is recomended. Default: None.
eval_data (Dataset|DataLoader): An iterable data loader is used for
evaluation at the end of epoch. If None, will not do evaluation.
An instance of paddle.io.Dataset or paddle.io.Dataloader
is recomended.
is recomended. Default: None.
batch_size (int): Integer number. The batch size of train_data and eval_data.
When train_data and eval_data are both the instance of Dataloader, this
parameter will be ignored.
epochs (int): Integer number. The number of epochs to train the model.
parameter will be ignored. Default: 1.
epochs (int): Integer number. The number of epochs to train the model. Default: 1.
eval_freq (int): The frequency, in number of epochs, an evalutation
is performed.
is performed. Default: 1.
log_freq (int): The frequency, in number of steps, the training logs
are printed.
are printed. Default: 10.
save_dir(str|None): The directory to save checkpoint during training.
If None, will not save checkpoint.
save_freq (int): The frequency, in number of epochs, to save checkpoint.
If None, will not save checkpoint. Default: None.
save_freq (int): The frequency, in number of epochs, to save checkpoint. Default: 1.
verbose (int): The verbosity mode, should be 0, 1, or 2.
0 = silent, 1 = progress bar, 2 = one line per epoch.
0 = silent, 1 = progress bar, 2 = one line per epoch. Default: 2.
drop_last (bool): whether drop the last incomplete batch of train_data
when dataset size is not divisible by the batch size. When train_data
is an instance of Dataloader, this parameter will be ignored.
is an instance of Dataloader, this parameter will be ignored. Default: False.
shuffle (bool): whther to shuffle train_data. When train_data is an instance
of Dataloader, this parameter will be ignored.
of Dataloader, this parameter will be ignored. Default: True.
num_workers (int): the number of subprocess to load data, 0 for no subprocess
used and loading data in main process. When train_data and eval_data are
both the instance of Dataloader, this parameter will be ignored.
both the instance of Dataloader, this parameter will be ignored. Default: 0.
callbacks (Callback|None): A list of `Callback` instances to apply
during training. If None, `ProgBarLogger` and `ModelCheckpoint`
are automatically inserted.
are automatically inserted. Default: None.
"""

assert train_data is not None, \
Expand Down Expand Up @@ -1066,18 +1066,20 @@ def evaluate(
evaluation. An instance of paddle.io.Dataset or
paddle.io.Dataloader is recomended.
batch_size (int): Integer number. The batch size of train_data and eval_data.
When train_data and eval_data are both the instance of Dataloader, this
parameter will be ignored.
When eval_data is the instance of Dataloader, this argument will be ignored.
Default: 1.
log_freq (int): The frequency, in number of steps, the eval logs
are printed.
are printed. Default: 10.
verbose (int): The verbosity mode, should be 0, 1, or 2.
0 = silent, 1 = progress bar, 2 = one line per epoch.
0 = silent, 1 = progress bar, 2 = one line per epoch. Default: 2.
num_workers (int): The number of subprocess to load data, 0 for no subprocess
used and loading data in main process. When train_data and eval_data are
both the instance of Dataloader, this parameter will be ignored.
both the instance of Dataloader, this parameter will be ignored. Default: 0.
callbacks (Callback|None): A list of `Callback` instances to apply
during training. If None, `ProgBarLogger` and `ModelCheckpoint`
are automatically inserted.
are automatically inserted. Default: None.
Returns:
dict: Result of metric.
"""

if fluid.in_dygraph_mode():
Expand Down Expand Up @@ -1142,16 +1144,18 @@ def predict(self,
is recomended.
batch_size (int): Integer number. The batch size of train_data and eval_data.
When train_data and eval_data are both the instance of Dataloader, this
parameter will be ignored.
argument will be ignored. Default: 1.
num_workers (int): the number of subprocess to load data, 0 for no subprocess
used and loading data in main process. When train_data and eval_data are
both the instance of Dataloader, this parameter will be ignored.
both the instance of Dataloader, this argument will be ignored. Default: 0.
stack_output (bool): whether stack output field like a batch, as for an output
filed of a sample is in shape [X, Y], test_data contains N samples, predict
output field will be in shape [N, X, Y] if stack_output is True, and will
be a length N list in shape [[X, Y], [X, Y], ....[X, Y]] if stack_outputs
is False. stack_outputs as False is used for LoDTensor output situation,
it is recommended set as True if outputs contains no LoDTensor. Default False
it is recommended set as True if outputs contains no LoDTensor. Default: False.
Returns:
list: output of models.
"""

if fluid.in_dygraph_mode():
Expand Down
7 changes: 4 additions & 3 deletions hapi/vision/models/mobilenetv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def forward(self, inputs):


def _mobilenet(arch, pretrained=False, **kwargs):
model = MobileNetV1(num_classes=1000, with_pool=True, **kwargs)
model = MobileNetV1(**kwargs)
if pretrained:
assert arch in model_urls, "{} model do not have a pretrained model now, you should set pretrained=False".format(
arch)
Expand All @@ -276,12 +276,13 @@ def _mobilenet(arch, pretrained=False, **kwargs):
return model


def mobilenet_v1(pretrained=False, scale=1.0):
def mobilenet_v1(pretrained=False, scale=1.0, **kwargs):
"""MobileNetV1
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False.
scale: (float): scale of channels in each layer. Default: 1.0.
"""
model = _mobilenet('mobilenetv1_' + str(scale), pretrained, scale=scale)
model = _mobilenet(
'mobilenetv1_' + str(scale), pretrained, scale=scale, **kwargs)
return model
7 changes: 4 additions & 3 deletions hapi/vision/models/mobilenetv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def forward(self, inputs):


def _mobilenet(arch, pretrained=False, **kwargs):
model = MobileNetV2(num_classes=1000, with_pool=True, **kwargs)
model = MobileNetV2(**kwargs)
if pretrained:
assert arch in model_urls, "{} model do not have a pretrained model now, you should set pretrained=False".format(
arch)
Expand All @@ -250,12 +250,13 @@ def _mobilenet(arch, pretrained=False, **kwargs):
return model


def mobilenet_v2(pretrained=False, scale=1.0):
def mobilenet_v2(pretrained=False, scale=1.0, **kwargs):
"""MobileNetV2
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False.
scale: (float): scale of channels in each layer. Default: 1.0.
"""
model = _mobilenet('mobilenetv2_' + str(scale), pretrained, scale=scale)
model = _mobilenet(
'mobilenetv2_' + str(scale), pretrained, scale=scale, **kwargs)
return model
Loading

0 comments on commit 8a312a9

Please sign in to comment.