-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetter_vae copy.py
167 lines (132 loc) · 5.27 KB
/
better_vae copy.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
# -*- coding: utf-8 -*-
"""better_vae
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1gArgTJw2lZpmCH0CInG8m9FJuw5nhLOs
"""
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
# Load your data
frames_file_path = "data/mTBI/frame_pixels.npy" # Add this line
df = pd.read_csv("data/mTBI/eye_motion_trace.csv")
print("loaded data")
# Dataset class
class LSTMDataset(Dataset):
def __init__(self, dataframe, frames_file_path):
self.dataframe = dataframe
self.frames_file_path = frames_file_path # This should be a string path
def __len__(self):
return len(self.dataframe)
def __getitem__(self, idx):
frame_data = self.load_frame_data(idx)
x = self.dataframe.iloc[idx]['x']
y = self.dataframe.iloc[idx]['y']
inputs = torch.tensor([x, y], dtype=torch.float32)
return inputs, torch.tensor(frame_data, dtype=torch.float32)
def load_frame_data(self, idx):
data = np.load(self.frames_file_path, mmap_mode='r')
frame_data = data[idx]
return frame_data
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
# frames is a tensor of shape [num_frames, height, width] or [num_frames, channels, height, width]
# Hyperparameters
input_channels = frames.size(1) if len(frames.shape) == 4 else 1 # Assuming grayscale or RGB images
image_size = frames.size(2) if len(frames.shape) == 4 else frames.size(1) # Assuming square images
latent_dim = 32
batch_size = 64
epochs = 20
# Flatten each frame and concatenate with eye movement shift data
frames = frames.view(frames.size(0), -1) if len(frames.shape) == 4 else frames.view(frames.size(0), -1, 1)
eye_movement_shifts_and_frames = torch.cat([eye_movement_shifts, frames], dim=1)
# Convert data to PyTorch tensors
eye_movement_shifts_and_frames = torch.FloatTensor(eye_movement_shifts_and_frames)
# Create a DataLoader
dataset = TensorDataset(eye_movement_shifts_and_frames, frames)
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
# VAE Model
class VAE(nn.Module):
def __init__(self, input_size, latent_dim):
super(VAE, self).__init__()
# Encoder
self.encoder = nn.Sequential(
nn.Linear(input_size, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
)
self.z_mean = nn.Linear(64, latent_dim)
self.z_log_var = nn.Linear(64, latent_dim)
# Decoder
self.decoder = nn.Sequential(
nn.Linear(latent_dim, 64),
nn.ReLU(),
nn.Linear(64, 128),
nn.ReLU(),
nn.Linear(128, input_size),
nn.Sigmoid() # Assuming you want values between 0 and 1
)
def reparameterize(self, mu, log_var):
std = torch.exp(0.5 * log_var)
eps = torch.randn_like(std)
return mu + eps * std
def forward(self, x):
# Encoder
x = self.encoder(x)
z_mean = self.z_mean(x)
z_log_var = self.z_log_var(x)
# Reparameterization trick
z = self.reparameterize(z_mean, z_log_var)
# Decoder
x_hat = self.decoder(z)
return x_hat, z_mean, z_log_var
# Instantiate the VAE model
input_size = eye_movement_shifts_and_frames.size(1)
model = VAE(input_size=input_size, latent_dim=latent_dim)
# Loss function
def loss_function(x_hat, x, z_mean, z_log_var):
reconstruction_loss = nn.functional.mse_loss(x_hat, x, reduction='sum')
kl_divergence = -0.5 * torch.sum(1 + z_log_var - z_mean.pow(2) - z_log_var.exp())
return reconstruction_loss + kl_divergence
# Optimizer
optimizer = optim.Adam(model.parameters(), lr=1e-3)
# Training
for epoch in range(epochs):
for batch in dataloader:
x, _ = batch
optimizer.zero_grad()
x_hat, z_mean, z_log_var = model(x)
loss = loss_function(x_hat, x, z_mean, z_log_var)
loss.backward()
optimizer.step()
print(f'Epoch {epoch + 1}/{epochs}, Loss: {loss.item()}')
# Generate new videos
with torch.no_grad():
# Assuming you have new eye movement shift data in new_eye_movement_shifts
new_eye_movement_shifts = torch.FloatTensor(new_eye_movement_shifts)
new_frames, _, _ = model(new_eye_movement_shifts)
new_frames = new_frames.numpy()
# You can now use new_frames for further analysis or visualization
with torch.no_grad():
# Assuming new_eye_movement_shifts_tensor is the tensor containing the new eye movement shifts
new_eye_movement_shifts_tensor = torch.FloatTensor(new_eye_movement_shifts)
generated_videos, _, _ = model(new_eye_movement_shifts_tensor)
generated_videos = generated_videos.numpy()
import matplotlib.pyplot as plt
import numpy as np
# Assuming generated_videos is a NumPy array containing the generated videos
# Reshape the videos if necessary
generated_videos = generated_videos.reshape((-1, input_channels, image_size, image_size))
# Visualize a few frames from the generated video
num_frames_to_visualize = 5
for i in range(num_frames_to_visualize):
plt.subplot(1, num_frames_to_visualize, i + 1)
plt.imshow(np.transpose(generated_videos[i], (1, 2, 0)))
plt.axis('off')
plt.show()