JuliaWolleb / diffusion-anomaly

Anomaly detection with diffusion models
MIT License
119 stars 23 forks source link

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

Open njan-creative opened 1 year ago

njan-creative commented 1 year ago

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

JuliaWolleb commented 1 year ago

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

njan-creative commented 1 year ago

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()
85zhanghao commented 1 year ago

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?

xyt000-xjj commented 1 year ago

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

JuliaWolleb commented 1 year ago

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

xyt000-xjj commented 1 year ago

ok,Thank you very much

xyt000-xjj commented 1 year ago

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

JuliaWolleb commented 1 year ago

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

xyt000-xjj commented 1 year ago

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.

xyt000-xjj commented 1 year ago

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

Yingshu-Li commented 1 year ago

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

The same as your loss. Is this one correct?

JuliaWolleb commented 1 year ago

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

Yingshu-Li commented 1 year ago

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