Esempio n. 1
0
def eval_and_plot_faster_rcnn(eval_model, num_images_to_plot, test_map_file, img_shape,
                              results_base_path, feature_node_name, classes,
                              drawUnregressedRois=False, drawNegativeRois=False,
                              nmsThreshold=0.5, nmsConfThreshold=0.0, bgrPlotThreshold = 0.8):
    # get image paths
    with open(test_map_file) as f:
        content = f.readlines()
    img_base_path = os.path.dirname(os.path.abspath(test_map_file))
    img_file_names = [os.path.join(img_base_path, x.split('\t')[1]) for x in content]

    # prepare model
    image_input = input_variable(img_shape, dynamic_axes=[Axis.default_batch_axis()], name=feature_node_name)
    dims_input = input_variable((1,6), dynamic_axes=[Axis.default_batch_axis()], name='dims_input')
    frcn_eval = eval_model(image_input, dims_input)

    #dims_input_const = cntk.constant([image_width, image_height, image_width, image_height, image_width, image_height], (1, 6))
    print("Plotting results from Faster R-CNN model for %s images." % num_images_to_plot)
    for i in range(0, num_images_to_plot):
        imgPath = img_file_names[i]

        # evaluate single image
        _, cntk_img_input, dims = load_resize_and_pad(imgPath, img_shape[2], img_shape[1])

        dims_input = np.array(dims, dtype=np.float32)
        dims_input.shape = (1,) + dims_input.shape
        output = frcn_eval.eval({frcn_eval.arguments[0]: [cntk_img_input], frcn_eval.arguments[1]: dims_input})

        out_dict = dict([(k.name, k) for k in output])
        out_cls_pred = output[out_dict['cls_pred']][0]
        out_rpn_rois = output[out_dict['rpn_rois']][0]
        out_bbox_regr = output[out_dict['bbox_regr']][0]

        labels = out_cls_pred.argmax(axis=1)
        scores = out_cls_pred.max(axis=1).tolist()

        if drawUnregressedRois:
            # plot results without final regression
            imgDebug = visualizeResultsFaster(imgPath, labels, scores, out_rpn_rois, img_shape[2], img_shape[1],
                                              classes, nmsKeepIndices=None, boDrawNegativeRois=drawNegativeRois,
                                              decisionThreshold=bgrPlotThreshold)
            imsave("{}/{}_{}".format(results_base_path, i, os.path.basename(imgPath)), imgDebug)

        # apply regression and nms to bbox coordinates
        regressed_rois = regress_rois(out_rpn_rois, out_bbox_regr, labels, dims)

        nmsKeepIndices = apply_nms_to_single_image_results(regressed_rois, labels, scores,
                                                    nms_threshold=nmsThreshold,
                                                    conf_threshold=nmsConfThreshold)

        img = visualizeResultsFaster(imgPath, labels, scores, regressed_rois, img_shape[2], img_shape[1],
                                     classes, nmsKeepIndices=nmsKeepIndices,
                                     boDrawNegativeRois=drawNegativeRois,
                                     decisionThreshold=bgrPlotThreshold)
        imsave("{}/{}_regr_{}".format(results_base_path, i, os.path.basename(imgPath)), img)
