pytorch cyclegann gives a Missing key error when testing
I have trained a model using the pix2pix pytorch implementation and would like to test it.
However when I test it I get the error
model [CycleGANModel] was created
loading the model from ./checkpoints/cycbw50/latest_net_G_A.pth
Traceback (most recent call last):
  File &quot;test.py&quot;, line 47, in &lt;module&gt;
    model.setup(opt)               # regular setup: load and print networks; create schedulers
  File &quot;/media/bitlockermount/SmartImageToDigitalTwin/SmartImageToDigitalTwin/bin/python/cyclegann/pytorch-CycleGAN-and-pix2pix/models/base_model.py&quot;, line 88, in setup
    self.load_networks(load_suffix)
  File &quot;/media/bitlockermount/SmartImageToDigitalTwin/SmartImageToDigitalTwin/bin/python/cyclegann/pytorch-CycleGAN-and-pix2pix/models/base_model.py&quot;, line 199, in load_networks
    net.load_state_dict(state_dict)
  File &quot;/home/bst/.local/lib/python3.8/site-packages/torch/nn/modules/module.py&quot;, line 846, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for ResnetGenerator:
    Missing key(s) in state_dict: &quot;model.1.bias&quot;, &quot;model.4.bias&quot;, &quot;model.7.bias&quot;, &quot;model.10.conv_block.1.bias&quot;, &quot;model.10.conv_block.5.bias&quot;, &quot;model.11.conv_block.1.bias&quot;, &quot;model.11.conv_block.5.bias&quot;, &quot;model.12.conv_block.1.bias&quot;, &quot;model.12.conv_block.5.bias&quot;, &quot;model.13.conv_block.1.bias&quot;, &quot;model.13.conv_block.5.bias&quot;, &quot;model.14.conv_block.1.bias&quot;, &quot;model.14.conv_block.5.bias&quot;, &quot;model.15.conv_block.1.bias&quot;, &quot;model.15.conv_block.5.bias&quot;, &quot;model.16.conv_block.1.bias&quot;, &quot;model.16.conv_block.5.bias&quot;, &quot;model.17.conv_block.1.bias&quot;, &quot;model.17.conv_block.5.bias&quot;, &quot;model.18.conv_block.1.bias&quot;, &quot;model.18.conv_block.5.bias&quot;, &quot;model.19.bias&quot;, &quot;model.22.bias&quot;. 
    Unexpected key(s) in state_dict: &quot;model.2.weight&quot;, &quot;model.2.bias&quot;, &quot;model.5.weight&quot;, &quot;model.5.bias&quot;, &quot;model.8.weight&quot;, &quot;model.8.bias&quot;, &quot;model.10.conv_block.2.weight&quot;, &quot;model.10.conv_block.2.bias&quot;, &quot;model.10.conv_block.6.weight&quot;, &quot;model.10.conv_block.6.bias&quot;, &quot;model.11.conv_block.2.weight&quot;, &quot;model.11.conv_block.2.bias&quot;, &quot;model.11.conv_block.6.weight&quot;, &quot;model.11.conv_block.6.bias&quot;, &quot;model.12.conv_block.2.weight&quot;, &quot;model.12.conv_block.2.bias&quot;, &quot;model.12.conv_block.6.weight&quot;, &quot;model.12.conv_block.6.bias&quot;, &quot;model.13.conv_block.2.weight&quot;, &quot;model.13.conv_block.2.bias&quot;, &quot;model.13.conv_block.6.weight&quot;, &quot;model.13.conv_block.6.bias&quot;, &quot;model.14.conv_block.2.weight&quot;, &quot;model.14.conv_block.2.bias&quot;, &quot;model.14.conv_block.6.weight&quot;, &quot;model.14.conv_block.6.bias&quot;, &quot;model.15.conv_block.2.weight&quot;, &quot;model.15.conv_block.2.bias&quot;, &quot;model.15.conv_block.6.weight&quot;, &quot;model.15.conv_block.6.bias&quot;, &quot;model.16.conv_block.2.weight&quot;, &quot;model.16.conv_block.2.bias&quot;, &quot;model.16.conv_block.6.weight&quot;, &quot;model.16.conv_block.6.bias&quot;, &quot;model.17.conv_block.2.weight&quot;, &quot;model.17.conv_block.2.bias&quot;, &quot;model.17.conv_block.6.weight&quot;, &quot;model.17.conv_block.6.bias&quot;, &quot;model.18.conv_block.2.weight&quot;, &quot;model.18.conv_block.2.bias&quot;, &quot;model.18.conv_block.6.weight&quot;, &quot;model.18.conv_block.6.bias&quot;, &quot;model.20.weight&quot;, &quot;model.20.bias&quot;, &quot;model.23.weight&quot;, &quot;model.23.bias&quot;. 

