def evaluate(self, data_loaders=None, logger=None, gen_images=False, score_acc=None): assert isinstance(score_acc, ScoreAccumulator) for loader in data_loaders: img_obj = loader.dataset.image_objects[0] x, y = img_obj.working_arr.shape[0], img_obj.working_arr.shape[1] predicted_img = torch.FloatTensor(x, y).fill_(0).to(self.device) for i, data in enumerate(loader, 1): inputs, labels = data['inputs'].to( self.device).float(), data['labels'].to( self.device).float() clip_ix = data['clip_ix'].to(self.device).int() outputs = F.softmax(self.model(inputs), 1) _, predicted = torch.max(outputs, 1) predicted_map = outputs[:, 1, :, :] for j in range(predicted_map.shape[0]): p, q, r, s = clip_ix[j] predicted_img[p:q, r:s] = predicted[j] print('Batch: ', i, end='\r') img_score = ScoreAccumulator() if gen_images: #### Test mode predicted_img = predicted_img.cpu().numpy() * 255 predicted_img[img_obj.extra['fill_in'] == 1] = 255 img_score.reset().add_array(predicted_img, img_obj.ground_truth) ### Only save scores for test images############################ self.conf['acc'].accumulate(img_score) # Global score prf1a = img_score.get_prfa() print(img_obj.file_name, ' PRF1A', prf1a) self.flush( logger, ','.join(str(x) for x in [img_obj.file_name] + prf1a)) ################################################################# IMG.fromarray(np.array(predicted_img, dtype=np.uint8)).save( os.path.join(self.log_dir, img_obj.file_name.split('.')[0] + '.png')) else: #### Validation mode img_score.reset().add_tensor( predicted_img, torch.FloatTensor(img_obj.extra['gt_mid']).to(self.device)) score_acc.accumulate(img_score) prf1a = img_score.get_prfa() print(img_obj.file_name, ' PRF1A', prf1a) self.flush( logger, ','.join(str(x) for x in [img_obj.file_name] + prf1a))
def epoch_ce_loss(self, **kw): """ One epoch implementation of binary cross-entropy loss :param kw: :return: """ running_loss = 0.0 score_acc = ScoreAccumulator() if self.model.training else kw.get('score_acc') assert isinstance(score_acc, ScoreAccumulator) for i, data in enumerate(kw['data_loader'], 1): inputs, labels = data['inputs'].to(self.device).float(), data['labels'].to(self.device).long() if self.model.training: self.optimizer.zero_grad() outputs = F.log_softmax(self.model(inputs), 1) _, predicted = torch.max(outputs, 1) loss = F.nll_loss(outputs, labels, weight=torch.FloatTensor(self.dparm(self.conf)).to(self.device)) if self.model.training: loss.backward() self.optimizer.step() current_loss = loss.item() running_loss += current_loss if self.model.training: score_acc.reset() p, r, f1, a = score_acc.add_tensor(predicted, labels).get_prfa() if i % self.log_frequency == 0: print('Epochs[%d/%d] Batch[%d/%d] loss:%.5f pre:%.3f rec:%.3f f1:%.3f acc:%.3f' % ( kw['epoch'], self.epochs, i, kw['data_loader'].__len__(), running_loss / self.log_frequency, p, r, f1, a)) running_loss = 0.0 self.flush(kw['logger'], ','.join(str(x) for x in [0, kw['epoch'], i, p, r, f1, a, current_loss]))
def epoch_dice_loss(self, **kw): score_acc = ScoreAccumulator() if self.model.training else kw.get('score_acc') assert isinstance(score_acc, ScoreAccumulator) running_loss = 0.0 for i, data in enumerate(kw['data_loader'], 1): inputs, labels = data['inputs'].to(self.device).float(), data['labels'].to(self.device).long() if self.model.training: self.optimizer.zero_grad() outputs = F.softmax(self.model(inputs), 1) _, predicted = torch.max(outputs, 1) loss = dice_loss(outputs[:, 1, :, :], labels, beta=rd.choice(np.arange(1, 2, 0.1).tolist())) if self.model.training: loss.backward() self.optimizer.step() current_loss = loss.item() running_loss += current_loss if self.model.training: score_acc.reset() p, r, f1, a = score_acc.add_tensor(predicted, labels).get_prfa() if i % self.log_frequency == 0: print('Epochs[%d/%d] Batch[%d/%d] loss:%.5f pre:%.3f rec:%.3f f1:%.3f acc:%.3f' % ( kw['epoch'], self.epochs, i, kw['data_loader'].__len__(), running_loss / self.log_frequency, p, r, f1, a)) running_loss = 0.0 self.flush(kw['logger'], ','.join(str(x) for x in [0, kw['epoch'], i, p, r, f1, a, current_loss]))
def epoch_dice_loss(self, **kw): score_acc = ScoreAccumulator() running_loss = 0.0 for i, data in enumerate(kw['data_loader'], 1): inputs, labels = data['inputs'].to( self.device).float(), data['labels'].to(self.device).long() # weights = data['weights'].to(self.device) self.optimizer.zero_grad() outputs = self.model(inputs) _, predicted = torch.max(outputs, 1) # Balancing imbalanced class as per computed weights from the dataset # w = torch.FloatTensor(2).random_(1, 100).to(self.device) # wd = torch.FloatTensor(*labels.shape).uniform_(0.1, 2).to(self.device) loss = dice_loss(outputs[:, 1, :, :], labels, beta=rd.choice(np.arange(1, 2, 0.1).tolist())) loss.backward() self.optimizer.step() current_loss = loss.item() running_loss += current_loss p, r, f1, a = score_acc.reset().add_tensor(predicted, labels).get_prfa() if i % self.log_frequency == 0: print( 'Epochs[%d/%d] Batch[%d/%d] loss:%.5f pre:%.3f rec:%.3f f1:%.3f acc:%.3f' % (kw['epoch'], self.epochs, i, kw['data_loader'].__len__(), running_loss / self.log_frequency, p, r, f1, a)) running_loss = 0.0 self.flush( self.train_logger, ','.join( str(x) for x in [0, kw['epoch'], i, p, r, f1, a, current_loss]))