Exemplo n.º 1
0
def main():
    train, valid, test = inputs.get_data()
    train_images, train_labels = inputs.make_batch(train)
    valid_images, valid_labels = inputs.make_batch(valid)
    test_images, test_labels = inputs.make_batch(test)

    num_classes = 47
    train_y = keras.utils.to_categorical(train_labels, num_classes)
    valid_y = keras.utils.to_categorical(valid_labels, num_classes)
    test_y = keras.utils.to_categorical(test_labels, num_classes)

    model = network(num_classes)
    model.summary()
    model.compile(loss='categorical_crossentropy',
                  optimizer='Adam',
                  metrics=['accuracy'])

    # verbose: display mode: 0:no display, 1: progress bar
    batch_size = 32
    history = model.fit(train_images,train_y,batch_size=32,\
    epochs=10,verbose=1,validation_data = (valid_images, valid_y))
    score = model.evaluate(test_images, test_y, verbose=0)

    # score[0]: loss, score[1]: accuracy
    print('Loss:', score[0])
    print('Accuracy:', score[1])

    backend.clear_session()
Exemplo n.º 2
0
    def load(self, num_cpus, num_gpus):
        """
        Load current model
        """
        if not self.is_trained:
            raise errors.ModelNotTrained()
        if self._keras_model:
            # Already loaded
            return

        K.clear_session()
        self._set_xpu_config(num_cpus, num_gpus)

        if self._state.get('h5py', None) is not None:
            self._keras_model = _load_keras_model(self._state.get('h5py'))
            # instantiate encoder model
            self._encoder_model = _get_encoder(self._keras_model)
            # instantiate decoder model
            self._decoder_model = _get_decoder(self._keras_model)
        else:
            raise errors.ModelNotTrained()

        if 'means' in self._state:
            self.means = np.array(self._state['means'])
        if 'stds' in self._state:
            self.stds = np.array(self._state['stds'])
        if 'scores' in self._state:
            self.scores = np.array(self._state['scores'])
        if self.min_threshold == 0 and self.max_threshold == 0:
            self.set_auto_threshold()
            logging.info(
                "setting threshold range min=%f max=%f",
                self.min_threshold,
                self.max_threshold,
            )
Exemplo n.º 3
0
 def unload(self):
     """
     Unload current model
     """
     self._keras_model = None
     self._encoder_model = None
     self._decoder_model = None
     K.clear_session()
Exemplo n.º 4
0
def main(argv=None):

    if os.path.isfile(IRIS_TRAINING) == 0:
        download_file()

    training_data = np.loadtxt('./iris_training.csv',
                               delimiter=',',
                               skiprows=1)
    test_data = np.loadtxt('./iris_test.csv', delimiter=',', skiprows=1)

    # split training and validation
    validation_data, training_data = np.split(
        training_data, [int(training_data.shape[0] / 3)])

    # A[:-1] is to slice excepe last element
    # A[-1] is the last element
    train_X = training_data[:, :-1]
    train_y = training_data[:, -1]

    valid_X = validation_data[:, :-1]
    valid_y = validation_data[:, -1]

    test_X = test_data[:, :-1]
    test_y = test_data[:, -1]

    num_classes = 3
    train_y = keras.utils.to_categorical(train_y, num_classes)
    valid_y = keras.utils.to_categorical(valid_y, num_classes)
    test_y = keras.utils.to_categorical(test_y, num_classes)

    model = network(num_classes)

    model.summary()

    model.compile(loss='categorical_crossentropy',
                  optimizer='Adam',
                  metrics=['accuracy'])

    # Callbacks definition
    tensorboard = callbacks.TensorBoard(log_dir='./logs/', histogram_freq=1)
    callback_list = [tensorboard]

    # verbose: display mode: 0:no display, 1: progress bar
    history = model.fit(train_X,
                        train_y,
                        batch_size=20,
                        epochs=2000,
                        verbose=0,
                        validation_data=(valid_X, valid_y),
                        callbacks=callback_list)

    score = model.evaluate(test_X, test_y, verbose=0)

    # score[0]: loss, score[1]: accuracy
    print('Loss:', score[0])
    print('Accuracy:', score[1])

    backend.clear_session()
