-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patha3code.py
64 lines (49 loc) · 1.98 KB
/
a3code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Importing relevant Libraries
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import time
import os
import torchvision
import matplotlib.pyplot as plt
from torchvision import datasets, models, transforms
from torch.nn import functional as F
import copy
from torch.autograd import Variable
###############################################################################
# Feature Extraction using AlexNet pretrained model
class AlexNetFeatures(nn.Module):
'''
Class that loads AlexNet Feature Model ('Convolution layers') with imagenet trained weights
input : image tensors with dimension Lx3x224x224
output : feature tensor with dimension Lx256x6x6
*L - Batch size
'''
def load_weights(self):
an_builtin = torchvision.models.alexnet(pretrained=True) # Loads the pretrained model weights
features_weight_i = [0, 3, 6, 8, 10]
for i in features_weight_i:
self.features[i].weight = an_builtin.features[i].weight
self.features[i].bias = an_builtin.features[i].bias
def __init__(self):
super(AlexNetFeatures, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(64, 192, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
)
self.load_weights() # Copies the weights to AlexNetFeatures model layers
def forward(self, x):
x = self.features(x)
return x