示例#1
0
def main():

    X_train, Y_train, X_test, Y_test, Y_test_uncat = data_setup()

    # Create data shards
    random_indices = np.random.choice(
        len(X_train), len(X_train), replace=False)
    X_train_permuted = X_train[random_indices]
    Y_train_permuted = Y_train[random_indices]
    X_train_shards = np.split(X_train_permuted, args.k)
    Y_train_shards = np.split(Y_train_permuted, args.k)

    if args.mal:
        # Load malicious data
        mal_data_X, mal_data_Y, true_labels = mal_data_setup(X_test, Y_test, Y_test_uncat)

    if args.train:
        p = Process(target=master)
        p.start()
        p.join()

        manager = Manager()
        return_dict = manager.dict()
        return_dict['eval_success'] = 0.0
        return_dict['eval_loss'] = 0.0

        if args.mal:
            return_dict['mal_suc_count'] = 0
            t_final = train_fn(X_train_shards, Y_train_shards, X_test, Y_test_uncat,
                               return_dict, mal_data_X, mal_data_Y)
            print('Malicious agent succeeded in %s of %s iterations' %
                  (return_dict['mal_suc_count'], t_final * args.mal_num))
        else:
            _ = train_fn(X_train_shards, Y_train_shards, X_test, Y_test_uncat,
                         return_dict)
    else:
        manager = Manager()
        return_dict = manager.dict()
        return_dict['eval_success'] = 0.0
        return_dict['eval_loss'] = 0.0
        if args.mal:
            return_dict['mal_suc_count'] = 0
        for t in range(args.T):
            if not os.path.exists(gv.dir_name + 'global_weights_t%s.npy' % t):
                print('No directory found for iteration %s' % t)
                break
            if args.mal:
                p_eval = Process(target=eval_func, args=(
                    X_test, Y_test_uncat, t, return_dict, mal_data_X, mal_data_Y))
            else:
            	p_eval = Process(target=eval_func, args=(
                    X_test, Y_test_uncat, t, return_dict))

            p_eval.start()
            p_eval.join()

        if args.mal:
            print('Malicious agent succeeded in %s of %s iterations' %
                  (return_dict['mal_suc_count'], (t-1) * args.mal_num))
import global_vars as gv
from utils.mnist import model_mnist
from utils.io_utils import data_setup, mal_data_setup

eutils = imp.load_source("utils", "interpret_utils/utils.py")
mnistutils = imp.load_source("utils_mnist", "interpret_utils/utils_mnist.py")

K.set_learning_phase(0)

gv.init()

args = gv.args

weights_np = np.load(gv.dir_name + 'global_weights_t%s.npy' % 8)

X_train, Y_train, X_test, Y_test, Y_test_uncat = data_setup()

mal_analyse = True

if mal_analyse:
    mal_data_X, mal_data_Y, true_labels = mal_data_setup(X_test,
                                                         Y_test,
                                                         Y_test_uncat,
                                                         gen_flag=False)

label_to_class_name = [str(i) for i in range(gv.NUM_CLASSES)]

if 'MNIST' in args.dataset:
    model = model_mnist(type=args.model_num)
elif args.dataset == 'CIFAR-10':
    model = cifar_10_model()