-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod3.py
447 lines (320 loc) · 14.2 KB
/
method3.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import numpy as np
import pandas as pd
import scipy.io
from torchmetrics.image import StructuralSimilarityIndexMeasure
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from PIL import Image
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import pickle
data = scipy.io.loadmat('./CSI1_ROIs_TR1.mat')
data2 = scipy.io.loadmat('./CSI1_ROIs_TR2.mat')
bands = ['LHPPA', 'RHLOC', 'LHLOC', 'RHEarlyVis', 'RHRSC', 'RHOPA',
'RHPPA', 'LHEarlyVis', 'LHRSC', 'LHOPA']
total_data = []
for i in range(5254):
curr_image_data = []
for band in bands:
curr_image_data.extend(data[band][i])
total_data.append(curr_image_data)
for i in range(5254):
curr_image_data = []
for band in bands:
curr_image_data.extend(data2[band][i])
total_data.append(curr_image_data)
file_path = "./CSI01_stim_lists.txt" # Replace with the path to your specific text file
file_strings = []
with open(file_path, "r") as file:
lines = file.readlines()
cleaned_lines = [line.strip() for line in lines] # Remove leading/trailing whitespace
file_strings.extend(cleaned_lines)
file_strings.extend(file_strings)
coco_location = "/coco/train2014/"
imagenet_location = "/imagenet/train/"
valid_indices = []
for i in range(len(file_strings)):
string = file_strings[i]
if string.startswith("rep_n"):
string = string.replace("rep_", "")
parts = string.split("_")
file_strings[i] = imagenet_location + parts[0] + '/' + string
valid_indices.append(i)
elif string.startswith("n0"):
parts = string.split("_")
file_strings[i] = imagenet_location + parts[0] + '/' + string
valid_indices.append(i)
elif string.startswith("COCO"):
file_strings[i] = coco_location + string
valid_indices.append(i)
# Extract the subset of rows
fmri_data = [total_data[i] for i in valid_indices]
image_data = [file_strings[i] for i in valid_indices]
print("Number of images = ", len(image_data))
print("Number of fmri data = ", len(fmri_data))
class CustomDataset(Dataset):
def __init__(self, data, image_paths, transform=None):
self.data = data
self.image_paths = image_paths
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
# Load the image from the file path
image = Image.open(self.image_paths[idx])
if image.mode != 'RGB':
image = image.convert('RGB')
# Apply transforms if specified
if self.transform:
image = self.transform(image)
# Extract the corresponding data and label
data_point = torch.tensor(self.data[idx], dtype=torch.float32)
return data_point, image
# Define the standard transforms for the images
image_transforms = transforms.Compose([
transforms.Resize((64, 64)), # Resize the image to match the input size of the generator
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# Create an instance of the CustomDataset and DataLoader
# Might need to convert fmri data and image data to numpy arrays
custom_dataset = CustomDataset(data=fmri_data, image_paths=image_data, transform=image_transforms)
dataloader = DataLoader(custom_dataset, batch_size=256, shuffle=True)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
class VGGAutoEncoder(nn.Module):
def __init__(self):
super(VGGAutoEncoder, self).__init__()
# VGG without Bn as AutoEncoder is hard to train
configs = [2, 2, 3, 3, 3]
self.encoder = VGGEncoder(configs=configs, enable_bn=True)
self.decoder = VGGDecoder(configs=configs[::-1], enable_bn=True)
self.flatten = nn.Flatten()
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
class VGG(nn.Module):
def __init__(self, configs, num_classes=1000, img_size=224, enable_bn=False):
super(VGG, self).__init__()
self.encoder = VGGEncoder(configs=configs, enable_bn=enable_bn)
self.img_size = img_size / 32
self.fc = nn.Sequential(
nn.Linear(in_features=int(self.img_size*self.img_size*512), out_features=4096),
nn.Dropout(p=0.5),
nn.ReLU(inplace=True),
nn.Linear(in_features=4096, out_features=4096),
nn.Dropout(p=0.5),
nn.ReLU(inplace=True),
nn.Linear(in_features=4096, out_features=num_classes)
)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight, mean=0, std=0.01)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=0.01)
nn.init.constant_(m.bias, 0)
def forward(self, x):
x = self.encoder(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
class VGGEncoder(nn.Module):
def __init__(self, configs, enable_bn=False):
super(VGGEncoder, self).__init__()
if len(configs) != 5:
raise ValueError("There should be 5 stage in VGG")
self.conv1 = EncoderBlock(input_dim=3, output_dim=64, hidden_dim=64, layers=configs[0], enable_bn=enable_bn)
self.conv2 = EncoderBlock(input_dim=64, output_dim=128, hidden_dim=128, layers=configs[1], enable_bn=enable_bn)
self.conv3 = EncoderBlock(input_dim=128, output_dim=256, hidden_dim=256, layers=configs[2], enable_bn=enable_bn)
self.conv4 = EncoderBlock(input_dim=256, output_dim=512, hidden_dim=512, layers=configs[3], enable_bn=enable_bn)
self.conv5 = EncoderBlock(input_dim=512, output_dim=512, hidden_dim=512, layers=configs[4], enable_bn=enable_bn)
self.flatten = nn.Flatten()
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.flatten(x)
return x
class VGGDecoder(nn.Module):
def __init__(self, configs, enable_bn=False):
super(VGGDecoder, self).__init__()
if len(configs) != 5:
raise ValueError("There should be 5 stage in VGG")
self.conv1 = DecoderBlock(input_dim=512, output_dim=512, hidden_dim=512, layers=configs[0], enable_bn=enable_bn)
self.conv2 = DecoderBlock(input_dim=512, output_dim=256, hidden_dim=512, layers=configs[1], enable_bn=enable_bn)
self.conv3 = DecoderBlock(input_dim=256, output_dim=128, hidden_dim=256, layers=configs[2], enable_bn=enable_bn)
self.conv4 = DecoderBlock(input_dim=128, output_dim=64, hidden_dim=128, layers=configs[3], enable_bn=enable_bn)
self.conv5 = DecoderBlock(input_dim=64, output_dim=3, hidden_dim=64, layers=configs[4], enable_bn=enable_bn)
self.gate = nn.Sigmoid()
def forward(self, x):
x = x.view(-1, 512, 4, 4)
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.gate(x)
return x
class EncoderBlock(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, layers, enable_bn=False):
super(EncoderBlock, self).__init__()
if layers == 1:
layer = EncoderLayer(input_dim=input_dim, output_dim=output_dim, enable_bn=enable_bn)
self.add_module('0 EncoderLayer', layer)
else:
for i in range(layers):
if i == 0:
layer = EncoderLayer(input_dim=input_dim, output_dim=hidden_dim, enable_bn=enable_bn)
elif i == (layers - 1):
layer = EncoderLayer(input_dim=hidden_dim, output_dim=output_dim, enable_bn=enable_bn)
else:
layer = EncoderLayer(input_dim=hidden_dim, output_dim=hidden_dim, enable_bn=enable_bn)
self.add_module('%d EncoderLayer' % i, layer)
maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.add_module('%d MaxPooling' % layers, maxpool)
def forward(self, x):
for name, layer in self.named_children():
x = layer(x)
return x
class DecoderBlock(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, layers, enable_bn=False):
super(DecoderBlock, self).__init__()
upsample = nn.ConvTranspose2d(in_channels=input_dim, out_channels=hidden_dim, kernel_size=2, stride=2)
self.add_module('0 UpSampling', upsample)
if layers == 1:
layer = DecoderLayer(input_dim=input_dim, output_dim=output_dim, enable_bn=enable_bn)
self.add_module('1 DecoderLayer', layer)
else:
for i in range(layers):
if i == 0:
layer = DecoderLayer(input_dim=input_dim, output_dim=hidden_dim, enable_bn=enable_bn)
elif i == (layers - 1):
layer = DecoderLayer(input_dim=hidden_dim, output_dim=output_dim, enable_bn=enable_bn)
else:
layer = DecoderLayer(input_dim=hidden_dim, output_dim=hidden_dim, enable_bn=enable_bn)
self.add_module('%d DecoderLayer' % (i+1), layer)
def forward(self, x):
for name, layer in self.named_children():
x = layer(x)
return x
class EncoderLayer(nn.Module):
def __init__(self, input_dim, output_dim, enable_bn):
super(EncoderLayer, self).__init__()
if enable_bn:
self.layer = nn.Sequential(
nn.Conv2d(in_channels=input_dim, out_channels=output_dim, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(output_dim),
nn.ReLU(inplace=True),
)
else:
self.layer = nn.Sequential(
nn.Conv2d(in_channels=input_dim, out_channels=output_dim, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.layer(x)
class DecoderLayer(nn.Module):
def __init__(self, input_dim, output_dim, enable_bn):
super(DecoderLayer, self).__init__()
if enable_bn:
self.layer = nn.Sequential(
nn.BatchNorm2d(input_dim),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=input_dim, out_channels=output_dim, kernel_size=3, stride=1, padding=1),
)
else:
self.layer = nn.Sequential(
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=input_dim, out_channels=output_dim, kernel_size=3, stride=1, padding=1),
)
def forward(self, x):
return self.layer(x)
checkpoint = torch.load("imagenet-vgg16.pth")
new_dict = {key.replace('module.', ''): value for key, value in checkpoint['state_dict'].items()}
autoencoder = VGGAutoEncoder()
model_dict = autoencoder.state_dict()
model_dict.update(new_dict)
autoencoder.load_state_dict(model_dict)
autoencoder = autoencoder.to(device)
class ComplexNetwork(nn.Module):
def __init__(self):
super(ComplexNetwork, self).__init__()
# Fully connected layers
self.fc1 = nn.Linear(1685, 1024)
self.batch_norm1 = nn.BatchNorm1d(1024)
self.dropout1 = nn.Dropout(0.5)
self.fc2 = nn.Linear(1024, 2048)
self.batch_norm2 = nn.BatchNorm1d(2048)
self.dropout2 = nn.Dropout(0.5)
self.fc3 = nn.Linear(2048, 2048)
self.batch_norm3 = nn.BatchNorm1d(2048)
def forward(self, x):
# Input shape: (batch_size, 1685)
# Fully connected layers with dropout
x = F.leaky_relu(self.batch_norm1(self.fc1(x)))
x = self.dropout1(x)
x = F.leaky_relu(self.batch_norm2(self.fc2(x)))
x = self.dropout2(x)
x = F.relu(self.batch_norm3(self.fc3(x)))
return x
def custom_loss(t1, t2):
# Calculate mean and standard deviation of your data
tensor1 = t1
tensor2 = t2
mean = tensor1.mean()
std = tensor1.std()
# Normalize your data
tensor1_normalized = (tensor1 - mean) / std
tensor2_normalized = (tensor2 - mean) / std
# Calculate MSE loss on normalized data
criterion = nn.SmoothL1Loss(size_average=None, reduce=None, reduction='mean', beta=1.0)
mse_loss = criterion(tensor1_normalized, tensor2_normalized)
return mse_loss
mriEncoder = ComplexNetwork()
mriEncoder = mriEncoder.to(device)
#criterion = nn.SmoothL1Loss(size_average=None, reduce=None, reduction='mean', beta=1.0)
optimizer = optim.Adam(mriEncoder.parameters(), lr=0.00001)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=5, verbose=True)
losses = []
# Training loop
def cosine_similarity_loss(x, y):
# Normalize the input vectors
x_normalized = F.normalize(x, p=2, dim=-1)
y_normalized = F.normalize(y, p=2, dim=-1)
# Compute cosine similarity
similarity = torch.sum(x_normalized * y_normalized, dim=-1)
# Invert the similarity to turn it into a similarity loss (maximize similarity)
similarity_loss = 1 - similarity
return similarity_loss.mean()
# Example usage in your training loop
for epoch in range(200):
for batch in dataloader:
fmri, target_image = batch
# Move data to GPU
fmri, target_image = fmri.to(device), target_image.to(device)
# Zero the gradients
optimizer.zero_grad()
# Forward pass: compute predicted image by passing fmri through the generator
img_encoding = autoencoder.encoder(target_image)
fmri_encoding = mriEncoder(fmri)
# Compute the cosine similarity loss
loss = custom_loss(img_encoding, fmri_encoding)
# Backward pass: compute gradient of the loss with respect to model parameters
loss.backward()
# Update the weights
optimizer.step()
scheduler.step(loss)
# Print the loss after each epoch
print(f'Epoch [{epoch+1}/{200}], Cosine Similarity Loss: {loss.item():.4f}')
losses.append(loss.item())
torch.save(mriEncoder.state_dict(), "./method3_model.pth")
with open('method3_losses.pkl', 'wb') as f:
pickle.dump(losses, f)
print("Code execution complete!")