Skip to content

Commit 1a74ebf

Browse files
author
wuysh
committed
python3 support
1 parent 86473f2 commit 1a74ebf

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

advbox/attacks/cw.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def _predict_adv(self, img_constrained):
316316
adv_logits = self.model.predict(img_constrained)
317317
adv_label = np.argmax(adv_logits)
318318

319-
return adv_label, adv_logits[0][adv_label] # adv_lab, adv_score
319+
return adv_label, adv_logits[adv_label] # adv_lab, adv_score
320320

321321

322322
CW_L2 = CW_L2_Attack

example/imagenet_example_fgsm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import logging
2424
import numpy as np
2525
import paddle.fluid as fluid
26-
import paddle.v2 as paddle
26+
import paddle
2727

2828
#classification
2929
import models

example/models/alexnet.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from builtins import object
12
import paddle
23
import paddle.fluid as fluid
34
import math
@@ -18,7 +19,7 @@
1819
}
1920

2021

21-
class AlexNet():
22+
class AlexNet(object):
2223
def __init__(self):
2324
self.params = train_parameters
2425

example/models/resnet.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from __future__ import division
2+
from builtins import str
3+
from builtins import range
4+
from builtins import object
5+
from past.utils import old_div
16
import paddle
27
import paddle.fluid as fluid
38
import math
@@ -58,7 +63,7 @@ def create_parameter(layers, shape, dtype):
5863

5964
return scale, bias, mean, variance
6065

61-
class ResNet():
66+
class ResNet(object):
6267
def __init__(self, layers=50):
6368
self.params = train_parameters
6469
self.layers = layers
@@ -129,7 +134,7 @@ def conv_bn_layer(self,
129134
num_filters=num_filters,
130135
filter_size=filter_size,
131136
stride=stride,
132-
padding=int((filter_size - 1) / 2),
137+
padding=int(old_div((filter_size - 1), 2)),
133138
groups=groups,
134139
param_attr=fluid.ParamAttr(name=param_name + '.w' + '_0'),
135140
act=None,

example/reader.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import division
12
# Copyright 2017 - 2018 Baidu Inc.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,6 +13,7 @@
1213
# See the License for the specific language governing permissions and
1314
# limitations under the License.
1415

16+
from past.utils import old_div
1517
import os
1618
import random
1719
import functools
@@ -40,8 +42,8 @@ def crop_image(img, target_size, center):
4042
width, height = img.size
4143
size = target_size
4244
if center == True:
43-
w_start = (width - size) / 2
44-
h_start = (height - size) / 2
45+
w_start = old_div((width - size), 2)
46+
h_start = old_div((height - size), 2)
4547
else:
4648
w_start = random.randint(0, width - size)
4749
h_start = random.randint(0, height - size)
@@ -62,7 +64,7 @@ def process_image(sample):
6264
if img.mode != 'RGB':
6365
img = img.convert('RGB')
6466

65-
img = np.array(img).astype('float32').transpose((2, 0, 1)) / 255
67+
img = old_div(np.array(img).astype('float32').transpose((2, 0, 1)), 255)
6668
img -= img_mean
6769
img /= img_std
6870
return [img], img_path

0 commit comments

Comments
 (0)