提交 289c8539 编写于 作者: W wangmeng28

Merge remote-tracking branch 'upstream/develop' into xception

......@@ -98,13 +98,16 @@ PaddlePaddle提供了丰富的运算单元,帮助大家以模块化的方式
图像相比文字能够提供更加生动、容易理解及更具艺术感的信息,是人们转递与交换信息的重要来源。图像分类是根据图像的语义信息对不同类别图像进行区分,是计算机视觉中重要的基础问题,也是图像检测、图像分割、物体跟踪、行为分析等其他高层视觉任务的基础,在许多领域都有着广泛的应用。如:安防领域的人脸识别和智能视频分析等,交通领域的交通场景识别,互联网领域基于内容的图像检索和相册自动归类,医学领域的图像识别等。
在图像分类任务中,我们向大家介绍如何训练AlexNet、VGG、GoogLeNet、ResNet和Xception模型。同时提供了一个够将Caffe训练好的模型文件转换为PaddlePaddle模型文件的模型转换工具。
在图像分类任务中,我们向大家介绍如何训练AlexNet、VGG、GoogLeNet、ResNet、Inception-v4、Inception-Resnet-V2和Xception模型。同时提供了能够将Caffe或TensorFlow训练好的模型文件转换为PaddlePaddle模型文件的模型转换工具。
- 11.1 [将Caffe模型文件转换为PaddlePaddle模型文件](https://github.com/PaddlePaddle/models/tree/develop/image_classification/caffe2paddle)
- 11.2 [AlexNet](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.3 [VGG](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.4 [Residual Network](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.5 [Xception](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.2 [将TensorFlow模型文件转换为PaddlePaddle模型文件](https://github.com/PaddlePaddle/models/tree/develop/image_classification/tf2paddle)
- 11.3 [AlexNet](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.4 [VGG](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.5 [Residual Network](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.6 [Inception-v4](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.7 [Inception-Resnet-V2](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 11.8 [Xception](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
## 12. 目标检测
......
......@@ -72,12 +72,15 @@ As an example for sequence-to-sequence learning, we take the machine translation
## 9. Image classification
For the example of image classification, we show you how to train AlexNet, VGG, GoogLeNet, ResNet and Xception models in PaddlePaddle. It also provides a model conversion tool that converts Caffe trained model files into PaddlePaddle model files.
For the example of image classification, we show you how to train AlexNet, VGG, GoogLeNet, ResNet, Inception-v4, Inception-Resnet-V2 and Xception models in PaddlePaddle. It also provides model conversion tools that convert Caffe or TensorFlow trained model files into PaddlePaddle model files.
- 9.1 [convert Caffe model file to PaddlePaddle model file](https://github.com/PaddlePaddle/models/tree/develop/image_classification/caffe2paddle)
- 9.2 [AlexNet](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.3 [VGG](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.4 [Residual Network](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.5 [Xception](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.2 [convert TensorFlow model file to PaddlePaddle model file](https://github.com/PaddlePaddle/models/tree/develop/image_classification/tf2paddle)
- 9.3 [AlexNet](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.4 [VGG](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.5 [Residual Network](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.6 [Inception-v4](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.7 [Inception-Resnet-V2](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
- 9.8 [Xception](https://github.com/PaddlePaddle/models/tree/develop/image_classification)
This tutorial is contributed by [PaddlePaddle](https://github.com/PaddlePaddle/Paddle) and licensed under the [Apache-2.0 license](LICENSE).
......@@ -278,7 +278,7 @@ Pairwise Rank复用上面的DNN结构,同一个source对两个target求相似
- `dnn_dims` 模型各层的维度设置,默认为 `256,128,64,32`,即模型有4层,各层维度如上设置
## 使用训练好的模型预测
详细命令行参数请执行 `python train.py --help`进行查阅。重要参数解释如下:
详细命令行参数请执行 `python infer.py --help`进行查阅。重要参数解释如下:
- `data_path` 需要预测的数据路径
- `prediction_output_path` 预测的输出路径
......
图像分类
=======================
这里将介绍如何在PaddlePaddle下使用AlexNet、VGG、GoogLeNet、ResNet和Xception模型进行图像分类。图像分类问题的描述和这些模型的介绍可以参考[PaddlePaddle book](https://github.com/PaddlePaddle/book/tree/develop/03.image_classification)
这里将介绍如何在PaddlePaddle下使用AlexNet、VGG、GoogLeNet、ResNet、Inception-v4、Inception-ResNet-v2和Xception模型进行图像分类。图像分类问题的描述和这些模型的介绍可以参考[PaddlePaddle book](https://github.com/PaddlePaddle/book/tree/develop/03.image_classification)
## 训练模型
......@@ -11,6 +11,8 @@
```python
import gzip
import argparse
import paddle.v2.dataset.flowers as flowers
import paddle.v2 as paddle
import reader
......@@ -18,6 +20,9 @@ import vgg
import resnet
import alexnet
import googlenet
import inception_v4
import inception_resnet_v2
import xception
# PaddlePaddle init
......@@ -29,6 +34,7 @@ paddle.init(use_gpu=False, trainer_count=1)
设置算法参数(如数据维度、类别数目和batch size等参数),定义数据输入层`image`和类别标签`lbl`
```python
# Use 3 * 331 * 331 or 3 * 299 * 299 for DATA_DIM in Inception-ResNet-v2.
DATA_DIM = 3 * 224 * 224
CLASS_DIM = 102
BATCH_SIZE = 128
......@@ -41,7 +47,7 @@ lbl = paddle.layer.data(
### 获得所用模型
这里可以选择使用AlexNet、VGG、GoogLeNet和ResNet模型中的一个模型进行图像分类。通过调用相应的方法可以获得网络最后的Softmax层。
这里可以选择使用AlexNet、VGG、GoogLeNet、ResNet、Inception-v4、Inception-ResNet-v2和Xception模型中的一个模型进行图像分类。通过调用相应的方法可以获得网络最后的Softmax层。
1. 使用AlexNet模型
......@@ -86,7 +92,27 @@ ResNet模型可以通过下面的代码获取:
out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM)
```
5. 使用Xception模型
5. 使用Inception-v4模型
Inception-v4模型可以通过下面的代码获取, 本例中使用的模型输入大小为`3 * 224 * 224` (原文献中使用的输入大小为`3 * 299 * 299`):
```python
out = inception_v4.inception_v4(image, class_dim=CLASS_DIM)
```
6. 使用Inception-ResNet-v2模型
提供的Inception-ResNet-v2模型支持`3 * 331 * 331``3 * 299 * 299`两种大小的输入,同时可以自行设置dropout概率,可以通过如下的代码使用:
```python
out = inception_resnet_v2.inception_resnet_v2(
image, class_dim=CLASS_DIM, dropout_rate=0.5, size=DATA_DIM)
```
注意,由于和其他几种模型输入大小不同,若配合提供的`reader.py`使用Inception-ResNet-v2时请先将`reader.py``paddle.image.simple_transform`中的参数为修改为相应大小。
7. 使用Xception模型
Xception模型可以通过下面的代码获取:
......@@ -181,7 +207,7 @@ def event_handler(event):
### 定义训练方法
对于AlexNet、VGG和ResNet,可以按下面的代码定义训练方法:
对于AlexNet、VGG、ResNet、Inception-v4、Inception-ResNet-v2和Xception,可以按下面的代码定义训练方法:
```python
# Create trainer
......
import paddle.v2 as paddle
def conv_bn_layer(input,
ch_out,
filter_size,
stride,
padding=0,
active_type=paddle.activation.Relu(),
ch_in=None):
"""layer wrapper assembling convolution and batchnorm layer"""
tmp = paddle.layer.img_conv(
input=input,
filter_size=filter_size,
num_channels=ch_in,
num_filters=ch_out,
stride=stride,
padding=padding,
act=paddle.activation.Linear(),
bias_attr=False)
return paddle.layer.batch_norm(input=tmp, epsilon=0.001, act=active_type)
def sequential_block(input, *layers):
"""helper function for sequential layers"""
for layer in layers:
layer_func, layer_conf = layer
input = layer_func(input, **layer_conf)
return input
def mixed_5b_block(input):
branch0 = conv_bn_layer(
input, ch_in=192, ch_out=96, filter_size=1, stride=1)
branch1 = sequential_block(input, (conv_bn_layer, {
"ch_in": 192,
"ch_out": 48,
"filter_size": 1,
"stride": 1
}), (conv_bn_layer, {
"ch_in": 48,
"ch_out": 64,
"filter_size": 5,
"stride": 1,
"padding": 2
}))
branch2 = sequential_block(input, (conv_bn_layer, {
"ch_in": 192,
"ch_out": 64,
"filter_size": 1,
"stride": 1
}), (conv_bn_layer, {
"ch_in": 64,
"ch_out": 96,
"filter_size": 3,
"stride": 1,
"padding": 1
}), (conv_bn_layer, {
"ch_in": 96,
"ch_out": 96,
"filter_size": 3,
"stride": 1,
"padding": 1
}))
branch3 = sequential_block(
input,
(paddle.layer.img_pool, {
"pool_size": 3,
"stride": 1,
"padding": 1,
"pool_type": paddle.pooling.Avg(),
"exclude_mode": False
}),
(conv_bn_layer, {
"ch_in": 192,
"ch_out": 64,
"filter_size": 1,
"stride": 1
}), )
out = paddle.layer.concat(input=[branch0, branch1, branch2, branch3])
return out
def block35(input, scale=1.0):
branch0 = conv_bn_layer(
input, ch_in=320, ch_out=32, filter_size=1, stride=1)
branch1 = sequential_block(input, (conv_bn_layer, {
"ch_in": 320,
"ch_out": 32,
"filter_size": 1,
"stride": 1
}), (conv_bn_layer, {
"ch_in": 32,
"ch_out": 32,
"filter_size": 3,
"stride": 1,
"padding": 1
}))
branch2 = sequential_block(input, (conv_bn_layer, {
"ch_in": 320,
"ch_out": 32,
"filter_size": 1,
"stride": 1
}), (conv_bn_layer, {
"ch_in": 32,
"ch_out": 48,
"filter_size": 3,
"stride": 1,
"padding": 1
}), (conv_bn_layer, {
"ch_in": 48,
"ch_out": 64,
"filter_size": 3,
"stride": 1,
"padding": 1
}))
out = paddle.layer.concat(input=[branch0, branch1, branch2])
out = paddle.layer.img_conv(
input=out,
filter_size=1,
num_channels=128,
num_filters=320,
stride=1,
padding=0,
act=paddle.activation.Linear(),
bias_attr=None)
out = paddle.layer.slope_intercept(out, slope=scale, intercept=0.0)
out = paddle.layer.addto(input=[input, out], act=paddle.activation.Relu())
return out
def mixed_6a_block(input):
branch0 = conv_bn_layer(
input, ch_in=320, ch_out=384, filter_size=3, stride=2)
branch1 = sequential_block(input, (conv_bn_layer, {
"ch_in": 320,
"ch_out": 256,
"filter_size": 1,
"stride": 1
}), (conv_bn_layer, {
"ch_in": 256,
"ch_out": 256,
"filter_size": 3,
"stride": 1,
"padding": 1
}), (conv_bn_layer, {
"ch_in": 256,
"ch_out": 384,
"filter_size": 3,
"stride": 2
}))
branch2 = paddle.layer.img_pool(
input,
num_channels=320,
pool_size=3,
stride=2,
pool_type=paddle.pooling.Max())
out = paddle.layer.concat(input=[branch0, branch1, branch2])
return out
def block17(input, scale=1.0):
branch0 = conv_bn_layer(
input, ch_in=1088, ch_out=192, filter_size=1, stride=1)
branch1 = sequential_block(input, (conv_bn_layer, {
"ch_in": 1088,
"ch_out": 128,
"filter_size": 1,
"stride": 1
}), (conv_bn_layer, {
"ch_in": 128,
"ch_out": 160,
"filter_size": [7, 1],
"stride": 1,
"padding": [3, 0]
}), (conv_bn_layer, {
"ch_in": 160,
"ch_out": 192,
"filter_size": [1, 7],
"stride": 1,
"padding": [0, 3]
}))
out = paddle.layer.concat(input=[branch0, branch1])
out = paddle.layer.img_conv(
input=out,
filter_size=1,
num_channels=384,
num_filters=1088,
stride=1,
padding=0,
act=paddle.activation.Linear(),
bias_attr=None)
out = paddle.layer.slope_intercept(out, slope=scale, intercept=0.0)
out = paddle.layer.addto(input=[input, out], act=paddle.activation.Relu())
return out
def mixed_7a_block(input):
branch0 = sequential_block(
input,
(conv_bn_layer, {
"ch_in": 1088,
"ch_out": 256,
"filter_size": 1,
"stride": 1
}),
(conv_bn_layer, {
"ch_in": 256,
"ch_out": 384,
"filter_size": 3,
"stride": 2
}), )
branch1 = sequential_block(
input,
(conv_bn_layer, {
"ch_in": 1088,
"ch_out": 256,
"filter_size": 1,
"stride": 1
}),
(conv_bn_layer, {
"ch_in": 256,
"ch_out": 288,
"filter_size": 3,
"stride": 2
}), )
branch2 = sequential_block(input, (conv_bn_layer, {
"ch_in": 1088,
"ch_out": 256,
"filter_size": 1,
"stride": 1
}), (conv_bn_layer, {
"ch_in": 256,
"ch_out": 288,
"filter_size": 3,
"stride": 1,
"padding": 1
}), (conv_bn_layer, {
"ch_in": 288,
"ch_out": 320,
"filter_size": 3,
"stride": 2
}))
branch3 = paddle.layer.img_pool(
input,
num_channels=1088,
pool_size=3,
stride=2,
pool_type=paddle.pooling.Max())
out = paddle.layer.concat(input=[branch0, branch1, branch2, branch3])
return out
def block8(input, scale=1.0, no_relu=False):
branch0 = conv_bn_layer(
input, ch_in=2080, ch_out=192, filter_size=1, stride=1)
branch1 = sequential_block(input, (conv_bn_layer, {
"ch_in": 2080,
"ch_out": 192,
"filter_size": 1,
"stride": 1
}), (conv_bn_layer, {
"ch_in": 192,
"ch_out": 224,
"filter_size": [3, 1],
"stride": 1,
"padding": [1, 0]
}), (conv_bn_layer, {
"ch_in": 224,
"ch_out": 256,
"filter_size": [1, 3],
"stride": 1,
"padding": [0, 1]
}))
out = paddle.layer.concat(input=[branch0, branch1])
out = paddle.layer.img_conv(
input=out,
filter_size=1,
num_channels=448,
num_filters=2080,
stride=1,
padding=0,
act=paddle.activation.Linear(),
bias_attr=None)
out = paddle.layer.slope_intercept(out, slope=scale, intercept=0.0)
out = paddle.layer.addto(
input=[input, out],
act=paddle.activation.Linear() if no_relu else paddle.activation.Relu())
return out
def inception_resnet_v2(input,
class_dim,
dropout_rate=0.5,
data_dim=3 * 331 * 331):
conv2d_1a = conv_bn_layer(
input, ch_in=3, ch_out=32, filter_size=3, stride=2)
conv2d_2a = conv_bn_layer(
conv2d_1a, ch_in=32, ch_out=32, filter_size=3, stride=1)
conv2d_2b = conv_bn_layer(
conv2d_2a, ch_in=32, ch_out=64, filter_size=3, stride=1, padding=1)
maxpool_3a = paddle.layer.img_pool(
input=conv2d_2b, pool_size=3, stride=2, pool_type=paddle.pooling.Max())
conv2d_3b = conv_bn_layer(
maxpool_3a, ch_in=64, ch_out=80, filter_size=1, stride=1)
conv2d_4a = conv_bn_layer(
conv2d_3b, ch_in=80, ch_out=192, filter_size=3, stride=1)
maxpool_5a = paddle.layer.img_pool(
input=conv2d_4a, pool_size=3, stride=2, pool_type=paddle.pooling.Max())
mixed_5b = mixed_5b_block(maxpool_5a)
repeat = sequential_block(mixed_5b, *([(block35, {"scale": 0.17})] * 10))
mixed_6a = mixed_6a_block(repeat)
repeat1 = sequential_block(mixed_6a, *([(block17, {"scale": 0.10})] * 20))
mixed_7a = mixed_7a_block(repeat1)
repeat2 = sequential_block(mixed_7a, *([(block8, {"scale": 0.20})] * 9))
block_8 = block8(repeat2, no_relu=True)
conv2d_7b = conv_bn_layer(
block_8, ch_in=2080, ch_out=1536, filter_size=1, stride=1)
avgpool_1a = paddle.layer.img_pool(
input=conv2d_7b,
pool_size=8 if data_dim == 3 * 299 * 299 else 9,
stride=1,
pool_type=paddle.pooling.Avg(),
exclude_mode=False)
drop_out = paddle.layer.dropout(input=avgpool_1a, dropout_rate=dropout_rate)
out = paddle.layer.fc(
input=drop_out, size=class_dim, act=paddle.activation.Softmax())
return out
import paddle.v2 as paddle
__all__ = ['inception_v4']
def img_conv(name,
input,
num_filters,
filter_size,
stride,
padding,
num_channels=None):
conv = paddle.layer.img_conv(
name=name,
input=input,
num_channels=num_channels,
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=padding,
act=paddle.activation.Linear())
norm = paddle.layer.batch_norm(
name=name + '_norm', input=conv, act=paddle.activation.Relu())
return norm
def stem(input):
conv0 = img_conv(
name='stem_conv0',
input=input,
num_channels=3,
num_filters=32,
filter_size=3,
stride=2,
padding=1)
conv1 = img_conv(
name='stem_conv1',
input=conv0,
num_channels=32,
num_filters=32,
filter_size=3,
stride=1,
padding=1)
conv2 = img_conv(
name='stem_conv2',
input=conv1,
num_channels=32,
num_filters=64,
filter_size=3,
stride=1,
padding=1)
def block0(input):
pool0 = paddle.layer.img_pool(
name='stem_branch0_pool0',
input=input,
num_channels=64,
pool_size=3,
stride=2,
pool_type=paddle.pooling.Max())
conv0 = img_conv(
name='stem_branch0_conv0',
input=input,
num_channels=64,
num_filters=96,
filter_size=3,
stride=2,
padding=1)
return paddle.layer.concat(input=[pool0, conv0])
def block1(input):
l_conv0 = img_conv(
name='stem_branch1_l_conv0',
input=input,
num_channels=160,
num_filters=64,
filter_size=1,
stride=1,
padding=0)
l_conv1 = img_conv(
name='stem_branch1_l_conv1',
input=l_conv0,
num_channels=64,
num_filters=96,
filter_size=3,
stride=1,
padding=1)
r_conv0 = img_conv(
name='stem_branch1_r_conv0',
input=input,
num_channels=160,
num_filters=64,
filter_size=1,
stride=1,
padding=0)
r_conv1 = img_conv(
name='stem_branch1_r_conv1',
input=r_conv0,
num_channels=64,
num_filters=64,
filter_size=(7, 1),
stride=1,
padding=(3, 0))
r_conv2 = img_conv(
name='stem_branch1_r_conv2',
input=r_conv1,
num_channels=64,
num_filters=64,
filter_size=(1, 7),
stride=1,
padding=(0, 3))
r_conv3 = img_conv(
name='stem_branch1_r_conv3',
input=r_conv2,
num_channels=64,
num_filters=96,
filter_size=3,
stride=1,
padding=1)
return paddle.layer.concat(input=[l_conv1, r_conv3])
def block2(input):
conv0 = img_conv(
name='stem_branch2_conv0',
input=input,
num_channels=192,
num_filters=192,
filter_size=3,
stride=2,
padding=1)
pool0 = paddle.layer.img_pool(
name='stem_branch2_pool0',
input=input,
num_channels=192,
pool_size=3,
stride=2,
pool_type=paddle.pooling.Max())
return paddle.layer.concat(input=[conv0, pool0])
conv3 = block0(conv2)
conv4 = block1(conv3)
conv5 = block2(conv4)
return conv5
def Inception_A(input, depth):
b0_pool0 = paddle.layer.img_pool(
name='inceptA{0}_branch0_pool0'.format(depth),
input=input,
num_channels=384,
pool_size=3,
stride=1,
padding=1,
pool_type=paddle.pooling.Avg())
b0_conv0 = img_conv(
name='inceptA{0}_branch0_conv0'.format(depth),
input=b0_pool0,
num_channels=384,
num_filters=96,
filter_size=1,
stride=1,
padding=0)
b1_conv0 = img_conv(
name='inceptA{0}_branch1_conv0'.format(depth),
input=input,
num_channels=384,
num_filters=96,
filter_size=1,
stride=1,
padding=0)
b2_conv0 = img_conv(
name='inceptA{0}_branch2_conv0'.format(depth),
input=input,
num_channels=384,
num_filters=64,
filter_size=1,
stride=1,
padding=0)
b2_conv1 = img_conv(
name='inceptA{0}_branch2_conv1'.format(depth),
input=b2_conv0,
num_channels=64,
num_filters=96,
filter_size=3,
stride=1,
padding=1)
b3_conv0 = img_conv(
name='inceptA{0}_branch3_conv0'.format(depth),
input=input,
num_channels=384,
num_filters=64,
filter_size=1,
stride=1,
padding=0)
b3_conv1 = img_conv(
name='inceptA{0}_branch3_conv1'.format(depth),
input=b3_conv0,
num_channels=64,
num_filters=96,
filter_size=3,
stride=1,
padding=1)
b3_conv2 = img_conv(
name='inceptA{0}_branch3_conv2'.format(depth),
input=b3_conv1,
num_channels=96,
num_filters=96,
filter_size=3,
stride=1,
padding=1)
return paddle.layer.concat(input=[b0_conv0, b1_conv0, b2_conv1, b3_conv2])
def Inception_B(input, depth):
b0_pool0 = paddle.layer.img_pool(
name='inceptB{0}_branch0_pool0'.format(depth),
input=input,
num_channels=1024,
pool_size=3,
stride=1,
padding=1,
pool_type=paddle.pooling.Avg())
b0_conv0 = img_conv(
name='inceptB{0}_branch0_conv0'.format(depth),
input=b0_pool0,
num_channels=1024,
num_filters=128,
filter_size=1,
stride=1,
padding=0)
b1_conv0 = img_conv(
name='inceptB{0}_branch1_conv0'.format(depth),
input=input,
num_channels=1024,
num_filters=384,
filter_size=1,
stride=1,
padding=0)
b2_conv0 = img_conv(
name='inceptB{0}_branch2_conv0'.format(depth),
input=input,
num_channels=1024,
num_filters=192,
filter_size=1,
stride=1,
padding=0)
b2_conv1 = img_conv(
name='inceptB{0}_branch2_conv1'.format(depth),
input=b2_conv0,
num_channels=192,
num_filters=224,
filter_size=(1, 7),
stride=1,
padding=(0, 3))
b2_conv2 = img_conv(
name='inceptB{0}_branch2_conv2'.format(depth),
input=b2_conv1,
num_channels=224,
num_filters=256,
filter_size=(7, 1),
stride=1,
padding=(3, 0))
b3_conv0 = img_conv(
name='inceptB{0}_branch3_conv0'.format(depth),
input=input,
num_channels=1024,
num_filters=192,
filter_size=1,
stride=1,
padding=0)
b3_conv1 = img_conv(
name='inceptB{0}_branch3_conv1'.format(depth),
input=b3_conv0,
num_channels=192,
num_filters=192,
filter_size=(1, 7),
stride=1,
padding=(0, 3))
b3_conv2 = img_conv(
name='inceptB{0}_branch3_conv2'.format(depth),
input=b3_conv1,
num_channels=192,
num_filters=224,
filter_size=(7, 1),
stride=1,
padding=(3, 0))
b3_conv3 = img_conv(
name='inceptB{0}_branch3_conv3'.format(depth),
input=b3_conv2,
num_channels=224,
num_filters=224,
filter_size=(1, 7),
stride=1,
padding=(0, 3))
b3_conv4 = img_conv(
name='inceptB{0}_branch3_conv4'.format(depth),
input=b3_conv3,
num_channels=224,
num_filters=256,
filter_size=(7, 1),
stride=1,
padding=(3, 0))
return paddle.layer.concat(input=[b0_conv0, b1_conv0, b2_conv2, b3_conv4])
def Inception_C(input, depth):
b0_pool0 = paddle.layer.img_pool(
name='inceptC{0}_branch0_pool0'.format(depth),
input=input,
num_channels=1536,
pool_size=3,
stride=1,
padding=1,
pool_type=paddle.pooling.Avg())
b0_conv0 = img_conv(
name='inceptC{0}_branch0_conv0'.format(depth),
input=b0_pool0,
num_channels=1536,
num_filters=256,
filter_size=1,
stride=1,
padding=0)
b1_conv0 = img_conv(
name='inceptC{0}_branch1_conv0'.format(depth),
input=input,
num_channels=1536,
num_filters=256,
filter_size=1,
stride=1,
padding=0)
b2_conv0 = img_conv(
name='inceptC{0}_branch2_conv0'.format(depth),
input=input,
num_channels=1536,
num_filters=384,
filter_size=1,
stride=1,
padding=0)
b2_conv1 = img_conv(
name='inceptC{0}_branch2_conv1'.format(depth),
input=b2_conv0,
num_channels=384,
num_filters=256,
filter_size=(1, 3),
stride=1,
padding=(0, 1))
b2_conv2 = img_conv(
name='inceptC{0}_branch2_conv2'.format(depth),
input=b2_conv0,
num_channels=384,
num_filters=256,
filter_size=(3, 1),
stride=1,
padding=(1, 0))
b3_conv0 = img_conv(
name='inceptC{0}_branch3_conv0'.format(depth),
input=input,
num_channels=1536,
num_filters=384,
filter_size=1,
stride=1,
padding=0)
b3_conv1 = img_conv(
name='inceptC{0}_branch3_conv1'.format(depth),
input=b3_conv0,
num_channels=384,
num_filters=448,
filter_size=(1, 3),
stride=1,
padding=(0, 1))
b3_conv2 = img_conv(
name='inceptC{0}_branch3_conv2'.format(depth),
input=b3_conv1,
num_channels=448,
num_filters=512,
filter_size=(3, 1),
stride=1,
padding=(1, 0))
b3_conv3 = img_conv(
name='inceptC{0}_branch3_conv3'.format(depth),
input=b3_conv2,
num_channels=512,
num_filters=256,
filter_size=(3, 1),
stride=1,
padding=(1, 0))
b3_conv4 = img_conv(
name='inceptC{0}_branch3_conv4'.format(depth),
input=b3_conv2,
num_channels=512,
num_filters=256,
filter_size=(1, 3),
stride=1,
padding=(0, 1))
return paddle.layer.concat(
input=[b0_conv0, b1_conv0, b2_conv1, b2_conv2, b3_conv3, b3_conv4])
def Reduction_A(input):
b0_pool0 = paddle.layer.img_pool(
name='ReductA_branch0_pool0',
input=input,
num_channels=384,
pool_size=3,
stride=2,
pool_type=paddle.pooling.Max())
b1_conv0 = img_conv(
name='ReductA_branch1_conv0',
input=input,
num_channels=384,
num_filters=384,
filter_size=3,
stride=2,
padding=1)
b2_conv0 = img_conv(
name='ReductA_branch2_conv0',
input=input,
num_channels=384,
num_filters=192,
filter_size=1,
stride=1,
padding=0)
b2_conv1 = img_conv(
name='ReductA_branch2_conv1',
input=b2_conv0,
num_channels=192,
num_filters=224,
filter_size=3,
stride=1,
padding=1)
b2_conv2 = img_conv(
name='ReductA_branch2_conv2',
input=b2_conv1,
num_channels=224,
num_filters=256,
filter_size=3,
stride=2,
padding=1)
return paddle.layer.concat(input=[b0_pool0, b1_conv0, b2_conv2])
def Reduction_B(input):
b0_pool0 = paddle.layer.img_pool(
name='ReductB_branch0_pool0',
input=input,
num_channels=1024,
pool_size=3,
stride=2,
pool_type=paddle.pooling.Max())
b1_conv0 = img_conv(
name='ReductB_branch1_conv0',
input=input,
num_channels=1024,
num_filters=192,
filter_size=1,
stride=1,
padding=0)
b1_conv1 = img_conv(
name='ReductB_branch1_conv1',
input=b1_conv0,
num_channels=192,
num_filters=192,
filter_size=3,
stride=2,
padding=1)
b2_conv0 = img_conv(
name='ReductB_branch2_conv0',
input=input,
num_channels=1024,
num_filters=256,
filter_size=1,
stride=1,
padding=0)
b2_conv1 = img_conv(
name='ReductB_branch2_conv1',
input=b2_conv0,
num_channels=256,
num_filters=256,
filter_size=(1, 7),
stride=1,
padding=(0, 3))
b2_conv2 = img_conv(
name='ReductB_branch2_conv2',
input=b2_conv1,
num_channels=256,
num_filters=320,
filter_size=(7, 1),
stride=1,
padding=(3, 0))
b2_conv3 = img_conv(
name='ReductB_branch2_conv3',
input=b2_conv2,
num_channels=320,
num_filters=320,
filter_size=3,
stride=2,
padding=1)
return paddle.layer.concat(input=[b0_pool0, b1_conv1, b2_conv3])
def inception_v4(input, class_dim):
conv = stem(input)
for i in range(4):
conv = Inception_A(conv, i)
conv = Reduction_A(conv)
for i in range(7):
conv = Inception_B(conv, i)
conv = Reduction_B(conv)
for i in range(3):
conv = Inception_C(conv, i)
pool = paddle.layer.img_pool(
name='incept_avg_pool',
input=conv,
num_channels=1536,
pool_size=7,
stride=1,
pool_type=paddle.pooling.Avg())
drop = paddle.layer.dropout(input=pool, dropout_rate=0.2)
out = paddle.layer.fc(
name='incept_fc',
input=drop,
size=class_dim,
act=paddle.activation.Softmax())
return out
import os
import gzip
import argparse
import numpy as np
from PIL import Image
import paddle.v2 as paddle
import reader
import vgg
import resnet
import alexnet
import googlenet
import inception_v4
import inception_resnet_v2
import xception
import argparse
import os
from PIL import Image
import numpy as np
WIDTH = 224
HEIGHT = 224
DATA_DIM = 3 * WIDTH * HEIGHT
DATA_DIM = 3 * 224 * 224 # Use 3 * 331 * 331 or 3 * 299 * 299 for Inception-ResNet-v2.
CLASS_DIM = 102
......@@ -29,7 +30,7 @@ def main():
help='The model for image classification',
choices=[
'alexnet', 'vgg13', 'vgg16', 'vgg19', 'resnet', 'googlenet',
'xception'
'inception-resnet-v2', 'inception_v4', 'xception'
])
parser.add_argument(
'params_path', help='The file which stores the parameters')
......@@ -53,6 +54,12 @@ def main():
out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM)
elif args.model == 'googlenet':
out, _, _ = googlenet.googlenet(image, class_dim=CLASS_DIM)
elif args.model == 'inception-resnet-v2':
assert DATA_DIM == 3 * 331 * 331 or DATA_DIM == 3 * 299 * 299
out = inception_resnet_v2.inception_resnet_v2(
image, class_dim=CLASS_DIM, dropout_rate=0.5, data_dim=DATA_DIM)
elif args.model == 'inception_v4':
out = inception_v4.inception_v4(image, class_dim=CLASS_DIM)
elif args.model == 'xception':
out = xception.xception(image, class_dim=CLASS_DIM)
......
import gzip
import argparse
import paddle.v2.dataset.flowers as flowers
import paddle.v2 as paddle
import reader
......@@ -6,10 +8,11 @@ import vgg
import resnet
import alexnet
import googlenet
import inception_v4
import inception_resnet_v2
import xception
import argparse
DATA_DIM = 3 * 224 * 224
DATA_DIM = 3 * 224 * 224 # Use 3 * 331 * 331 or 3 * 299 * 299 for Inception-ResNet-v2.
CLASS_DIM = 102
BATCH_SIZE = 128
......@@ -22,7 +25,7 @@ def main():
help='The model for image classification',
choices=[
'alexnet', 'vgg13', 'vgg16', 'vgg19', 'resnet', 'googlenet',
'xception'
'inception-resnet-v2', 'inception_v4', 'xception'
])
args = parser.parse_args()
......@@ -56,6 +59,12 @@ def main():
input=out2, label=lbl, coeff=0.3)
paddle.evaluator.classification_error(input=out2, label=lbl)
extra_layers = [loss1, loss2]
elif args.model == 'inception-resnet-v2':
assert DATA_DIM == 3 * 331 * 331 or DATA_DIM == 3 * 299 * 299
out = inception_resnet_v2.inception_resnet_v2(
image, class_dim=CLASS_DIM, dropout_rate=0.5, data_dim=DATA_DIM)
elif args.model == 'inception_v4':
out = inception_v4.inception_v4(image, class_dim=CLASS_DIM)
elif args.model == 'xception':
out = xception.xception(image, class_dim=CLASS_DIM)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册