Beispiel #1
0
def fuse_radio_ir_4_pred(radio_fn, ir_fn, out_dir='/tmp', model='D4'):
    """
    return the full path to the generated fused file
    """
    if (model != 'D5'):
        nsz = None
        if ('D3' == model):
            mask_ir = False
        else:
            mask_ir = True
    else:
        nsz = cfg.TEST.SCALES[0]  #i.e. 600
        mask_ir = True
    return fuse(radio_fn, ir_fn, out_dir, new_size=nsz, mask_ir=mask_ir)
Beispiel #2
0
def demo(sess,
         net,
         im_file,
         vis_file,
         fits_fn,
         conf_thresh=0.8,
         eval_class=True):
    """
    Detect object classes in an image using pre-computed object proposals.
    im_file:    The "fused" image file path
    vis_file:   The background image file on which detections are laid.
                Normallly, this is just the IR image file path
    fits_fn:    The FITS file path
    eval_class: True - use traditional per class-based evaluation style
                False - use per RoI-based evaluation

    """
    show_img_size = cfg.TEST.SCALES[0]
    if (not os.path.exists(im_file)):
        print(('%s cannot be found' % (im_file)))
        return -1
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    image_name = osp.basename(im_file)
    scores, boxes = im_detect(sess,
                              net,
                              im,
                              save_vis_dir=None,
                              img_name=os.path.splitext(image_name)[0])
    boxes *= float(show_img_size) / float(im.shape[0])
    timer.toc()
    sys.stdout.write('Done in {:.3f} secs'.format(timer.total_time))
    sys.stdout.flush()
    print(scores)

    im = cv2.imread(vis_file)

    my_dpi = 100
    fig = plt.figure()
    fig.set_size_inches(show_img_size / my_dpi, show_img_size / my_dpi)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)
    ax.set_xlim([0, show_img_size])
    ax.set_ylim([show_img_size, 0])
    #ax.set_aspect('equal')
    im = cv2.resize(im, (show_img_size, show_img_size))
    im = im[:, :, (2, 1, 0)]
    ax.imshow(im, aspect='equal')
    if (fits_fn is not None):
        patch_contour = fuse(fits_fn,
                             im,
                             None,
                             sigma_level=4,
                             mask_ir=False,
                             get_path_patch_only=True)
        ax.add_patch(patch_contour)
    NMS_THRESH = cfg.TEST.NMS  #cfg.TEST.RPN_NMS_THRESH # 0.3

    tt_vis = 0
    bbox_img = []
    bscore_img = []
    num_sources = 0
    #if (eval_class):
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis]))  #.astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        num_sources += vis_detections(im, cls, dets, ax, thresh=conf_thresh)
        #dets = np.hstack((dets, np.ones([dets.shape[0], 1]) * cls_ind))
        # if (dets.shape[0] > 0):
        #     bbox_img.append(dets)
        #     bscore_img.append(np.reshape(dets[:, -2], [-1, 1]))
    # else:
    #     for eoi_ind, eoi in enumerate(boxes):
    #         eoi_scores = scores[eoi_ind, 1:] # skip background
    #         cls_ind = np.argmax(eoi_scores) + 1 # add the background index back
    #         cls_boxes = boxes[eoi_ind, 4 * cls_ind : 4 * (cls_ind + 1)]
    #         cls_scores = scores[eoi_ind, cls_ind]
    #         dets = np.hstack((np.reshape(cls_boxes, [1, -1]),
    #                           np.reshape(cls_scores, [-1, 1])))#.astype(np.float32)
    #         dets = np.hstack((dets, np.ones([dets.shape[0], 1]) * cls_ind))
    #         bbox_img.append(dets)
    #         bscore_img.append(np.reshape(dets[:, -2], [-1, 1]))
    #
    # boxes_im = np.vstack(bbox_img)
    # scores_im = np.vstack(bscore_img)
    #
    # #if (not eval_class):
    # # a numpy float is a C double, so need to use float32
    # keep = nms(boxes_im[:, :-1].astype(np.float32), NMS_THRESH)
    # boxes_im = boxes_im[keep, :]
    # scores_im = scores_im[keep, :]
    #
    # keep_indices = range(boxes_im.shape[0])
    #num_sources = vis_detections(im, None, boxes_im[keep_indices, :], ax, thresh=conf_thresh)

    print((', found %d sources' % num_sources))
    return 0
