Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
闹闹不闹!
Mace
提交
554fd4c0
Mace
项目概览
闹闹不闹!
/
Mace
与 Fork 源项目一致
Fork自
Xiaomi / Mace
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Mace
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
554fd4c0
编写于
11月 21, 2018
作者:
李
李寅
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor image tensor tool
上级
e988e951
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
53 addition
and
39 deletion
+53
-39
tools/image/image_to_tensor.py
tools/image/image_to_tensor.py
+32
-24
tools/image/tensor_to_image.py
tools/image/tensor_to_image.py
+21
-15
未找到文件。
tools/image/image_to_tensor.py
浏览文件 @
554fd4c0
...
...
@@ -3,6 +3,8 @@ import os
import
sys
import
tensorflow
as
tf
# TODO(liyin): use dataset api and estimator with distributed strategy
FLAGS
=
None
...
...
@@ -32,32 +34,38 @@ def parse_args():
def
images_to_tensors
(
input_files
,
image_shape
,
mean_values
=
None
):
for
i
in
xrange
(
len
(
input_files
)):
with
tf
.
Session
()
as
sess
:
with
tf
.
gfile
.
FastGFile
(
input_files
[
i
],
'rb'
)
as
f
:
image_data
=
f
.
read
()
image_data
=
tf
.
image
.
decode_image
(
image_data
,
channels
=
image_shape
[
2
])
if
mean_values
:
image_data
=
tf
.
cast
(
image_data
,
dtype
=
tf
.
float32
)
mean_tensor
=
tf
.
constant
(
mean_values
,
dtype
=
tf
.
float32
,
shape
=
[
1
,
1
,
image_shape
[
2
]])
image_data
=
(
image_data
-
mean_tensor
)
/
255.0
else
:
image_data
=
tf
.
image
.
convert_image_dtype
(
image_data
,
dtype
=
tf
.
float32
)
image_data
=
tf
.
subtract
(
image_data
,
0.5
)
image_data
=
tf
.
multiply
(
image_data
,
2.0
)
with
tf
.
Graph
().
as_default
():
image_data
=
tf
.
placeholder
(
tf
.
string
,
name
=
'input'
)
image_data
=
tf
.
image
.
decode_image
(
image_data
,
channels
=
image_shape
[
2
])
if
mean_values
:
image_data
=
tf
.
cast
(
image_data
,
dtype
=
tf
.
float32
)
mean_tensor
=
tf
.
constant
(
mean_values
,
dtype
=
tf
.
float32
,
shape
=
[
1
,
1
,
image_shape
[
2
]])
image_data
=
(
image_data
-
mean_tensor
)
/
255.0
else
:
image_data
=
tf
.
image
.
convert_image_dtype
(
image_data
,
dtype
=
tf
.
float32
)
image_data
=
tf
.
subtract
(
image_data
,
0.5
)
image_data
=
tf
.
multiply
(
image_data
,
2.0
)
image_data
=
tf
.
expand_dims
(
image_data
,
0
)
image_data
=
tf
.
image
.
resize_bilinear
(
image_data
,
image_shape
[:
2
],
align_corners
=
False
)
image_data
=
tf
.
expand_dims
(
image_data
,
0
)
image_data
=
tf
.
image
.
resize_bilinear
(
image_data
,
image_shape
[:
2
],
align_corners
=
False
)
image
=
sess
.
run
(
image_data
)
output_file
=
os
.
path
.
join
(
FLAGS
.
output_dir
,
os
.
path
.
splitext
(
os
.
path
.
basename
(
input_files
[
i
]))[
0
]
+
'.dat'
)
image
.
tofile
(
output_file
)
with
tf
.
Session
()
as
sess
:
for
i
in
xrange
(
len
(
input_files
)):
with
tf
.
gfile
.
FastGFile
(
input_files
[
i
],
'rb'
)
as
f
:
src_image
=
f
.
read
()
dst_image
=
sess
.
run
(
image_data
,
feed_dict
=
{
'input:0'
:
src_image
})
output_file
=
os
.
path
.
join
(
FLAGS
.
output_dir
,
os
.
path
.
splitext
(
os
.
path
.
basename
(
input_files
[
i
]))[
0
]
+
'.dat'
)
dst_image
.
tofile
(
output_file
)
def
main
(
unused_args
):
...
...
tools/image/tensor_to_image.py
浏览文件 @
554fd4c0
...
...
@@ -4,6 +4,8 @@ import sys
import
numpy
as
np
import
tensorflow
as
tf
# TODO(liyin): use dataset api and estimator with distributed strategy
FLAGS
=
None
...
...
@@ -27,22 +29,26 @@ def parse_args():
def
tensors_to_images
(
input_files
,
image_shape
):
for
i
in
xrange
(
len
(
input_files
)):
with
tf
.
Graph
().
as_default
():
input
=
tf
.
placeholder
(
tf
.
float32
,
shape
=
image_shape
,
name
=
'input'
)
output
=
tf
.
placeholder
(
tf
.
string
,
name
=
'output_file'
)
# use the second channel if it is gray image
if
image_shape
[
2
]
==
2
:
_
,
input
=
tf
.
split
(
input
,
2
,
axis
=
2
)
tensor_data
=
tf
.
image
.
convert_image_dtype
(
input
,
tf
.
uint8
,
saturate
=
True
)
image_data
=
tf
.
image
.
encode_jpeg
(
tensor_data
,
quality
=
100
)
writer
=
tf
.
write_file
(
output
,
image_data
,
name
=
'output_writer'
)
with
tf
.
Session
()
as
sess
:
tensor_data
=
np
.
fromfile
(
input_files
[
i
],
dtype
=
np
.
float32
)
\
.
reshape
(
image_shape
)
# use the second channel if it is gray image
if
image_shape
[
2
]
==
2
:
_
,
tensor_data
=
tf
.
split
(
tensor_data
,
2
,
axis
=
2
)
tensor_data
=
tf
.
image
.
convert_image_dtype
(
tensor_data
,
tf
.
uint8
,
saturate
=
True
)
image_data
=
tf
.
image
.
encode_jpeg
(
tensor_data
,
quality
=
100
)
image
=
sess
.
run
(
image_data
)
output_file
=
os
.
path
.
join
(
FLAGS
.
output_dir
,
os
.
path
.
splitext
(
os
.
path
.
basename
(
input_files
[
i
]))[
0
]
+
'.jpg'
)
writer
=
tf
.
write_file
(
output_file
,
image
)
sess
.
run
(
writer
)
for
i
in
xrange
(
len
(
input_files
)):
input_data
=
np
.
fromfile
(
input_files
[
i
],
dtype
=
np
.
float32
)
\
.
reshape
(
image_shape
)
output_file
=
os
.
path
.
join
(
FLAGS
.
output_dir
,
os
.
path
.
splitext
(
os
.
path
.
basename
(
input_files
[
i
]))[
0
]
+
'.jpg'
)
sess
.
run
(
writer
,
feed_dict
=
{
'input:0'
:
input_data
,
'output_file:0'
:
output_file
})
def
main
(
unused_args
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录