コード例 #1
0
def main():
    logdir = os.path.dirname(args.seg_dir)
    log_filename = os.path.join(logdir, 'corloc_eval_%s.log' % datetime.now())
    logging.basicConfig(filename=log_filename, level=logging.DEBUG)
    tp = np.zeros(len(pascal_classes))
    gt = np.zeros(len(pascal_classes))

    image_list = get_list_from_file(args.image_list)
    for image in image_list:
        _, img_labels, bboxes = get_annotations_from_xml(
            os.path.join(args.annotation_dir, '%s.xml' % image),
            pascal_classes)
        gt += np.maximum(img_labels, 0)
        seg_mask = cv2.imread(os.path.join(args.seg_dir, '%s.png' % image))
        seg_mask = seg_mask[:, :, 0]
        seg_mask = seg_mask.astype(np.int32)
        for i in range(1, len(pascal_classes)):
            if img_labels[i] > 0:
                mask = (seg_mask == i)
                if np.sum(mask) > 0:
                    regionmap = label(mask)
                    region_prop = regionprops(regionmap)
                    region_area = np.array([r.area for r in region_prop])
                    best_region_idx = np.argmax(region_area)
                    bbox = np.array(region_prop[best_region_idx].bbox)
                    bbox = np.expand_dims(bbox, 0)
                    gt_bbox = bboxes[bboxes[:, 0] == i][:, 1:]
                    iou = get_iou_matrix(gt_bbox, bbox)
                    if np.amax(iou > 0.5):
                        tp[i] += 1
    for i in range(1, len(pascal_classes)):
        logging.info('%s: %.3f' % (pascal_classes[i], tp[i] / gt[i]))
    logging.info('CorLoc: %.3f' % (np.sum(tp[1:] / gt[1:]) /
                                   (len(pascal_classes) - 1)))
    logging.info('Instance correct rate: %d/%d' % (np.sum(tp), np.sum(gt)))
コード例 #2
0
ファイル: eval_heatmap.py プロジェクト: siyangl/WSOD
def main():
  logdir = os.path.dirname(args.heatmap_dir)
  log_filename = os.path.join(logdir, 'pointing_eval_%s.log' % datetime.now())
  logging.basicConfig(filename=log_filename, level=logging.DEBUG)
  logging.info('Folder to be evaluated: %s'%args.heatmap_dir)
  tp = np.zeros(len(pascal_classes))
  gt = np.zeros(len(pascal_classes))

  image_list = get_list_from_file(args.image_list)
  for image in image_list:
    img_size, img_labels, bboxes = get_annotations_from_xml(os.path.join(args.annotation_dir, '%s.xml'%image),
                                                     pascal_classes)
    gt += np.maximum(img_labels, 0)
    for i in range(1, len(pascal_classes)):
      if img_labels[i] > 0:
        heatmap = np.load(os.path.join(args.heatmap_dir, '%s_%s.npy'%(image, pascal_classes[i])))
        heatmap = cv2.resize(heatmap, (img_size[1], img_size[0]))
        global_argmax = np.where(heatmap==np.amax(heatmap))
        hmax = global_argmax[0]
        wmax = global_argmax[1]
        point = np.concatenate([hmax, wmax]) + 1
        gt_bbox = bboxes[bboxes[:, 0] == i][:, 1:]
        is_in = point_in_box(point, gt_bbox)
        if np.amax(is_in):
          tp[i] += 1

  for i in range(1, len(pascal_classes)):
    logging.info('%s: %.3f'%(pascal_classes[i], tp[i]/gt[i]))
  logging.info('CorLoc: %.3f'%(np.sum(tp[1:]/gt[1:])/(len(pascal_classes) - 1)))
  logging.info('Instance correct rate: %d/%d'%(np.sum(tp), np.sum(gt)))
コード例 #3
0
ファイル: file_utils_test.py プロジェクト: siyangl/WSOD
def test_get_label_and_objects_from_xml():
    xmlpath = '%s/data/utils/test_xml.xml' % ROOT_DIR
    pascal_classes = [
        '__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle',
        'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',
        'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',
        'tvmonitor'
    ]
    size, labels, objects = file_utils.get_annotations_from_xml(
        xmlpath, pascal_classes)
    assert np.array_equal(size, np.array([375, 500, 3]))
    assert np.array_equal(
        labels,
        np.array(
            [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0]))
    assert np.array_equal(
        objects,
        np.array([[9, 211, 263, 339, 324], [7, 264, 165, 372, 253],
                  [-9, 244, 5, 374, 67], [9, 194, 241, 299, 295],
                  [-15, 186, 277, 220, 312]]))
