Home

Awesome

tf_to_torch_model

In this repo, we convert some common Tensorflow models used in adversarial attacks to PyTorch models and provide the resultant models. Since these models are converted from their Tensorflow version, the inputs need the same normalization, i.e., [-1,1]. We have already done this, so you can use it directly.

model = nn.Sequential(
    # Images for inception classifier are normalized to be in [-1, 1] interval.
    Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5]), 
    net.KitModel(model_path).eval().cuda())

logit = model(input)

We also provide the PyTorch code for you to implement attacks on our converted models, e.g., I-FGSM (run the following command):

python torch_attack.py

File Description

dataset: Test images.

nets: Original tensorflow models.

nets_weight: Put the original Tensorflow network weight file into this directory.

torch_nets: Converted torch model.

torch_nets_weight: Put the converted Pytorch network weight file into this directory. (You can find them in Releases)

tf_attack.py: Sample attack method with tensorflow.

torch_attack.py: Sample attack method with PyTorch.

Model Accuracy

The following table shows the source of the converted model and the accuracy of the model on the 1000 test pictures (selected from Imagenet) given.

Converted modelModel sourcetorch Accuracy(%)tf Accuracy(%)input size
tf2torch_inception_v3inception_v3_2016_08_2896.2096.20299*299
tf2torch_inception_v4inception_v4_2016_09_0997.4097.40299*299
tf2torch_resnet_v2_50resnet_v2_50_2017_04_1494.9094.90299*299
tf2torch_resnet_v2_101resnet_v2_101_2017_04_1496.3096.30299*299
tf2torch_resnet_v2_152resnet_v2_152_2017_04_1495.8095.80299*299
tf2torch_inc_res_v2inception_resnet_v2_2016_08_3099.8099.80299*299
tf2torch_adv_inception_v3adv_inception_v3_2017_08_1894.9094.90299*299
tf2torch_ens3_adv_inc_v3ens3_adv_inception_v3_2017_08_1893.7093.70299*299
tf2torch_ens4_adv_inc_v3ens4_adv_inception_v3_2017_08_1891.6091.60299*299
tf2torch_ens_adv_inc_res_v2ens_adv_inception_resnet_v2_2017_08_1897.6097.60299*299

Implementation of sample attack

This table shows our result / paper result ("*" indicates white-box attack). The paper result is from Patch-wise Attack for Fooling Deep Neural Network, and we can see that we have obtained similar results with the converted model. The specific parameter settings can be found in the paper.

attack methodinc_v3*inc_v4resnet_v2_152inc_res_v2ens3_adv_inc_v3ens4_adv_inc_v3ens_adv_inc_res_v2
FGSM81.0/80.937.4/38.033.0/33.133.9/33.116.9/16.815.7/15.88.2/8.3
I-FGSM100.0/100.030.1/29.619.4/19.421.4/20.312.0/11.712.4/12.15.5/5.5
MI-FGSM100.0/100.055.1/54.142.8/43.551.7/50.922.2/21.921.6/21.111.2/10.5
DI-FGSM99.7/99.855.3/54.233.4/32.143.5/43.615.9/15.016.4/16.28.6/7.1
TI-FGSM////31.2/30.831.1/30.622.9/22.7
PI-FGSM100.0/100.057.5/58.647.6/45.052.2/51.338.4/39.339.0/39.528.0/28.8

Note !

  1. If you want to use aux_logits, using aux_logits=True to create the model:
model = nn.Sequential(
    # Images for inception classifier are normalized to be in [-1, 1] interval.
    Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5]), 
    net.KitModel(model_path, aux_logits=True).eval().cuda())
    
logits, aux_logits = model(input)
  1. Models with aux_logits:

    • tf2torch_inception_v3,
    • tf2torch_inception_v4,
    • tf2torch_inc_res_v2,
    • tf2torch_adv_inception_v3,
    • tf2torch_ens3_adv_inc_v3,
    • tf2torch_ens4_adv_inc_v3.