In [1]:
import os, sys, math
import numpy as np
from matplotlib import pyplot as plt
if 'google.colab' in sys.modules: # Colab-only Tensorflow version selector
%tensorflow_version 2.x
import tensorflow as tf
print("Tensorflow version " + tf.__version__)
In [0]:
#@title "display utilities [RUN ME]"
def display_9_images_from_dataset(dataset):
plt.figure(figsize=(13,13))
subplot=331
for i, (image, label) in enumerate(dataset):
plt.subplot(subplot)
plt.axis('off')
plt.imshow(image.numpy().astype(np.uint8))
plt.title(label.numpy().decode("utf-8"), fontsize=16)
subplot += 1
if i==8:
break
plt.tight_layout()
plt.subplots_adjust(wspace=0.1, hspace=0.1)
plt.show()
In [0]:
GCS_PATTERN = 'gs://flowers-public/*/*.jpg'
CLASSES = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips'] # flower labels (folder names in the data)
fileset=tf.data.Dataset.list_files to scan the data folderfor filename in fileset:... . Dataset.take(). Like this: for data in dataset.take(10): ....tf.data.Dataset.map to decode the JPEG files. You will find useful TF code snippets below.return image, label" in the decoding function, you will have a Dataset of pairs (image, label).display_9_images_from_dataset function. It expects the Dataset to have (image, label) elements.
In [6]:
nb_images = len(tf.io.gfile.glob(GCS_PATTERN))
print("Pattern matches {} images.".format(nb_images))
### QUESTIONS 1. and 2.
filenames_dataset = tf.data.Dataset.list_files(GCS_PATTERN)
for filename in filenames_dataset.take(10):
print(filename.numpy().decode('utf-8'))
### QUESTION 3.
# copy-pasted from "useful code snippets" below
def decode_jpeg(filename):
bits = tf.io.read_file(filename)
image = tf.image.decode_jpeg(bits)
return image
image_dataset = filenames_dataset.map(decode_jpeg)
for image in image_dataset.take(10):
print(image.numpy().shape)
### QUESTION 4.
# copy-pasted from "useful code snippets" below
def decode_jpeg_and_label(filename):
bits = tf.io.read_file(filename)
image = tf.image.decode_jpeg(bits)
label = tf.strings.split(tf.expand_dims(filename, axis=-1), sep='/')
label = label.values[-2]
return image, label
dataset = filenames_dataset.map(decode_jpeg_and_label)
for image, label in dataset.take(10):
print(image.numpy().shape, label.numpy().decode('utf-8'))
### QUESTION 5.
display_9_images_from_dataset(dataset)
In [0]:
def decode_jpeg(filename):
bits = tf.io.read_file(filename)
image = tf.image.decode_jpeg(bits)
return image
In [0]:
def decode_jpeg_and_label(filename):
bits = tf.io.read_file(filename)
image = tf.image.decode_jpeg(bits)
# parse flower name from containing directory
label = tf.strings.split(tf.expand_dims(filename, axis=-1), sep='/')
label = label.values[-2]
return image, label
author: Martin Gorner
twitter: @martin_gorner
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
This is not an official Google product but sample code provided for an educational purpose