-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvolutionalArchitecture.py
192 lines (150 loc) · 5.94 KB
/
convolutionalArchitecture.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset
import os
import matplotlib.pyplot as plt
import sys
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
writer = SummaryWriter()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# BATCH_SIZE = 100
# LOAD DATA HERE
topologyList = []
topologyLabels = []
BASE_OUTPUT_PATH = './neuralnetworkdata/'
TOPOLOGY_PATH = './neuralnetworkdata/TopologyOutput/'
BCOUTPUT_PATH = './neuralnetworkdata/BCOutput/'
topologies = os.listdir(TOPOLOGY_PATH)
boundaryConditions = os.listdir(BCOUTPUT_PATH)
# print(topologies)
preprocessedData = []
for path in range(len(topologies)):
topologyPath = TOPOLOGY_PATH + topologies[path]
bcPath = BCOUTPUT_PATH + boundaryConditions[path]
topology = np.load(topologyPath)
boundaryCondition = np.load(bcPath)
topologyconverted = np.array(topology, dtype=np.float32).reshape(41, 41)
# print(boundaryCondition.shape)
volfrac = int(topologies[path].split("_")[-1][:-4])
volumeFractionData = np.full((1, 41), volfrac)
dataList = [np.array([topologyconverted]), boundaryCondition, volumeFractionData]
preprocessedData.append(dataList)
class GeneratedTopologyDataset(Dataset):
def __init__(self, topology_list, topology_labels, volume_fractions, transform=None, target_transform=None):
self.topology_list = topology_list
self.topology_labels = topology_labels
self.transform = transform
self.volume_fractions = volume_fractions
self.target_transform = target_transform
self.labels = torch.cat((self.topology_labels, self.volume_fractions), 1).to(torch.float32)
def __len__(self):
return len(self.topology_labels)
def __getitem__(self, idx):
# print(label)
generatedTopology = self.topology_list[idx]
topologyLabel = self.topology_labels[idx]
volumeFractionLabel = self.volume_fractions[idx]
label = self.labels[idx]
# print(label)
# print(label.shape)
return generatedTopology, label
topologyList = torch.tensor(np.array([data[0] for data in preprocessedData]))
topologyLabels = torch.tensor(np.array([np.array(data[1]) for data in preprocessedData]))
volumeFraction = torch.tensor(np.array([data[2] for data in preprocessedData]))
BATCH_SIZE = 1
dataset = GeneratedTopologyDataset(topologyList, topologyLabels, volumeFraction)
train_dataset, test_dataset = torch.utils.data.random_split(dataset, [0.8, 0.2])
train_loader = torch.utils.data.DataLoader(train_dataset, shuffle=True, batch_size=BATCH_SIZE)
test_loader = torch.utils.data.DataLoader(test_dataset, shuffle=True, batch_size=BATCH_SIZE)
class NeuralNet(nn.Module):
def __init__(self, input_size, hidden_size, output_dim):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.fc2 = nn.Linear(hidden_size, 4096)
self.conv1 = nn.Conv2d(3, 3, 5)
self.conv2 = nn.Conv2d(3, 1, 3)
self.fc3 = nn.Linear(784, hidden_size)
self.fc4 = nn.Linear(hidden_size, output_dim)
def forward(self, x):
out = F.relu(self.fc1(x))
out = F.relu(self.fc2(out))
out = out.view(-1, 64, 64)
out = self.conv1(out)
# print(out.shape)
out = F.max_pool2d(out, 2)
out = self.conv2(out)
out = out.view(-1, 28, 28).flatten()
out = self.fc3(out)
out = self.fc4(out).view(-1, 41, 41)
return out
learning_rate = 0.001
NUM_EPOCHS = 200
model = NeuralNet(41, 1681, 1681).to(device)
criterion = nn.L1Loss().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
losses = []
averagedTrainLosses = []
averagedTestLosses = []
# TRAIN MODEL
for epoch in tqdm(range((NUM_EPOCHS))):
tmpTrainLosses = []
tmpTestLosses = []
for i, (topologies, labels) in enumerate(train_loader):
topologies = topologies.to(device)
labels = labels.to(device)
# print(labels.shape)
# Forward pass
outputs = model(labels)
# print(outputs.shape)
# print(topologies.shape)
loss = criterion(outputs.flatten(), topologies.flatten())
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
currloss = loss.item()
losses.append(currloss)
tmpTrainLosses.append(currloss)
averagedTrainLosses.append(np.mean(tmpTrainLosses))
writer.add_scalar('Loss/train', np.mean(tmpTrainLosses), epoch)
# find test loss
for i, (topologies, labels) in enumerate(test_loader):
topologies = topologies.to(device)
labels = labels.to(device)
# Forward pass
outputs = model(labels)
# print(outputs.shape)
# print(topologies.shape)
loss = criterion(outputs.flatten(), topologies.flatten())
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
currloss = loss.item()
losses.append(currloss)
tmpTestLosses.append(currloss)
print(f'Epoch [{epoch + 1}/{NUM_EPOCHS}], Loss: {np.mean(tmpTestLosses):.4f}')
averagedTestLosses.append(np.mean(tmpTestLosses))
writer.add_scalar('Loss/test', np.mean(tmpTestLosses), epoch)
# plot losses with matplotlib
# print(len(averagedLosses))
# writer.close()
torch.save(model.state_dict(), "model.pth")
plt.plot([i + 1 for i in range(len(averagedTestLosses))], averagedTestLosses)
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.title("Test Loss During Training")
plt.savefig("testlosses.png")
plt.show()
print("min test loss", min(averagedTestLosses))
plt.plot([i + 1 for i in range(len(averagedTrainLosses))], averagedTrainLosses)
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.title("Train Loss During Training")
print("min train loss", min(averagedTrainLosses))
plt.savefig("trainlosses.png")
plt.show()