Пример #1
0
    def evaluate_generator(self,
                           annotations,
                           comet_experiment=None,
                           iou_threshold=0.5,
                           max_detections=200):
        """ Evaluate prediction model using a csv fit_generator

        Args:
            annotations (str): Path to csv label file, labels are in the format -> path/to/image.png,x1,y1,x2,y2,class_name
            iou_threshold(float): IoU Threshold to count for a positive detection (defaults to 0.5)
            max_detections (int): Maximum number of bounding box predictions
            comet_experiment(object): A comet experiment class objects to track

        Return:
            mAP: Mean average precision of the evaluated data
        """
        #Format args for CSV generator
        classes_file = utilities.create_classes(annotations)
        arg_list = utilities.format_args(annotations, classes_file, self.config)
        args = parse_args(arg_list)

        #create generator
        validation_generator = CSVGenerator(
            args.annotations,
            args.classes,
            image_min_side=args.image_min_side,
            image_max_side=args.image_max_side,
            config=args.config,
            shuffle_groups=False,
        )

        average_precisions = evaluate(validation_generator,
                                      self.prediction_model,
                                      iou_threshold=iou_threshold,
                                      score_threshold=args.score_threshold,
                                      max_detections=max_detections,
                                      save_path=args.save_path,
                                      comet_experiment=comet_experiment)

        # print evaluation
        total_instances = []
        precisions = []
        for label, (average_precision, num_annotations) in average_precisions.items():
            print('{:.0f} instances of class'.format(num_annotations),
                  validation_generator.label_to_name(label),
                  'with average precision: {:.4f}'.format(average_precision))
            total_instances.append(num_annotations)
            precisions.append(average_precision)

        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)))

        mAP = sum(precisions) / sum(x > 0 for x in total_instances)
        print('mAP: {:.4f}'.format(mAP))
        return mAP
