File size: 9,584 Bytes
f97a499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
import numpy as np
import torch
import torch.distributed as dist
from torch.cuda.amp import GradScaler
from abc import ABC, abstractmethod
from utils.iteration.iterator import MetricMeter
from utils.ddp_utils import gather_object_across_processes


class BaseSegmentationModel(ABC):
    """
    This class is an abstract base class (ABC) for segmentation models.
    To create a subclass, you need to implement the following four methods:
       -- <__init__>:                      initialize the class.
       -- <set_input>:                     unpack data from dataset.
       -- <optimize_parameters>:           calculate losses, gradients, and update network weights.
       -- <evaluate_one_step>:                      performance evaluation.
    """
    def __init__(self, cfg, num_classes, amp=False):
        # initialize training CUDA devices
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        # training configuration
        self.cfg = cfg
        self.num_classes = num_classes
        self.is_mixed = amp
        self.scaler = GradScaler()
        self.start_epoch = -1

        # initialize networks, criterion, optimizer and scheduler
        self.network = None
        self.criterion = None
        self.optimizer = None
        self.scheduler = None

        # visualization
        self.visual_names = []
        self.loss_names = []

    def train(self):
        self.network.train()
        return self

    def eval(self):
        self.network.eval()
        return self

    def training(self):
        return self.network.training

    def initialize_metric_meter(self, class_list):
        self.class_list = class_list
        self.metric_meter = MetricMeter(metrics=['dice', 'hd95', 'asd'], class_names=class_list, subject_names=['name'])
        self.train_loss = MetricMeter(metrics=self.loss_names, class_names=['train'])
        self.val_loss = MetricMeter(metrics=['loss'], class_names=['val'])

    def update_loss_meter(self, print=False):
        loss_dict = {}
        for loss_name in self.loss_names:
            try:
                loss_value = float(getattr(self, loss_name))
                loss_list = gather_object_across_processes(loss_value)
                loss_value = np.mean(loss_list)
            except:
                continue
            loss_dict['train_{}'.format(loss_name)] = loss_value
        self.train_loss.update(loss_dict)
        stats = self.train_loss.report(print_stats=print, mean_only=True)
        return stats

    @abstractmethod
    def set_input(self, *args, **kwargs):
        raise NotImplementedError

    @abstractmethod
    def optimize_parameters(self, *args, **kwargs):
        raise NotImplementedError

    @abstractmethod
    def evaluate_one_step(self, *args, **kwargs):
        raise NotImplementedError

    def load_networks(self, ckpt_path, resume_training=False):
        checkpoint = torch.load(ckpt_path, map_location=self.device)
        print('Load ckpt weight: {}'.format(ckpt_path))
        self.network.load_state_dict(checkpoint['net'])
        if resume_training:
            print('Load training config for breakpoint continuation')
            self.optimizer.load_state_dict(checkpoint['optimizer'])
            self.scheduler.load_state_dict(checkpoint['scheduler'])
            self.scaler.load_state_dict(checkpoint['scaler'])
            self.start_epoch = checkpoint['epoch']

    def save_networks(self, epoch_index, save_dir):
        if dist.get_rank() == 0:
            checkpoint = {
                "net": self.network.state_dict(),
                'optimizer': self.optimizer.state_dict(),
                'scheduler': self.scheduler.state_dict(),
                'scaler': self.scaler.state_dict(),
                "epoch": epoch_index
            }
            torch.save(checkpoint,
                       os.path.join(save_dir, 'Epoch_{}.pkl'.format(epoch_index + 1)))


