-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.py
65 lines (52 loc) · 1.85 KB
/
config.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
import torch
import numpy as np
import yaml
import os
class ProjectConfig:
def __init__(self, cfg_path=None, exp_dir=None):
self.WINDOW_WIDTH = None
self.WINDOW_SIZE = None
self.STRIDE = None
self.IN_CHANNELS = None
self.BATCH_SIZE = None
self.FOLDER = None
self.LABELS = None
self.N_CLASSES = None
self.WEIGHTS = None
self.CACHE = None
self.DATASET = None
self.model_final = None
self.MAIN_FOLDER = None
self.DATA_FOLDER = None
self.LABEL_FOLDER = None
# Load a default config initially
# TODO: Change this path
self.init_paths(cfg_path='./experiment/cfg.yml', exp_dir='./experiment/')
def init_paths(self, cfg_path=None, exp_dir=None):
self.cfg_path = cfg_path
self.exp_dir = exp_dir
self.output_dir = self.exp_dir + 'output/'
self.load_config()
def load_config(self):
# Load config from the config file path
with open(self.cfg_path, 'r') as cfg_stream:
data = yaml.load(cfg_stream)
# parse this and save the items to their corresponding values
self.WINDOW_WIDTH = data['model']['window_size']
self.WINDOW_SIZE = (self.WINDOW_WIDTH, self.WINDOW_WIDTH)
self.STRIDE = data['model']['stride']
self.IN_CHANNELS = data['model']['in_channels']
self.BATCH_SIZE = data['model']['batch_size']
self.FOLDER = data['data_folder']
self.LABELS = data['labels']
self.N_CLASSES = len(self.LABELS)
self.WEIGHTS = torch.from_numpy(np.asarray(data['class_weights'], dtype=np.float32))
self.CACHE = data['cache']
self.DATASET = data['data']['dataset']
self.model_final = data['model_final_path']
self.MODEL_PATH = data['model_checkpoint']
self.MAIN_FOLDER = data['data']['dataset_path']
self.DATA_FOLDER = self.MAIN_FOLDER + data['data']['input_folder'] + '/{}.' + data['data']['data_format']
self.LABEL_FOLDER = self.MAIN_FOLDER + data['data']['label_folder'] + '/{}.tif'
# create an object
cfg = ProjectConfig()