ret=False)

# ## 3. Run the evaluation
#
# Now that we have instantiated a model and a data generator to serve the dataset, we can set up the evaluator and run the evaluation.
#
# The evaluator is quite flexible: It can compute the average precisions according to the Pascal VOC pre-2010 algorithm, which samples 11 equidistant points of the precision-recall curves, or according to the Pascal VOC post-2010 algorithm, which integrates numerically over the entire precision-recall curves instead of sampling a few individual points. You could also change the number of sampled recall points or the required IoU overlap for a prediction to be considered a true positive, among other things. Check out the `Evaluator`'s documentation for details on all the arguments.
#
# In its default settings, the evaluator's algorithm is identical to the official Pascal VOC pre-2010 Matlab detection evaluation algorithm, so you don't really need to tweak anything unless you want to.
#
# The evaluator roughly performs the following steps: It runs predictions over the entire given dataset, then it matches these predictions to the ground truth boxes, then it computes the precision-recall curves for each class, then it samples 11 equidistant points from these precision-recall curves to compute the average precision for each class, and finally it computes the mean average precision over all classes.

# In[6]:

evaluator = Evaluator(model=model,
                      n_classes=n_classes,
                      data_generator=dataset,
                      model_mode=model_mode)

results = evaluator(
    img_height=img_height,
    img_width=img_width,
    batch_size=4,  #少ない枚数のクラスがあるので小さくする
    data_generator_mode='resize',
    round_confidences=False,
    matching_iou_threshold=0.5,
    border_pixels='include',
    sorting_algorithm='quicksort',
    average_precision_mode='sample',
    num_recall_points=11,
    ignore_neutral_boxes=True,
    return_precisions=True,
Beispiel #2
0
image_set_filename = '../data/ghc/insulator+nuts/ImageSets/Main/val.txt'

# The XML parser needs to know what object class names to look for and in which order to map them to integers.
classes = cfg.CLASSES

dataset.parse_xml(images_dirs=[images_dir],
                  image_set_filenames=[image_set_filename],
                  annotations_dirs=[annotations_dir],
                  classes=classes,
                  include_classes='all',
                  exclude_truncated=False,
                  exclude_difficult=False,
                  ret=False)

evaluator = Evaluator(model=model,
                      n_classes=n_classes,
                      data_generator=dataset,
                      model_mode='inference')

results = evaluator(img_height=img_height,
                    img_width=img_width,
                    batch_size=8,
                    data_generator_mode='resize',
                    round_confidences=False,
                    matching_iou_threshold=0.5,
                    border_pixels='include',
                    sorting_algorithm='quicksort',
                    average_precision_mode='sample',
                    num_recall_points=11,
                    ignore_neutral_boxes=True,
                    return_precisions=True,
                    return_recalls=True,
                      bbox={
                          'facecolor': color,
                          'alpha': 0.5
                      })

#%%############################################################################
# Performance report

# Instantiate a convertor, 2d images to the 3d arrays: (width, height, 1)
from data_generator.object_detection_2d_photometric_ops import ConvertTo1Channel
convertor = ConvertTo1Channel()

# instantiate an evaluator using the test data set
from eval_utils.average_precision_evaluator import Evaluator
evaluator = Evaluator(model=main_model,
                      n_classes=n_classes,
                      data_generator=test_dataset,
                      model_mode='training')

# perform the evaluation
results = evaluator(img_height=80,
                    img_width=80,
                    batch_size=32,
                    data_generator_mode='resize',
                    convertor=convertor,
                    round_confidences=False,
                    matching_iou_threshold=0.5,
                    border_pixels='include',
                    sorting_algorithm='quicksort',
                    average_precision_mode='sample',
                    num_recall_points=11,
                    ignore_neutral_boxes=True,
classes = ['background', 'person', 'car', 'bike', 'motorbike']
#classes = ['background', 'person', 'car']

dataset.parse_xml(
    images_dirs=[images_dir],
    image_set_filenames=[image_set_filename],
    annotations_dirs=[annotations_dir],
    classes=classes[:4],
    #include_classes='all',
    include_classes=[0, 1, 2],
    exclude_truncated=False,
    exclude_difficult=False,
    ret=False)

evaluator = Evaluator(model=model,
                      n_classes=2,
                      data_generator=dataset,
                      model_mode=model_mode)

results = evaluator(img_height=img_height,
                    img_width=img_width,
                    batch_size=8,
                    data_generator_mode='resize',
                    round_confidences=False,
                    matching_iou_threshold=0.5,
                    border_pixels='include',
                    sorting_algorithm='quicksort',
                    average_precision_mode='sample',
                    num_recall_points=11,
                    ignore_neutral_boxes=True,
                    return_precisions=True,
                    return_recalls=True,
def evaluate_dataset(imageset_path, model):
    filenames = []
    with open(imageset_path, "r") as f:
        filenames = f.read()

    i = 0
    dataset = {}

    ground_truth = {}
    prediction_results = [[] for k in range(n_classes + 1)]

    filenames = filenames.split("\n")
    for filename in filenames:
        if not filename:
            continue

        img_id = filename
        i += 1
        # print(f"{i}/{len(filenames)}")

        img = numpy.asarray(
            Image.open(os.path.join(images_dir, f"{filename}.jpg")))
        gt_boxes = read_content(
            os.path.join(annotations_dir, f"{filename}.xml"))

        # resize = Resize(300, 300, labels_format={'class_id': 0, 'xmin': 1, 'ymin': 2, 'xmax': 3, 'ymax': 4})
        # inverter = None
        if img.shape != (300, 300, 3):
            continue

        dataset[filename] = (img, gt_boxes)

        y_pred = model.predict(numpy.array([img]))

        # 4: Decode the raw predictions in `y_pred`.

        y_pred_decoded = decode_detections(y_pred,
                                           confidence_thresh=0.5,
                                           iou_threshold=0.4,
                                           top_k=200,
                                           normalize_coords=normalize_coords,
                                           img_height=img_height,
                                           img_width=img_width)
        y_pred_decoded = y_pred_decoded[0]

        ground_truth[img_id] = (numpy.asarray(gt_boxes),
                                numpy.array([False] * len(gt_boxes)))

        for pred_box in y_pred_decoded:
            lbl, score, xmin, ymin, xmax, ymax = pred_box
            # pred_res = (img_id, score, int(xmin), int(ymin), int(xmax), int(ymax))
            pred_res = (img_id, score, xmin, ymin, xmax, ymax)
            prediction_results[int(lbl)].append(pred_res)

    evaluator = Evaluator(model=None,
                          n_classes=n_classes,
                          data_generator=None,
                          bypass=True)

    num_gt_per_class = numpy.array(
        [len(prediction_results[i]) for i in range(n_classes + 1)])
    evaluator.num_gt_per_class = num_gt_per_class
    evaluator.prediction_results = prediction_results
    evaluator.ground_truth = ground_truth

    true_positives, false_positives, cumulative_true_positives, cumulative_false_positives = evaluator.match_predictions(
        ignore_neutral_boxes=True,
        matching_iou_threshold=0.5,
        border_pixels='include',
        sorting_algorithm='quicksort',
        verbose=True,
        ret=True)

    cumulative_precisions, cumulative_recalls = evaluator.compute_precision_recall(
        verbose=True, ret=True)

    #############################################################################################
    # Compute the average precision for this class.
    #############################################################################################

    average_precisions = evaluator.compute_average_precisions(
        mode='sample', num_recall_points=11, verbose=True, ret=True)

    mean_average_precision = evaluator.compute_mean_average_precision(ret=True)

    ret = (mean_average_precision, average_precisions, cumulative_precisions,
           cumulative_recalls)
    return ret
Beispiel #6
0
def main():
    model_mode = 'inference'
    K.clear_session()  # Clear previous models from memory.

    model = ssd_300(image_size=(Config.img_height, Config.img_width,
                                Config.img_channels),
                    n_classes=Config.n_classes,
                    mode=model_mode,
                    l2_regularization=Config.l2_regularization,
                    scales=Config.scales,
                    aspect_ratios_per_layer=Config.aspect_ratios,
                    two_boxes_for_ar1=True,
                    steps=Config.steps,
                    offsets=Config.offsets,
                    clip_boxes=False,
                    variances=Config.variances,
                    normalize_coords=Config.normalize_coords,
                    subtract_mean=Config.mean_color,
                    swap_channels=[2, 1, 0],
                    confidence_thresh=0.01,
                    iou_threshold=0.45,
                    top_k=200,
                    nms_max_output_size=400)

    # 2: Load the trained weights into the model.

    weights_path = os.getcwd() + '/weights/' + args.model_name + ".h5"
    model.load_weights(weights_path, by_name=True)
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

    test_dataset = DataGenerator(load_images_into_memory=True,
                                 hdf5_dataset_path=os.getcwd() + "/data/" +
                                 args.dataset + '/polyp_test.h5')

    test_dataset_size = test_dataset.get_dataset_size()
    print("Number of images in the test dataset:\t{:>6}".format(
        test_dataset_size))

    classes = ['background', 'polyp']

    generator = test_dataset.generate(batch_size=1,
                                      shuffle=True,
                                      transformations=[],
                                      returns={
                                          'processed_images', 'filenames',
                                          'inverse_transform',
                                          'original_images', 'original_labels'
                                      },
                                      keep_images_without_gt=False)

    # Generate a batch and make predictions.

    i = 0
    confidence_threshold = Config.confidence_threshold

    for val in range(test_dataset_size):
        batch_images, batch_filenames, batch_inverse_transforms, batch_original_images, batch_original_labels = next(
            generator)

        print("Ground truth boxes:\n")
        print(np.array(batch_original_labels[i]))

        y_pred = model.predict(batch_images)

        # Perform confidence thresholding.
        y_pred_thresh = [
            y_pred[k][y_pred[k, :, 1] > confidence_threshold]
            for k in range(y_pred.shape[0])
        ]

        # Convert the predictions for the original image.
        # y_pred_thresh_inv = apply_inverse_transforms(y_pred_thresh, batch_inverse_transforms)

        np.set_printoptions(precision=2, suppress=True, linewidth=90)
        print("Predicted boxes:\n")
        print('   class   conf xmin   ymin   xmax   ymax')
        print(y_pred_thresh[i])

        plt.figure(figsize=(20, 12))
        plt.imshow(batch_images[i])

        current_axis = plt.gca()

        colors = plt.cm.hsv(
            np.linspace(0, 1, Config.n_classes +
                        1)).tolist()  # Set the colors for the bounding boxes
        classes = [
            'background', 'polyps'
        ]  # Just so we can print class names onto the image instead of IDs

        for box in batch_original_labels[i]:
            xmin = box[1]
            ymin = box[2]
            xmax = box[3]
            ymax = box[4]
            label = '{}'.format(classes[int(box[0])])
            current_axis.add_patch(
                plt.Rectangle((xmin, ymin),
                              xmax - xmin,
                              ymax - ymin,
                              color='green',
                              fill=False,
                              linewidth=2))
            current_axis.text(xmin,
                              ymin,
                              label,
                              size='x-large',
                              color='white',
                              bbox={
                                  'facecolor': 'green',
                                  'alpha': 1.0
                              })

        for box in y_pred_thresh[i]:
            xmin = box[2]
            ymin = box[3]
            xmax = box[4]
            ymax = box[5]
            color = colors[int(box[0])]
            label = '{}: {:.2f}'.format(classes[int(box[0])], box[1])
            current_axis.add_patch(
                plt.Rectangle((xmin, ymin),
                              xmax - xmin,
                              ymax - ymin,
                              color=color,
                              fill=False,
                              linewidth=2))
            current_axis.text(xmin,
                              ymin,
                              label,
                              size='x-large',
                              color='white',
                              bbox={
                                  'facecolor': color,
                                  'alpha': 1.0
                              })
        image = plt.gcf()
        # plt.show()
        plt.draw()
        image.savefig(os.getcwd() + "/val_ssd300/val_" + str(val) + ".png",
                      dpi=100)

    evaluator = Evaluator(model=model,
                          n_classes=Config.n_classes,
                          data_generator=test_dataset,
                          model_mode=model_mode)

    results = evaluator(img_height=Config.img_height,
                        img_width=Config.img_width,
                        batch_size=args.batch_size,
                        data_generator_mode='resize',
                        round_confidences=False,
                        matching_iou_threshold=0.3,
                        border_pixels='include',
                        sorting_algorithm='quicksort',
                        average_precision_mode='sample',
                        num_recall_points=11,
                        ignore_neutral_boxes=True,
                        return_precisions=True,
                        return_recalls=True,
                        return_average_precisions=True,
                        verbose=True)

    mean_average_precision, average_precisions, precisions, recalls, tp_count, fp_count, fn_count, polyp_precision, polyp_recall = results

    print("TP : %d, FP : %d, FN : %d " % (tp_count, fp_count, fn_count))
    print("{:<14}{:<6}{}".format('polyp', 'Precision ',
                                 round(polyp_precision, 3)))
    print("{:<14}{:<6}{}".format('polyp', 'Recall ', round(polyp_recall, 3)))

    # for i in range(1, len(average_precisions)):
    #     print("{:<14}{:<6}{}".format(classes[i], 'AP', round(average_precisions[i], 3)))
    #
    # print("{:<14}{:<6}{}".format('', 'mAP', round(mean_average_precision, 3)))
    # print('Precisions', np.mean(precisions[1]))
    # print('Recalls', np.mean(recalls[1]))

    m = max((Config.n_classes + 1) // 2, 2)
    n = 2

    fig, cells = plt.subplots(m, n, figsize=(n * 8, m * 8))
    val = 0
    for i in range(m):
        for j in range(n):
            if n * i + j + 1 > Config.n_classes: break
            cells[i, j].plot(recalls[n * i + j + 1],
                             precisions[n * i + j + 1],
                             color='blue',
                             linewidth=1.0)
            cells[i, j].set_xlabel('recall', fontsize=14)
            cells[i, j].set_ylabel('precision', fontsize=14)
            cells[i, j].grid(True)
            cells[i, j].set_xticks(np.linspace(0, 1, 11))
            cells[i, j].set_yticks(np.linspace(0, 1, 11))
            cells[i, j].set_title("{}, AP: {:.3f}".format(
                classes[n * i + j + 1], average_precisions[n * i + j + 1]),
                                  fontsize=16)
            image = plt.gcf()
            # plt.show()
            plt.draw()
            image.savefig(os.getcwd() + "/test_out/test_" + str(val) + ".png",
                          dpi=100)
            val += 1
Beispiel #7
0
def main():

    # Set a few configuration parameters.
    img_height = 300
    img_width = 300
    n_classes = 20
    model_mode = 'training'

    # Set the path to the `.h5` file of the model to be loaded.
    model_file = file_io.FileIO('gs://deeplearningteam11/vgg19BNmodel.h5',
                                mode='rb')

    # Store model locally on instance
    model_path = 'model.h5'
    with open(model_path, 'wb') as f:
        f.write(model_file.read())
    model_file.close()

    data_dir = "gs://deeplearningteam11/data"
    os.system("gsutil -m cp -r " + data_dir + "  " +
              os.path.dirname(__file__) + " > /dev/null 2>&1 ")
    # We need to create an SSDLoss object in order to pass that to the model loader.
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

    K.clear_session()  # Clear previous models from memory.

    model = load_model(model_path,
                       custom_objects={
                           'AnchorBoxes': AnchorBoxes,
                           'L2Normalization': L2Normalization,
                           'DecodeDetections': DecodeDetections,
                           'compute_loss': ssd_loss.compute_loss
                       })

    model.summary()

    te_dataset = DataGenerator(load_images_into_memory=True)
    tr_dataset = DataGenerator(load_images_into_memory=True)

    # TODO: Set the paths to the dataset here.
    tr_Pascal_VOC_dataset_images_dir = os.path.dirname(
        __file__) + "/" + "data/data/VOC2007/train/JPEGImages/"
    tr_Pascal_VOC_dataset_annotations_dir = os.path.dirname(
        __file__) + "/" + "data/data/VOC2007/train/Annotations/"
    tr_Pascal_VOC_dataset_image_set_filename = os.path.dirname(
        __file__) + "/" + "data/data/VOC2007/train/ImageSets/Main/trainval.txt"

    te_Pascal_VOC_dataset_images_dir = os.path.dirname(
        __file__) + "/" + "data/data/VOC2007/test/JPEGImages/"
    te_Pascal_VOC_dataset_annotations_dir = os.path.dirname(
        __file__) + "/" + "data/data/VOC2007/test/Annotations/"
    te_Pascal_VOC_dataset_image_set_filename = os.path.dirname(
        __file__) + "/" + "data/data/VOC2007/test/ImageSets/Main/test.txt"

    # The XML parser needs to now what object class names to look for and in which order to map them to integers.
    classes = [
        'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
        'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',
        'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',
        'tvmonitor'
    ]

    with tf.device('/device:GPU:0'):
        # Testing results
        te_dataset.parse_xml(
            images_dirs=[te_Pascal_VOC_dataset_images_dir],
            image_set_filenames=[te_Pascal_VOC_dataset_image_set_filename],
            annotations_dirs=[te_Pascal_VOC_dataset_annotations_dir],
            classes=classes,
            include_classes='all',
            exclude_truncated=False,
            exclude_difficult=True,
            ret=False,
            verbose=False)

        te_evaluator = Evaluator(model=model,
                                 n_classes=n_classes,
                                 data_generator=te_dataset,
                                 model_mode=model_mode)

        te_results = te_evaluator(img_height=img_height,
                                  img_width=img_width,
                                  batch_size=64,
                                  data_generator_mode='resize',
                                  round_confidences=False,
                                  matching_iou_threshold=0.5,
                                  border_pixels='include',
                                  sorting_algorithm='quicksort',
                                  average_precision_mode='sample',
                                  num_recall_points=11,
                                  ignore_neutral_boxes=True,
                                  return_precisions=True,
                                  return_recalls=True,
                                  return_average_precisions=True,
                                  verbose=False)

        mean_average_precision, average_precisions, precisions, recalls = te_results

        for i in range(1, len(average_precisions)):
            print("{:<14}{:<6}{}".format(classes[i], 'AP',
                                         round(average_precisions[i], 3)))
        print()
        print("{:<14}{:<6}{}".format('', 'mAP', round(mean_average_precision,
                                                      3)))

        print('TRAIN')
        tr_dataset.parse_xml(
            images_dirs=[tr_Pascal_VOC_dataset_images_dir],
            image_set_filenames=[tr_Pascal_VOC_dataset_image_set_filename],
            annotations_dirs=[tr_Pascal_VOC_dataset_annotations_dir],
            classes=classes,
            include_classes='all',
            exclude_truncated=False,
            exclude_difficult=True,
            ret=False,
            verbose=False)

        # Training results
        tr_evaluator = Evaluator(model=model,
                                 n_classes=n_classes,
                                 data_generator=tr_dataset,
                                 model_mode=model_mode)

        tr_results = tr_evaluator(img_height=img_height,
                                  img_width=img_width,
                                  batch_size=64,
                                  data_generator_mode='resize',
                                  round_confidences=False,
                                  matching_iou_threshold=0.5,
                                  border_pixels='include',
                                  sorting_algorithm='quicksort',
                                  average_precision_mode='sample',
                                  num_recall_points=11,
                                  ignore_neutral_boxes=True,
                                  return_precisions=True,
                                  return_recalls=True,
                                  return_average_precisions=True,
                                  verbose=False)

        mean_average_precision, average_precisions, precisions, recalls = tr_results

        for i in range(1, len(average_precisions)):
            print("{:<14}{:<6}{}".format(classes[i], 'AP',
                                         round(average_precisions[i], 3)))
        print()
        print("{:<14}{:<6}{}".format('', 'mAP', round(mean_average_precision,
                                                      3)))
Beispiel #8
0
                                  'alpha': 1.0
                              })
        # for box in y_pred_decoded_inv[i]:
        #     xmin = box[2]
        #     ymin = box[3]
        #     xmax = box[4]
        #     ymax = box[5]
        #     color = colors[int(box[0])]
        #     label = '{}: {:.2f}'.format(classes[int(box[0])], box[1])
        #     current_axis.add_patch(plt.Rectangle((xmin, ymin), xmax-xmin, ymax-ymin, color=color, fill=False, linewidth=2))
        #     current_axis.text(xmin, ymin, label, size='x-large', color='white', bbox={'facecolor':color, 'alpha':1.0})

elif evaluate_mode == 'MAP':
    evaluator = Evaluator(
        model=model,
        n_classes=n_classes,
        data_generator=val_dataset,  # train_dataset,  #
        model_mode=model_mode)

    results = evaluator(img_height=img_height,
                        img_width=img_width,
                        batch_size=batch_size,
                        data_generator_mode='resize',
                        round_confidences=False,
                        matching_iou_threshold=0.5,
                        border_pixels='include',
                        sorting_algorithm='quicksort',
                        average_precision_mode='sample',
                        num_recall_points=11,
                        ignore_neutral_boxes=True,
                        return_precisions=True,
def _main_(args):

    config_path = args.conf

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    ###############################
    #   Parse the annotations
    ###############################
    path_imgs_test = config['test']['test_image_folder']
    path_anns_test = config['test']['test_annot_folder']
    labels = config['model']['labels']
    categories = {}
    #categories = {"Razor": 1, "Gun": 2, "Knife": 3, "Shuriken": 4} #la categoría 0 es la background
    for i in range(len(labels)):
        categories[labels[i]] = i + 1
    print('\nTraining on: \t' + str(categories) + '\n')

    img_height = config['model']['input']  # Height of the model input images
    img_width = config['model']['input']  # Width of the model input images
    img_channels = 3  # Number of color channels of the model input images
    n_classes = len(
        labels
    )  # Number of positive classes, e.g. 20 for Pascal VOC, 80 for MS COCO
    classes = ['background'] + labels

    model_mode = 'training'
    # TODO: Set the path to the `.h5` file of the model to be loaded.
    model_path = config['train']['saved_weights_name']

    # We need to create an SSDLoss object in order to pass that to the model loader.
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)

    K.clear_session()  # Clear previous models from memory.

    model = load_model(model_path,
                       custom_objects={
                           'AnchorBoxes': AnchorBoxes,
                           'L2Normalization': L2Normalization,
                           'DecodeDetections': DecodeDetections,
                           'compute_loss': ssd_loss.compute_loss
                       })

    test_dataset = DataGenerator()
    test_dataset.parse_xml(
        images_dirs=[config['test']['test_image_folder']],
        image_set_filenames=[config['test']['test_image_set_filename']],
        annotations_dirs=[config['test']['test_annot_folder']],
        classes=classes,
        include_classes='all',
        exclude_truncated=False,
        exclude_difficult=False,
        ret=False)
    evaluator = Evaluator(model=model,
                          n_classes=n_classes,
                          data_generator=test_dataset,
                          model_mode=model_mode)

    results = evaluator(img_height=img_height,
                        img_width=img_width,
                        batch_size=4,
                        data_generator_mode='resize',
                        round_confidences=False,
                        matching_iou_threshold=0.5,
                        border_pixels='include',
                        sorting_algorithm='quicksort',
                        average_precision_mode='sample',
                        num_recall_points=11,
                        ignore_neutral_boxes=True,
                        return_precisions=True,
                        return_recalls=True,
                        return_average_precisions=True,
                        verbose=True)

    mean_average_precision, average_precisions, precisions, recalls = results

    total_instances = []
    precisions = []
    for i in range(1, len(average_precisions)):
        print('{:.0f} instances of class'.format(len(recalls[i])), classes[i],
              'with average precision: {:.4f}'.format(average_precisions[i]))
        total_instances.append(len(recalls[i]))
        precisions.append(average_precisions[i])

    if sum(total_instances) == 0:
        print('No test instances found.')
        return

    print('mAP using the weighted average of precisions among classes: {:.4f}'.
          format(
              sum([a * b for a, b in zip(total_instances, precisions)]) /
              sum(total_instances)))
    print('mAP: {:.4f}'.format(
        sum(precisions) / sum(x > 0 for x in total_instances)))

    for i in range(1, len(average_precisions)):
        print("{:<14}{:<6}{}".format(classes[i], 'AP',
                                     round(average_precisions[i], 3)))
    print()
    print("{:<14}{:<6}{}".format('', 'mAP', round(mean_average_precision, 3)))
Beispiel #10
0
weights_path = f'{root_path}/results/weights/ssd300_VOC_07+12_{backbone}.h5'
ssd.load_weights(weights_path, by_name=True)

adam = Adam(lr=1e-03, beta_1=.9, beta_2=.999, epsilon=1e-08, decay=.0)
ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.)

ssd.compile(optimizer=adam, loss=ssd_loss.compute_loss)

# Load data
###########
test_dataset = DataGenerator(
    load_images_into_memory=False,
    hdf5_dataset_path=f'{root_path}/data/VOC_07+12_test.h5')

evaluator = Evaluator(model=ssd,
                      n_classes=n_classes,
                      data_generator=test_dataset,
                      model_mode='inference')

results = evaluator(img_height=img_height,
                    img_width=img_width,
                    batch_size=batch_size,
                    data_generator_mode='resize',
                    round_confidences=False,
                    matching_iou_threshold=.5,
                    border_pixels='include',
                    sorting_algorithm='quicksort',
                    average_precision_mode='sample',
                    num_recall_points=11,
                    ignore_neutral_boxes=True,
                    return_precisions=True,
                    return_recalls=True,
def main():
    # create dataset
    dataset = DataGenerator()
    dataset.parse_xml(images_dirs=[dataset_images_dir],
                      image_set_filenames=[test_image_set_filename],
                      annotations_dirs=[dataset_annotations_dir],
                      classes=classes,
                      include_classes='all',
                      exclude_truncated=False,
                      exclude_difficult=False,
                      ret=False)

    # create model
    model = ssd_300(
        image_size=(img_height, img_width, 3),
        n_classes=n_classes,
        mode=model_mode,
        l2_regularization=0.0005,
        scales=[
            0.1, 0.2, 0.37, 0.54, 0.71, 0.88, 1.05
        ],  # The scales for MS COCO are [0.07, 0.15, 0.33, 0.51, 0.69, 0.87, 1.05]
        aspect_ratios_per_layer=[[1.0, 2.0, 0.5],
                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                 [1.0, 2.0, 0.5, 3.0, 1.0 / 3.0],
                                 [1.0, 2.0, 0.5], [1.0, 2.0, 0.5]],
        two_boxes_for_ar1=True,
        steps=None,
        offsets=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
        clip_boxes=False,
        variances=[0.1, 0.1, 0.2, 0.2],
        normalize_coords=True,
        subtract_mean=[123, 117, 104],
        swap_channels=[2, 1, 0],
        confidence_thresh=1.0e-4,
        iou_threshold=0.45,
        top_k=200,
        nms_max_output_size=400)

    # load weights and compile it
    model.load_weights(weights_path, by_name=True)
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

    evaluator = Evaluator(model=model,
                          n_classes=n_classes,
                          data_generator=dataset,
                          model_mode=model_mode)

    results = evaluator(img_height=img_height,
                        img_width=img_width,
                        batch_size=8,
                        data_generator_mode='resize',
                        round_confidences=False,
                        matching_iou_threshold=0.5,
                        border_pixels='include',
                        sorting_algorithm='quicksort',
                        average_precision_mode='sample',
                        num_recall_points=11,
                        ignore_neutral_boxes=True,
                        return_precisions=True,
                        return_recalls=True,
                        return_average_precisions=True,
                        verbose=True)

    mean_average_precision, average_precisions, precisions, recalls = results

    for i in range(1, len(average_precisions)):
        print("{:<14}{:<6}{}".format(classes[i], 'AP',
                                     round(average_precisions[i], 3)))
    print()
    print("{:<14}{:<6}{}".format('', 'mAP', round(mean_average_precision, 3)))

    m = max((n_classes + 1) // 2, 2)
    n = 2

    fig, cells = plt.subplots(m, n, figsize=(n * 8, m * 8))
    for i in range(m):
        for j in range(n):
            if n * i + j + 1 > n_classes: break
            cells[i, j].plot(recalls[n * i + j + 1],
                             precisions[n * i + j + 1],
                             color='blue',
                             linewidth=1.0)
            cells[i, j].set_xlabel('recall', fontsize=14)
            cells[i, j].set_ylabel('precision', fontsize=14)
            cells[i, j].grid(True)
            cells[i, j].set_xticks(np.linspace(0, 1, 6))
            cells[i, j].set_yticks(np.linspace(0, 1, 6))
            cells[i, j].set_xlim(0.0, 1.0)
            cells[i, j].set_ylim(0.0, 1.0)
            cells[i, j].set_title("{}, AP: {:.3f}".format(
                classes[n * i + j + 1], average_precisions[n * i + j + 1]),
                                  fontsize=16)

    if not os.path.isdir("evaluate_result"):
        os.makedirs("evaluate_result")

    plt.savefig('evaluate_result/ssd300_face_detection.png')
def main():
    model_mode = 'inference'
    K.clear_session()  # Clear previous models from memory.

    model = build_model(image_size=(Config.img_height, Config.img_width, Config.img_channels),
                        n_classes=Config.n_classes, mode=model_mode, l2_regularization=Config.l2_regularization,
                        scales=Config.scales,
                        aspect_ratios_per_layer=Config.steps,
                        two_boxes_for_ar1=True, steps=Config.steps, offsets=Config.offsets, clip_boxes=False,
                        variances=Config.variances, normalize_coords=Config.normalize_coords,
                        subtract_mean=Config.intensity_mean,
                        swap_channels=[2, 1, 0], confidence_thresh=0.01, iou_threshold=0.45, top_k=200,
                        nms_max_output_size=400)

    # 2: Load the trained weights into the model.

    weights_path = os.getcwd() + '/weights/' + args.model_name + ".h5"
    model.load_weights(weights_path, by_name=True)
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    ssd_loss = SSDLoss(neg_pos_ratio=3, alpha=1.0)
    model.compile(optimizer=adam, loss=ssd_loss.compute_loss)

    test_dataset = DataGenerator(load_images_into_memory=True,
                                 hdf5_dataset_path=os.getcwd() + "/data/" + args.dataset + '/polyp_test.h5')

    test_dataset_size = test_dataset.get_dataset_size()
    print("Number of images in the test dataset:\t{:>6}".format(test_dataset_size))

    classes = ['background', 'polyp']

    evaluator = Evaluator(model=model, n_classes=Config.n_classes, data_generator=test_dataset, model_mode=model_mode)

    results = evaluator(img_height=Config.img_height, img_width=Config.img_width, batch_size=args.batch_size,
                        data_generator_mode='resize',
                        round_confidences=False, matching_iou_threshold=0.5, border_pixels='include',
                        sorting_algorithm='quicksort',
                        average_precision_mode='sample', num_recall_points=11, ignore_neutral_boxes=True,
                        return_precisions=True, return_recalls=True, return_average_precisions=True, verbose=True)

    mean_average_precision, average_precisions, precisions, recalls = results

    for i in range(1, len(average_precisions)):
        print("{:<14}{:<6}{}".format(classes[i], 'AP', round(average_precisions[i], 3)))

    print("{:<14}{:<6}{}".format('', 'mAP', round(mean_average_precision, 3)))

    m = max((Config.n_classes + 1) // 2, 2)
    n = 2

    fig, cells = plt.subplots(m, n, figsize=(n * 8, m * 8))
    val = 0
    for i in range(m):
        for j in range(n):
            if n * i + j + 1 > Config.n_classes: break
            cells[i, j].plot(recalls[n * i + j + 1], precisions[n * i + j + 1], color='blue', linewidth=1.0)
            cells[i, j].set_xlabel('recall', fontsize=14)
            cells[i, j].set_ylabel('precision', fontsize=14)
            cells[i, j].grid(True)
            cells[i, j].set_xticks(np.linspace(0, 1, 11))
            cells[i, j].set_yticks(np.linspace(0, 1, 11))
            cells[i, j].set_title("{}, AP: {:.3f}".format(classes[n * i + j + 1], average_precisions[n * i + j + 1]),
                                  fontsize=16)
            image = plt.gcf()
            # plt.show()
            plt.draw()
            image.savefig(os.getcwd() + "/test_out/test_" + str(val) + ".png", dpi=100)
            val += 1
Beispiel #13
0
classes = [
    'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
    'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
    'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'
]
num_classes = len(classes) - 1

images_dir = '/home/adam/.keras/datasets/VOCdevkit'
# Ground truth
test_hdf5_path = osp.join(images_dir, '07_test.h5')
test_dataset = DataGenerator(load_images_into_memory=False,
                             hdf5_dataset_path=test_hdf5_path)
model_mode = 'training'
evaluator = Evaluator(model=model,
                      n_classes=num_classes,
                      data_generator=test_dataset,
                      model_mode=model_mode)

results = evaluator(img_height=image_height,
                    img_width=image_width,
                    batch_size=4,
                    data_generator_mode='resize',
                    round_confidences=False,
                    matching_iou_threshold=0.5,
                    border_pixels='include',
                    sorting_algorithm='quicksort',
                    average_precision_mode='sample',
                    num_recall_points=11,
                    ignore_neutral_boxes=True,
                    return_precisions=True,
                    return_recalls=True,