classDataLoader(ABC):"""An abstract class representing a dataset.All custom datasets should inherit.``__len__`` provides the size of the dataset and``__getitem__`` supports integer indexing in range from 0 to len(self)"""def__init__(self, config):""" Constructor:param config: data loader specific config"""self.config = config ifisinstance(config, Dict)else Dict(config)@abstractmethoddef__getitem__(self, index):pass@abstractmethoddef__len__(self):pass
classAccuracy(Metric):def__init__(self):super().__init__()self._name ="accuracy"self._matches =[]self.intersection =0.0self.union =0.0@propertydefvalue(self):"""Returns accuracy metric value for the last model output."""# print(self._matches[-1])return{self._name: self._matches[-1]}@propertydefavg_value(self):"""Returns accuracy metric value for all model outputs. Results per image are stored inself._matches, where True means a correct prediction and False a wrong prediction.Accuracy is computed as the number of correct predictions divided by the totalnumber of predictions."""miou =1.0* self.intersection / self.unionprint('miou', miou)return{self._name: miou}defupdate(self, output, target):"""Updates prediction matches.:param output: model output:param target: annotations"""predict = output[1]predict = predict[0]>0.5target = target[0]>0.5intersection = np.sum((predict)&(target))self.intersection += np.sum((predict)&(target))self.union += np.sum(predict)+ np.sum(target)- intersectionself._matches.append([self.intersection/(self.union+0.00001)])defreset(self):"""Resets the Accuracy metric. This is a required method that should initialize allattributes to their initial value."""self.intersection =0self.union =0self._matches =[]defget_attributes(self):"""Returns a dictionary of metric attributes {metric_name: {attribute_name: value}}.Required attributes: 'direction': 'higher-better' or 'higher-worse''type': metric type"""return{self._name:{"direction":"higher-better","type":"accuracy"}}