コード例 #4
0
ファイル: get_attention_map.py プロジェクト: siyangl/WSOD
def main(unused):
    net_CAM = caffe.Net(
        os.path.join(cfg.root_dir, 'SEC', 'deploy.prototxt'),
        os.path.join(cfg.root_dir, 'SEC', 'weights.caffemodel'), caffe.TEST)
    data_dir = getattr(data.cfg, '%s_dir' % args.dataset)
    filelist = get_list_from_file(
        os.path.join(data_dir, 'ImageSets', 'Main', '%s.txt' % args.image_set))
    print 'Num of images', len(filelist)

    for file in filelist:
        image = pylab.imread(
            os.path.join(data_dir, 'JPEGImages', '%s.jpg' % file))
        H = image.shape[0]
        W = image.shape[1]
        _, labels, _ = get_annotations_from_xml(
            os.path.join(data_dir, 'Annotations', '%s.xml' % file),
            data.cfg.pascal_classes)
        if np.sum(np.absolute(labels[1:])) > 0:
            print file

            net_CAM.blobs['images'].data[...][0] = preprocess(image, 321.0)
            net_CAM.forward()

            CAM_scores = net_CAM.blobs['fc7_CAM'].data[0]
            params = net_CAM.params['scores'][0].data[...]

            for i in range(1, 21):
                if labels[i] != 0:
                    w = params[i - 1]
                    heat_maps = np.sum(CAM_scores * w[:, None, None], axis=0)
                    if args.result_dir:
                        np.save(
                            '%s/%s_%s' % (args.result_dir, file,
                                          data.cfg.pascal_classes[i]),
                            heat_maps)
                    if args.vis_dir:
                        vis = visualize_heatmap(np.maximum(heat_maps, 0))
                        cv2.imwrite(
                            '%s/%s_%s.png' %
                            (args.vis_dir, file, data.cfg.pascal_classes[i]),
                            vis)
コード例 #5
0
ファイル: label_co_occur.py プロジェクト: siyangl/WSOD
def main(unused):
    category_list = file_utils.get_list_from_file(FLAGS.category_list_file)
    num_categories = len(category_list)
    co_occur_freq = np.zeros((num_categories, num_categories))
    category_freq = np.zeros((num_categories))
    image_list = file_utils.get_list_from_file(FLAGS.image_list)
    for image in image_list:
        xml_path = os.path.join(FLAGS.annotation_dir, '%s.xml' % image)
        _, labels, objs = file_utils.get_annotations_from_xml(
            xml_path, category_list)
        obj_labels = np.unique(np.absolute(objs[:, 0]))
        labels = np.absolute(labels)
        if obj_labels.shape[0] == 1:
            co_occur_freq[obj_labels[0], obj_labels[0]] += 1
        for l in range(0, obj_labels.shape[0]):
            for k in range(l + 1, obj_labels.shape[0]):
                co_occur_freq[obj_labels[l], obj_labels[k]] += 1
                co_occur_freq[obj_labels[k], obj_labels[l]] += 1
        category_freq += labels

    co_occur_prob = co_occur_freq / np.expand_dims(category_freq, 1)
    if FLAGS.save_dir:
        np.save(os.path.join(FLAGS.save_dir, 'pascal_co_occur'), co_occur_prob)
