-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtrain.py
executable file
·227 lines (190 loc) · 8.35 KB
/
train.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
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
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm
from PIL import Image
import h5py
import torch
from torch.utils import data
from torch import nn
import torch.optim as optim
from torchvision.transforms import Compose, Normalize, Resize, InterpolationMode
import sys
sys.path.append('../..')
import clip
from model import CLIP
from simple_tokenizer import SimpleTokenizer
class CXRDataset(data.Dataset):
"""Represents an abstract HDF5 dataset.
Input params:
file_path: Path to the folder containing the dataset (one or multiple HDF5 files).
recursive: If True, searches for h5 files in subdirectories.
load_data: If True, loads all the data immediately into RAM. Use this if
the dataset is fits into memory. Otherwise, leave this at false and
the data will load lazily.
data_cache_size: Number of HDF5 files that can be cached in the cache (default=3).
transform: PyTorch transform to apply to every data instance (default=None).
"""
def __init__(self, img_path, txt_path, column='report', size=None, transform=None):
super().__init__()
if size != None:
self.img_dset = h5py.File(img_path, 'r')['cxr'][:size]
self.txt_dset = pd.read_csv(txt_path)[column][:size]
else:
self.img_dset = h5py.File(img_path, 'r')['cxr']
self.txt_dset = pd.read_csv(txt_path)[column]
self.transform = transform
def __len__(self):
return len(self.txt_dset)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img = self.img_dset[idx] # np array, (320, 320)
img = np.expand_dims(img, axis=0)
img = np.repeat(img, 3, axis=0)
txt = self.txt_dset[idx] # python str
if type(txt) == type(float("nan")): # capture the case of empty "Impression" sections
txt = " "
img = torch.from_numpy(img) # torch, (3, 320, 320)
if self.transform:
img = self.transform(img)
sample = {'img': img, 'txt': txt }
return sample
def load_data(cxr_filepath, txt_filepath, batch_size=4, column='report', pretrained=False, verbose=False):
if torch.cuda.is_available():
dev = "cuda:0"
cuda_available = True
print('Using CUDA.')
else:
dev = "cpu"
cuda_available = False
print('Using cpu.')
device = torch.device(dev)
if cuda_available:
torch.cuda.set_device(device)
if pretrained:
input_resolution = 224
transform = Compose([
Normalize((101.48761, 101.48761, 101.48761), (83.43944, 83.43944, 83.43944)),
Resize(input_resolution, interpolation=InterpolationMode.BICUBIC),
])
print('Interpolation Mode: ', InterpolationMode.BICUBIC)
print("Finished image transforms for pretrained model.")
else:
input_resolution = 320
transform = Compose([
Normalize((101.48761, 101.48761, 101.48761), (83.43944, 83.43944, 83.43944)),
])
print("Finished image transforms for clip model.")
torch_dset = CXRDataset(img_path=cxr_filepath,
txt_path=txt_filepath, column=column, transform=transform)
if verbose:
for i in range(len(torch_dset)):
sample = torch_dset[i]
plt.imshow(sample['img'][0])
plt.show()
print(i, sample['img'].size(), sample['txt'])
if i == 3:
break
loader_params = {'batch_size':batch_size, 'shuffle': True, 'num_workers': 0}
data_loader = data.DataLoader(torch_dset, **loader_params)
return data_loader, device
def load_clip(model_path=None, pretrained=False, context_length=77):
'''
FUNCTION: load_clip
-------------------------------
This function loads in a model with the CLIP model
architecture.
args:
* model_path (optional) - path to model weights that the model
will be initialized with
* pretrained (optional) - if True, will load the pretrained
CLIP model
* context_length (optional) - length of the maximum number of
tokens that can be inputted into the CLIP model
'''
params = {
'embed_dim':768,
'image_resolution': 320,
'vision_layers': 12,
'vision_width': 768,
'vision_patch_size': 16,
'context_length': context_length,
'vocab_size': 49408,
'transformer_width': 512,
'transformer_heads': 8,
'transformer_layers': 12
}
# set device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if pretrained:
# load clip pre-trained model
model, preprocess = clip.load("ViT-B/32", device=device, jit=False)
print("Loaded in pretrained model.")
else:
model = CLIP(**params)
print("Loaded in clip model.")
# if a model_path is provided, load in weights to backbone
if model_path != None:
model.load_state_dict(torch.load(model_path, map_location=device))
return model
def preprocess_text(texts, model):
# if model.context_length is None:
# model = model.module
_tokenizer = SimpleTokenizer()
sot_token = _tokenizer.encoder["<|startoftext|>"]
eot_token = _tokenizer.encoder["<|endoftext|>"]
all_tokens = [[sot_token] + _tokenizer.encode(text) + [eot_token] for text in texts]
result = torch.zeros(len(all_tokens), model.context_length, dtype=torch.long)
for i, tokens in enumerate(all_tokens):
if len(tokens) > model.context_length:
tokens = tokens[:model.context_length]
tokens[model.context_length - 1] = eot_token
result[i, :len(tokens)] = torch.tensor(tokens)
return result
def make(config, cxr_filepath, txt_filepath, model_path=None):
'''
FUNCTION: make
---------------------------------
This function makes the model, the data loader, loss and optimizer.
args:
* config - dict, configuration of experiment
* cxr_filepath - string, filepath to chest x-ray images
* txt_filepath - string, filepath to corresponding text reports
* model_path - string, filepath to previously trained model
'''
data_loader, device = load_data(cxr_filepath, txt_filepath, batch_size=config.batch_size, pretrained=config.pretrained, column=config.column)
model = load_clip(model_path=model_path, pretrained=config.pretrained, context_length=config.context_length)
model.to(device)
print('Model on Device.')
# make the optimizer
criterion = nn.CrossEntropyLoss().cuda()
# todo: incorporate - torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0, T_mult=1, eta_min=0, last_epoch=-1, verbose=False)
optimizer = optim.AdamW(model.parameters(), lr=config.lr)
return model, data_loader, device, criterion, optimizer
def train_main(cxr_filepath, txt_filepath, hyperparams, output_path, model_path=None, pretrained=False):
'''
args:
* cxr_filpath- str filepath to cxr images
* txt_filepath- str filepath to text reports
* hyperparams- dictionary with the following hyperparams:
`batch_size`, `criterion`, `learning_rate`, `momentum`, `epochs`
* output_path- str filepath to where the trained model will be saved
* model_path- str filepath to model that will be used as baseline model for training.
If not provided, a model will be trained from scratch
* pretrained- whether or not the clip model was pretrained with generic images
This function is the main train function for CXR-CLIP.
'''
# unpack `hyperparams`
batch_size = hyperparams['batch_size']
criterion = hyperparams['criterion']
learning_rate = hyperparams['learning_rate']
momentum = hyperparams['momentum']
epochs = hyperparams['epochs']
# load input cxr + report data
data_loader, device = load_data(cxr_filepath, txt_filepath, batch_size=batch_size, pretrained=pretrained)
model = load_clip(model_path=model_path, pretrained=pretrained)
optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum)
train_clip(model, data_loader, device, criterion, optimizer, epochs, output_path)
return model