This repository has been archived by the owner on Mar 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
140 lines (113 loc) · 3.94 KB
/
main.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
import sys
from comet_ml import Experiment, OfflineExperiment
import logging
import warnings
warnings.simplefilter(action="ignore")
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold
import os
import torch
import torch.nn as nn
import matplotlib
np.random.seed(42)
torch.cuda.empty_cache()
# We import from other files
from config import args
from utils.utils import *
from data_loader.loader import *
from utils.load_data import (
load_pickled_dataset,
prepare_and_save_plots_dataset,
load_ground_truths_dataframe,
get_plot_ground_truth_coverages,
)
from learning.accuracy import *
from learning.kde_mixture import get_fitted_kde_mixture_from_dataset
from learning.train import train_full
from utils.load_data import load_pickled_dataset
from argparse import ArgumentParser
np.random.seed(42)
torch.cuda.empty_cache()
setup_experiment_folder(args, task="learning")
logger = create_a_logger(args)
experiment = launch_comet_experiment(args)
logger.info("args: \n" + str(args))
# try:
# dataset = load_pickled_dataset(args)
# except FileNotFoundError:
dataset = prepare_and_save_plots_dataset(args, args.corrected_gt_file_path)
logger.info(f"Dataset contains {len(dataset)} plots.")
# KDE Mixture
args.kde_mixture = get_fitted_kde_mixture_from_dataset(dataset, args)
def cross_validate():
# cross-validation
all_folds_loss_train_dicts = []
all_folds_loss_test_dicts = []
cloud_info_list_by_fold = {}
kf = KFold(n_splits=args.folds, random_state=42, shuffle=True)
for args.current_fold_id, (train_idx, val_idx) in enumerate(
kf.split(dataset), start=1
):
logger.info(f"Cross-validation FOLD = {args.current_fold_id}")
experiment.log_metric("Fold_ID", args.current_fold_id)
# CROSSVAL FOLD
train_set, test_set = get_train_val_datasets(
dataset, args, train_idx=train_idx, val_idx=val_idx
)
(
_,
all_epochs_train_loss_dict,
all_epochs_test_loss_dict,
cloud_info_list,
) = train_full(
train_set,
test_set,
args,
)
# UPDATE LOGS
log_last_stats_of_fold(
all_epochs_train_loss_dict,
all_epochs_test_loss_dict,
args,
)
all_folds_loss_train_dicts.append(all_epochs_train_loss_dict)
all_folds_loss_test_dicts.append(all_epochs_test_loss_dict)
cloud_info_list_by_fold[args.current_fold_id] = cloud_info_list
if args.mode == "DEV" and args.current_fold_id >= 1:
break
# UPDATE LOGS using relabeled data
for cloud_info_list in cloud_info_list_by_fold.values():
for cloud_info in cloud_info_list:
cloud_info["vt_veg_b"] = get_closest_class_center(cloud_info["vt_veg_b"])
cloud_info["vt_sol_nu"] = get_closest_class_center(cloud_info["vt_sol_nu"])
cloud_info["vt_veg_moy"] = get_closest_class_center(
cloud_info["vt_veg_moy"]
)
cloud_info["vt_veg_h"] = get_closest_class_center(cloud_info["vt_veg_h"])
post_cross_validation_logging(
"relabeled_summary",
all_folds_loss_train_dicts,
all_folds_loss_test_dicts,
cloud_info_list_by_fold,
args,
)
# UPDATE LOGS using original labels
ground_truths = load_ground_truths_dataframe(args.gt_file_path)
for cloud_info_list in cloud_info_list_by_fold.values():
for cloud_info in cloud_info_list:
pl_id = cloud_info["pl_id"]
(
cloud_info["vt_veg_b"],
cloud_info["vt_sol_nu"],
cloud_info["vt_veg_moy"],
cloud_info["vt_veg_h"],
) = get_plot_ground_truth_coverages(ground_truths, pl_id)
post_cross_validation_logging(
"summary",
all_folds_loss_train_dicts,
all_folds_loss_test_dicts,
cloud_info_list_by_fold,
args,
)
cross_validate()