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 The Hubel and Wiesel Cat Experiment. The parts before that should run fine. Thank you.
import tensorflow as tf
from tensorflow import keras
import cv
import numpy as np
import os
import matplotlib.pyplot as plt
# The Hubel and Wiesel Cat Experiment:
#Let's load our saved model although we already loaded it but suppose this was a separated file:
jsonfile openrc:UsersXXXOneDriveDocumentsXXXXXXallmodeljson", r
loadedmodeljson jsonfile.read
jsonfile.close
catbrain keras.models.modelfromjsonloadedmodeljson
catbrain.loadweightsrc:UsersXXXOneDriveDocumentsXXXXXXallmodelweights.h
printLoaded model from disk"
#Now, let's create our testing model for this experiment. This model will represent the cortex of our cat's brain ie 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.addkeraslayers.ConvD inputshape 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 brains cortex of our cat to this new singlelayer model, so evaluating the singlelayer model is equivalent to internally evaluating the cortex of the cat. We need to create a singlelayer 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:alltest:
# Write Your Code Here
# Try all the test images in D:alltest one by one, with the 'cortex' model eg cortex.predictimg
# 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
#This code finds the most excited neurons in the cortex and create, for each, a blank visual field image with a dot x 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 x blank images, one for each field of view of the four most excited neurons during the test.
# For each image, place a x 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 x 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 most excitable neurons in the cortex. First column is the visual field a blank x pixel image with a black dot actually a x 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 ie when the activation pattern that excites the neuron showed up in the patch Notice how the 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.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
