JuliaWolleb/diffusion-anomaly

classifier_sample_known - IndexError: index 1 is out of bounds for dimension 1 with size 1

njan-creative opened this issue · 13 comments

While using the command below for translation, error occurs.


python scripts/classifier_sample_known.py --data_dir path_to_testdata --model_path ./results/model.pt --classifier_path ./results/classifier.pt --dataset brats_or_chexpert --classifier_scale 100 --noise_level 500 $MODEL_FLAGS $DIFFUSION_FLAGS $CLASSIFIER_FLAGS $SAMPLE_FLAGS


Traceback (most recent call last):
File "scripts/classifier_sample_known.py", line 215, in
main()
File "scripts/classifier_sample_known.py", line 149, in main
noise_level=args.noise_level
File "./guided_diffusion/gaussian_diffusion.py", line 922, in ddim_sample_loop_known
viz.image(visualize(final["sample"].cpu()[0,1, ...]), opts=dict(caption="final 1" ))
IndexError: index 1 is out of bounds for dimension 1 with size 1

what are the dimensions for your output final["sample"]?

I commented out the viz part, as visualization was not working for me,
I saved it as npy instead by changing the below code.

gathered_samples = [th.zeros_like(sample) for _ in range(dist.get_world_size())]
dist.all_gather(gathered_samples, sample)  # gather not supported with NCCL
print('Num gathered : ', len(gathered_samples), gathered_samples[0].shape)
samples_npy = [sample.cpu().numpy() for sample in gathered_samples]
save_path = './output/' + number[0] + '.npy'
np.save(save_path, samples_npy[0])

I downloaded the output from the training server and used the below code..
op_npy, and ip_npy are the paths for saved input and output .npy files.
However, the heat map does not seem to work.

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

def visualize(img):
    _min = img.min()
    _max = img.max()
    normalized_img = (img - _min)/ (_max - _min)
    return normalized_img

o_mat = np.load(op_npy).squeeze()
o_mat = np.clip(o_mat,0,1)
imgo = Image.fromarray(np.uint8(o_mat * 255) , 'L')
imgo.show()

i_mat = np.load(ip_npy).squeeze()
imgi = Image.fromarray(np.uint8(i_mat) , 'L')
imgi.show()

diff = i_mat - o_mat
#print(np.unique(diff))
#imgd = Image.fromarray(np.uint8(visualize(diff)*255) , 'L')
imgd.show()

hmap = visualize(diff)
hmap = np.round(hmap,2)
#print(np.unique(np.round(hmap,2)))

plt.imshow(hmap, cmap='hot') #, interpolation='nearest')
plt.show()

While using the command below for translation, error occurs.

python scripts/classifier_sample_known.py --data_dir path_to_testdata --model_path ./results/model.pt --classifier_path ./results/classifier.pt --dataset brats_or_chexpert --classifier_scale 100 --noise_level 500 $MODEL_FLAGS $DIFFUSION_FLAGS $CLASSIFIER_FLAGS $SAMPLE_FLAGS

Traceback (most recent call last): File "scripts/classifier_sample_known.py", line 215, in main() File "scripts/classifier_sample_known.py", line 149, in main noise_level=args.noise_level File "./guided_diffusion/gaussian_diffusion.py", line 922, in ddim_sample_loop_known viz.image(visualize(final["sample"].cpu()[0,1, ...]), opts=dict(caption="final 1" )) IndexError: index 1 is out of bounds for dimension 1 with size 1

I have encountered the same problem. How do you solve this problem?

Traceback (most recent call last):
File "scripts/classifier_sample_known.py", line 217, in
main()
File "scripts/classifier_sample_known.py", line 149, in main
noise_level=args.noise_level
File "../guided_diffusion/gaussian_diffusion.py", line 922, in ddim_sample_loop_known
viz.image(visualize(final["sample"].cpu()[0,1, ...]), opts=dict(caption="final 1" ))
IndexError: index 1 is out of bounds for dimension 1 with size 1
Is there a solution?Thank you

try
viz.image(visualize(final["sample"].cpu()[0,0, ...]), opts=dict(caption="final 1" ))

ok,Thank you very much

Excuse me,In my experiment, images with diseases were not reconstructed as healthy images, why?

try to adjust L and s according to your dataset, maybe this helps.

OK, thank you. I will try it. In my experiment, the input and output images appear to be consistent, so that no abnormal areas can be see.

image
Professor, is there a problem with my classifier loss?

image Professor, is there a problem with my classifier loss?

The same as your loss. Is this one correct?

Well, is this with the noisy images? This is possible, but just check the performance during image-to-image translation

Well, is this with the noisy images? This is possible, but just check the performance during image-to-image translation

Thanks for your reply. Another question is I found you print the grad while training. But I found that the gard is None. Is there any problem?
image