Exemplo n.º 1
0
def predict(model,
            seriesuid,
            datapath='train/',
            ww=1500,
            wl=-400,
            detect_threshold=0):
    # df = pd.DataFrame(columns=['seriesuid', 'coordX', 'coordY', 'coordZ', 'class', 'probability'])

    mhd_file = os.path.join(root_dir, datapath, '{}.mhd'.format(seriesuid))
    itk_image = sitk.ReadImage(mhd_file)
    image3d = sitk.GetArrayViewFromImage(itk_image)

    columns = ['seriesuid', 'z', 'y1', 'x1', 'y2', 'x2', 'label', 'score']
    row_list = []
    for i in range(image3d.shape[0]):
        image = image3d[i]
        image = (normalize(image, ww, wl) * ww).astype(np.int32)
        image = image[..., np.newaxis]
        results = model.detect([image], verbose=0)
        r = results[0]
        if len(r['class_ids']) == 0:
            continue
        idx = r['scores'] > detect_threshold
        r['rois'] = r['rois'][idx]
        r['scores'] = r['scores'][idx]
        r['class_ids'] = r['class_ids'][idx]
        for j in range(len(r['class_ids'])):
            row_list.append([
                seriesuid, i, *r['rois'][j], r['class_ids'][j], r['scores'][j]
            ])
    df = pd.DataFrame(row_list, columns=columns)
    return df
Exemplo n.º 2
0
def segment_from_image(image_path):
    class_names = load_coco_names('coco.names')

    model = mrcnn.model.MaskRCNN(mode="inference",
                                 model_dir="./logs",
                                 config=InferenceConfig())
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    image = cv2.imread(image_path)
    img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    results = model.detect([img], verbose=1)[0]
    for i in range(len(results['rois'])):
        left = results['rois'][i, 1]
        top = results['rois'][i, 0]
        right = results['rois'][i, 3]
        bottom = results['rois'][i, 2]
        text = class_names[results['class_ids'][i]] + str(
            round(results['scores'][i], 3))
        mask = results['masks'][..., i]
        np.random.seed(results['class_ids'][i])
        mask = np.stack([
            mask.astype('int') * np.ones(image.shape[:-1]) *
            np.random.randint(0, 255, 1),
            mask.astype('int') * np.ones(image.shape[:-1]) *
            np.random.randint(0, 255, 1),
            mask.astype('int') * np.ones(image.shape[:-1]) *
            np.random.randint(0, 255, 1)
        ],
                        axis=2).astype('uint8')
        cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 1)
        cv2.putText(image, text, (left, top), cv2.FONT_HERSHEY_DUPLEX,
                    (right - left) / 300, (255, 255, 255), 1)
        image = cv2.addWeighted(image, 1, mask, 0.5, 0)
    cv2.imshow('result', image)
    cv2.waitKey()
Exemplo n.º 3
0
def evaluate_coco(model,
                  dataset,
                  coco,
                  eval_type="bbox",
                  limit=0,
                  image_ids=None):
    """Runs official COCO evaluation.
    dataset: A Dataset object with valiadtion data
    eval_type: "bbox" or "segm" for bounding box or segmentation evaluation
    limit: if not 0, it's the number of images to use for evaluation
    """
    # Pick COCO images from the dataset
    image_ids = image_ids or dataset.image_ids

    # Limit to a subset
    if limit:
        image_ids = image_ids[:limit]

    # Get corresponding COCO image IDs.
    coco_image_ids = [dataset.image_info[id]["id"] for id in image_ids]

    t_prediction = 0
    t_start = time.time()

    results = []
    for i, image_id in enumerate(image_ids):
        # Load image
        image = dataset.load_image(image_id)

        # Run detection
        t = time.time()
        r = model.detect([image], verbose=0)[0]
        t_prediction += (time.time() - t)

        # Convert results to COCO format
        # Cast masks to uint8 because COCO tools errors out on bool
        image_results = build_coco_results(dataset, coco_image_ids[i:i + 1],
                                           r["rois"], r["class_ids"],
                                           r["scores"],
                                           r["masks"].astype(np.uint8))
        results.extend(image_results)

    # Load results. This modifies results with additional attributes.
    coco_results = coco.loadRes(results)

    # Evaluate
    cocoEval = COCOeval(coco, coco_results, eval_type)
    cocoEval.params.imgIds = coco_image_ids
    cocoEval.evaluate()
    cocoEval.accumulate()
    cocoEval.summarize()

    print("Prediction time: {}. Average {}/image".format(
        t_prediction, t_prediction / len(image_ids)))
    print("Total time: ", time.time() - t_start)
