-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIFAR100.py
345 lines (288 loc) · 13.8 KB
/
CIFAR100.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
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR100
from torch.utils.data import DataLoader, random_split
import matplotlib.pyplot as plt
from torch import Tensor
import torch.nn.init as init
import math
import torch.nn.functional as F
import time
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
print(torch.__version__)
class TULERNN(nn.Module):
__constants__ = ['in_features', 'out_features']
def __init__(self, in_features: int, out_features: int, bias: bool = True,
device=None, dtype=None) -> None:
factory_kwargs = {'device': device, 'dtype': dtype}
super(TULERNN, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.Parameter(torch.empty((out_features, in_features), **factory_kwargs))
self.threshold = nn.Parameter(torch.empty((out_features, 1), **factory_kwargs))
if bias:
self.bias = nn.Parameter(torch.empty(out_features, **factory_kwargs))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self) -> None:
init.xavier_uniform_(self.weight)
init.xavier_uniform_(self.threshold)
if self.bias is not None:
fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0
init.uniform_(self.bias, -bound, bound)
def forward(self, input: Tensor) -> Tensor:
linear_output = F.linear(input, self.weight, self.bias)
exceed_threshold = linear_output <= self.threshold.transpose(0, 1)
output = linear_output * exceed_threshold + linear_output * (~exceed_threshold)
return output
def extra_repr(self) -> str:
return 'in_features={}, out_features={}, bias={}'.format(
self.in_features, self.out_features, self.bias is not None
)
class BrainNeuronActivation(nn.Module):
def __init__(self, num_neurons):
super(BrainNeuronActivation, self).__init__()
self.thresholds = nn.Parameter(torch.rand(num_neurons))
self.parm1 = nn.Parameter(torch.rand(num_neurons))
self.parm2 = nn.Parameter(torch.rand(num_neurons))
self.parm3 = nn.Parameter(torch.rand(num_neurons))
self.parm4 = nn.Parameter(torch.rand(num_neurons))
self.parm5 = nn.Parameter(torch.rand(num_neurons))
def forward(self, input):
output = input.clone()
output = torch.where(output < 0, self.parm1 * output, output)
action_potentials = torch.where(output >= self.thresholds, self.parm5 + self.parm2 * (output - self.parm4), self.parm3 * output)
return action_potentials
class Try_TULER(nn.Module):
def __init__(self, output_features=100, dropout=0.5):
super(Try_TULER, self).__init__()
self.conv_layers = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layers = nn.Sequential(
nn.Flatten(),
TULERNN(1024, 2048),
nn.ReLU(),
TULERNN(2048, output_features),
nn.Softmax(dim=1),
)
# Move the entire model to CUDA
self.to(device)
def forward(self, x):
x = self.conv_layers(x)
x = x.view(x.size(0), -1) # Flatten the CNN output
output = self.fc_layers(x)
return output
class Try(nn.Module):
def __init__(self, output_features=100, dropout=0.5):
super(Try, self).__init__()
self.conv_layers = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layers = nn.Sequential(
nn.Flatten(),
nn.Linear(1024, 2048),
nn.ReLU(),
nn.Linear(2048, output_features),
nn.Softmax(dim=1),
)
# Move the entire model to CUDA
self.to(device)
def forward(self, x):
x = self.conv_layers(x)
x = x.view(x.size(0), -1) # Flatten the CNN output
output = self.fc_layers(x)
return output
def topk_accuracy(outputs, targets, k=5):
with torch.no_grad():
_, topk_predictions = outputs.topk(k, 1, True, True)
topk_correct = topk_predictions.eq(targets.view(-1, 1).expand_as(topk_predictions))
# Compute the number of correct predictions for each sample
correct_per_sample = topk_correct.sum(dim=1)
# If any of the top k predictions is correct, consider it as a true positive
correct_samples = (correct_per_sample > 0).float()
# Compute the average top-k accuracy over the entire batch
topk_accuracy = correct_samples.mean().item() * 100.0
# Ensure the top-k accuracy does not exceed 100%
topk_accuracy = min(topk_accuracy, 100.0)
return topk_accuracy
def train_and_evaluate(model, train_loader, test_loader, lr=0.0001, epochs=100):
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=lr)
# Move the model to CUDA if available
model.to(device)
# Lists to store training and testing accuracies and losses
train_accuracies = []
train_losses = []
test_accuracies = []
test_losses = []
for epoch in range(epochs):
model.train()
for inputs, targets in train_loader:
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
model.eval()
top1_correct_train = 0
top5_correct_train = 0
total_train = 0
train_loss = 0
with torch.no_grad():
for inputs, targets in train_loader:
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
total_train += targets.size(0)
train_loss += criterion(outputs, targets).item()
top1_correct_train += (predicted == targets).sum().item()
top5_correct_train += topk_accuracy(outputs, targets, k=5)
top1_train_accuracy = 100 * top1_correct_train / total_train
top5_train_accuracy = 100 * top5_correct_train / total_train
train_accuracies.append((top1_train_accuracy))
train_loss /= len(train_loader)
train_losses.append(train_loss)
top1_correct_test = 0
top5_correct_test = 0
total_test = 0
test_loss = 0
with torch.no_grad():
for inputs, targets in test_loader:
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
total_test += targets.size(0)
test_loss += criterion(outputs, targets).item()
top1_correct_test += (predicted == targets).sum().item()
top5_correct_test += topk_accuracy(outputs, targets, k=5)
test_top1_accuracy = 100 * top1_correct_test / total_test
test_top5_accuracy = 100 * top5_correct_test / total_test
test_accuracies.append((test_top1_accuracy))
test_loss /= len(test_loader)
test_losses.append(test_loss)
print(f"Epoch {epoch + 1}/{epochs}, "
f"Train Top-1 Accuracy: {top1_train_accuracy:.4f}%, "
f"Train Top-5 Accuracy: {top5_train_accuracy:.4f}%, "
f"Test Top-1 Accuracy: {test_top1_accuracy:.4f}%, "
f"Test Top-5 Accuracy: {test_top5_accuracy:.4f}%")
return train_accuracies, train_losses, test_accuracies, test_losses
# Load the CIFAR-100 dataset
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
cifar100_dataset = CIFAR100(root="./data", train=True, transform=transform, download=True)
train_size = int(0.8 * len(cifar100_dataset))
test_size = len(cifar100_dataset) - train_size
train_dataset, test_dataset = random_split(cifar100_dataset, [train_size, test_size])
train_loader = DataLoader(train_dataset, batch_size=50, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=50, shuffle=False)
# Test the Try_TULER model with different activation functions
activation_functions = [
BrainNeuronActivation(2048),
nn.ReLU(),
nn.PReLU(),
]
activation_scores_try_tuler = []
activation_losses_try_tuler = []
activation_scores_try = []
activation_losses_try = []
for activation_function in activation_functions:
print(f"Testing Try with activation: {activation_function.__class__.__name__}")
model = Try(output_features=10) # CIFAR-100 has 100 output classes
model.fc_layers[2] = activation_function
train_accuracies, train_losses, test_accuracies, test_losses = train_and_evaluate(model, train_loader, test_loader)
activation_scores_try.append((train_accuracies, test_accuracies))
activation_losses_try.append((train_losses, test_losses))
for activation_function in activation_functions:
print(f"Testing Try_TULER with activation: {activation_function.__class__.__name__}")
model = Try_TULER(output_features=10) # CIFAR-100 has 100 output classes
model.fc_layers[2] = activation_function
train_accuracies, train_losses, test_accuracies, test_losses = train_and_evaluate(model, train_loader, test_loader)
activation_scores_try_tuler.append((train_accuracies, test_accuracies))
activation_losses_try_tuler.append((train_losses, test_losses))
# Test the Try model with different activation functions
def save_to_log(filename, model_type, activation_functions, activation_scores, activation_losses):
with open(filename, 'a') as f:
f.write(f"Model Type: {model_type}\n")
for i, activation_function in enumerate(activation_functions):
f.write(f"Activation Function: {activation_function.__class__.__name__}\n")
train_scores, test_scores = activation_scores[i]
train_losses, test_losses = activation_losses[i]
f.write("Epoch\tTrain Accuracy\tTest Accuracy\tTrain Loss\tTest Loss\n")
for epoch in range(len(train_scores)):
f.write(f"{epoch+1}\t{train_scores[epoch]:.4f}\t{test_scores[epoch]:.4f}\t{train_losses[epoch]:.6f}\t{test_losses[epoch]:.6f}\n")
f.write("\n")
# Save all activations for Try_TULER to log.txt
#save_to_log('CIFAR100_Try_TULER_log.txt', 'Try_TULER', activation_functions, activation_scores_try_tuler, activation_losses_try_tuler)
# Save all activations for Try to log.txt
#save_to_log('CIFAR100_Try_log.txt', 'Try', activation_functions, activation_scores_try, activation_losses_try)
# Plotting function for all activations in one plot
def plot_all_activations(activation_scores, activation_losses, activation_functions):
plt.figure(figsize=(12, 6))
for i, activation_function in enumerate(activation_functions):
train_scores, test_scores = activation_scores[i]
train_losses, test_losses = activation_losses[i]
x = range(1, len(train_scores) + 1)
# Plot accuracy
plt.subplot(1, 2, 1)
plt.plot(x, train_scores, label=f"Train - {activation_function.__class__.__name__}")
plt.plot(x, test_scores, label=f"Test - {activation_function.__class__.__name__} - Test")
# Plot loss
plt.subplot(1, 2, 2)
plt.plot(x, train_losses, label=f"Train - {activation_function.__class__.__name__}")
plt.plot(x, test_losses, label=f"Test - {activation_function.__class__.__name__}")
plt.subplot(1, 2, 1)
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Accuracy vs Epochs')
plt.legend()
plt.subplot(1, 2, 2)
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Loss vs Epochs')
plt.legend()
plt.tight_layout()
# Save the plot as SVG and PNG files
# Get the current timestamp
timestamp = time.strftime("%Y%m%d_%H%M%S")
# Save the plot as SVG and PNG files with the timestamp in the filename
plt.savefig(f'activations_plot_{timestamp}.svg', format='svg')
plt.savefig(f'activations_plot_{timestamp}.png', format='png')
plt.show()
# Plot all activations for Try_TULER
plot_all_activations(activation_scores_try_tuler, activation_losses_try_tuler, activation_functions)
# Plot all activations for Try
plot_all_activations(activation_scores_try, activation_losses_try, activation_functions)