Exemplo n.º 5
0
        def cross_val_model(params):
            keras_model = None
            # Destroys the current TF graph and creates a new one.
            # Useful to avoid clutter from old models / layers.
            K.clear_session()
            self._set_xpu_config(num_cpus, num_gpus)

            self.span = W = params.span
            (X_miss, X_train), (X_miss_val, X_test) = self.train_test_split(
                dataset,
                train_size=train_size,
                abnormal=abnormal,
            )
            if len(X_train) == 0:
                raise errors.NoData("insufficient training data")
            if len(X_test) == 0:
                raise errors.NoData("insufficient validation data")

            # expected input data shape: (batch_size, timesteps,)
            # network parameters
            input_shape = (W, )
            intermediate_dim = params.intermediate_dim
            latent_dim = params.latent_dim

            # VAE model = encoder + decoder
            # build encoder model
            main_input = Input(shape=input_shape)
            # bool vector to flag missing data points
            aux_input = Input(shape=input_shape)
            aux_output = Lambda(lambda x: x)(aux_input)
            x = Dense(intermediate_dim,
                      kernel_regularizer=regularizers.l2(0.01),
                      activation='relu')(main_input)
            z_mean = Dense(latent_dim, name='z_mean')(x)
            z_log_var = Dense(latent_dim, name='z_log_var')(x)

            # use reparameterization trick to push the sampling out as input
            # note that "output_shape" isn't necessary with the TensorFlow backend
            z = Lambda(sampling, output_shape=(latent_dim, ),
                       name='z')([z_mean, z_log_var])

            # build decoder model
            x = Dense(intermediate_dim,
                      kernel_regularizer=regularizers.l2(0.01),
                      activation='relu',
                      name='dense_1')(z)
            main_output = Dense(W, activation='linear', name='dense_2')(x)

            # instantiate Donut model
            keras_model = _Model([main_input, aux_input],
                                 [main_output, aux_output],
                                 name='donut')
            add_loss(keras_model, W)
            optimizer_cls = None
            if params.optimizer == 'adam':
                optimizer_cls = tf.keras.optimizers.Adam()

            keras_model.compile(optimizer=optimizer_cls, )

            _stop = EarlyStopping(
                monitor='val_loss',
                patience=5,
                verbose=_verbose,
                mode='auto',
            )
            keras_model.fit_generator(
                generator(X_train, X_miss, batch_size, keras_model),
                epochs=num_epochs,
                steps_per_epoch=len(X_train) / batch_size,
                verbose=_verbose,
                validation_data=([X_test, X_miss_val], None),
                callbacks=[_stop],
                workers=0,  # https://github.com/keras-team/keras/issues/5511
            )

            # How well did it do?
            score = keras_model.evaluate(
                [X_test, X_miss_val],
                batch_size=batch_size,
                verbose=_verbose,
            )

            self.current_eval += 1
            if progress_cb is not None:
                progress_cb(self.current_eval, max_evals)

            return score, keras_model
