-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
298 lines (221 loc) · 10.4 KB
/
train.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from __future__ import print_function
import os
# Initialize model, optimizer, and InfoNCE loss
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = MultiWayTransformerWithInfoNCELoss().to(device)
optimizer = optim.Adam(model.parameters(), lr=1e-4)
info_nce_loss = InfoNCELoss(temperature=0.07)
# Path to save the best model
best_model_path = '/content/drive/MyDrive/best_vtmo_model.pth'
# Load the best model if it exists
if os.path.exists(best_model_path):
model.load_state_dict(torch.load(best_model_path, map_location=device))
print("Loaded the best model from memory.")
# Training loop with model saving
num_epochs = 15
best_val_loss = float('inf')
for epoch in range(num_epochs):
model.train()
for concatenated_img, _ in train_loader:
# Split the concatenated images into separate image and touch sensor parts
img, touch_img = torch.chunk(concatenated_img, 2, dim=1)
img, touch_img = img.to(device), touch_img.to(device)
# Forward pass
img_out, touch_out = model(img, touch_img)
# Compute InfoNCE loss
loss = info_nce_loss(img_out, touch_out)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f"Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}")
# Validation loop with model saving
model.eval()
val_loss = 0
with torch.no_grad():
for concatenated_img, _ in val_loader:
img, touch_img = torch.chunk(concatenated_img, 2, dim=1)
img, touch_img = img.to(device), touch_img.to(device)
img_out, touch_out = model(img, touch_img)
val_loss += info_nce_loss(img_out, touch_out).item()
val_loss /= len(val_loader)
print(f"Validation Loss: {val_loss:.4f}")
# Save the model if validation loss is the best we've seen so far
if val_loss < best_val_loss:
best_val_loss = val_loss
torch.save(model.state_dict(), best_model_path)
print(f"Best model saved with validation loss: {best_val_loss:.4f}")
# Path to the saved best model
best_model_path = '/content/drive/MyDrive/best_vtmo_model.pth'
# Initialize the model and load the best model weights if available
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = MultiWayTransformerWithInfoNCELoss().to(device)
if torch.cuda.is_available():
model.load_state_dict(torch.load(best_model_path))
else:
model.load_state_dict(torch.load(best_model_path, map_location=device))
print("Loaded the best validation model for testing.")
# Set the model to evaluation mode
model.eval()
# Initialize counters for accuracy
correct_matches = 0
total_samples = 0
temperature = 0.07 # Use the same temperature as in training
with torch.no_grad():
for concatenated_img, _ in test_loader: # Assuming `test_loader` is defined
# Split the concatenated images into separate image and touch sensor parts
img, touch_img = torch.chunk(concatenated_img, 2, dim=1)
img, touch_img = img.to(device), touch_img.to(device)
# Forward pass
img_out, touch_out = model(img, touch_img)
# Normalize outputs to calculate cosine similarity
img_out = F.normalize(img_out, dim=-1)
touch_out = F.normalize(touch_out, dim=-1)
# Calculate cosine similarity matrix (batch_size x batch_size)
similarity_matrix = torch.matmul(img_out, touch_out.T) / temperature
# For each image, check if the highest similarity is with the correct tactile pair
batch_size = similarity_matrix.size(0)
total_samples += batch_size
# Find the indices of the maximum values along each row (prediction)
predicted_indices = similarity_matrix.argmax(dim=1)
# Check if each prediction is the correct index
correct_matches += (predicted_indices ==
torch.arange(batch_size, device=device)).sum().item()
# Calculate accuracy
accuracy = correct_matches / total_samples
print(f"Test Accuracy: {accuracy * 100:.2f}%")
# !pip install ptflops
# Define a wrapper for compatibility with ptflops
class MultiWayTransformerWrapper(nn.Module):
def __init__(self, model):
super(MultiWayTransformerWrapper, self).__init__()
self.model = model
def forward(self, x):
# Split the input tensor into `img` and `touch_img`
img, touch_img = torch.chunk(x, 2, dim=1)
return self.model(img, touch_img)
# Initialize the original model and the wrapper
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = MultiWayTransformerWithInfoNCELoss().to(device)
model_wrapper = MultiWayTransformerWrapper(model).to(device)
# Define the input shape: concatenating two inputs of shape (3, 224, 224) along the channel dimension
input_shape = (6, 224, 224) # Concatenate img and touch_img channels
# Calculate FLOPs and Params using the wrapper
with torch.cuda.device(0):
flops, params = get_model_complexity_info(
model_wrapper, input_shape, as_strings=True, print_per_layer_stat=True)
print(f"FLOPs: {flops}")
print(f"Parameters: {params}")
# Now use the same methods and loss to train a model that consists of two encoders (initialized from Beit-base).
# Define the model with two Beit-based encoders
class DualEncoderModel(nn.Module):
def __init__(self):
super(DualEncoderModel, self).__init__()
# Initialize two Beit-base models as encoders
self.img_encoder = BeitModel.from_pretrained(
"microsoft/beit-base-patch16-224")
self.touch_encoder = BeitModel.from_pretrained(
"microsoft/beit-base-patch16-224")
def forward(self, img, touch_img):
# Get CLS token embeddings from both encoders
img_outputs = self.img_encoder(img)
touch_outputs = self.touch_encoder(touch_img)
# Extract the CLS token as the final representation
# CLS token for visual image
img_out = img_outputs.last_hidden_state[:, 0]
# CLS token for tactile image
touch_out = touch_outputs.last_hidden_state[:, 0]
return img_out, touch_out
# Initialize model, optimizer, and InfoNCE loss
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = DualEncoderModel().to(device)
optimizer = optim.Adam(model.parameters(), lr=1e-4)
info_nce_loss = InfoNCELoss(temperature=0.07)
# Path to save the best model
best_model_path = '/content/drive/MyDrive/best_dual_encoder_model.pth'
# Training loop with model saving
num_epochs = 15
best_val_loss = float('inf')
for epoch in range(num_epochs):
model.train()
for concatenated_img, _ in train_loader: # Assuming train_loader is defined
# Split the concatenated images into separate image and touch sensor parts
img, touch_img = torch.chunk(concatenated_img, 2, dim=1)
img, touch_img = img.to(device), touch_img.to(device)
# Forward pass
img_out, touch_out = model(img, touch_img)
# Compute InfoNCE loss
loss = info_nce_loss(img_out, touch_out)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f"Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}")
# Validation loop with model saving
model.eval()
val_loss = 0
with torch.no_grad():
for concatenated_img, _ in val_loader: # Assuming val_loader is defined
img, touch_img = torch.chunk(concatenated_img, 2, dim=1)
img, touch_img = img.to(device), touch_img.to(device)
img_out, touch_out = model(img, touch_img)
val_loss += info_nce_loss(img_out, touch_out).item()
val_loss /= len(val_loader)
print(f"Validation Loss: {val_loss:.4f}")
# Save the model if validation loss is the best we've seen so far
if val_loss < best_val_loss:
best_val_loss = val_loss
torch.save(model.state_dict(), best_model_path)
print(f"Best model saved with validation loss: {best_val_loss:.4f}")
# Initialize the model and load the best model weights if available
device = 'cuda' if torch.cuda.is_available() else 'cpu'
if torch.cuda.is_available():
model.load_state_dict(torch.load(best_model_path))
else:
model.load_state_dict(torch.load(best_model_path, map_location=device))
print("Loaded the best validation model for testing.")
# Set the model to evaluation mode
model.eval()
# Initialize counters for accuracy
correct_matches = 0
total_samples = 0
temperature = 0.07 # Use the same temperature as in training
with torch.no_grad():
for concatenated_img, _ in test_loader: # Assuming `test_loader` is defined
# Split the concatenated images into separate image and touch sensor parts
img, touch_img = torch.chunk(concatenated_img, 2, dim=1)
img, touch_img = img.to(device), touch_img.to(device)
# Forward pass
img_out, touch_out = model(img, touch_img)
# Normalize outputs to calculate cosine similarity
img_out = F.normalize(img_out, dim=-1)
touch_out = F.normalize(touch_out, dim=-1)
# Calculate cosine similarity matrix (batch_size x batch_size)
similarity_matrix = torch.matmul(img_out, touch_out.T) / temperature
# For each image, check if the highest similarity is with the correct tactile pair
batch_size = similarity_matrix.size(0)
total_samples += batch_size
# Find the indices of the maximum values along each row (prediction)
predicted_indices = similarity_matrix.argmax(dim=1)
# Check if each prediction is the correct index
correct_matches += (predicted_indices ==
torch.arange(batch_size, device=device)).sum().item()
# Calculate accuracy
accuracy = correct_matches / total_samples
print(f"Test Accuracy: {accuracy * 100:.2f}%")
# Based on the model in baseline
# Initialize the DualEncoderModel and the wrapper
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = DualEncoderModel().to(device)
model_wrapper = DualEncoderWrapper(model).to(device)
# Define the input shape: concatenating two inputs of shape (3, 224, 224) along the channel dimension
input_shape = (6, 224, 224) # Concatenate img and touch_img channels
# Calculate FLOPs and Params using the wrapper
with torch.cuda.device(0):
flops, params = get_model_complexity_info(
model_wrapper, input_shape, as_strings=True, print_per_layer_stat=True)
print(f"FLOPs: {flops}")