BiomedSciAI / histocartography

A standardized Python API with necessary preprocessing, machine learning and explainability tools to facilitate graph-analytics in computational pathology.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

forward() missing 1 required positional argument: 'H'

Ramxnan opened this issue · comments

Got this error when trying to implement my own model into the histocartography model

`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
c:\Users\raman\OneDrive - softsensor.ai\histocartography\v1.ipynb Cell 1 in <cell line: 13>()
10 knn_graph_builder = KNNGraphBuilder(k=5, thresh=50, add_loc_feats=True)
12 image = np.array(Image.open('docs/_static/283_dcis_4.png'))
---> 13 nuclei_map, _ = nuclei_detector.process(image)
14 features = feature_extractor.process(image, nuclei_map)
15 cell_graph = knn_graph_builder.process(nuclei_map, features)

File c:\Users\raman\OneDrive - softsensor.ai\histocartography\histocartography\pipeline.py:138, in PipelineStep.process(self, output_name, *args, **kwargs)
135 return self._process_and_save(
136 *args, output_name=output_name, **kwargs)
137 else:
--> 138 return self._process(*args, **kwargs)

File c:\Users\raman\OneDrive - softsensor.ai\histocartography\histocartography\preprocessing\nuclei_extraction.py:118, in NucleiExtractor._process(self, input_image, tissue_mask)
106 def _process( # type: ignore[override]
107 self,
108 input_image: np.ndarray,
109 tissue_mask: Optional[np.ndarray] = None,
110 ) -> Tuple[np.ndarray, np.ndarray]:
111 """Extract nuclei from the input_image
112 Args:
113 input_image (np.array): Original RGB image
(...)
...
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []

TypeError: forward() missing 1 required positional argument: 'H'`

How do i proceed?

Hi @Ramxnan thanks for using Histocartography! Can you please give a bit more context to help us understand the issue?

Hi, sorry for being vague, im trying to implement my own model in the histocartography code instead of the existing pannuke.pt

image

Here im loading the VHUT model instead of pannuke,

while running the code:

import collections
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

from histocartography.preprocessing import NucleiExtractor, DeepFeatureExtractor, KNNGraphBuilder
nuclei_detector = NucleiExtractor()
feature_extractor = DeepFeatureExtractor(architecture='resnet34', patch_size=72)
knn_graph_builder = KNNGraphBuilder(k=5, thresh=50, add_loc_feats=True)
image = np.array(Image.open('/content/drive/MyDrive/Ramanan/histocartography/docs/_static/283_dcis_4.png'))
plt.imshow(image)
nuclei_map, _ = nuclei_detector.process(image)
features = feature_extractor.process(image, nuclei_map)
cell_graph = knn_graph_builder.process(nuclei_map, features)

calling nuclei_detector.process(image) throws an error as follows:

`Patch-level nuclei detection: 0%| | 0/13 [00:00<?, ?it/s]

TypeError Traceback (most recent call last)
in
----> 1 nuclei_map, _ = nuclei_detector.process(image)
2 features = feature_extractor.process(image, nuclei_map)
3 cell_graph = knn_graph_builder.process(nuclei_map, features)

5 frames
/content/drive/MyDrive/Ramanan/histocartography/histocartography/pipeline.py in process(self, output_name, *args, **kwargs)
136 *args, output_name=output_name, **kwargs)
137 else:
--> 138 return self._process(*args, **kwargs)
139
140 @AbstractMethod

/content/drive/MyDrive/Ramanan/histocartography/histocartography/preprocessing/nuclei_extraction.py in _process(self, input_image, tissue_mask)
117 Tuple[np.ndarray, np.ndarray, np.ndarray]: instance_map, instance_centroids
118 """
--> 119 return self._extract_nuclei(input_image, tissue_mask)
120
121 def _extract_nuclei(

/content/drive/MyDrive/Ramanan/histocartography/histocartography/preprocessing/nuclei_extraction.py in _extract_nuclei(self, input_image, tissue_mask)
159 image_batch = image_batch.to(self.device)
160 with torch.no_grad():
--> 161 out = self.model(image_batch).cpu()
162 for i in range(out.shape[0]):
163 left = coords[i][0] # left, bottom, right, top

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.7/dist-packages/torch/nn/parallel/data_parallel.py in forward(self, *inputs, **kwargs)
148 with torch.autograd.profiler.record_function("DataParallel.forward"):
149 if not self.device_ids:
--> 150 return self.module(*inputs, **kwargs)
151
152 for t in chain(self.module.parameters(), self.module.buffers()):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []

TypeError: forward() missing 1 required positional argument: 'H'`

I am afraid this is related to a pytorch version, can you try to reproduce the error on a separate environment with torch 1.7.0 ?

Sadly I get the same error with lower versions of torch as well, is there any resources you know which could help me?

This issue appears similar to pytorch/pytorch#71165