Chapter 6: Decoding Images
Activity 17: Predict if an Image Is of a Cat or a Dog
Solution:
- If you look at the name of the images in the dataset, you will find that the images of dogs start with dog followed by '.' and then a number, for example – "dog.123.jpg". Similarly, the images of cats start with cat. So, let's create a function to get the label from the name of the file:
def get_label(file): class_label = file.split('.')[0] if class_label == 'dog': label_vector = [1,0] elif class_label == 'cat': label_vector = [0,1] return label_vector
Then, create a function to read, resize, and preprocess the images:
import os import numpy as np from PIL import Image from tqdm import tqdm from random import shuffle SIZE = 50 def get_data(): data = [] files = os.listdir...