File size: 9,126 Bytes
797d116 |
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
import os
from typing import Optional
import numpy as np
import math
from PIL import Image
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision.transforms import (
CenterCrop,
Compose,
Normalize,
RandomHorizontalFlip,
RandomResizedCrop,
RandomRotation,
RandomAffine,
Resize,
ToTensor)
# from transformers import ViTImageProcessor
# from transformers import ViTForImageClassification
from transformers import AdamW
from transformers import AutoImageProcessor, ResNetForImageClassification
import lightning as L
from data import RetinopathyDataset, Split
from metrics import Metrics
def worker_init_fn(worker_id: int) -> None:
""" Initialize workers in a way that they draw different
random samples and do not repeat identical pseudorandom
sequences of each other, which may be the case with Fork
multiprocessing.
Args:
worker_id (int): id of a preprocessing worker process launched
by one DDP training process.
"""
state = np.random.get_state()
assert isinstance(state, tuple)
assert isinstance(state[1], np.ndarray)
seed_arr = state[1]
seed_np = seed_arr[0] + worker_id
np.random.seed(seed_np)
seed_pt = seed_np + 1111
torch.manual_seed(seed_pt)
print(f"Setting numpy seed to {seed_np} and pytorch seed to {seed_pt} in worker {worker_id}")
class ViTLightningModule(L.LightningModule):
""" Lightning Module that implements neural network training hooks. """
def __init__(self, debug: bool) -> None:
super().__init__()
self.save_hyperparameters()
np.random.seed(53)
# pretrained_name = 'google/vit-base-patch16-224-in21k'
# pretrained_name = 'google/vit-base-patch16-384-in21k'
# pretrained_name = "microsoft/resnet-50"
pretrained_name = "microsoft/resnet-34"
# processor = ViTImageProcessor.from_pretrained(pretrained_name)
processor = AutoImageProcessor.from_pretrained(pretrained_name)
image_mean = processor.image_mean # type: ignore
image_std = processor.image_std # type: ignore
# size = processor.size["height"] # type: ignore
# size = processor.size["shortest_edge"] # type: ignore
size = 896 # 448
normalize = Normalize(mean=image_mean, std=image_std)
train_transforms = Compose(
[
# RandomRotation((-180, 180)),
RandomAffine((-180, 180), shear=10),
RandomResizedCrop(size, scale=(0.5, 1.0)),
RandomHorizontalFlip(),
ToTensor(),
normalize,
]
)
val_transforms = Compose(
[
Resize(size),
CenterCrop(size),
ToTensor(),
normalize,
]
)
self.dataset = RetinopathyDataset("retinopathy_data")
# print_data_stats(self.dataset, "all_data")
train_data, val_data = Split.make_splits(
self.dataset,
train_transforms=(train_transforms, torch.tensor),
val_transforms=(val_transforms, torch.tensor),
train_fraction=0.9,
stratify_train=True,
stratify_val=True,
)
assert len(set(train_data.indices).intersection(set(val_data.indices))) == 0
label2id = {label: id for id, label in self.dataset.label_map.items()}
num_classes = len(self.dataset.label_map)
labelmap = self.dataset.label_map
assert len(labelmap) == num_classes
assert set(labelmap.keys()) == set(range(num_classes))
train_batch_size = 4 if debug else 20
val_batch_size = 4 if debug else 20
num_gpus = torch.cuda.device_count()
print(f"{num_gpus=}")
num_cores = torch.get_num_threads()
print(f"{num_cores=}")
num_threads_per_gpu = max(1, int(math.ceil(num_cores / num_gpus))) \
if num_gpus > 0 else 1
num_workers = 1 if debug else num_threads_per_gpu
print(f"{num_workers=}")
self._train_dataloader = DataLoader(
train_data,
shuffle=True,
num_workers=num_workers,
persistent_workers=num_workers > 0,
pin_memory=True,
batch_size=train_batch_size,
worker_init_fn=worker_init_fn,
)
self._val_dataloader = DataLoader(
val_data,
shuffle=False,
num_workers=num_workers,
persistent_workers=num_workers > 0,
pin_memory=True,
batch_size=val_batch_size,
)
# print_data_stats(self._val_dataloader, "val")
# print_data_stats(self._train_dataloader, "train")
img_batch, label_batch = next(iter(self._train_dataloader))
assert isinstance(img_batch, torch.Tensor)
assert isinstance(label_batch, torch.Tensor)
print(f"{img_batch.shape=} {label_batch.shape=}")
assert img_batch.shape == (train_batch_size, 3, size, size)
assert label_batch.shape == (train_batch_size,)
self.example_input_array = torch.randn_like(img_batch)
# self._model = ViTForImageClassification.from_pretrained(
# pretrained_name,
# num_labels=len(self.dataset.label_map),
# id2label=self.dataset.label_map,
# label2id=label2id)
self._model = ResNetForImageClassification.from_pretrained(
pretrained_name,
num_labels=len(self.dataset.label_map),
id2label=self.dataset.label_map,
label2id=label2id,
ignore_mismatched_sizes=True)
assert isinstance(self._model, nn.Module)
self.train_metrics: Optional[Metrics] = None
self.val_metrics: Optional[Metrics] = None
@property
def num_classes(self):
return len(self.dataset.label_map)
@property
def labelmap(self):
return self.dataset.label_map
def forward(self, img_batch):
outputs = self._model(img_batch) # type: ignore
return outputs.logits
def common_step(self, batch, batch_idx):
img_batch, label_batch = batch
logits = self(img_batch)
criterion = nn.CrossEntropyLoss()
loss = criterion(logits, label_batch)
preds_batch = logits.argmax(-1)
return loss, preds_batch, label_batch
def on_train_epoch_start(self) -> None:
self.train_metrics = Metrics(
self.num_classes,
self.labelmap,
"train",
self.log).to(self.device)
def training_step(self, batch, batch_idx):
loss, preds, labels = self.common_step(batch, batch_idx)
assert self.train_metrics is not None
self.train_metrics.update(loss, preds, labels)
if False and batch_idx == 0:
self._dump_train_images()
return loss
def _dump_train_images(self) -> None:
""" Save augmented images to disk for inspection. """
img_batch, label_batch = next(iter(self._train_dataloader))
for i_img, (img, label) in enumerate(zip(img_batch, label_batch)):
img_np = img.cpu().numpy()
denorm_np = (img_np - img_np.min()) / (img_np.max() - img_np.min())
img_uint8 = (255 * denorm_np).astype(np.uint8)
pil_img = Image.fromarray(np.transpose(img_uint8, (1, 2, 0)))
if self.logger is not None and self.logger.log_dir is not None:
assert isinstance(self.logger.log_dir, str)
os.makedirs(self.logger.log_dir, exist_ok=True)
path = os.path.join(self.logger.log_dir,
f"img_{i_img:02d}_{label.item()}.png")
pil_img.save(path)
def on_train_epoch_end(self) -> None:
assert self.train_metrics is not None
self.train_metrics.log()
assert self.logger is not None
if self.logger.log_dir is not None:
path = os.path.join(self.logger.log_dir, "inference")
self.save_checkpoint_dk(path)
def save_checkpoint_dk(self, dirpath: str) -> None:
if self.global_rank == 0:
self._model.save_pretrained(dirpath)
def validation_step(self, batch, batch_idx):
loss, preds, labels = self.common_step(batch, batch_idx)
assert self.val_metrics is not None
self.val_metrics.update(loss, preds, labels)
return loss
def on_validation_epoch_start(self) -> None:
self.val_metrics = Metrics(
self.num_classes,
self.labelmap,
"val",
self.log).to(self.device)
def on_validation_epoch_end(self) -> None:
assert self.val_metrics is not None
self.val_metrics.log()
def configure_optimizers(self):
# No WD is the same as 1e-3 and better than 1e-2
# LR 1e-3 is worse than 1e-4 (without LR scheduler)
return AdamW(self.parameters(),
lr=1e-4,
)
|