Commit 586faee9 authored by Johann Schröder's avatar Johann Schröder
Browse files

install script tested, requirements.txt

parent b2ed3307
python -m pip install --upgrade pip
pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
pip install -r requirements.txt
pip install git+https://github.com/BindsNET/bindsnet.git
pip install snntorch
git clone https://github.com/bamsumit/slayerPytorch.git
cd slayerPytorch && python setup.py install && cd ..
rm -rf lib/python3.9/site-packages/cv2/qt/plugins
mv slayerPytorch/ slayer/
pip install -qU git+https://github.com/norse/norse
......@@ -216,10 +216,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -231,10 +229,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_izhi_results_raw.csv'
filename_pretty = 'norse_dvsgesture_izhi_results_pretty.csv'
......
......@@ -228,10 +228,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -243,10 +241,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_izhi_results_raw.csv'
filename_pretty = 'norse_dvsgesture_izhi_results_pretty.csv'
......
......@@ -237,10 +237,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -252,10 +250,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_izhi_results_raw.csv'
filename_pretty = 'norse_dvsgesture_izhi_results_pretty.csv'
......
......@@ -255,10 +255,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -270,10 +268,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_izhi_results_raw.csv'
filename_pretty = 'norse_dvsgesture_izhi_results_pretty.csv'
......
......@@ -7,7 +7,11 @@ import numpy as np
import matplotlib.pyplot as plt
import tonic
import gc
import csv
import datetime
import sys
import time
from prettytable.prettytable import from_csv
plt.style.use('science')
gc.collect()
torch.cuda.empty_cache()
......@@ -115,12 +119,13 @@ class Model(torch.nn.Module):
return log_p_y
EPOCHS = 10
LR = 0.002
INPUT_FEATURES = np.product(trainset.sensor_size)
# INPUT_FEATURES = np.product((64, 64))
HIDDEN_FEATURES = 100
OUTPUT_FEATURES = len(trainset.classes)
DEVICE = torch.device("cuda")
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
layers = (HIDDEN_FEATURES)
model = Model(
snn=SNN(
......@@ -131,7 +136,6 @@ model = Model(
).to(DEVICE)
optimizer = torch.optim.Adam(model.parameters(), lr=LR)
EPOCHS = 10
def train(model, device, train_loader, optimizer):
......@@ -181,6 +185,7 @@ test_accuracies = []
torch.autograd.set_detect_anomaly(True)
pbar = tqdm(total=EPOCHS, colour='blue', leave=False)
start_time = time.time()
for epoch in range(EPOCHS):
training_loss, mean_loss, train_accuracy = train(
model, DEVICE, train_loader, optimizer)
......@@ -192,14 +197,18 @@ for epoch in range(EPOCHS):
test_accuracies.append(test_accuracy)
pbar.update()
print("Izhikevich Parameter:")
print("a:", p_izhi.a,
"\nb:", p_izhi.b,
"\nc:", p_izhi.c,
"\nd:", p_izhi.d,
"\nbias:", p_izhi.bias,
"\nthreshold:", p_izhi.v_th,
"\nslope:", p_izhi.alpha)
runtime = time.time() - start_time
date = datetime.datetime.now()
filetype = '.png'
d = str(date.day) + "_" + str(date.month) + "_" + \
str(date.year) + "_" + str(date.time())
d = d.split('.')[0]
scriptname = sys.argv[0].split('.')[0]
tf_str = str(transform).replace(" ", "").splitlines(keepends=False)[1:-1]
print(
f"Train Accuracy:{train_accuracies[-1]:.2f}, Test Accuracy{test_accuracies[-1]:.2f}")
fig = plt.figure(facecolor="w", figsize=(10, 5))
......@@ -209,6 +218,11 @@ plt.title("Loss Curves")
plt.legend(["Train Loss", "Test Loss"])
plt.xlabel("Epoch")
plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -217,4 +231,75 @@ plt.title("Accuracy Curves")
plt.legend(["Train Accuracy", "Test Accuracy"])
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.show()
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_izhi_results_raw.csv'
filename_pretty = 'norse_dvsgesture_izhi_results_pretty.csv'
fields = ['n',
' date',
' filename',
' device',
' runtime',
' accuracy (train, test)',
' loss (train, test)',
' epochs',
' batch_size',
' lr',
' layers',
' method',
' alpha',
' a',
' b',
' c',
' d',
' bias',
' tau_inv',
' v_th',
' transform']
num_rows = 0
for r in open(filename_raw):
num_rows += 1
if num_rows != 0:
num_rows -= 1
rows = [[num_rows,
d.replace('_', ' '),
sys.argv[0].split('.')[0] + '.py',
DEVICE,
runtime,
(train_accuracies[-1], test_accuracies[-1]),
(mean_losses[-1], test_losses[-1]),
EPOCHS,
BATCH_SIZE,
LR,
layers,
p_izhi.method,
p_izhi.alpha,
p_izhi.a,
p_izhi.b,
p_izhi.c,
p_izhi.d,
p_izhi.bias,
p_izhi.tau_inv,
p_izhi.v_th,
tf_str]]
with open(filename_raw, 'a') as csvfile:
csvwriter = csv.writer(csvfile)
if num_rows == 0:
csvwriter.writerow(fields)
csvwriter.writerows(rows)
with open(filename_raw) as fp:
table = from_csv(fp)
f = open(filename_pretty, "w")
f.write(table.get_string())
f.close()
......@@ -211,10 +211,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -226,10 +224,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_lif_results_raw.csv'
filename_pretty = 'norse_dvsgesture_lif_results_pretty.csv'
......
......@@ -220,10 +220,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -235,10 +233,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_lif_results_raw.csv'
filename_pretty = 'norse_dvsgesture_lif_results_pretty.csv'
......
......@@ -225,10 +225,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -240,10 +238,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_lif_results_raw.csv'
filename_pretty = 'norse_dvsgesture_lif_results_pretty.csv'
......
......@@ -261,10 +261,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -276,10 +274,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_lif_results_raw.csv'
filename_pretty = 'norse_dvsgesture_lif_results_pretty.csv'
......
......@@ -208,10 +208,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -223,10 +221,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_dvsgesture_lif_results_raw.csv'
filename_pretty = 'norse_dvsgesture_lif_results_pretty.csv'
......
......@@ -215,10 +215,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', mean_losses, test_losses)
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -230,10 +228,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_nmnist_izhi_results_raw.csv'
filename_pretty = 'norse_nmnist_izhi_results_pretty.csv'
......
......@@ -226,10 +226,8 @@ plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_train_data" +
"_" + d + '.npy', mean_losses)
np.save("./plots/" + scriptname + "/loss_test_data" +
"_" + d + '.npy', test_losses)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -241,10 +239,8 @@ plt.ylabel("Accuracy")
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_train_data" +
"_" + d + '.npy', train_accuracies)
np.save("./plots/" + scriptname + "/acc_test_data" +
"_" + d + '.npy', test_accuracies)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_nmnist_izhi_results_raw.csv'
filename_pretty = 'norse_nmnist_izhi_results_pretty.csv'
......
......@@ -133,7 +133,8 @@ HIDDEN_FEATURES_L1 = 100
HIDDEN_FEATURES_L2 = 100
HIDDEN_FEATURES_L3 = 100
OUTPUT_FEATURES = len(trainset.classes)
DEVICE = torch.device("cuda")
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
layers = (HIDDEN_FEATURES_L1, HIDDEN_FEATURES_L2, HIDDEN_FEATURES_L3)
model = Model(
snn=SNN(
......@@ -195,6 +196,7 @@ test_accuracies = []
torch.autograd.set_detect_anomaly(True)
pbar = tqdm(total=EPOCHS, colour='blue', leave=False)
start_time = time.time()
for epoch in range(EPOCHS):
training_loss, mean_loss, train_accuracy = train(
model, DEVICE, train_loader, optimizer)
......@@ -206,14 +208,18 @@ for epoch in range(EPOCHS):
test_accuracies.append(test_accuracy)
pbar.update()
print("Izhikevich Parameter:")
print("a:", p_izhi.a,
"\nb:", p_izhi.b,
"\nc:", p_izhi.c,
"\nd:", p_izhi.d,
"\nbias:", p_izhi.bias,
"\nthreshold:", p_izhi.v_th,
"\nslope:", p_izhi.alpha)
runtime = time.time() - start_time
date = datetime.datetime.now()
filetype = '.png'
d = str(date.day) + "_" + str(date.month) + "_" + \
str(date.year) + "_" + str(date.time())
d = d.split('.')[0]
scriptname = sys.argv[0].split('.')[0]
tf_str = str(transform).replace(" ", "").splitlines(keepends=False)[1:-1]
print(
f"Train Accuracy:{train_accuracies[-1]:.2f}, Test Accuracy{test_accuracies[-1]:.2f}")
fig = plt.figure(facecolor="w", figsize=(10, 5))
......@@ -223,6 +229,11 @@ plt.title("Loss Curves")
plt.legend(["Train Loss", "Test Loss"])
plt.xlabel("Epoch")
plt.ylabel("Loss")
# plt.show()
plt.savefig("./plots/" + scriptname + "/loss" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/loss_data" +
"_" + d + '.npy', [mean_losses, test_losses])
fig = plt.figure(facecolor="w", figsize=(10, 5))
plt.plot(train_accuracies)
......@@ -231,4 +242,77 @@ plt.title("Accuracy Curves")
plt.legend(["Train Accuracy", "Test Accuracy"])
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.show()
# plt.show()
plt.savefig("./plots/" + scriptname + "/acc" + "_" + d + filetype)
np.save("./plots/" + scriptname + "/acc_data" +
"_" + d + '.npy', [train_accuracies, test_accuracies])
filename_raw = 'norse_nmnist_izhi_results_raw.csv'
filename_pretty = 'norse_nmnist_izhi_results_pretty.csv'
fields = ['n',
' date',
' filename',
' device',
' runtime',
' accuracy (train, test)',
' loss (train, test)',
' epochs',
' batch_size',
' lr',
' layers',
' first saccade only',
' method',
' alpha',
' a',
' b',
' c',
' d',
' bias',
' tau_inv',
' v_th',
' transform']
num_rows = 0
for r in open(filename_raw):
num_rows += 1
if num_rows != 0:
num_rows -= 1
rows = [[num_rows,
d.replace('_', ' '),
sys.argv[0].split('.')[0] + '.py',
DEVICE,
runtime,
(train_accuracies[-1], test_accuracies[-1]),
(mean_losses[-1], test_losses[-1]),
EPOCHS,
BATCH_SIZE,
LR,
layers,
first_saccade_only,
p_izhi.method,
p_izhi.alpha,
p_izhi.a,
p_izhi.b,
p_izhi.c,
p_izhi.d,
p_izhi.bias,
p_izhi.tau_inv,
p_izhi.v_th,
tf_str]]
with open(filename_raw, 'a') as csvfile:
csvwriter = csv.writer(csvfile)
if num_rows == 0:
csvwriter.writerow(fields)
csvwriter.writerows(rows)
with open(filename_raw) as fp:
table = from_csv(fp)