#Covnet model had been defined
class ConvNetNew(torch.nn.Module):
def __init__(self):
super(ConvNetNew, self).__init__()
#############################################################################
#
# TODO: Complete the network #Note: similar as Task 1
#############################################################################
#
# Block 1: 3 x 175 x 300 --> 32 x 87 x 150
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
# Block 2: 32 x 87 x 150 --> 64 x 43 x 75
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.relu2 = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
# Block 3: 64 x 43 x 75 --> 128 x 21 x 37
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.relu3 = nn.ReLU()
self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=2)
# Block 4: 128 x 21 x 37 --> 256 x 10 x 18
self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1)
self.bn4 = nn.BatchNorm2d(256)
self.relu4 = nn.ReLU()
self.maxpool4 = nn.MaxPool2d(kernel_size=2, stride=2)
#### [3%] ####
# AdaptiveAvgPool: 1 x 1
self.avgpool = nn.AdaptiveAvgPool1d(1)
# Linear layers: 256 x 1 x 1 --> 128
self.fc1 = nn.Linear(256 * 1 * 1, 128)
# Dropout
self.dropout = nn.Dropout(0.5)
#############################################################################
#
# END OF YOUR CODE #
#############################################################################
#
def forward(self, x):
#############################################################################
#
# TODO: implement the fordward #Note: similar as Task 1
#############################################################################
#
# Block 1: 3 x 175 x 300 --> 32 x 87 x 150
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.maxpool1(x)
# Block 2: 32 x 87 x 150 --> 64 x 43 x 75
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
x = self.maxpool2(x)
# Block 3: 64 x 43 x 75 --> 128 x 21 x 37
x = self.conv3(x)
x = self.bn3(x)
x = self.relu3(x)
x = self.maxpool3(x)
# Block 4: 128 x 21 x 37 --> 256 x 10 x 18
x = self.conv4(x)
x = self.bn4(x)
x = self.relu4(x)
x = self.maxpool4(x)
#### [3%] ####
# AdaptiveAvgPool:
self.avgpool = nn.AdaptiveAvgPool1d(1)
# Flatten the output for the linear layers
x = x.view(x.size(0), -1)
# Linear layers: 256 x 1 x 1 --> 256
x = self.fc1(x)
x = self.relu1(x)
x = self.dropout(x)
#############################################################################
#
# END OF YOUR CODE #
#############################################################################
#
return x
#With the defined convolution layers (ConvNetNew()), the whole contrastive learning
framework could be constructed. The encoder_q and encoder_k have the same convolutional
layers. However, the encoder_k will not be optimized by the.
#Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf
1. #Covnet model had been defined
class ConvNetNew(torch.nn.Module):
def __init__(self):
super(ConvNetNew, self).__init__()
#############################################################################
#
# TODO: Complete the network #Note: similar as Task 1
#############################################################################
#
# Block 1: 3 x 175 x 300 --> 32 x 87 x 150
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)
# Block 2: 32 x 87 x 150 --> 64 x 43 x 75
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.relu2 = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)
# Block 3: 64 x 43 x 75 --> 128 x 21 x 37
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.relu3 = nn.ReLU()
self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=2)
# Block 4: 128 x 21 x 37 --> 256 x 10 x 18
self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1)
self.bn4 = nn.BatchNorm2d(256)
2. self.relu4 = nn.ReLU()
self.maxpool4 = nn.MaxPool2d(kernel_size=2, stride=2)
#### [3%] ####
# AdaptiveAvgPool: 1 x 1
self.avgpool = nn.AdaptiveAvgPool1d(1)
# Linear layers: 256 x 1 x 1 --> 128
self.fc1 = nn.Linear(256 * 1 * 1, 128)
# Dropout
self.dropout = nn.Dropout(0.5)
#############################################################################
#
# END OF YOUR CODE #
#############################################################################
#
def forward(self, x):
#############################################################################
#
# TODO: implement the fordward #Note: similar as Task 1
#############################################################################
#
# Block 1: 3 x 175 x 300 --> 32 x 87 x 150
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.maxpool1(x)
3. # Block 2: 32 x 87 x 150 --> 64 x 43 x 75
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
x = self.maxpool2(x)
# Block 3: 64 x 43 x 75 --> 128 x 21 x 37
x = self.conv3(x)
x = self.bn3(x)
x = self.relu3(x)
x = self.maxpool3(x)
# Block 4: 128 x 21 x 37 --> 256 x 10 x 18
x = self.conv4(x)
x = self.bn4(x)
x = self.relu4(x)
x = self.maxpool4(x)
#### [3%] ####
# AdaptiveAvgPool:
self.avgpool = nn.AdaptiveAvgPool1d(1)
# Flatten the output for the linear layers
x = x.view(x.size(0), -1)
# Linear layers: 256 x 1 x 1 --> 256
x = self.fc1(x)
x = self.relu1(x)
x = self.dropout(x)
4. #############################################################################
#
# END OF YOUR CODE #
#############################################################################
#
return x
#With the defined convolution layers (ConvNetNew()), the whole contrastive learning
framework could be constructed. The encoder_q and encoder_k have the same convolutional
layers. However, the encoder_k will not be optimized by the training (the gradient of encoder_k
should be set False). To update the parameters of encoder_k, the parameters will copy
encoder_q's.
class ConLNet(torch.nn.Module):
def __init__(self):
super(ConLNet, self).__init__()
#############################################################################
#
# TODO: Define the convolutional layer q & k like ConvNet without linear layers #[18%]
#############################################################################
#
#create encoder_q and encoder_k
#copy encoder_q's paremeter to encoder_k
# initialize
# not update by gradient
5. def infoNCE(q, k):
# normalizing q and k with size of NxK, N is batch size, K is the negative sample number.
# positive logits: Nx1 multiply the corresponding elements of the matrix and sum them row by
row) on q and k. Hint: use torch.einsum()
# negative logits: NxK (matrix product) on q and the negative sample. Hint: use
torch.einsum()
# concate logits to Nx(1+K)
# apply temperature to logits
# set labels zeros with size of N
# calculate infoNCE by CrossEntropy using nn.CrossEntropyLoss().cuda()
return loss
def forward(self, im1, im2):
# copy encoder_q's paremeter to encoder_k
6. # compute features of encoder_q and encoder_k by im1 and im2
# create the 128 negative sample features by randn, and size is the same as the input
# calculate the loss
#############################################################################
#
# END OF YOUR CODE #
#############################################################################
#
return loss
model = ConLNet()
###########################################
#Step3: Training contrastive learning network. From step1 to step2, the contrastive learning
model and dataload are defined. In this section, we will build up the forward process of
contrastive learning. Without the label of each data, the loss is calculated by comparing the
similarity of the features extracted by two parallel encoders.
##################################################
optimizer = optim.SGD(model.parameters(), lr=learning_rate)
epochs = 100
def contrative_learning(model):
7. for epoch in range(epochs):
for step, (image, imagenorm, label) in enumerate(train_loader):
#############################################################################
#
# TODO: Complete the forward process# Hint: similar as task 1
#############################################################################
#
# calculate the loss by the defined model above
# optimization
print('epoch= {} t loss= {:.3f}'.format(epoch, loss))
#############################################################################
#
# END OF YOUR CODE #
#############################################################################
#
torch.save(distillation_train.state_dict(), './drive/MyDrive/contrastive_model.pkl')