Inception model transfer values

As we mentioned earlier, we will be using the pre-trained inception model on the ImageNet dataset. So, we need to download this pre-trained model from the internet.

Let's start off by defining data_dir for the inception model:

inception.data_dir = 'inception/'

The weights of the pre-trained inception model are about 85 MB. The following line of code will download it if it doesn't exist in the data_dir defined previously:

inception.maybe_download()

Downloading Inception v3 Model ...
- Download progress: 100%

We will load the inception model so that we can use it as a feature extractor for our CIFAR-10 images:

# Loading the inception model so that we can inialized it with the pre-trained weights and customize for our model
inception_model = inception.Inception()

As we mentioned previously, calculating the transfer values for the CIFAR-10 dataset will take some time, so we need to cache them for future use. Thankfully, there's a helper function in the inception module that can help us do that:

from inception import transfer_values_cache

Next up, we need to set the file paths for the cached training and testing files:

file_path_train = os.path.join(cifar10.data_path, 'inception_cifar10_train.pkl')
file_path_test = os.path.join(cifar10.data_path, 'inception_cifar10_test.pkl')
print("Processing Inception transfer-values for the training images of Cifar-10 ...")
# First we need to scale the imgs to fit the Inception model requirements as it requires all pixels to be from 0 to 255,
# while our training examples of the CIFAR-10 pixels are between 0.0 and 1.0
imgs_scaled = training_images * 255.0

# Checking if the transfer-values for our training images are already calculated and loading them, if not calculate and save them.
transfer_values_training = transfer_values_cache(cache_path=file_path_train,
images=imgs_scaled,
model=inception_model)
print("Processing Inception transfer-values for the testing images of Cifar-10 ...")
# First we need to scale the imgs to fit the Inception model requirements as it requires all pixels to be from 0 to 255,
# while our training examples of the CIFAR-10 pixels are between 0.0 and 1.0
imgs_scaled = testing_images * 255.0
# Checking if the transfer-values for our training images are already calculated and loading them, if not calcaulate and save them.
transfer_values_testing = transfer_values_cache(cache_path=file_path_test,
images=imgs_scaled,
model=inception_model)

As mentioned before, we have 50,000 images in the training set of the CIFAR-10 dataset. So let's check the shapes of the transfer values of these images. It should be 2,048 for each image in this training set:

transfer_values_training.shape

Output:

(50000, 2048)

We need to do the same for the test set:

transfer_values_testing.shape

Output:

(10000, 2048)

To intuitively understand how the transfer values look, we are going to define a helper function to enable us to use the plot the transfer values of a specific image from the training or the testing sets:

def plot_transferValues(ind):
print("Original input image:")

# Plot the image at index ind of the test set.
plt.imshow(testing_images[ind], interpolation='nearest')
plt.show()

print("Transfer values using Inception model:")

# Visualize the transfer values as an image.
transferValues_img = transfer_values_testing[ind]
transferValues_img = transferValues_img.reshape((32, 64))

# Plotting the transfer values image.
plt.imshow(transferValues_img, interpolation='nearest', cmap='Reds')
plt.show()
plot_transferValues(i=16)

Input image:

Figure 10.5: Input image

Transfer values for the image using the inception model:

Figure 10.6: Transfer values for the input image in Figure 10.3
plot_transferValues(i=17)


Figure 10.7: Input image

Transfer values for the image using the inception model:


Figure 10.8: Transfer values for the input image in Figure 10.5
..................Content has been hidden....................

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