Exemple #1
0
k.tensorflow_backend.set_session(tf.Session(config=gpu_config))

if not os.path.exists(save_model_dir):
    os.makedirs(save_model_dir)

data = MNIST(data_dir,
             data_name,
             validation_size,
             model_meta=model_mnist_meta,
             input_data_format=CHANNELS_LAST,
             output_data_format=data_format,
             train_size=train_size,
             train_sel_rand=train_sel_rand)

if pre_idx_path is not None:
    pre_idx = utils.load_model_idx(pre_idx_path)
    data.apply_pre_idx(pre_idx)
if ex_data_dir is not None and ex_data_name is not None and ex_data_size > 0:
    data.append_train_data(ex_data_dir,
                           ex_data_name,
                           ex_data_size,
                           input_data_format=CHANNELS_LAST,
                           output_data_format=data_format,
                           sel_rand=ex_data_sel_rand)

# config data if using transfer training here
is_trans = args.is_trans == 'yes'
if is_trans:
    print("Get the soft label of the transfer model")
    trans_random_spike = None if args.trans_random_spike is None else parse_rand_spike(
        args.trans_random_spike)
    os.environ['TF_GPU_THREAD_COUNT'] = str(gpu_thread_count)
    os.environ['TF_USE_CUDNN_BATCHNORM_SPATIAL_PERSISTENT'] = '1'
    os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1'

    with tf.Session(config=gpu_config) as sess:
        models = []
        models_idx = []
        models_pred = []
        gpu_i = 0
        for _model_name in target_model_names:
            with tf.device('/gpu:' + str(gpu_i % gpu_count)):
                _path = os.path.join(target_model_dir, _model_name)
                models.append(MODEL(_path, sess,
                                    input_data_format=data_format, data_format=data_format,
                                    dropout=dropout, rand_params=para_random_spike, is_batch=True))
                models_idx.append(utils.load_model_idx(_path))
                gpu_i += 1

        for _eval_lab in eval_lab:
            models_pred.append(utils.load_obj(_eval_lab, directory=''))

        data = MNIST(_dir, name, model_meta=model_meta, validation_size=0,
                     input_data_format=CHANNELS_LAST, output_data_format=data_format, batch_size=batch_size,
                     boxmin=boxmin, boxmax=boxmax)

        num_labels = model_meta.labels
        if 0 <= num_labels <= 255:
            label_data_type = np.uint8
        else:
            label_data_type = np.uint16
                      cmax=boxmax)
    y_targets = tf.placeholder(tf.int32, [None])
    y_predict = tf.placeholder(tf.float32, [None, model_meta.labels])
    out_top_k = tf.nn.in_top_k(y_predict, y_targets, top_k)

    for cur_save_model_name in save_model_name_list:
        fp.write(cur_save_model_name.encode())
        print("=============================")
        print("valid transferability for model %s" % cur_save_model_name)
        print("=============================")
        # get current model
        cur_path = os.path.join(save_model_dir, cur_save_model_name)
        eva_model.load_weights(cur_path)
        det_model.load_weights(cur_path)
        cur_model = eva_model
        cur_model_idx = utils.load_model_idx(cur_path)
        if cur_model_idx is None:
            cur_model_idx = utils.save_model_idx(cur_path, data)

        # restore the key for current key
        # prepare encrypted data if we need to evaluate multi times
        # if encrypt and iteration > 1:
        #     enc_obj.restore_key(cur_path)
        #     data_attack_adv = enc_obj.enc_tf(sess, bak_data_attack_adv, normalize=True, batch_size=batch_size)
        #     data_attack_adv_img = enc_obj.enc_tf(sess, bak_data_attack_adv_img, normalize=True, batch_size=batch_size)

        cur_det_dict = detector_dict[cur_save_model_name] if cur_save_model_name in detector_dict else {}
        cur_det_set, cur_thrs_set, cur_det_gpu_idx = \
            worker.build_detector(detector_model_dir, cur_det_dict,
                                  cur_save_model_name, save_model_dir, cur_path,
                                  MODEL, det_model, data, data_format, is_det_joint, cur_model_idx)
