Ejemplo n.º 1
0
def predict_for_submit(model):
    """Predict on the test set for final submission.
    model: the model to test.
    limit: the images to be used.
    save: whether to save the masks.
    """
    for i in range(args.limit, 70):
        image = np.load(args.data + "image_test_np/liver_" + str(i) + ".npy").astype(np.float32)  # [512, 512, D]
        nib_image = nib.load(args.data + "imagesTs/test-volume-" + str(i) + ".nii.gz")
        affine = nib_image.affine
        ori_shape = nib_image.shape
        start_time = time.time()
        try:
            result = model.detect([image])[0]
            print("processing", i, "detect_time:", time.time() - start_time)
            mask = result["mask"]
            mask = resize(mask, ori_shape, order=0, preserve_range=True, anti_aliasing=False)
            vol = nib.Nifti1Image(mask.astype(np.uint8), affine)
            if not os.path.exists("./results/submissions/"):
                os.makedirs("./results/submissions/")
            nib.save(vol, "./results/submissions/test-segmentation-" + str(i) + ".nii")
        except:
            pass

    print("prediction completed")
Ejemplo n.º 2
0
def main():
    # Header & Page Config.
    st.set_page_config(
        page_title="Parth - Detr",
        layout="centered")
    st.title("Object Detection using DETR:")

    # This will let you upload PNG, JPG & JPEG File
    buffer = st.file_uploader("Upload your Image here", type = ["jpg", "png", "jpeg"])

    if buffer:
        # Object Detecting
        with st.spinner('Wait for it...'):
            # Slider for changing confidence
            confidence = st.slider('Confidence Threshold', 0, 100, 90)

            # Calculating time for detection
            t1 = time.time()
            im = Image.open(buffer)
            im.save("saved_images/image.jpg")
            res_img = detect("saved_images/image.jpg", confidence)
            t2 = time.time()
        
        # Displaying the image
        st.image(res_img, use_column_width = True)
        
        # Printing Time
        st.write("\n")
        st.write("Time taken: ", t2-t1, "sec.")
