提交 9c5c1ee3 编写于 作者: A Amirsina Torfi

video tutorials

上级 8c171c69
......@@ -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_>`_ |
+----+------------------------------------------+--------------------------+--------------------------------------------------------------------+----------------------------------------+
......
此差异已折叠。
# -*- 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.
先完成此消息的编辑!
想要评论请 注册