提交 b7744be3 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!111 Adjust the structure of tests package.

Merge pull request !111 from jxlang910/master
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for developed features of MindArmour.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes python unit tests for developed features of MindArmour.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for model attacks, model defenses and
their evaluation functions.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for white-box attack algorithms.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for black-box attack algorithms.
"""
......@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import numpy as np
import pytest
......@@ -24,9 +23,7 @@ from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import HopSkipJumpAttack
from mindarmour.utils.logger import LogUtil
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../../../../../"))
from example.mnist_demo.lenet5_net import LeNet5
from ut.python.utils.mock_net import Net
context.set_context(mode=context.GRAPH_MODE)
context.set_context(device_target="Ascend")
......@@ -64,25 +61,27 @@ def random_target_labels(true_labels):
def create_target_images(dataset, data_labels, target_labels):
res = []
for label in target_labels:
for i in range(len(data_labels)):
if data_labels[i] == label:
for i, data_label in enumerate(data_labels):
if data_label == label:
res.append(dataset[i])
break
return np.array(res)
# public variable
def get_model():
# upload trained network
current_dir = os.path.dirname(os.path.abspath(__file__))
ckpt_name = os.path.join(current_dir,
'../../test_data/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
net = LeNet5()
'../../../dataset/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
net = Net()
load_dict = load_checkpoint(ckpt_name)
load_param_into_net(net, load_dict)
net.set_train(False)
model = ModelToBeAttacked(net)
return model
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
......@@ -97,9 +96,9 @@ def test_hsja_mnist_attack():
# get test data
test_images_set = np.load(os.path.join(current_dir,
'../../test_data/test_images.npy'))
'../../../dataset/test_images.npy'))
test_labels_set = np.load(os.path.join(current_dir,
'../../test_data/test_labels.npy'))
'../../../dataset/test_labels.npy'))
# prediction accuracy before attack
model = get_model()
batch_num = 1 # the number of batches of attacking samples
......
......@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import numpy as np
import pytest
......@@ -24,9 +23,7 @@ from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import NES
from mindarmour.utils.logger import LogUtil
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../../../../../"))
from example.mnist_demo.lenet5_net import LeNet5
from ut.python.utils.mock_net import Net
context.set_context(mode=context.GRAPH_MODE)
context.set_context(device_target="Ascend")
......@@ -73,31 +70,35 @@ def _pseudorandom_target(index, total_indices, true_class):
def create_target_images(dataset, data_labels, target_labels):
res = []
for label in target_labels:
for i in range(len(data_labels)):
if data_labels[i] == label:
for i, data_label in enumerate(data_labels):
if data_label == label:
res.append(dataset[i])
break
return np.array(res)
def get_model(current_dir):
ckpt_name = os.path.join(current_dir,
'../../test_data/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
net = LeNet5()
'../../../dataset/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
net = Net()
load_dict = load_checkpoint(ckpt_name)
load_param_into_net(net, load_dict)
net.set_train(False)
model = ModelToBeAttacked(net)
return model
def get_dataset(current_dir):
# upload trained network
# get test data
test_images = np.load(os.path.join(current_dir,
'../../test_data/test_images.npy'))
'../../../dataset/test_images.npy'))
test_labels = np.load(os.path.join(current_dir,
'../../test_data/test_labels.npy'))
'../../../dataset/test_labels.npy'))
return test_images, test_labels
def nes_mnist_attack(scene, top_k):
"""
hsja-Attack test
......
......@@ -15,7 +15,6 @@
PointWise Attack test
"""
import os
import sys
import numpy as np
import pytest
......@@ -27,10 +26,7 @@ from mindarmour import BlackModel
from mindarmour.adv_robustness.attacks import PointWiseAttack
from mindarmour.utils.logger import LogUtil
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../../../../../"))
from example.mnist_demo.lenet5_net import LeNet5
from ut.python.utils.mock_net import Net
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
......@@ -65,16 +61,16 @@ def test_pointwise_attack_method():
# upload trained network
current_dir = os.path.dirname(os.path.abspath(__file__))
ckpt_name = os.path.join(current_dir,
'../../test_data/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
net = LeNet5()
'../../../dataset/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
net = Net()
load_dict = load_checkpoint(ckpt_name)
load_param_into_net(net, load_dict)
# get one mnist image
input_np = np.load(os.path.join(current_dir,
'../../test_data/test_images.npy'))[:3]
'../../../dataset/test_images.npy'))[:3]
labels = np.load(os.path.join(current_dir,
'../../test_data/test_labels.npy'))[:3]
'../../../dataset/test_labels.npy'))[:3]
model = ModelToBeAttacked(net)
pre_label = np.argmax(model.predict(input_np), axis=1)
LOGGER.info(TAG, 'original sample predict labels are :{}'.format(pre_label))
......
......@@ -90,7 +90,7 @@ def test_salt_and_pepper_attack_method():
labels = labels.astype(np.float32)
attack = SaltAndPepperNoiseAttack(model, sparse=False)
is_adv, adv_data, query_times = attack.generate(inputs, labels)
_, adv_data, _ = attack.generate(inputs, labels)
assert np.any(adv_data[0] != inputs[0]), 'Salt and pepper attack method: ' \
'generate value must not be equal' \
' to original value.'
......
......@@ -15,7 +15,6 @@
LBFGS-Attack test.
"""
import os
import sys
import numpy as np
import pytest
......@@ -25,9 +24,7 @@ from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindarmour.adv_robustness.attacks import LBFGS
from mindarmour.utils.logger import LogUtil
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../../../../"))
from example.mnist_demo.lenet5_net import LeNet5
from ut.python.utils.mock_net import Net
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
......@@ -50,16 +47,16 @@ def test_lbfgs_attack():
# upload trained network
current_dir = os.path.dirname(os.path.abspath(__file__))
ckpt_name = os.path.join(current_dir,
'../test_data/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
net = LeNet5()
'../../dataset/trained_ckpt_file/checkpoint_lenet-10_1875.ckpt')
net = Net()
load_dict = load_checkpoint(ckpt_name)
load_param_into_net(net, load_dict)
# get one mnist image
input_np = np.load(os.path.join(current_dir,
'../test_data/test_images.npy'))[:1]
'../../dataset/test_images.npy'))[:1]
label_np = np.load(os.path.join(current_dir,
'../test_data/test_labels.npy'))[:1]
'../../dataset/test_labels.npy'))[:1]
LOGGER.debug(TAG, 'true label is :{}'.format(label_np[0]))
classes = 10
target_np = np.random.randint(0, classes, 1)
......
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for defense algorithms.
"""
......@@ -23,10 +23,11 @@ from mindspore import context
from mindspore import nn
from mindspore.nn.optim.momentum import Momentum
from mock_net import Net
from mindarmour.adv_robustness.defenses import AdversarialDefense
from mindarmour.utils.logger import LogUtil
from ut.python.utils.mock_net import Net
LOGGER = LogUtil.get_instance()
TAG = 'Ad_Test'
......
......@@ -22,13 +22,14 @@ from mindspore import context
from mindspore import nn
from mindspore.nn.optim.momentum import Momentum
from mock_net import Net
from mindarmour.adv_robustness.attacks import FastGradientSignMethod
from mindarmour.adv_robustness.attacks import \
ProjectedGradientDescent
from mindarmour.adv_robustness.defenses import EnsembleAdversarialDefense
from mindarmour.utils.logger import LogUtil
from ut.python.utils.mock_net import Net
LOGGER = LogUtil.get_instance()
TAG = 'Ead_Test'
......
......@@ -22,10 +22,11 @@ from mindspore import context
from mindspore import nn
from mindspore.nn.optim.momentum import Momentum
from mock_net import Net
from mindarmour.adv_robustness.defenses import NaturalAdversarialDefense
from mindarmour.utils.logger import LogUtil
from ut.python.utils.mock_net import Net
LOGGER = LogUtil.get_instance()
TAG = 'Nad_Test'
......
......@@ -22,10 +22,11 @@ from mindspore import context
from mindspore import nn
from mindspore.nn.optim.momentum import Momentum
from mock_net import Net
from mindarmour.adv_robustness.defenses import ProjectedAdversarialDefense
from mindarmour.utils.logger import LogUtil
from ut.python.utils.mock_net import Net
LOGGER = LogUtil.get_instance()
TAG = 'Pad_Test'
......
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for detection algorithms of white-box attacks.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for detection algorithms of black-box attacks.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for the evaluations of white-box attack and defense.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for the evaluations of black-box attack and defense.
"""
......@@ -16,6 +16,8 @@ Radar map test.
"""
import pytest
from mindarmour.adv_robustness.evaluations import RadarMetric
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
......@@ -31,7 +33,7 @@ def test_radar_metric():
# create obj
_ = RadarMetric(metrics_name, metrics_data, metrics_labels, title='',
scale='sparse')
scale='sparse')
@pytest.mark.level0
......
# Copyright 2019 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
mocked model for UT of defense algorithms.
"""
import numpy as np
from mindspore import nn
from mindspore import Tensor
from mindspore.nn import WithLossCell, TrainOneStepCell
from mindspore.nn.optim.momentum import Momentum
from mindspore import context
from mindspore.common.initializer import TruncatedNormal
from mindarmour.adv_robustness.attacks import FastGradientSignMethod
def conv(in_channels, out_channels, kernel_size, stride=1, padding=0):
weight = weight_variable()
return nn.Conv2d(in_channels, out_channels,
kernel_size=kernel_size, stride=stride, padding=padding,
weight_init=weight, has_bias=False, pad_mode="valid")
def fc_with_initialize(input_channels, out_channels):
weight = weight_variable()
bias = weight_variable()
return nn.Dense(input_channels, out_channels, weight, bias)
def weight_variable():
return TruncatedNormal(0.02)
class Net(nn.Cell):
"""
Lenet network
"""
def __init__(self):
super(Net, self).__init__()
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16*5*5, 120)
self.fc2 = fc_with_initialize(120, 84)
self.fc3 = fc_with_initialize(84, 10)
self.relu = nn.ReLU()
self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
self.flatten = nn.Flatten()
def construct(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.conv2(x)
x = self.relu(x)
x = self.max_pool2d(x)
x = self.flatten(x)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
return x
if __name__ == '__main__':
num_classes = 10
batch_size = 32
sparse = False
context.set_context(mode=context.GRAPH_MODE)
context.set_context(device_target='Ascend')
# create test data
inputs_np = np.random.rand(batch_size, 1, 32, 32).astype(np.float32)
labels_np = np.random.randint(num_classes, size=batch_size).astype(np.int32)
if not sparse:
labels_np = np.eye(num_classes)[labels_np].astype(np.float32)
net = Net()
# test fgsm
attack = FastGradientSignMethod(net, eps=0.3)
attack.generate(inputs_np, labels_np)
# test train ops
loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=sparse)
optimizer = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()),
0.01, 0.9)
loss_net = WithLossCell(net, loss_fn)
train_net = TrainOneStepCell(loss_net, optimizer)
train_net.set_train()
train_net(Tensor(inputs_np), Tensor(labels_np))
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for model fuzzing.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for differential-privacy training and
privacy breach estimation.
"""
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for differential-privacy training and
privacy-budget estimation.
"""
......@@ -26,7 +26,7 @@ from mindarmour.privacy.diff_privacy import NoiseMechanismsFactory
from mindarmour.privacy.diff_privacy import ClipMechanismsFactory
from mindarmour.privacy.diff_privacy import DPOptimizerClassFactory
from test_network import LeNet5
from ut.python.utils.mock_net import Net
def dataset_generator(batch_size, batches):
......@@ -48,7 +48,7 @@ def test_dp_model_with_pynative_mode():
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
norm_bound = 1.0
initial_noise_multiplier = 0.01
network = LeNet5()
network = Net()
batch_size = 32
batches = 128
epochs = 1
......@@ -88,7 +88,7 @@ def test_dp_model_with_graph_mode():
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
norm_bound = 1.0
initial_noise_multiplier = 0.01
network = LeNet5()
network = Net()
batch_size = 32
batches = 128
epochs = 1
......@@ -126,7 +126,7 @@ def test_dp_model_with_graph_mode_ada_gaussian():
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
norm_bound = 1.0
initial_noise_multiplier = 0.01
network = LeNet5()
network = Net()
batch_size = 32
batches = 128
epochs = 1
......
......@@ -25,7 +25,7 @@ import mindspore.context as context
from mindarmour.privacy.diff_privacy import PrivacyMonitorFactory
from mindarmour.utils.logger import LogUtil
from test_network import LeNet5
from ut.python.utils.mock_net import Net
LOGGER = LogUtil.get_instance()
TAG = 'DP-Monitor Test'
......@@ -57,7 +57,7 @@ def test_dp_monitor():
suggest_epoch = rdp.max_epoch_suggest()
LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
suggest_epoch)
network = LeNet5()
network = Net()
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
......@@ -86,7 +86,7 @@ def test_dp_monitor_gpu():
suggest_epoch = rdp.max_epoch_suggest()
LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
suggest_epoch)
network = LeNet5()
network = Net()
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
......@@ -115,7 +115,7 @@ def test_dp_monitor_cpu():
suggest_epoch = rdp.max_epoch_suggest()
LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
suggest_epoch)
network = LeNet5()
network = Net()
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
......@@ -145,7 +145,7 @@ def test_dp_monitor_zcdp():
suggest_epoch = zcdp.max_epoch_suggest()
LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
suggest_epoch)
network = LeNet5()
network = Net()
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
......@@ -174,7 +174,7 @@ def test_dp_monitor_zcdp_gpu():
suggest_epoch = zcdp.max_epoch_suggest()
LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
suggest_epoch)
network = LeNet5()
network = Net()
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
......@@ -203,7 +203,7 @@ def test_dp_monitor_zcdp_cpu():
suggest_epoch = zcdp.max_epoch_suggest()
LOGGER.info(TAG, 'The recommended maximum training epochs is: %s',
suggest_epoch)
network = LeNet5()
network = Net()
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), 0.01, 0.9)
......
......@@ -19,7 +19,7 @@ from mindspore.train.model import Model
from mindarmour.privacy.diff_privacy import DPOptimizerClassFactory
from test_network import LeNet5
from ut.python.utils.mock_net import Net
@pytest.mark.level0
......@@ -29,7 +29,7 @@ from test_network import LeNet5
@pytest.mark.component_mindarmour
def test_optimizer():
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
network = LeNet5()
network = Net()
lr = 0.01
momentum = 0.9
micro_batches = 2
......@@ -47,7 +47,7 @@ def test_optimizer():
@pytest.mark.component_mindarmour
def test_optimizer_gpu():
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
network = LeNet5()
network = Net()
lr = 0.01
momentum = 0.9
micro_batches = 2
......@@ -65,7 +65,7 @@ def test_optimizer_gpu():
@pytest.mark.component_mindarmour
def test_optimizer_cpu():
context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
network = LeNet5()
network = Net()
lr = 0.01
momentum = 0.9
micro_batches = 2
......
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes unit tests for privacy breach estimation.
"""
......@@ -14,9 +14,6 @@
"""
membership inference test
"""
import os
import sys
import pytest
import numpy as np
......@@ -28,12 +25,12 @@ import mindspore.context as context
from mindarmour.privacy.evaluation import MembershipInference
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../"))
from defenses.mock_net import Net
from ut.python.utils.mock_net import Net
context.set_context(mode=context.GRAPH_MODE)
def dataset_generator(batch_size, batches):
"""mock training data."""
data = np.random.randn(batches*batch_size, 1, 32, 32).astype(
......
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This package includes common methods called by unit tests.
"""
......@@ -11,6 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
mocked model for UT of defense algorithms.
"""
from mindspore import nn
from mindspore.common.initializer import TruncatedNormal
......@@ -29,15 +32,15 @@ def fc_with_initialize(input_channels, out_channels):
def weight_variable():
return TruncatedNormal(0.05)
return TruncatedNormal(0.02)
class LeNet5(nn.Cell):
class Net(nn.Cell):
"""
Lenet network
"""
def __init__(self):
super(LeNet5, self).__init__()
super(Net, self).__init__()
self.conv1 = conv(1, 6, 5)
self.conv2 = conv(6, 16, 5)
self.fc1 = fc_with_initialize(16*5*5, 120)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册