Exemplo n.º 4
0
def inference(model,
              dataset,
              image_ids,
              config,
              detect_threshold=0,
              verbose=0):
    # Test on a random image
    ncol = 2
    nrow = len(image_ids)
    ax_size = 16 / ncol
    fig, axes = plt.subplots(nrow,
                             ncol,
                             figsize=(ax_size * ncol, ax_size * nrow))
    fig.tight_layout()

    for i, image_id in enumerate(image_ids):
        ax1 = axes[i, 0]
        ax2 = axes[i, 1]
        ax1.axis('off')
        ax2.axis('off')

        original_image, image_meta, gt_class_id, gt_bbox = mrcnn.model.load_image_gt(
            dataset, config, image_id, use_mini_mask=False)

        mrcnn.visualize.display_instances(original_image,
                                          gt_bbox,
                                          gt_class_id,
                                          dataset.class_names,
                                          ax=ax1,
                                          cmap=plt.cm.gray)

        results = model.detect([original_image], verbose=verbose)
        r = results[0]
        idx = r['scores'] > detect_threshold
        r['rois'] = r['rois'][idx]
        r['scores'] = r['scores'][idx]
        r['class_ids'] = r['class_ids'][idx]
        mrcnn.visualize.display_instances(original_image,
                                          r['rois'],
                                          r['class_ids'],
                                          dataset.class_names,
                                          r['scores'],
                                          ax=ax2,
                                          cmap=plt.cm.gray)
Exemplo n.º 5
0
    def main(self):
        print("detecting cars...")
        """Create a model object and load the weights."""
        model = mrcnn.model.MaskRCNN(mode="inference",
                                     model_dir=MODEL_DIR,
                                     config=self.config)

        model.load_weights(COCO_MODEL_PATH, by_name=True)
        """Check class numbers"""
        dataset = coco.CocoDataset()
        dataset.load_coco(MODEL_DIR, "train")
        dataset.prepare()
        """Load an image"""
        IMAGE = self.dir  #os.path.abspath("../../resources/images/stjohns.jpg")
        image = skimage.io.imread(IMAGE)
        results = model.detect([image], verbose=1)
        """Visualize results"""
        r = results[0]
        r = self.filter_vehicles(r)
        """Save image"""
        t = int(time.time())
        name = str(t) + ".png"
        path = os.path.abspath("../output")

        mrcnn.visualize.display_instances(path,
                                          name,
                                          image,
                                          r['rois'],
                                          r['masks'],
                                          r['class_ids'],
                                          dataset.class_names,
                                          r['scores'],
                                          title='# os detect cars: {}'.format(
                                              len(r['class_ids'])))

        return len(r['class_ids'])
Exemplo n.º 6
0
    # Number of classes = number of classes + 1 (+1 for the background). The background class is named BG
    NUM_CLASSES = len(CLASS_NAMES)


# Initialize the Mask R-CNN model for inference and then load the weights.
# This step builds the Keras model architecture.
model = mrcnn.model.MaskRCNN(mode="inference",
                             config=SimpleConfig(),
                             model_dir=os.getcwd())

# Load the weights into the model.
model.load_weights(filepath="mask_rcnn_coco.h5", by_name=True)

# load the input image, convert it from BGR to RGB channel
image = cv2.imread("sample_image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Perform a forward pass of the network to obtain the results
r = model.detect([image], verbose=0)

# Get the results for the first image.
r = r[0]

# Visualize the detected objects.
mrcnn.visualize.display_instances(image=image,
                                  boxes=r['rois'],
                                  masks=r['masks'],
                                  class_ids=r['class_ids'],
                                  class_names=CLASS_NAMES,
                                  scores=r['scores'])
Exemplo n.º 7
0
    # Number of classes = number of classes + 1 (+1 for the background). The background class is named BG
    NUM_CLASSES = len(CLASS_NAMES)


# Initialize the Mask R-CNN model for inference and then load the weights.
# This step builds the Keras model architecture.
model = mrcnn.model.MaskRCNN(mode="inference",
                             config=SimpleConfig(),
                             model_dir=os.getcwd())

# Load the weights into the model.
model.load_weights(filepath="mask_rcnn_coco.h5", by_name=True)

# load the input image, convert it from BGR to RGB channel
image = cv2.imread("test.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Perform a forward pass of the network to obtain the results
r = model.detect([image])

# Get the results for the first image.
r = r[0]

# Visualize the detected objects.
mrcnn.visualize.display_instances(image=image,
                                  boxes=r['rois'],
                                  masks=r['masks'],
                                  class_ids=r['class_ids'],
                                  class_names=CLASS_NAMES,
                                  scores=r['scores'])