Esempio n. 2
0
def eval_and_plot_faster_rcnn(eval_model, num_images_to_plot, test_map_file, img_shape,
                              results_base_path, feature_node_name, classes,
                              drawUnregressedRois=False, drawNegativeRois=False,
                              nmsThreshold=0.5, nmsConfThreshold=0.0, bgrPlotThreshold = 0.8):
    # get image paths
    with open(test_map_file) as f:
        content = f.readlines()
    img_base_path = os.path.dirname(os.path.abspath(test_map_file))
    img_file_names = [os.path.join(img_base_path, x.split('\t')[1]) for x in content]

    # prepare model
    image_input = input_variable(img_shape, dynamic_axes=[Axis.default_batch_axis()], name=feature_node_name)
    dims_input = input_variable((1,6), dynamic_axes=[Axis.default_batch_axis()], name='dims_input')
    frcn_eval = eval_model(image_input, dims_input)

    #dims_input_const = cntk.constant([image_width, image_height, image_width, image_height, image_width, image_height], (1, 6))
    print("Plotting results from Faster R-CNN model for %s images." % num_images_to_plot)
    for i in range(0, num_images_to_plot):
        imgPath = img_file_names[i]

        # evaluate single image
        _, cntk_img_input, dims = load_resize_and_pad(imgPath, img_shape[2], img_shape[1])

        dims_input = np.array(dims, dtype=np.float32)
        dims_input.shape = (1,) + dims_input.shape
        output = frcn_eval.eval({frcn_eval.arguments[0]: [cntk_img_input], frcn_eval.arguments[1]: dims_input})

        out_dict = dict([(k.name, k) for k in output])
        out_cls_pred = output[out_dict['cls_pred']][0]
        out_rpn_rois = output[out_dict['rpn_rois']][0]
        out_bbox_regr = output[out_dict['bbox_regr']][0]

        labels = out_cls_pred.argmax(axis=1)
        scores = out_cls_pred.max(axis=1).tolist()

        if drawUnregressedRois:
            # plot results without final regression
            imgDebug = visualizeResultsFaster(imgPath, labels, scores, out_rpn_rois, img_shape[2], img_shape[1],
                                              classes, nmsKeepIndices=None, boDrawNegativeRois=drawNegativeRois,
                                              decisionThreshold=bgrPlotThreshold)
            imsave("{}/{}_{}".format(results_base_path, i, os.path.basename(imgPath)), imgDebug)

        # apply regression and nms to bbox coordinates
        regressed_rois = regress_rois(out_rpn_rois, out_bbox_regr, labels, dims)

        nmsKeepIndices = apply_nms_to_single_image_results(regressed_rois, labels, scores,
                                                    nms_threshold=nmsThreshold,
                                                    conf_threshold=nmsConfThreshold)

        img = visualizeResultsFaster(imgPath, labels, scores, regressed_rois, img_shape[2], img_shape[1],
                                     classes, nmsKeepIndices=nmsKeepIndices,
                                     boDrawNegativeRois=drawNegativeRois,
                                     decisionThreshold=bgrPlotThreshold)
        imsave("{}/{}_regr_{}".format(results_base_path, i, os.path.basename(imgPath)), img)
Esempio n. 3
0
def faster_rcnn_pred(eval_model,
                     prediction_in,
                     prediction_out,
                     class_map_file,
                     img_shape,
                     feature_node_name,
                     nms_threshold=0.5,
                     nms_conf_threshold=0.0,
                     headers=True,
                     output_width_height=False,
                     suppressed_labels=()):
    """
    Performs predictions with a model in continuous mode.

    :param eval_model: the model to use
    :param prediction_in: the input dir for images (list)
    :param prediction_out: the output dir for images and ROI CSV files (list)
    :param class_map_file: the class map file to use
    :param img_shape: the shpage
    :param feature_node_name: the name of the feature node
    :param nms_threshold:
    :param nms_conf_threshold:
    :param headers: whether to output the headers in the ROI file
    :param output_width_height: whether to output x/y/width/height or x0/y0/x1/y1
    :param suppressed_labels: the labels to suppress from being output in the ROI files
    """

    # load labels
    str_labels = load_class_labels(class_map_file)
    print("Class labels: %s" % str(str_labels))

    # prepare model
    image_input = input_variable(img_shape,
                                 dynamic_axes=[Axis.default_batch_axis()],
                                 name=feature_node_name)
    dims_input = input_variable((1, 6),
                                dynamic_axes=[Axis.default_batch_axis()],
                                name='dims_input')
    frcn_eval = eval_model(image_input, dims_input)

    while True:
        any = False

        # iterate directory pairs
        for p in range(len(prediction_in)):
            pred_in = prediction_in[p]
            pred_out = prediction_out[p]

            # only png and jpg files
            files = [
                (pred_in + os.sep + x) for x in os.listdir(pred_in)
                if (x.lower().endswith(".png") or x.lower().endswith(".jpg"))
            ]

            for f in files:
                start = datetime.now()
                print(start, "-", f)

                img_path = pred_out + os.sep + os.path.basename(f)
                roi_path = pred_out + os.sep + os.path.splitext(
                    os.path.basename(f))[0] + ".csv"

                cntk_img_input = None
                try:
                    _, cntk_img_input, dims = load_resize_and_pad(
                        f, img_shape[2], img_shape[1])
                except Exception as e:
                    print(str(e))

                try:
                    # delete any existing old files in output dir
                    if os.path.exists(img_path):
                        try:
                            os.remove(img_path)
                        except:
                            print(
                                "Failed to remove existing image in output directory: ",
                                img_path)
                    if os.path.exists(roi_path):
                        try:
                            os.remove(roi_path)
                        except:
                            print(
                                "Failed to remove existing ROI file in output directory: ",
                                roi_path)
                    # move into output dir
                    os.rename(f, img_path)
                except:
                    img_path = None

                if cntk_img_input is None:
                    continue
                if img_path is None:
                    continue

                dims_input = np.array(dims, dtype=np.float32)
                dims_input.shape = (1, ) + dims_input.shape
                output = frcn_eval.eval({
                    frcn_eval.arguments[0]: [cntk_img_input],
                    frcn_eval.arguments[1]:
                    dims_input
                })

                out_dict = dict([(k.name, k) for k in output])
                out_cls_pred = output[out_dict['cls_pred']][0]
                out_rpn_rois = output[out_dict['rpn_rois']][0]
                out_bbox_regr = output[out_dict['bbox_regr']][0]

                labels = out_cls_pred.argmax(axis=1)
                scores = out_cls_pred.max(axis=1).tolist()

                # apply regression and nms to bbox coordinates
                regressed_rois = regress_rois(out_rpn_rois, out_bbox_regr,
                                              labels, dims)
                nms_keep_indices = apply_nms_to_single_image_results(
                    regressed_rois,
                    labels,
                    scores,
                    nms_threshold=nms_threshold,
                    conf_threshold=nms_conf_threshold)

                save_rois_to_file(regressed_rois,
                                  nms_keep_indices,
                                  labels,
                                  str_labels,
                                  scores,
                                  pred_out,
                                  img_path,
                                  headers=headers,
                                  output_width_height=output_width_height,
                                  suppressed_labels=suppressed_labels,
                                  dims=dims)

                timediff = datetime.now() - start
                print("  time:", timediff)

        # nothing processed at all, lets wait for files to appear
        if not any:
            sleep(1)
