Compare commits

..

No commits in common. "entropy-testing" and "main" have entirely different histories.

6 changed files with 31 additions and 102 deletions

2
LICENSE Executable file → Normal file
View File

@ -1,4 +1,4 @@
Copyright (c) 2024 Eduardo Cueto-Mendoza.
Copyright (c) 2024 TastyPancakes.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

View File

@ -1,7 +1,3 @@
# Energy efficiency comparison
This experiment compares a Frequentist CNN model against a Bayesian CNN model
## Example run command
python run_service.py -f1 -s -e && sleep 60 && python run_service.py -f2 -s -e && sleep 60 && python run_service.py -f3 -s -e && sleep 60 && python run_service.py -f4 -s -e && sleep 60 && python run_service.py -f5 -s -e && sleep 60 & python run_service.py -f6 -s -e && sleep 60 && python run_service.py -f7 -s -e && sleep 60 && python run_service.py -b1 -s -e && sleep 60 && python run_service.py -b2 -s -e && sleep 60 && python run_service.py -b3 -s -e && sleep 60 && python run_service.py -b4 -s -e && sleep 60 && python run_service.py -b5 -s -e && sleep 60 && python run_service.py -b6 -s -e && sleep 60 && python run_service.py -b7 -s -e && sleep 60
This experiment compares a Frequentist CNN model against a Bayesian CNN model

View File

@ -22,6 +22,6 @@ def makeArguments(arguments: ArgumentParser) -> dict:
help="Save model")
all_args.add_argument('--net_type', default='lenet', type=str,
help='model = [lenet/AlexNet/3Conv3FC]')
all_args.add_argument('--dataset', default='MNIST', type=str,
all_args.add_argument('--dataset', default='CIFAR10', type=str,
help='dataset = [MNIST/CIFAR10/CIFAR100]')
return vars(all_args.parse_args())

61
main_bayesian.py Normal file → Executable file
View File

@ -4,7 +4,7 @@ import os
import data
import utils
import torch
# import pickle
import pickle
import metrics
import numpy as np
from datetime import datetime
@ -15,41 +15,12 @@ from models.BayesianModels.BayesianAlexNet import BBBAlexNet
from models.BayesianModels.Bayesian3Conv3FC import BBB3Conv3FC
from stopping_crit import earlyStopping, energyBound, accuracyBound
# with (open("configuration.pkl", "rb")) as file:
# while True:
# try:
# cfg = pickle.load(file)
# except EOFError:
# break
cfg = {
"model": {"net_type": "lenet", "type": "bayes", "size": 1,
"layer_type": "lrt", "activation_type": "softplus",
"priors": {
'prior_mu': 0,
'prior_sigma': 0.1,
'posterior_mu_initial': (0, 0.1), # (mean,std) normal_
'posterior_rho_initial': (-5, 0.1), # (mean,std) normal_
},
"n_epochs": 100,
"sens": 1e-9,
"energy_thrs": 100000,
"acc_thrs": 0.99,
"lr": 0.001,
"num_workers": 4,
"valid_size": 0.2,
"batch_size": 256,
"train_ens": 1,
"valid_ens": 1,
"beta_type": 0.1, # 'Blundell','Standard',etc.
# Use float for const value
},
#"data": "CIFAR10",
"data": "MNIST",
"stopping_crit": 1,
"save": 1,
"pickle_path": None,
}
with (open("configuration.pkl", "rb")) as file:
while True:
try:
cfg = pickle.load(file)
except EOFError:
break
# CUDA settings
@ -155,7 +126,8 @@ def run(dataset, net_type):
activation_type).to(device)
ckpt_dir = f'checkpoints/{dataset}/bayesian'
ckpt_name = f'checkpoints/{dataset}/bayesian/model_{net_type}_{layer_type}_{activation_type}_{cfg["model"]["size"]}'
ckpt_name = f'checkpoints/{dataset}/bayesian/model_{net_type}_{layer_type}\
_{activation_type}_{cfg["model"]["size"]}.pt'
if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir, exist_ok=True)
@ -206,23 +178,18 @@ def run(dataset, net_type):
break
elif stp == 4:
print('Using accuracy bound')
if accuracyBound(train_acc, cfg["model"]["acc_thrs"]) == 1:
if accuracyBound(train_acc, cfg.acc_thrs) == 1:
break
else:
print('Training for {} epochs'.format(cfg["model"]["n_epochs"]))
if sav == 1:
# save model when finished
# if epoch == cfg["model"]["n_epochs"]-1:
torch.save({
'epoch': epoch,
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': train_loss,
}, ckpt_name + '_epoch_{}.pt'.format(epoch))
if epoch == cfg.n_epochs-1:
torch.save(net.state_dict(), ckpt_name)
# with open("bayes_exp_data_"+str(cfg["model"]["size"])+".pkl", 'wb') as f:
# pickle.dump(train_data, f)
with open("bayes_exp_data_"+str(cfg["model"]["size"])+".pkl", 'wb') as f:
pickle.dump(train_data, f)
if __name__ == '__main__':

