Skip to content

Commit

Permalink
python3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
wuysh committed Feb 18, 2020
1 parent 86473f2 commit 1a74ebf
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion advbox/attacks/cw.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def _predict_adv(self, img_constrained):
adv_logits = self.model.predict(img_constrained)
adv_label = np.argmax(adv_logits)

return adv_label, adv_logits[0][adv_label] # adv_lab, adv_score
return adv_label, adv_logits[adv_label] # adv_lab, adv_score


CW_L2 = CW_L2_Attack
Expand Down
2 changes: 1 addition & 1 deletion example/imagenet_example_fgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import logging
import numpy as np
import paddle.fluid as fluid
import paddle.v2 as paddle
import paddle

#classification
import models
Expand Down
3 changes: 2 additions & 1 deletion example/models/alexnet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import object
import paddle
import paddle.fluid as fluid
import math
Expand All @@ -18,7 +19,7 @@
}


class AlexNet():
class AlexNet(object):
def __init__(self):
self.params = train_parameters

Expand Down
9 changes: 7 additions & 2 deletions example/models/resnet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from __future__ import division
from builtins import str
from builtins import range
from builtins import object
from past.utils import old_div
import paddle
import paddle.fluid as fluid
import math
Expand Down Expand Up @@ -58,7 +63,7 @@ def create_parameter(layers, shape, dtype):

return scale, bias, mean, variance

class ResNet():
class ResNet(object):
def __init__(self, layers=50):
self.params = train_parameters
self.layers = layers
Expand Down Expand Up @@ -129,7 +134,7 @@ def conv_bn_layer(self,
num_filters=num_filters,
filter_size=filter_size,
stride=stride,
padding=int((filter_size - 1) / 2),
padding=int(old_div((filter_size - 1), 2)),
groups=groups,
param_attr=fluid.ParamAttr(name=param_name + '.w' + '_0'),
act=None,
Expand Down
8 changes: 5 additions & 3 deletions example/reader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import division
# Copyright 2017 - 2018 Baidu Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from past.utils import old_div
import os
import random
import functools
Expand Down Expand Up @@ -40,8 +42,8 @@ def crop_image(img, target_size, center):
width, height = img.size
size = target_size
if center == True:
w_start = (width - size) / 2
h_start = (height - size) / 2
w_start = old_div((width - size), 2)
h_start = old_div((height - size), 2)
else:
w_start = random.randint(0, width - size)
h_start = random.randint(0, height - size)
Expand All @@ -62,7 +64,7 @@ def process_image(sample):
if img.mode != 'RGB':
img = img.convert('RGB')

img = np.array(img).astype('float32').transpose((2, 0, 1)) / 255
img = old_div(np.array(img).astype('float32').transpose((2, 0, 1)), 255)
img -= img_mean
img /= img_std
return [img], img_path
Expand Down

0 comments on commit 1a74ebf

Please sign in to comment.