def call(self, context):
        args = context.args

        input_path = args.inputData1
        dicoms_path = os.path.join(input_path, "dicoms")
        data = json.load(os.path.join(input_path, "data.json"))

        try:
            case, spacing, instances = read_dicom.load_dicom2(dicoms_path)
Exemple #2
0
    def call(self, context):
        args = context.args

        input_path = args.inputData1
        input_npys_path = os.path.join(input_path, "npys")
        data = json.load(os.path.join(input_path, "data.json"))

        npys = [
            os.path.join(input_npys_path, "case.npy"),
            os.path.join(input_npys_path, "spacing.npy"),
            os.path.join(input_npys_path, "prep_mask.npy"),
        ]
        case, spacing, prep_mask = asyncio.map(npy.load,
                                               npys,
                                               thread=True,
                                               pbar=True,
                                               workers=len(npys))

        try:
            prep_data, extendbox, imgs, coord, nzhw = cpu_preprocess_2(
                case, spacing, prep_mask, self.split_comber)
        except FunctionTimedOut:
            raise Exception("Preprocess timeout")

        output_path = args.outputData1
        output_npys_path = path.mkdirs(os.path.join(output_path, "npys"))

        json.dump(data, os.path.join(output_path, "data.json"))
        npys = [
            (case, os.path.join(output_npys_path, "case.npy")),
            (spacing, os.path.join(output_npys_path, "spacing.npy")),
            (prep_data, os.path.join(output_npys_path, "prep_data.npy")),
            (prep_mask, os.path.join(output_npys_path, "prep_mask.npy")),
            (extendbox, os.path.join(output_npys_path, "extendbox.npy")),
            (imgs.numpy(), os.path.join(output_npys_path, "imgs.npy")),
            (coord.numpy(), os.path.join(output_npys_path, "coord.npy")),
            (nzhw, os.path.join(output_npys_path, "nzhw.npy")),
        ]
        asyncio.starmap(npy.dump,
                        npys,
                        thread=True,
                        pbar=True,
                        workers=len(npys))

        return output_path
Exemple #3
0
def get_balloon_dicts(img_dir, json_file):
    """
    Parsing via json
    """
    imgs_anns = json.load(json_file)

    dataset_dicts = []
    imagefile = [i.split(".jpg")[0] + ".jpg" for i in imgs_anns["metadata"].keys()]
    for idx, v in enumerate(set(imagefile)):
        record = {}

        indices = [i for i, x in enumerate(imagefile) if x == v]

        filename = os.path.join(img_dir, v)
        height, width = image.read(filename).shape[:2]

        record["file_name"] = filename
        record["image_id"] = idx
        record["height"] = height
        record["width"] = width

        objs = []
        for index in indices:
            data = list(imgs_anns["metadata"].values())[index]
            xy = data["xy"][1:]
            px = xy[::2]
            py = xy[1::2]
            poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
            poly = list(itertools.chain.from_iterable(poly))
            obj = {
                "bbox": [np.min(px), np.min(py), np.max(px), np.max(py)],
                "bbox_mode": BoxMode.XYXY_ABS,
                "segmentation": [poly],
                "category_id": 0,
                "iscrowd": 0,
            }
            objs.append(obj)

        record["annotations"] = objs
        dataset_dicts.append(record)
    return dataset_dicts
Exemple #4
0
    def call(self, context):
        args = context.args

        input_path = args.inputData1
        npys_path = os.path.join(input_path, "npys")
        data = json.load(os.path.join(input_path, "data.json"))

        npys = [
            os.path.join(npys_path, "case.npy"),
            os.path.join(npys_path, "spacing.npy"),
            os.path.join(npys_path, "prep_data.npy"),
            os.path.join(npys_path, "prep_mask.npy"),
            os.path.join(npys_path, "extendbox.npy"),
            os.path.join(npys_path, "imgs.npy"),
            os.path.join(npys_path, "coord.npy"),
            os.path.join(npys_path, "nzhw.npy"),
        ]
        case, spacing, prep_data, prep_mask, extendbox, imgs, coord, nzhw = asyncio.map(
            npy.load, npys, thread=True, pbar=True, workers=len(npys))
        imgs = torch.from_numpy(imgs)
        coord = torch.from_numpy(coord)

        try:
            nodule_df, pbb = self.lung_dete.prediction(imgs,
                                                       coord,
                                                       nzhw,
                                                       spacing,
                                                       extendbox,
                                                       batch=self.args.batch)
            preb = self.lung_isnc.nodule_cls(nodule_df, case, spacing)
            preb = self.lung_lobe(preb, case, prep_mask, spacing)
            data["nodule_preb"] = preb.to_dict()

            logger.info("Sending predicted images...")
            images = np.array([img for img in pickWithPbb(prep_data, pbb)])

            return data, convert.flatAsImage(images)
        except FunctionTimedOut:
            raise Exception("Predict timeout")
    def call(self, context):
        args = context.args

        input_path = args.inputData1
        input_npys_path = os.path.join(input_path, "npys")
        data = json.load(os.path.join(input_path, "data.json"))

        npys = [
            os.path.join(input_npys_path, "case.npy"),
            os.path.join(input_npys_path, "spacing.npy"),
        ]
        case, spacing = asyncio.map(npy.load,
                                    npys,
                                    thread=True,
                                    pbar=True,
                                    workers=len(npys))

        try:
            prep_mask = self.lung_segm.cut(case, 30)
        except FunctionTimedOut:
            raise Exception("Preprocess timeout")

        output_path = args.outputData1
        output_npys_path = path.mkdirs(os.path.join(output_path, "npys"))

        json.dump(data, os.path.join(output_path, "data.json"))
        npys = [
            (case, os.path.join(output_npys_path, "case.npy")),
            (spacing, os.path.join(output_npys_path, "spacing.npy")),
            (prep_mask, os.path.join(output_npys_path, "prep_mask.npy")),
        ]
        asyncio.starmap(npy.dump,
                        npys,
                        thread=True,
                        pbar=True,
                        workers=len(npys))

        return output_path
Exemple #6
0
def SPTrainer(context):
    args = context.args
    model = args.inputModel
    device = "cuda" if args.__gpu else "cpu"
    logger.info(
        "**********Use {} Device for Training**********".format(device))

    trainImg = args.inputTrainImage
    trainJson = os.path.join(args.inputTrainJson, "project.json")

    if args.inputTestImage is None or args.inputTestJson is None:
        testImg = trainImg
    else:
        testImg = args.inputTestImage

    if args.inputTestImage is None or args.inputTestJson is None:
        testJson = trainJson
    else:
        testJson = os.path.join(args.inputTestJson, "project.json")
    jsonData = json.load(trainJson)
    classes = list(jsonData["attribute"]["1"]["options"].values())
    model.dataset_register(trainImg, testImg, trainJson, testJson, classes)
    model.device(device)
    params = {
        "IMS_PER_BATCH": args.ImgsPerBatch,
        "BASE_LR": args.BaseLR,
        "MAX_ITER": args.MaxIter,
        "BATCH_SIZE_PER_IMAGE": args.BatchSizePerImg,
        "NUM_CLASSES": len(classes),
        "NUM_WORKERS": args.NumWorkers,
    }
    model.set_params(**params)
    trainer = model.train()
    model.evaluate(trainer, args.outputData)

    return model, args.outputData