Esempio n. 4
0
def faster_rcnn_eval(eval_model,
                     test_map_file,
                     class_map_file,
                     img_shape,
                     results_base_path,
                     feature_node_name,
                     nms_threshold=0.5,
                     nms_conf_threshold=0.0,
                     headers=True,
                     output_width_height=False,
                     suppressed_labels=()):
    """
    Tests a Faster R-CNN model and outputs rois.

    :param eval_model: the model
    :param test_map_file: the map file with the test images/labels
    :param class_map_file: the class map file
    :param img_shape: the shape
    :param results_base_path: the base directory for the results
    :param feature_node_name: the feature node name
    :param nms_threshold:
    :param nms_conf_threshold:
    :param headers: whether to output the headers in the ROI file
    :param output_width_height: whether to output x/y/width/height or x0/y0/x1/y1
    :param suppressed_labels: the labels to suppress from being output in the ROI files
    """

    # load labels
    str_labels = load_class_labels(class_map_file)
    print("Class labels: %s" % str(str_labels))

    # get image paths
    with open(test_map_file) as f:
        content = f.readlines()
    img_base_path = os.path.dirname(os.path.abspath(test_map_file))
    img_file_names = [
        os.path.join(img_base_path,
                     x.split('\t')[1]) for x in content
    ]

    # prepare model
    image_input = input_variable(img_shape,
                                 dynamic_axes=[Axis.default_batch_axis()],
                                 name=feature_node_name)
    dims_input = input_variable((1, 6),
                                dynamic_axes=[Axis.default_batch_axis()],
                                name='dims_input')
    frcn_eval = eval_model(image_input, dims_input)

    print("Outputting results from Faster R-CNN model for %s images." %
          len(img_file_names))
    for i in range(0, len(img_file_names)):
        start = datetime.now()
        img_path = img_file_names[i]
        print(
            str(i + 1) + "/" + str(len(img_file_names)) + ": " +
            img_file_names[i])

        # evaluate single image
        _, cntk_img_input, dims = load_resize_and_pad(img_path, img_shape[2],
                                                      img_shape[1])

        dims_input = np.array(dims, dtype=np.float32)
        dims_input.shape = (1, ) + dims_input.shape
        output = frcn_eval.eval({
            frcn_eval.arguments[0]: [cntk_img_input],
            frcn_eval.arguments[1]: dims_input
        })

        out_dict = dict([(k.name, k) for k in output])
        out_cls_pred = output[out_dict['cls_pred']][0]
        out_rpn_rois = output[out_dict['rpn_rois']][0]
        out_bbox_regr = output[out_dict['bbox_regr']][0]

        labels = out_cls_pred.argmax(axis=1)
        scores = out_cls_pred.max(axis=1).tolist()

        # apply regression and nms to bbox coordinates
        regressed_rois = regress_rois(out_rpn_rois, out_bbox_regr, labels,
                                      dims)
        nms_keep_indices = apply_nms_to_single_image_results(
            regressed_rois,
            labels,
            scores,
            nms_threshold=nms_threshold,
            conf_threshold=nms_conf_threshold)

        save_rois_to_file(regressed_rois,
                          nms_keep_indices,
                          labels,
                          str_labels,
                          scores,
                          results_base_path,
                          img_path,
                          headers=headers,
                          output_width_height=output_width_height,
                          suppressed_labels=suppressed_labels,
                          dims=dims)

        timediff = datetime.now() - start
        print("  time:", timediff)