def run(file_name, n_samples, p_n, q_n, activation = 'relu', cifar=False, tinyimagenet=False):
    np.random.seed(1215)
    tf.set_random_seed(1215)
    random.seed(1215)
    keras_model = load_model(file_name, custom_objects={'fn':fn, 'tf':tf})
    if tinyimagenet:
        model = CNNModel(keras_model, inp_shape = (64,64,3))
    elif cifar:
        model = CNNModel(keras_model, inp_shape = (32,32,3))
    else:
        model = CNNModel(keras_model)

    #Set correct linear_bounds function
    global linear_bounds
    if activation == 'relu':
        linear_bounds = relu_linear_bounds
    elif activation == 'ada':
        linear_bounds = ada_linear_bounds
    elif activation == 'sigmoid':
        linear_bounds = sigmoid_linear_bounds
    elif activation == 'tanh':
        linear_bounds = tanh_linear_bounds
    elif activation == 'arctan':
        linear_bounds = atan_linear_bounds
    upper_bound_conv.recompile()
    lower_bound_conv.recompile()
    compute_bounds.recompile()

    if cifar:
        inputs, targets, true_labels, true_ids, img_info = generate_data(CIFAR(), samples=n_samples, targeted=True, random_and_least_likely = True, target_type = 0b0010, predictor=model.model.predict, start=0)
    elif tinyimagenet:
        inputs, targets, true_labels, true_ids, img_info = generate_data(tinyImagenet(), samples=n_samples, targeted=True, random_and_least_likely = True, target_type = 0b0010, predictor=model.model.predict, start=0)
    else:
        inputs, targets, true_labels, true_ids, img_info = generate_data(MNIST(), samples=n_samples, targeted=True, random_and_least_likely = True, target_type = 0b0010, predictor=model.model.predict, start=0)
    #0b01111 <- all
    #0b0010 <- random
    #0b0001 <- top2
    #0b0100 <- least

    steps = 15
    eps_0 = 0.05
    summation = 0
    warmup(model, inputs[0].astype(np.float32), eps_0, p_n, find_output_bounds)
        
    start_time = time.time()
    for i in range(len(inputs)):
        print('--- CNN-Cert: Computing eps for input image ' + str(i)+ '---')
        predict_label = np.argmax(true_labels[i])
        target_label = np.argmax(targets[i])
        weights = model.weights[:-1]
        biases = model.biases[:-1]
        shapes = model.shapes[:-1]
        W, b, s = model.weights[-1], model.biases[-1], model.shapes[-1]
        last_weight = (W[predict_label,:,:,:]-W[target_label,:,:,:]).reshape([1]+list(W.shape[1:]))
        weights.append(last_weight)
        biases.append(np.asarray([b[predict_label]-b[target_label]]))
        shapes.append((1,1,1))

        #Perform binary search
        log_eps = np.log(eps_0)
        log_eps_min = -np.inf
        log_eps_max = np.inf
        for j in range(steps):
            LB, UB = find_output_bounds(weights, biases, shapes, model.pads, model.strides, inputs[i].astype(np.float32), np.exp(log_eps), p_n)
            print("Step {}, eps = {:.5f}, {:.6s} <= f_c - f_t <= {:.6s}".format(j,np.exp(log_eps),str(np.squeeze(LB)),str(np.squeeze(UB))))
            if LB > 0: #Increase eps
                log_eps_min = log_eps
                log_eps = np.minimum(log_eps+1, (log_eps_max+log_eps_min)/2)
            else: #Decrease eps
                log_eps_max = log_eps
                log_eps = np.maximum(log_eps-1, (log_eps_max+log_eps_min)/2)
        
        if p_n == 105:
            str_p_n = 'i'
        else:
            str_p_n = str(p_n)
        
        print("[L1] method = CNN-Cert-{}, model = {}, image no = {}, true_id = {}, target_label = {}, true_label = {}, norm = {}, robustness = {:.5f}".format(activation,file_name, i, true_ids[i],target_label,predict_label,str_p_n,np.exp(log_eps_min)))
        summation += np.exp(log_eps_min)
    K.clear_session()
    
    eps_avg = summation/len(inputs)
    total_time = (time.time()-start_time)/len(inputs)
    print("[L0] method = CNN-Cert-{}, model = {}, total images = {}, norm = {}, avg robustness = {:.5f}, avg runtime = {:.2f}".format(activation,file_name,len(inputs),str_p_n,eps_avg,total_time))
    return eps_avg, total_time
def main():

    batch_size = _BATCH_SIZE
    noise_dim = _NOISE_DIM
    lamb = 10.0

    train = get_data()
    train_images, train_labels = make_batch(train)

    gen = generator()
    dis = discriminator()
    gen.summary()
    dis.summary()

    dis_opt = Adam(lr=1.0e-4, beta_1=0.0, beta_2=0.9)
    gen_opt = Adam(lr=1.0e-4, beta_1=0.0, beta_2=0.9)

    gen.trainable = True
    dis.trainable = False
    gen_inputs = Input(shape=(noise_dim, ))
    gen_outputs = gen(gen_inputs)
    dis_outputs = dis(gen_outputs)
    gen_model = Model(inputs=[gen_inputs], outputs=[dis_outputs])
    gen_model.compile(loss=wasserstein_loss, optimizer=gen_opt)
    gen_model.summary()

    gen.trainable = False
    dis.trainable = True
    real_inputs = Input(shape=train_images.shape[1:])
    dis_real_outputs = dis(real_inputs)

    fake_inputs = Input(shape=(noise_dim, ))
    gen_fake_outputs = gen(fake_inputs)
    dis_fake_outputs = dis(gen_fake_outputs)

    interpolate = RandomWeightedAverage()([real_inputs, gen_fake_outputs])
    dis_interpolate_outputs = dis(interpolate)

    gp_reg = partial(gradient_penalty, interpolate=interpolate, lamb=lamb)
    #gp_reg.__name__ = 'gradient_penalty'

    dis_model = Model(inputs=[real_inputs, fake_inputs],\
    outputs=[dis_real_outputs, dis_fake_outputs,dis_interpolate_outputs])

    dis_model.compile(loss=[wasserstein_loss, wasserstein_loss, gp_reg],
                      optimizer=dis_opt)
    dis_model.summary()

    max_epoch = 10001
    max_train_only_dis = 5
    minibatch_size = batch_size * max_train_only_dis
    max_loop = int(train_images.shape[0] / minibatch_size)

    real = np.zeros((batch_size, train_images.shape[1], train_images.shape[2],
                     train_images.shape[3]),
                    dtype=np.float32)
    minibatch_train_images = np.zeros(
        (minibatch_size, train_images.shape[1], train_images.shape[2],
         train_images.shape[3]),
        dtype=np.float32)

    progbar = Progbar(target=max_epoch)
    real_label = [-1] * batch_size
    fake_label = [1] * batch_size
    dummy_label = [0] * batch_size
    for epoch in range(max_epoch):

        np.random.shuffle(train_images)
        for loop in range(max_loop):

            minibatch_train_images = train_images[loop *
                                                  minibatch_size:(loop + 1) *
                                                  minibatch_size]
            for train_only_dis in range(max_train_only_dis):

                real = minibatch_train_images[train_only_dis *
                                              batch_size:(train_only_dis + 1) *
                                              batch_size]
                noise = np.random.uniform(
                    -1, 1, (batch_size, noise_dim)).astype(np.float32)
                dis_loss = dis_model.train_on_batch(
                    [real, noise], [real_label, fake_label, dummy_label])

            noise = np.random.uniform(-1, 1, (batch_size, noise_dim)).astype(
                np.float32)
            gen_loss = gen_model.train_on_batch(noise, real_label)

        progbar.add(1,
                    values=[("dis_loss", dis_loss[0]), ("gen_loss", gen_loss)])
        if epoch % 100 == 0:
            noise = np.random.uniform(-1, 1,
                                      (batch_size, 10)).astype(np.float32)
            fake = gen.predict(noise)
            tmp = [r.reshape(-1, 32) for r in fake]
            tmp = np.concatenate(tmp, axis=1)
            img = ((tmp / 2.0 + 0.5) * 255.0).astype(np.uint8)
            Image.fromarray(img).save("generate/%d.png" % (epoch))

    backend.clear_session()
Exemplo n.º 8
0
def main():

    if os.path.isfile(macro._LOCAL_SAVE_DATA) == 0:

        # download data and compute featuers (see "download_data.py")
        # atomic_numbers use to compute composition vector
        # labels is target properties (formation energy)
        train_labels, compositions, features, atomic_numbers = dl.get_data()

        # compute bag-of-atom vector that trains GAN (see "preprocess.py")
        boa_vectors = pre.compute_bag_of_atom_vector(compositions,
                                                     atomic_numbers)
        train_data = np.concatenate([boa_vectors, features], axis=1)

        save_data = pd.DataFrame(
            np.concatenate([train_labels, train_data], axis=1))
        save_data.to_csv(macro._LOCAL_SAVE_DATA, index=False, header=False)

    else:
        data = pd.read_csv(macro._LOCAL_SAVE_DATA,
                           delimiter=',',
                           engine="python",
                           header=None)
        data = np.array(data)
        train_labels, train_data = np.split(data, [1], axis=1)

    # normalization of training data such that min is 0 and max is 1 (see "preprocess.py")
    normalized_train_data, data_max, data_min = pre.normalize_for_train(
        train_data)
    normalized_train_labels, max_train_prop, min_train_prop = pre.normalize_for_train(
        train_labels)

    # Save normalization parameter to .csv to use generation
    save_data = pd.DataFrame(
        np.concatenate([max_train_prop, min_train_prop, data_max, data_min],
                       axis=0))
    save_data.to_csv(macro._SAVE_NORMALIZATION_PARAM,
                     index=False,
                     header=False)

    ### start initialization of training GAN ###

    # set hyperparameters
    batch_size = macro._BATCH_SIZE  # batch size
    noise_dim = macro._NOISE_DIM  # dimension of noise to input generator
    property_dim = macro._PROP_DIM  # the number of properties
    lamb = macro._LAMB  # hyperparameter for W-GAN-GP
    max_epoch = macro._MAX_EPOCH  # maximum iteration of outer loop
    max_train_only_dis = macro._MAX_EPOCH_TRAIN_DISCRIMINATOR  # maximum iteration of inner loop defined by W-GAN-GP paper (https://arxiv.org/pdf/1704.00028.pdf)
    max_loop = int(train_data.shape[0] / batch_size)

    # set model (see "model.py")
    # in this code, we apply AC-GAN based network architecture (https://arxiv.org/abs/1610.09585)
    # difference between AC-GAN is that our model is the regression, not classification
    gen = model.generator(normalized_train_data.shape[1])
    dis = model.discriminator(normalized_train_data.shape[1])

    # rf is the output layer of discriminator that discriminates real or fake
    rf = model.real_fake()

    # pred is the output layer of discriminator that predicts target property
    pred = model.prediction()

    # set optimization method
    dis_opt = Adam(lr=1.0e-4, beta_1=0.0, beta_2=0.9)
    gen_opt = Adam(lr=1.0e-4, beta_1=0.0, beta_2=0.9)

    # first set discriminator's parameters for training
    gen.trainable = False  # generator's parameter does not update
    dis.trainable = True
    rf.trainable = True
    pred.trainable = True

    # set variables when inputting real data
    real_inputs = Input(shape=normalized_train_data.shape[1:])
    dis_real_outputs = dis(real_inputs)
    real_fake_from_real = rf(dis_real_outputs)
    predictions_from_real = pred(dis_real_outputs)

    # set variables when inputting fake data
    fake_inputs = Input(shape=(noise_dim + property_dim, ))
    gen_fake_outputs = gen(fake_inputs)
    dis_fake_outputs = dis(gen_fake_outputs)
    real_fake_from_fake = rf(dis_fake_outputs)

    # set loss function for discriminator
    # in this case, we apply W-GAN-GP based loss function because of improving stability
    # W-GAN-GP (https://arxiv.org/pdf/1704.00028.pdf)
    # W-GAN-GP is unsupervised training, on the other hand, our model is supervised (conditional).
    # So, we apply wasserstein_loss to real_fake part and apply mean_squared_error to prediction part
    interpolate = model.RandomWeightedAverage()(
        [real_inputs, gen_fake_outputs])
    dis_interpolate_outputs = dis(interpolate)
    real_fake_interpolate = rf(dis_interpolate_outputs)

    # gradient penalty of W-GAN-GP
    gp_reg = partial(model.gradient_penalty,
                     interpolate=interpolate,
                     lamb=lamb)
    gp_reg.__name__ = 'gradient_penalty'

    # connect inputs and outputs of the discriminator
    # prediction part is trained by only using training dataset (i.e., predict part is not trained by generated samples)
    dis_model = Model(inputs=[real_inputs, fake_inputs],\
    outputs=[real_fake_from_real, real_fake_from_fake, real_fake_interpolate, predictions_from_real])

    # compile
    dis_model.compile(loss=[model.wasserstein_loss,model.wasserstein_loss,\
    gp_reg,'mean_squared_error'],optimizer=dis_opt)

    # second set generator's parameters for training
    gen.trainable = True  # generator's parameters only update
    dis.trainable = False
    rf.trainable = False
    pred.trainable = False

    # set variables when inputting noise and target property
    gen_inputs = Input(shape=(noise_dim + property_dim, ))
    gen_outputs = gen(gen_inputs)

    # set variables for discriminator when inputting fake data
    dis_outputs = dis(gen_outputs)
    real_fake = rf(dis_outputs)
    predictions = pred(dis_outputs)

    # connect inputs and outputs of the discriminator
    gen_model = Model(inputs=[gen_inputs], outputs=[real_fake, predictions])

    # compile
    # generator is trained by real_fake classification and prediction of target property
    gen_model.compile(loss=[model.wasserstein_loss, 'mean_squared_error'],
                      optimizer=gen_opt)

    # if you need progress bar
    progbar = Progbar(target=max_epoch)

    # set the answer to train each model
    real_label = [-1] * batch_size
    fake_label = [1] * batch_size
    dummy_label = [0] * batch_size

    #real = np.zeros((batch_size,train_data.shape[1]), dtype=np.float32)
    inputs = np.zeros((batch_size, noise_dim + property_dim), dtype=np.float32)

    # epoch
    for epoch in range(max_epoch):

        # iteration
        for loop in range(max_loop):

            # shuffle to change the trainng order and select data
            sdata, slabels, bak = pre.paired_shuffle(normalized_train_data,
                                                     normalized_train_labels)
            real = sdata[loop * batch_size:(loop + 1) * batch_size]
            properties = slabels[loop * batch_size:(loop + 1) * batch_size]

            # generator's parameters does not update
            gen.trainable = False
            dis.trainable = True
            rf.trainable = True
            pred.trainable = True

            # train discriminator
            for train_only_dis in range(max_train_only_dis):
                noise = np.random.uniform(
                    -1, 1, (batch_size, noise_dim)).astype(np.float32)
                for i in range(len(noise)):
                    inputs[i] = np.hstack((noise[i], properties[i]))
                dis_loss = dis_model.train_on_batch(
                    [real, inputs],
                    [real_label, fake_label, dummy_label, properties])

            # second train only generator
            gen.trainable = True
            dis.trainable = False
            rf.trainable = False
            pred.trainable = False
            noise = np.random.uniform(-1, 1, (batch_size, noise_dim)).astype(
                np.float32)
            for i in range(len(noise)):
                inputs[i] = np.hstack((noise[i], properties[i]))
            gen_loss = gen_model.train_on_batch([inputs],
                                                [real_label, properties])

        # if you need progress bar
        progbar.add(1,
                    values=[("dis_loss", dis_loss[0]),
                            ("gen_loss", gen_loss[0])])

    # save generated samples and models
    eval.save(normalized_train_data, gen, dis, pred, rf)

    backend.clear_session()
Exemplo n.º 9
0
def main():
    train = get_data()
    train_images, train_labels = make_batch(train)

    dis = discriminator()
    dis.summary()

    dis_opt = Adam(lr=1.0e-4, beta_1=0.0, beta_2=0.9)
    dis.compile(loss='binary_crossentropy', optimizer=dis_opt)

    gen = generator()
    gen.summary()

    gen.trainable = True
    dis.trainable = False
    comb = combine(gen, dis)
    comb.summary()

    gen_opt = Adam(lr=1.0e-4, beta_1=0.0, beta_2=0.9)
    comb.compile(loss='binary_crossentropy', optimizer=gen_opt)

    batch_size = _BATCH_SIZE
    noise_dim = _NOISE_DIM
    max_epoch = 10001
    max_train_only_dis = 5
    minibatch_size = batch_size * max_train_only_dis
    max_loop = int(train_images.shape[0] / minibatch_size)

    real = np.zeros((batch_size, train_images.shape[1], train_images.shape[2],
                     train_images.shape[3]),
                    dtype=np.float32)
    minibatch_train_images = np.zeros(
        (minibatch_size, train_images.shape[1], train_images.shape[2],
         train_images.shape[3]),
        dtype=np.float32)

    progbar = Progbar(target=max_epoch)
    real_label = [-1] * batch_size
    fake_label = [1] * batch_size
    for epoch in range(max_epoch):

        np.random.shuffle(train_images)
        for loop in range(max_loop):

            minibatch_train_images = train_images[loop *
                                                  minibatch_size:(loop + 1) *
                                                  minibatch_size]
            for train_only_dis in range(max_train_only_dis):

                real = minibatch_train_images[train_only_dis *
                                              batch_size:(train_only_dis + 1) *
                                              batch_size]
                noise = np.random.uniform(
                    -1, 1, (batch_size, noise_dim)).astype(np.float32)

                dis.trainable = False
                y = [1] * batch_size
                gen_loss = comb.train_on_batch(noise, y)

                dis.trainable = True
                y = [1] * batch_size + [0] * batch_size
                fake = gen.predict(noise)
                dis_loss = dis.train_on_batch(np.concatenate((real, fake)), y)

        progbar.add(1, values=[("dis_loss", dis_loss), ("gen_loss", gen_loss)])
        if epoch % 100 == 0:
            tmp = [r.reshape(-1, 32) for r in fake]
            tmp = np.concatenate(tmp, axis=1)
            img = ((tmp / 2.0 + 0.5) * 255.0).astype(np.uint8)
            Image.fromarray(img).save("generate/%d.png" % (epoch))

    backend.clear_session()
Exemplo n.º 10
0
def transfer_model(source_df, target_df, test_df, method_flag, fold_num):

	source_labels, source_data = np.split(np.array(source_df),[1],axis=1)
	target_labels, target_data = np.split(np.array(target_df),[1],axis=1)
	test_labels, test_data = np.split(np.array(test_df),[1],axis=1)

	# normalization
	#normalized_source_data = pre.normalize(source_data)
	#normalized_target_data = pre.normalize(target_data)
	#normalized_test_data = pre.normalize(test_data)
	normalized_source_data = source_data
	normalized_target_data = target_data
	normalized_test_data = test_data


	### constuct model for source domain task  ###

	# optimization
	opt = Adam()

	# network setting
	latent = models.latent(normalized_source_data.shape[1])
	sll = models.source_last_layer()
	tll = models.target_last_layer()

	source_inputs = Input(shape=normalized_source_data.shape[1:])
	latent_features = latent(source_inputs)
	source_predictors = sll(latent_features)

	latent.trainable = mc._SORUCE_LATENT_TRAIN
	source_predictors.trainable = True

	source_nn = Model(inputs=[source_inputs], outputs=[source_predictors])
	source_nn.compile(loss=['mean_squared_error'],optimizer=opt)
	#source_nn.summary()

	# training using source domain data
	if method_flag != mc._SCRATCH:
		source_max_loop = int(normalized_source_data.shape[0]/mc._BATCH_SIZE)
		source_progbar = Progbar(target=mc._SOURCE_EPOCH_NUM)
		for epoch in range(mc._SOURCE_EPOCH_NUM):
			shuffle_data, shuffle_labels, _ = pre.paired_shuffle(normalized_source_data,source_labels,1)

			for loop in range(source_max_loop):
				batch_train_data = shuffle_data[loop*mc._BATCH_SIZE:(loop+1)*mc._BATCH_SIZE]
				batch_train_labels = shuffle_labels[loop*mc._BATCH_SIZE:(loop+1)*mc._BATCH_SIZE]
				batch_train_labels = np.reshape(batch_train_labels, [len(batch_train_labels)])
				one_hots = np.identity(mc._SOURCE_DIM_NUM)[np.array(batch_train_labels, dtype=np.int32)]
				loss = source_nn.train_on_batch([batch_train_data],[one_hots])

			#source_progbar.add(1, values=[("source loss",loss)])

		# save
		#latent.save('../results/source_latent.h5')
		#sll.save('../results/source_last_layer.h5')

	# compute relation vectors
	if method_flag == mc._SCRATCH or method_flag == mc._CONV_TRANSFER:
		target_vectors = np.identity(mc._TARGET_DIM_NUM)[np.array(target_labels, dtype=np.int32)]
		target_vectors = np.reshape(target_vectors, [target_vectors.shape[0], target_vectors.shape[2]])
	elif method_flag == mc._COUNT_ATDL:
		target_labels, relations = rv.compute_relation_labels(source_nn, normalized_target_data, target_labels, fold_num)
		target_vectors = np.identity(mc._SOURCE_DIM_NUM)[np.array(target_labels, dtype=np.int32)]
		target_vectors = np.reshape(target_vectors, [target_vectors.shape[0], target_vectors.shape[2]])
	else:
		relation_vectors = rv.compute_relation_vectors(source_nn, normalized_target_data, target_labels, fold_num, method_flag)
		target_vectors = np.zeros((len(target_labels),mc._SOURCE_DIM_NUM), dtype=np.float32)
		for i in range(len(target_labels)):
			target_vectors[i] = relation_vectors[int(target_labels[i])]

	### tuning model for target domain task	 ###

	latent.trainable = mc._TARGET_LATENT_TRAIN
	target_inputs = Input(shape=normalized_target_data.shape[1:])
	latent_features = latent(target_inputs)
	if method_flag == mc._SCRATCH or method_flag == mc._CONV_TRANSFER:
		predictors = tll(latent_features)
		label_num = mc._TARGET_DIM_NUM
	else:
		predictors= sll(latent_features)
		label_num = mc._SOURCE_DIM_NUM

	target_nn = Model(inputs=[target_inputs], outputs=[predictors])
	target_nn.compile(loss=['mean_squared_error'],optimizer=opt)
	#target_nn.summary()

	# training using target domain data
	target_max_loop = int(normalized_target_data.shape[0]/mc._BATCH_SIZE)
	target_progbar = Progbar(target=mc._TARGET_EPOCH_NUM)
	for epoch in range(mc._TARGET_EPOCH_NUM):

		shuffle_data, shuffle_labels, _ = \
		pre.paired_shuffle(normalized_target_data, target_vectors, label_num)
		for loop in range(target_max_loop):
			batch_train_data = shuffle_data[loop*mc._BATCH_SIZE:(loop+1)*mc._BATCH_SIZE]
			batch_train_labels = shuffle_labels[loop*mc._BATCH_SIZE:(loop+1)*mc._BATCH_SIZE]
			loss = target_nn.train_on_batch([batch_train_data],[batch_train_labels])
		#target_progbar.add(1, values=[("target loss",loss)])


	# compute outputs of test data of target domain
	x = target_nn.predict([normalized_test_data])
	if method_flag == mc._SCRATCH or method_flag == mc._CONV_TRANSFER:
		idx = np.argmax(x, axis=1)
	elif method_flag == mc._COUNT_ATDL:
		idx = np.argmax(x,axis=1)
		for j in range(len(test_labels)):
			for i in range(mc._TARGET_DIM_NUM):
				if test_labels[j] == i:
					test_labels[j] = relations[i]
					break
	else:
		distance, idx = Neighbors(x, relation_vectors, 1)
		idx = idx[:,0]

	backend.clear_session()
	return idx.T, test_labels.T