| 29 | |
| 30 | |
| 31 | class TrainEngine(object): |
| 32 | def __init__(self, local_rank, world_size=0, DDP=False, SyncBatchNorm=False): |
| 33 | # init setting |
| 34 | self.local_rank = local_rank |
| 35 | self.world_size = world_size |
| 36 | self.device_ = f'cuda:{local_rank}' |
| 37 | # create tool |
| 38 | self.cls_meter_ = MultilabelClassificationMetric() |
| 39 | self.loss_meter_ = MultiClassificationMetric() |
| 40 | self.top1_meter_ = MultiClassificationMetric() |
| 41 | self.DDP = DDP |
| 42 | self.SyncBN = SyncBatchNorm |
| 43 | |
| 44 | def create_env(self, cfg): |
| 45 | # create network |
| 46 | self.netloc_ = load_model(cfg.network.name, cfg.network.class_num, self.SyncBN) |
| 47 | print(self.netloc_) |
| 48 | |
| 49 | self.netloc_.cuda() |
| 50 | if self.DDP: |
| 51 | if self.SyncBN: |
| 52 | self.netloc_ = torch.nn.SyncBatchNorm.convert_sync_batchnorm(self.netloc_) |
| 53 | self.netloc_ = DDP(self.netloc_, |
| 54 | device_ids=[self.local_rank], |
| 55 | broadcast_buffers=True, |
| 56 | ) |
| 57 | |
| 58 | # create loss function |
| 59 | self.criterion_ = nn.CrossEntropyLoss().cuda() |
| 60 | |
| 61 | # create optimizer |
| 62 | self.optimizer_ = torch.optim.AdamW(self.netloc_.parameters(), lr=cfg.optimizer.lr, |
| 63 | betas=(cfg.optimizer.beta1, cfg.optimizer.beta2), eps=cfg.optimizer.eps, |
| 64 | weight_decay=cfg.optimizer.weight_decay) |
| 65 | |
| 66 | # create scheduler |
| 67 | self.scheduler_ = torch.optim.lr_scheduler.CosineAnnealingLR(self.optimizer_, cfg.train.epoch_num, |
| 68 | eta_min=cfg.scheduler.min_lr) |
| 69 | |
| 70 | def train_multi_class(self, train_loader, epoch_idx, ema_start): |
| 71 | starttime = datetime.datetime.now() |
| 72 | # switch to train mode |
| 73 | self.netloc_.train() |
| 74 | self.loss_meter_.reset() |
| 75 | self.top1_meter_.reset() |
| 76 | # train |
| 77 | train_loader = tqdm(train_loader, desc='train', ascii=True) |
| 78 | for imgs_idx, (imgs_tensor, imgs_label, _, _) in enumerate(train_loader): |
| 79 | # set cuda |
| 80 | imgs_tensor = imgs_tensor.cuda() # [256, 3, 224, 224] |
| 81 | imgs_label = imgs_label.cuda() |
| 82 | # clear gradients(zero the parameter gradients) |
| 83 | self.optimizer_.zero_grad() |
| 84 | # calc forward |
| 85 | preds = self.netloc_(imgs_tensor) |
| 86 | # calc acc & loss |
| 87 | loss = self.criterion_(preds, imgs_label) |
| 88 |
no outgoing calls
no test coverage detected