You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, i was trying to implement the tio.HistogramStandardization function to my code to do a segmentation neural network, but following the torchio examples I keep getting the same error when training the HistogramStandardization function: RuntimeWarning: divide by zero encountered in divide slopes = (s2 - s1) / (pc2 - pc1). I think the rest of the code doesn't have any problems so i don't know why this error might be appearing. The files of landmarks_{modality}.npy are also empty after the code has been executed. Here is my code:
root_dir = 'C:/Users/Alejandro/Desktop/ASNR-MICCAI-BraTS2023-GLI-Challenge-TrainingData'
output_dir = 'C:/Users/Alejandro/Desktop/BraTS Challenge CODE'
def collect_image_paths(root_dir):
volume_paths = {}
def process_directory(dir_path):
nonlocal volume_paths
for entry in os.scandir(dir_path):
if entry.is_dir():
process_directory(entry.path)
elif entry.is_file() and entry.name.endswith('.nii.gz'):
parts = entry.name.split('-')
if len(parts) >= 2:
modality = parts[-1].split('.')[0]
volume_paths.setdefault(modality, []).append(entry.path)
else:
print(f"Ignoring file: {entry.name}. Filename format doesn't match the expected pattern.")
process_directory(root_dir)
return volume_paths
def process_volume_paths(modality, volume_paths, output_dir):
landmarks = tio.HistogramStandardization.train(
volume_paths,
output_path=os.path.join(output_dir, f'landmarks_{modality}.pt')
)
np.save(os.path.join(output_dir, f'landmarks_{modality}.npy'), landmarks)
return modality, landmarks
volume_paths_dict = collect_image_paths(root_dir)
landmarks_dict = {}
with concurrent.futures.ThreadPoolExecutor() as executor:
future_to_modality = {
executor.submit(process_volume_paths, modality, volume_paths, output_dir): modality
for modality, volume_paths in volume_paths_dict.items()
}
for future in concurrent.futures.as_completed(future_to_modality):
modality = future_to_modality[future]
try:
modality, landmarks = future.result()
landmarks_dict[modality] = landmarks
except Exception as exc:
print(f"Exception processing {modality}: {exc}")
def transform():
include = ['t1c', 't1n', 't2f', 't2w', 'seg']
transform = tio.Compose([
tio.ToCanonical(include=include),
#tio.CropOrPad((128, 192, 128), include=include),
tio.RandomFlip(axes=['LR', 'AP', 'IS'], p=0.25, include=include),
tio.HistogramStandardization(landmarks_dict, include=include),
tio.ZNormalization(include=include),
tio.OneOf({
tio.RandomAffine(include=include): 0.7,
tio.RandomElasticDeformation(include=include): 0.3,
}),
tio.RandomNoise(p=0.2, include=include)
])
return transform
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, i was trying to implement the tio.HistogramStandardization function to my code to do a segmentation neural network, but following the torchio examples I keep getting the same error when training the HistogramStandardization function: RuntimeWarning: divide by zero encountered in divide slopes = (s2 - s1) / (pc2 - pc1). I think the rest of the code doesn't have any problems so i don't know why this error might be appearing. The files of landmarks_{modality}.npy are also empty after the code has been executed. Here is my code:
Beta Was this translation helpful? Give feedback.
All reactions