Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QKForex
TensorFlow-Course
提交
9c5c1ee3
T
TensorFlow-Course
项目概览
QKForex
/
TensorFlow-Course
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TensorFlow-Course
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9c5c1ee3
编写于
11月 01, 2020
作者:
A
Amirsina Torfi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
video tutorials
上级
8c171c69
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
441 addition
and
0 deletion
+441
-0
README.rst
README.rst
+9
-0
codes/ipython/advanced/tfrecords.ipynb
codes/ipython/advanced/tfrecords.ipynb
+296
-0
codes/python/advanced/tfrecords.py
codes/python/advanced/tfrecords.py
+136
-0
未找到文件。
README.rst
浏览文件 @
9c5c1ee3
...
...
@@ -406,6 +406,10 @@ Advanced
.. _pythondgenerator: https://github.com/instillai/TensorFlow-Course/blob/master/codes/python/advanced/dataset_generator.py
.. _videodgenerator: https://youtu.be/-YsgMdDPu3g
.. _ipythontfrecords: https://github.com/instillai/TensorFlow-Course/blob/master/codes/ipython/advanced/tfrecords.ipynb
.. _pythontfrecords: https://github.com/instillai/TensorFlow-Course/blob/master/codes/python/advanced/tfrecords.py
.. _videotfrecords: https://youtu.be/zqavy_5QMk8
.. |ctraining| image:: https://colab.research.google.com/assets/colab-badge.svg
:target: https://colab.research.google.com/github/instillai/TensorFlow-Course/blob/master/codes/ipython/advanced/custom_training.ipynb
...
...
@@ -413,6 +417,9 @@ Advanced
.. |dgenerator| image:: https://colab.research.google.com/assets/colab-badge.svg
:target: https://colab.research.google.com/github/instillai/TensorFlow-Course/blob/master/codes/ipython/advanced/dataset_generator.ipynb
.. |dgenerator| image:: https://colab.research.google.com/assets/colab-badge.svg
:target: https://colab.research.google.com/github/instillai/TensorFlow-Course/blob/master/codes/ipython/advanced/tfrecords.ipynb
+----+------------------------------------------+--------------------------+--------------------------------------------------------------------+----------------------------------------+
| # | topic | Run | Source Code | Media |
...
...
@@ -421,6 +428,8 @@ Advanced
+----+------------------------------------------+--------------------------+--------------------------------------------------------------------+----------------------------------------+
| 2 | *Dataset Generator* | |dgenerator| | `Notebook <ipythondgenerator_>`_ / `Python <pythondgenerator_>`_ | `Video Tutorial <videodgenerator_>`_ |
+----+------------------------------------------+--------------------------+--------------------------------------------------------------------+----------------------------------------+
| 2 | *Create TFRecords* | |tfrecords| | `Notebook <ipythontfrecords_>`_ / `Python <pythontfrecords_>`_ | `Video Tutorial <videotfrecords_>`_ |
+----+------------------------------------------+--------------------------+--------------------------------------------------------------------+----------------------------------------+
...
...
codes/ipython/advanced/tfrecords.ipynb
0 → 100644
浏览文件 @
9c5c1ee3
此差异已折叠。
点击以展开。
codes/python/advanced/tfrecords.py
0 → 100644
浏览文件 @
9c5c1ee3
# -*- coding: utf-8 -*-
"""TFRecords.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1p-Nz6v3CyqKSc-QazX1FgvZkamt5T-uC
"""
import
tensorflow
as
tf
from
tensorflow
import
keras
import
numpy
as
np
# Load MNIST data
(
x_train
,
y_train
),
(
x_test
,
y_test
)
=
keras
.
datasets
.
mnist
.
load_data
()
# Preprocessing
x_train
=
x_train
/
255.0
x_test
=
x_test
/
255.0
# Track the data type
dataType
=
x_train
.
dtype
print
(
f
"Data type:
{
dataType
}
"
)
labelType
=
y_test
.
dtype
print
(
f
"Data type:
{
labelType
}
"
)
im_list
=
[]
n_samples_to_show
=
16
c
=
0
for
i
in
range
(
n_samples_to_show
):
im_list
.
append
(
x_train
[
i
])
# Visualization
import
matplotlib.pyplot
as
plt
from
mpl_toolkits.axes_grid1
import
ImageGrid
fig
=
plt
.
figure
(
figsize
=
(
4.
,
4.
))
# Ref: https://matplotlib.org/3.1.1/gallery/axes_grid1/simple_axesgrid.html
grid
=
ImageGrid
(
fig
,
111
,
# similar to subplot(111)
nrows_ncols
=
(
4
,
4
),
# creates 2x2 grid of axes
axes_pad
=
0.1
,
# pad between axes in inch.
)
# Show image grid
for
ax
,
im
in
zip
(
grid
,
im_list
):
# Iterating over the grid returns the Axes.
ax
.
imshow
(
im
,
'gray'
)
plt
.
show
()
# Convert values to compatible tf.Example types.
def
_bytes_feature
(
value
):
"""Returns a bytes_list from a string / byte."""
if
isinstance
(
value
,
type
(
tf
.
constant
(
0
))):
value
=
value
.
numpy
()
# BytesList won't unpack a string from an EagerTensor.
return
tf
.
train
.
Feature
(
bytes_list
=
tf
.
train
.
BytesList
(
value
=
[
value
]))
def
_float_feature
(
value
):
"""Returns a float_list from a float / double."""
return
tf
.
train
.
Feature
(
float_list
=
tf
.
train
.
FloatList
(
value
=
[
value
]))
def
_int64_feature
(
value
):
"""Returns an int64_list from a bool / enum / int / uint."""
return
tf
.
train
.
Feature
(
int64_list
=
tf
.
train
.
Int64List
(
value
=
[
value
]))
# Create the features dictionary.
def
image_example
(
image
,
label
,
dimension
):
feature
=
{
'dimension'
:
_int64_feature
(
dimension
),
'label'
:
_int64_feature
(
label
),
'image_raw'
:
_bytes_feature
(
image
.
tobytes
()),
}
return
tf
.
train
.
Example
(
features
=
tf
.
train
.
Features
(
feature
=
feature
))
record_file
=
'mnistTrain.tfrecords'
n_samples
=
x_train
.
shape
[
0
]
dimension
=
x_train
.
shape
[
1
]
with
tf
.
io
.
TFRecordWriter
(
record_file
)
as
writer
:
for
i
in
range
(
n_samples
):
image
=
x_train
[
i
]
label
=
y_train
[
i
]
tf_example
=
image_example
(
image
,
label
,
dimension
)
writer
.
write
(
tf_example
.
SerializeToString
())
# Create the dataset object from tfrecord file(s)
dataset
=
tf
.
data
.
TFRecordDataset
(
record_file
)
# Decoding function
def
parse_record
(
record
):
name_to_features
=
{
'dimension'
:
tf
.
io
.
FixedLenFeature
([],
tf
.
int64
),
'label'
:
tf
.
io
.
FixedLenFeature
([],
tf
.
int64
),
'image_raw'
:
tf
.
io
.
FixedLenFeature
([],
tf
.
string
),
}
return
tf
.
io
.
parse_single_example
(
record
,
name_to_features
)
def
decode_record
(
record
):
image
=
tf
.
io
.
decode_raw
(
record
[
'image_raw'
],
out_type
=
dataType
,
little_endian
=
True
,
fixed_length
=
None
,
name
=
None
)
label
=
record
[
'label'
]
dimension
=
record
[
'dimension'
]
image
=
tf
.
reshape
(
image
,
(
dimension
,
dimension
))
return
(
image
,
label
)
im_list
=
[]
n_samples_to_show
=
16
c
=
0
for
record
in
dataset
:
c
+=
1
if
c
>
n_samples_to_show
:
break
parsed_record
=
parse_record
(
record
)
decoded_record
=
decode_record
(
parsed_record
)
image
,
label
=
decoded_record
im_list
.
append
(
image
)
# Visualization
import
matplotlib.pyplot
as
plt
from
mpl_toolkits.axes_grid1
import
ImageGrid
fig
=
plt
.
figure
(
figsize
=
(
4.
,
4.
))
# Ref: https://matplotlib.org/3.1.1/gallery/axes_grid1/simple_axesgrid.html
grid
=
ImageGrid
(
fig
,
111
,
# similar to subplot(111)
nrows_ncols
=
(
4
,
4
),
# creates 2x2 grid of axes
axes_pad
=
0.1
,
# pad between axes in inch.
)
# Show image grid
for
ax
,
im
in
zip
(
grid
,
im_list
):
# Iterating over the grid returns the Axes.
ax
.
imshow
(
im
,
'gray'
)
plt
.
show
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录