-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
183 lines (146 loc) · 6.15 KB
/
main.py
File metadata and controls
183 lines (146 loc) · 6.15 KB
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
import os
import torch
from torchvision import models
from src.__init__ import ROOT_DIR
from src.data.dataloader import get_datalaoders
from src.models.test import test_chexpert
from src.models.train import train_chexpert
from src.models.test import test_brixia
from src.models.train import train_brixia
from src.models.train import train_combined
from src.models.test import test_combined
def chexpert_model(pretrained_model_path=None, **train_config):
""" Trains a model on the CheXpert dataset
Arguments:
- pretrained_model_path (string): Path of the pretrained model
- train_config (dict): Dictionary of train parameters
Returns:
"""
path_to_data = os.path.join(ROOT_DIR, 'data/chexpert/dataset')
# Create data loaders
train_loader, val_loader, test_loader = get_datalaoders('chexpert', path_to_data, **train_config)
# Use weighted BCE
if train_config['weighted_bce']:
weights = train_loader.dataset.get_weights()
train_config['criterion'] = torch.nn.BCEWithLogitsLoss(pos_weight=weights)
else:
train_config['criterion'] = torch.nn.BCEWithLogitsLoss()
# Load pretrained model on ImageNet
model = models.densenet121(pretrained=True)
# Change last layer to CheXpert targets
model.classifier = torch.nn.Linear(1024, len(train_loader.dataset.PRED_LABEL))
# Load pretrained model on CheXpert
if pretrained_model_path is not None:
model.load_state_dict(torch.load(pretrained_model_path))
# Train model
pretrained_model_path = train_chexpert(model, train_loader, val_loader, **train_config)
# Load trained model
model.load_state_dict(torch.load(pretrained_model_path))
# Test model
test_chexpert(model, test_loader, **train_config)
def brixia_model(pretrained_model_path=None, **train_config):
""" Trains a model on the BrixIA dataset
Arguments:
- pretrained_model_path (string): Path of the pretrained model
- train_config (dict): Dictionary of train parameters
Returns:
"""
path_to_data = os.path.join(ROOT_DIR, 'data/brixia/')
mode = train_config.get('mode', 'paper')
if mode == 'paper':
num_targets = 6 * 4
elif mode == 'regression':
num_targets = 1
# Create data loaders
train_loader, val_loader, test_loader = get_datalaoders('brixia', path_to_data, **train_config)
# Load pretrained model on ImageNet
model = models.densenet121(pretrained=True)
if pretrained_model_path is not None:
model_dict = torch.load(pretrained_model_path)
# Get output shape of loaded model
num_out = model_dict['classifier.weight'].shape[0]
# Change last layer
model.classifier = torch.nn.Linear(1024, num_out, bias=False)
# Load pretrained model
model.load_state_dict(model_dict)
num_out = model.state_dict()['classifier.weight'].shape[0]
# Change last layer to BrixIA targets
if num_out != num_targets:
model.classifier = torch.nn.Linear(1024, num_targets)
# Train model
pretrained_model_path = train_brixia(model, train_loader, val_loader, **train_config)
# Load model
model.load_state_dict(torch.load(pretrained_model_path))
# Test model
test_brixia(model, test_loader, **train_config)
def combined_model(pretrained_model_path=None, **train_config):
""" Trains a model on the combined datasets
Arguments:
- pretrained_model_path (string): Path of the pretrained model
- train_config (dict): Dictionary of train parameters
Returns:
"""
path_to_data = [os.path.join(ROOT_DIR, 'data/nih/dataset'),
os.path.join(ROOT_DIR, 'data/chexpert/dataset'),
os.path.join(ROOT_DIR, 'data/brixia/')]
# Create data loaders
train_loader, val_loader, test_loader = get_datalaoders('combined', path_to_data, **train_config)
# Use weighted BC
train_config['criterion'] = torch.nn.BCEWithLogitsLoss(reduction='none')
# Load pretrained model on ImageNet
model = models.densenet121(pretrained=True)
# Change last layer to binary classification
model.classifier = torch.nn.Linear(1024, 1)
# Load pretrained model
if pretrained_model_path is not None:
model.load_state_dict(torch.load(pretrained_model_path))
# Train model
pretrained_model_path = train_combined(model, train_loader, val_loader, **train_config)
# Load trained model
model.load_state_dict(torch.load(pretrained_model_path))
# Test model
test_combined(model, test_loader, **train_config)
if __name__ == '__main__':
pre_path = "./runs/chexpert_pretrained/model.pth"
train_config = {
'batch_size': 60,
'input_size': (224, 224),
'n_epochs': 20,
'orientation': 'frontal',
'optim': torch.optim.Adam,
'weighted_bce': False,
'optim_kwargs': {'lr': 0.001, 'weight_decay': 0.0},
'scheduler': torch.optim.lr_scheduler.ReduceLROnPlateau,
'scheduler_kwargs': {'factor': 0.1, 'patience': 3, 'mode': 'max'},
'early_stopping': 10,
'experiment_name': 'chexpert_4_noweights',
}
chexpert_model(**train_config)
train_config = {
'batch_size': 60,
'input_size': (224, 224),
'n_epochs': 30,
'optim': torch.optim.Adam,
'optim_kwargs': {'lr': 0.0001, 'weight_decay': 0.0},
'scheduler': torch.optim.lr_scheduler.ReduceLROnPlateau,
'scheduler_kwargs': {'factor': 0.1, 'patience': 4, 'mode': 'max'},
'alpha': 0.7,
'early_stopping': 6,
'mode': 'paper',
'criterion': torch.nn.MSELoss(),
'experiment_name': 'brixia_checkpoints',
}
#brixia_model("./runs/chexpert_3_continue_noweights", **train_config)
train_config = {
'batch_size': 60,
'input_size': (224, 224),
'n_epochs': 20,
'orientation': 'frontal',
'optim': torch.optim.Adam,
'optim_kwargs': {'lr': 0.001, 'weight_decay': 0.0},
'scheduler': torch.optim.lr_scheduler.ReduceLROnPlateau,
'scheduler_kwargs': {'factor': 0.1, 'patience': 3, 'mode': 'max'},
'early_stopping': 10,
'experiment_name': 'combined_1',
}
#combined_model(**train_config)