IyatomiLab / LeafGAN

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test script leafgan

BartvanMarrewijk opened this issue · comments

I have succesfully trained the leaf gan model on a custom dataset but an error arise when using the test.py for the leaf_gan model.

The first error is that the threshold option is not defined in test_options.py. This was easily solved by copying from train_options.py.

The second error is that the netLFLseg is not defined in leaf_gan_model.py. This is caused by the if statements at line 80, why is the LFLSeg module in this if statement?

`

if self.isTrain:
  
      self.netD_A = networks.define_D(opt.output_nc, opt.ndf, opt.netD,											opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)
  
      self.netD_B = networks.define_D(opt.input_nc, opt.ndf, opt.netD,											opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)
  
      self.segResNet = models.resnet101()
  
      num_ftrs = self.segResNet.fc.in_features
  
      self.segResNet.fc = nn.Linear(num_ftrs, 3) 
  
      self.segResNet.load_state_dict(torch.load(load_path), strict=True)
      self.segResNet.to(self.device)
      self.segResNet.eval()
      # self.segResNet = torch.nn.DataParallel(self.segResNet, self.gpu_ids)
  
      self.netLFLSeg = GradCAM(model=self.segResNet)
      ######################################################

Is is safe to change it to this:

if self.isTrain:

    self.netD_A = networks.define_D(opt.output_nc, opt.ndf, opt.netD,											opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)

    self.netD_B = networks.define_D(opt.input_nc, opt.ndf, opt.netD,											opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)

self.segResNet = models.resnet101()

num_ftrs = self.segResNet.fc.in_features

self.segResNet.fc = nn.Linear(num_ftrs, 3) 

self.segResNet.load_state_dict(torch.load(load_path), strict=True)
self.segResNet.to(self.device)
self.segResNet.eval()
# self.segResNet = torch.nn.DataParallel(self.segResNet, self.gpu_ids)

self.netLFLSeg = GradCAM(model=self.segResNet)
		######################################################

`

Hi @studentWUR,

Testing the LeafGAN model doesn't require loading the LFLSeg module nor setting the threshold value.
In the test_option.py, the isTrain flag is set to False. So, the error you got above seems not to happen when you test the model.

Could you show me the bash command when you test your model? Also, could you check your test_option.py again?

Hi @huuquan1994 ,

Thank you for the quick reply. The isTrain is indeed set to false while running test.py. My test-options.py is exactly the same as on the git. Further, I did not make any large changes. See output ofr the terminal below


python test.py --dataroot ./datasets/dataset_standard/valid/ --name dataset_standard_leaf --model leaf_gan
----------------- Options ---------------
             aspect_ratio: 1.0                           
               batch_size: 1                             
          checkpoints_dir: ./checkpoints                 
                crop_size: 256                           
                 dataroot: ./datasets/dataset_standard/valid/	[default: ./datasets/dataset_standard/train/]
             dataset_mode: unaligned                     
                direction: AtoB                          
          display_winsize: 256                           
                    epoch: latest                        
                     eval: False                         
                  gpu_ids: 0                             
                init_gain: 0.02                          
                init_type: normal                        
                 input_nc: 3                             
                  isTrain: False                         	[default: None]
                load_iter: 0                             	[default: 0]
                load_size: 256                           
         max_dataset_size: inf                           
                    model: leaf_gan                      	[default: test]
               n_layers_D: 3                             
                     name: dataset_standard_leaf         	[default: experiment_name]
                      ndf: 64                            
                     netD: basic                         
                     netG: resnet_9blocks                
                      ngf: 64                            
               no_dropout: True                          
                  no_flip: False                         
                     norm: instance                      
                    ntest: inf                           
                 num_test: 50                            
              num_threads: 4                             
                output_nc: 3                             
                    phase: test                          
               preprocess: resize_and_crop               
              results_dir: ./results/                    
           serial_batches: False                         
                   suffix:                               
                threshold: 0.35                          
                  verbose: False                         
----------------- End -------------------
dataset [UnalignedDataset] was created
initialize network with normal
initialize network with normal
model [LeafGANModel] was created
loading the model from ./checkpoints/dataset_standard_leaf/latest_net_G_A.pth
loading the model from ./checkpoints/dataset_standard_leaf/latest_net_G_B.pth
---------- Networks initialized -------------
[Network G_A] Total number of parameters : 11.378 M
[Network G_B] Total number of parameters : 11.378 M
-----------------------------------------------
Traceback (most recent call last):
  File "test.py", line 60, in <module>
    model.test()           # run inference
  File "/home/ubuntu16/domain_adaptation/LeafGAN/models/base_model.py", line 105, in test
    self.forward()
  File "/home/ubuntu16/domain_adaptation/LeafGAN/models/leaf_gan_model.py", line 166, in forward
    self.background_real_A, self.foreground_real_A = self.get_masking(self.real_A, self.opt.threshold)
  File "/home/ubuntu16/domain_adaptation/LeafGAN/models/leaf_gan_model.py", line 120, in get_masking
    probs, idx = self.netLFLSeg.forward(tensor.to(torch.device('cuda:{}'.format(1))))
AttributeError: 'LeafGANModel' object has no attribute 'netLFLSeg'

Hi @studentWUR,

Thanks for your detailed log and sorry for the trouble.
I found that the instruction I wrote was wrong. When testing the LeafGAN, we should load it with the CycleGAN model. (Since we don't need the LFLSeg module, and the generator architecture of the CycleGAN model is the same as LeafGAN).

At the moment, please modify your command to this (i.e., load the trained LeafGAN model under CycleGAN architecture)

python test.py --dataroot ./datasets/dataset_standard/valid/ --name dataset_standard_leaf --model cycle_gan

Anyway, I'll modify the code of the leaf_gan_model.py so that we can run the test with the isTrain flag off.
Please wait a bit.

Hi @studentWUR

I've updated the code of the leaf_gan_model.py to run on testing.
Now the following two commands are equivalent. You can either run one of the followings

python test.py --dataroot ./datasets/dataset_standard/valid/ --name dataset_standard_leaf --model leaf_gan
# or
python test.py --dataroot ./datasets/dataset_standard/valid/ --name dataset_standard_leaf --model cycle_gan

If you find any problems, please feel free to ask me.

Hi @huuquan1994,

Thanks a lot it works perfectly now :)

Can you please share the complete source code. When I tried to execute what you have posted 1 of the package is missing.
I hope you will share It will help for my academics.
Thank you
Mail Id; soundaryagowda166@gmail.com

@soundaryabc123
Hi there, the source code I have pushed is the full source code.
You should be able to run it if you've installed all the required packages.

Could you send me your error logs?

@soundaryabc123
All required packages are in requirements.txt

You can install all of them via this command
pip install -r requirements.txt

@soundaryabc123
What do you mean by "which file to execute first"?
My code is based on the CycleGAN repository.
If you're not sure how to run my code, it's better to take a look at the CycleGAN's notebook.
After that, you should be able to run my code.

Next time, if you encounter any errors, make sure to post the error logs.

Hi @soundaryabc123,

Sorry for the late reply!
For the dataset, we are not allowed to share it because it belongs to a national project.

For executing the code, have you tried to run the Original CycleGAN code? I recommend you to try it first then come back hereafter. Sorry again but I can't do the meeting.

For the Colab link, I'll make it in my spare time (hopefully soon).

Anyway, if you encounter any problems, please raise a new issue.

@soundaryabc123
Of course, you can train with your own datasets.
I'll prepare the Colab or Notebook when I have time.