The opt file for the training:
----------------- Options ---------------
               batch_size: 1                             
                    beta1: 0.5                           
          checkpoints_dir: ./checkpoints                 
           continue_train: False                         
                crop_size: 256                           
                 dataroot: ./datasets/datasets/boundedwalls_50_0.1/ [default: None]
             dataset_mode: aligned                          [default: unaligned]
                direction: AtoB                          
              display_env: main                          
             display_freq: 400                           
               display_id: 1                             
            display_ncols: 4                             
             display_port: 8097                          
           display_server: http://localhost              
          display_winsize: 256                           
                    epoch: latest                        
              epoch_count: 1                             
                 gan_mode: lsgan                         
                  gpu_ids: 0                             
                init_gain: 0.02                          
                init_type: normal                        
                 input_nc: 3                             
                  isTrain: True                             [default: None]
                 lambda_A: 10.0                          
                 lambda_B: 10.0                          
          lambda_identity: 0.5                           
                load_iter: 0                                [default: 0]
                load_size: 286                           
                       lr: 0.0002                        
           lr_decay_iters: 50                            
                lr_policy: linear                        
         max_dataset_size: inf                           
                    model: cycle_gan                     
                 n_epochs: 100                           
           n_epochs_decay: 100                           
               n_layers_D: 3                             
                     name: cycbw50                          [default: experiment_name]
                      ndf: 64                            
                     netD: basic                         
                     netG: resnet_9blocks                
                      ngf: 64                            
               no_dropout: True                          
                  no_flip: False                         
                  no_html: False                         
                     norm: batch                            [default: instance]
              num_threads: 4                             
                output_nc: 3                             
                    phase: train                         
                pool_size: 50                            
               preprocess: resize_and_crop               
               print_freq: 100                           
             save_by_iter: False                         
          save_epoch_freq: 5                             
         save_latest_freq: 5000                          
           serial_batches: False                         
                   suffix:                               
         update_html_freq: 1000                          
                  verbose: False                         
----------------- End -------------------

And when testing it I used the following testing settings
python3 test.py --dataroot ./datasets/datasets/boundedwalls_50_0.1 --name cycbw50 --model pix2pix --netG resnet_9blocks --direction BtoA --dataset_mode aligned --norm batch --load_size 286

----------------- Options ---------------
             aspect_ratio: 1.0                           
               batch_size: 1                             
          checkpoints_dir: ./checkpoints                 
                crop_size: 256                           
                 dataroot: ./datasets/datasets/boundedwalls_50_0.1/ [default: None]
             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: cycle_gan                        [default: test]
               n_layers_D: 3                             
                     name: cycbw50                          [default: experiment_name]
                      ndf: 64                            
                     netD: basic                         
                     netG: resnet_9blocks                
                      ngf: 64                            
               no_dropout: True                          
                  no_flip: False                         
                     norm: instance                      
                 num_test: 50                            
              num_threads: 4                             
                output_nc: 3                             
                    phase: test                          
               preprocess: resize_and_crop               
              results_dir: ./results/                    
           serial_batches: False                         
                   suffix:                               
                  verbose: False                         
----------------- End -------------------

Does anybody see what I'm doing wrong here? I would like to run this network such that I get results for individual images, so far the test function seems the most promising but it just crashes on this neural network.

I think the problem here is some layer the bias=None but in testing the model required this, you should check the code for details.
After I check your config in train and test, the norm is different. For the code in GitHub, the norm difference may set the bias term is True or False.
if type(norm_layer) == functools.partial:
   use_bias = norm_layer.func == nn.InstanceNorm2d
else:
   use_bias = norm_layer == nn.InstanceNorm2d

model = [nn.ReflectionPad2d(3), 
         nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=use_bias),
         norm_layer(ngf),
         nn.ReLU(True)]

You can check it here.
