MadryLab / robustness

A library for experimenting with, training and evaluating neural networks, with a focus on adversarial robustness.
MIT License
903 stars 181 forks source link

Cannot load model CIFAR-10 - epsilon = 0.25 or 1.0 #105

Closed vietvo89 closed 2 years ago

vietvo89 commented 2 years ago

Hi

When loading "cifar_l2_0_5.pt", it was successful.

=> loading checkpoint '../robustness/checkpoints/resnet50/cifar_l2_0_5.pt'
=> loaded checkpoint '../robustness/checkpoints/resnet50/cifar_l2_0_5.pt' (epoch 153)

But I could not load model "cifar_l2_0_25.pt" and I got an error as below. I am not sure what is going wrong. I used the same code to load both models but the model 0.5 is ok, model 0.25 or even 1.0 are not successful loading.

=> loading checkpoint '../robustness/checkpoints/resnet50/cifar_l2_0_25.pt'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_26509/4233603415.py in <module>
      1 from robustness.model_utils import make_and_restore_model
----> 2 model, _ = make_and_restore_model(arch='resnet50', dataset=ds,
      3              resume_path='../robustness/checkpoints/resnet50/cifar_l2_0_25.pt')
      4 model.eval()
      5 pass

~/anaconda3/envs/bayes_attack/lib/python3.8/site-packages/robustness/model_utils.py in make_and_restore_model(arch, dataset, resume_path, parallel, pytorch_pretrained, add_custom_forward, *_)
     91     if resume_path and os.path.isfile(resume_path):
     92         print("=> loading checkpoint '{}'".format(resume_path))
---> 93         checkpoint = ch.load(resume_path, pickle_module=dill)
     94 
     95         # Makes us able to load models saved with legacy versions

~/anaconda3/envs/bayes_attack/lib/python3.8/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
    583                     return torch.jit.load(opened_file)
    584                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--> 585         return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
    586 
    587 

~/anaconda3/envs/bayes_attack/lib/python3.8/site-packages/torch/serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
    763     unpickler = pickle_module.Unpickler(f, **pickle_load_args)
    764     unpickler.persistent_load = persistent_load
--> 765     result = unpickler.load()
    766 
    767     deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

~/anaconda3/envs/bayes_attack/lib/python3.8/site-packages/dill/_dill.py in load(self)
    523 
    524     def load(self): #NOTE: if settings change, need to update attributes
--> 525         obj = StockUnpickler.load(self)
    526         if type(obj).__module__ == getattr(_main_module, '__name__', '__main__'):
    527             if not self._ignore:

TypeError: an integer is required (got type bytes)
vietvo89 commented 2 years ago

When I tested it with a clean model "cifar_nat.pt", it worked. I properly guess it could be something wrong with models 0.25 and 1.0.

motoight commented 2 years ago

I am facing the same problem

Buhua-Liu commented 2 years ago

Changing the environment from python=3.8.8 to python=3.6.13 solved the problem for me. @vietvo89 @motoight

vietvo89 commented 2 years ago

Thank @Buhua-Liu, in fact, there are two ways of coping with this problem. One is to use python < 3.8 while the other way is to save the model as in #71 and #81