Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
8a484dbd
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8a484dbd
编写于
5月 07, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
5月 07, 2020
浏览文件
操作
浏览文件
下载
差异文件
!812 [CR] add lenet train and eval st case
Merge pull request !812 from jinyaohui/train_eval
上级
d004ef22
8b49a4cf
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
72 addition
and
7 deletion
+72
-7
tests/st/networks/test_gpu_lenet.py
tests/st/networks/test_gpu_lenet.py
+72
-7
未找到文件。
tests/st/networks/test_gpu_lenet.py
浏览文件 @
8a484dbd
...
...
@@ -13,18 +13,26 @@
# limitations under the License.
# ============================================================================
import
os
import
pytest
import
numpy
as
np
import
mindspore.nn
as
nn
import
mindspore.context
as
context
from
mindspore
import
Tensor
from
mindspore.nn.optim
import
Momentum
import
mindspore.context
as
context
from
mindspore.ops
import
operations
as
P
from
mindspore.nn
import
TrainOneStepCell
,
WithLossCell
from
mindspore.nn
import
Dense
from
mindspore.common.initializer
import
initializer
import
mindspore.nn
as
nn
from
mindspore.nn
import
Dense
,
TrainOneStepCell
,
WithLossCell
from
mindspore.nn.optim
import
Momentum
from
mindspore.nn.metrics
import
Accuracy
from
mindspore.train
import
Model
from
mindspore.common
import
dtype
as
mstype
from
mindspore.common.initializer
import
initializer
from
mindspore.model_zoo.lenet
import
LeNet5
from
mindspore.train.callback
import
LossMonitor
import
mindspore.dataset
as
ds
import
mindspore.dataset.transforms.vision.c_transforms
as
CV
import
mindspore.dataset.transforms.c_transforms
as
C
from
mindspore.dataset.transforms.vision
import
Inter
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"GPU"
)
...
...
@@ -64,7 +72,7 @@ class LeNet(nn.Cell):
def
multisteplr
(
total_steps
,
gap
,
base_lr
=
0.9
,
gamma
=
0.1
,
dtype
=
mstype
.
float32
):
lr
=
[]
for
step
in
range
(
total_steps
):
lr_
=
base_lr
*
gamma
**
(
step
//
gap
)
lr_
=
base_lr
*
gamma
**
(
step
//
gap
)
lr
.
append
(
lr_
)
return
Tensor
(
np
.
array
(
lr
),
dtype
)
...
...
@@ -90,3 +98,60 @@ def test_train_lenet():
loss
=
train_network
(
data
,
label
)
losses
.
append
(
loss
)
print
(
losses
)
def
create_dataset
(
data_path
,
batch_size
=
32
,
repeat_size
=
1
,
num_parallel_workers
=
1
):
"""
create dataset for train or test
"""
# define dataset
mnist_ds
=
ds
.
MnistDataset
(
data_path
)
resize_height
,
resize_width
=
32
,
32
rescale
=
1.0
/
255.0
shift
=
0.0
rescale_nml
=
1
/
0.3081
shift_nml
=
-
1
*
0.1307
/
0.3081
# define map operations
resize_op
=
CV
.
Resize
((
resize_height
,
resize_width
),
interpolation
=
Inter
.
LINEAR
)
# Bilinear mode
rescale_nml_op
=
CV
.
Rescale
(
rescale_nml
,
shift_nml
)
rescale_op
=
CV
.
Rescale
(
rescale
,
shift
)
hwc2chw_op
=
CV
.
HWC2CHW
()
type_cast_op
=
C
.
TypeCast
(
mstype
.
int32
)
# apply map operations on images
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"label"
,
operations
=
type_cast_op
,
num_parallel_workers
=
num_parallel_workers
)
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"image"
,
operations
=
resize_op
,
num_parallel_workers
=
num_parallel_workers
)
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"image"
,
operations
=
rescale_op
,
num_parallel_workers
=
num_parallel_workers
)
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"image"
,
operations
=
rescale_nml_op
,
num_parallel_workers
=
num_parallel_workers
)
mnist_ds
=
mnist_ds
.
map
(
input_columns
=
"image"
,
operations
=
hwc2chw_op
,
num_parallel_workers
=
num_parallel_workers
)
# apply DatasetOps
buffer_size
=
10000
mnist_ds
=
mnist_ds
.
shuffle
(
buffer_size
=
buffer_size
)
# 10000 as in LeNet train script
mnist_ds
=
mnist_ds
.
batch
(
batch_size
,
drop_remainder
=
True
)
mnist_ds
=
mnist_ds
.
repeat
(
repeat_size
)
return
mnist_ds
@
pytest
.
mark
.
level0
@
pytest
.
mark
.
platform_x86_gpu_training
@
pytest
.
mark
.
env_onecard
def
test_train_and_eval_lenet
():
context
.
set_context
(
mode
=
context
.
GRAPH_MODE
,
device_target
=
"GPU"
,
enable_mem_reuse
=
False
)
network
=
LeNet5
(
10
)
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
net_opt
=
nn
.
Momentum
(
network
.
trainable_params
(),
0.01
,
0.9
)
model
=
Model
(
network
,
net_loss
,
net_opt
,
metrics
=
{
"Accuracy"
:
Accuracy
()})
print
(
"============== Starting Training =============="
)
ds_train
=
create_dataset
(
os
.
path
.
join
(
'/home/workspace/mindspore_dataset/mnist'
,
"train"
),
32
,
1
)
model
.
train
(
1
,
ds_train
,
callbacks
=
[
LossMonitor
()],
dataset_sink_mode
=
True
)
print
(
"============== Starting Testing =============="
)
ds_eval
=
create_dataset
(
os
.
path
.
join
(
'/home/workspace/mindspore_dataset/mnist'
,
"test"
),
32
,
1
)
acc
=
model
.
eval
(
ds_eval
,
dataset_sink_mode
=
True
)
print
(
"============== Accuracy:{} =============="
.
format
(
acc
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录