Loading the dataset

Before doing anything else, load the dataset by performing the following steps:

  1. Start by creating a list of image paths, using the glob module as follows:
imagesA = glob(data_dir + '/testA/*.*')
imagesB = glob(data_dir + '/testB/*.*')

We have data from two domains, A and B, which is why we have created two lists.

  1. Next, iterate over the lists. Load, resize, and horizontally flip the images inside the loop, as follows:
allImagesA = []
allImagesB = []

# Iterate over the lists
for
index, filename in enumerate(imagesA):
# Load images
imgA = imread(filename, mode='RGB')
imgB = imread(imagesB[index], mode='RGB')

# Resize images
imgA = imresize(imgA, (128, 128))
imgB = imresize(imgB, (128, 128))

# Randomly horizontally flip images
if np.random.random() > 0.5:
imgA = np.fliplr(imgA)
imgB = np.fliplr(imgB)

allImagesA.append(imgA)
allImagesB.append(imgB)
  1. Now, normalize the images to bring the pixel values to a range between -1 and 1, as follows:
# Normalize images
allImagesA = np.array(allImagesA) / 127.5 - 1.
allImagesB = np.array(allImagesB) / 127.5 - 1.

 

The entire code to load the dataset appears as follows:

def load_images(data_dir):
imagesA = glob(data_dir + '/testA/*.*')
imagesB = glob(data_dir + '/testB/*.*')

allImagesA = []
allImagesB = []

for index, filename in enumerate(imagesA):
# Load images
imgA = imread(filename, mode='RGB')
imgB = imread(imagesB[index], mode='RGB')

# Resize images
imgA = imresize(imgA, (128, 128))
imgB = imresize(imgB, (128, 128))

# Randomly horizontally flip images
if np.random.random() > 0.5:
imgA = np.fliplr(imgA)
imgB = np.fliplr(imgB)

allImagesA.append(imgA)
allImagesB.append(imgB)

# Normalize images
allImagesA = np.array(allImagesA) / 127.5 - 1.
allImagesB = np.array(allImagesB) / 127.5 - 1.

return allImagesA, allImagesB

The preceding function will return two Numpy ndarrays. We will use it to load and preprocess the images before we start the training.

..................Content has been hidden....................

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