60
main_frequentist.py Normal file → Executable file
View File

@ -2,7 +2,7 @@ from __future__ import print_function
import os
import data
import torch
# import pickle
import pickle
import metrics
import numpy as np
import torch.nn as nn
@ -13,41 +13,12 @@ from models.NonBayesianModels.AlexNet import AlexNet
from stopping_crit import earlyStopping, energyBound, accuracyBound
from models.NonBayesianModels.ThreeConvThreeFC import ThreeConvThreeFC
# with (open("configuration.pkl", "rb")) as file:
# while True:
# try:
# cfg = pickle.load(file)
# except EOFError:
# break
cfg = {
"model": {"net_type": "lenet", "type": "freq", "size": 1,
"layer_type": "lrt", "activation_type": "softplus",
"priors": {
'prior_mu': 0,
'prior_sigma': 0.1,
'posterior_mu_initial': (0, 0.1), # (mean,std) normal_
'posterior_rho_initial': (-5, 0.1), # (mean,std) normal_
},
"n_epochs": 100,
"sens": 1e-9,
"energy_thrs": 100000,
"acc_thrs": 0.99,
"lr": 0.001,
"num_workers": 4,
"valid_size": 0.2,
"batch_size": 256,
"train_ens": 1,
"valid_ens": 1,
"beta_type": 0.1, # 'Blundell','Standard',etc.
# Use float for const value
},
#"data": "CIFAR10",
"data": "MNIST",
"stopping_crit": 1,
"save": 1,
"pickle_path": None,
}
with (open("configuration.pkl", "rb")) as file:
while True:
try:
cfg = pickle.load(file)
except EOFError:
break
# CUDA settings
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
@ -109,7 +80,8 @@ def run(dataset, net_type):
net = getModel(net_type, inputs, outputs).to(device)
ckpt_dir = f'checkpoints/{dataset}/frequentist'
ckpt_name = f'checkpoints/{dataset}/frequentist/model_{net_type}_{cfg["model"]["size"]}'
ckpt_name = f'checkpoints/{dataset}/frequentist/model\
_{net_type}_{cfg["model"]["size"]}.pt'
if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir, exist_ok=True)
@ -160,17 +132,11 @@ def run(dataset, net_type):
if sav == 1:
# save model when finished
# if epoch == n_epochs:
# torch.save(net.state_dict(), ckpt_name)
torch.save({
'epoch': epoch,
'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': train_loss,
}, ckpt_name + '_epoch_{}.pt'.format(epoch))
if epoch == n_epochs:
torch.save(net.state_dict(), ckpt_name)
# with open("freq_exp_data_"+str(cfg["model"]["size"])+".pkl", 'wb') as f:
# pickle.dump(train_data, f)
with open("freq_exp_data_"+str(cfg["model"]["size"])+".pkl", 'wb') as f:
pickle.dump(train_data, f)
if __name__ == '__main__':

View File

@ -24,7 +24,7 @@ cfg = {
},
"n_epochs": 100,
"sens": 1e-9,
"energy_thrs": 100000,
"energy_thrs": 10000,
"acc_thrs": 0.99,
"lr": 0.001,
"num_workers": 4,