Пример #1
0
    def __init__(self, plugin_file_name: str, name: str = 'plugin-1'):
        r"""
        Loads a custom python file specified in `opprogram`, which 
        should contain a callable named "exec" such that 
        a call of `op_call(chunk, args)` operates on the chunk.
        """
        super().__init__(name=name)

        if not plugin_file_name.endswith('.py'):
            plugin_file_name += '.py'

        plugin_dir = path.join(path.dirname(path.realpath(__file__)),
                               '../plugins')
        plugin_dir1 = path.join(plugin_dir,
                                'chunkflow-plugins/chunkflowplugins')

        plugin_dirs = ['./', plugin_dir, plugin_dir1]
        if 'CHUNKFLOW_PLUGIN_DIR' in os.environ:
            plugin_dirs.append(os.environ['CHUNKFLOW_PLUGIN_DIR'])

        for plugin_dir in plugin_dirs:
            fname = path.join(plugin_dir, plugin_file_name)
            if path.exists(fname):
                logging.info(f'loading plugin {fname}')
                program = load_source(fname)
                # assuming this is a func / static functor for now, maybe make it a class?
                self.execute = program.execute
                break

        assert os.path.exists(fname), f'did not find plugin: {fname}'
        assert hasattr(self, 'execute')
Пример #2
0
    def __init__(self, plugin_file: str, name: str = 'plugin-1'):
        r"""
        Loads a custom python file specified in `opprogram`, which 
        should contain a callable named "exec" such that 
        a call of `op_call(chunk, args)` operates on the chunk.
        """
        super().__init__(name=name)

        if not plugin_file.endswith('.py'):
            plugin_file += '.py'

        if not path.exists(plugin_file):
            plugin_file = path.join(path.dirname(path.realpath(__file__)),
                                    '../plugins', path.basename(plugin_file))
            if not path.exists(plugin_file):
                plugin_file = path.join(
                    path.dirname(path.realpath(__file__)),
                    '../plugins/chunkflow-plugins/chunkflowplugins',
                    path.basename(plugin_file))

        assert path.exists(plugin_file)

        program = load_source(plugin_file)

        # assuming this is a func / static functor for now, maybe make it a class?
        self.execute = program.execute
Пример #3
0
    def __init__(self,
                 patch_size: Union[tuple, list],
                 patch_overlap: Union[tuple, list],
                 model_file_name: str,
                 weight_file_name: str,
                 use_batch_norm: bool = True,
                 is_static_batch_norm: bool = False,
                 num_output_channels: int = 3,
                 mask: np.ndarray = None):
        super().__init__(patch_size, patch_overlap)
        self.num_output_channels = num_output_channels
        if torch.cuda.is_available():
            self.is_gpu = True
            # put mask to gpu
            self.mask = torch.from_numpy(self.mask).cuda()
        else:
            self.is_gpu = False

        net_source = load_source(model_file_name)

        if hasattr(net_source, "load_model"):
            self.net = net_source.load_model(weight_file_name)
        else:
            self.net = net_source.InstantiatedModel
            chkpt = torch.load(weight_file_name)
            state_dict = chkpt['state_dict'] if 'state_dict' in chkpt else chkpt
            self.net.load_state_dict(state_dict)

        if self.is_gpu:
            self.net = self.net.cuda()
            # data parallel do not work with old emvision net
            #self.net = torch.nn.DataParallel(
            #    self.net, device_ids=range(torch.cuda.device_count()))

        # Print model's state_dict
        #print("Model's state_dict:")
        #for param_tensor in self.net.state_dict():
        #    print(param_tensor, "\t", self.net.state_dict()[param_tensor].size())

        if use_batch_norm and is_static_batch_norm:
            self.net.eval()

        if hasattr(net_source, "pre_process"):
            self.pre_process = net_source.pre_process
        else:
            self.pre_process = self._pre_process

        if hasattr(net_source, "post_process"):
            self.post_process = net_source.post_process
        else:
            self.post_process = self._identity
Пример #4
0
    def __init__(self, convnet_model: str, convnet_weight_path: str,
                 input_patch_size: tuple, 
                 output_patch_size: tuple, 
                 output_patch_overlap: tuple,
                 num_output_channels: int = 1, 
                 dtype: str='float32',
                 bump: str='wu'):
        # To-Do: support zung function
        assert bump == 'wu'
        super().__init__(input_patch_size, output_patch_size, 
                         output_patch_overlap, num_output_channels, 
                         dtype=dtype)

        self.num_output_channels = num_output_channels
   
        net_source = load_source(convnet_model)

        assert hasattr(net_source, "PatchInferencer")
        self.patch_inferencer = net_source.PatchInferencer(
            convnet_weight_path, 
            self.output_patch_mask,)
Пример #5
0
    def __init__(self,
                 convnet_model: str,
                 convnet_weight_path: str,
                 input_patch_size: tuple,
                 output_patch_size: tuple,
                 output_patch_overlap: tuple,
                 num_output_channels: int = 1,
                 dtype: str = 'float32',
                 bump: str = 'wu'):
        # To-Do: support zung function
        assert bump == 'wu'
        super().__init__(input_patch_size,
                         output_patch_size,
                         output_patch_overlap,
                         num_output_channels,
                         dtype=dtype)

        self.num_output_channels = num_output_channels
        if torch.cuda.is_available():
            self.is_gpu = True
            # put mask to gpu
            self.output_patch_mask = torch.from_numpy(
                self.output_patch_mask).cuda()
        else:
            self.is_gpu = False

        net_source = load_source(convnet_model)

        if hasattr(net_source, "load_model"):
            self.model = net_source.load_model(convnet_weight_path)
        else:
            self.model = net_source.InstantiatedModel
            if self.is_gpu:
                map_location = 'cuda'
            else:
                map_location = 'cpu'
            chkpt = torch.load(convnet_weight_path, map_location=map_location)
            state_dict = chkpt['state_dict'] if 'state_dict' in chkpt else chkpt
            self.model.load_state_dict(state_dict)

        if self.is_gpu and next(self.model.parameters()).is_cuda:
            self.model.cuda()

            # data parallel do not work with old emvision net
            #self.model = torch.nn.DataParallel(
            #    self.model, device_ids=range(torch.cuda.device_count()))

        # Print model's state_dict
        #print("Model's state_dict:")
        #for param_tensor in self.model.state_dict():
        #    print(param_tensor, "\t", self.model.state_dict()[param_tensor].size())

        if hasattr(net_source, "pre_process"):
            self.pre_process = net_source.pre_process
        else:
            self.pre_process = self._pre_process

        if hasattr(net_source, "post_process"):
            self.post_process = net_source.post_process
        else:
            self.post_process = self._identity