def load_object(file_name, model):
    model = modellib.MaskRCNN(
        mode="inference", model_dir=MODEL_DIR, config=config)
    image = skimage.io.imread(file_name)
    model = os.path.dirname
    results = model.detect([image], verbose=1)
    r = results[0]
    # visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
    #                             class_names, r['scores'])
    N = len(r['rois'])
    colors = random_colors(N)
    figsize = (16, 16)
    _, ax = plt.subplots(1, figsize=figsize)

    height, width = image.shape[:2]
    ax.axis('off')
    ax.margins(0, 0)
    color = (.2, 0.5, 0.9)
    captions = None
    masked_image = image.astype(np.uint32).copy()
    counts = {}
    output = []
    for i in range(N):
        y1, x1, y2, x2 = r['rois'][i]
    if not captions:
        caption = class_names[r['class_ids'][i]]
        if caption not in counts:
            counts[caption] = 1
            caption = caption+str(counts[caption])
        else:
            counts[caption] += 1
            caption = caption+str(counts[caption])
    else:
        caption = captions[i]
    ax.text(x1, y1 + 8, caption,
            color='w', size=11, backgroundcolor="none")
    output.append(caption)

    mask = r['masks'][:, :, i]
    masked_image = apply_mask(masked_image, mask, color)

    padded_mask = np.zeros(
        (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
    padded_mask[1:-1, 1:-1] = mask
    contours = find_contours(padded_mask, 0.5)
    for verts in contours:
        verts = np.fliplr(verts) - 1
        p = Polygon(verts, facecolor="none", edgecolor=color)
        ax.add_patch(p)

    # fig = ax.imshow(masked_image.astype(np.uint8))
    # fig.axes.get_xaxis().set_visible(False)
    # fig.axes.get_yaxis().set_visible(False)
    ax.axes.get_xaxis().set_visible(False)
    ax.axes.get_yaxis().set_visible(False)
    plt.savefig('show.jpg', bbox_inches='tight',
                pad_inches=0)
    return r
Ejemplo n.º 4
0
def classify():

    print('Starting...')
    data = request.form
    plant_type = data['plant_type']
    # print(base64.b64decode(request.files['file'].read())[:10])
    img = PILImage.create(base64.b64decode(request.files['file'].read()))
    # import pdb; pdb.set_trace()
    print('Detecting...')
    diagnosis = detect(img, plant_type)
    response = {'diagnosis': diagnosis}
    return jsonify(response)
Ejemplo n.º 5
0
def run_model(n_clicks, n_submit, iou, confidence, checklist, url):
    apply_nms = 'enabled' in checklist
    try:
        im = Image.open(requests.get(url, stream=True).raw)
    except:
        return go.Figure().update_layout(title='Incorrect URL')

    tstart = time.time()

    scores, boxes = detect(im, detr, transform, device=DEVICE)
    scores, boxes = filter_boxes(scores,
                                 boxes,
                                 confidence=confidence,
                                 iou=iou,
                                 apply_nms=apply_nms)

    scores = scores.data.numpy()
    boxes = boxes.data.numpy()

    tend = time.time()

    fig = pil_to_fig(im,
                     showlegend=True,
                     title=f'DETR Predictions ({tend-tstart:.2f}s)')
    existing_classes = set()

    for i in range(boxes.shape[0]):
        class_id = scores[i].argmax()
        label = CLASSES[class_id]
        confidence = scores[i].max()
        x0, y0, x1, y1 = boxes[i]

        # only display legend when it's not in the existing classes
        showlegend = label not in existing_classes
        text = f"class={label}<br>confidence={confidence:.3f}"

        add_bbox(
            fig,
            x0,
            y0,
            x1,
            y1,
            opacity=0.7,
            group=label,
            name=label,
            color=COLORS[class_id],
            showlegend=showlegend,
            text=text,
        )

        existing_classes.add(label)

    return fig, not apply_nms
Ejemplo n.º 6
0
    def __init__(self):
        # create root window
        self.main_window = tk.Tk()
        self.main_window.pack_propagate(0)
        self.main_window.geometry("500x700")
        self.main_window.title("ECSU Protein Anomaly Labeling")
        self.main_window.configure(bg="SlateGray3")
        self.main_window.resizable(0, 0)

        # create frames
        self.font = Font(family='Times', size=24, weight="bold")
        self.frame1 = tk.LabelFrame(self.main_window,
                                    text="Inputs",
                                    bg="SlateGray3",
                                    fg="RoyalBlue3",
                                    bd=5,
                                    relief="groove",
                                    font=self.font)  # create frame 1
        self.frame1.place(x=10, y=10, anchor="nw", height=80, width=480)

        self.frame2 = tk.LabelFrame(self.main_window,
                                    text="Sequence",
                                    bg="SlateGray3",
                                    fg="RoyalBlue3",
                                    bd=5,
                                    relief="groove",
                                    font=self.font)  # create frame 2
        self.frame2.place(x=10, y=90, anchor="nw", height=200, width=480)

        self.frame3 = tk.LabelFrame(self.main_window,
                                    text="Labels",
                                    bg="SlateGray3",
                                    fg="RoyalBlue3",
                                    bd=5,
                                    relief="groove",
                                    font=self.font)  # create frame 3
        self.frame3.place(x=10, y=290, anchor="nw", height=400, width=480)

        # add components into frame1
        self.label1 = tk.Label(self.frame1,
                               text="Chain",
                               relief="groove",
                               bg="SlateGray3",
                               fg="blue4",
                               font=Font(family="helvetica",
                                         size=16,
                                         weight="bold"))
        self.label1.place(x=5, y=5, anchor="nw", height=30, width=80)
        self.options = control.getChains()
        #self.options = ["A", "B"]
        self.variable = tk.StringVar(self.frame1)
        self.variable.set(self.options[0])
        self.menu = tk.OptionMenu(self.frame1, self.variable, *self.options)
        self.menu.config(bg="SlateGray3")
        self.menu.place(x=90, y=5, anchor="nw", height=30, width=100)
        #self.menu.bind('<Activate>', self.choose_chain)
        self.label2 = tk.Label(self.frame1,
                               text="Threshold",
                               relief="groove",
                               bg="SlateGray3",
                               fg="blue4",
                               font=Font(family="helvetica",
                                         size=16,
                                         weight="bold"))
        self.label2.place(x=210, y=5, anchor="nw", height=30, width=120)
        self.threhold = tk.StringVar(self.frame1, value='10')
        self.entry = tk.Entry(self.frame1,
                              textvariable=self.threhold,
                              bd=1,
                              relief="flat",
                              justify="center",
                              bg="SlateGray3",
                              fg="blue4")
        self.entry.config(highlightbackground="SlateGray3")
        self.entry.place(x=340, y=5, height=30, width=50, anchor="nw")
        self.button = tk.Button(self.frame1,
                                text="Check",
                                relief="groove",
                                bd=1,
                                bg="SlateGray3",
                                fg="blue4",
                                font=Font(family="helvetica",
                                          size=16,
                                          weight="bold"),
                                command=self.update)
        self.button.place(x=390, y=10, anchor="nw")

        # data
        self.current_chain = self.variable.get()
        self.current_threshold = float(self.entry.get())
        self.mol_name = control.getMolName()
        #print 'Molecule name:', self.mol_name
        #print 'Threhold:', self.current_threshold

        os.chdir(
            os.path.dirname(os.path.realpath(__file__))
        )  # change the working directory, the default directory is root which is not writable
        #print 'Working directory: ', os.path.dirname(os.path.realpath(__file__))
        self.labels = model.detect(
            self.mol_name,
            self.current_threshold)  # information of labeled residues
        #print(self.labels)

        # Chimera display
        rc('color blue')  # display whole molecule as blue

        # add components into frame2

        tk.mainloop()
Ejemplo n.º 7
0
def test(model, limit, save, bbox):
    """Test the model.
    model: the model to test.
    limit: the images to be used.
    save: whether to save the masks.
    limit: whether to draw the bboxes.
    """
    per_class_ious = []
    info = json.load(open(args.data + "dataset.json"))
    info = list(info['train_and_test'])
    detect_time = 0
    for path in info[:limit]:
        path_image = path['image']
        path_label = path['label']
        image = nib.load(path_image).get_data().copy()
        label = nib.load(path_label)  # load the gt-masks
        affine = label.affine  # prepared to save the predicted mask later
        label = label.get_data().copy()
        image = np.expand_dims(image, -1)
        start_time = time.time()
        result = model.detect([image])[0]
        detect_time += time.time() - start_time
        print("detect_time:", time.time() - start_time)
        """The shape of result: a dict containing
        {
            "rois": final_rois,           [N, (y1, x1, z1, y2, x2, z2)] in real coordinates
            "class_ids": final_class_ids, [N]
            "scores": final_scores,       [N]
            "mask": final_mask,           [mask_shape[0], mask_shape[1], mask_shape[2]]
        }"""
        rois = result["rois"]
        class_ids = result["class_ids"]
        scores = result["scores"]
        mask = result["mask"]
        # Prepare the gt-masks and pred-masks to calculate the ious.
        gt_masks = np.zeros((image.shape[0], image.shape[1], image.shape[2],
                             model.config.NUM_CLASSES - 1))
        pred_masks = np.zeros((image.shape[0], image.shape[1], image.shape[2],
                               model.config.NUM_CLASSES - 1))
        # Generate the per instance gt masks.
        for j in range(model.config.NUM_CLASSES - 1):
            gt_masks[:, :, :, j][label == j + 1] = 1
        # Generate the per instance predicted masks.
        for j in range(model.config.NUM_CLASSES - 1):
            pred_masks[:, :, :, j][mask == j + 1] = 1
        # calculate different kind of ious
        per_class_iou = utils.compute_per_class_mask_iou(gt_masks, pred_masks)
        per_class_ious.append(per_class_iou)
        # Save the results
        if save == "true":
            # Draw bboxes
            if bbox == "true":
                y1, x1, z1, y2, x2, z2 = rois[0, :]
                mask[y1, x1:x2, z1] = 10
                mask[y1, x1:x2, z2] = 10
                mask[y2, x1:x2, z1] = 10
                mask[y2, x1:x2, z2] = 10
                mask[y1:y2, x1, z1] = 10
                mask[y1:y2, x2, z1] = 10
                mask[y1:y2, x1, z2] = 10
                mask[y1:y2, x2, z2] = 10
                mask[y1, x1, z1:z2] = 10
                mask[y1, x2, z1:z2] = 10
                mask[y2, x1, z1:z2] = 10
                mask[y2, x2, z1:z2] = 10
            vol = nib.Nifti1Image(mask.astype(np.int32), affine)
            if not os.path.exists("./results"):
                os.makedirs("./results")
            nib.save(
                vol, "./results/" + str(per_class_iou.mean()) + "_" +
                path_image[-17:])
        print(path_image[-17:] + " detected done. iou = " + str(per_class_iou))
    print("Test completed.")
    # Print the iou results.
    per_class_ious = np.array(per_class_ious)
    print("per class iou mean:", np.mean(per_class_ious, axis=0))
    print("std:", np.std(per_class_ious, axis=0))
    print("Total ious mean:", per_class_ious.mean())
    print("Total detect time:", detect_time)
Ejemplo n.º 8
0
from Utils import Generate_Dataset,SplitPDF
from config import window_size,model_path
from model import build_model,detect
from Preprocessor import Preprocessor
import argparse


parser = argparse.ArgumentParser(description='Enter A file to detect its EOD')
parser.add_argument('PDF_Path')
args = parser.parse_args()
PDF_Path = args.PDF_Path

if __name__ == '__main__':

    print("Loading and preprocessing the PDF file")
    processor = Preprocessor(PDF_Path)
    preprocessed = processor.Preprocess()

    print("File successfully loaded and processed")
    Generator = Generate_Dataset(preprocessed,window_size)
    X_testCNN,X_testLSTM = Generator.generate()

    print("Building the model")
    MainModel = build_model()
    EOD = detect(MainModel,model_path,X_testLSTM,X_testCNN)

    print("Saving the splitted files")
    splitter = SplitPDF(PDF_Path,EOD)
    splitter.split()
 def test_firearm_distance_close(self):
     score = detect("")
     self.assertTrue(score > threshold)
Ejemplo n.º 10
0
 def test_firearm_indoor(self):
     score = detect("")
     self.assertTrue(score > threshold)
Ejemplo n.º 11
0
 def test_firearm_outdoor_rain(self):
     score = detect("")
     self.assertTrue(score > threshold)
Ejemplo n.º 12
0
                           image_id, use_mini_mask=False)

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

visualize.display_instances(original_image,
                            gt_bbox,
                            gt_mask,
                            gt_class_id,
                            dataset_train.class_names,
                            figsize=(8, 8))

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

r = results[0]

visualize.display_instances(original_image, r['rois'], r['masks'],
                            r['class_ids'], dataset_val.class_names,
                            r['scores'])

####################################################################

#            EVALUATING THE MEAN AVERAGE PRECISION                 #

####################################################################

# Compute VOC-Style mAP @ IoU=0.5
# Running on 30 images. Increase for better accuracy.
Ejemplo n.º 13
0
def test(model):
    """Test the model."""
    save_path = "./results/" + args.weights[27:] + "/"
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    per_class_ious = []
    box_iou = []
    detect_time = 0
    for i in range(args.limit, 131):
        image = np.load(args.data + "image_np/liver_" + str(i) + ".npy").astype(np.float32)  # [H, W, D]
        label = np.load(args.data + "label_np/liver_label_" + str(i) + ".npy").astype(np.int32)  # [H, W, D, num_class]
        gt_bbox = utils.extract_bboxes(label)
        gt_bbox = utils.extend_bbox(gt_bbox, label.shape)
        nib_label = nib.load("/media/disk1/LiTS/labelTr/segmentation-" + str(i) + ".nii.gz")
        affine = nib_label.affine  # prepared to save the predicted mask later
        ori_shape = nib_label.shape
        try:
            start_time = time.time()
            result = model.detect([image])[0]
            detect_time += time.time() - start_time
            print("detect_time:", time.time() - start_time)
            """The shape of result: a dict containing
            {
                "rois": final_rois,           [N, (y1, x1, z1, y2, x2, z2)] in real coordinates
                "class_ids": final_class_ids, [N]
                "scores": final_scores,       [N]
                "mask": final_mask,           [mask_shape[0], mask_shape[1], mask_shape[2]]
            }"""
            rois = result["rois"]
            class_ids = result["class_ids"]
            scores = result["scores"]
            mask = result["mask"]
            if args.stage == 'beginning':
                mask = np.zeros(mask.shape).astype(np.int32)
            rois = rois.clip(min=0)
            rois[:, 3] = rois[:, 3].clip(max=mask.shape[0] - 1)
            rois[:, 4] = rois[:, 4].clip(max=mask.shape[1] - 1)
            rois[:, 5] = rois[:, 5].clip(max=mask.shape[2] - 1)
            rois = rois.astype(np.int32)
            # import pdb
            # pdb.set_trace()
            # compute bbox iou
            print("gt_bbox:", gt_bbox, "pred_bbox:", rois)
            box_iou.append(utils.compute_overlaps(np.array([gt_bbox]), rois)[0, 0])
            if args.stage != 'beginning':
                # Prepare the gt-masks and pred-masks to calculate the ious. [H, W, D, num_classes - 1]
                gt_masks = np.zeros(label.shape[:3] + (model.config.NUM_CLASSES - 1,))
                pred_masks = np.zeros(image.shape + (model.config.NUM_CLASSES - 1,))
                for j in range(model.config.NUM_CLASSES - 1):
                    gt_masks[:, :, :, j][label == j + 1] = 1
                    pred_masks[:, :, :, j][mask == j + 1] = 1
                # calculate different kind of ious
                per_class_iou = utils.compute_per_class_mask_iou(gt_masks, pred_masks)
                per_class_ious.append(per_class_iou)
            # Save the results
            if args.save:
                # Draw bboxes
                if args.bbox:
                    for j in range(rois.shape[0]):
                        y1, x1, z1, y2, x2, z2 = rois[j, :]
                        mask[y1:y2, x1:x2, z1:z2] = 100
                mask = resize(mask, ori_shape, order=0, mode='constant', preserve_range=True, anti_aliasing=False)
                vol = nib.Nifti1Image(mask.astype(np.uint8), affine)
                if args.stage != 'beginning':
                    nib.save(vol, save_path + str(per_class_iou.mean()) + "_liver_" + str(i) + ".nii.gz")
                    print("liver_" + str(i) + " detected done. iou = " + str(per_class_iou))
                else:
                    nib.save(vol, save_path + str(box_iou[-1]) + "_liver_" + str(i) + ".nii.gz")
                    print("liver_" + str(i) + " detected done. box_iou = " + str(box_iou[-1]))
        except:
            print("detect error!")
            pass
    print("Test completed.")
    # Print the iou results.
    box_iou = np.array(box_iou)
    print("box iou:", box_iou)
    print("mean:", box_iou.mean())
    if args.stage != 'beginning':
        per_class_ious = np.array(per_class_ious)
        print("per class iou mean:", np.mean(per_class_ious, axis=0))
        print("std:", np.std(per_class_ious, axis=0))
        print("Total ious mean:", per_class_ious.mean())
        print("Total detect time:", detect_time)