Implement a function that takes in an input matrix, a filter weight matrix, a filter bias, a stride parameter, a dilation parameter, a padding parameter, and returns the output of applying the filter to the input matrix. Check your implementation using the convoluation examples in Tutorial 6.
Answer. [Write your solution here. Add cells as needed.]
The torchvision
package contains a subpackage
torchvision.models
for some pretrained models.
See the documentation at
https://pytorch.org/docs/stable/torchvision/models.html.
We can load the pretrained AlexNet and inspect its architecture as follows.
from torchvision.models import alexnet
net = alexnet(pretrained=True)
print(net) # nicely print the architecture
print(net._modules) # use the _modules store all the child modules
print(net._modules['features']) # prints the features module
In PyTorch, a module can be something as simple as a ReLU activation layer, or as complex as an AlexNet. Note that in theory, we usually do not use a separate layer for the activation function, but this is treated as a separate layer in PyTorch.
(a)
How many modules are in the pretrained AlexNet at the top level?
How many modules are there in the features
module?
Answer. [Write your solution here. Add cells as needed.]
(b) How many filters are there in the first convolutional layer? What are the hyperparameters for the filters?
Answer. [Write your solution here. Add cells as needed.]
(c) How many parameters are used by the network?
Answer. [Write your solution here. Add cells as needed.]
(d)
Load the racoon image scipy.misc.face()
, and use the
pretrained AlexNet to obtain the top 10 predictions together with their
probabilities.
You may find it helpful to refer to
https://pytorch.org/hub/pytorch_vision_alexnet/.
In addition, AlexNet only output values for each index, not values for each
of the 1,000 class names, so you will need to map the indices to string
names to make sense of the predictions.
The file imagenet_class_index.json
can be used for this purpose.
Answer. [Write your solution here. Add cells as needed.]