Skip to content

Commit

Permalink
DeepLabV3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
qjadud1994 committed Jun 10, 2021
1 parent e333d3d commit 4dd1d64
Show file tree
Hide file tree
Showing 33 changed files with 14,009 additions and 10 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,15 @@ dmypy.json

checkpoints/
logs/
DeepLab-V2-PyTorch/data/
DeepLab-V2-PyTorch/__pycache__/
DeepLab-V2-PyTorch/libs/__pycache__/


DeepLabV3Plus-Pytorch/checkpoints
DeepLabV3Plus-Pytorch/__pycache__
DeepLabV3Plus-Pytorch/datasets/__pycache__
DeepLabV3Plus-Pytorch/metrics/__pycache__
DeepLabV3Plus-Pytorch/network/__pycache__
DeepLabV3Plus-Pytorch/utils/__pycache__

4 changes: 2 additions & 2 deletions DeepLab-V2-PyTorch/eval.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
DATASET=voc12
LOG_DIR=Deeplabv2_pseudo_segmentation_labels_2gpu

CUDA_VISIBLE_DEVICES=0 python main.py test \
CUDA_VISIBLE_DEVICES=1 python main.py test \
-c configs/${DATASET}.yaml \
-m data/models/${LOG_DIR}/deeplabv2_resnet101_msc/*/checkpoint_final.pth \
--log_dir=${LOG_DIR}

# evaluate the model with CRF post-processing
CUDA_VISIBLE_DEVICES=0 python main.py crf \
CUDA_VISIBLE_DEVICES=1 python main.py crf \
-c configs/${DATASET}.yaml \
--log_dir=${LOG_DIR}
101 changes: 101 additions & 0 deletions DeepLab-V2-PyTorch/libs/utils/stream_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import numpy as np
from sklearn.metrics import confusion_matrix

class _StreamMetrics(object):
def __init__(self):
""" Overridden by subclasses """
raise NotImplementedError()

def update(self, gt, pred):
""" Overridden by subclasses """
raise NotImplementedError()

def get_results(self):
""" Overridden by subclasses """
raise NotImplementedError()

def to_str(self, metrics):
""" Overridden by subclasses """
raise NotImplementedError()

def reset(self):
""" Overridden by subclasses """
raise NotImplementedError()

class StreamSegMetrics(_StreamMetrics):
"""
Stream Metrics for Semantic Segmentation Task
"""
def __init__(self, n_classes):
self.n_classes = n_classes
self.confusion_matrix = np.zeros((n_classes, n_classes))

def update(self, label_trues, label_preds):
for lt, lp in zip(label_trues, label_preds):
self.confusion_matrix += self._fast_hist( lt.flatten(), lp.flatten() )

@staticmethod
def to_str(results):
string = "\n"
for k, v in results.items():
if k!="Class IoU":
string += "%s: %f\n"%(k, v)

#string+='Class IoU:\n'
#for k, v in results['Class IoU'].items():
# string += "\tclass %d: %f\n"%(k, v)
return string

def _fast_hist(self, label_true, label_pred):
mask = (label_true >= 0) & (label_true < self.n_classes)
hist = np.bincount(
self.n_classes * label_true[mask].astype(int) + label_pred[mask],
minlength=self.n_classes ** 2,
).reshape(self.n_classes, self.n_classes)
return hist

def get_results(self):
"""Returns accuracy score evaluation result.
- overall accuracy
- mean accuracy
- mean IU
- fwavacc
"""
hist = self.confusion_matrix
acc = np.diag(hist).sum() / hist.sum()
acc_cls = np.diag(hist) / hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
cls_iu = dict(zip(range(self.n_classes), iu))

return {
"Overall Acc": acc,
"Mean Acc": acc_cls,
"FreqW Acc": fwavacc,
"Mean IoU": mean_iu,
"Class IoU": cls_iu,
}

def reset(self):
self.confusion_matrix = np.zeros((self.n_classes, self.n_classes))

class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()

def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0

def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count

Loading

0 comments on commit 4dd1d64

Please sign in to comment.