<jupyter_start><jupyter_text>Loading data into ColabIf you want to use Colab notebooks for your HW or projects, you'll inevitably need to get your datasets into the Colbab environment. There are many ways to do this (https://colab.research.google.com/notebooks/io.ipynb) but the easiest tends to be just loading your files into your Drive account. I've gathered a small dataset from Kaggle's Cats-vs-Dogs challenge and made the images available here if you're interested - https://s3.amazonaws.com/anly-590/cats_dogs_small/cats_dogs_small.zipThen, I've uploaded these images directories into my Drive. By mounting my Drive volume, we can access folders and files located in Drive and pull them into a Colab notebook, exactly as if we were accessing local files on a local notebook.<jupyter_code>from google.colab import drive
drive.mount('/content/gdrive/')
plt.imshow(load_img("/content/gdrive/My Drive/anly590-datasets/cats_dogs_small/test/cat/2000.jpg"))

path_train_data = "/content/gdrive/My Drive/anly590-datasets/cats_dogs_small/train/"
path_validation_data = "/content/gdrive/My Drive/anly590-datasets/cats_dogs_small/val"
path_test_data = "/content/gdrive/My Drive/anly590-datasets/cats_dogs_small/test"

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        path_train_data,
        classes = ['cat','dog'],
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')

validation_generator = train_datagen.flow_from_directory(
        path_validation_data,
        classes = ['cat','dog'],
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')

test_generator = test_datagen.flow_from_directory(
        path_test_data,
        classes = ['cat','dog'],
        target_size=(150,150),
        batch_size=32,
        class_mode='binary')<jupyter_output>Found 2002 images belonging to 2 classes.
Found 1002 images belonging to 2 classes.
Found 1007 images belonging to 2 classes.<jupyter_text>Transfer Learning with VGG backbone<jupyter_code>conv_base = VGG16(weights="imagenet",include_top=False,input_shape=(150,150,3))
model = Sequential()
model.add(conv_base)
model.add(Flatten())
model.add(Dense(256, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.summary()
model.trainable_weights<jupyter_output><empty_output><jupyter_text>We'll use the VGG convolutional base, but it is important that we only train the weights on our Dense layers. That is, we want to keep the VGG weights fixed and not adjuste them during training. This is known as "freezing" the weights for transfer learning.<jupyter_code>conv_base.trainable = False
model.trainable_weights<jupyter_output><empty_output><jupyter_text>Now we'll compile and train the model.<jupyter_code>model.compile(loss="binary_crossentropy", optimizer=RMSprop(1e-4),metrics=["acc"])
model.fit_generator(train_generator, steps_per_epoch=100, epochs=5, validation_data=validation_generator,validation_steps=25)<jupyter_output>Epoch 1/5
  3/100 [..............................] - ETA: 17:32 - loss: 0.9418 - acc: 0.5289<jupyter_text>Exercise - CIFAR **10** The CIFAR-10 dataset is an image classification dataset with small images over 10 categories such as truck, bird, airplane, and several more. The dataset is available in Keras and comes with over 50 thousand images. For this exercise, we're going to use only a small sub-sample of the images (one thousand images) to explore the benefits of transfer learning.0. Below, we load in the CIFAR-10 dataset and generate only a tiny subsample of the data that we will use for training.1. Build and train a deep CNN for the subset of CIFAR-10 data. Evaluate your model using any metrics you prefer.2. Use transfer learning to leverage an existing pre-trained convolutional backbone such as VGG (or any other). Note - when you load the backbone model, you'll like have to adjust the input_shape to match the CIFAR-10 images.<jupyter_code>from keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train[0:1000]
y_train = y_train[0:1000]
plt.figure(figsize = (2,2))
plt.imshow(x_train[319])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.097926267281106, 'fraction_numerical': 0.034101382488479264, 'mean_word_length': 4.819034852546917, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '549695', 'license': 'MIT', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1324, 'n_words': 411, 'path': 'Materials/L8 - CNNs 2 + Transfer Learning/TransferLearning.ipynb', 'repo_name': 'WuZhuoran/ANLY-590-Neural-Nets-and-Deep-Learning', 'size': '4340'}