Beispiel #3
0
def demo(sess,
         net,
         im_file,
         vis_file,
         fits_fn,
         conf_thresh=0.8,
         eval_class=True,
         extra_vis_png=False,
         plot=False):
    """
    Detect object classes in an image using pre-computed object proposals.
    im_file:    The "fused" image file path
    vis_file:   The background image file on which detections are laid.
                Normallly, this is just the IR image file path
    fits_fn:    The FITS file path
    eval_class: True - use traditional per class-based evaluation style
                False - use per RoI-based evaluation

    """
    show_img_size = cfg.TEST.SCALES[0]

    if (not os.path.exists(im_file)):
        print('%s cannot be found' % (im_file))

        return -1

    # Add ground truth values to dictionary
    im = cv2.imread(im_file)
    im_file_name = im_file[5:26]
    add_to_dict(im_file_name)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    image_name = osp.basename(im_file)
    scores, boxes = im_detect(sess,
                              net,
                              im,
                              save_vis_dir=None,
                              img_name=os.path.splitext(image_name)[0])
    boxes *= float(show_img_size) / float(im.shape[0])
    timer.toc()
    sys.stdout.write('Done in {:.3f} secs'.format(timer.total_time))
    sys.stdout.flush()

    im = cv2.imread(vis_file)

    my_dpi = 100
    fig = plt.figure()

    if plot:
        fig.set_size_inches(show_img_size / my_dpi * 2, show_img_size / my_dpi)
        fig.suptitle(im_file_name, fontsize=18)
        ax = fig.add_subplot(1, 2, 1)
    else:
        fig.set_size_inches(show_img_size / my_dpi, show_img_size / my_dpi)
        ax = plt.Axes(fig, [0., 0., 1., 1.])

    im = cv2.resize(im, (show_img_size, show_img_size))
    im = im[:, :, (2, 1, 0)]

    ax.axis('off')
    ax.set_axis_off()
    fig.add_axes(ax)
    ax.set_xlim([0, show_img_size])
    ax.set_ylim([show_img_size, 0])

    ax.imshow(im, aspect='equal')

    if ((fits_fn is not None) and (not extra_vis_png)):
        patch_contour = fuse(fits_fn,
                             im,
                             None,
                             sigma_level=4,
                             mask_ir=False,
                             get_path_patch_only=True)
        ax.add_patch(patch_contour)
    NMS_THRESH = cfg.TEST.NMS  #cfg.TEST.RPN_NMS_THRESH # 0.3

    tt_vis = 0
    bbox_img = []
    bscore_img = []
    num_sources = 0

    #if (eval_class):
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]

        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis]))  #.astype(np.float32)

        #get copy of dets for plotting purposes
        bboxs = np.empty(np.shape(dets))
        np.copyto(bboxs, dets)

        keep = nms(dets, NMS_THRESH)

        dets = dets[keep, :]

        if plot:
            num_sources += plt_detections(im,
                                          cls,
                                          dets,
                                          ax,
                                          bboxs,
                                          thresh=conf_thresh)
            fig.subplots_adjust(top=0.85)
        else:
            num_sources += vis_detections(im,
                                          cls,
                                          dets,
                                          ax,
                                          thresh=conf_thresh)

        #add_to_csv(scores)

        #dets = np.hstack((dets, np.ones([dets.shape[0], 1]) * cls_ind))
        # if (dets.shape[0] > 0):
        #     bbox_img.append(dets)
        #     bscore_img.append(np.reshape(dets[:, -2], [-1, 1]))
    # else:
    #     for eoi_ind, eoi in enumerate(boxes):
    #         eoi_scores = scores[eoi_ind, 1:] # skip background
    #         cls_ind = np.argmax(eoi_scores) + 1 # add the background index back
    #         cls_boxes = boxes[eoi_ind, 4 * cls_ind : 4 * (cls_ind + 1)]
    #         cls_scores = scores[eoi_ind, cls_ind]
    #         dets = np.hstack((np.reshape(cls_boxes, [1, -1]),
    #                           np.reshape(cls_scores, [-1, 1])))#.astype(np.float32)
    #         dets = np.hstack((dets, np.ones([dets.shape[0], 1]) * cls_ind))
    #         bbox_img.append(dets)
    #         bscore_img.append(np.reshape(dets[:, -2], [-1, 1]))
    #
    # boxes_im = np.vstack(bbox_img)
    # scores_im = np.vstack(bscore_img)
    #
    # #if (not eval_class):
    # # a numpy float is a C double, so need to use float32
    # keep = nms(boxes_im[:, :-1].astype(np.float32), NMS_THRESH)
    # boxes_im = boxes_im[keep, :]
    # scores_im = scores_im[keep, :]
    #
    # keep_indices = range(boxes_im.shape[0])
    #num_sources = vis_detections(im, None, boxes_im[keep_indices, :], ax, thresh=conf_thresh)

    print(', found %d sources' % num_sources)

    #If no sources detected plot average position of detection boxes
    if num_sources == 0:
        boxes = np.reshape(boxes, (cfg.TEST.RPN_POST_NMS_TOP_N, 7, 4))
        bboxs = np.average(boxes, axis=1)
        #Draw ground truth boxes
        for i in range(len(ground_truth['act_xmin'])):
            ax.add_patch(
                plt.Rectangle(
                    (ground_truth['act_xmin'][i], ground_truth['act_ymin'][i]),
                    ground_truth['act_xmax'][i] - ground_truth['act_xmin'][i],
                    ground_truth['act_ymax'][i] - ground_truth['act_ymin'][i],
                    fill=False,
                    edgecolor='gray',
                    linewidth=2.0,
                    linestyle='--'))
        #Draw proposed boxes
        for a_box in range(np.shape(bboxs)[0]):

            bbox = bboxs[a_box, :4]
            ax.add_patch(
                plt.Rectangle((bbox[0], bbox[1]),
                              bbox[2] - bbox[0],
                              bbox[3] - bbox[1],
                              fill=False,
                              edgecolor=colors_2[a_box],
                              linewidth=1.0))

    ##Plot scores for each box as a grouped histogram
    if plot:
        axa = fig.add_subplot(1, 2, 2)
        barwidth = (1 - 0.1) / cfg.TEST.RPN_POST_NMS_TOP_N
        axa.set_ylim([0, 1])
        r1 = np.arange(len(scores[0, :]))

        for i in range(0, int(np.shape(scores)[0])):

            axa.bar(r1,
                    scores[i, :],
                    color=colors_2[i],
                    width=barwidth,
                    label='det_' + str(i + 1))
            r1 = [x + barwidth for x in r1]

        axa.set_xticks([r + 0.45 for r in range(len(scores[0, :]))])
        axa.set_xticklabels(CLASSES[:], fontsize=8)

        #Draw horizontal line at threshold, add legend and title
        axa.axhline(conf_thresh, linestyle='--', color='black', linewidth=1.0)
        plt.legend(loc='upper right', fontsize='x-small')
        axa.set_title('Class Scores for each Detection')

        # Generate text string for ClaRAN's prediction
        claran_text = ''
        for p in range(len(predict['pred_class'])):
            claran_text += str(predict['pred_class'][p].replace('_', 'C_') +
                               'P ' +
                               "{:.2f}".format(predict['score'][p])) + " "
        ax.text(5,
                20,
                '{:s}'.format('ClaRAN: ' + claran_text),
                bbox=dict(facecolor='None', alpha=0.4, edgecolor='None'),
                fontsize=8,
                color='black')

        # Generate text string for ground truth
        gt_text = ''
        for q in ground_truth['act_class']:
            gt_text += q.replace('_', 'C_') + 'P '
        ax.text(5,
                40,
                '{:s}'.format('Ground Truth: ' + gt_text),
                bbox=dict(facecolor='None', alpha=0.4, edgecolor='None'),
                fontsize=8,
                color='black')

        plt.tight_layout()
        fig.subplots_adjust(top=0.85)

    plt.draw()

    # save results to CSV if ClaRAN has found a single source
    #if num_sources == 1:

    #dict_to_csv( dict(ground_truth.items() + predict.items()) )

    return 0