facebookresearch / ImageNet-Adversarial-Training

ImageNet classifier with state-of-the-art adversarial robustness
Other
674 stars 87 forks source link

Want to Convert to Pytorch #7

Closed goldblum closed 5 years ago

goldblum commented 5 years ago

Hi! I want to run some tests on this model and adapt it with some techniques using Pytorch. I don't have the computational resources to retrain the model. Is there a way for me to convert this model into Pytorch, loading the parameters and all? Thanks for your help!

ppwwyyxx commented 5 years ago

You can load the pre-trained weights by np.load. The graph architecture is written in the code (nets.py and resnet_model.py). So you have enough information to convert the model to Pytorch.

As far as I'm aware, there is currently no "automatic" way to translate a tensorflow model to pytorch. ONNX may get you half-way there? (but I'm not familiar with it)

a1600012888 commented 5 years ago

Hi, I have successfully converted the ResNet152 Baseline model to Pytorch. (I measured the accuracy under different attacks to verify it.) I can provide the code and the model.

goldblum commented 5 years ago

Awesome. I would appreciate that very much. This is the ResNet152 with adversarial training but no feature denoising?

a1600012888 commented 5 years ago

Awesome. I would appreciate that very much. This is the ResNet152 with adversarial training but no feature denoising?

Yes. I only translated the ResNet152 part. I hope sharing the code with you helps for converting the feature denoising part. code and model

goldblum commented 5 years ago

Great. Thank you so much for your help.

On Fri, Apr 12, 2019 at 9:51 AM Tianyuan Zhang notifications@github.com wrote:

Awesome. I would appreciate that very much. This is the ResNet152 with adversarial training but no feature denoising?

Yes. I only translated the ResNet152 part. I hope sharing the code with you helps for converting the feature denoising part. code and model https://drive.google.com/open?id=1KQSe91znWWwaUPG1TSpqUyfyw0o0VgMV

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/facebookresearch/ImageNet-Adversarial-Training/issues/7#issuecomment-482582306, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbAMBf2FyXoZ92MMsNjX_yxa9d-TPqHks5vgI9jgaJpZM4cfesX .

anianruoss commented 5 years ago

Hi @a1600012888, thank you for your script. I have been trying to reproduce the model accuracy but I can't get it to work.

I load the model with:

model = models.resnet152()
model.load_state_dict(
    torch.load('res152-adv-pytorch.model', map_location=lambda storage, loc: storage)
)
model.eval()

and ImageNet data as:

data_loader = torch.utils.data.DataLoader(
    datasets.ImageFolder(
            'ImageNet/validation',
            transform=transforms.Compose([
                transforms.Resize(256),
                transforms.CenterCrop(224),
                transforms.ToTensor().
                transforms.Normalize(mean=[.5,.5,.5], std=[.5,.5,.5])
            ])
        ),
    batch_size=batch_size,
    shuffle=True
)

but I always get 0% accuracy. I made sure to convert the images from RGB to BGR but that doesn't work either. Do you mind sharing how you managed to reproduce the accuracy?

a1600012888 commented 5 years ago

Hi, @anianruoss How about the BN statistics? The model I uploaded did not contains the running mean and running var of the BatchNorm layer.

anianruoss commented 5 years ago

@a1600012888 how did you perform this?

anianruoss commented 5 years ago

I tried to load the BN statistics like this:

    buffers = dict()                                                            

    for name, param in net.named_buffers():                                     
        try:                                                                    
            tf_param = torch_buffer_dic[name]                                   
        except KeyError:                                                        
            print(name, param.shape)                                            
            continue                                                            
        buffers[name] = torch.tensor(tf_param, dtype=param.dtype)               
        #print(name, 'buffer shape:{},  is_right:{}'.format(param.shape, param.shape == tf_param.shape))

    # check the number of weights.                                              
    print(len(torch_weight_dic), num_param)                                     
    net.load_state_dict(torch_weight_dic, strict=False)  # note that my code did not convert the buffer part. e.g.: running mean in BN

    model_dict = net.state_dict()                                               
    model_dict.update(buffers)                                                  
    net.load_state_dict(model_dict)    

but without success. Did you do something else to make it work?

a1600012888 commented 5 years ago

@a1600012888 how did you perform this?

I let this model runs on the training set with learning rate being zero. (With training mode.) You can carry out a more sophisticated BN post processing layer by layer.

anianruoss commented 5 years ago

@a1600012888 I don't have the computational resources to run the model on the training set. Could you upload the post-processed model if you have already performed this step?

luluvi commented 5 years ago

Awesome. I would appreciate that very much. This is the ResNet152 with adversarial training but no feature denoising?

hello,I also want to convert to pytorch,Can you successfully add feature denoising on pytorch, can you share it with me? Grateful. thanks!

luluvi commented 5 years ago

Awesome. I would appreciate that very much. This is the ResNet152 with adversarial training but no feature denoising?

Yes. I only translated the ResNet152 part. I hope sharing the code with you helps for converting the feature denoising part. code and model

Can you share your code again,I cannot open your link,thank you very much

Jack-lx-jiang commented 5 years ago

@a1600012888 Could you share more details about the evaluation? I can only get 56% accuracy on clean examples and 0% accuracy on adversarial examples. This is my date transformation code:

transforms.Compose([
        transforms.Resize(256, PIL.Image.BICUBIC),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])

and I transpose the image from RGB to BGR by: images = images[:, [2, 1, 0], :, :]

About the bn statistics, I tried two ways:

  1. load the weights of running mean and running variance directly from the weight file.
  2. Run on the training set with train mode and without backpropagation.

Unfortunately, I cannot get more than 56% accuracy either way. I do not know whether my data processing or the weight conversion goes wrong? Any advice is welcome! Thanks!

Equationliu commented 3 years ago

I cannot get more than 56% accuracy either

I cannot get 60%+ accuracy either, have you solved this?