Modified the code for frequentist experiment

This commit is contained in:
Eduardo Cueto 2023-06-29 11:45:28 +01:00
parent a2300273a5
commit 0f8ee842a3
3 changed files with 35 additions and 64 deletions

18
.gitignore vendored
View File

@ -1,19 +1,21 @@
__pycache__/
checkpoints/
data/
data_budget/
experiment-power-draw/
**/__pycache__/
**/__init__.py
**/**/__pycache__/
**/**/__init__.py
stp
sav
**/__pycache__/
**/__init__.py
checkpoints/
__pycache__/
data_budget/
bayes_*
times_*
.vscode
freq_*
data/
.idea
*.pkl
*.txt
stp
sav
bay
frq
sav

View File

@ -8,20 +8,26 @@ import metrics
import argparse
import numpy as np
import torch.nn as nn
import amd_sample_draw
from datetime import datetime
import config_frequentist as cfg
from torch.optim import Adam, lr_scheduler
from gpu_power_func import total_watt_consumed
from models.NonBayesianModels.LeNet import LeNet
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
# CUDA settings
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
def getModel(net_type, inputs, outputs,wide=cfg.wide):
def getModel(net_type, inputs, outputs,wide=cfg["model"]["size"]):
if (net_type == 'lenet'):
return LeNet(outputs, inputs,wide)
elif (net_type == 'alexnet'):
@ -64,11 +70,11 @@ def validate_model(net, criterion, valid_loader):
def run(dataset, net_type):
# Hyper Parameter settings
n_epochs = cfg.n_epochs
lr = cfg.lr
num_workers = cfg.num_workers
valid_size = cfg.valid_size
batch_size = cfg.batch_size
n_epochs = cfg["model"]["n_epochs"]
lr = cfg["model"]["lr"]
num_workers = cfg["model"]["num_workers"]
valid_size = cfg["model"]["valid_size"]
batch_size = cfg["model"]["batch_size"]
trainset, testset, inputs, outputs = data.getDataset(dataset)
train_loader, valid_loader, test_loader = data.getDataloader(
@ -76,15 +82,13 @@ 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.wide}.pt'
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)
with open("stp", "r") as file:
stp = int(file.read())
with open("sav", "r") as file:
sav = int(file.read())
stp = cfg["stopping_crit"]
sav = cfg["save"]
criterion = nn.CrossEntropyLoss()
optimizer = Adam(net.parameters(), lr=lr)
@ -108,25 +112,25 @@ def run(dataset, net_type):
if stp == 2:
#print('Using early stopping')
if earlyStopping(early_stop,valid_acc,epoch,cfg.sens) == 1:
if earlyStopping(early_stop,valid_acc,epoch,cfg["model"]["sens"]) == 1:
break
elif stp == 3:
#print('Using energy bound')
if energyBound(cfg.energy_thrs) == 1:
if energyBound(cfg["model"]["energy_thrs"]) == 1:
break
elif stp == 4:
#print('Using accuracy bound')
if accuracyBound(train_acc,cfg.acc_thrs) == 1:
if accuracyBound(train_acc,cfg["model"]["acc_thrs"]) == 1:
break
else:
print('Training for {} epochs'.format(cfg.n_epochs))
print('Training for {} epochs'.format(cfg["model"]["n_epochs"]))
if sav == 1:
# save model when finished
if epoch == n_epochs:
torch.save(net.state_dict(), ckpt_name)
with open("freq_exp_data_"+str(cfg.wide)+".pkl", 'wb') as f:
with open("freq_exp_data_"+str(cfg["model"]["size"])+".pkl", 'wb') as f:
pickle.dump(train_data, f)
@ -134,11 +138,7 @@ if __name__ == '__main__':
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Initial Time =", current_time)
parser = argparse.ArgumentParser(description = "PyTorch Frequentist Model Training")
parser.add_argument('--net_type', default='lenet', type=str, help='model')
parser.add_argument('--dataset', default='MNIST', type=str, help='dataset = [MNIST/CIFAR10/CIFAR100]')
args = parser.parse_args()
run(args.dataset, args.net_type)
run(cfg["model"]["data"], cfg["model"]["net_type"])
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Final Time =", current_time)

View File

@ -60,34 +60,20 @@ cfg["model"]["size"] = wide
cfg["data"] = args["dataset"]
cfg["model"]["net_type"] = args["net_type"]
#with open("tmp", "w") as file:
# file.write(str(wide))
if args['EarlyStopping']:
cfg["stopping_crit"] = 2
#with open("stp", "w") as file:
# file.write('2')
elif args['EnergyBound']:
cfg["stopping_crit"] = 3
#with open("stp", "w") as file:
# file.write('3')
elif args['AccuracyBound']:
cfg["stopping_crit"] = 4
#with open("stp", "w") as file:
# file.write('4')
else:
cfg["stopping_crit"] = 1
#with open("stp", "w") as file:
# file.write('1')
if args['Save']:
cfg["save"] = 1
#with open("sav", "w") as file:
# file.write('1')
else:
cfg["save"] = 0
#with open("sav", "w") as file:
# file.write('0')
cfg["pickle_path"] = "{}_wattdata_{}.pkl".format(cfg["model"]["type"],cfg["model"]["size"])
@ -96,7 +82,7 @@ with open("configuration.pkl", "wb") as f:
pickle.dump(cfg, f)
#print(args)
print(cfg)
#print(cfg)
sleep(3)
@ -105,18 +91,10 @@ if cmd[1] == "main_frequentist.py":
cmd2 = ["./cpu_watt.sh", "freq_{}_cpu_watts".format(wide)]
cmd3 = ["./mem_free.sh", "freq_{}_ram_use".format(wide)]
cmd4 = ["./radeontop.sh", "freq_{}_flop_app".format(wide)]
#with open("frq", "w") as file:
# file.write(str(1))
#with open("bay", "w") as file:
# file.write(str(0))
elif cmd[1] == "main_bayesian.py":
cmd2 = ["./cpu_watt.sh", "bayes_{}_cpu_watts".format(wide)]
cmd3 = ["./mem_free.sh", "bayes_{}_ram_use".format(wide)]
cmd4 = ["./radeontop.sh", "bayes_{}_flop_app".format(wide)]
#with open("bay", "w") as file:
# file.write(str(1))
#with open("frq", "w") as file:
# file.write(str(0))
path = sub.check_output(['pwd'])
@ -126,18 +104,9 @@ path = path.replace('\n', '')
startWattCounter = 'python ' + path + '/amd_sample_draw.py'
#test = startNODE.split()
#test.append(pythonEnd)
#test.append(pythonEnd2)
#startNODE = test
#print(startNODE)
#print(startWattCounter)
p1 = sub.Popen(cmd)
#p2 = sub.Popen(startWattCounter.split(),stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
p2 = sub.Popen(startWattCounter.split())
p2 = sub.Popen(startWattCounter.split(),stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
p3 = sub.Popen(cmd2,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
p4 = sub.Popen(cmd3,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)
p5 = sub.Popen(cmd4,stdin=sub.PIPE,stdout=sub.PIPE, stderr=sub.PIPE)