def detectAlphabets(imageToRecognize):
    args = parse_args()
    args.val_path = imageToRecognize
    # os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    keras.backend.tensorflow_backend.set_session(get_session())
    model = keras.models.load_model(os.path.join(dir, '../snapshots/resnet50_csv_wtext.h5'), custom_objects=custom_objects)
    test_image_data_generator = keras.preprocessing.image.ImageDataGenerator()
    #
    # # create a generator for testing data
    test_generator = CSVGenerator(
        csv_data_file=args.annotations,
        csv_class_file=args.classes,
        image_data_generator=test_image_data_generator,
        batch_size=args.batch_size
    )
    # index = 0
    # load image
    image = read_image_bgr(args.val_path)

    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)

    # process image
    start = time.time()
    _, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)
    print('detections:', detections)
    # compute predicted labels and scores
    predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
    scores = detections[0, np.arange(detections.shape[1]), 4 + predicted_labels]
    print("label=", predicted_labels)
    # correct for image scale
    scaled_detection = detections[0, :, :4] / scale

    # visualize detections
    recognized = {}

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
        if score < 0.35:
            continue
        b = scaled_detection[idx, :4].astype(int)
        cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 1)

        caption = test_generator.label_to_name(label)
        if caption == "equal":
            caption = "="
        cv2.putText(draw, caption, (b[0], b[1] - 1), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
        recognized[caption] = b
        print(caption + ", score=" + str(score))

    plt.imshow(draw)
    plt.show()
    return recognized, draw
        detections[:, :, 3] = np.minimum(image.shape[0], detections[:, :, 3])

        # correct boxes for image scale
        detections[0, :, :4] /= scale

        # change to (x, y, w, h) (MS COCO standard)
        detections[:, :, 2] -= detections[:, :, 0]
        detections[:, :, 3] -= detections[:, :, 1]

        # compute predicted labels and scores
        for detection in detections[0, ...]:
            label = np.argmax(detection[4:])
            # append detections for each positively labeled class
            if float(detection[4 + label]) > args.score_threshold:
                image_result = {
                    'image_id'    : test_generator.image_names[i],
                    'category_id' : test_generator.label_to_name(label),
                    'scores'      : [float(det) for i,det in 
                                     enumerate(detection) if i >= 4],
                    'bbox'        : (detection[:4]).tolist(),
                }
                # append detection to results
                results.append(image_result)
        # append image to list of processed images
        image_ids.append(test_generator.image_names[i])

        # print progress
        print('{}/{}'.format(i, len(test_generator.image_names)), end='\r')
    out_name = '{}_predict_results_{}.pickle'.format(os.path.splitext(args.test_path)[0],str(args.score_threshold).split(".")[-1])
    pickle.dump(results,open(out_name,'wb'))
        detections[0, :, :4] /= scale

        # change to (x, y, w, h) (MS COCO standard)
        detections[:, :, 2] -= detections[:, :, 0]
        detections[:, :, 3] -= detections[:, :, 1]

        # compute predicted labels and scores
        for detection in detections[0, ...]:
            label = int(detection[4])
            # append detections for each positively labeled class
            if float(detection[4 + label]) > args.score_threshold:
                image_result = {
                    'image_id':
                    test_generator.image_names[i],
                    'category_id':
                    test_generator.label_to_name(label),
                    'scores':
                    [float(det) for i, det in enumerate(detection) if i >= 5],
                    'bbox': (detection[:4]).tolist(),
                }
                # append detection to results
                results.append(image_result)
        # append image to list of processed images
        image_ids.append(test_generator.image_names[i])

        # print progress
        print('{}/{}'.format(i, len(test_generator.image_names)), end='\r')
    out_name = '{}_predict_results_{}.pickle'.format(
        os.path.splitext(args.test_path)[0],
        str(args.score_threshold).split(".")[-1])
    pickle.dump(results, open(out_name, 'wb'))
Пример #5
0
def main():

    os.makedirs(RESULTS_DIR, exist_ok=True)

    # os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    keras.backend.tensorflow_backend.set_session(get_session())

    if SNAPSHOT_FILE:
        snapshot_file = SNAPSHOT_FILE
    else:
        snapshots = os.listdir(SNAPSHOT_DIR)
        if not snapshots:
            raise Exception("There are no snapshots")

        snapshot_file = os.path.join(SNAPSHOT_DIR,
                                     sorted(snapshots, reverse=True)[0])

    print("snapshot: {}".format(snapshot_file))

    model = keras.models.load_model(
        snapshot_file, custom_objects=custom_objects)
    # print(model.summary())

    # create image data generator object
    val_image_data_generator = keras.preprocessing.image.ImageDataGenerator()

    # create a generator for testing data
    val_generator = CSVGenerator(
        ANNOTATIONS,
        CLASSES,
        val_image_data_generator,
        batch_size=BATCH_SIZE,
        base_dir=BASE_DIR)

    for image_index in range(val_generator.size()):

        # load image
        image = val_generator.load_image(image_index)

        # copy to draw on
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        # preprocess image for network
        image = val_generator.preprocess_image(image)
        image, scale = val_generator.resize_image(image)
        annotations = val_generator.load_annotations(image_index)

        # process image
        start = time.time()
        _, _, detections = model.predict_on_batch(
            np.expand_dims(image, axis=0))
        print("processing time: ", time.time() - start)

        # compute predicted labels and scores
        predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
        scores = detections[
            0, np.arange(detections.shape[1]), 4 + predicted_labels]

        # correct for image scale
        detections[0, :, :4] /= scale

        # visualize detections
        detected = False
        for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
            if score < 0.5:
                continue

            detected = True

            if VISUALIZE_RESULTS:

                b = detections[0, idx, :4].astype(int)
                cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
                caption = "{} {:.3f}".format(
                    val_generator.label_to_name(label), score)
                cv2.putText(draw, caption, (b[0], b[1] - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
                cv2.putText(draw, caption, (b[0], b[1] - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)

        if VISUALIZE_RESULTS and detected:

            # visualize annotations
            for annotation in annotations:

                label = int(annotation[4])
                b = annotation[:4].astype(int)
                cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 2)
                caption = "{}".format(val_generator.label_to_name(label))
                cv2.putText(draw, caption, (b[0], b[1] - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
                cv2.putText(draw, caption, (b[0], b[1] - 10),
                            cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)

            plt.figure(figsize=(15, 15))
            plt.axis('off')
            plt.imshow(draw)
            plt.show()