コード例 #6
0
ファイル: eval_seedmap.py プロジェクト: siyangl/WSOD
def main():
  logdir = os.path.dirname(args.seg_dir)
  log_filename = os.path.join(logdir, 'seed_eval_%s.log' % datetime.now())
  logging.basicConfig(filename=log_filename, level=logging.DEBUG)
  logging.info('Folder to be evaluated: %s, %s'%(args.seg_dir, args.seedmask_dir))
  tp = np.zeros(len(pascal_classes))
  gt = np.zeros(len(pascal_classes))
  pd = np.zeros(len(pascal_classes))

  image_list = get_list_from_file(args.image_list)
  for image in image_list:
    img_size, img_labels, bboxes = get_annotations_from_xml(os.path.join(args.annotation_dir, '%s.xml'%image),
                                                            pascal_classes)
    fg_map = np.zeros(img_size, dtype=np.bool)
    for i in range(1, len(pascal_classes)):
      if img_labels[i] > 0:
        seg = cv2.imread(os.path.join(args.seg_dir, '%s_%s.png'%(image, pascal_classes[i])))
        seedmask = cv2.imread(os.path.join(args.seedmask_dir, '%s_%s.png'%(image, pascal_classes[i])))
        cue = np.logical_and((seg == i), (seedmask==1))
        gt_bbox = bboxes[np.absolute(bboxes[:, 0]) == i][:, 1:]
        box_map = boxes_to_seg(gt_bbox, img_size)
        gt[i] += np.sum(box_map)
        tp[i] += np.sum(np.logical_and(box_map, cue))
        pd[i] += np.sum(cue)
        fg_map = np.logical_or(fg_map, box_map)

    bg_cue = np.logical_and((seg == 0), (seedmask==1))
    bg_map = np.logical_not(fg_map)
    gt[0] += np.sum(bg_map)
    tp[0] += np.sum(np.logical_and(bg_map, bg_cue))
    pd[0] += np.sum(bg_cue)

  for i in range(len(pascal_classes)):
    logging.info('%s: recall: %.3f, precision: %.3f'%(pascal_classes[i], tp[i]/gt[i], tp[i]/pd[i]))
  logging.info('Average recall: %.3f'%(np.sum(tp[1:]/gt[1:])/(len(pascal_classes) - 1)))
  logging.info('Average precision: %.3f'%(np.sum(tp[1:]/pd[1:])/(len(pascal_classes) - 1)))
コード例 #7
0
def _find_image_files(data_dir, annotation_dir, image_list_file, category_list_file):
  """Build a list of all images files and labels in the data set.

  Args:
    data_dir: string, path to the root directory of images.
    annotation_dir: path to annotation files
    image_list_file: a text file listing all image file names
    category_list_file: a text file listing all possible classes for this dataset

  Returns:
    filenames: list of strings; each string is a path to an image file.
    labels: list of vector; each vector identifies the ground truth.
    roi_labels: list of vector, identifying one class of interest
    obj_labels: list of array, instance-level annotation
    obj_bboxes: list of array, instance-level box annotation
    proposals: regions of interest (for the class of interest if roi_labels are valid,
      else for all exsiting categories
  """

  labels = []
  filenames = []
  proposals = []
  roi_labels = []
  obj_labels = []
  obj_bboxes = []
  filename_list = file_utils.get_list_from_file(image_list_file)
  class_list = file_utils.get_list_from_file(category_list_file)
  print(len(filename_list))

  # Construct the list of JPEG files and labels.
  for filename in filename_list:
    jpeg_file_path = '%s/%s.jpg' % (data_dir, filename)
    # read .xml label file
    xml_path = '%s/%s.xml' % (annotation_dir, filename)
    img_size, label, objects = file_utils.get_annotations_from_xml(xml_path, class_list)
    bbox = objects[:, 1:]
    obj_label = objects[:, 0]

    if FLAGS.normalize_bbox:
      bbox = box_utils.bbox_normalize(bbox, img_size[0:2], one_index=True)

    # get bbox
    if FLAGS.roi_dir:
      roi_dir = os.path.join(FLAGS.roi_dir, '%s.mat' % filename)
      roi_contents = sio.loadmat(roi_dir)
      if not 'boxes' in roi_contents:  # category-dependent proposals
        file_duplicates = 0
        for key in roi_contents.keys():
          if not key.startswith('__'):
            label_index = class_list.index(key)
            if label[label_index] == -1:
              continue
            roi_label = np.zeros(len(class_list), dtype=np.int64)
            roi_label[label_index] = 1
            proposal = roi_contents[key]
            assert proposal.shape[1] == 4
            if FLAGS.normalize_bbox:
              proposal = box_utils.bbox_normalize(proposal, img_size[0:2], one_index=True)
            file_duplicates += 1
            proposals.append(proposal)
            roi_labels.append(roi_label)
      else:  # category-independent proposals
        file_duplicates = 1
        proposal = roi_contents['boxes']
        assert proposal.shape[1] == 4
        if FLAGS.normalize_bbox:
          proposal = box_utils.bbox_normalize(proposal, img_size[0:2], one_index=True)
        proposals.append(proposal)
        roi_labels.append(label)
    else:
      file_duplicates = 1
      proposals.append(np.array([[-1, -1, -1, -1]]))
      roi_labels.append(label)

    labels.extend([label] * file_duplicates)
    filenames.extend([jpeg_file_path]*file_duplicates)
    obj_labels.extend([obj_label]*file_duplicates)
    obj_bboxes.extend([bbox]*file_duplicates)

  print(len(labels))
  print(len(filenames))
  print(len(obj_labels))
  shuffled_index = range(len(filenames))
  random.seed(12345)
  random.shuffle(shuffled_index)

  filenames = [filenames[i] for i in shuffled_index]
  labels = [labels[i] for i in shuffled_index]
  roi_labels = [roi_labels[i] for i in shuffled_index]
  proposals = [proposals[i] for i in shuffled_index]
  obj_labels = [obj_labels[i] for i in shuffled_index]
  obj_bboxes = [obj_bboxes[i] for i in shuffled_index]

  # print(filenames[0])
  # print(labels[0])
  # print(roi_labels[0])
  # print(obj_bboxes[0])
  # print(obj_labels[0])
  return filenames, labels, roi_labels, proposals, obj_labels, obj_bboxes
