Python Machine Learning 3rd Edition by Sebastian Raschka & Vahid Mirjalili, Packt Publishing Ltd. 2019
Code Repository: https://github.com/rasbt/python-machine-learning-book-3rd-edition
Code License: MIT License
Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s).
%load_ext watermark
%watermark -a "Sebastian Raschka & Vahid Mirjalili" -u -d -p numpy,scipy,matplotlib,tensorflow
Sebastian Raschka & Vahid Mirjalili last updated: 2019-12-06 numpy 1.17.4 scipy 1.3.1 matplotlib 3.1.0 tensorflow 2.0.0
from IPython.display import Image
%matplotlib inline
Image(filename='images/01.png', width=800)
Image(filename='images/02.png', width=500)
#! pip install tensorflow
import tensorflow as tf
print('TensorFlow version:', tf.__version__)
import numpy as np
np.set_printoptions(precision=3)
TensorFlow version: 2.0.0
! python -c 'import tensorflow as tf; print(tf.__version__)'
2.0.0
a = np.array([1, 2, 3], dtype=np.int32)
b = [4, 5, 6]
t_a = tf.convert_to_tensor(a)
t_b = tf.convert_to_tensor(b)
print(t_a)
print(t_b)
tf.Tensor([1 2 3], shape=(3,), dtype=int32) tf.Tensor([4 5 6], shape=(3,), dtype=int32)
tf.is_tensor(a), tf.is_tensor(t_a)
(False, True)
t_ones = tf.ones((2, 3))
t_ones.shape
TensorShape([2, 3])
t_ones.numpy()
array([[1., 1., 1.], [1., 1., 1.]], dtype=float32)
const_tensor = tf.constant([1.2, 5, np.pi], dtype=tf.float32)
print(const_tensor)
tf.Tensor([1.2 5. 3.142], shape=(3,), dtype=float32)
t_a_new = tf.cast(t_a, tf.int64)
print(t_a_new.dtype)
<dtype: 'int64'>
t = tf.random.uniform(shape=(3, 5))
t_tr = tf.transpose(t)
print(t.shape, ' --> ', t_tr.shape)
(3, 5) --> (5, 3)
t = tf.zeros((30,))
t_reshape = tf.reshape(t, shape=(5, 6))
print(t_reshape.shape)
(5, 6)
t = tf.zeros((1, 2, 1, 4, 1))
t_sqz = tf.squeeze(t, axis=(2, 4))
print(t.shape, ' --> ', t_sqz.shape)
(1, 2, 1, 4, 1) --> (1, 2, 4)
tf.random.set_seed(1)
t1 = tf.random.uniform(shape=(5, 2),
minval=-1.0,
maxval=1.0)
t2 = tf.random.normal(shape=(5, 2),
mean=0.0,
stddev=1.0)
t3 = tf.multiply(t1, t2).numpy()
print(t3)
[[-0.27 -0.874] [-0.017 -0.175] [-0.296 -0.139] [-0.727 0.135] [-0.401 0.004]]
t4 = tf.math.reduce_mean(t1, axis=0)
print(t4)
tf.Tensor([0.09 0.207], shape=(2,), dtype=float32)
t5 = tf.linalg.matmul(t1, t2, transpose_b=True)
print(t5.numpy())
[[-1.144 1.115 -0.87 -0.321 0.856] [ 0.248 -0.191 0.25 -0.064 -0.331] [-0.478 0.407 -0.436 0.022 0.527] [ 0.525 -0.234 0.741 -0.593 -1.194] [-0.099 0.26 0.125 -0.462 -0.396]]
t6 = tf.linalg.matmul(t1, t2, transpose_a=True)
print(t6.numpy())
[[-1.711 0.302] [ 0.371 -1.049]]
norm_t1 = tf.norm(t1, ord=2, axis=1).numpy()
print(norm_t1)
[1.046 0.293 0.504 0.96 0.383]
np.sqrt(np.sum(np.square(t1), axis=1))
array([1.046, 0.293, 0.504, 0.96 , 0.383], dtype=float32)
tf.random.set_seed(1)
t = tf.random.uniform((6,))
print(t.numpy())
t_splits = tf.split(t, 3)
[item.numpy() for item in t_splits]
[0.165 0.901 0.631 0.435 0.292 0.643]
[array([0.165, 0.901], dtype=float32), array([0.631, 0.435], dtype=float32), array([0.292, 0.643], dtype=float32)]
tf.random.set_seed(1)
t = tf.random.uniform((5,))
print(t.numpy())
t_splits = tf.split(t, num_or_size_splits=[3, 2])
[item.numpy() for item in t_splits]
[0.165 0.901 0.631 0.435 0.292]
[array([0.165, 0.901, 0.631], dtype=float32), array([0.435, 0.292], dtype=float32)]
A = tf.ones((3,))
B = tf.zeros((2,))
C = tf.concat([A, B], axis=0)
print(C.numpy())
[1. 1. 1. 0. 0.]
A = tf.ones((3,))
B = tf.zeros((3,))
S = tf.stack([A, B], axis=1)
print(S.numpy())
[[1. 0.] [1. 0.] [1. 0.]]
a = [1.2, 3.4, 7.5, 4.1, 5.0, 1.0]
ds = tf.data.Dataset.from_tensor_slices(a)
print(ds)
<TensorSliceDataset shapes: (), types: tf.float32>
for item in ds:
print(item)
tf.Tensor(1.2, shape=(), dtype=float32) tf.Tensor(3.4, shape=(), dtype=float32) tf.Tensor(7.5, shape=(), dtype=float32) tf.Tensor(4.1, shape=(), dtype=float32) tf.Tensor(5.0, shape=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32)
ds_batch = ds.batch(3)
for i, elem in enumerate(ds_batch, 1):
print('batch {}:'.format(i), elem.numpy())
batch 1: [1.2 3.4 7.5] batch 2: [4.1 5. 1. ]
tf.random.set_seed(1)
t_x = tf.random.uniform([4, 3], dtype=tf.float32)
t_y = tf.range(4)
ds_x = tf.data.Dataset.from_tensor_slices(t_x)
ds_y = tf.data.Dataset.from_tensor_slices(t_y)
ds_joint = tf.data.Dataset.zip((ds_x, ds_y))
for example in ds_joint:
print(' x: ', example[0].numpy(),
' y: ', example[1].numpy())
x: [0.165 0.901 0.631] y: 0 x: [0.435 0.292 0.643] y: 1 x: [0.976 0.435 0.66 ] y: 2 x: [0.605 0.637 0.614] y: 3
## method 2:
ds_joint = tf.data.Dataset.from_tensor_slices((t_x, t_y))
for example in ds_joint:
print(' x: ', example[0].numpy(),
' y: ', example[1].numpy())
x: [0.165 0.901 0.631] y: 0 x: [0.435 0.292 0.643] y: 1 x: [0.976 0.435 0.66 ] y: 2 x: [0.605 0.637 0.614] y: 3
ds_trans = ds_joint.map(lambda x, y: (x*2-1.0, y))
for example in ds_trans:
print(' x: ', example[0].numpy(),
' y: ', example[1].numpy())
x: [-0.67 0.803 0.262] y: 0 x: [-0.131 -0.416 0.285] y: 1 x: [ 0.952 -0.13 0.32 ] y: 2 x: [0.21 0.273 0.229] y: 3
tf.random.set_seed(1)
ds = ds_joint.shuffle(buffer_size=len(t_x))
for example in ds:
print(' x: ', example[0].numpy(),
' y: ', example[1].numpy())
x: [0.976 0.435 0.66 ] y: 2 x: [0.435 0.292 0.643] y: 1 x: [0.165 0.901 0.631] y: 0 x: [0.605 0.637 0.614] y: 3
ds = ds_joint.batch(batch_size=3,
drop_remainder=False)
batch_x, batch_y = next(iter(ds))
print('Batch-x: \n', batch_x.numpy())
print('Batch-y: ', batch_y.numpy())
Batch-x: [[0.165 0.901 0.631] [0.435 0.292 0.643] [0.976 0.435 0.66 ]] Batch-y: [0 1 2]
ds = ds_joint.batch(3).repeat(count=2)
for i,(batch_x, batch_y) in enumerate(ds):
print(i, batch_x.shape, batch_y.numpy())
0 (3, 3) [0 1 2] 1 (1, 3) [3] 2 (3, 3) [0 1 2] 3 (1, 3) [3]
ds = ds_joint.repeat(count=2).batch(3)
for i,(batch_x, batch_y) in enumerate(ds):
print(i, batch_x.shape, batch_y.numpy())
0 (3, 3) [0 1 2] 1 (3, 3) [3 0 1] 2 (2, 3) [2 3]
tf.random.set_seed(1)
## Order 1: shuffle -> batch -> repeat
ds = ds_joint.shuffle(4).batch(2).repeat(3)
for i,(batch_x, batch_y) in enumerate(ds):
print(i, batch_x.shape, batch_y.numpy())
0 (2, 3) [2 1] 1 (2, 3) [0 3] 2 (2, 3) [0 3] 3 (2, 3) [1 2] 4 (2, 3) [3 0] 5 (2, 3) [1 2]
tf.random.set_seed(1)
## Order 1: shuffle -> batch -> repeat
ds = ds_joint.shuffle(4).batch(2).repeat(20)
for i,(batch_x, batch_y) in enumerate(ds):
print(i, batch_x.shape, batch_y.numpy())
0 (2, 3) [2 1] 1 (2, 3) [0 3] 2 (2, 3) [0 3] 3 (2, 3) [1 2] 4 (2, 3) [3 0] 5 (2, 3) [1 2] 6 (2, 3) [1 3] 7 (2, 3) [2 0] 8 (2, 3) [1 2] 9 (2, 3) [3 0] 10 (2, 3) [3 0] 11 (2, 3) [2 1] 12 (2, 3) [3 0] 13 (2, 3) [1 2] 14 (2, 3) [3 0] 15 (2, 3) [2 1] 16 (2, 3) [2 3] 17 (2, 3) [0 1] 18 (2, 3) [1 2] 19 (2, 3) [0 3] 20 (2, 3) [0 1] 21 (2, 3) [2 3] 22 (2, 3) [3 2] 23 (2, 3) [0 1] 24 (2, 3) [3 0] 25 (2, 3) [1 2] 26 (2, 3) [1 3] 27 (2, 3) [2 0] 28 (2, 3) [2 1] 29 (2, 3) [0 3] 30 (2, 3) [2 3] 31 (2, 3) [0 1] 32 (2, 3) [3 1] 33 (2, 3) [2 0] 34 (2, 3) [3 2] 35 (2, 3) [1 0] 36 (2, 3) [3 0] 37 (2, 3) [2 1] 38 (2, 3) [0 2] 39 (2, 3) [3 1]
tf.random.set_seed(1)
## Order 2: batch -> shuffle -> repeat
ds = ds_joint.batch(2).shuffle(4).repeat(3)
for i,(batch_x, batch_y) in enumerate(ds):
print(i, batch_x.shape, batch_y.numpy())
0 (2, 3) [0 1] 1 (2, 3) [2 3] 2 (2, 3) [0 1] 3 (2, 3) [2 3] 4 (2, 3) [2 3] 5 (2, 3) [0 1]
tf.random.set_seed(1)
## Order 2: batch -> shuffle -> repeat
ds = ds_joint.batch(2).shuffle(4).repeat(20)
for i,(batch_x, batch_y) in enumerate(ds):
print(i, batch_x.shape, batch_y.numpy())
0 (2, 3) [0 1] 1 (2, 3) [2 3] 2 (2, 3) [0 1] 3 (2, 3) [2 3] 4 (2, 3) [2 3] 5 (2, 3) [0 1] 6 (2, 3) [2 3] 7 (2, 3) [0 1] 8 (2, 3) [2 3] 9 (2, 3) [0 1] 10 (2, 3) [2 3] 11 (2, 3) [0 1] 12 (2, 3) [2 3] 13 (2, 3) [0 1] 14 (2, 3) [2 3] 15 (2, 3) [0 1] 16 (2, 3) [0 1] 17 (2, 3) [2 3] 18 (2, 3) [2 3] 19 (2, 3) [0 1] 20 (2, 3) [0 1] 21 (2, 3) [2 3] 22 (2, 3) [2 3] 23 (2, 3) [0 1] 24 (2, 3) [2 3] 25 (2, 3) [0 1] 26 (2, 3) [2 3] 27 (2, 3) [0 1] 28 (2, 3) [0 1] 29 (2, 3) [2 3] 30 (2, 3) [0 1] 31 (2, 3) [2 3] 32 (2, 3) [2 3] 33 (2, 3) [0 1] 34 (2, 3) [2 3] 35 (2, 3) [0 1] 36 (2, 3) [2 3] 37 (2, 3) [0 1] 38 (2, 3) [0 1] 39 (2, 3) [2 3]
import pathlib
imgdir_path = pathlib.Path('cat_dog_images')
file_list = sorted([str(path) for path in imgdir_path.glob('*.jpg')])
print(file_list)
['cat_dog_images/cat-01.jpg', 'cat_dog_images/cat-02.jpg', 'cat_dog_images/cat-03.jpg', 'cat_dog_images/dog-01.jpg', 'cat_dog_images/dog-02.jpg', 'cat_dog_images/dog-03.jpg']
import matplotlib.pyplot as plt
import os
fig = plt.figure(figsize=(10, 5))
for i,file in enumerate(file_list):
img_raw = tf.io.read_file(file)
img = tf.image.decode_image(img_raw)
print('Image shape: ', img.shape)
ax = fig.add_subplot(2, 3, i+1)
ax.set_xticks([]); ax.set_yticks([])
ax.imshow(img)
ax.set_title(os.path.basename(file), size=15)
# plt.savefig('ch13-catdot-examples.pdf')
plt.tight_layout()
plt.show()
Image shape: (900, 1200, 3) Image shape: (900, 1200, 3) Image shape: (900, 742, 3) Image shape: (800, 1200, 3) Image shape: (800, 1200, 3) Image shape: (900, 1200, 3)
labels = [1 if 'dog' in os.path.basename(file) else 0
for file in file_list]
print(labels)
[0, 0, 0, 1, 1, 1]
ds_files_labels = tf.data.Dataset.from_tensor_slices(
(file_list, labels))
for item in ds_files_labels:
print(item[0].numpy(), item[1].numpy())
b'cat_dog_images/cat-01.jpg' 0 b'cat_dog_images/cat-02.jpg' 0 b'cat_dog_images/cat-03.jpg' 0 b'cat_dog_images/dog-01.jpg' 1 b'cat_dog_images/dog-02.jpg' 1 b'cat_dog_images/dog-03.jpg' 1
def load_and_preprocess(path, label):
image = tf.io.read_file(path)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [img_height, img_width])
image /= 255.0
return image, label
img_width, img_height = 120, 80
ds_images_labels = ds_files_labels.map(load_and_preprocess)
fig = plt.figure(figsize=(10, 5))
for i,example in enumerate(ds_images_labels):
print(example[0].shape, example[1].numpy())
ax = fig.add_subplot(2, 3, i+1)
ax.set_xticks([]); ax.set_yticks([])
ax.imshow(example[0])
ax.set_title('{}'.format(example[1].numpy()),
size=15)
plt.tight_layout()
#plt.savefig('ch13-catdog-dataset.pdf')
plt.show()
(80, 120, 3) 0 (80, 120, 3) 0 (80, 120, 3) 0 (80, 120, 3) 1 (80, 120, 3) 1 (80, 120, 3) 1
! pip install tensorflow-datasets
Requirement already satisfied: tensorflow-datasets in /Users/sebastian/miniconda3/lib/python3.7/site-packages (1.2.0) Requirement already satisfied: dill in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (0.3.1.1) Requirement already satisfied: protobuf>=3.6.1 in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (3.10.1) Requirement already satisfied: promise in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (2.2.1) Requirement already satisfied: numpy in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (1.17.4) Requirement already satisfied: absl-py in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (0.8.1) Requirement already satisfied: wrapt in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (1.11.2) Requirement already satisfied: tensorflow-metadata in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (0.15.0) Requirement already satisfied: termcolor in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (1.1.0) Requirement already satisfied: tqdm in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (4.40.0) Requirement already satisfied: future in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (0.17.1) Requirement already satisfied: psutil in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (5.6.5) Requirement already satisfied: six in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (1.13.0) Requirement already satisfied: attrs in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (19.3.0) Requirement already satisfied: requests>=2.19.0 in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-datasets) (2.22.0) Requirement already satisfied: setuptools in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from protobuf>=3.6.1->tensorflow-datasets) (42.0.2.post20191203) Requirement already satisfied: googleapis-common-protos in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from tensorflow-metadata->tensorflow-datasets) (1.6.0) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from requests>=2.19.0->tensorflow-datasets) (3.0.4) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from requests>=2.19.0->tensorflow-datasets) (1.25.7) Requirement already satisfied: certifi>=2017.4.17 in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from requests>=2.19.0->tensorflow-datasets) (2019.11.28) Requirement already satisfied: idna<2.9,>=2.5 in /Users/sebastian/miniconda3/lib/python3.7/site-packages (from requests>=2.19.0->tensorflow-datasets) (2.8)
import tensorflow_datasets as tfds
print(len(tfds.list_builders()))
print(tfds.list_builders()[:5])
101 ['abstract_reasoning', 'aflw2k3d', 'amazon_us_reviews', 'bair_robot_pushing_small', 'bigearthnet']
## Run this to see the full list:
tfds.list_builders()
['abstract_reasoning', 'aflw2k3d', 'amazon_us_reviews', 'bair_robot_pushing_small', 'bigearthnet', 'binarized_mnist', 'binary_alpha_digits', 'caltech101', 'caltech_birds2010', 'caltech_birds2011', 'cats_vs_dogs', 'celeb_a', 'celeb_a_hq', 'chexpert', 'cifar10', 'cifar100', 'cifar10_corrupted', 'clevr', 'cnn_dailymail', 'coco', 'coco2014', 'coil100', 'colorectal_histology', 'colorectal_histology_large', 'curated_breast_imaging_ddsm', 'cycle_gan', 'deep_weeds', 'definite_pronoun_resolution', 'diabetic_retinopathy_detection', 'downsampled_imagenet', 'dsprites', 'dtd', 'dummy_dataset_shared_generator', 'dummy_mnist', 'emnist', 'eurosat', 'fashion_mnist', 'flores', 'food101', 'gap', 'glue', 'groove', 'higgs', 'horses_or_humans', 'image_label_folder', 'imagenet2012', 'imagenet2012_corrupted', 'imdb_reviews', 'iris', 'kitti', 'kmnist', 'lfw', 'lm1b', 'lsun', 'mnist', 'mnist_corrupted', 'moving_mnist', 'multi_nli', 'nsynth', 'omniglot', 'open_images_v4', 'oxford_flowers102', 'oxford_iiit_pet', 'para_crawl', 'patch_camelyon', 'pet_finder', 'quickdraw_bitmap', 'resisc45', 'rock_paper_scissors', 'rock_you', 'scene_parse150', 'shapes3d', 'smallnorb', 'snli', 'so2sat', 'squad', 'stanford_dogs', 'stanford_online_products', 'starcraft_video', 'sun397', 'super_glue', 'svhn_cropped', 'ted_hrlr_translate', 'ted_multi_translate', 'tf_flowers', 'titanic', 'trivia_qa', 'uc_merced', 'ucf101', 'visual_domain_decathlon', 'voc2007', 'wikipedia', 'wmt14_translate', 'wmt15_translate', 'wmt16_translate', 'wmt17_translate', 'wmt18_translate', 'wmt19_translate', 'wmt_t2t_translate', 'wmt_translate', 'xnli']
Fetching CelebA dataset
celeba_bldr = tfds.builder('celeb_a')
print(celeba_bldr.info.features)
print('\n', 30*"=", '\n')
print(celeba_bldr.info.features.keys())
print('\n', 30*"=", '\n')
print(celeba_bldr.info.features['image'])
print('\n', 30*"=", '\n')
print(celeba_bldr.info.features['attributes'].keys())
print('\n', 30*"=", '\n')
print(celeba_bldr.info.citation)
FeaturesDict({ 'attributes': FeaturesDict({ '5_o_Clock_Shadow': Tensor(shape=(), dtype=tf.bool), 'Arched_Eyebrows': Tensor(shape=(), dtype=tf.bool), 'Attractive': Tensor(shape=(), dtype=tf.bool), 'Bags_Under_Eyes': Tensor(shape=(), dtype=tf.bool), 'Bald': Tensor(shape=(), dtype=tf.bool), 'Bangs': Tensor(shape=(), dtype=tf.bool), 'Big_Lips': Tensor(shape=(), dtype=tf.bool), 'Big_Nose': Tensor(shape=(), dtype=tf.bool), 'Black_Hair': Tensor(shape=(), dtype=tf.bool), 'Blond_Hair': Tensor(shape=(), dtype=tf.bool), 'Blurry': Tensor(shape=(), dtype=tf.bool), 'Brown_Hair': Tensor(shape=(), dtype=tf.bool), 'Bushy_Eyebrows': Tensor(shape=(), dtype=tf.bool), 'Chubby': Tensor(shape=(), dtype=tf.bool), 'Double_Chin': Tensor(shape=(), dtype=tf.bool), 'Eyeglasses': Tensor(shape=(), dtype=tf.bool), 'Goatee': Tensor(shape=(), dtype=tf.bool), 'Gray_Hair': Tensor(shape=(), dtype=tf.bool), 'Heavy_Makeup': Tensor(shape=(), dtype=tf.bool), 'High_Cheekbones': Tensor(shape=(), dtype=tf.bool), 'Male': Tensor(shape=(), dtype=tf.bool), 'Mouth_Slightly_Open': Tensor(shape=(), dtype=tf.bool), 'Mustache': Tensor(shape=(), dtype=tf.bool), 'Narrow_Eyes': Tensor(shape=(), dtype=tf.bool), 'No_Beard': Tensor(shape=(), dtype=tf.bool), 'Oval_Face': Tensor(shape=(), dtype=tf.bool), 'Pale_Skin': Tensor(shape=(), dtype=tf.bool), 'Pointy_Nose': Tensor(shape=(), dtype=tf.bool), 'Receding_Hairline': Tensor(shape=(), dtype=tf.bool), 'Rosy_Cheeks': Tensor(shape=(), dtype=tf.bool), 'Sideburns': Tensor(shape=(), dtype=tf.bool), 'Smiling': Tensor(shape=(), dtype=tf.bool), 'Straight_Hair': Tensor(shape=(), dtype=tf.bool), 'Wavy_Hair': Tensor(shape=(), dtype=tf.bool), 'Wearing_Earrings': Tensor(shape=(), dtype=tf.bool), 'Wearing_Hat': Tensor(shape=(), dtype=tf.bool), 'Wearing_Lipstick': Tensor(shape=(), dtype=tf.bool), 'Wearing_Necklace': Tensor(shape=(), dtype=tf.bool), 'Wearing_Necktie': Tensor(shape=(), dtype=tf.bool), 'Young': Tensor(shape=(), dtype=tf.bool), }), 'image': Image(shape=(218, 178, 3), dtype=tf.uint8), 'landmarks': FeaturesDict({ 'lefteye_x': Tensor(shape=(), dtype=tf.int64), 'lefteye_y': Tensor(shape=(), dtype=tf.int64), 'leftmouth_x': Tensor(shape=(), dtype=tf.int64), 'leftmouth_y': Tensor(shape=(), dtype=tf.int64), 'nose_x': Tensor(shape=(), dtype=tf.int64), 'nose_y': Tensor(shape=(), dtype=tf.int64), 'righteye_x': Tensor(shape=(), dtype=tf.int64), 'righteye_y': Tensor(shape=(), dtype=tf.int64), 'rightmouth_x': Tensor(shape=(), dtype=tf.int64), 'rightmouth_y': Tensor(shape=(), dtype=tf.int64), }), }) ============================== dict_keys(['image', 'landmarks', 'attributes']) ============================== Image(shape=(218, 178, 3), dtype=tf.uint8) ============================== dict_keys(['5_o_Clock_Shadow', 'Arched_Eyebrows', 'Attractive', 'Bags_Under_Eyes', 'Bald', 'Bangs', 'Big_Lips', 'Big_Nose', 'Black_Hair', 'Blond_Hair', 'Blurry', 'Brown_Hair', 'Bushy_Eyebrows', 'Chubby', 'Double_Chin', 'Eyeglasses', 'Goatee', 'Gray_Hair', 'Heavy_Makeup', 'High_Cheekbones', 'Male', 'Mouth_Slightly_Open', 'Mustache', 'Narrow_Eyes', 'No_Beard', 'Oval_Face', 'Pale_Skin', 'Pointy_Nose', 'Receding_Hairline', 'Rosy_Cheeks', 'Sideburns', 'Smiling', 'Straight_Hair', 'Wavy_Hair', 'Wearing_Earrings', 'Wearing_Hat', 'Wearing_Lipstick', 'Wearing_Necklace', 'Wearing_Necktie', 'Young']) ============================== @inproceedings{conf/iccv/LiuLWT15, added-at = {2018-10-09T00:00:00.000+0200}, author = {Liu, Ziwei and Luo, Ping and Wang, Xiaogang and Tang, Xiaoou}, biburl = {https://www.bibsonomy.org/bibtex/250e4959be61db325d2f02c1d8cd7bfbb/dblp}, booktitle = {ICCV}, crossref = {conf/iccv/2015}, ee = {http://doi.ieeecomputersociety.org/10.1109/ICCV.2015.425}, interhash = {3f735aaa11957e73914bbe2ca9d5e702}, intrahash = {50e4959be61db325d2f02c1d8cd7bfbb}, isbn = {978-1-4673-8391-2}, keywords = {dblp}, pages = {3730-3738}, publisher = {IEEE Computer Society}, timestamp = {2018-10-11T11:43:28.000+0200}, title = {Deep Learning Face Attributes in the Wild.}, url = {http://dblp.uni-trier.de/db/conf/iccv/iccv2015.html#LiuLWT15}, year = 2015 }
# Download the data, prepare it, and write it to disk
celeba_bldr.download_and_prepare()
# Load data from disk as tf.data.Datasets
datasets = celeba_bldr.as_dataset(shuffle_files=False)
datasets.keys()
dict_keys(['test', 'train', 'validation'])
#import tensorflow as tf
ds_train = datasets['train']
assert isinstance(ds_train, tf.data.Dataset)
example = next(iter(ds_train))
print(type(example))
print(example.keys())
<class 'dict'> dict_keys(['attributes', 'image', 'landmarks'])
ds_train = ds_train.map(lambda item:
(item['image'], tf.cast(item['attributes']['Male'], tf.int32)))
ds_train = ds_train.batch(18)
images, labels = next(iter(ds_train))
print(images.shape, labels)
(18, 218, 178, 3) tf.Tensor([1 1 0 1 1 1 0 0 0 1 1 0 1 0 1 0 0 0], shape=(18,), dtype=int32)
fig = plt.figure(figsize=(12, 8))
for i,(image,label) in enumerate(zip(images, labels)):
ax = fig.add_subplot(3, 6, i+1)
ax.set_xticks([]); ax.set_yticks([])
ax.imshow(image)
ax.set_title('{}'.format(label), size=15)
plt.show()
Alternative ways for loading a dataset
mnist, mnist_info = tfds.load('mnist', with_info=True,
shuffle_files=False)
print(mnist_info)
print(mnist.keys())
tfds.core.DatasetInfo( name='mnist', version=1.0.0, description='The MNIST database of handwritten digits.', urls=['https://storage.googleapis.com/cvdf-datasets/mnist/'], features=FeaturesDict({ 'image': Image(shape=(28, 28, 1), dtype=tf.uint8), 'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=10), }), total_num_examples=70000, splits={ 'test': 10000, 'train': 60000, }, supervised_keys=('image', 'label'), citation="""@article{lecun2010mnist, title={MNIST handwritten digit database}, author={LeCun, Yann and Cortes, Corinna and Burges, CJ}, journal={ATT Labs [Online]. Available: http://yann. lecun. com/exdb/mnist}, volume={2}, year={2010} }""", redistribution_info=, ) dict_keys(['test', 'train'])
ds_train = mnist['train']
assert isinstance(ds_train, tf.data.Dataset)
ds_train = ds_train.map(lambda item:
(item['image'], item['label']))
ds_train = ds_train.batch(10)
batch = next(iter(ds_train))
print(batch[0].shape, batch[1])
fig = plt.figure(figsize=(15, 6))
for i,(image,label) in enumerate(zip(batch[0], batch[1])):
ax = fig.add_subplot(2, 5, i+1)
ax.set_xticks([]); ax.set_yticks([])
ax.imshow(image[:, :, 0], cmap='gray_r')
ax.set_title('{}'.format(label), size=15)
plt.show()
(10, 28, 28, 1) tf.Tensor([3 4 7 1 9 0 8 8 9 7], shape=(10,), dtype=int64)
Readers may ignore the next cell.
! python ../.convert_notebook_to_script.py --input ch13_part1.ipynb --output ch13_part1.py
[NbConvertApp] Converting notebook ch13_part1.ipynb to script [NbConvertApp] Writing 10679 bytes to ch13_part1.py