Question: PLEASE HELP IN PYTHON In this assignment, you use Convolutional Neural Networks ( CNN ) to solve the cat dog problem to visualize how different

PLEASE HELP IN PYTHON
In this assignment, you use Convolutional Neural Networks (CNN) to solve the cat dog problem to visualize how different layers of CNN learn.
The given code is written down below. If possible, fill out and complete the missing sections titled "Write your code here" in part 4. The Hubel and Wiesel Cat Experiment. The parts before that should run fine. Thank you.
import tensorflow as tf
from tensorflow import keras
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
#4. The Hubel and Wiesel Cat Experiment:
#Let's load our saved model (although we already loaded it, but suppose this was a separated file):
json_file = open(r"c:\Users\XXX\OneDrive\Documents\XXX\XXX\all/model.json", 'r')
loaded_model_json = json_file.read()
json_file.close()
cat_brain = keras.models.model_from_json(loaded_model_json)
cat_brain.load_weights(r"c:\Users\XXX\OneDrive\Documents\XXX\XXX\all/model.weights.h5")
print("Loaded model from disk")
#Now, let's create our testing model for this experiment. This model will represent the cortex of our cat's brain (i.e the fisrt layer of our trained model). So, notice that the model will be added only one single layer:
# Initialising the CNN
cortex = keras.models.Sequential()
# Convolution
cortex.add(keras.layers.Conv2D(32,(3,3), input_shape =(50,50,3), activation = 'relu'))
#We added only one layer because we want to evaluate the output of that layer (cortex), instead of the whole network or brain. Thus, we can see which neurons (in this first layer) are more excitable and what patterns get them excited.
#Next, we are going to transfer the weights of the first layer of our loaded model (brain's cortex of our cat) to this new single-layer model, so evaluating the single-layer model is equivalent to internally evaluating the cortex of the cat. We need to create a single-layer model and read its responses, because in the original model those internal values are not accessible (only the outputs of the final layer are):
# Write your code here (one line)
#This code counts how many times each neuron (in the cortex) gets excited with the images in dir 'D:/all/test':
# Write Your Code Here
# Try all the test images (8495) in 'D:/all/test', one by one, with the 'cortex' model (e.g. cortex.predict(img))
# and find out, for each image, which neuron of the model was activated or responded
# with the highest value (the most excited or exitable neuron).
# For each neuron, keep counting how many times it was the most excited or exitable.
# So that when you finish testing all the test images, you can rank the neurons
# by the number of times they got to be the most excited.
# Remember that the output of the 'cortex' model for one single image is of size (1,48,48,32).
#This code finds the 4 most excited neurons in the cortex and create, for each, a blank visual field image with a dot (3x3) in its position:
#Write Your Code Here
# Find the four neurons (in the ranking) that managed to be the most excited neuron, most times during the test.
# Build four 50x50 blank images, one for each field of view of the four most excited neurons during the test.
# For each image, place a 3x3 black patch right at the position
# where its associated neuron lies within the layer of the model 'cortex'.
#This code finds the patches that excited the most excited neurons:
# Write Your code here
# For each of the four neurons, collect six 3x3 patches,
# from any six test images that have caused the neuron to be the most excited.
#Let's plot the results:
#Write your code here
# plot all the calculations above, so your output image will resemble the one below:
#In the figure above, rows represent info about the (4) most excitable neurons in the cortex. First column is the visual field (a blank 50x50 pixel image) with a black dot (actually a 3x3 pixel dot) placed over the patch associated to the respective neuron (which is actually the position of the neuron in the layer). The rest of the columns are examples of the same patch (zoomed in) at the moment when it got the neuron firing (i.e. when the activation pattern that excites the neuron showed up in the patch). Notice how the 6 patterns in each row are consistent (almost equal), this is, the neuron responds only to a specific stimulus in its associated patch, as Hubel and Wiesel discovered in real cats.
PLEASE HELP IN PYTHON In this assignment, you use

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!