Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
11b6473c
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
11b6473c
编写于
5月 18, 2018
作者:
D
daminglu
提交者:
GitHub
5月 18, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Image classification & word2vec (#10738)
上级
40a2ee9a
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
152 addition
and
42 deletion
+152
-42
python/paddle/fluid/tests/book/high-level-api/CMakeLists.txt
python/paddle/fluid/tests/book/high-level-api/CMakeLists.txt
+1
-0
python/paddle/fluid/tests/book/high-level-api/image_classification/CMakeLists.txt
...s/book/high-level-api/image_classification/CMakeLists.txt
+7
-0
python/paddle/fluid/tests/book/high-level-api/image_classification/cifar10_small_test_set.py
...-level-api/image_classification/cifar10_small_test_set.py
+82
-0
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_resnet.py
.../image_classification/test_image_classification_resnet.py
+31
-20
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_vgg.py
...api/image_classification/test_image_classification_vgg.py
+31
-22
未找到文件。
python/paddle/fluid/tests/book/high-level-api/CMakeLists.txt
浏览文件 @
11b6473c
...
...
@@ -8,3 +8,4 @@ endforeach()
add_subdirectory
(
fit_a_line
)
add_subdirectory
(
recognize_digits
)
add_subdirectory
(
image_classification
)
python/paddle/fluid/tests/book/high-level-api/image_classification/CMakeLists.txt
0 → 100644
浏览文件 @
11b6473c
file
(
GLOB TEST_OPS RELATIVE
"
${
CMAKE_CURRENT_SOURCE_DIR
}
"
"test_*.py"
)
string
(
REPLACE
".py"
""
TEST_OPS
"
${
TEST_OPS
}
"
)
# default test
foreach
(
src
${
TEST_OPS
}
)
py_test
(
${
src
}
SRCS
${
src
}
.py
)
endforeach
()
python/paddle/fluid/tests/book/high-level-api/image_classification/cifar10_small_test_set.py
0 → 100644
浏览文件 @
11b6473c
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
CIFAR dataset.
This module will download dataset from
https://www.cs.toronto.edu/~kriz/cifar.html and parse train/test set into
paddle reader creators.
The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes,
with 6000 images per class. There are 50000 training images and 10000 test
images.
The CIFAR-100 dataset is just like the CIFAR-10, except it has 100 classes
containing 600 images each. There are 500 training images and 100 testing
images per class.
"""
import
cPickle
import
itertools
import
numpy
import
paddle.v2.dataset.common
import
tarfile
__all__
=
[
'train10'
]
URL_PREFIX
=
'https://www.cs.toronto.edu/~kriz/'
CIFAR10_URL
=
URL_PREFIX
+
'cifar-10-python.tar.gz'
CIFAR10_MD5
=
'c58f30108f718f92721af3b95e74349a'
def
reader_creator
(
filename
,
sub_name
,
batch_size
=
None
):
def
read_batch
(
batch
):
data
=
batch
[
'data'
]
labels
=
batch
.
get
(
'labels'
,
batch
.
get
(
'fine_labels'
,
None
))
assert
labels
is
not
None
for
sample
,
label
in
itertools
.
izip
(
data
,
labels
):
yield
(
sample
/
255.0
).
astype
(
numpy
.
float32
),
int
(
label
)
def
reader
():
with
tarfile
.
open
(
filename
,
mode
=
'r'
)
as
f
:
names
=
(
each_item
.
name
for
each_item
in
f
if
sub_name
in
each_item
.
name
)
batch_count
=
0
for
name
in
names
:
batch
=
cPickle
.
load
(
f
.
extractfile
(
name
))
for
item
in
read_batch
(
batch
):
if
isinstance
(
batch_size
,
int
)
and
batch_count
>
batch_size
:
break
batch_count
+=
1
yield
item
return
reader
def
train10
(
batch_size
=
None
):
"""
CIFAR-10 training set creator.
It returns a reader creator, each sample in the reader is image pixels in
[0, 1] and label in [0, 9].
:return: Training reader creator
:rtype: callable
"""
return
reader_creator
(
paddle
.
v2
.
dataset
.
common
.
download
(
CIFAR10_URL
,
'cifar'
,
CIFAR10_MD5
),
'data_batch'
,
batch_size
=
batch_size
)
python/paddle/fluid/tests/book/high-level-api/image_classification/
no
test_image_classification_resnet.py
→
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_resnet.py
浏览文件 @
11b6473c
...
...
@@ -17,6 +17,7 @@ from __future__ import print_function
import
paddle
import
paddle.fluid
as
fluid
import
numpy
import
cifar10_small_test_set
def
resnet_cifar10
(
input
,
depth
=
32
):
...
...
@@ -81,46 +82,50 @@ def train_network():
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
cost
)
accuracy
=
fluid
.
layers
.
accuracy
(
input
=
predict
,
label
=
label
)
return
avg_cost
,
accuracy
return
[
avg_cost
,
accuracy
]
def
train
(
use_cuda
,
save_path
):
def
train
(
use_cuda
,
train_program
,
save_dirname
):
BATCH_SIZE
=
128
EPOCH_NUM
=
1
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
cifar
.
train10
(
),
buf_size
=
128
*
10
),
cifar10_small_test_set
.
train10
(
batch_size
=
10
),
buf_size
=
128
*
10
),
batch_size
=
BATCH_SIZE
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
BATCH_SIZE
)
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
End
Iteration
):
if
(
event
.
batch_id
%
10
)
==
0
:
avg_cost
,
accuracy
=
trainer
.
test
(
reader
=
test_reader
)
if
isinstance
(
event
,
fluid
.
End
StepEvent
):
avg_cost
,
accuracy
=
trainer
.
test
(
reader
=
test_reader
,
feed_order
=
[
'pixel'
,
'label'
]
)
print
(
'BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}'
.
format
(
event
.
batch_id
+
1
,
avg_cost
,
accuracy
))
print
(
'Loss {0:2.2}, Acc {1:2.2}'
.
format
(
avg_cost
,
accuracy
))
if
accuracy
>
0.01
:
# Low threshold for speeding up CI
trainer
.
params
.
save
(
save_path
)
return
if
accuracy
>
0.01
:
# Low threshold for speeding up CI
if
save_dirname
is
not
None
:
trainer
.
save_params
(
save_dirname
)
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
train_
network
,
train_
func
=
train_program
,
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.001
),
place
=
place
,
event_handler
=
event_handler
)
trainer
.
train
(
train_reader
,
EPOCH_NUM
,
event_handler
=
event_handler
)
place
=
place
)
trainer
.
train
(
reader
=
train_reader
,
num_epochs
=
EPOCH_NUM
,
event_handler
=
event_handler
,
feed_order
=
[
'pixel'
,
'label'
])
def
infer
(
use_cuda
,
save_path
):
params
=
fluid
.
Params
(
save_path
)
def
infer
(
use_cuda
,
inference_program
,
save_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inference_network
,
params
,
place
=
place
)
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save_dirname
,
place
=
place
)
# The input's dimension of conv should be 4-D or 5-D.
# Use normilized image pixels as input data, which should be in the range
...
...
@@ -135,8 +140,14 @@ def main(use_cuda):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"image_classification_resnet.inference.model"
train
(
use_cuda
,
save_path
)
infer
(
use_cuda
,
save_path
)
train
(
use_cuda
=
use_cuda
,
train_program
=
train_network
,
save_dirname
=
save_path
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
inference_network
,
save_dirname
=
save_path
)
if
__name__
==
'__main__'
:
...
...
python/paddle/fluid/tests/book/high-level-api/image_classification/
no
test_image_classification_vgg.py
→
python/paddle/fluid/tests/book/high-level-api/image_classification/test_image_classification_vgg.py
浏览文件 @
11b6473c
...
...
@@ -17,6 +17,7 @@ from __future__ import print_function
import
paddle
import
paddle.fluid
as
fluid
import
numpy
import
cifar10_small_test_set
def
vgg16_bn_drop
(
input
):
...
...
@@ -60,46 +61,48 @@ def train_network():
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
predict
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
cost
)
accuracy
=
fluid
.
layers
.
accuracy
(
input
=
predict
,
label
=
label
)
return
avg_cost
,
accuracy
return
[
avg_cost
,
accuracy
]
def
train
(
use_cuda
,
save_path
):
def
train
(
use_cuda
,
train_program
,
save_dirname
):
BATCH_SIZE
=
128
EPOCH_NUM
=
1
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
cifar
.
train10
(
),
buf_size
=
128
*
10
),
cifar10_small_test_set
.
train10
(
batch_size
=
10
),
buf_size
=
128
*
10
),
batch_size
=
BATCH_SIZE
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
cifar
.
test10
(),
batch_size
=
BATCH_SIZE
)
def
event_handler
(
event
):
if
isinstance
(
event
,
fluid
.
End
Iteration
):
if
(
event
.
batch_id
%
10
)
==
0
:
avg_cost
,
accuracy
=
trainer
.
test
(
reader
=
test_reader
)
if
isinstance
(
event
,
fluid
.
End
StepEvent
):
avg_cost
,
accuracy
=
trainer
.
test
(
reader
=
test_reader
,
feed_order
=
[
'pixel'
,
'label'
]
)
print
(
'BatchID {1:04}, Loss {2:2.2}, Acc {3:2.2}'
.
format
(
event
.
batch_id
+
1
,
avg_cost
,
accuracy
))
print
(
'Loss {0:2.2}, Acc {1:2.2}'
.
format
(
avg_cost
,
accuracy
))
if
accuracy
>
0.01
:
# Low threshold for speeding up CI
trainer
.
params
.
save
(
save_path
)
return
if
accuracy
>
0.01
:
# Low threshold for speeding up CI
if
save_dirname
is
not
None
:
trainer
.
save_params
(
save_dirname
)
return
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
trainer
=
fluid
.
Trainer
(
train_network
,
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.001
),
train_func
=
train_program
,
place
=
place
,
event_handler
=
event_handler
)
trainer
.
train
(
train_reader
,
EPOCH_NUM
,
event_handler
=
event_handler
)
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
0.001
))
trainer
.
train
(
reader
=
train_reader
,
num_epochs
=
1
,
event_handler
=
event_handler
,
feed_order
=
[
'pixel'
,
'label'
])
def
infer
(
use_cuda
,
save_path
):
params
=
fluid
.
Params
(
save_path
)
def
infer
(
use_cuda
,
inference_program
,
save_dirname
=
None
):
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
inferencer
=
fluid
.
Inferencer
(
inference_network
,
params
,
place
=
place
)
inferencer
=
fluid
.
Inferencer
(
infer_func
=
inference_program
,
param_path
=
save_dirname
,
place
=
place
)
# The input's dimension of conv should be 4-D or 5-D.
# Use normilized image pixels as input data, which should be in the range
...
...
@@ -114,8 +117,14 @@ def main(use_cuda):
if
use_cuda
and
not
fluid
.
core
.
is_compiled_with_cuda
():
return
save_path
=
"image_classification_vgg.inference.model"
train
(
use_cuda
,
save_path
)
infer
(
use_cuda
,
save_path
)
train
(
use_cuda
=
use_cuda
,
train_program
=
train_network
,
save_dirname
=
save_path
)
infer
(
use_cuda
=
use_cuda
,
inference_program
=
inference_network
,
save_dirname
=
save_path
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录