Exemple #4
0
def build_detector(detector_model_dir,
                   detector_model_names,
                   save_model_name,
                   save_model_dir,
                   model_path,
                   MODEL,
                   det_model,
                   data,
                   data_format,
                   is_det_joint,
                   model_idx,
                   gpu_count=1):
    det_dict = {}
    det_set = {}
    det_idx_set = {}
    dropout_rate_set = {}
    det_gpu_idx = {}

    for val in detector_model_names:
        if val == '':
            continue

        cur_det_name, cur_p, cur_det_type, cur_dropout_rate, cur_model_id = val.split(
            '/')
        cur_model_id = int(cur_model_id)
        cur_det_path = os.path.join(detector_model_dir, cur_det_name)
        cur_detector = {
            "p": cur_p,
            "type": cur_det_type,
            "dropout_rate": cur_dropout_rate
        }
        det_dict[cur_det_name] = cur_detector

        if type(det_model) is list:
            cur_det_model = det_model[cur_model_id]
            cur_model_path = os.path.join(save_model_dir,
                                          save_model_name[cur_model_id])
            cur_det_idx = model_idx[cur_model_id]
        else:
            cur_det_model = det_model
            cur_model_path = model_path
            cur_det_idx = model_idx
        default_det_idx = cur_det_idx

        with tf.device('/gpu:' + str(cur_model_id % gpu_count)):
            # build detector
            print("# build detector: ", cur_det_name)
            print("type:", cur_det_type)
            print("p:", cur_p)
            print("drop_rate:", cur_dropout_rate)

            if cur_det_type == 'AED':
                cur_detector = AEDetector(cur_det_path, p=int(cur_p))
                cur_det_idx = load_model_idx(cur_det_path)
            elif cur_det_type == "DBD":
                id_reformer = IdReformer()
                print("# build reformer", cur_det_name)
                cur_reformer_t = SimpleReformer(cur_det_path)
                classifier = Classifier(cur_model_path,
                                        MODEL,
                                        data_format=data_format,
                                        model=cur_det_model)
                cur_detector = DBDetector(reconstructor=id_reformer,
                                          prober=cur_reformer_t,
                                          classifier=classifier,
                                          T=int(cur_p))
                cur_det_idx = load_model_idx(cur_det_path)

        if cur_det_idx is None:
            cur_det_idx = default_det_idx

        det_idx_set[cur_det_name] = cur_det_idx['validate']

        dropout_rate_set[cur_det_name] = float(cur_dropout_rate)
        det_set[cur_det_name] = cur_detector
        det_gpu_idx[cur_det_name] = cur_model_id % gpu_count

    # compute thrs
    thrs_set = {}
    det_info = {
        "model": save_model_name,
        "model_dir": save_model_dir,
        "det": det_dict,
        "det_dir": detector_model_dir,
        "joint_thrs": is_det_joint
    }

    cache_path = os.path.join(detector_model_dir, "cache")

    if is_det_joint:
        marks_set = []
        num = 0
        cache = load_cache(det_info, cache_path)
        if cache is None:
            cache_data = {}
            for cur_det_name, cur_det in det_set.items():
                validation_data = data.train_data_orig[
                    det_idx_set[cur_det_name]]
                num = int(
                    len(validation_data) * dropout_rate_set[cur_det_name])
                marks = cur_det.mark(validation_data, data_format=data_format)
                marks_set.append(marks)

                marks = np.sort(marks)
                cache_data[cur_det_name] = marks[-num]
                print("compute thrs for model #", cur_det_name, "#:",
                      marks[-num])

            marks_set = np.transpose(marks_set)
            marks_max = np.max(marks_set, axis=1)
            marks_max = np.sort(marks_max)
            max_thrs = marks_max[-num]

            cache_data['thrs'] = max_thrs
            if len(det_set) > 0:
                hash_id = save_cache(det_info, cache_data, cache_path)
                print("save cache:", hash_id)
        else:
            print("hit cache:", cache['hash_id'])
            cache_data = cache['data']
            for cur_det_name, cur_det in det_set.items():
                print("compute thrs for model #", cur_det_name, "#:",
                      cache_data[cur_det_name])
            max_thrs = cache_data['thrs']

        for cur_det_name, cur_det in det_set.items():
            thrs_set[cur_det_name] = max_thrs

        print("use joint thrs:", max_thrs)
    else:
        cache = load_cache(det_info, cache_path)
        if cache is None:
            cache_data = {}
            for cur_det_name, cur_det in det_set.items():
                validation_data = data.train_data_orig[
                    det_idx_set[cur_det_name]]
                num = int(
                    len(validation_data) * dropout_rate_set[cur_det_name])
                marks = cur_det.mark(validation_data, data_format=data_format)
                marks = np.sort(marks)

                thrs_set[cur_det_name] = marks[-num]
                cache_data[cur_det_name] = marks[-num]
                print("compute thrs for model #", cur_det_name, "#:",
                      marks[-num])

            if len(det_set) > 0:
                hash_id = save_cache(det_info, cache_data, cache_path)
                print("save cache:", hash_id)
        else:
            print("hit cache:", cache['hash_id'])
            cache_data = cache['data']
            for cur_det_name, cur_det in det_set.items():
                thrs_set[cur_det_name] = cache_data[cur_det_name]
                print("compute thrs for model #", cur_det_name, "#:",
                      cache_data[cur_det_name])

    return det_set, thrs_set, det_gpu_idx