Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format with black #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions alternative_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
transform_no_aug = transforms.Compose([TT, NRM])

# Downloading/Louding CIFAR10 data
trainset = CIFAR10(root='./data', train=True, download=True) # , transform = transform_with_aug)
testset = CIFAR10(root='./data', train=False, download=True) # , transform = transform_no_aug)
trainset = CIFAR10(
root="./data", train=True, download=True
) # , transform = transform_with_aug)
testset = CIFAR10(
root="./data", train=False, download=True
) # , transform = transform_no_aug)
classDict = trainset.class_to_idx

# Separating trainset/testset data/label
Expand All @@ -31,6 +35,7 @@

# Define a function to separate CIFAR classes by class index


def get_class_i(x, y, i):
"""
x: trainset.train_data or testset.test_data
Expand Down Expand Up @@ -89,35 +94,43 @@ def index_of_which_bin(self, bin_sizes, absolute_index, verbose=False):

# ================== Usage ================== #

def get_n_fold_datasets_train(t, batch_size, class_names=['cat', 'dog']):

def get_n_fold_datasets_train(t, batch_size, class_names=["cat", "dog"]):

# Let's choose cats (class 3 of CIFAR) and dogs (class 5 of CIFAR) as trainset/testset
cat_dog_trainset = \
DatasetMaker(
[get_class_i(x_train, y_train, classDict[class_names[0]]), get_class_i(x_train, y_train, classDict[class_names[1]])],
transform_with_aug
)
cat_dog_trainset = DatasetMaker(
[
get_class_i(x_train, y_train, classDict[class_names[0]]),
get_class_i(x_train, y_train, classDict[class_names[1]]),
],
transform_with_aug,
)

kwargs = {'num_workers': 3, 'pin_memory': False}
kwargs = {"num_workers": 3, "pin_memory": False}

# Create datasetLoaders from trainset and testse

trainsetLoader = DataLoader(cat_dog_trainset, batch_size=batch_size, shuffle=True, **kwargs)
trainsetLoader = DataLoader(
cat_dog_trainset, batch_size=batch_size, shuffle=True, **kwargs
)
return trainsetLoader


def get_n_fold_datasets_test(t, batch_size, class_names=['cat', 'dog']):
def get_n_fold_datasets_test(t, batch_size, class_names=["cat", "dog"]):
# Let's choose cats (class 3 of CIFAR) and dogs (class 5 of CIFAR) as trainset/testset
cat_dog_testset = \
DatasetMaker(
[get_class_i(x_test, y_test, classDict[class_names[0]]),
get_class_i(x_test, y_test, classDict[class_names[1]])],
transform_no_aug
)
cat_dog_testset = DatasetMaker(
[
get_class_i(x_test, y_test, classDict[class_names[0]]),
get_class_i(x_test, y_test, classDict[class_names[1]]),
],
transform_no_aug,
)

kwargs = {'num_workers': 3, 'pin_memory': False}
kwargs = {"num_workers": 3, "pin_memory": False}

# Create datasetLoaders from trainset and testse

testsetLoader = DataLoader(cat_dog_testset, batch_size=batch_size, shuffle=False, **kwargs)
return testsetLoader
testsetLoader = DataLoader(
cat_dog_testset, batch_size=batch_size, shuffle=False, **kwargs
)
return testsetLoader
38 changes: 26 additions & 12 deletions csv_logger.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
import csv
import pandas as pd

def extract_metrics_from_ordeered_dict(ordered_dict, mode='train', result={}):

def extract_metrics_from_ordeered_dict(ordered_dict, mode="train", result={}):
for key in ordered_dict.keys():
name = key.split('/')[-1]
result[mode+'_'+name] = [ordered_dict[key]]
name = key.split("/")[-1]
result[mode + "_" + name] = [ordered_dict[key]]
return result


def extract_metrics_from_scalaer_dict(log_dict):
result_dict = {}
for key in log_dict.keys():
mode = key.split('-')[0]
mode = key.split("-")[0]
extract_metrics_from_ordeered_dict(log_dict[key], mode, result_dict)
return result_dict


def log_to_csv(value_dict, savename):
df = pd.DataFrame.from_dict(value_dict)
df.to_csv(savename+'.csv', sep=';')
df.to_csv(savename + ".csv", sep=";")


def record_metrics(value_dict, log_dict, train_accuracy, train_loss, test_accuracy, test_loss, epoch, time):
def record_metrics(
value_dict,
log_dict,
train_accuracy,
train_loss,
test_accuracy,
test_loss,
epoch,
time,
):

result_dict = extract_metrics_from_scalaer_dict(log_dict)
result_dict['train_accuracy'] = [train_accuracy]
result_dict['test_accuracy'] = [test_accuracy]
result_dict['train_loss'] = [train_loss]
result_dict['test_loss'] = [test_loss]
result_dict['epoch'] = [epoch]
result_dict['time_per_step'] = [time]
result_dict["train_accuracy"] = [train_accuracy]
result_dict["test_accuracy"] = [test_accuracy]
result_dict["train_loss"] = [train_loss]
result_dict["test_loss"] = [test_loss]
result_dict["epoch"] = [epoch]
result_dict["time_per_step"] = [time]
if value_dict is None:
return result_dict
else:
Expand Down
17 changes: 10 additions & 7 deletions extract_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@


def tabulate_events(dpath):
summary_iterators = [EventAccumulator(os.path.join(dpath, dname)).Reload() for dname in os.listdir(dpath)]
summary_iterators = [
EventAccumulator(os.path.join(dpath, dname)).Reload()
for dname in os.listdir(dpath)
]

tags = summary_iterators[0].Tags()['scalars']
tags = summary_iterators[0].Tags()["scalars"]

for it in summary_iterators:
assert it.Tags()['scalars'] == tags
assert it.Tags()["scalars"] == tags

out = defaultdict(list)
steps = []
Expand Down Expand Up @@ -41,13 +44,13 @@ def to_csv(dpath):


def get_file_path(dpath, tag):
file_name = tag.replace("/", "_") + '.csv'
folder_path = os.path.join(dpath, 'csv')
file_name = tag.replace("/", "_") + ".csv"
folder_path = os.path.join(dpath, "csv")
if not os.path.exists(folder_path):
os.makedirs(folder_path)
return os.path.join(folder_path, file_name)


if __name__ == '__main__':
if __name__ == "__main__":
path = "train_run_576_1152_2304"
to_csv(path)
to_csv(path)
Loading