Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
曾经的那一瞬间
Models
提交
d58be675
M
Models
项目概览
曾经的那一瞬间
/
Models
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d58be675
编写于
1月 06, 2022
作者:
F
Frederick Liu
提交者:
A. Unique TensorFlower
1月 06, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Internal change
PiperOrigin-RevId: 420130685
上级
ded32f05
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
97 addition
and
1 deletion
+97
-1
official/vision/beta/ops/preprocess_ops.py
official/vision/beta/ops/preprocess_ops.py
+81
-1
official/vision/beta/ops/preprocess_ops_test.py
official/vision/beta/ops/preprocess_ops_test.py
+16
-0
未找到文件。
official/vision/beta/ops/preprocess_ops.py
浏览文件 @
d58be675
...
...
@@ -15,7 +15,7 @@
"""Preprocessing ops."""
import
math
from
typing
import
Optional
from
typing
import
Optional
,
Tuple
,
Union
from
six.moves
import
range
import
tensorflow
as
tf
...
...
@@ -303,6 +303,86 @@ def resize_and_crop_image_v2(image,
return
output_image
,
image_info
def
resize_image
(
image
:
tf
.
Tensor
,
size
:
Union
[
Tuple
[
int
,
int
],
int
],
max_size
:
Optional
[
int
]
=
None
,
method
:
tf
.
image
.
ResizeMethod
=
tf
.
image
.
ResizeMethod
.
BILINEAR
):
"""Resize image with size and max_size.
Args:
image: the image to be resized.
size: if list to tuple, resize to it. If scalar, we keep the same
aspect ratio and resize the short side to the value.
max_size: only used when size is a scalar. When the larger side is larger
than max_size after resized with size we used max_size to keep the aspect
ratio instead.
method: the method argument passed to tf.image.resize.
Returns:
the resized image and image_info to be used for downstream processing.
image_info: a 2D `Tensor` that encodes the information of the image and the
applied preprocessing. It is in the format of
[[original_height, original_width], [resized_height, resized_width],
[y_scale, x_scale], [0, 0]], where [resized_height, resized_width]
is the actual scaled image size, and [y_scale, x_scale] is the
scaling factor, which is the ratio of
scaled dimension / original dimension.
"""
def
get_size_with_aspect_ratio
(
image_size
,
size
,
max_size
=
None
):
h
=
image_size
[
0
]
w
=
image_size
[
1
]
if
max_size
is
not
None
:
min_original_size
=
tf
.
cast
(
tf
.
math
.
minimum
(
w
,
h
),
dtype
=
tf
.
float32
)
max_original_size
=
tf
.
cast
(
tf
.
math
.
maximum
(
w
,
h
),
dtype
=
tf
.
float32
)
if
max_original_size
/
min_original_size
*
size
>
max_size
:
size
=
tf
.
cast
(
tf
.
math
.
floor
(
max_size
*
min_original_size
/
max_original_size
),
dtype
=
tf
.
int32
)
else
:
size
=
tf
.
cast
(
size
,
tf
.
int32
)
else
:
size
=
tf
.
cast
(
size
,
tf
.
int32
)
if
(
w
<=
h
and
w
==
size
)
or
(
h
<=
w
and
h
==
size
):
return
tf
.
stack
([
h
,
w
])
if
w
<
h
:
ow
=
size
oh
=
tf
.
cast
(
(
tf
.
cast
(
size
,
dtype
=
tf
.
float32
)
*
tf
.
cast
(
h
,
dtype
=
tf
.
float32
)
/
tf
.
cast
(
w
,
dtype
=
tf
.
float32
)),
dtype
=
tf
.
int32
)
else
:
oh
=
size
ow
=
tf
.
cast
(
(
tf
.
cast
(
size
,
dtype
=
tf
.
float32
)
*
tf
.
cast
(
w
,
dtype
=
tf
.
float32
)
/
tf
.
cast
(
h
,
dtype
=
tf
.
float32
)),
dtype
=
tf
.
int32
)
return
tf
.
stack
([
oh
,
ow
])
def
get_size
(
image_size
,
size
,
max_size
=
None
):
if
isinstance
(
size
,
(
list
,
tuple
)):
return
size
[::
-
1
]
else
:
return
get_size_with_aspect_ratio
(
image_size
,
size
,
max_size
)
orignal_size
=
tf
.
shape
(
image
)[
0
:
2
]
size
=
get_size
(
orignal_size
,
size
,
max_size
)
rescaled_image
=
tf
.
image
.
resize
(
image
,
tf
.
cast
(
size
,
tf
.
int32
),
method
=
method
)
image_scale
=
size
/
orignal_size
image_info
=
tf
.
stack
([
tf
.
cast
(
orignal_size
,
dtype
=
tf
.
float32
),
tf
.
cast
(
size
,
dtype
=
tf
.
float32
),
tf
.
cast
(
image_scale
,
tf
.
float32
),
tf
.
constant
([
0.0
,
0.0
],
dtype
=
tf
.
float32
)
])
return
rescaled_image
,
image_info
def
center_crop_image
(
image
):
"""Center crop a square shape slice from the input image.
...
...
official/vision/beta/ops/preprocess_ops_test.py
浏览文件 @
d58be675
...
...
@@ -225,6 +225,22 @@ class InputUtilsTest(parameterized.TestCase, tf.test.TestCase):
np
.
random
.
randint
(
low
=
0
,
high
=
num_boxes
,
size
=
(
num_boxes
,)),
tf
.
int64
)
_
=
preprocess_ops
.
random_crop
(
image
,
boxes
,
labels
)
@
parameterized
.
parameters
(
((
640
,
640
,
3
),
(
1000
,
1000
),
None
,
(
1000
,
1000
,
3
)),
((
1280
,
640
,
3
),
320
,
None
,
(
640
,
320
,
3
)),
((
640
,
1280
,
3
),
320
,
None
,
(
320
,
640
,
3
)),
((
640
,
640
,
3
),
320
,
100
,
(
100
,
100
,
3
)))
def
test_resize_image
(
self
,
input_shape
,
size
,
max_size
,
expected_shape
):
resized_img
,
image_info
=
preprocess_ops
.
resize_image
(
tf
.
zeros
((
input_shape
)),
size
,
max_size
)
self
.
assertAllEqual
(
tf
.
shape
(
resized_img
),
expected_shape
)
self
.
assertAllEqual
(
image_info
[
0
],
input_shape
[:
-
1
])
self
.
assertAllEqual
(
image_info
[
1
],
expected_shape
[:
-
1
])
self
.
assertAllEqual
(
image_info
[
2
],
np
.
array
(
expected_shape
[:
-
1
])
/
np
.
array
(
input_shape
[:
-
1
]))
self
.
assertAllEqual
(
image_info
[
3
],
[
0
,
0
])
if
__name__
==
'__main__'
:
tf
.
test
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录