class MultiNetworkSegmentationModel(ABC):
    """
    This class is an abstract base class (ABC) for segmentation models.
    To create a subclass, you need to implement the following four methods:
       -- <__init__>:                      initialize the class.
       -- <set_input>:                     unpack data from dataset.
       -- <optimize_parameters>:           calculate losses, gradients, and update network weights.
       -- <evaluate_one_step>:                      performance evaluation.
    """
    def __init__(self, cfg, num_classes, amp=False):
        # initialize training CUDA devices
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        # training configuration
        self.cfg = cfg
        self.num_classes = num_classes
        self.is_mixed = amp
        self.scaler = GradScaler()
        self.start_epoch = -1

        # initialize networks, criterion, optimizer and scheduler
        self.net_names = []

        # visualization
        self.visual_names = []
        self.loss_names = []

    def train(self):
        for name in self.net_names:
            net = getattr(self, name)
            net.train()
        return self

    def eval(self):
        for name in self.net_names:
            net = getattr(self, name)
            net.eval()
        return self

    def training(self):
        return getattr(self, self.net_names[0]).training

    def initialize_metric_meter(self, class_list):
        self.class_list = class_list
        self.metric_meter = MetricMeter(metrics=['dice', 'hd95', 'asd'], class_names=class_list, subject_names=['name'])
        self.train_loss = MetricMeter(metrics=self.loss_names, class_names=['train'])
        self.val_loss = MetricMeter(metrics=['loss'], class_names=['val'])

    def update_loss_meter(self, print=False):
        loss_dict = {}
        for loss_name in self.loss_names:
            try:
                loss_value = float(getattr(self, loss_name))
                loss_list = gather_object_across_processes(loss_value)
                loss_value = np.mean(loss_list)
            except:
                continue
            loss_dict['train_{}'.format(loss_name)] = loss_value
        self.train_loss.update(loss_dict)
        stats = self.train_loss.report(print_stats=print, mean_only=True)
        return stats

    @abstractmethod
    def set_input(self, *args, **kwargs):
        raise NotImplementedError

    @abstractmethod
    def optimize_parameters(self, *args, **kwargs):
        raise NotImplementedError

    @abstractmethod
    def evaluate_one_step(self, *args, **kwargs):
        raise NotImplementedError

    def load_networks(self, ckpt_path, resume_training=False, strict=True):
        checkpoint = torch.load(ckpt_path, map_location=self.device)
        print('Load ckpt weight: {}'.format(ckpt_path))
        if resume_training:
            print('Load training config for breakpoint continuation')
            self.scaler.load_state_dict(checkpoint['scaler'])
            self.start_epoch = checkpoint['epoch']
        for name in self.net_names:
            try:
                getattr(self, name).load_state_dict(checkpoint[name], strict=strict)
                if resume_training:
                    getattr(self, '{}_optimizer'.format(name)).load_state_dict(checkpoint['{}_optimizer'.format(name)])
                    getattr(self, '{}_scheduler'.format(name)).load_state_dict(checkpoint['{}_scheduler'.format(name)])
            except:
                print('Failed to load network: {}'.format(name))

    def load_single_network(self, ckpt_path, net_name, resume_training=False, strict=True):
        checkpoint = torch.load(ckpt_path, map_location=self.device)
        print('Load ckpt weight: {}'.format(ckpt_path))
        if resume_training:
            print('Load training config for breakpoint continuation')
            self.scaler.load_state_dict(checkpoint['scaler'])
            self.start_epoch = checkpoint['epoch']
        getattr(self, net_name).load_state_dict(checkpoint[net_name], strict=strict)
        if resume_training:
            getattr(self, '{}_optimizer'.format(net_name)).load_state_dict(checkpoint['{}_optimizer'.format(net_name)])
            getattr(self, '{}_scheduler'.format(net_name)).load_state_dict(checkpoint['{}_scheduler'.format(net_name)])

    def save_networks(self, epoch_index, save_dir):
        if dist.get_rank() == 0:
            checkpoint = {}
            for name in self.net_names:
                checkpoint[name] = getattr(self, name).state_dict()
                checkpoint['{}_optimizer'.format(name)] = getattr(self, '{}_optimizer'.format(name)).state_dict()
                checkpoint['{}_scheduler'.format(name)] = getattr(self, '{}_scheduler'.format(name)).state_dict()
            checkpoint['scaler'] = self.scaler.state_dict()
            checkpoint['epoch'] = epoch_index
            torch.save(checkpoint, os.path.join(save_dir, 'Epoch_{}.pkl'.format(epoch_index)))

    def save_best_networks(self, epoch_index, save_dir):
        if dist.get_rank() == 0:
            checkpoint = {}
            for name in self.net_names:
                checkpoint[name] = getattr(self, name).state_dict()
                checkpoint['{}_optimizer'.format(name)] = getattr(self, '{}_optimizer'.format(name)).state_dict()
                checkpoint['{}_scheduler'.format(name)] = getattr(self, '{}_scheduler'.format(name)).state_dict()
            checkpoint['scaler'] = self.scaler.state_dict()
            checkpoint['epoch'] = epoch_index
            torch.save(checkpoint, os.path.join(save_dir, 'Epoch_best.pkl'))