text
stringlengths
0
4.99k
100 786M 100 786M 0 0 44.4M 0 0:00:17 0:00:17 --:--:-- 49.6M
image_classification_from_scratch.ipynb MSR-LA - 3467.docx readme[1].txt
kagglecatsanddogs_3367a.zip PetImages
Now we have a PetImages folder which contain two subfolders, Cat and Dog. Each subfolder contains image files for each category.
!ls PetImages
Cat Dog
Filter out corrupted images
When working with lots of real-world image data, corrupted images are a common occurence. Let's filter out badly-encoded images that do not feature the string "JFIF" in their header.
import os
num_skipped = 0
for folder_name in (\"Cat\", \"Dog\"):
folder_path = os.path.join(\"PetImages\", folder_name)
for fname in os.listdir(folder_path):
fpath = os.path.join(folder_path, fname)
try:
fobj = open(fpath, \"rb\")
is_jfif = tf.compat.as_bytes(\"JFIF\") in fobj.peek(10)
finally:
fobj.close()
if not is_jfif:
num_skipped += 1
# Delete corrupted image
os.remove(fpath)
print(\"Deleted %d images\" % num_skipped)
Deleted 1590 images
Generate a Dataset
image_size = (180, 180)
batch_size = 32
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
\"PetImages\",
validation_split=0.2,
subset=\"trainin\",
seed=1337,
image_size=image_size,
batch_size=batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
\"PetImages\",
validation_split=0.2,
subset=\"validation\",
seed=1337,
image_size=image_size,
batch_size=batch_size,
)
Found 23410 files belonging to 2 classes.
Using 18728 files for training.
Found 23410 files belonging to 2 classes.
Using 4682 files for validation.
Visualize the data
Here are the first 9 images in the training dataset. As you can see, label 1 is \"dog\" and label 0 is "cat".
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype(\"uint8\"))
plt.title(int(labels[i]))
plt.axis(\"off\")
png
Using image data augmentation
When you don't have a large image dataset, it's a good practice to artificially introduce sample diversity by applying random yet realistic transformations to the training images, such as random horizontal flipping or small random rotations. This helps expose the model to different aspects of the training data while sl...
data_augmentation = keras.Sequential(
[
layers.RandomFlip(\"horizontal\"),
layers.RandomRotation(0.1),
]
)
Let's visualize what the augmented samples look like, by applying data_augmentation repeatedly to the first image in the dataset:
plt.figure(figsize=(10, 10))
for images, _ in train_ds.take(1):
for i in range(9):
augmented_images = data_augmentation(images)
ax = plt.subplot(3, 3, i + 1)
plt.imshow(augmented_images[0].numpy().astype(\"uint8\"))
plt.axis(\"off\")
png
Standardizing the data
Our image are already in a standard size (180x180), as they are being yielded as contiguous float32 batches by our dataset. However, their RGB channel values are in the [0, 255] range. This is not ideal for a neural network; in general you should seek to make your input values small. Here, we will standardize values to...
Two options to preprocess the data
There are two ways you could be using the data_augmentation preprocessor:
Option 1: Make it part of the model, like this:
inputs = keras.Input(shape=input_shape)
x = data_augmentation(inputs)
x = layers.Rescaling(1./255)(x)