Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
fe1f9e99
B
book
项目概览
PaddlePaddle
/
book
通知
16
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
fe1f9e99
编写于
4月 07, 2017
作者:
Q
qingqing01
提交者:
GitHub
4月 07, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #271 from qingqing01/image
model saving and inference for 03.image_classification
上级
ad5be8a3
ea03d59e
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
137 addition
and
3 deletion
+137
-3
03.image_classification/README.en.md
03.image_classification/README.en.md
+28
-1
03.image_classification/README.md
03.image_classification/README.md
+28
-0
03.image_classification/index.en.html
03.image_classification/index.en.html
+28
-1
03.image_classification/index.html
03.image_classification/index.html
+28
-0
03.image_classification/train.py
03.image_classification/train.py
+25
-1
未找到文件。
03.image_classification/README.en.md
浏览文件 @
fe1f9e99
...
...
@@ -169,6 +169,7 @@ We must import and initialize PaddlePaddle (enable/disable GPU, set the number o
```
python
import
sys
import
gzip
import
paddle.v2
as
paddle
from
vgg
import
vgg_bn_drop
from
resnet
import
resnet_cifar10
...
...
@@ -437,6 +438,10 @@ def event_handler(event):
sys
.
stdout
.
write
(
'.'
)
sys
.
stdout
.
flush
()
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
# save parameters
with
gzip
.
open
(
'params_pass_%d.tar.gz'
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
128
),
...
...
@@ -475,7 +480,29 @@ Figure 12. The error rate of VGG model on CIFAR10
</p>
After training is done, the model from each pass is saved in
`output/pass-%05d`
. For example, the model of Pass 300 is saved in
`output/pass-00299`
.
## Application
After training is done, users can use the trained model to classify images. The following code shows how to infer through
`paddle.infer`
interface.
```
python
from
PIL
import
Image
import
numpy
as
np
def
load_image
(
file
):
im
=
Image
.
open
(
file
)
im
=
im
.
resize
((
32
,
32
),
Image
.
ANTIALIAS
)
im
=
np
.
array
(
im
).
astype
(
np
.
float32
).
flatten
()
im
=
im
/
255.0
return
im
test_data
=
[]
test_data
.
append
((
load_image
(
'image/dog.png'
),))
probs
=
paddle
.
infer
(
output_layer
=
out
,
parameters
=
parameters
,
input
=
test_data
)
lab
=
np
.
argsort
(
-
probs
)
# probs and lab are the results of one batch data
print
"Label of image/dog.png is: %d"
%
lab
[
0
][
0
]
```
## Conclusion
...
...
03.image_classification/README.md
浏览文件 @
fe1f9e99
...
...
@@ -156,6 +156,7 @@ Paddle API提供了自动加载cifar数据集模块 `paddle.dataset.cifar`。
```
python
import
sys
import
gzip
import
paddle.v2
as
paddle
from
vgg
import
vgg_bn_drop
from
resnet
import
resnet_cifar10
...
...
@@ -409,6 +410,7 @@ def event_handler_plot(event):
cost_ploter
.
plot
()
step
+=
1
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
128
),
...
...
@@ -429,6 +431,10 @@ def event_handler(event):
sys
.
stdout
.
write
(
'.'
)
sys
.
stdout
.
flush
()
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
# save parameters
with
gzip
.
open
(
'params_pass_%d.tar.gz'
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
128
),
...
...
@@ -467,6 +473,28 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123}
图12. CIFAR10数据集上VGG模型的分类错误率
</p>
## 应用模型
可以使用训练好的模型对图片进行分类,下面程序展示了如何使用
`paddle.infer`
接口进行推断。
```
python
from
PIL
import
Image
import
numpy
as
np
def
load_image
(
file
):
im
=
Image
.
open
(
file
)
im
=
im
.
resize
((
32
,
32
),
Image
.
ANTIALIAS
)
im
=
np
.
array
(
im
).
astype
(
np
.
float32
).
flatten
()
im
=
im
/
255.0
return
im
test_data
=
[]
test_data
.
append
((
load_image
(
'image/dog.png'
),))
probs
=
paddle
.
infer
(
output_layer
=
out
,
parameters
=
parameters
,
input
=
test_data
)
lab
=
np
.
argsort
(
-
probs
)
# probs and lab are the results of one batch data
print
"Label of image/dog.png is: %d"
%
lab
[
0
][
0
]
```
## 总结
...
...
03.image_classification/index.en.html
浏览文件 @
fe1f9e99
...
...
@@ -211,6 +211,7 @@ We must import and initialize PaddlePaddle (enable/disable GPU, set the number o
```python
import sys
import gzip
import paddle.v2 as paddle
from vgg import vgg_bn_drop
from resnet import resnet_cifar10
...
...
@@ -479,6 +480,10 @@ def event_handler(event):
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, paddle.event.EndPass):
# save parameters
with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f:
parameters.to_tar(f)
result = trainer.test(
reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128),
...
...
@@ -517,7 +522,29 @@ Figure 12. The error rate of VGG model on CIFAR10
</p>
After training is done, the model from each pass is saved in `output/pass-%05d`. For example, the model of Pass 300 is saved in `output/pass-00299`.
## Application
After training is done, users can use the trained model to classify images. The following code shows how to infer through `paddle.infer` interface.
```python
from PIL import Image
import numpy as np
def load_image(file):
im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32).flatten()
im = im / 255.0
return im
test_data = []
test_data.append((load_image('image/dog.png'),))
probs = paddle.infer(
output_layer=out, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print "Label of image/dog.png is: %d" % lab[0][0]
```
## Conclusion
...
...
03.image_classification/index.html
浏览文件 @
fe1f9e99
...
...
@@ -198,6 +198,7 @@ Paddle API提供了自动加载cifar数据集模块 `paddle.dataset.cifar`。
```python
import sys
import gzip
import paddle.v2 as paddle
from vgg import vgg_bn_drop
from resnet import resnet_cifar10
...
...
@@ -451,6 +452,7 @@ def event_handler_plot(event):
cost_ploter.plot()
step += 1
if isinstance(event, paddle.event.EndPass):
result = trainer.test(
reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128),
...
...
@@ -471,6 +473,10 @@ def event_handler(event):
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, paddle.event.EndPass):
# save parameters
with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f:
parameters.to_tar(f)
result = trainer.test(
reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128),
...
...
@@ -509,6 +515,28 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123}
图12. CIFAR10数据集上VGG模型的分类错误率
</p>
## 应用模型
可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。
```python
from PIL import Image
import numpy as np
def load_image(file):
im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32).flatten()
im = im / 255.0
return im
test_data = []
test_data.append((load_image('image/dog.png'),))
probs = paddle.infer(
output_layer=out, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print "Label of image/dog.png is: %d" % lab[0][0]
```
## 总结
...
...
03.image_classification/train.py
浏览文件 @
fe1f9e99
...
...
@@ -13,6 +13,7 @@
# limitations under the License
import
sys
import
gzip
import
paddle.v2
as
paddle
...
...
@@ -66,6 +67,10 @@ def main():
sys
.
stdout
.
write
(
'.'
)
sys
.
stdout
.
flush
()
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
# save parameters
with
gzip
.
open
(
'params_pass_%d.tar.gz'
%
event
.
pass_id
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
128
),
...
...
@@ -81,11 +86,30 @@ def main():
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
cifar
.
train10
(),
buf_size
=
50000
),
batch_size
=
128
),
num_passes
=
200
,
num_passes
=
1
,
event_handler
=
event_handler
,
feeding
=
{
'image'
:
0
,
'label'
:
1
})
# inference
from
PIL
import
Image
import
numpy
as
np
def
load_image
(
file
):
im
=
Image
.
open
(
file
)
im
=
im
.
resize
((
32
,
32
),
Image
.
ANTIALIAS
)
im
=
np
.
array
(
im
).
astype
(
np
.
float32
).
flatten
()
im
=
im
/
255.0
return
im
test_data
=
[]
test_data
.
append
((
load_image
(
'image/dog.png'
),
))
probs
=
paddle
.
infer
(
output_layer
=
out
,
parameters
=
parameters
,
input
=
test_data
)
lab
=
np
.
argsort
(
-
probs
)
# probs and lab are the results of one batch data
print
"Label of image/dog.png is: %d"
%
lab
[
0
][
0
]
if
__name__
==
'__main__'
:
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录