action='append', default=None)
    group1.add_argument('--transfo-random', type=parse_transfo, nargs='+',
            action='append', default=None)
    group2 = parser.add_mutually_exclusive_group(required=True)
    group2.add_argument('--lmdb', type=str, default=None)
    group2.add_argument('--npz', type=str, default=None)
    args = parser.parse_args()

    print "Load model"
    net = caffe.Net(args.proto, args.model, caffe.TEST)
    caffe.set_mode_cpu()
    print "args", vars(args)
    if args.lmdb != None:
        reader = lmdb_reader(args.lmdb)
    if args.npz != None:
        reader = npz_reader(args.npz)

    if args.transfo_grid:
        trs = utils.parse_transfo_grid(args.transfo_grid)
    if args.transfo_random:
        trs = utils.parse_transfo_random(args.transfo_random)

    trs_len = len(trs)
    print "Transformations: %s" % ("\n\t".join(map(repr, trs)))

    count = 0
    accuracies = defaultdict(list) # extreme disto value correctly classified

    print "Test network against transformations"
    def process( (i, image, label) ):
        res = []
示例#2
0
import numpy as np
import argparse
from collections import defaultdict
import multiprocessing
from utils import npz_reader, gen_adversial

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--in-npz', type=str, required=True)
    parser.add_argument('--out-npz', type=str, required=True)
    parser.add_argument('--proto', type=str, required=True)
    parser.add_argument('--model', type=str, required=True)
    args = parser.parse_args()

    print "Load dataset and model"
    reader = npz_reader(args.in_npz)

    net = caffe.Net(args.proto, args.model, caffe.TEST)
    caffe.set_mode_cpu()

    labels = set(range(0, 10)) # FIXME: suppose MNIST
    def process( (i, img, l) ):
        mylabels = labels - set([ l ])
        return [ gen_adversial(net, img, l, tl) for tl in mylabels ]

    num_cores = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(num_cores)
    res = defaultdict(list)
    count = 0
    try:
        for i, xss in enumerate(pool.imap_unordered(process, reader)):