def __call__(self, detections): batches_scores, batches_boxes = detections device = batches_scores.device batch_size = batches_scores.size(0) results = [] # print("PostProcessor:detections: ", detections) for batch_id in range(batch_size): processed_boxes = [] processed_scores = [] processed_labels = [] per_img_scores, per_img_boxes = batches_scores[ batch_id], batches_boxes[batch_id] # (N, #CLS) (N, 4) # print("PostProcessor:per_img_boxes: ", per_img_boxes) for class_id in range(1, per_img_scores.size(1)): # skip background scores = per_img_scores[:, class_id] mask = scores > self.cfg.TEST.CONFIDENCE_THRESHOLD scores = scores[mask] if scores.size(0) == 0: continue boxes = per_img_boxes[mask, :] boxes[:, 0::2] *= self.width boxes[:, 1::2] *= self.height keep = boxes_nms(boxes, scores, self.cfg.TEST.NMS_THRESHOLD, self.cfg.TEST.MAX_PER_CLASS) nmsed_boxes = boxes[keep, :] nmsed_labels = torch.tensor([class_id] * keep.size(0), device=device) nmsed_scores = scores[keep] processed_boxes.append(nmsed_boxes) processed_scores.append(nmsed_scores) processed_labels.append(nmsed_labels) if len(processed_boxes) == 0: processed_boxes = torch.empty(0, 4) processed_labels = torch.empty(0) processed_scores = torch.empty(0) else: processed_boxes = torch.cat(processed_boxes, 0) processed_labels = torch.cat(processed_labels, 0) processed_scores = torch.cat(processed_scores, 0) if processed_boxes.size(0) > self.cfg.TEST.MAX_PER_IMAGE > 0: processed_scores, keep = torch.topk( processed_scores, k=self.cfg.TEST.MAX_PER_IMAGE) processed_boxes = processed_boxes[keep, :] processed_labels = processed_labels[keep] # print("PostProcessor:boxes: ", boxes) container = Container(boxes=processed_boxes, labels=processed_labels, scores=processed_scores) container.img_width = self.width container.img_height = self.height results.append(container) return results
def __getitem__(self, index): image = self._read_image(index) height, width, _ = image.shape cv_image = image.copy() boxes, labels, is_difficult = self._get_annotation(index) # For display purpose for box in boxes: cv2.rectangle(cv_image, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) #print(self.root_path + "/box/" + self.images[image_id]) #cv2.imwrite(self.root_path + "/box/" + self.images[image_id], write_img) if self.transform: image, boxes, labels = self.transform(image, boxes, labels) if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) # Legacy code for plotting priors # plot_boxes = boxes.clone() # plot_boxes = box_utils.convert_locations_to_boxes(plot_boxes, config.priors, config.center_variance, # config.size_variance) # plot_priors = config.priors[labels.nonzero()] # plot_priors = box_utils.center_form_to_corner_form(plot_priors) # #plot_priors = plot_boxes[labels.nonzero()] # plot_priors = np.array(plot_priors.data).squeeze() # print(len(plot_priors.shape)) # if len(plot_priors.shape) == 1: # xmin, ymin, xmax, ymax = plot_priors[0], plot_priors[1], plot_priors[2], plot_priors[3] # cv2.rectangle(cv_image, (int(xmin * width), int(ymin * height)), (int(xmax * width), int(ymax * height)), # (0, 0, 255), 2) # else: # for box in plot_priors: # xmin, ymin, xmax, ymax = box[0], box[1], box[2], box[3] # cv2.rectangle(cv_image, (int(xmin * width), int(ymin * height)), (int(xmax * width), int(ymax * height)), (0, 0, 255), 2) # here we should plot the associated priors to the actual object #print(labels.nonzero()) #cv_img = image.numpy() #cv_img = np.transpose(cv_img, (1,2,0)) #cv2.imwrite("images/" + str(index) + ".jpg", cv_img) #print(image.shape) #cv2.imshow("Imagine", cv_image) #cv2.waitKey(0) #cv2.destroyAllWindows() #cv.imwrite(self.root_path + "/priors/" + self.images[self._get_image_id(index)], cv_image) return image, targets, index
def __call__(self, detections): uncontainered_results = self._call(detections) results = [] for boxes, scores, labels in uncontainered_results: container = Container(boxes=boxes, labels=labels, scores=scores) container.img_width = self.width container.img_height = self.height results.append(container) return results
def __getitem__(self, index): boxes, labels, is_difficult = self._get_annotation(index) image = self._read_image(index) if self.transform: image, boxes, labels = self.transform(image, boxes, labels) if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) return image, targets, index
def __call__(self, detections): batches_scores, batches_boxes = detections device = batches_scores.device batch_size = batches_scores.size(0) results = [] for batch_id in range(batch_size): scores, boxes = batches_scores[batch_id], batches_boxes[ batch_id] # (N, #CLS) (N, 4) num_boxes = scores.shape[0] num_classes = scores.shape[1] boxes = boxes.view(num_boxes, 1, 4).expand(num_boxes, num_classes, 4) labels = torch.arange(num_classes, device=device) labels = labels.view(1, num_classes).expand_as(scores) # remove predictions with the background label if self.cls_loss != 'FocalLoss': boxes = boxes[:, 1:] scores = scores[:, 1:] labels = labels[:, 1:] # batch everything, by making every class prediction be a separate instance boxes = boxes.reshape(-1, 4) scores = scores.reshape(-1) labels = labels.reshape(-1) # remove low scoring boxes indices = torch.nonzero( scores > self.cfg.TEST.CONFIDENCE_THRESHOLD).squeeze(1) boxes, scores, labels = boxes[indices], scores[indices], labels[ indices] boxes[:, 0::2] *= self.width boxes[:, 1::2] *= self.height keep = batched_nms(boxes, scores, labels, self.cfg.TEST.NMS_THRESHOLD) # keep only topk scoring predictions keep = keep[:self.cfg.TEST.MAX_PER_IMAGE] if self.cls_loss == 'FocalLoss': boxes, scores, labels = boxes[keep], scores[ keep], labels[keep] + 1 else: boxes, scores, labels = boxes[keep], scores[keep], labels[keep] container = Container(boxes=boxes, labels=labels, scores=scores) container.img_width = self.width container.img_height = self.height results.append(container) return results
def __call__(self, batch): transposed_batch = list(zip(*batch)) images = default_collate(transposed_batch[0]) img_ids = default_collate(transposed_batch[2]) if self.is_train: list_targets = transposed_batch[1] targets = Container( {key: default_collate([d[key] for d in list_targets]) for key in list_targets[0]} ) else: targets = None return images, targets, img_ids
def __getitem__(self, index): image = Image.open(self.image_files[index]).convert("RGB") image = np.array(image) boxes, labels = self._get_annotation(index) if self.transform: image, boxes, labels = self.transform(image, boxes, labels) if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) return image, targets, index
def __getitem__(self, index): image_id = self.ids[index] # print(image_id) boxes, labels = self._get_annotation(image_id) image = self._read_image(image_id) if self.transform: image, boxes, labels = self.transform(image, boxes, labels) if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) return image, targets, index
def __getitem__(self, index): image_id = self.ids[index] boxes, labels, is_difficult = self._get_annotation(image_id) if not self.keep_difficult: boxes = boxes[is_difficult == 0] labels = labels[is_difficult == 0] image = self._read_image(image_id) if self.transform: image, boxes, labels = self.transform(image, boxes, labels) if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) return image, targets, index
def __getitem__(self, index): image_id = self.ids[index] boxes, labels = self._get_annotation(image_id) image = self._read_image(image_id) if self.transform: image, boxes, labels = self.transform(image, boxes, labels) # for Objectness GT +++++++++++++++++++ boxes_norm = boxes labels_norm = labels # for Objectness GT +++++++++++++++++++ if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) return image, targets, index, boxes_norm, labels_norm
def __getitem__(self, index): fn = self.imageFilenames[index] # load the image as a PIL Image image = self.readImage(fn) imgW, imgH = image.shape[1], image.shape[0] # Cache size for later in evaluation... # if fn not in self.cachedInfo: # imgInfo = {'width': imgW, 'height': imgH} # #print('caching info for fn = {}'.format(fn)) # self.cachedInfo[fn] = imgInfo boxes, labels = self._get_annotation(fn) # Clamp to image dimensions. boxes[:, 0] = clamp(boxes[:, 0], 0, imgW - 1) boxes[:, 1] = clamp(boxes[:, 1], 0, imgH - 1) boxes[:, 2] = clamp(boxes[:, 2], 0, imgW - 1) boxes[:, 3] = clamp(boxes[:, 3], 0, imgH - 1) for i in range(boxes.shape[0]): bb = boxes[i, :] assert np.all(inRange(bb[0], 0, imgW - 1)), 'corners= {}, img size= {}x{}'.format( bb, imgW, imgH) assert np.all(inRange(bb[2], 0, imgW - 1)), 'corners= {}, img size= {}x{}'.format( bb, imgW, imgH) assert np.all(inRange(bb[1], 0, imgH - 1)), 'corners= {}, img size= {}x{}'.format( bb, imgW, imgH) assert np.all(inRange(bb[3], 0, imgH - 1)), 'corners= {}, img size= {}x{}'.format( bb, imgW, imgH) if self.transform: image, boxes, labels = self.transform(image, boxes, labels) if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) # return the image, the targets and the index in your dataset return image, targets, index
def __getitem__(self, index): # load the image as a PIL Image image = self.generator.DrawRotatedRectangle( self.contours[ index ] ) # load the bounding boxes in x1, y1, x2, y2 order. boxes = np.array( [ self.boxes[ index ] ], dtype = np.float32 ) # and labels labels = np.array( [ [ 1 ] ], dtype=np.int64) if self.transform: image, boxes, labels = self.transform(image, boxes, labels) if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) # return the image, the targets and the index in your dataset return image, targets, index
def __call__(self, detections): batches_scores, batches_boxes = detections device = batches_scores.device batch_size = batches_scores.size(0) results = [] for batch_id in range(batch_size): scores, boxes = batches_scores[batch_id], batches_boxes[batch_id] num_boxes = scores.shape[0] num_classes = scores.shape[1] boxes = boxes.view(num_boxes, 1, 4).expand(num_boxes, num_classes, 4) labels = torch.arange(num_classes, device=device) labels = labels.view(1, num_classes).expand_as(scores) # remove predictions with the background label boxes = boxes[:, 1:] scores = scores[:, 1:] labels = labels[:, 1:] boxes = boxes.reshape(-1, 4) scores = scores.reshape(-1) labels = labels.reshape(-1) # remove low scoring boxes indices = torch.nonzero(scores > self.cfg.TEST.CONFIDENCE_THRESHOLD).squeeze(1) boxes, scores, labels = boxes[indices], scores[indices], labels[indices] boxes[:, 0::2] *= self.width boxes[:, 1::2] *= self.height keep = nms(boxes,scores,self.cfg.TEST.NMS_THRESHOLD) keep = keep[:self.cfg.TEST.MAX_PER_IMAGE] boxes, scores, labels = boxes[keep], scores[keep], labels[keep] container = Container(boxes=boxes, labels=labels, scores=scores) container.img_width = self.width container.img_height = self.height results.append(container) return results
def __getitem__(self, index): img_name = self.anno_list[index].split('.')[0] + '.jpg' img_path = os.path.join(self.img_dir, img_name) image = self._read_image(img_path) boxes, labels, is_difficult = self._get_annotation(index) # load the bounding boxes in x1, y1, x2, y2 order. # boxes = np.array((N, 4), dtype=np.float32) # and labels # labels = np.array((N, ), dtype=np.int64) if not self.keep_difficult: boxes = boxes[is_difficult == 0] labels = labels[is_difficult == 0] if self.transform: image, boxes, labels = self.transform(image, boxes, labels) if self.target_transform: boxes, labels = self.target_transform(boxes, labels) targets = Container( boxes=boxes, labels=labels, ) return image, targets, index
def __call__(self, detections): batches_scores, batches_boxes = detections batchb = len(batches_boxes) #print("batch boxes: %d" % (batchb)) device = batches_scores.device batch_size = batches_scores.size(0) results = [] for batch_id in range(batch_size): scores, boxes = batches_scores[batch_id], batches_boxes[ batch_id] # (N, #CLS) (N, 4) num_boxes = scores.shape[0] num_classes = scores.shape[1] #print(num_boxes) boxes = boxes.view(num_boxes, 1, 4).expand(num_boxes, num_classes, 4) labels = torch.arange(num_classes, device=device) labels = labels.view(1, num_classes).expand_as(scores) _t = {'nms': Timer()} _t['nms'].tic() # remove predictions with the background label boxes = boxes[:, 1:] scores = scores[:, 1:] labels = labels[:, 1:] # batch everything, by making every class prediction be a separate instance boxes = boxes.reshape(-1, 4) scores = scores.reshape(-1) labels = labels.reshape(-1) # remove low scoring boxes indices = torch.nonzero( scores > self.cfg.TEST.CONFIDENCE_THRESHOLD).squeeze(1) boxes, scores, labels = boxes[indices], scores[indices], labels[ indices] boxes[:, 0::2] *= self.width boxes[:, 1::2] *= self.height bb = len(boxes) #print("before boxes: %d" % (bb)) keep = batched_nms(boxes, scores, labels, self.cfg.TEST.NMS_THRESHOLD) #keep = nms(boxes, scores,self.cfg.TEST.NMS_THRESHOLD) # keep only topk scoring predictions keep = keep[:self.cfg.TEST.MAX_PER_IMAGE] boxes, scores, labels = boxes[keep], scores[keep], labels[keep] ab = len(boxes) #print("after boxes: %d" % (ab)) #print(ab) container = Container(boxes=boxes, labels=labels, scores=scores) container.img_width = self.width container.img_height = self.height results.append(container) nms_time = _t['nms'].toc() #print("nms time: %.2f ms" % (1000*nms_time)) print((1000 * nms_time)) return results