Spaces:
Build error
Build error
Weicheng HE commited on
Commit ·
1215771
1
Parent(s): 504b328
initial commit
Browse filesFiles changed (33)
- .gitignoreAdded+2
- README.md+56-13
- abstractions/Box.pyAdded+104
- abstractions/__init__.pyAdded+1
- base_cam.pyAdded+223
- detectron2monitor.pyAdded+231
- enlarge.pyAdded+320
- interface.pyAdded+95
- interface_tabbed.pyAdded+155
- models/configs/Base-RCNN-FPN.yamlAdded+50
- models/configs/vanilla_regnet.yamlAdded+33
- models/configs/vanilla_resnet.yamlAdded+28
- models/metadata.pyAdded+35
- models/regnet.pyAdded+84
- models/regnet_model.pyAdded+446
- models/weights/model_final_resnet_kitti.pthAdded+3
- monitors/kitti/resnet/kmeans/1.pklAdded+3
- monitors/kitti/resnet/kmeans/4.pklAdded+3
- monitors/kitti/resnet/kmeans/5.pklAdded+3
- monitors/kitti/resnet/kmeans/6.pklAdded+3
- requirements.txtAdded+12
- runtime_monitors/Monitor.pyAdded+56
- runtime_monitors/__init__.pyAdded+2
- train_feats/kitti/resnet/kitti-train_feats_tp_dict.pickleAdded+3
- util/Clustering.pyAdded+138
- util/Monitor_construction.pyAdded+144
- util/__init__.pyAdded+3
- utils_clustering.pyAdded+61
- val_feats/kitti/resnet/ID-bdd-OOD-coco_feats_fp_dict.pickleAdded+3
- val_feats/kitti/resnet/OOD-open_feats_fp_dict.pickleAdded+3
- val_feats/kitti/resnet/kitti-val_feats_fp_dict.pickleAdded+3
- val_feats/kitti/resnet/kitti-val_feats_tp_dict.pickleAdded+3
- val_feats/kitti/resnet/voc-ood_feats_fp_dict.pickleAdded+3
12.DS_Store*.pyc12345678910111213---title: MonitoringInterfaceemoji: 🐢colorFrom: yellowcolorTo: redsdk: gradiosdk_version: 4.1.1app_file: app.pypinned: falselicense: mit---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556# monitoring-interface## Requirements```pip install -r requiremnets.txt```To install Detectron2, please follow [here](https://detectron2.readthedocs.io/tutorials/install.html).## Dataset PreparationWe use [Fiftyone](https://docs.voxel51.com) library to load and visualize datasets.BDD100k, COCO, KITTI and OpenImage can be loaded directly through [Fiftyone Datasets Zoo](https://docs.voxel51.com/user_guide/dataset_zoo/datasets.html?highlight=zoo).For other datasets, such as NuScene can be loaded manually via the following simple pattern:```pythonimport fiftyone as fo# A name for the datasetname = "my-dataset"# The directory containing the dataset to importdataset_dir = "/path/to/dataset"# The type of the dataset being importeddataset_type = fo.types.COCODetectionDataset # for exampledataset = fo.Dataset.from_dir(dataset_dir=dataset_dir,dataset_type=dataset_type,name=name,)```The custom dataset folder should have the following structure:```└── /path/to/dataset|├── Data└── labels.json```Notice that the annotation file `labels.json` should be prepared in COCO format.## Interface demoThree interfaces are provided:- `interface.py`: all-in-1 interface- `interface_tabbed.py`: tabbed interface- `enlarge.py`: interface for monitor interval enlargementTo run any of these interfaces, just execute `python <script name.py>`.Please note that feature extraction for both training data and evaluation data can be a time-consuming process. However, if you are only interested in testing monitor construction, monitor evaluation, or monitoring demo, you can use the following settings to load a pretrained model along with the corresponding extracted features and monitors.| ID | Backbone | Clustering method for Monitors || ----- | -------- | -------------------------------- || KITTI | ResNet | KMeans(nb_clusters=[1, 4, 5, 6]) |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104from copy import deepcopy
class Box: def __init__(self): self.dimensions = None self.ivals = [] self.element_indexes = [] # record this box is built for what samples self.low_bound_indexes = dict() # record which samples visit the low bound for each dimension self.high_bound_indexes = dict() # record which samples visit the low bound for each dimension
def build(self, dimensions, points): # a point is a tuple (index, n-dim numpy) # index = point[0] # value = point[1] piter = iter(points) self.dimensions = dimensions self.ivals = [] self.element_indexes = [] self.low_bound_indexes = dict() self.high_bound_indexes = dict()
try: point = next(piter) except StopIteration: return else: self.element_indexes.append(point[0]) # update index list i = 0 for coord in point[1]: if(i >= self.dimensions): break self.ivals.append([coord, coord]) self.low_bound_indexes["n"+str(i+1)] = [point[0]] # update low bound visiting index list self.high_bound_indexes["n"+str(i+1)] = [point[0]] # update upper bound visiting index list i += 1 if(len(self.ivals) != self.dimensions): raise "IllegalArgument"
while True: try: point = next(piter) except StopIteration: break else: self.element_indexes.append(point[0]) # update index list i = 0 for coord in point[1]: if(i >= self.dimensions): break ival = self.ivals[i] if(coord < ival[0]): ival[0] = coord self.low_bound_indexes["n"+str(i+1)] = [point[0]] # update the bound and its index elif(coord == ival[0]): low_index_list = self.low_bound_indexes["n"+str(i+1)] low_index_list.append(point[0])
if(coord > ival[1]): ival[1] = coord self.high_bound_indexes["n"+str(i+1)] = [point[0]] # update the bound and its index elif(coord == ival[1]): high_index_list = self.high_bound_indexes["n"+str(i+1)] high_index_list.append(point[0]) i += 1
def query(self, point): i = 0 for coord in point: if(i >= self.dimensions): break ival = self.ivals[i] if(coord < ival[0] or coord > ival[1]): return False i += 1 return True
def __str__(self): return self.ivals.__str__() def query_delta(self, point, delta): i = 0 for coord in point: if(i >= self.dimensions): break ival = self.ivals[i] if(coord < ival[0]*(1+delta) or coord > ival[1]*(1+delta)): return False i += 1 return True
def boxes_query(point, boxes): for box in boxes: if len(box.ivals): if box.query(point): return True return False
def boxes_query_delta(point, boxes, delta): for box in boxes: if len(box.ivals): if box.query_delta(point, delta): return True return False1from .Box import *123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223import numpy as npimport torchimport ttach as ttafrom typing import Callable, List, Tuplefrom pytorch_grad_cam.activations_and_gradients import ActivationsAndGradientsfrom pytorch_grad_cam.utils.svd_on_activations import get_2d_projectionfrom pytorch_grad_cam.utils.image import scale_cam_imagefrom pytorch_grad_cam.utils.model_targets import ClassifierOutputTargetfrom pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
# https://arxiv.org/abs/2008.00299
class BaseCAM: def __init__(self, model: torch.nn.Module, target_layers: List[torch.nn.Module], use_cuda: bool = False, reshape_transform: Callable = None, compute_input_gradient: bool = False, uses_gradients: bool = True) -> None: self.model = model.eval() self.target_layers = target_layers self.cuda = use_cuda if self.cuda: self.model = model.cuda() self.reshape_transform = reshape_transform self.compute_input_gradient = compute_input_gradient self.uses_gradients = uses_gradients self.activations_and_grads = ActivationsAndGradients( self.model, target_layers, reshape_transform)
""" Get a vector of weights for every channel in the target layer. Methods that return weights channels, will typically need to only implement this function. """
def get_cam_weights(self, input_tensor: torch.Tensor, target_layers: List[torch.nn.Module], targets: List[torch.nn.Module], activations: torch.Tensor, grads: torch.Tensor) -> np.ndarray: raise Exception("Not Implemented")
def get_cam_image(self, input_tensor: torch.Tensor, target_layer: torch.nn.Module, targets: List[torch.nn.Module], activations: torch.Tensor, grads: torch.Tensor, eigen_smooth: bool = False) -> np.ndarray:
weights = self.get_cam_weights(input_tensor, target_layer, targets, activations, grads) weighted_activations = weights[:, :, None, None] * activations if eigen_smooth: cam = get_2d_projection(weighted_activations) else: cam = weighted_activations.sum(axis=1) return cam
def forward(self, input_tensor: torch.Tensor, targets: List[torch.nn.Module], eigen_smooth: bool = False) -> np.ndarray: if self.cuda: input_tensor = input_tensor.cuda()
if self.compute_input_gradient: input_tensor = torch.autograd.Variable(input_tensor, requires_grad=True)
outputs = self.activations_and_grads(input_tensor) if targets is None: target_categories = np.argmax(outputs.cpu().data.numpy(), axis=-1) targets = [ClassifierOutputTarget( category) for category in target_categories]
if self.uses_gradients: self.model.zero_grad() loss = sum([target(output) for target, output in zip(targets, outputs)]) loss.backward(retain_graph=True)
# In most of the saliency attribution papers, the saliency is # computed with a single target layer. # Commonly it is the last convolutional layer. # Here we support passing a list with multiple target layers. # It will compute the saliency image for every image, # and then aggregate them (with a default mean aggregation). # This gives you more flexibility in case you just want to # use all conv layers for example, all Batchnorm layers, # or something else. cam_per_layer = self.compute_cam_per_layer(input_tensor, targets, eigen_smooth) return self.aggregate_multi_layers(cam_per_layer)
def get_target_width_height(self, input_tensor: torch.Tensor) -> Tuple[int, int]: width, height = input_tensor.size(-1), input_tensor.size(-2) return width, height
def compute_cam_per_layer( self, input_tensor: torch.Tensor, targets: List[torch.nn.Module], eigen_smooth: bool) -> np.ndarray: activations_list = [a.cpu().data.numpy() for a in self.activations_and_grads.activations] grads_list = [g.cpu().data.numpy() for g in self.activations_and_grads.gradients] target_size = self.get_target_width_height(input_tensor[0]["image"])
cam_per_target_layer = [] # Loop over the saliency image from every layer for i in range(len(self.target_layers)): target_layer = self.target_layers[i] layer_activations = None layer_grads = None if i < len(activations_list): layer_activations = activations_list[i] if i < len(grads_list): layer_grads = grads_list[i]
cam = self.get_cam_image(input_tensor, target_layer, targets, layer_activations, layer_grads, eigen_smooth) cam = np.maximum(cam, 0) scaled = scale_cam_image(cam, target_size) cam_per_target_layer.append(scaled[:, None, :])
return cam_per_target_layer
def aggregate_multi_layers( self, cam_per_target_layer: np.ndarray) -> np.ndarray: cam_per_target_layer = np.concatenate(cam_per_target_layer, axis=1) cam_per_target_layer = np.maximum(cam_per_target_layer, 0) result = np.mean(cam_per_target_layer, axis=1) return scale_cam_image(result)
def forward_augmentation_smoothing(self, input_tensor: torch.Tensor, targets: List[torch.nn.Module], eigen_smooth: bool = False) -> np.ndarray: transforms = tta.Compose( [ tta.HorizontalFlip(), tta.Multiply(factors=[0.9, 1, 1.1]), ] ) cams = [] for transform in transforms: augmented_tensor = transform.augment_image(input_tensor) cam = self.forward(augmented_tensor, targets, eigen_smooth)
# The ttach library expects a tensor of size BxCxHxW cam = cam[:, None, :, :] cam = torch.from_numpy(cam) cam = transform.deaugment_mask(cam)
# Back to numpy float32, HxW cam = cam.numpy() cam = cam[:, 0, :, :] cams.append(cam)
cam = np.mean(np.float32(cams), axis=0) return cam
def __call__(self, input_tensor: torch.Tensor, targets: List[torch.nn.Module] = None, aug_smooth: bool = False, eigen_smooth: bool = False) -> np.ndarray:
# Smooth the CAM result with test time augmentation if aug_smooth is True: return self.forward_augmentation_smoothing( input_tensor, targets, eigen_smooth)
return self.forward(input_tensor, targets, eigen_smooth)
def __del__(self): self.activations_and_grads.release()
def __enter__(self): return self
def __exit__(self, exc_type, exc_value, exc_tb): self.activations_and_grads.release() if isinstance(exc_value, IndexError): # Handle IndexError here... print( f"An exception occurred in CAM with block: {exc_type}. Message: {exc_value}") return True
class EigenCAM(BaseCAM): def __init__(self, model, target_layers, use_cuda=False, reshape_transform=None): super(EigenCAM, self).__init__(model, target_layers, use_cuda, reshape_transform, uses_gradients=False)
def get_cam_image(self, input_tensor, target_layer, target_category, activations, grads, eigen_smooth): return get_2d_projection(activations)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231import torchfrom detectron2.utils.logger import setup_loggersetup_logger()
from detectron2.config import get_cfgimport detectron2.data.transforms as Tfrom detectron2.checkpoint import DetectionCheckpointerfrom detectron2.modeling import build_modelfrom detectron2.data.detection_utils import read_imagefrom detectron2.utils.visualizer import Visualizerfrom detectron2.data import MetadataCatalog
import numpy as npimport cv2import osimport timeimport pickleimport gradio as grimport tqdmimport matplotlib.pyplot as pltimport iofrom PIL import Imagetorch.manual_seed(0)np.random.seed(0)
torch.backends.cudnn.deterministic = Truetorch.backends.cudnn.benchmark = False
from models.regnet import build_regnet_fpn_backboneimport models.metadata as metadata
from utils_clustering import *
from base_cam import EigenCAMfrom pytorch_grad_cam.utils.model_targets import FasterRCNNBoxScoreTarget
fullName2ab_dict = {'PASCAL-VOC':"voc", 'BDD100K':"bdd", 'KITTI':"kitti", 'Speed signs':"speed", 'NuScenes':"nu"}ab2FullName_dict = {'voc':"PASCAL-VOC", 'bdd':"BDD100K", 'kitti':"KITTI", 'speed':"Speed signs", 'nu':"NuScenes"}class Detectron2Monitor(): def __init__(self, id, backbone, confidence_threshold=0.05): self.id, self.label_list = self._get_label_list(id) self.backbone = backbone self.confidence_threshold = confidence_threshold self.cfg, self.device, self.model = self._get_model() self.label_dict = {i:label for i, label in enumerate(self.label_list)} self.eval_list = ["ID-voc-OOD-coco", "OOD-open", "voc-val"] if self.id == "voc" else ["ID-bdd-OOD-coco", "OOD-open", "voc-ood", f"{self.id}-val"] MetadataCatalog.get("custom_dataset").set(thing_classes=self.label_list) def _get_label_list(self, id): id = fullName2ab_dict[id] if id == 'voc': label_list = metadata.VOC_THING_CLASSES elif id == 'bdd': label_list = metadata.BDD_THING_CLASSES elif id == 'kitti': label_list = metadata.KITTI_THING_CLASSES elif id == 'speed' or id == 'prescan': label_list = metadata.SPEED_THING_CLASSES else: label_list = metadata.NU_THING_CLASSES return id, label_list
def _get_model(self): cfg = get_cfg() cfg.merge_from_file(f"models/configs/vanilla_{self.backbone}.yaml") cfg.MODEL.WEIGHTS = f"models/weights/model_final_{self.backbone}_{self.id}.pth" cfg.MODEL.DEVICE='cpu' cfg.MODEL.ROI_HEADS.NUM_CLASSES = len(self.label_list) cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = self.confidence_threshold model = build_model(cfg) model.eval() checkpointer = DetectionCheckpointer(model) checkpointer.load(cfg.MODEL.WEIGHTS) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) return cfg, device, model
def _inference(self, model, inputs): with torch.no_grad(): images = model.preprocess_image(inputs) features = model.backbone(images.tensor) proposals, _ = model.proposal_generator(images, features, None) # RPN
features_ = [features[f] for f in model.roi_heads.box_in_features] box_features = model.roi_heads.box_pooler(features_, [x.proposal_boxes for x in proposals]) box_features = model.roi_heads.box_head(box_features) # features of all 1k candidates predictions = model.roi_heads.box_predictor(box_features) pred_instances, pred_inds = model.roi_heads.box_predictor.inference(predictions, proposals) pred_instances = model.roi_heads.forward_with_given_boxes(features, pred_instances)
# output boxes, masks, scores, etc pred_instances = model._postprocess(pred_instances, inputs, images.image_sizes) # scale box to orig size # features of the proposed boxes feats = box_features[pred_inds].cpu().numpy() return pred_instances, feats
def _load_monitors(self, clustering_algo, nb_clusters, eps=5, min_samples=10): if clustering_algo == "dbscan": with open(f"monitors/{self.id}/{self.backbone}/{clustering_algo}/eps{eps}_min_samples{min_samples}.pkl", 'rb') as f: monitors_dict = pickle.load(f) else: with open(f"monitors/{self.id}/{self.backbone}/{clustering_algo}/{nb_clusters}.pkl", 'rb') as f: monitors_dict = pickle.load(f) return monitors_dict def _evaluate(self, clustering_algo, nb_clusters, eps, min_samples): dataset_name = f"{self.id}-val" with open(f'val_feats/{self.id}/{self.backbone}/{dataset_name}_feats_tp_dict.pickle', 'rb') as f: feats_tp_dict = pickle.load(f) with open(f'val_feats/{self.id}/{self.backbone}/{dataset_name}_feats_fp_dict.pickle', 'rb') as f: feats_fp_dict = pickle.load(f) monitors_dict = self._load_monitors(clustering_algo, nb_clusters, eps, min_samples) # make verdicts on ID data data_tp = [] data_fp = [] accept_sum = {"tp": 0, "fp": 0} reject_sum = {"tp": 0, "fp": 0} for label in tqdm.tqdm(self.label_list, desc="Evaluation on ID data"): if label in monitors_dict: verdict = monitors_dict[label].make_verdicts(feats_tp_dict[label]) data_tp.append([label, len(verdict), np.sum(verdict)/len(verdict)]) accept_sum["tp"] += np.sum(verdict) reject_sum["tp"] += len(verdict) - np.sum(verdict) verdict = monitors_dict[label].make_verdicts(feats_fp_dict[label]) data_fp.append([label, len(verdict), (len(verdict)-np.sum(verdict))/len(verdict)]) accept_sum["fp"] += np.sum(verdict) reject_sum["fp"] += len(verdict) - np.sum(verdict) TPR = round((accept_sum['tp'] / (reject_sum['tp'] + accept_sum['tp'])*100), 2) FPR = round((accept_sum['fp'] / (reject_sum['fp'] + accept_sum['fp'])*100), 2) id_name = ab2FullName_dict[self.id] df_id = pd.DataFrame([[id_name, f"{TPR}%", f"{FPR}%"]], columns=["Dataset", "TPR", "FPR"]) data_ood = [] i = 0 self.eval_list.remove(dataset_name) for dataset_name in tqdm.tqdm(self.eval_list, desc="Evaluation on OOD data"): accept_sum = {"tp": 0, "fp": 0} reject_sum = {"tp": 0, "fp": 0} with open(f'val_feats/{self.id}/{self.backbone}/{dataset_name}_feats_fp_dict.pickle', 'rb') as f: feats_fp_dict = pickle.load(f) for label in self.label_list: if label in monitors_dict: verdict = monitors_dict[label].make_verdicts(feats_fp_dict[label]) accept_sum["fp"] += np.sum(verdict) reject_sum["fp"] += len(verdict) - np.sum(verdict) FPR = round((accept_sum['fp'] / (reject_sum['fp'] + accept_sum['fp'])*100), 2) data_ood.append([dataset_name, str(FPR)+"%"]) i += 1 # prepare dataframes df_ood = pd.DataFrame(data_ood, columns=["Dataset", "FPR"]) df_ood["Dataset"] = ["COCO", "Open Images"] if self.id == "voc" else ["COCO", "Open Images", "VOC-OOD"] return df_id, df_ood
def _postprocess_cam(self, raw_cam, img_width, img_height): cam_orig = np.sum(raw_cam, axis=0) # [H,W] cam_orig = np.maximum(cam_orig, 0) # ReLU cam_orig -= np.min(cam_orig) cam_orig /= np.max(cam_orig) cam = cv2.resize(cam_orig, (img_width, img_height)) return cam
def _fasterrcnn_reshape_transform(self, x): target_size = x['p6'].size()[-2 : ] activations = [] for key, value in x.items(): activations.append(torch.nn.functional.interpolate(torch.abs(value), target_size, mode='bilinear')) activations = torch.cat(activations, axis=1) return activations def _get_input_dict(self, original_image): height, width = original_image.shape[:2] transform_gen = T.ResizeShortestEdge( [self.cfg.INPUT.MIN_SIZE_TEST, self.cfg.INPUT.MIN_SIZE_TEST], self.cfg.INPUT.MAX_SIZE_TEST ) image = transform_gen.get_transform(original_image).apply_image(original_image) image = torch.as_tensor(image.astype("float32").transpose(2, 0, 1)) inputs = {"image": image, "height": height, "width": width} return inputs def get_output(self, monitors_dict, img): image = read_image(img, format="BGR") input_image_dict = [self._get_input_dict(image)] pred_instances, feats = self._inference(self.model, input_image_dict) detections = pred_instances[0]["instances"].to("cpu") cls_idxs = detections.pred_classes.detach().numpy() # get labels from class indices labels = [self.label_dict[i] for i in cls_idxs] # count values in labels, and return a dictionary labels_count_dict = dict((i, labels.count(i)) for i in labels) v = Visualizer(image[..., ::-1], MetadataCatalog.get("custom_dataset"), scale=1) v = v.draw_instance_predictions(detections) img_detection = v.get_image() df = pd.DataFrame(list(labels_count_dict.items()), columns=['Object', 'Count']) verdicts = [] for label, feat in zip(labels, feats): verdict = monitors_dict[label].make_verdicts(feat[np.newaxis,:])[0] verdicts.append(verdict) detections_ood = detections[[i for i, x in enumerate(verdicts) if not x]] detections_ood.pred_classes = torch.tensor([5]*len(detections_ood.pred_classes)) labels_ood = [label for label, verdict in zip(labels, verdicts) if not verdict] verdicts_ood = ["Rejected"]*len(labels_ood) df_verdict = pd.DataFrame(list(zip(labels_ood, verdicts_ood)), columns=['Object', 'Verdict']) v = Visualizer(image[..., ::-1], MetadataCatalog.get("custom_dataset"), scale=1) for box in detections_ood.pred_boxes.to('cpu'): v.draw_box(box) v.draw_text("OOD", tuple(box[:2].numpy())) v = v.get_output() img_ood = v.get_image() pred_bboxes = detections.pred_boxes.tensor.numpy().astype(np.int32) target_layers = [self.model.backbone] targets = [FasterRCNNBoxScoreTarget(labels=labels, bounding_boxes=pred_bboxes)] cam = EigenCAM(self.model, target_layers, use_cuda=False, reshape_transform=self._fasterrcnn_reshape_transform) grayscale_cam = cam(input_image_dict, targets) cam = self._postprocess_cam(grayscale_cam, input_image_dict[0]["width"], input_image_dict[0]["height"]) plt.rcParams["figure.figsize"] = (30,10) plt.imshow(img_detection[..., ::-1], interpolation='none') plt.imshow(cam, cmap='jet', alpha=0.5) plt.axis("off") img_buff = io.BytesIO() plt.savefig(img_buff, format='png', bbox_inches='tight', pad_inches=0) img_cam = Image.open(img_buff) image_dict = {} image_dict["image"] = image image_dict["cam"] = img_cam image_dict["detection"] = img_detection image_dict["verdict"] = img_ood return image_dict, df, df_verdict123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320import torchfrom detectron2.utils.logger import setup_loggersetup_logger()
from detectron2.config import get_cfgimport detectron2.data.transforms as Tfrom detectron2.checkpoint import DetectionCheckpointerfrom detectron2.modeling import build_model
import numpy as npimport cv2import osimport argparseimport timeimport h5pyimport pickleimport gradio as grimport fiftyone as fofrom fiftyone import ViewField as Fimport tqdmtorch.manual_seed(0)np.random.seed(0)
torch.backends.cudnn.deterministic = Truetorch.backends.cudnn.benchmark = False
from vos.detection.modeling.regnet import build_regnet_fpn_backboneimport core.metadata as metadata
from utils_clustering import *
fullName2ab_dict = {'PASCAL-VOC':"voc", 'BDD100K':"bdd", 'KITTI':"kitti", 'Speed signs':"speed", 'NuScenes':"nu"}ab2FullName_dict = {'voc':"PASCAL-VOC", 'bdd':"BDD100K", 'kitti':"KITTI", 'speed':"Speed signs", 'nu':"NuScenes"}class Detectron2Monitor(): def __init__(self, id, backbone): self.id, self.label_list = self._get_label_list(id) self.backbone = backbone self.cfg, self.device, self.model = self._get_model() self.label_dict = {i:label for i, label in enumerate(self.label_list)} self.eval_list = ["ID-voc-OOD-coco", "OOD-open", "voc-val"] if self.id == "voc" else ["ID-bdd-OOD-coco", "OOD-open", "voc-ood", f"{self.id}-val"] def _get_label_list(self, id): id = fullName2ab_dict[id] if id == 'voc': label_list = metadata.VOC_THING_CLASSES elif id == 'bdd': label_list = metadata.BDD_THING_CLASSES elif id == 'kitti': label_list = metadata.KITTI_THING_CLASSES elif id == 'speed' or id == 'prescan': label_list = metadata.SPEED_THING_CLASSES else: label_list = metadata.NU_THING_CLASSES return id, label_list
def _get_model(self): cfg = get_cfg() cfg.merge_from_file(f"/home/hugo/bdd100k-monitoring/monitoringObjectDetection/vanilla_{self.backbone}.yaml") cfg.MODEL.WEIGHTS = f"models/model_final_{self.backbone}_{self.id}.pth" cfg.MODEL.DEVICE='cuda' cfg.MODEL.ROI_HEADS.NUM_CLASSES = len(self.label_list) model = build_model(cfg) model.eval() checkpointer = DetectionCheckpointer(model) checkpointer.load(cfg.MODEL.WEIGHTS) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) return cfg, device, model
def _inference(self, model, inputs): with torch.no_grad(): images = model.preprocess_image(inputs) features = model.backbone(images.tensor) proposals, _ = model.proposal_generator(images, features, None) # RPN
features_ = [features[f] for f in model.roi_heads.box_in_features] box_features = model.roi_heads.box_pooler(features_, [x.proposal_boxes for x in proposals]) box_features = model.roi_heads.box_head(box_features) # features of all 1k candidates predictions = model.roi_heads.box_predictor(box_features) pred_instances, pred_inds = model.roi_heads.box_predictor.inference(predictions, proposals) pred_instances = model.roi_heads.forward_with_given_boxes(features, pred_instances)
# output boxes, masks, scores, etc pred_instances = model._postprocess(pred_instances, inputs, images.image_sizes) # scale box to orig size # features of the proposed boxes feats = box_features[pred_inds].cpu().numpy() return pred_instances, feats
def _save_features(self, feats_npy, dataset_view, file_path): features_idx_dict = {cls:[] for cls in self.label_list} for sample in tqdm.tqdm(dataset_view, desc="Saving features"): for detection in sample.prediction.detections: label_pred = detection.label feature_idx = detection.feature_idx features_idx_dict[label_pred].append(feature_idx) feats_dict = {cls:feats_npy[features_idx_dict[cls]] for cls in self.label_list} if not os.path.exists(file_path): os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, 'wb') as f: pickle.dump(feats_dict, f)
def _extract(self, dataset_name): dataset = fo.load_dataset(dataset_name) aug = T.AugmentationList([T.ResizeShortestEdge( [self.cfg.INPUT.MIN_SIZE_TEST, self.cfg.INPUT.MIN_SIZE_TEST], self.cfg.INPUT.MAX_SIZE_TEST), ] ) i = 0 feats_list = [] for sample in tqdm.tqdm(dataset, desc="Extracting features"): image = cv2.imread(sample.filepath) height, width = image.shape[:2] input = T.AugInput(image) transform = aug(input) image = input.image image = torch.as_tensor(image.astype("float32").transpose(2, 0, 1)).to(self.device) inputs = [{"image": image, "height": height, "width": width}]
preds, feats = self._inference(self.model, inputs) boxes = preds[0]["instances"].pred_boxes.tensor.cpu().detach().numpy() classes = preds[0]["instances"].pred_classes.cpu().detach().numpy() scores = preds[0]["instances"].scores.cpu().detach().numpy() feats_list.extend(feats) if i == 1000: np.save('feats.npy', feats_list) detections = [] for score, label, box in zip(scores, classes, boxes): x1, y1, x2, y2 = box rel_box = [x1/width, y1/height, (x2 - x1) / width, (y2 - y1) / height] label = self.label_dict[label] detections.append( fo.Detection( label=label, bounding_box=rel_box, confidence=score, feature_idx=i ), ) i += 1 sample["prediction"] = fo.Detections(detections=detections) sample.save() feats_npy = np.array(feats_list) with h5py.File(f'feats_{self.id}-train_{self.backbone}.h5', 'w') as f: dset = f.create_dataset(f"feats_{self.id}-train_{self.backbone}", data=feats_npy) if dataset.name.endswith(("train", "val")): results = dataset.evaluate_detections( "prediction", gt_field="detections", eval_key="eval", compute_mAP=True) # results.print_report() # print("mAP: ", results.mAP()) tp_prediction_view = dataset.filter_labels("prediction", F("eval") == "tp") self._save_features(feats_npy, tp_prediction_view, f"train_feats/{self.id}/{self.backbone}/{self.id}-train_feats_tp_dict.pickle") if dataset.name.endswith("val"): fp_prediction_view = dataset.filter_labels("prediction", F("eval") == "fp") self._save_features(feats_npy, fp_prediction_view, f"val_feats/{self.id}/{self.backbone}/{self.dataset_name}_feats_fp_dict.pickle") else: self._save_features(feats_npy, dataset, f"val_feats/{self.id}/{self.backbone}/{self.dataset_name}_feats_fp_dict.pickle") def _construct(self, clustering_algo, nb_clusters=4, eps=5, min_samples=10): with open(f"/home/hugo/bdd100k-monitoring/train_feats/{self.id}/{self.backbone}/{self.id}-train_feats_tp_dict.pickle", 'rb') as f: feats_dict = pickle.load(f) dir_path = f'monitors/{self.id}/{self.backbone}/{clustering_algo}' if not os.path.exists(dir_path): os.makedirs(dir_path) monitor_dict = {} for class_, fts in tqdm.tqdm(feats_dict.items(), desc="Constructing monitors"): if clustering_algo == "kmeans": clusters = k_means_cluster(fts, nb_clusters) elif clustering_algo == "spectral": clusters = spectral_cluster(fts, nb_clusters) elif clustering_algo == "dbscan": clusters = dbscan_cluster(fts, eps, min_samples) dims = fts.shape[1] box_list = [] for cl_id, points in clusters.items(): box = Box() box.build(dims, points) box_list.append(box) monitor = Monitor(good_ref=box_list) monitor_dict[class_] = monitor if clustering_algo == "dbscan": with open(f"monitors/{self.id}/{self.backbone}/{clustering_algo}/eps{eps}_min_samples{min_samples}.pkl" , 'wb') as f: pickle.dump(monitor_dict, f) else: with open(f"monitors/{self.id}/{self.backbone}/{clustering_algo}/{nb_clusters}.pkl" , 'wb') as f: pickle.dump(monitor_dict, f)
def _load_monitors(self, clustering_algo, nb_clusters, eps=5, min_samples=10): if clustering_algo == "dbscan": with open(f"monitors/{self.id}/{self.backbone}/{clustering_algo}/eps{eps}_min_samples{min_samples}.pkl", 'rb') as f: monitors_dict = pickle.load(f) else: with open(f"monitors/{self.id}/{self.backbone}/{clustering_algo}/{nb_clusters}.pkl", 'rb') as f: monitors_dict = pickle.load(f) return monitors_dict def _evaluate(self, monitors_dict): dataset_name = f"{self.id}-val" with open(f'val_feats/{self.id}/{self.backbone}/{dataset_name}_feats_tp_dict.pickle', 'rb') as f: feats_tp_dict = pickle.load(f) with open(f'val_feats/{self.id}/{self.backbone}/{dataset_name}_feats_fp_dict.pickle', 'rb') as f: feats_fp_dict = pickle.load(f) # monitors_dict = self._load_monitors(clustering_algo, nb_clusters, eps, min_samples) # make verdicts on ID data data_tp = [] data_fp = [] accept_sum = {"tp": 0, "fp": 0} reject_sum = {"tp": 0, "fp": 0} for label in tqdm.tqdm(self.label_list, desc="Evaluation on ID data"): if label in monitors_dict: verdict = monitors_dict[label].make_verdicts(feats_tp_dict[label]) data_tp.append([label, len(verdict), np.sum(verdict)/len(verdict)]) accept_sum["tp"] += np.sum(verdict) reject_sum["tp"] += len(verdict) - np.sum(verdict) verdict = monitors_dict[label].make_verdicts(feats_fp_dict[label]) data_fp.append([label, len(verdict), (len(verdict)-np.sum(verdict))/len(verdict)]) accept_sum["fp"] += np.sum(verdict) reject_sum["fp"] += len(verdict) - np.sum(verdict) TPR = round((accept_sum['tp'] / (reject_sum['tp'] + accept_sum['tp'])*100), 2) FPR = round((accept_sum['fp'] / (reject_sum['fp'] + accept_sum['fp'])*100), 2) id_name = ab2FullName_dict[self.id] df_id = pd.DataFrame([[id_name, f"{TPR}%", f"{FPR}%"]], columns=["Dataset", "TPR", "FPR"]) data_ood = [] i = 0 self.eval_list.remove(dataset_name) for dataset_name in tqdm.tqdm(self.eval_list, desc="Evaluation on OOD data"): accept_sum = {"tp": 0, "fp": 0} reject_sum = {"tp": 0, "fp": 0} with open(f'val_feats/{self.id}/{self.backbone}/{dataset_name}_feats_fp_dict.pickle', 'rb') as f: feats_fp_dict = pickle.load(f) for label in self.label_list: if label in monitors_dict: verdict = monitors_dict[label].make_verdicts(feats_fp_dict[label]) accept_sum["fp"] += np.sum(verdict) reject_sum["fp"] += len(verdict) - np.sum(verdict) FPR = round((accept_sum['fp'] / (reject_sum['fp'] + accept_sum['fp'])*100), 2) data_ood.append([dataset_name, str(FPR)+"%"]) i += 1 # prepare dataframes df_ood = pd.DataFrame(data_ood, columns=["Dataset", "FPR"]) df_ood["Dataset"] = ["COCO", "Open Images"] if self.id == "voc" else ["COCO", "Open Images", "VOC-OOD"] return df_id, df_ood
def _enlarge(self, monitors_dict, delta): for label, monitor in monitors_dict.items(): for i in range(len(monitor.good_ref)): monitor.good_ref[i].ivals = monitor.good_ref[i].ivals*np.array([1-delta, 1+delta]) monitors_dict[label] = monitor return monitors_dict
def fx_gradio(id, backbone, progress=gr.Progress(track_tqdm=True)): detectron2monitor = Detectron2Monitor(id, backbone) t0 = time.time() detectron2monitor._extract(f"{detectron2monitor.id}-train") minutes, seconds = divmod(time.time()-t0, 60) return f"Total feature extraction time: {int(minutes):02d}:{int(seconds):02d}"
def construct_gradio(id, backbone, clustering_algo, nb_clusters, eps, min_samples, progress=gr.Progress(track_tqdm=True)): detection2monitor = Detectron2Monitor(id, backbone) t0 = time.time() detection2monitor._construct(clustering_algo, nb_clusters, eps, min_samples) minutes, seconds = divmod(time.time()-t0, 60) return f"Total monitor construction time: {int(minutes):02d}:{int(seconds):02d}"
def fx_eval_gradio(id, backbone, progress=gr.Progress(track_tqdm=True)): detectron2monitor = Detectron2Monitor(id, backbone) t0 = time.time() for dataset_name in tqdm.tqdm(detectron2monitor.eval_list, desc="Evaluation data preparation"): detectron2monitor._extract(dataset_name) minutes, seconds = divmod(time.time()-t0, 60) return f"Total evaluation data preparation time: {int(minutes):02d}:{int(seconds):02d}"
def eval_gradio(id, backbone, clustering_algo, nb_clusters, eps, min_samples, progress=gr.Progress(track_tqdm=True)): detectron2monitor = Detectron2Monitor(id, backbone) monitors_dict = detectron2monitor._load_monitors(clustering_algo, nb_clusters, eps, min_samples) df_id, df_ood = detectron2monitor._evaluate(monitors_dict) return df_id, df_ood
def enlarge_gradio(id, backbone, clustering_algo, nb_clusters, eps, min_samples, delta, progress=gr.Progress(track_tqdm=True)): detectron2monitor = Detectron2Monitor(id, backbone) monitors_dict = detectron2monitor._load_monitors(clustering_algo, nb_clusters, eps, min_samples) monitors_dict_enlarge = detectron2monitor._enlarge(monitors_dict, delta) df_id, df_ood = detectron2monitor._evaluate(monitors_dict_enlarge) # if clustering_algo == "dbscan": # with open(f"monitors/{id}/{backbone}/{clustering_algo}/eps{eps}_min_samples{min_samples}_delta{delta}.pkl" , 'wb') as f: # pickle.dump(monitors_dict, f) # else: # with open(f"monitors/{id}/{backbone}/{clustering_algo}/{nb_clusters}_delta{delta}.pkl" , 'wb') as f: # pickle.dump(monitors_dict, f) # return f"Monitors enlarged by {delta*100}%" return df_id, df_oodwith gr.Blocks(theme='soft') as demo: gr.Markdown("# Monitor enlargment utility") id = gr.Radio(['PASCAL-VOC', 'BDD100K', 'KITTI', 'Speed signs', 'NuScenes'], label="Dataset") backbone = gr.Radio(['regnet', 'resnet'], label="Backbone") clustering_algo = gr.Dropdown(['kmeans', 'spectral', 'dbscan', 'opticals'], label="Clustering algorithm") with gr.Row(): nb_clusters = gr.Number(value=5, label="Number of clusters", precision=0) eps = gr.Number(value=5, label="Epsilon", precision=0) min_samples = gr.Number(value=10, label="Minimum samples", precision=0) delta = gr.Slider(minimum=0, maximum=2.5, step=0.05, label="Delta") with gr.Row(): with gr.Group("Original monitors"): eval_id = gr.Dataframe(type="pandas", label="ID performance") eavl_ood = gr.Dataframe(type="pandas", label="OOD performance") eval_btn = gr.Button("Monitor Evaluation") with gr.Column("Enlarged monitors"): eval_id2 = gr.Dataframe(type="pandas", label="ID performance") eavl_ood2 = gr.Dataframe(type="pandas", label="OOD performance") enlarge_btn = gr.Button("Monitor Enlargement") eval_btn.click(fn=eval_gradio, inputs=[id, backbone, clustering_algo, nb_clusters, eps, min_samples], outputs=[eval_id, eavl_ood]) enlarge_btn.click(fn=enlarge_gradio, inputs=[id, backbone, clustering_algo, nb_clusters, eps, min_samples, delta], outputs=[eval_id2, eavl_ood2])
demo.queue().launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import torchfrom detectron2.utils.logger import setup_loggersetup_logger()
import numpy as npimport timeimport gradio as grimport tqdmtorch.manual_seed(0)np.random.seed(0)
torch.backends.cudnn.deterministic = Truetorch.backends.cudnn.benchmark = False
from detectron2monitor import Detectron2Monitor
def eval_gradio(id, backbone, clustering_algo, nb_clusters, eps, min_samples, progress=gr.Progress(track_tqdm=True)): detectron2monitor = Detectron2Monitor(id, backbone) df_id, df_ood = detectron2monitor._evaluate(clustering_algo, nb_clusters, eps, min_samples) return df_id, df_ood
def inference_gradio(id, backbone, clustering_algo, nb_clusters, eps, min_samples, file): detectron2monitor = Detectron2Monitor(id, backbone, 0.5) monitors_dict = detectron2monitor._load_monitors(clustering_algo, nb_clusters, eps, min_samples) image_dict, df, df_verdict = detectron2monitor.get_output(monitors_dict, file) return image_dict["detection"], image_dict["verdict"], image_dict["cam"], df, df_verdict
with gr.Blocks(theme='soft') as demo: gr.Markdown("# Runtime Monitoring Computer Vision Models") gr.Markdown( """This interactive demo presents an approach to monitoring neural networks-based computer vision models using box abstraction-based techniques. Our method involves abstracting features extracted from training data to construct monitors. The demo walks users through the entire process, from monitor construction to evaluation. <!-- The interface is divided into several basic modules:
- **In-distribution dataset and backbone**: This module allows users to select their target model and dataset.
- **Feature extraction**: Neuron activation pattern are extracted from the model's intermediate layers using training data. These features represent the good behaviors of the model.- **Monitor construction**: Extracted features are grouped using different clustering techniques. These clusters are then abstracted to serve as references for the monitors. - **Evaluation preparation**: To facilate the evalution, the features should be extracted from evaluation datasets prior to monitor evalution. - **Monitor Evaluation**: The effectiveness of monitors in detecting Out-of-Distribution (OoD) objects are assessed. One of our core metric is FPR 95, which represents the false positive (incorrectly detected objects) rate when the true positive rate for ID is set at 95%. --> """ ) with gr.Tab("Image Classification"): id = gr.Radio(['MNIST', 'CIFAR-10', 'CIFAR-100', 'ImageNet-100', 'ImageNet-1K'], label="Dataset") backbone = gr.Radio(['LeNet-5', 'ResNet-18', 'WideResNet-28', 'ResNet-50'], label="Backbone") with gr.Tab("Object Detection"): id = gr.Radio(['PASCAL-VOC', 'BDD100K', 'KITTI', 'Speed signs', 'NuScenes'], label="Dataset") backbone = gr.Radio(['regnet', 'resnet'], label="Backbone") clustering_algo = gr.Dropdown(['kmeans', 'spectral', 'dbscan', 'opticals'], label="Clustering algorithm") with gr.Row(): nb_clusters = gr.Number(value=5, label="Number of clusters", precision=0) eps = gr.Number(value=5, label="Epsilon", precision=0) min_samples = gr.Number(value=10, label="Minimum samples", precision=0) with gr.Column(): # with gr.Column(): # with gr.Group(): # extract_btn = gr.Button("Extract features") # output1 = gr.Textbox(label="Output") # with gr.Group(): # construct_btn = gr.Button("Monitor Construction") # clustering_algo = gr.Dropdown(['kmeans', 'spectral', 'dbscan', 'opticals'], label="Clustering algorithm") # with gr.Row(): # nb_clusters = gr.Number(value=5, label="Number of clusters", precision=0) # eps = gr.Number(value=5, label="Epsilon", precision=0) # min_samples = gr.Number(value=10, label="Minimum samples", precision=0) # output2 = gr.Textbox(label="Output") # with gr.Column(): # with gr.Group(): # prep_btn = gr.Button("Evaluation Data Preparation") # prep_output = gr.Textbox(label="Output") with gr.Group(): eval_btn = gr.Button("Monitor Evaluation") eval_id = gr.Dataframe(type="pandas", label="ID performance") eavl_ood = gr.Dataframe(type="pandas", label="OOD performance") with gr.Row(): with gr.Column(): image = gr.Image(type="filepath", label="Input") button = gr.Button("Infer") with gr.Column(): with gr.Tab("Detection"): detection = gr.Image(label="Output") df = gr.Dataframe(label="Detection summary") with gr.Tab("Verdict"): verdict = gr.Image(label="Output") df_verdict = gr.Dataframe(label="Verdict summary") with gr.Tab("Explainable AI"): cam = gr.Image(label="Output") button.click(fn=inference_gradio, inputs=[id, backbone, clustering_algo, nb_clusters, eps, min_samples, image], outputs=[detection, verdict, cam, df, df_verdict]) # extract_btn.click(fn=fx_gradio, inputs=[id, backbone], outputs=[output1]) # construct_btn.click(fn=construct_gradio, inputs=[id, backbone, clustering_algo, nb_clusters, eps, min_samples], outputs=[output2]) # prep_btn.click(fn=fx_eval_gradio, inputs=[id, backbone], outputs=[prep_output]) eval_btn.click(fn=eval_gradio, inputs=[id, backbone, clustering_algo, nb_clusters, eps, min_samples], outputs=[eval_id, eavl_ood])demo.queue().launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155import torchfrom detectron2.utils.logger import setup_loggersetup_logger()
from detectron2.config import get_cfgimport detectron2.data.transforms as Tfrom detectron2.checkpoint import DetectionCheckpointerfrom detectron2.modeling import build_modelfrom detectron2.data.detection_utils import read_imagefrom detectron2.utils.visualizer import Visualizerfrom detectron2.data import MetadataCatalog
import numpy as npimport timeimport gradio as grimport fiftyone as fofrom fiftyone import ViewField as Fimport tqdmimport matplotlib.pyplot as pltimport iofrom PIL import Imagetorch.manual_seed(0)np.random.seed(0)
torch.backends.cudnn.deterministic = Truetorch.backends.cudnn.benchmark = Falsefrom detectron2monitor import Detectron2Monitor
def fx_gradio(id, backbone, progress=gr.Progress(track_tqdm=True)): detectron2monitor = Detectron2Monitor(id, backbone) t0 = time.time() detectron2monitor._extract(f"{detectron2monitor.id}-train") minutes, seconds = divmod(time.time()-t0, 60) return f"Total feature extraction time: {int(minutes):02d}:{int(seconds):02d}"
def construct_gradio(id, backbone, clustering_algo, nb_clusters, eps, min_samples, progress=gr.Progress(track_tqdm=True)): detection2monitor = Detectron2Monitor(id, backbone) t0 = time.time() detection2monitor._construct(clustering_algo, nb_clusters, eps, min_samples) minutes, seconds = divmod(time.time()-t0, 60) return f"Total monitor construction time: {int(minutes):02d}:{int(seconds):02d}"
def fx_eval_gradio(id, backbone, progress=gr.Progress(track_tqdm=True)): detectron2monitor = Detectron2Monitor(id, backbone) t0 = time.time() for dataset_name in tqdm.tqdm(detectron2monitor.eval_list, desc="Evaluation data preparation"): detectron2monitor._extract(dataset_name) minutes, seconds = divmod(time.time()-t0, 60) return f"Total evaluation data preparation time: {int(minutes):02d}:{int(seconds):02d}"
def eval_gradio(id, backbone, clustering_algo, nb_clusters, eps, min_samples, progress=gr.Progress(track_tqdm=True)): detectron2monitor = Detectron2Monitor(id, backbone) df_id, df_ood = detectron2monitor._evaluate(clustering_algo, nb_clusters, eps, min_samples) return df_id, df_ood
def inference_gradio(id, backbone, clustering_algo, nb_clusters, eps, min_samples, file): detectron2monitor = Detectron2Monitor(id, backbone, 0.5) monitors_dict = detectron2monitor._load_monitors(clustering_algo, nb_clusters, eps, min_samples) image_dict, df, df_verdict = detectron2monitor.get_output(monitors_dict, file) return image_dict["detection"], image_dict["verdict"], image_dict["cam"], df, df_verdict
with gr.Blocks(theme='soft') as demo: gr.Markdown("# Runtime Monitoring Computer Vision Models") gr.Markdown( """This interactive demo presents an approach to monitoring neural networks-based computer vision models using box abstraction-based techniques. Our method involves abstracting features extracted from training data to construct monitors. The demo walks users through the entire process, from monitor construction to evaluation. The interface is divided into several basic modules:
- **In-distribution dataset and backbone**: This module allows users to select their target model and dataset.- **Feature extraction**: Neuron activation pattern are extracted from the model's intermediate layers using training data. These features represent the good behaviors of the model.- **Monitor construction**: Extracted features are grouped using different clustering techniques. These clusters are then abstracted to serve as references for the monitors. - **Evaluation preparation**: To facilate the evalution, the features should be extracted from evaluation datasets prior to monitor evalution. - **Monitor Evaluation**: The effectiveness of monitors in detecting Out-of-Distribution (OoD) objects are assessed. One of our core metric is FPR 95, which represents the false positive (incorrectly detected objects) rate when the true positive rate for ID is set at 95%. """ ) with gr.Tab("Image Classification"): id = gr.Radio(['MNIST', 'CIFAR-10', 'CIFAR-100', 'ImageNet-100', 'ImageNet-1K'], label="Dataset") backbone = gr.Radio(['LeNet-5', 'ResNet-18', 'WideResNet-28', 'ResNet-50'], label="Backbone") with gr.Tab("Feature extraction"): extract_btn = gr.Button("Extract features") output1 = gr.Textbox(label="Output") with gr.Tab("Monitor construction"): construct_btn = gr.Button("Monitor Construction") clustering_algo = gr.Dropdown(['kmeans', 'spectral', 'dbscan', 'opticals'], label="Clustering algorithm") with gr.Row(): nb_clusters = gr.Number(value=5, label="Number of clusters", precision=0) eps = gr.Number(value=5, label="Epsilon", precision=0) min_samples = gr.Number(value=10, label="Minimum samples", precision=0) output2 = gr.Textbox(label="Output") with gr.Tab("Evaluation"): prep_btn = gr.Button("Evaluation Data Preparation") prep_output = gr.Textbox(label="Output") with gr.Tab("Evaluation results"): eval_btn = gr.Button("Monitor Evaluation") eval_id = gr.Dataframe(type="pandas", label="ID performance") eavl_ood = gr.Dataframe(type="pandas", label="OOD performance") with gr.Tab("Inference"): with gr.Row().style(equal_height=True): with gr.Column(): image = gr.Image(type="filepath", label="Input") button = gr.Button("Infer") with gr.Column(): with gr.Tab("Detection"): detection = gr.Image(label="Output") df = gr.Dataframe(label="Detection summary") with gr.Tab("Verdict"): verdict = gr.Image(label="Output") df_verdict = gr.Dataframe(label="Verdict summary") with gr.Tab("Explainable AI"): cam = gr.Image(label="Output") with gr.Tab("Object Detection"): id = gr.Radio(['PASCAL-VOC', 'BDD100K', 'KITTI', 'Speed signs', 'NuScenes'], label="Dataset") backbone = gr.Radio(['regnet', 'resnet'], label="Backbone") with gr.Tab("Feature extraction"): extract_btn = gr.Button("Extract features") output1 = gr.Textbox(label="Output") with gr.Tab("Monitor construction"): construct_btn = gr.Button("Monitor Construction") clustering_algo = gr.Dropdown(['kmeans', 'spectral', 'dbscan', 'opticals'], label="Clustering algorithm") with gr.Row(): nb_clusters = gr.Number(value=5, label="Number of clusters", precision=0) eps = gr.Number(value=5, label="Epsilon", precision=0) min_samples = gr.Number(value=10, label="Minimum samples", precision=0) output2 = gr.Textbox(label="Output") with gr.Tab("Evaluation preparation"): prep_btn = gr.Button("Evaluation Data Preparation") prep_output = gr.Textbox(label="Output") with gr.Tab("Evaluation results"): eval_btn = gr.Button("Monitor Evaluation") eval_id = gr.Dataframe(type="pandas", label="ID performance") eavl_ood = gr.Dataframe(type="pandas", label="OOD performance") with gr.Tab("Inference"): with gr.Row().style(equal_height=True): with gr.Column(): image = gr.Image(type="filepath", label="Input") button = gr.Button("Infer") with gr.Column(): with gr.Tab("Detection"): detection = gr.Image(label="Output") df = gr.Dataframe(label="Detection summary") with gr.Tab("Verdict"): verdict = gr.Image(label="Output") df_verdict = gr.Dataframe(label="Verdict summary") with gr.Tab("Explainable AI"): cam = gr.Image(label="Output") button.click(fn=inference_gradio, inputs=[id, backbone, clustering_algo, nb_clusters, eps, min_samples, image], outputs=[detection, verdict, cam, df, df_verdict]) extract_btn.click(fn=fx_gradio, inputs=[id, backbone], outputs=[output1]) construct_btn.click(fn=construct_gradio, inputs=[id, backbone, clustering_algo, nb_clusters, eps, min_samples], outputs=[output2]) prep_btn.click(fn=fx_eval_gradio, inputs=[id, backbone], outputs=[prep_output]) eval_btn.click(fn=eval_gradio, inputs=[id, backbone, clustering_algo, nb_clusters, eps, min_samples], outputs=[eval_id, eavl_ood])demo.queue().launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950MODEL: META_ARCHITECTURE: "GeneralizedRCNN"
BACKBONE: NAME: "build_resnet_fpn_backbone"
RESNETS: OUT_FEATURES: ["res2", "res3", "res4", "res5"]
FPN: IN_FEATURES: ["res2", "res3", "res4", "res5"]
ANCHOR_GENERATOR: # One size for each in feature map SIZES: [[32], [64], [128], [256], [512]] # Three aspect ratios (same for all in feature maps) ASPECT_RATIOS: [[0.5, 1.0, 2.0]]
RPN: IN_FEATURES: ["p2", "p3", "p4", "p5", "p6"] PRE_NMS_TOPK_TRAIN: 2000 # Per FPN level PRE_NMS_TOPK_TEST: 1000 # Per FPN level # Detectron1 uses 2000 proposals per-batch, # (See "modeling/rpn/rpn_outputs.py" for details of this legacy issue) # which is approximately 1000 proposals per-image since the default # batch size for FPN is 2. POST_NMS_TOPK_TRAIN: 1000 POST_NMS_TOPK_TEST: 1000
ROI_HEADS: NAME: "StandardROIHeads" IN_FEATURES: ["p2", "p3", "p4", "p5"]
ROI_BOX_HEAD: NAME: "FastRCNNConvFCHead" NUM_FC: 2 POOLER_RESOLUTION: 7
ROI_MASK_HEAD: NAME: "MaskRCNNConvUpsampleHead" NUM_CONV: 4 POOLER_RESOLUTION: 14
INPUT: MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800)
SOLVER: CHECKPOINT_PERIOD: 210000
VERSION: 2123456789101112131415161718192021222324252627282930313233_BASE_: "Base-RCNN-FPN.yaml"MODEL: PIXEL_STD: [57.375, 57.120, 58.395] BACKBONE: NAME: "build_regnetx_fpn_backbone" META_ARCHITECTURE: "GeneralizedRCNN" WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl"# WEIGHTS: "./data/VOC-Detection/faster-rcnn/faster_rcnn_R_50_FPN_all_logistic/random_seed_0/model_final.pth"
# PROPOSAL_GENERATOR:# NAME: "RPNLogistic" FPN: IN_FEATURES: ["s1", "s2", "s3", "s4"] MASK_ON: False RESNETS: DEPTH: 50 ROI_HEADS: NAME: "StandardROIHeads" NUM_CLASSES: 10INPUT: MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) MIN_SIZE_TEST: 800DATASETS: TRAIN: ('bdd_custom_train',) TEST: ('bdd_custom_val',)SOLVER: IMS_PER_BATCH: 16 BASE_LR: 0.02 STEPS: (60000, 80000) MAX_ITER: 90000 # 17.4 epochs WARMUP_ITERS: 100DATALOADER: NUM_WORKERS: 8 # Depends on the available memory12345678910111213141516171819202122232425262728_BASE_: "Base-RCNN-FPN.yaml"MODEL: META_ARCHITECTURE: "GeneralizedRCNN" WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl"# WEIGHTS: "./data/VOC-Detection/faster-rcnn/faster_rcnn_R_50_FPN_all_logistic/random_seed_0/model_final.pth"
# PROPOSAL_GENERATOR:# NAME: "RPNLogistic" MASK_ON: False RESNETS: DEPTH: 50 ROI_HEADS: NAME: "StandardROIHeads" NUM_CLASSES: 10INPUT: MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) MIN_SIZE_TEST: 800DATASETS: TRAIN: ('bdd_custom_train',) TEST: ('bdd_custom_val',)SOLVER: IMS_PER_BATCH: 16 BASE_LR: 0.02 STEPS: (60000, 80000) MAX_ITER: 90000 # 17.4 epochs WARMUP_ITERS: 100DATALOADER: NUM_WORKERS: 8 # Depends on the available memory1234567891011121314151617181920212223242526272829303132333435VOC_THING_CLASSES = ['person', 'bird', 'cat', 'cow', 'dog', 'horse', 'sheep', 'airplane', 'bicycle', 'boat', 'bus', 'car', 'motorcycle', 'train', 'bottle', 'chair', 'dining table', 'potted plant', 'couch', 'tv', ]
BDD_THING_CLASSES = ['pedestrian', 'rider', 'car', 'truck', 'bus', 'train', 'motorcycle', 'bicycle', 'traffic light', 'traffic sign']KITTI_THING_CLASSES = ["Car", "Pedestrian", "Cyclist", "Van", "Truck", "Tram"]SPEED_THING_CLASSES = ['100kph','120kph','20kph','30kph','40kph','5kph','50kph','60kph','70kph','80kph']NU_THING_CLASSES = ['car','truck','trailer','bus','construction_vehicle','bicycle','motorcycle','pedestrian','traffic_cone','barrier']123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384from .regnet_model import RegNetfrom .regnet_model import SimpleStem, ResBottleneckBlock
from detectron2.modeling.backbone.build import BACKBONE_REGISTRYfrom detectron2.modeling.backbone.fpn import FPN, LastLevelMaxPool
from detectron2.layers import ( Conv2d, DeformConv, FrozenBatchNorm2d, ModulatedDeformConv, ShapeSpec, get_norm,)
# train.cudnn_benchmark = True
@BACKBONE_REGISTRY.register()def build_regnet_fpn_backbone(cfg, input_shape: ShapeSpec): """ Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ bottom_up = RegNet( stem_class=SimpleStem, stem_width=32, block_class=ResBottleneckBlock, depth=22, w_a=31.41, w_0=96, w_m=2.24, group_width=64, se_ratio=0.25, freeze_at=2, norm="FrozenBN", out_features=["s1", "s2", "s3", "s4"], ) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS backbone = FPN( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=LastLevelMaxPool(), fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) return backbone
@BACKBONE_REGISTRY.register()def build_regnetx_fpn_backbone(cfg, input_shape: ShapeSpec): """ Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ bottom_up = RegNet( stem_class=SimpleStem, stem_width=32, block_class=ResBottleneckBlock, depth=23, w_a=38.65, w_0=96, w_m=2.43, group_width=40, freeze_at=2, norm="FrozenBN", out_features=["s1", "s2", "s3", "s4"],) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS backbone = FPN( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=LastLevelMaxPool(), fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) return backbone123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved"""Implementation of RegNet models from :paper:`dds` and :paper:`scaling`.This code is adapted from https://github.com/facebookresearch/pycls with minimal modifications.Some code duplication exists between RegNet and ResNets (e.g., ResStem) in order to simplifymodel loading."""
import numpy as npfrom torch import nn
from detectron2.layers import CNNBlockBase, ShapeSpec, get_norm
from detectron2.modeling.backbone import Backbone
__all__ = [ "AnyNet", "RegNet", "ResStem", "SimpleStem", "VanillaBlock", "ResBasicBlock", "ResBottleneckBlock",]
def conv2d(w_in, w_out, k, *, stride=1, groups=1, bias=False): """Helper for building a conv2d layer.""" assert k % 2 == 1, "Only odd size kernels supported to avoid padding issues." s, p, g, b = stride, (k - 1) // 2, groups, bias return nn.Conv2d(w_in, w_out, k, stride=s, padding=p, groups=g, bias=b)
def gap2d(): """Helper for building a global average pooling layer.""" return nn.AdaptiveAvgPool2d((1, 1))
def pool2d(k, *, stride=1): """Helper for building a pool2d layer.""" assert k % 2 == 1, "Only odd size kernels supported to avoid padding issues." return nn.MaxPool2d(k, stride=stride, padding=(k - 1) // 2)
def init_weights(m): """Performs ResNet-style weight initialization.""" if isinstance(m, nn.Conv2d): # Note that there is no bias due to BN fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(mean=0.0, std=np.sqrt(2.0 / fan_out)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1.0) m.bias.data.zero_() elif isinstance(m, nn.Linear): m.weight.data.normal_(mean=0.0, std=0.01) m.bias.data.zero_()
class ResStem(CNNBlockBase): """ResNet stem for ImageNet: 7x7, BN, AF, MaxPool."""
def __init__(self, w_in, w_out, norm, activation_class): super().__init__(w_in, w_out, 4) self.conv = conv2d(w_in, w_out, 7, stride=2) self.bn = get_norm(norm, w_out) self.af = activation_class() self.pool = pool2d(3, stride=2)
def forward(self, x): for layer in self.children(): x = layer(x) return x
class SimpleStem(CNNBlockBase): """Simple stem for ImageNet: 3x3, BN, AF."""
def __init__(self, w_in, w_out, norm, activation_class): super().__init__(w_in, w_out, 2) self.conv = conv2d(w_in, w_out, 3, stride=2) self.bn = get_norm(norm, w_out) self.af = activation_class()
def forward(self, x): for layer in self.children(): x = layer(x) return x
class SE(nn.Module): """Squeeze-and-Excitation (SE) block: AvgPool, FC, Act, FC, Sigmoid."""
def __init__(self, w_in, w_se, activation_class): super().__init__() self.avg_pool = gap2d() self.f_ex = nn.Sequential( conv2d(w_in, w_se, 1, bias=True), activation_class(), conv2d(w_se, w_in, 1, bias=True), nn.Sigmoid(), )
def forward(self, x): return x * self.f_ex(self.avg_pool(x))
class VanillaBlock(CNNBlockBase): """Vanilla block: [3x3 conv, BN, Relu] x2."""
def __init__(self, w_in, w_out, stride, norm, activation_class, _params): super().__init__(w_in, w_out, stride) self.a = conv2d(w_in, w_out, 3, stride=stride) self.a_bn = get_norm(norm, w_out) self.a_af = activation_class() self.b = conv2d(w_out, w_out, 3) self.b_bn = get_norm(norm, w_out) self.b_af = activation_class()
def forward(self, x): for layer in self.children(): x = layer(x) return x
class BasicTransform(nn.Module): """Basic transformation: [3x3 conv, BN, Relu] x2."""
def __init__(self, w_in, w_out, stride, norm, activation_class, _params): super().__init__() self.a = conv2d(w_in, w_out, 3, stride=stride) self.a_bn = get_norm(norm, w_out) self.a_af = activation_class() self.b = conv2d(w_out, w_out, 3) self.b_bn = get_norm(norm, w_out) self.b_bn.final_bn = True
def forward(self, x): for layer in self.children(): x = layer(x) return x
class ResBasicBlock(CNNBlockBase): """Residual basic block: x + f(x), f = basic transform."""
def __init__(self, w_in, w_out, stride, norm, activation_class, params): super().__init__(w_in, w_out, stride) self.proj, self.bn = None, None if (w_in != w_out) or (stride != 1): self.proj = conv2d(w_in, w_out, 1, stride=stride) self.bn = get_norm(norm, w_out) self.f = BasicTransform(w_in, w_out, stride, norm, activation_class, params) self.af = activation_class()
def forward(self, x): x_p = self.bn(self.proj(x)) if self.proj else x return self.af(x_p + self.f(x))
class BottleneckTransform(nn.Module): """Bottleneck transformation: 1x1, 3x3 [+SE], 1x1."""
def __init__(self, w_in, w_out, stride, norm, activation_class, params): super().__init__() w_b = int(round(w_out * params["bot_mul"])) w_se = int(round(w_in * params["se_r"])) groups = w_b // params["group_w"] self.a = conv2d(w_in, w_b, 1) self.a_bn = get_norm(norm, w_b) self.a_af = activation_class() self.b = conv2d(w_b, w_b, 3, stride=stride, groups=groups) self.b_bn = get_norm(norm, w_b) self.b_af = activation_class() self.se = SE(w_b, w_se, activation_class) if w_se else None self.c = conv2d(w_b, w_out, 1) self.c_bn = get_norm(norm, w_out) self.c_bn.final_bn = True
def forward(self, x): for layer in self.children(): x = layer(x) return x
class ResBottleneckBlock(CNNBlockBase): """Residual bottleneck block: x + f(x), f = bottleneck transform."""
def __init__(self, w_in, w_out, stride, norm, activation_class, params): super().__init__(w_in, w_out, stride) self.proj, self.bn = None, None if (w_in != w_out) or (stride != 1): self.proj = conv2d(w_in, w_out, 1, stride=stride) self.bn = get_norm(norm, w_out) self.f = BottleneckTransform(w_in, w_out, stride, norm, activation_class, params) self.af = activation_class()
def forward(self, x): x_p = self.bn(self.proj(x)) if self.proj else x return self.af(x_p + self.f(x))
class AnyStage(nn.Module): """AnyNet stage (sequence of blocks w/ the same output shape)."""
def __init__(self, w_in, w_out, stride, d, block_class, norm, activation_class, params): super().__init__() for i in range(d): block = block_class(w_in, w_out, stride, norm, activation_class, params) self.add_module("b{}".format(i + 1), block) stride, w_in = 1, w_out
def forward(self, x): for block in self.children(): x = block(x) return x
class AnyNet(Backbone): """AnyNet model. See :paper:`dds`."""
def __init__( self, *, stem_class, stem_width, block_class, depths, widths, group_widths, strides, bottleneck_ratios, se_ratio, activation_class, freeze_at=0, norm="BN", out_features=None, ): """ Args: stem_class (callable): A callable taking 4 arguments (channels in, channels out, normalization, callable returning an activation function) that returns another callable implementing the stem module. stem_width (int): The number of output channels that the stem produces. block_class (callable): A callable taking 6 arguments (channels in, channels out, stride, normalization, callable returning an activation function, a dict of block-specific parameters) that returns another callable implementing the repeated block module. depths (list[int]): Number of blocks in each stage. widths (list[int]): For each stage, the number of output channels of each block. group_widths (list[int]): For each stage, the number of channels per group in group convolution, if the block uses group convolution. strides (list[int]): The stride that each network stage applies to its input. bottleneck_ratios (list[float]): For each stage, the ratio of the number of bottleneck channels to the number of block input channels (or, equivalently, output channels), if the block uses a bottleneck. se_ratio (float): The ratio of the number of channels used inside the squeeze-excitation (SE) module to it number of input channels, if SE the block uses SE. activation_class (callable): A callable taking no arguments that returns another callable implementing an activation function. freeze_at (int): The number of stages at the beginning to freeze. see :meth:`freeze` for detailed explanation. norm (str or callable): normalization for all conv layers. See :func:`layers.get_norm` for supported format. out_features (list[str]): name of the layers whose outputs should be returned in forward. RegNet's use "stem" and "s1", "s2", etc for the stages after the stem. If None, will return the output of the last layer. """ super().__init__() self.stem = stem_class(3, stem_width, norm, activation_class)
current_stride = self.stem.stride self._out_feature_strides = {"stem": current_stride} self._out_feature_channels = {"stem": self.stem.out_channels} self.stages_and_names = [] prev_w = stem_width
for i, (d, w, s, b, g) in enumerate( zip(depths, widths, strides, bottleneck_ratios, group_widths) ): params = {"bot_mul": b, "group_w": g, "se_r": se_ratio} stage = AnyStage(prev_w, w, s, d, block_class, norm, activation_class, params) name = "s{}".format(i + 1) self.add_module(name, stage) self.stages_and_names.append((stage, name)) self._out_feature_strides[name] = current_stride = int( current_stride * np.prod([k.stride for k in stage.children()]) ) self._out_feature_channels[name] = list(stage.children())[-1].out_channels prev_w = w
self.apply(init_weights)
if out_features is None: out_features = [name] self._out_features = out_features assert len(self._out_features) children = [x[0] for x in self.named_children()] for out_feature in self._out_features: assert out_feature in children, "Available children: {} does not include {}".format( ", ".join(children), out_feature ) self.freeze(freeze_at)
def forward(self, x): """ Args: x: Tensor of shape (N,C,H,W). H, W must be a multiple of ``self.size_divisibility``. Returns: dict[str->Tensor]: names and the corresponding features """ assert x.dim() == 4, f"Model takes an input of shape (N, C, H, W). Got {x.shape} instead!" outputs = {} x = self.stem(x) if "stem" in self._out_features: outputs["stem"] = x for stage, name in self.stages_and_names: x = stage(x) if name in self._out_features: outputs[name] = x return outputs
def output_shape(self): return { name: ShapeSpec( channels=self._out_feature_channels[name], stride=self._out_feature_strides[name] ) for name in self._out_features }
def freeze(self, freeze_at=0): """ Freeze the first several stages of the model. Commonly used in fine-tuning. Layers that produce the same feature map spatial size are defined as one "stage" by :paper:`FPN`. Args: freeze_at (int): number of stages to freeze. `1` means freezing the stem. `2` means freezing the stem and one residual stage, etc. Returns: nn.Module: this model itself """ if freeze_at >= 1: self.stem.freeze() for idx, (stage, _) in enumerate(self.stages_and_names, start=2): if freeze_at >= idx: for block in stage.children(): block.freeze() return self
def adjust_block_compatibility(ws, bs, gs): """Adjusts the compatibility of widths, bottlenecks, and groups.""" assert len(ws) == len(bs) == len(gs) assert all(w > 0 and b > 0 and g > 0 for w, b, g in zip(ws, bs, gs)) vs = [int(max(1, w * b)) for w, b in zip(ws, bs)] gs = [int(min(g, v)) for g, v in zip(gs, vs)] ms = [np.lcm(g, b) if b > 1 else g for g, b in zip(gs, bs)] vs = [max(m, int(round(v / m) * m)) for v, m in zip(vs, ms)] ws = [int(v / b) for v, b in zip(vs, bs)] assert all(w * b % g == 0 for w, b, g in zip(ws, bs, gs)) return ws, bs, gs
def generate_regnet_parameters(w_a, w_0, w_m, d, q=8): """Generates per stage widths and depths from RegNet parameters.""" assert w_a >= 0 and w_0 > 0 and w_m > 1 and w_0 % q == 0 # Generate continuous per-block ws ws_cont = np.arange(d) * w_a + w_0 # Generate quantized per-block ws ks = np.round(np.log(ws_cont / w_0) / np.log(w_m)) ws_all = w_0 * np.power(w_m, ks) ws_all = np.round(np.divide(ws_all, q)).astype(int) * q # Generate per stage ws and ds (assumes ws_all are sorted) ws, ds = np.unique(ws_all, return_counts=True) # Compute number of actual stages and total possible stages num_stages, total_stages = len(ws), ks.max() + 1 # Convert numpy arrays to lists and return ws, ds, ws_all, ws_cont = (x.tolist() for x in (ws, ds, ws_all, ws_cont)) return ws, ds, num_stages, total_stages, ws_all, ws_cont
class RegNet(AnyNet): """RegNet model. See :paper:`dds`."""
def __init__( self, *, stem_class, stem_width, block_class, depth, w_a, w_0, w_m, group_width, stride=2, bottleneck_ratio=1.0, se_ratio=0.0, activation_class=None, freeze_at=0, norm="BN", out_features=None, ): """ Build a RegNet from the parameterization described in :paper:`dds` Section 3.3. Args: See :class:`AnyNet` for arguments that are not listed here. depth (int): Total number of blocks in the RegNet. w_a (float): Factor by which block width would increase prior to quantizing block widths by stage. See :paper:`dds` Section 3.3. w_0 (int): Initial block width. See :paper:`dds` Section 3.3. w_m (float): Parameter controlling block width quantization. See :paper:`dds` Section 3.3. group_width (int): Number of channels per group in group convolution, if the block uses group convolution. bottleneck_ratio (float): The ratio of the number of bottleneck channels to the number of block input channels (or, equivalently, output channels), if the block uses a bottleneck. stride (int): The stride that each network stage applies to its input. """ ws, ds = generate_regnet_parameters(w_a, w_0, w_m, depth)[0:2] ss = [stride for _ in ws] bs = [bottleneck_ratio for _ in ws] gs = [group_width for _ in ws] ws, bs, gs = adjust_block_compatibility(ws, bs, gs)
def default_activation_class(): return nn.ReLU(inplace=True)
super().__init__( stem_class=stem_class, stem_width=stem_width, block_class=block_class, depths=ds, widths=ws, strides=ss, group_widths=gs, bottleneck_ratios=bs, se_ratio=se_ratio, activation_class=default_activation_class if activation_class is None else activation_class, freeze_at=freeze_at, norm=norm, out_features=out_features, )123version https://git-lfs.github.com/spec/v1oid sha256:e577cbf96b80d227eb4959271de68c69f72d653a7ed406cda5c17c4b83bfd388size 330233039123version https://git-lfs.github.com/spec/v1oid sha256:fc623c2a3c64b52fc93d099f365352d4d206d8f94298f2cee7c40c94ddba7b8asize 136810565123version https://git-lfs.github.com/spec/v1oid sha256:d88075b898e5bcf962ff03babe51150db2e5b8287777c878e2e4854134b47d39size 152229171123version https://git-lfs.github.com/spec/v1oid sha256:84b571641d7207d14e31390a465f3bfbe494da90deac82d9e68cd6d3574f376bsize 157291017123version https://git-lfs.github.com/spec/v1oid sha256:6d85e0d97dccfba7f618e87813db07514d2dacf8ab5655a195da0ad5f2091856size 160503921123456789101112torchdetectron2numpyopencv-pythonh5pypicklegradiofiftyonetqdmmatplotlibgrad-camscikit-learn1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from abstractions import *
import pickle
import numpy as np
class Monitor(object):
def __init__(self, good_ref=None):
# self.abs_type = abs_type
self.good_ref = good_ref
def set_reference(self, good_ref):
self.good_ref = good_ref
# def get_identity(self):
# print("Monitor for network:" + self.netName + "class: " + str(self.classification) + "at layer " + str(self.location))
def make_verdicts(self, features):
if len(self.good_ref):
verdicts = ref_query(features, self.good_ref)
else:
raise RuntimeError("No reference exists!")
return verdicts
# def make_verdicts_delta(self, features, delta):
# if len(self.good_ref):
# verdicts = ref_query_delta(features, self.good_ref, delta)
# else:
# raise RuntimeError("No reference exists!")
# return verdicts
def ref_query(features, reference):
query_results = [boxes_query(x, reference) for x in features]
return query_results
# def ref_query_delta(features, reference, delta):
# query_results = [boxes_query_delta(x, reference, delta) for x in features]
# return query_results
# def query_infusion(in_good_ref, in_bad_ref):
# if len(in_good_ref) == len(in_bad_ref): #0: acceptance (true, false), 1: rejection (false, true or false), 2: uncertainty (true, true)
# verdicts = np.zeros(len(in_good_ref), dtype=int)
# for i in range(len(in_good_ref)):
# if not in_good_ref[i]:
# verdicts[i] = 1
# elif in_bad_ref[i]:
# verdicts[i] = 2
# return verdicts
# else:
# print("Error: IllegalArgument") 12from .Monitor import *
123version https://git-lfs.github.com/spec/v1oid sha256:59624dd3527292e3cde0107ecc38f7fc85bb96bbe3b9a3458f6a7dab25b2fe76size 126263743123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138#!/usr/bin/env python
# coding: utf-8
import numpy as np
import pandas as pd
import time
import os.path
# from sklearnex import patch_sklearn, unpatch_sklearn
# patch_sklearn()
from sklearn.cluster import KMeans
from sklearn.cluster import MeanShift
# values: a two-dimensional array, m number of n-dimensional vectors to be clustered;
def modified_kmeans_cluster(values_to_cluster, threshold, k_start, n_clusters=None):
if n_clusters is not None:
kmeans = KMeans(n_clusters=n_clusters, n_init="auto", random_state=0).fit(values_to_cluster)
return kmeans.labels_
else:
n_clusters = k_start
n_values = len(values_to_cluster)
assert n_values > 0
kmeans = KMeans(n_clusters=n_clusters, n_init="auto", random_state=0).fit(values_to_cluster)
inertias = [kmeans.inertia_]
while n_values > n_clusters:
n_clusters_new = n_clusters + 1
kmeans_new = KMeans(n_clusters=n_clusters_new, n_init="auto", random_state=0).fit(values_to_cluster)
inertias.append(kmeans_new.inertia_)
if terminate_clustering(inertias, threshold):
break
kmeans = kmeans_new
n_clusters += 1
return kmeans.labels_
def terminate_clustering(inertias, threshold):
# method: compute relative improvement toward previous step
assert len(inertias) > 1
improvement = 1 - (inertias[-1] / inertias[-2])
return improvement < threshold
def cluster_existed_features(network_folder_path, classes, layers_indexes, taus):
appendixes = ["_correctly_classified_features.csv", "_incorrectly_classified_features.csv"]
product = ((i, y, appendix) for i in layers_indexes for y in classes for appendix in appendixes)
for i, y, appendix in product:
start_time = time.time()
# load data for class y at layer minus i
features_file_path = network_folder_path +"Layer_minus_" + str(i) + "/class_" + str(y) + appendix
df = pd.read_csv(features_file_path)
index_values = df["index"].to_numpy()
values_to_cluster = df[df.columns[3:]].to_numpy()
if len(values_to_cluster):
# specify path and then load existing clustering results
k_and_taus = dict()
taus_existed = []
clustering_results = pd.DataFrame(df, columns=["index", "true_label", "pred_label"])
clustering_results_path = network_folder_path + "Layer_minus_" + str(i) + "/clustering_results_class_" + str(y) + appendix
if os.path.exists(clustering_results_path):
clustering_results = pd.read_csv(clustering_results_path)
for col in clustering_results.columns[3:]:
k_and_taus[col] = clustering_results[col].max() + 1
# update the existing values of tau
taus_existed = [float(key) for key in k_and_taus.keys()]
# remove existing tau from list existed_taus
taus_new = [tau for tau in taus if tau not in taus_existed]
# iterate every tau to cluster the given data
for tau in taus_new:
# fix starting searching point
k_start = 1
bigger_taus = [x for x in taus_existed if x > tau]
if len(bigger_taus):
tau_closest = min(bigger_taus)
k_start = k_and_taus[str(tau_closest)]
# start to cluster
cluster_labels = modified_kmeans_cluster(values_to_cluster, tau, k_start)
clustering_results[str(tau)] = cluster_labels
taus_existed.append(tau)
k_and_taus[str(tau)] = max(cluster_labels) + 1
clustering_results.to_csv(clustering_results_path, index = False)
elapsed_time = time.time() - start_time
print("file:" + "Layer_minus_" + str(i) + "_class_" + str(y) + appendix + ",", "lasting time:", elapsed_time, "seconds")
def features_clustering(features, taus, nb_clusters):
start_time = time.time()
values_to_cluster = features
if len(values_to_cluster):
# specify path and then load existing clustering results
k_and_taus = dict()
taus_existed = []
# if os.path.exists(clustering_results_path):
# clustering_results = pd.read_csv(clustering_results_path)
# for col in clustering_results.columns[3:]:
# k_and_taus[col] = clustering_results[col].max() + 1
# else:
# clustering_results = pd.DataFrame()
# update the existing values of tau
taus_existed = [float(key) for key in k_and_taus.keys()]
# remove existing tau from list existed_taus
taus_new = [tau for tau in taus if tau not in taus_existed]
clustering_results = dict()
# iterate every tau to cluster the given data
for tau in taus_new:
# fix starting searching point
k_start = 1
bigger_taus = [x for x in taus_existed if x > tau]
if len(bigger_taus):
tau_closest = min(bigger_taus)
k_start = k_and_taus[str(tau_closest)]
# start to cluster
cluster_labels = modified_kmeans_cluster(values_to_cluster, tau, k_start, nb_clusters)
clustering_results[str(tau)] = cluster_labels
taus_existed.append(tau)
k_and_taus[str(tau)] = max(cluster_labels) + 1
# clustering_results.to_csv(clustering_results_path, index = False)
elapsed_time = time.time() - start_time
# print("clustering time:", elapsed_time, "seconds")
return clustering_results
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
import numpy as np
import pandas as pd
import pickle
import os
import sys
# append the path of the parent directory
sys.path.append("..")
from abstractions import *
from runtime_monitors import *
# def monitors_offline_construction(network_name, network_folder_path, classes, layers_indexes, taus):
# appendixes = ["_correctly_classified_features.csv", "_incorrectly_classified_features.csv"]
# product = ((i, y) for i in layers_indexes for y in classes)
# for i, y in product:
# # load obtained features to creat reference
# path_bad_features = network_folder_path +"Layer_minus_" + str(i) + "/class_" + str(y) + appendixes[1]
# path_good_features = network_folder_path +"Layer_minus_" + str(i) + "/class_" + str(y) + appendixes[0]
# bad_feat_clustering_results = []
# good_feat_clustering_results = []
# if os.path.exists(path_bad_features):
# df_bad_features = pd.read_csv(path_bad_features)
# bad_features_to_cluster = df_bad_features[df_bad_features.columns[3:]].to_numpy()
# bad_features_index = df_bad_features["index"].to_numpy()
# # load clustering results to partition the features
# bad_feat_clustering_results_path = network_folder_path + "Layer_minus_" + str(i) + "/clustering_results_class_" + str(y) + appendixes[1]
# if os.path.exists(bad_feat_clustering_results_path):
# bad_feat_clustering_results = pd.read_csv(bad_feat_clustering_results_path)
# if os.path.exists(path_good_features):
# df_good_features = pd.read_csv(path_good_features)
# good_features_to_cluster = df_good_features[df_good_features.columns[3:]].to_numpy()
# good_features_index = df_good_features["index"].to_numpy()
# # load clustering results to partition the features
# good_feat_clustering_results_path = network_folder_path + "Layer_minus_" + str(i) + "/clustering_results_class_" + str(y) + appendixes[0]
# n_dim = good_features_to_cluster.shape[1]
# if os.path.exists(good_feat_clustering_results_path):
# good_feat_clustering_results = pd.read_csv(good_feat_clustering_results_path)
# for tau in taus:
# good_loc_boxes = []
# bad_loc_boxes = []
# if len(bad_feat_clustering_results):
# # load clustering result related to tau
# bad_feat_clustering_result = bad_feat_clustering_results[str(tau)]
# # determine the labels of clusters
# bad_num_clusters = np.amax(bad_feat_clustering_result) + 1
# bad_clustering_labels = np.arange(bad_num_clusters)
# # extract the indices of vectors in a cluster
# bad_clusters_indices = []
# for k in bad_clustering_labels:
# bad_indices_cluster_k, = np.where(bad_feat_clustering_result == k)
# bad_clusters_indices.append(bad_indices_cluster_k)
# # creat local box for each cluster
# bad_loc_boxes = [Box() for i in bad_clustering_labels]
# for j in range(len(bad_loc_boxes)):
# bad_points_j = [(bad_features_index[i], bad_features_to_cluster[i]) for i in bad_clusters_indices[j]]
# bad_loc_boxes[j].build(n_dim, bad_points_j)
# if len(good_feat_clustering_results):
# # load clustering result related to tau
# good_feat_clustering_result = good_feat_clustering_results[str(tau)]
# # determine the labels of clusters
# good_num_clusters = np.amax(good_feat_clustering_result) + 1
# good_clustering_labels = np.arange(good_num_clusters)
# # extract the indices of vectors in a cluster
# good_clusters_indices = []
# for k in good_clustering_labels:
# good_indices_cluster_k, = np.where(good_feat_clustering_result == k)
# good_clusters_indices.append(good_indices_cluster_k)
# # creat local box for each cluster
# good_loc_boxes = [Box() for i in good_clustering_labels]
# for j in range(len(good_loc_boxes)):
# good_points_j = [(good_features_index[i], good_features_to_cluster[i]) for i in good_clusters_indices[j]]
# good_loc_boxes[j].build(n_dim, good_points_j)
# # creat the monitor for class y at layer i
# monitor_y_i = Monitor("Box", network_name, y, i, good_ref=good_loc_boxes, bad_ref=bad_loc_boxes)
# # save the created monitor
# monitor_stored_folder_path = network_folder_path + "Monitors/"
# if not os.path.exists(monitor_stored_folder_path):
# os.makedirs(monitor_stored_folder_path)
# monitor_stored_path = monitor_stored_folder_path + network_name + "_monitor_for_class_" + str(y) + "_at_layer_minus_" + str(i) + "_tau_" + str(tau) + ".pkl"
# with open(monitor_stored_path, 'wb') as f:
# pickle.dump(monitor_y_i, f)
def monitor_construction_from_features(features, taus, clustering_results, class_name, monitor_saving_folder):
# if os.path.exists(clustering_result_path):
# clustering_results = pd.read_csv(clustering_result_path)
# else:
# raise RuntimeError("Please partition your data first!")
for tau in taus:
loc_boxes = []
if len(features):
n_dim = features.shape[1]
# load clustering result related to tau
clustering_result = clustering_results[str(tau)]
# determine the labels of clusters
num_clusters = np.amax(clustering_result) + 1
clustering_labels = np.arange(num_clusters)
# extract the indices of vectors in a cluster
clusters_indices = []
for k in clustering_labels:
indices_cluster_k, = np.where(clustering_result == k)
clusters_indices.append(indices_cluster_k)
# creat local box for each cluster
loc_boxes = [Box() for i in clustering_labels]
for j in range(len(loc_boxes)):
points_j = [(i, features[i]) for i in clusters_indices[j]]
loc_boxes[j].build(n_dim, points_j)
else:
raise RuntimeError("There exists no feature for building monitor!!")
# creat the monitor for class y at layer i
monitor = Monitor(good_ref=loc_boxes)
# save the created monitor
if not os.path.exists(monitor_saving_folder):
os.makedirs(monitor_saving_folder)
monitor_saving_path = monitor_saving_folder + "monitor_for_clustering_parameter" + "_tau_" + str(tau) + ".pkl"
with open(monitor_saving_path, 'wb') as f:
pickle.dump(monitor, f)
123from .Clustering import *
from .Monitor_construction import *
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061from util import *from sklearn.cluster import KMeans, SpectralClustering, DBSCANfrom sklearn import metricsimport numpy as npimport warningsimport osfrom util.Monitor_construction import Boxfrom runtime_monitors import *def k_means_cluster(data, n_clusters): kmeans = KMeans(n_clusters=n_clusters, init='k-means++', random_state=0, n_init="auto") kmeans.fit_predict(data)
lbs = kmeans.labels_ # cluster labels clusters = dict() for lb in set(lbs): idx = np.where(lbs == lb)[0] clusters[lb] = list(zip(idx, data[idx])) return clusters
def spectral_cluster(data, n_clusters): n_neighbors = min(n_clusters, 10) spectral = SpectralClustering(n_clusters=n_clusters, affinity='nearest_neighbors', n_neighbors=n_neighbors, gamma=1.0, eigen_solver="arpack", random_state=0) # catch warnings related to kneighbors_graph with warnings.catch_warnings(): warnings.filterwarnings( "ignore", message="the number of connected components of the " + "connectivity matrix is [0-9]{1,2}" + " > 1. Completing it to avoid stopping the tree early.", category=UserWarning, ) warnings.filterwarnings( "ignore", message="Graph is not fully connected, spectral embedding" + " may not work as expected.", category=UserWarning, ) spectral = spectral.fit(data)
lbs = spectral.labels_ # cluster labels clusters = dict() for lb in set(lbs): idx = np.where(lbs == lb)[0] clusters[lb] = list(zip(idx, data[idx])) return clusters
def dbscan_cluster(data, eps, min_samples): db = DBSCAN(eps=eps, min_samples=min_samples).fit(data) lbs = db.labels_ # cluster labels n_cls = len(set(lbs)) - (1 if -1 in lbs else 0) # number of clusters n_noise = list(lbs).count(-1)
clusters = dict() for lb in set(lbs): idx = np.where(lbs == lb)[0] clusters[lb] = list(zip(idx, data[idx])) return clusters123version https://git-lfs.github.com/spec/v1oid sha256:e0a450635f32e07fe9ff5a4a04dbda1c8188984fb5b2443ff117dc53d7df778dsize 53633470123version https://git-lfs.github.com/spec/v1oid sha256:0d18d1c5d2f94d059979bce4c1f812707cc3539f798d630b5fc8de2f7e7c80b6size 45707710123version https://git-lfs.github.com/spec/v1oid sha256:636fc37b49df78c5670ff65e0138640cf873985c06df15fc2fa918b256b39901size 8860091123version https://git-lfs.github.com/spec/v1oid sha256:1d7ec3e6034d0c7164f50aab06397cb19176ef5466f766b53d0e3099369ef82asize 31691197123version https://git-lfs.github.com/spec/v1oid sha256:4c6e2edb71aa70fdc42e49db901e7fbf3143d98c060c7d9c8ef8eb543be05b6fsize 45461950.gitignoreAdded+2
README.md+56-13
abstractions/Box.pyAdded+104
abstractions/__init__.pyAdded+1
base_cam.pyAdded+223
detectron2monitor.pyAdded+231
enlarge.pyAdded+320
interface.pyAdded+95
interface_tabbed.pyAdded+155
models/configs/Base-RCNN-FPN.yamlAdded+50
models/configs/vanilla_regnet.yamlAdded+33
models/configs/vanilla_resnet.yamlAdded+28
models/metadata.pyAdded+35
models/regnet.pyAdded+84
models/regnet_model.pyAdded+446
models/weights/model_final_resnet_kitti.pthAdded+3
monitors/kitti/resnet/kmeans/1.pklAdded+3
monitors/kitti/resnet/kmeans/4.pklAdded+3
monitors/kitti/resnet/kmeans/5.pklAdded+3
monitors/kitti/resnet/kmeans/6.pklAdded+3
requirements.txtAdded+12
runtime_monitors/Monitor.pyAdded+56
runtime_monitors/__init__.pyAdded+2
train_feats/kitti/resnet/kitti-train_feats_tp_dict.pickleAdded+3
util/Clustering.pyAdded+138
util/Monitor_construction.pyAdded+144
util/__init__.pyAdded+3
utils_clustering.pyAdded+61
val_feats/kitti/resnet/ID-bdd-OOD-coco_feats_fp_dict.pickleAdded+3
val_feats/kitti/resnet/OOD-open_feats_fp_dict.pickleAdded+3
val_feats/kitti/resnet/kitti-val_feats_fp_dict.pickleAdded+3
val_feats/kitti/resnet/kitti-val_feats_tp_dict.pickleAdded+3
val_feats/kitti/resnet/voc-ood_feats_fp_dict.pickleAdded+3