Esempio n. 5
0
def faster_rcnn_eval_and_plot(eval_model,
                              num_images_to_plot,
                              test_map_file,
                              class_map_file,
                              img_shape,
                              results_base_path,
                              feature_node_name,
                              classes,
                              draw_unregressed_rois=False,
                              draw_negative_rois=False,
                              nms_threshold=0.5,
                              nms_conf_threshold=0.0,
                              bgr_plot_threshold=0.8,
                              headers=True,
                              output_width_height=False,
                              suppressed_labels=()):
    """
    Tests a Faster R-CNN model and plots images with detected boxes.

    :param eval_model: the model to evaluate
    :param num_images_to_plot: the number of images to plot
    :param test_map_file: the map file with the test images/labels
    :param class_map_file: the class map file
    :param img_shape: the shape
    :param results_base_path: the base of the output directory
    :param feature_node_name: the feature node name
    :param classes:
    :param draw_unregressed_rois: whether to draw unregressed ROIs
    :param draw_negative_rois: whether to draw negative ROIs (eg background)
    :param nms_threshold:
    :param nms_conf_threshold:
    :param bgr_plot_threshold:
    :param headers: whether to output the headers in the ROI file
    :param output_width_height: whether to output x/y/width/height or x0/y0/x1/y1
    """

    # load labels
    str_labels = load_class_labels(class_map_file)
    print("Class labels: %s" % str(str_labels))

    # get image paths
    with open(test_map_file) as f:
        content = f.readlines()
    img_base_path = os.path.dirname(os.path.abspath(test_map_file))
    img_file_names = [
        os.path.join(img_base_path,
                     x.split('\t')[1]) for x in content
    ]

    # prepare model
    image_input = input_variable(img_shape,
                                 dynamic_axes=[Axis.default_batch_axis()],
                                 name=feature_node_name)
    dims_input = input_variable((1, 6),
                                dynamic_axes=[Axis.default_batch_axis()],
                                name='dims_input')
    frcn_eval = eval_model(image_input, dims_input)

    # dims_input_const = cntk.constant([image_width, image_height, image_width, image_height, image_width, image_height], (1, 6))
    print("Plotting results from Faster R-CNN model for %s images." %
          num_images_to_plot)
    for i in range(0, num_images_to_plot):
        img_path = img_file_names[i]

        # save padded image
        write_padded_image(
            img_path, "{}/{}-padded.jpg".format(
                results_base_path,
                os.path.splitext(os.path.basename(img_path))[0]))

        # evaluate single image
        _, cntk_img_input, dims = load_resize_and_pad(img_path, img_shape[2],
                                                      img_shape[1])

        dims_input = np.array(dims, dtype=np.float32)
        dims_input.shape = (1, ) + dims_input.shape
        output = frcn_eval.eval({
            frcn_eval.arguments[0]: [cntk_img_input],
            frcn_eval.arguments[1]: dims_input
        })

        out_dict = dict([(k.name, k) for k in output])
        out_cls_pred = output[out_dict['cls_pred']][0]
        out_rpn_rois = output[out_dict['rpn_rois']][0]
        out_bbox_regr = output[out_dict['bbox_regr']][0]

        labels = out_cls_pred.argmax(axis=1)
        scores = out_cls_pred.max(axis=1).tolist()

        if draw_unregressed_rois:
            # plot results without final regression
            img_debug = faster_rcnn_visualize_results(
                img_path,
                labels,
                scores,
                out_rpn_rois,
                img_shape[2],
                img_shape[1],
                classes,
                nms_keep_indices=None,
                draw_negative_rois=draw_negative_rois,
                decision_threshold=bgr_plot_threshold)
            imsave(
                "{}/{}".format(results_base_path, os.path.basename(img_path)),
                img_debug)

        # apply regression and nms to bbox coordinates
        regressed_rois = regress_rois(out_rpn_rois, out_bbox_regr, labels,
                                      dims)

        nms_keep_indices = apply_nms_to_single_image_results(
            regressed_rois,
            labels,
            scores,
            nms_threshold=nms_threshold,
            conf_threshold=nms_conf_threshold)

        save_rois_to_file(regressed_rois,
                          nms_keep_indices,
                          labels,
                          str_labels,
                          scores,
                          results_base_path,
                          img_path,
                          headers=headers,
                          output_width_height=output_width_height,
                          suppressed_labels=suppressed_labels,
                          dims=dims)

        img = faster_rcnn_visualize_results(
            img_path,
            labels,
            scores,
            regressed_rois,
            img_shape[2],
            img_shape[1],
            classes,
            nms_keep_indices=nms_keep_indices,
            draw_negative_rois=draw_negative_rois,
            decision_threshold=bgr_plot_threshold)
        imsave(
            "{}/{}-regr.jpg".format(
                results_base_path,
                os.path.splitext(os.path.basename(img_path)))[0], img)