-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn1_andrew.py
172 lines (136 loc) · 4.85 KB
/
cnn1_andrew.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
import numpy as np
import os
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
import time
from torch.optim.lr_scheduler import OneCycleLR
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from blitz.modules import BayesianLinear
from blitz.utils import variational_estimator
#Saving
save_name = 'Newest'
save_dir = './models'
#Training
train_run = 'trainset'
train_dir_ext = 'Newest_Data_Noisy'
#Testing
test_run = 'testset'
test_dir_ext = train_dir_ext
#Training parameters
img_size = 116
lr = 1e-3
num_epochs = 5
batch_size = 8
#Normalization (close to peak suppression / calibration mask average)
normalization = 0.03
#Lab to space distance conversion
Dtel_lab = 2.201472e-3 #Telescope diameter used in simulations: quadrature_code/generate_images.py
Dtel_space = 2.4 #Roman Telescope
lab2space = Dtel_space / Dtel_lab
class StarshadeDataset(Dataset):
def __init__(self, data_dir, root_name, transform=None):
self.root_dir = os.path.join(data_dir, root_name)
self.root_name = root_name
self.transform = transform
#Load shifts from csv file
self.shifts = np.genfromtxt(os.path.join(self.root_dir, \
root_name + '.csv'), delimiter=',')
def __len__(self):
return len(self.shifts)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
#Load image
img_path = os.path.join(self.root_dir, str(idx).zfill(6) + '.npy')
image = np.load(img_path).astype('float32')
#Normalize the image
image /= normalization
#Grab the current shift and scale to space-scale
xy = self.shifts[idx, 1:].astype(np.float32)
xy *= lab2space
if self.transform:
image = self.transform(image)
sample = [image, xy]
return sample
@variational_estimator
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 8, 3, 1)
self.conv2 = nn.Conv2d(8, 16, 3, 1)
self.fc1 = nn.Linear(16 * (((img_size - 2) // 2 - 2) // 2) * (((img_size - 2) // 2 - 2) // 2), 128)
self.fc2 = BayesianLinear(128, 2)
def forward(self, X1):
X1 = self.conv1(X1)
X1 = F.max_pool2d(X1, 2)
X1 = F.relu(X1)
X1 = self.conv2(X1)
X1 = F.max_pool2d(X1, 2)
X1 = F.relu(X1)
X1 = torch.flatten(X1, 1)
X = X1
X = self.fc1(X)
X = F.relu(X)
X = self.fc2(X)
return X
def train(model, trainloader, optimizer, epoch):
model.train()
for batch_idx, batch in enumerate(trainloader):
optimizer.zero_grad()
# output = model(batch[0])
# loss = F.mse_loss(output, batch[1])
loss = model.sample_elbo(inputs=batch[0], labels=batch[1], criterion=torch.nn.MSELoss(), sample_nbr=2)
loss.backward()
optimizer.step()
if batch_idx % 25 == 0:
print(f'Train Epoch: {epoch} [{batch_idx*len(batch[1])}/{len(trainloader.dataset)}]\tLoss: {loss.item()/len(batch[1])}')
def test(model, testloader):
model.eval()
test_loss = 0
with torch.no_grad():
for batch in testloader:
# output = model(batch[0], batch[1])
# test_loss += F.mse_loss(output, batch[2]).item()
output = model(batch[0])
test_loss += F.mse_loss(output, batch[1]).item()
test_loss /= len(testloader.dataset)
print(f'\nTest Set: Average Loss {test_loss}\n')
def main():
#Build directories
data_base_dir = 'quadrature_code/Simulated_Images'
train_dir = os.path.join(data_base_dir, train_dir_ext)
test_dir = os.path.join(data_base_dir, test_dir_ext)
#Transform
transform = transforms.Compose([transforms.ToTensor()])
#Load training data
trainset = StarshadeDataset(train_dir, train_run, transform=transform)
trainloader = DataLoader(trainset, batch_size=8, shuffle=True)
#Load testing data
testset = StarshadeDataset(test_dir, test_run, transform=transform)
testloader = DataLoader(testset, batch_size=8, shuffle=False)
#Create model
model = CNN()
optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-2)
#Build scheduler
scheduler = OneCycleLR(optimizer, lr, total_steps=num_epochs)
#Loop through epochs
for epoch in range(num_epochs):
#Train
train(model, trainloader, optimizer, epoch)
#Test
test(model, testloader) #TODO: is it necessary to test here?
#Step scheduler
scheduler.step()
#Save model
torch.save(model.state_dict(), os.path.join(save_dir, save_name + '.pt'))
if __name__ == '__main__':
#Start timer
tik = time.perf_counter()
#Run main script
main()
#Print time
tok = time.perf_counter()
print(f'\nElapsed time: {tok-tik:.2f} [s]\n')