提取特征遇到问题
Raymvp opened this issue · 7 comments
你好!我在用CLAM提取特征的时候遇到类似的问题,在我提取到某张图片的时候出现以下报错,前面的图片都可以正常提取特征,下面是下半部分的报错,上半部分由于命令行窗口大小限制没能看到:
File "/home/ubuntu/anaconda3/envs/clam/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loop
data = fetcher.fetch(index)
File "/home/ubuntu/anaconda3/envs/clam/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 49, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/ubuntu/anaconda3/envs/clam/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 49, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/ubuntu/CLAM/datasets/dataset_h5.py", line 151, in getitem
img = self.wsi.read_region(coord, self.patch_level, (self.patch_size, self.patch_size)).convert('RGB')
File "/home/ubuntu/anaconda3/envs/clam/lib/python3.7/site-packages/openslide/init.py", line 237, in read_region
self._osr, location[0], location[1], level, size[0], size[1]
File "/home/ubuntu/anaconda3/envs/clam/lib/python3.7/site-packages/openslide/lowlevel.py", line 311, in read_region
_read_region(slide, buf, x, y, level, w, h)
File "/home/ubuntu/anaconda3/envs/clam/lib/python3.7/site-packages/openslide/lowlevel.py", line 225, in _check_error
raise OpenSlideError(err)
openslide.lowlevel.OpenSlideError: Corrupt JPEG data: premature end of data segment
请问您有遇到类似的情况吗
会有这个问题。
您可以在读取patch的地方,加上捕获openslide.lowlevel.OpenSlideError
异常的代码,以跳过部分无法读取的patch。
也就是这行代码附近,
img = self.wsi.read_region(coord, self.patch_level, (self.patch_size, self.patch_size)).convert('RGB')
。
我使用参数和CLAM一样。有时会遇到您提到的问题。据观察,少部分无法读取的patch一般都落在背景区域,所以在实验中,我们通过捕获异常的方式直接跳过了此类patch。
您还记得您的异常捕获代码吗?我这个代码会报错
def getitem(self, idx):
with h5py.File(self.file_path,'r') as hdf5_file:
coord = hdf5_file['coords'][idx]
try:
img = self.wsi.read_region(coord, self.patch_level, (self.patch_size, self.patch_size)).convert('RGB')
except openslide.lowlevel.OpenSlideError:
print(f"Failed to read region at coordinates: {coord}")
return None, None
if self.target_patch_size is not None:
img = img.resize(self.target_patch_size)
img = self.roi_transforms(img).unsqueeze(0)
return img, coord
您可以给下具体的错误信息
具体记不得了,通过查阅历史记录看到有此类报错。
File "/home/ubuntu/CLAM/utils/utils.py", line 41, in collate_features
img = torch.cat([item[0] for item in batch], dim = 0)
TypeError: expected Tensor as element 392 in argument 0, but got NoneType
您可以分享下您那段异常捕捉的代码吗
我用的代码跟你贴的类似。根据你提供的错误信息,貌似是错误是因为item中存在None,大概跟读取patch错误无关。