Data analysis and preprocessing

For this task, we will be using SVHN dataset, which is an abbreviation for Street View House Numbers by Stanford (http://ufldl.stanford.edu/housenumbers/). So, let's start the implementation by importing the required packages for this implementation:

# Lets start by loading the necessary libraries
%matplotlib inline

import pickle as pkl
import time
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import loadmat
import tensorflow as tf
import os

Next up, we are going to define a helper class to download the SVHN dataset (remember that you need to manually create the input_data_dir first):

from urllib.request import urlretrieve
from os.path import isfile, isdir
from tqdm import tqdm
input_data_dir = 'input/'

input_data_dir = 'input/'

if not isdir(input_data_dir):
raise Exception("Data directory doesn't exist!")

class DLProgress(tqdm):
last_block = 0

def hook(self, block_num=1, block_size=1, total_size=None):
self.total = total_size
self.update((block_num - self.last_block) * block_size)
self.last_block = block_num

if not isfile(input_data_dir + "train_32x32.mat"):
with DLProgress(unit='B', unit_scale=True, miniters=1, desc='SVHN Training Set') as pbar:
urlretrieve(
'http://ufldl.stanford.edu/housenumbers/train_32x32.mat',
input_data_dir + 'train_32x32.mat',
pbar.hook)

if not isfile(input_data_dir + "test_32x32.mat"):
with DLProgress(unit='B', unit_scale=True, miniters=1, desc='SVHN Training Set') as pbar:
urlretrieve(
'http://ufldl.stanford.edu/housenumbers/test_32x32.mat',
input_data_dir + 'test_32x32.mat',
pbar.hook)


train_data = loadmat(input_data_dir + 'train_32x32.mat')
test_data = loadmat(input_data_dir + 'test_32x32.mat')
Output:
trainset shape: (32, 32, 3, 73257)
testset shape: (32, 32, 3, 26032)

Let's get a sense of what these images look like:

indices = np.random.randint(0, train_data['X'].shape[3], size=36)
fig, axes = plt.subplots(6, 6, sharex=True, sharey=True, figsize=(5,5),)
for ii, ax in zip(indices, axes.flatten()):
ax.imshow(train_data['X'][:,:,:,ii], aspect='equal')
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
plt.subplots_adjust(wspace=0, hspace=0)
Output:
Figure 7: Sample images from the SVHN dataset.

Next up, we need to scale our images to be between -1 and 1, and this will be necessary since we are going to use the tanh() function, which will squash the output values of the generator:

# Scaling the input images
def scale_images(image, feature_range=(-1, 1)):
# scale image to (0, 1)
image = ((image - image.min()) / (255 - image.min()))

# scale the image to feature range
min, max = feature_range
image = image * (max - min) + min
return image
class Dataset:
def __init__(self, train_set, test_set, validation_frac=0.5, shuffle_data=True, scale_func=None):
split_ind = int(len(test_set['y']) * (1 - validation_frac))
self.test_input, self.valid_input = test_set['X'][:, :, :, :split_ind], test_set['X'][:, :, :, split_ind:]
self.test_target, self.valid_target = test_set['y'][:split_ind], test_set['y'][split_ind:]
self.train_input, self.train_target = train_set['X'], train_set['y']

# The street house number dataset comes with lots of labels,
# but because we are going to do semi-supervised learning we are going to assume that we don't have all labels
# like, assume that we have only 1000
self.label_mask = np.zeros_like(self.train_target)
self.label_mask[0:1000] = 1

self.train_input = np.rollaxis(self.train_input, 3)
self.valid_input = np.rollaxis(self.valid_input, 3)
self.test_input = np.rollaxis(self.test_input, 3)

if scale_func is None:
self.scaler = scale_images
else:
self.scaler = scale_func
self.train_input = self.scaler(self.train_input)
self.valid_input = self.scaler(self.valid_input)
self.test_input = self.scaler(self.test_input)
self.shuffle = shuffle_data

def batches(self, batch_size, which_set="train"):
input_name = which_set + "_input"
target_name = which_set + "_target"

num_samples = len(getattr(dataset, target_name))
if self.shuffle:
indices = np.arange(num_samples)
np.random.shuffle(indices)
setattr(dataset, input_name, getattr(dataset, input_name)[indices])
setattr(dataset, target_name, getattr(dataset, target_name)[indices])
if which_set == "train":
dataset.label_mask = dataset.label_mask[indices]

dataset_input = getattr(dataset, input_name)
dataset_target = getattr(dataset, target_name)

for jj in range(0, num_samples, batch_size):
input_vals = dataset_input[jj:jj + batch_size]
target_vals = dataset_target[jj:jj + batch_size]

if which_set == "train":
# including the label mask in case of training
# to pretend that we don't have all the labels
yield input_vals, target_vals, self.label_mask[jj:jj + batch_size]
else:
yield input_vals, target_vals
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset