SlideShare a Scribd company logo
1 of 7
Download to read offline
#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 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
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
# 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):
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')

More Related Content

Similar to #Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf

Can someone please explain what the code below is doing and comment on.pdf
Can someone please explain what the code below is doing and comment on.pdfCan someone please explain what the code below is doing and comment on.pdf
Can someone please explain what the code below is doing and comment on.pdfkuldeepkumarapgsi
 
Python program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docxPython program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docxLukeQVdGrantg
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python TricksBryan Helmig
 
Font classification with 5 deep learning models using tensor flow
Font classification with 5 deep learning models using tensor flowFont classification with 5 deep learning models using tensor flow
Font classification with 5 deep learning models using tensor flowDevatanu Banerjee
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Leonardo Borges
 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfactexerode
 
DeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningDeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningMasahiro Sakai
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schoolsDan Bowen
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Coxlachie
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortStefan Marr
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Mozaic Works
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...David Walker
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!José Paumard
 
5707_10_auto-encoder.pptx
5707_10_auto-encoder.pptx5707_10_auto-encoder.pptx
5707_10_auto-encoder.pptxSidoriOne
 
SCons an Introduction
SCons an IntroductionSCons an Introduction
SCons an Introductionslantsixgames
 
R programming lab 1 - jupyter notebook
R programming lab   1 - jupyter notebookR programming lab   1 - jupyter notebook
R programming lab 1 - jupyter notebookAshwini Mathur
 
Hybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskitHybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskitVijayananda Mohire
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnArnaud Joly
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISPDevnology
 

Similar to #Covnet model had been defined class ConvNetNew(torch.nn.Module).pdf (20)

Can someone please explain what the code below is doing and comment on.pdf
Can someone please explain what the code below is doing and comment on.pdfCan someone please explain what the code below is doing and comment on.pdf
Can someone please explain what the code below is doing and comment on.pdf
 
Python program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docxPython program to build deep learning algorithm using a CNNs model to.docx
Python program to build deep learning algorithm using a CNNs model to.docx
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python Tricks
 
Font classification with 5 deep learning models using tensor flow
Font classification with 5 deep learning models using tensor flowFont classification with 5 deep learning models using tensor flow
Font classification with 5 deep learning models using tensor flow
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
 
Codeware
CodewareCodeware
Codeware
 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
 
DeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningDeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep Learning
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Cox
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low Effort
 
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
 
5707_10_auto-encoder.pptx
5707_10_auto-encoder.pptx5707_10_auto-encoder.pptx
5707_10_auto-encoder.pptx
 
SCons an Introduction
SCons an IntroductionSCons an Introduction
SCons an Introduction
 
R programming lab 1 - jupyter notebook
R programming lab   1 - jupyter notebookR programming lab   1 - jupyter notebook
R programming lab 1 - jupyter notebook
 
Hybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskitHybrid quantum classical neural networks with pytorch and qiskit
Hybrid quantum classical neural networks with pytorch and qiskit
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 

Recently uploaded

Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

#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')