JuliaWolleb / diffusion-anomaly

Anomaly detection with diffusion models
MIT License
123 stars 25 forks source link

Trouble with Inferencing #12

Open Mitchnoff opened 1 year ago

Mitchnoff commented 1 year ago

I believe I have successfully trained embeddings by following the steps for the classifier and the model itself. I made a shell script to do the inferencing:

MODEL_FLAGS="--image_size 256 --num_channels 128 --class_cond True --num_res_blocks 2 --num_heads 1 --learn_sigma True --use_scale_shift_norm False --attention_resolutions 16"
DIFFUSION_FLAGS="--diffusion_steps 1000 --noise_schedule linear --rescale_learned_sigmas False --rescale_timesteps False"
TRAIN_FLAGS="--lr 1e-4 --batch_size 10"
CLASSIFIER_FLAGS="--image_size 256 --classifier_attention_resolutions 32,16,8 --classifier_depth 4 --classifier_width 32 --classifier_pool attention --classifier_resblock_updown True --classifier_use_scale_shift_norm True"
SAMPLE_FLAGS="--batch_size 1 --num_samples 1 --timestep_respacing ddim1000 --use_ddim True"

# tried for classifier_path:
# ./_results_big/emabrats2update_0.9999_350000.pt
# ./_results_big/brats2update350000.pt
# ./_results_big/optbrats2update340000.pt
# ./_results_2/modelbratsclass149999.pt

python scripts/classifier_sample_known.py \
    --data_dir /path/to/data/chexpert/testing/ \
    --model_path ./_results_big/brats2update350000.pt \
    --classifier_path ./_results_2/modelbratsclass149999.pt \
    --dataset chexpert \
    --classifier_scale 100 \
    --noise_level 500 \
    $MODEL_FLAGS $DIFFUSION_FLAGS $CLASSIFIER_FLAGS 

IMPORTANT NOTE: for the model path and classifier path renamed the old results folders to _results_big and _results_2 that contained results from previous runs. This is why in the shell script I have the .pt files in different directories than the typical results directory.

Another note is that I am training on the Chexpert dataset, however the names of the .pt files are brats for some reason.

From what I gathered these are the correct models to use but I am not certain. When I run inference I get this error:

Traceback (most recent call last):
  File "scripts/classifier_sample_known.py", line 215, in <module>
    main()
  File "scripts/classifier_sample_known.py", line 142, in main
    sample, x_noisy, org = sample_fn(
  File "./guided_diffusion/gaussian_diffusion.py", line 569, in p_sample_loop_known
    for sample in self.p_sample_loop_progressive(
  File "./guided_diffusion/gaussian_diffusion.py", line 691, in p_sample_loop_progressive
    viz.image(visualize(img[0, 1,...]), opts=dict(caption=str(i)))
IndexError: index 1 is out of bounds for dimension 1 with size 1

Any tips on how to resolve this are greatly appreciated as I am uncertain how to progress.

JuliaWolleb commented 1 year ago

Hi Yes on the Chexpert dataset, the names of the .pt files should not be brats of course. You can change that to chexpert or whatever you like. The error message seems to be only for visualization. The problem is that you try to plot the second channel, but on Chexpert there is only a one-channel image. Therefore, you need to change it to viz.image(visualize(img[0, 0,...]), opts=dict(caption=str(i))). However, I am wondering why you end up in _p_sample_loopknown rather than _ddim_sample_loopknown. did you set use_ddim=True? I think you forgot the $SAMPLE_FLAGS when you apply the command

python scripts/classifier_sample_known.py \
    --data_dir /path/to/data/chexpert/testing/ \
    --model_path ./_results_big/brats2update350000.pt \
    --classifier_path ./_results_2/modelbratsclass149999.pt \
    --dataset chexpert \
    --classifier_scale 100 \
    --noise_level 500 \
    $MODEL_FLAGS $DIFFUSION_FLAGS $CLASSIFIER_FLAGS $SAMPLE_FLAGS
xyt000-xjj commented 1 year ago

hello! I want to ask, is it OK to use my own dataset to directly divide the dataset into healthy and diseased ones? Do I need to label it with other labels

xyt000-xjj commented 1 year ago

QQ图片20230312095419 My classification loss is a little strange

ZZZGGGG commented 1 year ago

QQ图片20230312095419 My classification loss is a little strange

Hello, I have been reproducing this paper recently. When I was training the classifier model, my loss did not decrease. May I consult your parameter setting? The dataset I use is chexpert, and then the healthy label is 1 and the diseased label is 0