def compute_batch_ap(image_ids):
    APs = []
    for image_id in image_ids:
        # Load image
        image, image_meta, gt_class_id, gt_bbox =\
            data.load_image_gt(dataset, config, image_id)
        # Run object detection
        results = model.detect([image], verbose=0)
        # Compute AP
        r = results[0]
        AP, precisions, recalls, overlaps = utils.compute_ap(
            gt_bbox, gt_class_id, r['rois'], r['class_ids'], r['scores'])
        APs.append(AP)
    return APs
Esempio n. 2
0
dataset_val.prepare()


# Load and display random samples
class CheckConfig(CocoConfig):
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1


check_config = CheckConfig()

# image_ids = np.random.choice(dataset_train.image_ids, 1)
image_ids = dataset_train.image_ids
for image_id in image_ids:
    original_image, image_meta, gt_class_id, gt_bbox =\
        data.load_image_gt(dataset_train, check_config, image_id)

    # visualize.display_instances(original_image, gt_bbox, gt_class_id,
    #                             dataset_train.class_names, ax=get_ax())

#%% Create Model
# Create model in training mode
model = FasterRCNN(mode="training", config=config, model_dir=LOG_ROOT)
model.plot_model()
model.print_summary()

model_path = model.find_last()
model.load_weights(model_path, by_name=True)

model.train(dataset_train,
            dataset_val,
print("Images: {}\nClasses: {}".format(len(dataset.image_ids),
                                       dataset.class_names))

#%% Load Model
# Create model in inference mode
with tf.device(DEVICE):
    model = FasterRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights
model.load_weights(weights_path, by_name=True)

#%% Run Detection
image_id = random.choice(dataset.image_ids)
image, image_meta, gt_class_id, gt_bbox =\
    data.load_image_gt(dataset, config, image_id)
info = dataset.image_info[image_id]
print("image ID: {}.{} ({}) {}".format(info["source"], info["id"], image_id,
                                       dataset.image_reference(image_id)))
# Run object detection
results = model.detect([image], verbose=1)

# Display results
ax = get_ax(1)
r = results[0]
visualize.display_instances(image,
                            r['rois'],
                            r['class_ids'],
                            dataset.class_names,
                            r['scores'],
                            ax=ax,
# Recreate the model in inference mode
model = FasterRCNN(mode="inference",
                   config=inference_config,
                   model_dir=LOG_ROOT)
# model.plot_model()
# model.print_summary()

# Load trained weights
model_path = model.find_last("best")
model.load_weights(model_path, by_name=True)

#%% Test on a random image
image_id = random.choice(dataset_val.image_ids)
original_image, image_meta, gt_class_id, gt_bbox =\
    data.load_image_gt(dataset_val, inference_config, image_id)

log("original_image", original_image)
log("image_meta", image_meta)
log("gt_class_id", gt_class_id)
log("gt_bbox", gt_bbox)

visualize.display_instances(original_image,
                            gt_bbox,
                            gt_class_id,
                            dataset_train.class_names,
                            ax=get_ax())

t1 = time.time()
results = model.detect([original_image], verbose=1)
t2 = time.time()