Extracting the dataset

After downloading the dataset, extract the files in the data folder manually, or execute the following commands to extract the files:

# Move to data directory
cd data

# Extract wiki_crop.tar
tar -xvf wiki_crop.tar

The wiki_crop.tar file contains 62,328 images and a wiki.mat file that holds all the labels. The scipy.io library has a method called loadmat, which is a really handy method to load .mat files in Python. Use the following code to load extracted .mat files:

def load_data(wiki_dir, dataset='wiki'):
# Load the wiki.mat file
meta = loadmat(os.path.join(wiki_dir, "{}.mat".format(dataset)))

# Load the list of all files
full_path = meta[dataset][0, 0]["full_path"][0]

# List of Matlab serial date numbers
dob = meta[dataset][0, 0]["dob"][0]

# List of years when photo was taken
photo_taken = meta[dataset][0, 0]["photo_taken"][0] # year

# Calculate age for all dobs
age = [calculate_age(photo_taken[i], dob[i]) for i in range(len(dob))]

# Create a list of tuples containing a pair of an image path and age
images = []
age_list = []
for index, image_path in enumerate(full_path):
images.append(image_path[0])
age_list.append(age[index])

# Return a list of all images and respective age
return images, age_list

The photo_taken variable is a list of years and dob is Matlab's serial date number for the corresponding photo in the list. We can calculate the age of the person from the serial date number and the year the photo was taken. Use the following code to calculate the age:

def calculate_age(taken, dob):
birth = datetime.fromordinal(max(int(dob) - 366, 1))

# assume the photo was taken in the middle of the year
if birth.month < 7:
return taken - birth.year
else:
return taken - birth.year - 1

We have now successfully downloaded and extracted the dataset. In the next section, let's work on the Keras implementation of an Age-cGAN.

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

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