コード例 #8
0
def _find_image_files(data_dir, annotation_dir, image_list_file,
                      category_list_file, seg_dir, seg_mask_dir):
    """Build a list of all images files and labels in the data set."""

    labels = []
    filenames = []
    seg_files = []
    roi_labels = []
    seg_mask_files = []
    filename_list = file_utils.get_list_from_file(image_list_file)
    class_list = file_utils.get_list_from_file(category_list_file)
    print(len(filename_list))

    # Construct the list of JPEG files and labels.
    for filename in filename_list:
        jpeg_file_path = '%s/%s.jpg' % (data_dir, filename)
        # read .xml label file
        xml_path = '%s/%s.xml' % (annotation_dir, filename)
        img_size, label, _ = file_utils.get_annotations_from_xml(
            xml_path, class_list)

        # get segmentation
        if FLAGS.one_hot:
            file_duplicates = 0
            for i in range(1, len(class_list)):
                cls = class_list[i]
                if label[i] > 0:
                    file_duplicates += 1
                    roi_label = np.zeros(len(class_list), dtype=np.int64)
                    roi_label[i] = 1
                    seg_file = os.path.join(seg_dir,
                                            '%s_%s.png' % (filename, cls))
                    seg_mask_file = os.path.join(seg_mask_dir,
                                                 '%s_%s.png' % (filename, cls))
                    seg_files.append(seg_file)
                    seg_mask_files.append(seg_mask_file)
                    roi_labels.append(roi_label)
        else:
            file_duplicates = 1
            roi_label = label
            seg_file = os.path.join(seg_dir, '%s.png' % (filename))
            seg_mask_file = os.path.join(seg_mask_dir, '%s.png' % (filename))
            seg_files.append(seg_file)
            seg_mask_files.append(seg_mask_file)
            roi_labels.append(roi_label)

        labels.extend([label] * file_duplicates)
        filenames.extend([jpeg_file_path] * file_duplicates)

    # Shuffle the ordering of all image files in order to guarantee
    # random ordering of the images with respect to label in the
    # saved TFRecord files. Make the randomization repeatable.
    print(len(labels))
    print(len(filenames))
    print(len(seg_files))
    print(len(seg_mask_files))

    shuffled_index = range(len(filenames))
    random.seed(12345)
    random.shuffle(shuffled_index)

    filenames = [filenames[i] for i in shuffled_index]
    labels = [labels[i] for i in shuffled_index]
    roi_labels = [roi_labels[i] for i in shuffled_index]
    seg_files = [seg_files[i] for i in shuffled_index]
    seg_mask_files = [seg_mask_files[i] for i in shuffled_index]

    # print(filenames[0])
    # print(labels[0])
    # print(roi_labels[0])
    # print(obj_bboxes[0])
    # print(obj_labels[0])
    # return
    return filenames, labels, roi_labels, seg_files, seg_mask_files