Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
3beb871b
Unverified
提交
3beb871b
authored
9月 16, 2021
作者:
Jiacong Fang
提交者:
GitHub
9月 16, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Multiple TF export improvements (#4824)
* Add fused conv support * Set all saved_model values to non trainable * Fix TFLite fp16 model export * Fix int8 TFLite conversion
上级
6b44ecd5
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
8 行增加
和
4 行删除
+8
-4
export.py
export.py
+5
-2
tf.py
models/tf.py
+3
-2
没有找到文件。
export.py
浏览文件 @
3beb871b
...
@@ -145,6 +145,7 @@ def export_saved_model(model, im, file, dynamic,
...
@@ -145,6 +145,7 @@ def export_saved_model(model, im, file, dynamic,
inputs
=
keras
.
Input
(
shape
=
(
*
imgsz
,
3
),
batch_size
=
None
if
dynamic
else
batch_size
)
inputs
=
keras
.
Input
(
shape
=
(
*
imgsz
,
3
),
batch_size
=
None
if
dynamic
else
batch_size
)
outputs
=
tf_model
.
predict
(
inputs
,
tf_nms
,
agnostic_nms
,
topk_per_class
,
topk_all
,
iou_thres
,
conf_thres
)
outputs
=
tf_model
.
predict
(
inputs
,
tf_nms
,
agnostic_nms
,
topk_per_class
,
topk_all
,
iou_thres
,
conf_thres
)
keras_model
=
keras
.
Model
(
inputs
=
inputs
,
outputs
=
outputs
)
keras_model
=
keras
.
Model
(
inputs
=
inputs
,
outputs
=
outputs
)
keras_model
.
trainable
=
False
keras_model
.
summary
()
keras_model
.
summary
()
keras_model
.
save
(
f
,
save_format
=
'tf'
)
keras_model
.
save
(
f
,
save_format
=
'tf'
)
...
@@ -183,15 +184,17 @@ def export_tflite(keras_model, im, file, int8, data, ncalib, prefix=colorstr('Te
...
@@ -183,15 +184,17 @@ def export_tflite(keras_model, im, file, int8, data, ncalib, prefix=colorstr('Te
print
(
f
'
\n
{prefix} starting export with tensorflow {tf.__version__}...'
)
print
(
f
'
\n
{prefix} starting export with tensorflow {tf.__version__}...'
)
batch_size
,
ch
,
*
imgsz
=
list
(
im
.
shape
)
# BCHW
batch_size
,
ch
,
*
imgsz
=
list
(
im
.
shape
)
# BCHW
f
=
file
.
with_suffix
(
'
.tflite'
)
f
=
str
(
file
)
.
replace
(
'.pt'
,
'-fp16
.tflite'
)
converter
=
tf
.
lite
.
TFLiteConverter
.
from_keras_model
(
keras_model
)
converter
=
tf
.
lite
.
TFLiteConverter
.
from_keras_model
(
keras_model
)
converter
.
target_spec
.
supported_ops
=
[
tf
.
lite
.
OpsSet
.
TFLITE_BUILTINS
]
converter
.
target_spec
.
supported_ops
=
[
tf
.
lite
.
OpsSet
.
TFLITE_BUILTINS
]
converter
.
target_spec
.
supported_types
=
[
tf
.
float16
]
converter
.
optimizations
=
[
tf
.
lite
.
Optimize
.
DEFAULT
]
converter
.
optimizations
=
[
tf
.
lite
.
Optimize
.
DEFAULT
]
if
int8
:
if
int8
:
dataset
=
LoadImages
(
check_dataset
(
data
)[
'train'
],
img_size
=
imgsz
,
auto
=
False
)
# representative data
dataset
=
LoadImages
(
check_dataset
(
data
)[
'train'
],
img_size
=
imgsz
,
auto
=
False
)
# representative data
converter
.
representative_dataset
=
lambda
:
representative_dataset_gen
(
dataset
,
ncalib
)
converter
.
representative_dataset
=
lambda
:
representative_dataset_gen
(
dataset
,
ncalib
)
converter
.
target_spec
.
supported_ops
=
[
tf
.
lite
.
OpsSet
.
TFLITE_BUILTINS_INT8
]
converter
.
target_spec
.
supported_ops
=
[
tf
.
lite
.
OpsSet
.
TFLITE_BUILTINS_INT8
]
converter
.
target_spec
.
supported_types
=
[]
converter
.
inference_input_type
=
tf
.
uint8
# or tf.int8
converter
.
inference_input_type
=
tf
.
uint8
# or tf.int8
converter
.
inference_output_type
=
tf
.
uint8
# or tf.int8
converter
.
inference_output_type
=
tf
.
uint8
# or tf.int8
converter
.
experimental_new_quantizer
=
False
converter
.
experimental_new_quantizer
=
False
...
@@ -249,7 +252,7 @@ def run(data=ROOT / 'data/coco128.yaml', # 'dataset.yaml path'
...
@@ -249,7 +252,7 @@ def run(data=ROOT / 'data/coco128.yaml', # 'dataset.yaml path'
# Load PyTorch model
# Load PyTorch model
device
=
select_device
(
device
)
device
=
select_device
(
device
)
assert
not
(
device
.
type
==
'cpu'
and
half
),
'--half only compatible with GPU export, i.e. use --device 0'
assert
not
(
device
.
type
==
'cpu'
and
half
),
'--half only compatible with GPU export, i.e. use --device 0'
model
=
attempt_load
(
weights
,
map_location
=
device
,
inplace
=
True
,
fuse
=
not
any
(
tf_exports
)
)
# load FP32 model
model
=
attempt_load
(
weights
,
map_location
=
device
,
inplace
=
True
,
fuse
=
True
)
# load FP32 model
nc
,
names
=
model
.
nc
,
model
.
names
# number of classes, class names
nc
,
names
=
model
.
nc
,
model
.
names
# number of classes, class names
# Input
# Input
...
...
models/tf.py
浏览文件 @
3beb871b
...
@@ -70,8 +70,9 @@ class TFConv(keras.layers.Layer):
...
@@ -70,8 +70,9 @@ class TFConv(keras.layers.Layer):
# see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch
# see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch
conv
=
keras
.
layers
.
Conv2D
(
conv
=
keras
.
layers
.
Conv2D
(
c2
,
k
,
s
,
'SAME'
if
s
==
1
else
'VALID'
,
use_bias
=
False
,
c2
,
k
,
s
,
'SAME'
if
s
==
1
else
'VALID'
,
use_bias
=
False
if
hasattr
(
w
,
'bn'
)
else
True
,
kernel_initializer
=
keras
.
initializers
.
Constant
(
w
.
conv
.
weight
.
permute
(
2
,
3
,
1
,
0
)
.
numpy
()))
kernel_initializer
=
keras
.
initializers
.
Constant
(
w
.
conv
.
weight
.
permute
(
2
,
3
,
1
,
0
)
.
numpy
()),
bias_initializer
=
'zeros'
if
hasattr
(
w
,
'bn'
)
else
keras
.
initializers
.
Constant
(
w
.
conv
.
bias
.
numpy
()))
self
.
conv
=
conv
if
s
==
1
else
keras
.
Sequential
([
TFPad
(
autopad
(
k
,
p
)),
conv
])
self
.
conv
=
conv
if
s
==
1
else
keras
.
Sequential
([
TFPad
(
autopad
(
k
,
p
)),
conv
])
self
.
bn
=
TFBN
(
w
.
bn
)
if
hasattr
(
w
,
'bn'
)
else
tf
.
identity
self
.
bn
=
TFBN
(
w
.
bn
)
if
hasattr
(
w
,
'bn'
)
else
tf
.
identity
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论