run.py 3.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""Brain Mapper Run File

Description:
-------------
This file contains all the relevant functions for running BrainMapper.
The network can be ran in one of these modes:
    - train
    - evaluate path
    - evaluate whole


Usage
-------------
In order to run the network, in the terminal, the user needs to pass it relevant arguments:
    - (TODO: ADD ARGUMENTS)

"""

19
20
21
import torch
from utils.data_utils import get_datasets
import BrainMapperUNet as BrainMapperUNet
22
import torch.utils.data as data
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

# Set the default floating point tensor type to FloatTensor

torch.set_default_tensor_type(torch.FloatTensor)

def load_data(data_parameters):
    """Dataset Loader

    This function loads the training and testing datasets.
    TODO: Will need to define if all the training data is loaded as bulk or individually!

    Args:
        data_parameters (dict): Dictionary containing relevant information for the datafiles.
        data_parameters = {
            data_directory: 'path/to/directory'
            train_data_file: 'training_data'
            train_output_targets: 'training_targets'
            test_data_file: 'testing_data'
            test_target_file: 'testing_targets'
        }

    Returns:
        train_data (dataset object): Pytorch map-style dataset object, mapping indices to training data samples.
        test_data (dataset object): Pytorch map-style dataset object, mapping indices to testing data samples.

    Raises:
        None

    """
    print("Data is loading...")
    train_data, test_data = get_datasets(data_parameters)
    print("Data has loaded!")
    print("Training dataset size is {}".format(len(train_data)))
    print("Testing dataset size is {}".format(len(test_data)))

    return train_data, test_data
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
def train(data_parameters, training_parameters):
    """Name
    
    Desc
    Currently, the data loaded is set to have multiple sub-processes. 
    A high enough number of workers assures that CPU computations are efficiently managed, i.e. that the bottleneck is indeed the neural network's forward and backward operations on the GPU (and not data generation)
    Loader memory is also pinned, to speed up data transfer from CPU to GPU  by using the page-locked memory.
    Train data is also re-shuffled at each training epoch. 

    Args:
        data_parameters(dict):
        training_parameters(dict):{
            paraters
        }
        network_parameters (dict): Contains information relevant parameters = {
            parameters
        }
        parameters = {
            'kernel_heigth': 5
            'kernel_width': 5
            'kernel_classification': 1
            'input_channels': 1
            'output_channels': 64
            'convolution_stride': 1
            'dropout': 0.2
            'pool_kernel_size': 2
            'pool_stride': 2
            'up_mode': 'upconv'
            'number_of_classes': 1
        }

    Returns:
        None

    Raises:
        None
    """

    train_data, test_data = load_data(data_parameters)

    train_loader = data.DataLoader(
        dataset= train_data,
        batch_size= training_parameters['train_batch_size'],
        shuffle= True,
        num_workers= 4,
        pin_memory= True
    )

    test_loader = data.DataLoader(
        dataset= test_data,
        batch_size= training_parameters['test_batch_size'],
        shuffle= False,
        num_workers= 4,
        pin_memory= True 
    )

    if training_parameters['use_pre_trained']:
        BrainMapperModel = torch.load(training_parameters['pre_trained_path'])
    else:
        BrainMapperModel = BrainMapperUNet(network_parameters)

    solver = Solver(
        # TODO - need to write the solver !
    )
124
125
126
127
128
129
130
131
132
133
134
135

def evaluate_path():
    pass

def evaluate_network():
    pass

def delete_files():
    pass

if __name__ == '__main__':
    pass