在数字化转型中,选择合适的跨平台开发框架不仅能提高效率,还有助于确保数据安全与合规性。
821
2022-08-31
python设计模式之适配器模式(Adapter Pattern)
适配器模式(Adapter Pattern):将一个类的接口转换成为客户希望的另外一个接口.Adapter Pattern使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.
这里performanceLogger 需要根据不同的需求调用ClassificationEvaluator,SegmentationEvaluator,VOCEvaluator或者COCOEvaluator类的实现
其实也可以使用工厂模式来实现。
代码来自:= MetricLogger(self.dictionary, self.cfg)
class SegmentationEvaluator(object): def __init__(self, dictionary): self.dictionary = dictionary self.num_class = len(self.dictionary) self.confusion_matrix = np.zeros((self.num_class,)*2) def Pixel_Accuracy(self): Acc = np.diag(self.confusion_matrix).sum() / self.confusion_matrix.sum() return Acc def Mean_Pixel_Accuracy(self): Acc = np.diag(self.confusion_matrix) / self.confusion_matrix.sum(axis=1) Acc = np.nanmean(Acc) return Acc def Mean_Intersection_over_Union(self): MIoU = np.diag(self.confusion_matrix) / ( np.sum(self.confusion_matrix, axis=1) + np.sum(self.confusion_matrix, axis=0) - np.diag(self.confusion_matrix)) MIoU = np.nanmean(MIoU) return MIoU def Frequency_Weighted_Intersection_over_Union(self): freq = np.sum(self.confusion_matrix, axis=1) / np.sum(self.confusion_matrix) iu = np.diag(self.confusion_matrix) / ( np.sum(self.confusion_matrix, axis=1) + np.sum(self.confusion_matrix, axis=0) - np.diag(self.confusion_matrix)) FWIoU = (freq[freq > 0] * iu[freq > 0]).sum() return FWIoU def _generate_matrix(self, gt_image, pre_image): mask = (gt_image >= 0) & (gt_image < self.num_class) label = self.num_class * gt_image[mask].astype('int') + pre_image[mask] count = np.bincount(label, minlength=self.num_class**2) confusion_matrix = count.reshape(self.num_class, self.num_class) return confusion_matrix def get(self): performances = {} performances['Acc'] = self.Pixel_Accuracy() performances['mAcc'] = self.Mean_Pixel_Accuracy() performances['mIoU'] = self.Mean_Intersection_over_Union() performances['FWIoU'] = self.Frequency_Weighted_Intersection_over_Union() performances['performance'] = performances['mIoU'] return performances def add_batch(self, gt_image, pre_image): assert gt_image.shape == pre_image.shape gt_image = gt_image.data.cpu().numpy() pre_image = pre_image.data.cpu().numpy() self.confusion_matrix += self._generate_matrix(gt_image, pre_image) def reset(self): self.confusion_matrix = np.zeros((self.num_class,) * 2)
class MetricLogger(object): 'Using adapter design patterns' def __init__(self, dictionary, cfg): self.dictionary = dictionary self.cfg = cfg self.type = self.cfg.EVAL_FUNC if self.type == 'segmentation': self._evaluator = SegmentationEvaluator(self.dictionary) elif self.type == 'voc_detection': self._evaluator = VOCEvaluator(self.dictionary) elif self.type == 'coco_detection': self._evaluator = COCOEvaluator(self.dictionary) elif self.type == 'classification': self._evaluator = ClassificationEvaluator(self.dictionary) else: raise NotImplementedError def update(self, *kwargs): self._evaluator.add_batch(*kwargs) def get(self): return self._evaluator.get()
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~