Closed varunnrao closed 3 years ago
PyTorch is not deterministic: https://pytorch.org/docs/stable/notes/randomness.html. Therefore there is probably not much we can do, unless there is evidence that detectron2 introduces more non-determinism than what's in pytorch.
I don't think it's the pytorch non determinism causing the issue here. I am using a pretrained detectron2 masked cascade faster rcnn model for inference.
It doesn't seem right that the outputs differ at the RPN stage but it's reproducible after the RoI heads.
Hence I feel there is possibly some non determinism introduced by detectron2. Could you please have another look at the code, in case there is some obvious mistakes in the pre/post processing or model loading?
Could you please reopen this issue? It doesn't seem resolved. Thanks.
Instructions To Reproduce the Issue:
cfg = get_cfg() cfg.merge_from_file(CONFIG_PATH) cfg.MODEL.WEIGHTS = MODEL_PATH model = build_model(cfg) model.eval() transform_gen = T.ResizeShortestEdge( [cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST], cfg.INPUT.MAX_SIZE_TEST ) input_format = cfg.INPUT.FORMAT device = torch.device(cfg.MODEL.DEVICE) num_channels = len(cfg.MODEL.PIXEL_MEAN) pixel_mean = torch.Tensor(cfg.MODEL.PIXEL_MEAN).view(num_channels, 1, 1) pixel_std = torch.Tensor(cfg.MODEL.PIXEL_STD).view(num_channels, 1, 1) normalizer = lambda x: (x - pixel_mean) / pixel_std
img = cv2.imread('image0008.jpg') plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
bbox_list = [] logit_list = [] if(input_format == "RGB"):
whether the model expects BGR inputs or RGB
input_height, input_width = img.shape[:2] img_transform = transform_gen.get_transform(img).apply_image(img) img_tensor = torch.as_tensor(img_transform.astype("float32").transpose(2, 0, 1)) img_tensor = normalizer(img_tensor) img_gpu = img_tensor.to(device)
can be a list of images;
img_list = ImageList.from_tensors([img_gpu], model.backbone.size_divisibility)
features = model.backbone(img_list.tensor)
refer https://detectron2.readthedocs.io/tutorials/models.html#model-output-format
for output format of proposal generator
returns a list of proposals;
proposals, _ = model.proposal_generator(img_list, features) proposal_boxes = proposals[0].proposal_boxes scale_x, scale_y = (input_width / proposals[0].image_size[1], input_height / proposals[0].image_size[0]) proposal_boxes.scale(scale_x, scale_y)
note output format is in [XYXY] even though input may be in [XYXY] or [XYWH]
objectness_logits = proposals[0].objectness_logits
for bbox, logit in zip(proposal_boxes, objectness_logits): bbox, logit = bbox.cpu().numpy(), logit.cpu().numpy().item() x, y, w, h = int(bbox[0]), int(bbox[1]), int(bbox[2]) - int(bbox[0]) + 1, int(bbox[3]) - int(bbox[1]) + 1 plt.gca().add_patch(Rectangle((x,y),w,h,linewidth=1,edgecolor='r',facecolor='none')) bbox_list.append([x,y,w,h]) logit_list.append(logit) plt.show()
wget -nc -q https://github.com/facebookresearch/detectron2/raw/master/detectron2/utils/collect_env.py && python collect_env.py
wget: /home/ubuntu/anaconda3/lib/libuuid.so.1: no version information available (required by wget)
PyTorch built with: