예제 #1
0
    def train(self,
              data,
              archive_name,
              num_epochs=100,
              batch_size=256,
              if_save=True,
              fn=None):
        self.model.compile(loss='mean_squared_error',
                           metrics=['mean_squared_error'],
                           optimizer='adam')

        noise = self.v_noise * np.random.normal(size=np.shape(data.train_data))
        noisy_train_data = data.train_data + noise
        noisy_train_data = np.clip(noisy_train_data, 0.0, 1.0)
        if fn is None:
            train_data = data.train_data
            validation_data = data.validation_data
        else:
            train_data = fn(data.train_data)
            validation_data = fn(data.validation_data)

        self.model.fit(noisy_train_data,
                       train_data,
                       batch_size=batch_size,
                       validation_data=(data.validation_data, validation_data),
                       epochs=num_epochs,
                       shuffle=True)

        if if_save:
            model_name = os.path.join(self.model_dir, archive_name)
            self.model.save(model_name)
            utils.save_model_idx(model_name, data)
예제 #2
0
    def train(self, data, archive_name, alpha, num_epochs=10, batch_size=128):
        noise = self.v_noise * np.random.normal(size=np.shape(data.train_data))
        noisy_train_data = data.train_data + noise
        noisy_train_data = np.clip(noisy_train_data, 0.0, 1.0)

        train_zeros = [np.zeros_like(data.train_data)] * self.n_pack
        val_zeros = [np.zeros_like(data.validation_data)] * self.n_pack

        self.model.compile(loss="mean_squared_error",
                           optimizer="adam",
                           loss_weights=[1.0] * self.n_pack +
                           [-alpha] * self.n_pack)

        self.model.fit(
            noisy_train_data, [data.train_data] * self.n_pack + train_zeros,
            batch_size=batch_size,
            validation_data=(data.validation_data,
                             [data.validation_data] * self.n_pack + val_zeros),
            epochs=num_epochs,
            shuffle=True)

        for i in range(self.n_pack):
            model = Model(self.model.input, self.model.outputs[i])
            model_name = os.path.join(self.model_dir,
                                      archive_name + "_" + str(i))
            model.save(model_name)
            utils.save_model_idx(model_name, data)
예제 #3
0
def train(data,
          file_name,
          params,
          rand_params,
          num_epochs=50,
          batch_size=128,
          is_batch=True,
          dropout=0.0,
          data_format=None,
          init_model=None,
          train_temp=1,
          data_gen=None):
    """
    Standard neural network training procedure.
    """
    _input = Input(shape=data.train_data.shape[1:])
    x = _input

    x = Conv2D(params[0], (3, 3), padding="same", data_format=data_format)(x)
    x = Activation('relu')(x)
    x = Lambda(function=random_spike,
               arguments={
                   "sample_rate": rand_params[0],
                   "scaling": rand_params[1],
                   "is_batch": is_batch
               })(x)
    x = Conv2D(params[1], (3, 3), padding="same", data_format=data_format)(x)
    x = Activation('relu')(x)
    x = Lambda(function=random_spike,
               arguments={
                   "sample_rate": rand_params[2],
                   "scaling": rand_params[3],
                   "is_batch": is_batch
               })(x)
    x = MaxPooling2D(pool_size=(2, 2), data_format=data_format)(x)
    x = Lambda(function=random_spike,
               arguments={
                   "sample_rate": rand_params[4],
                   "scaling": rand_params[5],
                   "is_batch": is_batch
               })(x)

    x = Conv2D(params[2], (3, 3), padding="same", data_format=data_format)(x)
    x = Activation('relu')(x)
    x = Lambda(function=random_spike,
               arguments={
                   "sample_rate": rand_params[6],
                   "scaling": rand_params[7],
                   "is_batch": is_batch
               })(x)
    x = Conv2D(params[3], (3, 3), padding="same", data_format=data_format)(x)
    x = Activation('relu')(x)
    x = Lambda(function=random_spike,
               arguments={
                   "sample_rate": rand_params[8],
                   "scaling": rand_params[9],
                   "is_batch": is_batch
               })(x)
    x = MaxPooling2D(pool_size=(2, 2), data_format=data_format)(x)
    x = Lambda(function=random_spike,
               arguments={
                   "sample_rate": rand_params[10],
                   "scaling": rand_params[11],
                   "is_batch": is_batch
               })(x)

    x = Flatten()(x)
    x = Dense(params[4])(x)
    x = Activation('relu')(x)
    x = Lambda(function=random_spike,
               arguments={
                   "sample_rate": rand_params[12],
                   "scaling": rand_params[13],
                   "is_batch": is_batch
               })(x)
    if dropout > 0:
        x = Dropout(dropout)(x, training=True)
    x = Dense(params[5])(x)
    x = Activation('relu')(x)
    x = Lambda(function=random_spike,
               arguments={
                   "sample_rate": rand_params[14],
                   "scaling": rand_params[15],
                   "is_batch": is_batch
               })(x)
    x = Dense(10)(x)
    model = Model(_input, x)
    model.summary()

    def fn(correct, predicted):
        return tf.nn.softmax_cross_entropy_with_logits(labels=correct,
                                                       logits=predicted /
                                                       train_temp)

    if init_model is not None:
        model.load_weights(init_model)
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

    model.compile(loss=fn, optimizer=sgd, metrics=['accuracy'])

    if data_gen is None:
        model.fit(data.train_data,
                  data.train_labels,
                  batch_size=batch_size,
                  validation_data=(data.test_data, data.test_labels),
                  nb_epoch=num_epochs,
                  shuffle=True)
    else:
        data_flow = data_gen.flow(data.train_data,
                                  data.train_labels,
                                  batch_size=128,
                                  shuffle=True)
        model.fit_generator(data_flow,
                            steps_per_epoch=len(data_flow),
                            validation_data=(data.validation_data,
                                             data.validation_labels),
                            nb_epoch=num_epochs,
                            shuffle=True)

    if file_name is not None:
        model.save(file_name)

    # save idx
    utils.save_model_idx(file_name, data)
    return model
    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)

        # concat reformer in front of models
예제 #5
0
def train_wrn(data_generator, file_name, num_epochs=200, debug=True, gpus=None,
              initial_epoch=0, initial_model=None, data_format=None, rand_spike=0, dropout=0.0, train_temp=1):
    # For WRN-16-8 put N = 2, k = 8
    # For WRN-28-10 put N = 4, k = 10
    # For WRN-40-4 put N = 6, k = 4

    def _create_model():
        return wrn.create_wide_residual_network(data.train_data.shape[1:],
                                                nb_classes=10, N=4, k=10, dropout=dropout, weight_decay=0.0005,
                                                output_logits=True, input_data_format=data_format,
                                                data_format=data_format, rand_spike=rand_spike)

    devices = get_available_gpus()
    devices_len = len(devices)

    if gpus is None:
        gpus = []
    elif isinstance(gpus, list):
        pass
    elif isinstance(gpus, int):
        gpus = list(range(gpus))
    else:
        raise ValueError('number of gpus is either list or int')

    if devices_len >= 2 and len(gpus) >= 2:
        with tf.device('/cpu:0'):
            _model = _create_model()
        _model_multi = multi_gpu_model(_model, gpus)
    else:

        _model = _create_model()
        _model_multi = _model

    # learning rate schedule
    lr_schedule = [60, 120, 160, 180]  # epoch_step

    def schedule(epoch_idx):
        if (epoch_idx + 1) < lr_schedule[0]:
            return 0.1
        elif (epoch_idx + 1) < lr_schedule[1]:
            return 0.02  # lr_decay_ratio = 0.2
        elif (epoch_idx + 1) < lr_schedule[2]:
            return 0.004
        elif (epoch_idx + 1) < lr_schedule[3]:
            return 0.0008
        return 0.0008

    def fn(correct, predicted):
        return tf.nn.softmax_cross_entropy_with_logits(labels=correct,
                                                       logits=predicted/train_temp)

    sgd = SGD(lr=0.1, decay=0, momentum=0.9, nesterov=True)
    if initial_epoch > 0:
        _model_multi.load_weights(initial_model)
    _model_multi.compile(loss=fn,
                         optimizer=sgd,
                         metrics=['accuracy'])
    _model.summary()

    log_path = file_name + "_log"
    checkpoint_path = os.path.join(log_path, "checkpoint")
    if not os.path.exists(checkpoint_path):
        os.makedirs(checkpoint_path)
    _model_multi.fit_generator(data_generator,
                               steps_per_epoch=len(data_generator),
                               validation_data=(data.validation_data, data.validation_labels),
                               epochs=num_epochs,
                               initial_epoch=initial_epoch,
                               callbacks=[
                                   LearningRateScheduler(schedule=schedule),
                                   SGDLearningRateTracker(),
                                   ModelCheckpoint(os.path.join(
                                       checkpoint_path, 'weights.{epoch:02d}-{val_acc:.2f}-{val_loss:.2f}.hdf5'),
                                                   monitor='val_acc',
                                                   verbose=1,
                                                   save_best_only=True,
                                                   save_weights_only=True,
                                                   mode='auto')
                               ],
                               shuffle=True)

    if debug:
        plot_model(_model, os.path.join(log_path, "WRN-{0}-{1}.png".format(28, 10)), show_shapes=True,
                   show_layer_names=True)
        with open(os.path.join(log_path, 'WRN-{0}-{1}.json'.format(28, 10)), 'w') as f:
            f.write(_model.to_json())
            f.close()

    if file_name is not None:
        _model.save(file_name)

    # save idx
    utils.save_model_idx(file_name, data)

    return _model