def evaluate(net_file, model_file): """ main """ #1, build model net_path = os.path.dirname(net_file) if net_path not in sys.path: sys.path.insert(0, net_path) from lenet import LeNet as MyNet #1, define network topology images = fluid.layers.data(name='image', shape=[1, 28, 28], dtype='float32') label = fluid.layers.data(name='label', shape=[1], dtype='int64') net = MyNet({'data': images}) prediction = net.layers['prob'] acc = fluid.layers.accuracy(input=prediction, label=label) place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) #2, load weights if model_file.find('.npy') > 0: net.load(data_path=model_file, exe=exe, place=place) else: net.load(data_path=model_file, exe=exe) #3, test this model test_program = fluid.default_main_program().clone() test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128) feeder = fluid.DataFeeder(feed_list=[images, label], place=place) fetch_list = [acc, prediction] print('go to test model using test set') acc_val = test_model(exe, test_program, \ fetch_list, test_reader, feeder) print('test accuracy is [%.4f], expected value[0.919]' % (acc_val))
import os import cv2 import numpy as np from lenet import LeNet data_dir = "mnist/test" net = LeNet() net.load("lenet.npy") files = os.listdir(data_dir) images = [] labels = [] for f in files: img = cv2.imread(os.path.join(data_dir, f), cv2.IMREAD_GRAYSCALE) img = cv2.resize(img, (32, 32)) img = img.astype(np.float32).reshape(32, 32, 1) / 255.0 images.append(img) labels.append(int(f[0])) x = np.array(images) y = np.array(labels) predict = net.predict(x) tp = np.sum(predict == y) accuracy = float(tp) / len(files) print("accuracy=%f" % accuracy)
def main(use_cuda): """ Advbox demo which demonstrate how to use advbox. """ TOTAL_NUM = 500 IMG_NAME = 'image' LABEL_NAME = 'label' weight_file = "fluid/lenet/lenet.npy" #1, define network topology images = fluid.layers.data(name=IMG_NAME, shape=[1, 28, 28], dtype='float32') # gradient should flow images.stop_gradient = False label = fluid.layers.data(name=LABEL_NAME, shape=[1], dtype='int64') net = LeNet({'data': images}) prediction = net.layers['prob'] cost = fluid.layers.cross_entropy(input=prediction, label=label) avg_cost = fluid.layers.mean(x=cost) #根据配置选择使用CPU资源还是GPU资源 place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() exe = fluid.Executor(place) #这句很关键 没有的话会报错 # AttributeError: 'NoneType' object has no attribute 'get_tensor' exe.run(fluid.default_startup_program()) #加载参数 net.load(data_path=weight_file, exe=exe, place=place) BATCH_SIZE = 1 test_reader = paddle.batch(paddle.reader.shuffle( paddle.dataset.mnist.test(), buf_size=128 * 10), batch_size=BATCH_SIZE) # advbox demo m = PaddleModel(fluid.default_main_program(), IMG_NAME, LABEL_NAME, prediction.name, avg_cost.name, (-1, 1), channel_axis=1) attack = FGSM(m) # attack = FGSMT(m) attack_config = {"epsilons": 0.3} # use test data to generate adversarial examples total_count = 0 fooling_count = 0 for data in test_reader(): total_count += 1 adversary = Adversary(data[0][0], data[0][1]) # FGSM non-targeted attack adversary = attack(adversary, **attack_config) if adversary.is_successful(): fooling_count += 1 print( 'attack success, original_label=%d, adversarial_label=%d, count=%d' % (data[0][1], adversary.adversarial_label, total_count)) else: print('attack failed, original_label=%d, count=%d' % (data[0][1], total_count)) if total_count >= TOTAL_NUM: print( "[TEST_DATASET]: fooling_count=%d, total_count=%d, fooling_rate=%f" % (fooling_count, total_count, float(fooling_count) / total_count)) break print("fgsm attack done")