-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstm.py
75 lines (58 loc) · 2.51 KB
/
lstm.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
# -*- coding: utf-8 -*-
"""lstm
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19aiQICbRQ30rL60eOVsID47o_yUPSurH
"""
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
# Define a model for sequence-to-sequence mapping
class FrameGenerationModel(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(FrameGenerationModel, self).__init__()
self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
lstm_out, _ = self.lstm(x)
output = self.fc(lstm_out)
return output
# Assuming you have your frame shift sequences and corresponding image sequences loaded as tensors
frame_shifts, images = ...
# Define the model
input_size = 2 # Assuming one-dimensional frame shifts
hidden_size = 64 # Adjust based on your needs
output_size = 3 * 512 * 512 # Assuming images are RGB with dimensions 64x64
model = FrameGenerationModel(input_size, hidden_size, output_size)
# Define the loss function and optimizer
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Create DataLoader
dataset = TensorDataset(frame_shifts, images)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
# Training loop
num_epochs = 10
for epoch in range(num_epochs):
model.train()
for frame_shifts_batch, images_batch in dataloader:
optimizer.zero_grad()
# Expand dimensions to match the expected input shape for the LSTM
frame_shifts_batch = frame_shifts_batch.unsqueeze(-1)
# Forward pass
outputs = model(frame_shifts_batch)
# Reshape outputs and targets to compute the loss
outputs = outputs.view(-1, output_size)
images_batch = images_batch.view(-1, output_size)
loss = criterion(outputs, images_batch)
loss.backward()
optimizer.step()
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
# After training, you can use the model to generate image sequences
model.eval()
with torch.no_grad():
# Example: Generate images for a new sequence of frame shifts
new_frame_shifts = torch.randn(1, sequence_length, input_size) # Adjust based on your needs
new_frame_shifts = new_frame_shifts.unsqueeze(-1)
generated_images = model(new_frame_shifts)
# Make sure to post-process the generated_images tensor as needed for visualization or further use