Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MindSpore
book
提交
14ac1fb6
B
book
项目概览
MindSpore
/
book
通知
3
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
14ac1fb6
编写于
4月 02, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
4月 02, 2020
浏览文件
操作
浏览文件
下载
差异文件
!1 update lenet and alexnet
Merge pull request !1 from wukesong/update-lenet-alexnet
上级
c929b056
6e304d04
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
12 addition
and
11 deletion
+12
-11
chapter03/lenet/lenet.py
chapter03/lenet/lenet.py
+0
-1
chapter04/alexnet/alexnet.py
chapter04/alexnet/alexnet.py
+0
-1
chapter04/alexnet/main.py
chapter04/alexnet/main.py
+12
-9
未找到文件。
chapter03/lenet/lenet.py
浏览文件 @
14ac1fb6
...
...
@@ -13,7 +13,6 @@
# limitations under the License.
# ============================================================================
"""LeNet."""
import
mindspore.ops.operations
as
P
import
mindspore.nn
as
nn
from
mindspore.common.initializer
import
TruncatedNormal
...
...
chapter04/alexnet/alexnet.py
浏览文件 @
14ac1fb6
...
...
@@ -14,7 +14,6 @@
# ============================================================================
"""Alexnet."""
from
config
import
alexnet_cfg
as
cfg
import
mindspore.ops.operations
as
P
import
mindspore.nn
as
nn
from
mindspore.common.initializer
import
TruncatedNormal
...
...
chapter04/alexnet/main.py
浏览文件 @
14ac1fb6
...
...
@@ -17,9 +17,9 @@ AlexNet example tutorial
Usage:
python alexnet.py
with --device_target=GPU: After 20 epoch training, the accuracy is up to 80%
with --device_target=Ascend: After 10 epoch training, the accuracy is up to 81%
"""
import
os
import
argparse
from
config
import
alexnet_cfg
as
cfg
from
alexnet
import
AlexNet
...
...
@@ -35,7 +35,7 @@ from mindspore.nn.metrics import Accuracy
from
mindspore.common
import
dtype
as
mstype
def
create_dataset
(
data_path
,
batch_size
=
32
,
repeat_size
=
1
):
def
create_dataset
(
data_path
,
batch_size
=
32
,
repeat_size
=
1
,
mode
=
"train"
):
"""
create dataset for train or test
"""
...
...
@@ -46,21 +46,23 @@ def create_dataset(data_path, batch_size=32, repeat_size=1):
resize_op
=
CV
.
Resize
((
cfg
.
image_height
,
cfg
.
image_width
))
rescale_op
=
CV
.
Rescale
(
rescale
,
shift
)
normalize_op
=
CV
.
Normalize
((
0.4914
,
0.4822
,
0.4465
),
(
0.2023
,
0.1994
,
0.2010
))
random_crop_op
=
CV
.
RandomCrop
([
32
,
32
],
[
4
,
4
,
4
,
4
])
random_horizontal_op
=
CV
.
RandomHorizontalFlip
()
if
mode
==
"train"
:
random_crop_op
=
CV
.
RandomCrop
([
32
,
32
],
[
4
,
4
,
4
,
4
])
random_horizontal_op
=
CV
.
RandomHorizontalFlip
()
channel_swap_op
=
CV
.
HWC2CHW
()
typecast_op
=
C
.
TypeCast
(
mstype
.
int32
)
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"label"
,
operations
=
typecast_op
)
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"image"
,
operations
=
random_crop_op
)
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"image"
,
operations
=
random_horizontal_op
)
if
mode
==
"train"
:
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"image"
,
operations
=
random_crop_op
)
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"image"
,
operations
=
random_horizontal_op
)
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"image"
,
operations
=
resize_op
)
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"image"
,
operations
=
rescale_op
)
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"image"
,
operations
=
normalize_op
)
cifar_ds
=
cifar_ds
.
map
(
input_columns
=
"image"
,
operations
=
channel_swap_op
)
cifar_ds
=
cifar_ds
.
shuffle
(
buffer_size
=
cfg
.
buffer_size
)
cifar_ds
=
cifar_ds
.
repeat
(
repeat_size
)
cifar_ds
=
cifar_ds
.
batch
(
batch_size
,
drop_remainder
=
True
)
cifar_ds
=
cifar_ds
.
repeat
(
repeat_size
)
return
cifar_ds
...
...
@@ -88,7 +90,8 @@ if __name__ == "__main__":
print
(
"============== Starting Training =============="
)
ds_train
=
create_dataset
(
args
.
data_path
,
cfg
.
batch_size
,
repeat_size
)
repeat_size
,
args
.
mode
)
config_ck
=
CheckpointConfig
(
save_checkpoint_steps
=
cfg
.
save_checkpoint_steps
,
keep_checkpoint_max
=
cfg
.
keep_checkpoint_max
)
ckpoint_cb
=
ModelCheckpoint
(
prefix
=
"checkpoint_alexnet"
,
directory
=
args
.
ckpt_path
,
config
=
config_ck
)
...
...
@@ -98,7 +101,7 @@ if __name__ == "__main__":
print
(
"============== Starting Testing =============="
)
param_dict
=
load_checkpoint
(
args
.
ckpt_path
)
load_param_into_net
(
network
,
param_dict
)
ds_eval
=
create_dataset
(
args
.
data_path
)
ds_eval
=
create_dataset
(
args
.
data_path
,
mode
=
args
.
mode
)
acc
=
model
.
eval
(
ds_eval
,
dataset_sink_mode
=
args
.
dataset_sink_mode
)
print
(
"============== Accuracy:{} =============="
.
format
(
acc
))
else
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录