In this chapter, we will be working with the Facades dataset, which is available at the following link:
http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/facades.tar.gz.
This dataset contains facade labels and ground truth facade images. A facade is generally the front side of a building, and facade labels are architectural labels of a facade image. We will learn more about facades after we download the dataset. Perform the following commands to download and extract the dataset:
- Download the dataset by executing the following commands:
# Before downloading the dataset navigate to data directory
cd data
# Download the dataset
wget http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/facades.tar.gz
- After downloading the dataset, extract the dataset using the following command:
tar -xvzf facades.tar.gz
The file structure of the downloaded dataset is as follows:
The dataset is divided into training, testing, and validation datasets. Let's work on extracting the images.
Perform the following steps to load the dataset:
- Start by creating a list of .h5 files containing facade labels and another list of .h5 containing facade images as follows:
data_dir_path = os.path.join(data_dir, data_type)
# Get all .h5 files containing training images
facade_photos_h5 = [f for f in os.listdir(os.path.join(data_dir_path, 'images')) if '.h5' in f]
facade_labels_h5 = [f for f in os.listdir(os.path.join(data_dir_path, 'facades')) if '.h5' in f]
- Next, iterate (loop) over the lists to load each image sequentially:
final_facade_photos = None
final_facade_labels = None
for index in range(len(facade_photos_h5)):
All code, following this step, will be inside the preceding for loop.
- Next, load the h5 files containing images and retrieve the Numpy NDArrays of the actual images:
facade_photos_path = data_dir_path + '/images/' +
facade_photos_h5[index]
facade_labels_path = data_dir_path + '/facades/' +
facade_labels_h5[index]
facade_photos = h5py.File(facade_photos_path, 'r')
facade_labels = h5py.File(facade_labels_path, 'r')
- Next, resize the images to the desired image size as follows:
# Resize and normalize images
num_photos = facade_photos['data'].shape[0]
num_labels = facade_labels['data'].shape[0]
all_facades_photos = np.array(facade_photos['data'], dtype=np.float32)
all_facades_photos = all_facades_photos.reshape((num_photos, img_width, img_height, 1)) / 255.0
all_facades_labels = np.array(facade_labels['data'], dtype=np.float32)
all_facades_labels = all_facades_labels.reshape((num_labels, img_width, img_height, 1)) / 255.0
- Next, add the resized images to the final NDArrays:
if final_facade_photos is not None and final_facade_labels is not None:
final_facade_photos = np.concatenate([final_facade_photos,
all_facades_photos], axis=0)
final_facade_labels = np.concatenate([final_facade_labels, all_facades_labels], axis=0)
else:
final_facade_photos = all_facades_photos
final_facade_labels = all_facades_labels
The entire code to load and resize images looks as follows:
def load_dataset(data_dir, data_type, img_width, img_height):
data_dir_path = os.path.join(data_dir, data_type)
# Get all .h5 files containing training images
facade_photos_h5 = [f for f in os.listdir(os.path.join(data_dir_path, 'images')) if '.h5' in f]
facade_labels_h5 = [f for f in os.listdir(os.path.join(data_dir_path, 'facades')) if '.h5' in f]
final_facade_photos = None
final_facade_labels = None
for index in range(len(facade_photos_h5)):
facade_photos_path = data_dir_path + '/images/' + facade_photos_h5[index]
facade_labels_path = data_dir_path + '/facades/' + facade_labels_h5[index]
facade_photos = h5py.File(facade_photos_path, 'r')
facade_labels = h5py.File(facade_labels_path, 'r')
# Resize and normalize images
num_photos = facade_photos['data'].shape[0]
num_labels = facade_labels['data'].shape[0]
all_facades_photos = np.array(facade_photos['data'], dtype=np.float32)
all_facades_photos = all_facades_photos.reshape((num_photos, img_width, img_height, 1)) / 255.0
all_facades_labels = np.array(facade_labels['data'], dtype=np.float32)
all_facades_labels = all_facades_labels.reshape((num_labels, img_width, img_height, 1)) / 255.0
if final_facade_photos is not None and final_facade_labels is not None:
final_facade_photos = np.concatenate([final_facade_photos, all_facades_photos], axis=0)
final_facade_labels = np.concatenate([final_facade_labels, all_facades_labels], axis=0)
else:
final_facade_photos = all_facades_photos
final_facade_labels = all_facades_labels
return final_facade_photos, final_facade_labels
The previous function will load images from the .h5 files inside the training, testing, and validation directories.