def load_params(self, args):
        """
        Load arguments
        """

        # Tile Size
        self.tile_size = int(args['--tile_size'])
        # Paths
        self.model_path = args['--model']
        # get absolute path for input directory - otherwise may give error in JP2Image.m
        self.input_dir = os.path.abspath(args['--input_dir'])
        self.output_dir = args['--output_dir']
        rm_n_mkdir(self.output_dir)
        self.logging_dir = args['--logging_dir']
        logging_dir = self.output_dir + '/' + self.logging_dir
        rm_n_mkdir(logging_dir)
        logger.set_logger_dir(logging_dir)

        self.logging_level = args['--logging_level']
        #TODO: this depends on tensorflow getting first crack at the logger (and adding the defailt std_out handler with INFO-level logging)
        logger._logger.handlers[0].setLevel(self.logging_level)
        logger._logger.setLevel(self.logging_level)

        # Processing
        self.batch_size = int(args['--batch_size'])
        # Below specific to WSI processing
        self.return_masks = args['--return_masks']

        self.tiss_lvl = 3  # default WSI level at which perform tissue segmentation
        print(f"'--tissue_level' provided:{args['--tissue_level']}")
        try:
            if args['--tissue_level'] and int(args['--tissue_level']) > 3:
                self.tiss_lvl = int(args['--tissue_level'])
        except:
            pass
Beispiel #2
0
    def process_all_files(self):
        """Process all image files within a directory. The function will:

        1) Load the image
        2) Extract patches the entire image
        3) Run inference
        4) Return instance prediction, overlay and JSON files

        """
        save_dir = self.output_dir
        file_list = glob.glob("%s/*" % self.input_dir)
        file_list.sort()  # ensure same order

        rm_n_mkdir(save_dir)

        pbar = tqdm.tqdm(desc='Processing Images', leave=True,
                         total=len(file_list),
                         ncols=80, ascii=True, position=0)

        for filename in file_list:
            filename = os.path.basename(filename)
            basename = os.path.splitext(filename)[0]

            rm_n_mkdir(save_dir + '/' + basename)

            ###
            img = cv2.imread(self.input_dir + "/" + filename)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

            ###
            pred_map = self.__gen_prediction(img, self.predictor)

            pred_inst, pred_info = process(
                pred_map, nr_types=self.nr_types, return_dict=True, return_probs=self.return_probs)

            overlaid_output = visualize_instances(img, pred_info, self.model_name)
            overlaid_output = cv2.cvtColor(overlaid_output, cv2.COLOR_BGR2RGB)

            cv2.imwrite("%s/%s/overlay.png" % (save_dir, basename), overlaid_output)
            np.save("%s/%s/instances.npy" % (save_dir, basename), pred_inst)

            # save result info as json file
            json_dict = {}
            for inst_id, inst_info in pred_info.items():
                new_inst_info = {}
                for info_name, info_value in inst_info.items():
                    # convert to JSON
                    if isinstance(info_value, np.ndarray):
                        info_value = info_value.tolist()
                    new_inst_info[info_name] = info_value
                json_dict[int(inst_id)] = new_inst_info
            with open("%s/%s/nuclei_dict.json" % (save_dir, basename), "w") as handle:
                json.dump(json_dict, handle)
            
            pbar.update()
        pbar.close()
Beispiel #3
0
    def process_all_wsi(self):
        """
        Process each WSI one at a time and save results as npz file
        """

        if os.path.isdir(self.output_dir) == False:
            rm_n_mkdir(self.output_dir)

        for filename in self.file_list:
            filename = os.path.basename(filename)
            self.basename = os.path.splitext(filename)[0]
            # this will overwrite file is it was processed previously
            rm_n_mkdir(self.output_dir + '/' + self.basename)
            start_time_total = time.time()
            self.process_wsi(filename)
            end_time_total = time.time()
            print('. FINISHED. Time: ',
                  time_it(start_time_total, end_time_total), 'secs')
Beispiel #4
0
    def process(self):
        """
        Process image files within a directory.
        For each image, the function will:
        1) Load the image
        2) Extract patches the entire image
        3) Run inference
        4) Return output numpy file and overlay
        """

        save_dir = self.output_dir
        file_list = glob.glob('%s/*' % self.input_dir)
        file_list.sort()  # ensure same order

        rm_n_mkdir(save_dir)
        for filename in file_list:
            filename = os.path.basename(filename)
            basename = os.path.splitext(filename)[0]
            print(self.input_dir, basename, end=' ', flush=True)
            print(filename)

            ###
            img = cv2.imread(self.input_dir + '/' + filename)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

            ###
            pred_map = self.__gen_prediction(img, self.predictor)

            pred_inst, pred_type = proc_utils.process_instance(
                pred_map, nr_types=self.nr_types)

            overlaid_output = visualize_instances(img, pred_inst, pred_type)
            overlaid_output = cv2.cvtColor(overlaid_output, cv2.COLOR_BGR2RGB)

            # combine instance and type arrays for saving
            pred_inst = np.expand_dims(pred_inst, -1)
            pred_type = np.expand_dims(pred_type, -1)
            pred = np.dstack([pred_inst, pred_type])

            cv2.imwrite('%s/%s.png' % (save_dir, basename), overlaid_output)
            np.save('%s/%s.npy' % (save_dir, basename), pred)
Beispiel #5
0
    def process_all_wsi(self):
        """
        Process each WSI one at a time and save results as npz file
        """

        if os.path.isdir(self.output_dir) == False:
            rm_n_mkdir(self.output_dir)

        for filename in self.file_list:
            filename = os.path.basename(filename)
            self.basename = os.path.splitext(filename)[0]
            # this will overwrite file is it was processed previously
            rm_n_mkdir(self.output_dir + '/' + self.basename)
            start_time_wsi = time.time()
            self.process_wsi(filename)
            end_time_wsi = time.time()
            logger.info(
                f"inferWSI- {self.basename} FINISHED. Time: {time_it(start_time_wsi, end_time_wsi)} secs"
            )

        logger.shutdown()
Beispiel #6
0
    def process_all_files(self):
        """Process each WSI and save results as JSON file"""

        if not os.path.exists(self.cache_dir):
            rm_n_mkdir(self.cache_dir)

        if not os.path.exists(self.output_dir):
            rm_n_mkdir(self.output_dir)

        for filename in self.file_list:
            filename = os.path.basename(filename)
            self.basename = os.path.splitext(filename)[0]
            self.output_dir_wsi = self.output_dir + "/" + self.basename
            # if not os.path.exists(self.output_dir_wsi):
            start = time.perf_counter()  # init timer

            rm_n_mkdir(self.output_dir_wsi)
            start_time_total = time.time()
            self.process_wsi(filename)

            end = time.perf_counter()
            print("Overall Time:", round(end-start), 'secs')