Esempio n. 1
0
def eval(X, Y, args, n_classes=10, exp=0, split=0):
    x_shape = [args.batch_size] + list(X.shape[1:])
    y_shape = [args.batch_size] + list(Y.shape[1:])
    #save_file = args.save_path + "exp_{}/model_split{}.ckpt".format(exp, split)

    with tf.Graph().as_default():
        dset = InputGenerator([None]+list(X.shape[1:]), n_classes, args.z_size, batch_size=args.batch_size, n_epochs=1)
        aae = AAE("test", batch_size=args.batch_size, n_epochs=1, n_classes=n_classes, z_size=args.z_size, input_shape=x_shape)

        iterador = dset.create_eval_generator()
        x_input, y_input = iterador.get_next()
        _, y_tilde = aae.encoder(x_input)
        acc, acc_op = tf.metrics.mean_per_class_accuracy(tf.argmax(y_input, -1), tf.argmax(y_tilde, -1), n_classes)

        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, tf.train.latest_checkpoint(args.save_path + "exp_{}/".format(exp)))
            sess.run(tf.local_variables_initializer())
            sess.run(iterador.initializer, feed_dict={dset.x_input:X, dset.y_input:Y})

            n_steps = (len(X) // args.batch_size)
            with tqdm(n_steps, desc="Eval", unit="Steps") as pbar:
                try:
                    while True:
                        accuracy, _ = sess.run([acc, acc_op])
                        pbar.update()
                        pbar.set_postfix(Test_Acc=accuracy)
                except tf.errors.OutOfRangeError:
                    pass

            out_string = "Split {} Accuracy: {:02.3f}% \n".format(split + 1, 100 * accuracy)
            print(out_string)
            return out_string
Esempio n. 2
0
def generate(args, n_classes=10, exp=0, split=0, n_generados=1000):
    #x_shape = [args.batch_size] + list(X.shape[1:])
    #y_shape = [args.batch_size] + list(Y.shape[1:])
    save_file = args.save_path + "exp_{}/model_split{}.ckpt".format(exp, split)

    with tf.Graph().as_default():
        dset = InputGenerator([None, 28, 28, 1], n_classes, args.z_size, batch_size=args.batch_size, n_epochs=1)
        aae = AAE("test", batch_size=n_classes, n_epochs=1, n_classes=n_classes, z_size=args.z_size, input_shape=[None,28,28,1])

        iterador = dset.create_gen_generator()
        z_input, y_input = iterador.get_next()

        x_recon = aae.decoder(z_input, y_input) #neutralizar los numeros
        #x_bin = tf.cast(tf.greater_equal(x_recon, 0.5), tf.float32) #binarizar lo generado
        _, y_tilde = aae.encoder(x_recon)
        acc, acc_op = tf.metrics.mean_per_class_accuracy(tf.argmax(y_input, -1), tf.argmax(y_tilde, -1), n_classes)

        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, tf.train.latest_checkpoint(args.save_path + "exp_{}/".format(exp)))
            sess.run(tf.local_variables_initializer())
            sess.run(iterador.initializer)

            X_gen = np.empty((0, 28, 28, 1), dtype=np.float32)
            Y_gen = np.empty((0), dtype=np.int32)

            with tqdm(desc="Generador", unit="Steps") as pbar:
                while True:
                    x_p, y_p, y_clases, accuracy, _ = sess.run([x_recon, y_tilde, y_input, acc, acc_op])
                    pbar.update()
                    idx = (y_clases.argmax(-1) == y_p.argmax(-1)) & (y_p.max(-1) >= args.gen_threshold)
                    X_gen = np.append(X_gen, x_p[idx], axis=0)
                    Y_gen = np.append(Y_gen, y_p[idx].argmax(-1), axis=0)
                    n_gen = np.bincount(Y_gen).mean()
                    pbar.set_postfix(Gen_acc=accuracy, D_gen=n_gen, refresh=False)
                    if n_gen >= n_generados:
                        break

            assert len(X_gen) == len(Y_gen)
            gen_str = "Generados:" + str(np.bincount(Y_gen)) + "\n"
            print(gen_str)
            # print("Elementos Generados por Clase:", np.bincount(Y_gen))
            return X_gen, Y_gen, gen_str
Esempio n. 3
0
def get_train_fit(X, Y, args, n_classes=10, exp=0, split=0):
    x_shape = [args.batch_size] + list(X.shape[1:])
    y_shape = [args.batch_size] + list(Y.shape[1:])
    #save_file = args.save_path + "exp_{}/model_split{}.ckpt".format(exp, split)

    with tf.Graph().as_default():
        dset = InputGenerator([None]+list(X.shape[1:]), n_classes, args.z_size, batch_size=args.batch_size, n_epochs=1)
        aae = AAE("test", batch_size=args.batch_size, n_epochs=1, n_classes=n_classes, z_size=args.z_size, input_shape=x_shape)

        iterador = dset.create_eval_generator()
        x_input, y_input = iterador.get_next()
        _, y_tilde = aae.encoder(x_input, supervised=True)
        acc, acc_op = tf.metrics.mean_per_class_accuracy(tf.argmax(y_input, -1), tf.argmax(y_tilde, -1), n_classes)

        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, tf.train.latest_checkpoint(args.save_path + "exp_{}/".format(exp)))
            sess.run(iterador.initializer, feed_dict={dset.x_input:X, dset.y_input:Y})

            n_steps = (len(X) // args.batch_size)
            with tqdm(n_steps, desc="Novelty Train", unit="Steps") as pbar:
                try:
                    train_logits= np.empty([0, n_classes], dtype=np.float32)
                    while True:
                        logit = sess.run(y_tilde)
                        pbar.update()
                        train_logits = np.append(train_logits, logit, axis=0)
                except tf.errors.OutOfRangeError:
                    pass

                except tf.errors.OutOfRangeError:
                    pass

            assert len(train_logits) == len(Y.argmax(-1))
            om = OpenMax(n_classes)
            om.fit(train_logits, Y.argmax(-1))

        return om
Esempio n. 4
0
def get_novel_detections(X, Y, detector, args, n_classes=10, exp=0, split=0):
    save_file = args.save_path + "exp_{}/model_split{}.ckpt".format(exp, split)
    with tf.Graph().as_default():
        dset = InputGenerator([None, 28, 28, 1], n_classes-1, args.z_size, batch_size=args.batch_size, n_epochs=1)
        aae = AAE("test", batch_size=args.batch_size, z_size=args.z_size, n_epochs=1, n_classes=n_classes-1)

        #modelo
        iterador = dset.create_test_generator()
        x_input = iterador.get_next()
        _, y_tilde = aae.encoder(x_input, supervised=True)

        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, tf.train.latest_checkpoint(args.save_path + "exp_{}/".format(exp)))
            sess.run(iterador.initializer, {dset.x_input:X})
            eval_logits = np.empty((0, n_classes-1), dtype=np.float32)
            try:
                while True:
                    logit = sess.run(y_tilde)
                    eval_logits = np.append(eval_logits, logit, axis=0)
            except tf.errors.OutOfRangeError:
                pass

            openmax = detector.evaluate(eval_logits, Y)

            Ypred = np.where(openmax.max(-1) <= args.threshold, n_classes, openmax.argmax(-1))

            detect = np.intersect1d(np.argwhere(Ypred == n_classes).squeeze(), np.argwhere(Y == n_classes-1).squeeze()) #detectar los casos correctos
            percentage = len(detect)/len(Y[Y == (n_classes-1)])
            res_text = "D={} ({:.2f}%)\n".format(len(detect), 100*percentage)
            print(res_text)

            if percentage <= args.n_detects:
                print("No hay realmente información nueva")
                return False, None, None, res_text
            else:
                return True, X[detect], Y[detect], res_text
Esempio n. 5
0
def predict():
    ####################################################
    ##Compute test metrics
    ####################################################
    tf.reset_default_graph()
    aae = AAE(dims)
    saver, reconstruct_x, _, _, _ = aae.build_graph()

    k_list = range(1, 21)
    r_list = [[] for _ in range(len(k_list))]
    n_list = [[] for _ in range(len(k_list))]
    p_list = [[] for _ in range(len(k_list))]
    m_list = [[] for _ in range(len(k_list))]

    with tf.Session() as sess:
        saver.restore(sess, '{}/model'.format(chkpt_dir))

        for bnum, st_idx in enumerate(range(0, N_test, batch_size_test)):
            end_idx = min(st_idx + batch_size_test, N_test)
            X = test_data_tr[idxlist_test[st_idx:end_idx]]
            X = X.toarray().astype('float32')

            pred_val = sess.run(reconstruct_x, feed_dict={aae.x_input: X})
            # exclude examples from training and validation (if any)
            pred_val[X.nonzero()] = -np.inf
            for i in range(len(k_list)):
                r_list[i].append(
                    Recall_at_k_batch(
                        pred_val,
                        test_data_te[idxlist_test[st_idx:end_idx]],
                        k=k_list[i]))
                n_list[i].append(
                    NDCG_binary_at_k_batch(
                        pred_val,
                        test_data_te[idxlist_test[st_idx:end_idx]],
                        k=k_list[i]))
                p_list[i].append(
                    Precision_at_k_batch(
                        pred_val,
                        test_data_te[idxlist_test[st_idx:end_idx]],
                        k=k_list[i]))
                m_list[i].append(
                    MAP_at_k_batch(pred_val,
                                   test_data_te[idxlist_test[st_idx:end_idx]],
                                   k=k_list[i]))

    for i in range(len(k_list)):
        r = np.concatenate(r_list[i]).mean()
        print("Test Recall@%d=%.5f" % (k_list[i], r))

    for i in range(len(k_list)):
        n = np.concatenate(n_list[i]).mean()
        print("Test NDCG@%d=%.5f" % (k_list[i], n))

    for i in range(len(k_list)):
        p = np.concatenate(p_list[i]).mean()
        print("Test Precision@%d=%.5f" % (k_list[i], p))

    for i in range(len(k_list)):
        m = np.concatenate(m_list[i]).mean()
        print("Test MAP@%d=%.5f" % (k_list[i], m))
Esempio n. 6
0
idxlist = list(range(N))
idxlist_vad = range(N_vad)
idxlist_test = range(N_test)

batch_size = 500
batch_size_vad = 2000
batch_size_test = 2000
batches_per_epoch = int(np.ceil(float(N) / batch_size))

####################################################
##Train a AAE
####################################################

dims = [n_tags, args.layer1, args.layer2]
tf.reset_default_graph()
aae = AAE(dims, random_seed=98765)
saver, reconstruct_x, loss_vars, train_op_vars, merged_var = aae.build_graph()
autoencoder_loss, dc_loss, generator_loss = loss_vars
autoencoder_op, discriminator_op, generator_op = train_op_vars

recall_var = tf.Variable(0.0)
recall_summary = tf.summary.scalar('recall_at_k_validation', recall_var)

####################################################
##Set up logging and checkpoint directory
####################################################
arch_str = "I-%s-I" % ('-'.join([str(d) for d in aae.dims[1:-1]]))
log_dir = './log/AAE_{}'.format(arch_str)
if os.path.exists(log_dir):
    shutil.rmtree(log_dir)
print("log directory: %s" % log_dir)
Esempio n. 7
0
(X_train, label_train), _ = mnist.load_data()
X_train = tf.cast(X_train, tf.float32) / 255
n_data = X_train.shape[0]
dataset = tf.data.Dataset.from_tensor_slices(X_train)
dataset = dataset.shuffle(n_data)  # TODO redo shuffling in proper location?
dataset = dataset.batch(BATCHSIZE)
dataset = dataset.repeat(DATASET_REPS)
n_batch = ceil((n_data / BATCHSIZE) * DATASET_REPS)
print(
    f"DATASET SIZE: {n_data}\nBATCHSIZE: {BATCHSIZE}\nDATASET REPS: {DATASET_REPS}"
)

### MODEL ###

model = AAE(n_latent=6)
optimizer = tf.keras.optimizers.Adam()

### TRAINING ###


def train_step(model, optimizer, X):
    with tf.GradientTape() as tape:
        z, r = model(X)
        loss = model.loss(X, r)
    grads = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))
    return loss


# training loop
with graph.as_default():

    encoder_layers = [n_mnist_pixels, 1000, 1000, 2]
    encoder_dropout = [False, False, False]
    encoder_outfuns = [tf.nn.relu, tf.nn.relu, linear]

    decoder_layers = [2, 1000, 1000, n_mnist_pixels]
    decoder_dropout = [False, False, False]
    decoder_outfuns = [tf.nn.relu, tf.nn.relu, tf.nn.tanh]

    adversarial_layers = [2, 1000, 1000, 1]
    adversarial_dropout = [True, True, False]
    adversarial_outfuns = [tf.nn.relu, tf.nn.relu, tf.nn.sigmoid]

    aae = AAE(rlr, alr, weight_scale, encoder_outfuns, encoder_layers,
              decoder_outfuns, decoder_layers, adversarial_outfuns,
              adversarial_layers)

    # Tf Session
    with tf.Session(config=config) as session:
        # writer = tf.summary.FileWriter("output", session.graph)
        session.run(tf.global_variables_initializer())
        R_losses = []
        for epoch in range(epochs):

            r_losses = []
            data, data_labels = mmanager.shuffle_imgs()
            for t in range(len(data) // num_samples):

                # train step
                curr_data_sample, curr_data_labels = get_data_sample(t)
Esempio n. 9
0
def main(args):
    """ Parameters """
    batch_size            = args.batch_size
    num_frames_per_clip   = args.num_frames_per_clip
    dataset_shuffle       = args.dataset_shuffle
    num_epochs            = args.num_epochs
    initial_learning_rate = args.initial_learning_rate
    z_dim                 = args.z_dim
    image_crop_size       = args.image_crop_size
    
    ''' Dataset Reader'''
    reader=db.DBreader(batch_size=batch_size, n_frames_clip=num_frames_per_clip, 
                       resize=[image_crop_size, image_crop_size], shuffle=dataset_shuffle)

    ''' Build Graph'''
    # input placeholder
    x = tf.placeholder(tf.float32, 
                       shape=[batch_size, num_frames_per_clip, IMAGE_CROP_SIZE, IMAGE_CROP_SIZE, 1])
    z_sample = tf.placeholder(tf.float32, 
                              shape=[batch_size, 1, image_crop_size//4, image_crop_size//4, z_dim])
    
    ''' Network Architecture'''
    model = AAE()

    y, z, neg_marginal_likelihood, D_loss, G_loss = model.adversarial_autoencoder(x, z_sample)
    
    ''' Optimization '''
    t_vars = tf.trainable_variables()
    d_vars = [var for var in t_vars if "Discriminator" in var.name]
    g_vars = [var for var in t_vars if "Encoder" in var.name]
    ae_vars = [var for var in t_vars if "Encoder" or "Decoder" in var.name]

    train_op_ae = tf.train.AdamOptimizer(initial_learning_rate).minimize(neg_marginal_likelihood, var_list=ae_vars)
    train_op_d  = tf.train.AdamOptimizer(initial_learning_rate/5).minimize(D_loss, var_list=d_vars)
    train_op_g  = tf.train.AdamOptimizer(initial_learning_rate).minimize(G_loss, var_list=g_vars)
    
    ''' Training '''
    total_batch = reader.n_train_clips // batch_size

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print("Variable Initialized")

        for epoch in range(num_epochs):
            # Train Dataset Random Shuffling
            reader.initialize(True)

            for i in range(total_batch):
                train_x = reader.next_batch() / 255.

                # now here, generate z_sample by random noise
                # z_sample.shape.as_list()  --> sample's shape
                train_z_sample = np.random.random(z_sample.shape.as_list())

                # Reconstruction Loss
                _, loss_likelihood = sess.run([train_op_ae, neg_marginal_likelihood],
                                             feed_dict={x:train_x, z_sample:train_z_sample})

                # Discriminator loss
                _, d_loss = sess.run([train_op_d, D_loss],
                                    feed_dict={x:train_x, z_sample:train_z_sample})

                # Generator loss
                for _ in range(2):
                    _, g_loss = sess.run([train_op_g, G_loss],
                                        feed_dict={x:train_x, z_sample:train_z_sample})

                tot_loss = loss_likelihood + d_loss + g_loss
                print(" >> [%03d - %d/%d]: L_tot %03.2f, L_likelihood %03.2f, d_loss %03.2f, g_loss %03.2f" % (epoch, i, total_batch, tot_loss, loss_likelihood, d_loss, g_loss))

            # print cost every epoch
            print("epoch %03d: L_tot %03.2f, L_likelihood %03.2f, d_loss %03.2f, g_loss %03.2f" % (epoch, tot_loss, loss_likelihood, d_loss, g_loss))
Esempio n. 10
0
def generate(X, Y, args, n_classes=10, exp=0, split=0, n_generados=1000):
    x_shape = [args.batch_size] + list(X.shape[1:])
    y_shape = [args.batch_size] + list(Y.shape[1:])
    save_file = args.save_path + "exp_{}/model_split{}.ckpt".format(exp, split)

    with tf.Graph().as_default():
        dset = InputGenerator([None] + list(X.shape[1:]),
                              n_classes,
                              args.z_size,
                              batch_size=args.batch_size,
                              n_epochs=1)
        aae = AAE("test",
                  batch_size=n_classes,
                  n_epochs=1,
                  n_classes=n_classes,
                  z_size=args.z_size,
                  input_shape=x_shape)

        iterador = dset.create_gen_generator()
        z_input, y_input = iterador.get_next()

        x_recon = aae.decoder(z_input, y_input)  #neutralizar los numeros
        _, y_tilde = aae.encoder(x_recon)
        acc, acc_op = tf.metrics.mean_per_class_accuracy(
            tf.argmax(y_input, -1), tf.argmax(y_tilde, -1), n_classes)

        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(
                sess,
                tf.train.latest_checkpoint(args.save_path +
                                           "exp_{}/".format(exp)))
            sess.run(tf.local_variables_initializer())
            sess.run(iterador.initializer)

            X_gen = np.empty((0, 28, 28, 1), dtype=np.float32)  #[]
            Y_gen = np.empty((0, n_classes), dtype=np.float32)  #[]
            with tqdm(desc="Generador", unit="Steps") as pbar:
                while True:  #for step in pbar:
                    #y_clases = one_hot(np.random.randint(n_classes, size=args.batch_size), n_classes)
                    #Z = np.random.normal(0, 0.75, (args.batch_size, args.z_size))
                    x_p, y_p, y_clases, accuracy, _ = sess.run(
                        [x_recon, y_tilde, y_input, acc, acc_op])

                    pbar.update()
                    idx = (y_clases.argmax(-1) == y_p.argmax(-1)
                           )  # & (y_p.max(-1) >= 0.9)
                    # idx = np.argwhere(y_clases.argmax(-1) == y_p.argmax(-1)).squeeze()
                    X_gen = np.append(X_gen, x_p[idx], axis=0)  #+= [x_p[idx]]
                    Y_gen = np.append(Y_gen, y_p[idx], axis=0)  #+= [y_p[idx]]
                    ngen = np.bincount(Y_gen.argmax(-1)).mean()
                    pbar.set_postfix(Gen_Acc=accuracy,
                                     D_Gen=ngen,
                                     refresh=False)
                    if ngen >= n_generados:
                        break

            #print("Generated Acc={:02.3f}%".format(100*accuracy))
            assert len(X_gen) == len(Y_gen)
            #X_gen = np.concatenate(X_gen, axis=0)
            #Y_gen = np.concatenate(Y_gen, axis=0).argmax(-1)
            Y_gen = Y_gen.argmax(-1)

            gen_str = "Elementos:" + str(np.bincount(Y_gen)) + "\n"
            print(gen_str)
            # print("Elementos Generados por Clase:", np.bincount(Y_gen))
            return X_gen, Y_gen, gen_str
Esempio n. 11
0
def train(X,
          Y,
          args,
          increment=False,
          n_classes=10,
          exp=0,
          split=0,
          n_epochs=1):
    x_shape = [args.batch_size] + list(X.shape[1:])
    y_shape = [args.batch_size] + list(Y.shape[1:])
    save_file = args.save_path + "exp_{}/model_split{}.ckpt".format(exp, split)
    monitor_path = args.monitor + "exp_{}/split_{}".format(exp, split)

    with tf.Graph().as_default():

        dset = InputGenerator([None] + list(X.shape[1:]),
                              n_classes=n_classes,
                              args.z_size,
                              batch_size=args.batch_size,
                              n_epochs=n_epochs)
        aae = AAE("train",
                  batch_size=args.batch_size,
                  n_epochs=n_epochs,
                  n_classes=n_classes,
                  z_size=args.z_size,
                  input_shape=x_shape)

        iterador = dset.create_train_generator()
        (x_input, y_input), (z_real, y_real) = iterador.get_next()
        # Estructura
        z_hat, y_hat = aae.encoder(x_input)

        pz = tf.sigmoid(z_hat + 1e-8)
        entropia = tf.reduce_mean(-tf.reduce_sum(pz * tf.log(pz), 1))
        #entropia = tf.reduce_mean(0.5*tf.norm(z_hat - z_real, ord=1, axis=1))
        x_recon = aae.decoder(z_hat, y_hat)

        dz_real = aae.discriminator_z(z_real)
        dz_fake = aae.discriminator_z(z_hat)
        dy_real = aae.discriminator_y(y_real)
        dy_fake = aae.discriminator_y(y_hat)

        _, y_tilde = aae.encoder(x_input, supervised=True)

        # Metricas
        acc, acc_op = tf.metrics.mean_per_class_accuracy(
            tf.argmax(y_input, -1), tf.argmax(y_tilde, -1), n_classes)
        mse, mse_op = tf.metrics.mean_squared_error(x_input, x_recon)

        # Costos
        ae_loss = tf.losses.log_loss(
            x_input, x_recon
        )  # tf.reduce_mean(aae.binary_crossentropy(x_input, x_recon))
        clf_loss = tf.losses.softmax_cross_entropy(
            y_input, y_tilde
        )  #tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_input, logits=y_tilde))

        dz_real_loss = tf.losses.sigmoid_cross_entropy(
            tf.ones_like(dz_real), dz_real
        )  #tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(dz_real), logits=dz_real))
        dz_fake_loss = tf.losses.sigmoid_cross_entropy(
            tf.zeros_like(dz_fake), dz_fake
        )  #tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.zeros_like(dz_fake), logits=dz_fake))
        dz_loss = dz_real_loss + dz_fake_loss

        dy_real_loss = tf.losses.sigmoid_cross_entropy(
            tf.ones_like(dy_real), dy_real
        )  #tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(dy_real), logits=dy_real))
        dy_fake_loss = tf.losses.sigmoid_cross_entropy(
            tf.zeros_like(dy_fake), dy_fake
        )  #tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.zeros_like(dy_fake), logits=dy_fake))
        dy_loss = dy_real_loss + dy_fake_loss

        gz_loss = tf.losses.sigmoid_cross_entropy(
            tf.ones_like(dz_fake), dz_fake
        )  #tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(dz_fake), logits=dz_fake))
        gy_loss = tf.losses.sigmoid_cross_entropy(
            tf.ones_like(dy_fake), dy_fake
        )  #tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(dy_fake), logits=dy_fake))
        gen_loss = gz_loss + gy_loss

        # Training ops
        all_vars = tf.trainable_variables()
        dz_vars = [var for var in all_vars if "Discriminator_Z" in var.name]
        dy_vars = [var for var in all_vars if "Discriminator_Y" in var.name]
        enc_vars = [var for var in all_vars if "Encoder" in var.name]

        if increment:
            increment_vars = [
                var for var in tf.global_variables() if "Y_" not in var.name
            ]
            init_vars = [
                var for var in tf.global_variables() if "Y_" in var.name
            ]
        else:
            increment_vars = None
            init_vars = None

        step_tensor = tf.Variable(0, trainable=False, name="Step")
        #learning_rate = tf.train.polynomial_decay(0.005, global_step=step_tensor, decay_steps=20000, end_learning_rate=0.000001, power=2, name="Learning_rate")
        learning_rate = 0.001
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

        with tf.control_dependencies(update_ops):
            ae_opt = tf.train.AdamOptimizer(learning_rate).minimize(
                ae_loss, global_step=step_tensor)
            dz_opt = tf.train.AdamOptimizer(learning_rate).minimize(
                dz_loss, var_list=dz_vars)
            dy_opt = tf.train.AdamOptimizer(learning_rate).minimize(
                dy_loss, var_list=dy_vars)
            gen_opt = tf.train.AdamOptimizer(learning_rate).minimize(
                gen_loss, var_list=enc_vars)
            clf_opt = tf.train.AdamOptimizer(learning_rate).minimize(
                clf_loss, var_list=enc_vars)
            train_ops = tf.group([ae_opt, dz_opt, dy_opt, gen_opt, clf_opt])

        # summaries
        tf.summary.scalar("Losses/AE_loss", ae_loss)
        tf.summary.scalar("Losses/Dis_Y_loss", dy_loss)
        tf.summary.scalar("Losses/Dis_Z_loss", dz_loss)
        tf.summary.scalar("Losses/Gen_loss", gen_loss)
        tf.summary.scalar("Losses/Clf_loss", clf_loss)
        tf.summary.scalar("Metrics/Accuracy", acc)
        tf.summary.scalar("Metrics/MSE", mse)
        tf.summary.scalar("Metrics/Entropy", entropia)
        #tf.summary.scalar("Metrics/LearningRate",learning_rate)
        tf.summary.histogram("Z_pred", z_hat)
        tf.summary.histogram("Z_real", z_real)
        tf.summary.image("X_Real", x_input, 10)
        tf.summary.image("X_Recon", x_recon, 10)
        summary_op = tf.summary.merge_all()

        if increment:
            saver = tf.train.Saver(increment_vars)
        ckpt_saver = tf.train.Saver()

        with tf.Session() as sess:
            if increment:
                sess.run(tf.global_variables_initializer())
                saver.restore(
                    sess,
                    tf.train.latest_checkpoint(args.save_path +
                                               "exp_{}/".format(exp)))
                # sess.run(tf.variables_initializer(init_vars))
            else:
                sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            summary_writer = tf.summary.FileWriter(monitor_path)
            #Cargar los datasets
            sess.run(iterador.initializer,
                     feed_dict={
                         dset.x_input: X,
                         dset.y_input: Y
                     })
            n_steps = (len(X) // args.batch_size) * n_epochs
            # Operacion de entrenamiento:
            with tqdm(desc="Train", total=n_steps, unit="Steps",
                      miniters=10) as pbar:
                try:
                    while True:
                        _, step, accuracy, msqer, _, _, summary = sess.run([
                            train_ops, step_tensor, acc, mse, acc_op, mse_op,
                            summary_op
                        ])
                        summary_writer.add_summary(summary, step)
                        pbar.update()
                        if step % 10 == 0:
                            pbar.set_postfix(Accuracy=accuracy,
                                             MSE=msqer,
                                             refresh=False)

                except tf.errors.OutOfRangeError:
                    pass
            ckpt_saver.save(sess, save_path=save_file)
        print("Done!")
Esempio n. 12
0
def main(args):
    dataset = EMNIST()

    with open("res_exp2.txt", "w") as f:
        for exp in np.arange(1, args.n_exps+1): #Haremos 1 validacion por ahora
            #exp=1
            f.write("\n-- Experimento {}/{} --\n".format(exp, args.n_exps))#1, 1))#

            #Directorios de guardado
            create_dir(args.save_path + "exp_{}/".format(exp))

            np.random.seed(args.seed)
            #Entrenaremos la red con los datos iniciales de los digitos
            Xtrain, Ytrain = dataset.load_segment_of_data(np.arange(10), "train")
            Xtrain, Ytrain = shuffle(Xtrain, Ytrain, random_state=args.seed)
            Ytrain = one_hot(Ytrain, 10)

            train(Xtrain, Ytrain, args, n_classes=10, exp=exp, n_epochs=args.n_epochs)
            #Evaluamos el desempeño del modelo
            Xtest, Ytest = dataset.load_segment_of_data(np.arange(10), "test")
            Ytest = one_hot(Ytest, 10)
            eval_str = eval(Xtest, Ytest, args, n_classes=10, exp=1)
            f.write(eval_str)
            f.write("Letra;SoftmaxAcc;OpenMaxAcc;Deteccion;Errores\n")

            omax = get_train_fit(Xtrain, Ytrain, args, n_classes=10, exp=1)
            save_file = args.save_path + "exp_{}/model_split{}.ckpt".format(exp, 0)

            Xtest, Ytest = dataset.load_segment_of_data(np.arange(10), "test")
            with tf.Graph().as_default():
                dset = InputGenerator([None,28,28,1], 10, args.z_size, batch_size=args.batch_size, n_epochs=1)
                aae = AAE("test", batch_size=args.batch_size, z_size=args.z_size, n_epochs=1, n_classes=10)
                iterador = dset.create_test_generator()
                x_input = iterador.get_next()
                _, y_tilde = aae.encoder(x_input, supervised=True)

                saver = tf.train.Saver()
                with tf.Session() as sess:
                    saver.restore(sess, tf.train.latest_checkpoint(args.save_path + "exp_{}/".format(exp)))

                    for i in np.arange(10, dataset.n_classes):
                        Xc, Yc = dataset.load_segment_of_data([i], kind="train")
                        Xchar = np.concatenate([Xtest, Xc], 0)
                        Ychar = np.concatenate([Ytest, Yc], 0)
                        Ychar[Ychar>9] = 10

                        sess.run(iterador.initializer, {dset.x_input:Xchar})
                        eval_logits = np.empty((0, 10), dtype=np.float32)
                        try:
                            while True:
                                logit = sess.run(y_tilde)
                                eval_logits = np.append(eval_logits, logit, axis=0)
                        except tf.errors.OutOfRangeError:
                            pass

                        openmax = omax.evaluate(eval_logits, Ychar)
                        softmax = omax._softmax(eval_logits)

                        Ypred = np.where(openmax.max(-1) <= args.threshold, 10, openmax.argmax(-1))
                        sm_acc = accuracy_score(Ychar, softmax.argmax(-1))
                        om_acc = accuracy_score(Ychar, Ypred)

                        detect = len(np.intersect1d(np.argwhere(Ypred == 10).squeeze(), np.argwhere(Ychar == 10).squeeze()))/len(Yc)
                        mistakes = len(np.intersect1d(np.argwhere(Ypred == 10).squeeze(), np.argwhere(Ychar < 10).squeeze()))/len(Yc)
                        res_text = "{};{:2.3f};{:2.3f};{:2.3f};{:2.3f}\n".format(CARACTERES[i], 100*sm_acc, 100*om_acc, 100*detect, 100*mistakes)
                        print(res_text)
                        f.write(res_text)
Esempio n. 13
0
def train(X, Y , args, n_classes=10, exp=0, split=0, n_epochs=1):
    x_shape = [args.batch_size] + list(X.shape[1:])
    y_shape = [args.batch_size] + list(Y.shape[1:])
    save_file = args.save_path + "exp_{}/model_split{}.ckpt".format(exp, split)

    with tf.Graph().as_default():
        dset = InputGenerator([None]+list(X.shape[1:]), n_classes, z_size=args.z_size, batch_size=args.batch_size, n_epochs=n_epochs)
        aae = AAE("train", batch_size=args.batch_size, n_epochs=n_epochs, z_size=args.z_size, n_classes=n_classes)

        iterador = dset.create_train_generator()
        (x_input, y_input), (z_real, y_real) = iterador.get_next()
        # Estructura
        z_hat, y_hat = aae.encoder(x_input)
        x_recon = aae.decoder(z_hat, y_hat)

        dz_real = aae.discriminator_z(z_real)
        dz_fake = aae.discriminator_z(z_hat)
        dy_real = aae.discriminator_y(y_real)
        dy_fake = aae.discriminator_y(y_hat)

        _, y_tilde = aae.encoder(x_input, supervised=True)

        # Metricas
        acc, acc_op = tf.metrics.mean_per_class_accuracy(tf.argmax(y_input, -1), tf.argmax(y_tilde, -1), n_classes)
        mse, mse_op = tf.metrics.mean_squared_error(x_input, x_recon)

        # Costos
        ae_loss = tf.losses.log_loss(x_input, x_recon)
        clf_loss = tf.losses.softmax_cross_entropy(y_input, y_tilde)

        dz_real_loss = tf.losses.sigmoid_cross_entropy(tf.ones_like(dz_real), dz_real)
        dz_fake_loss = tf.losses.sigmoid_cross_entropy(tf.zeros_like(dz_fake), dz_fake)
        dz_loss = dz_real_loss + dz_fake_loss

        dy_real_loss = tf.losses.sigmoid_cross_entropy(tf.ones_like(dy_real), dy_real)
        dy_fake_loss = tf.losses.sigmoid_cross_entropy(tf.zeros_like(dy_fake), dy_fake)
        dy_loss = dy_real_loss + dy_fake_loss

        gz_loss = tf.losses.sigmoid_cross_entropy(tf.ones_like(dz_fake), dz_fake)
        gy_loss = tf.losses.sigmoid_cross_entropy(tf.ones_like(dy_fake), dy_fake)
        gen_loss = gz_loss + gy_loss

        # Training ops
        all_vars = tf.trainable_variables()
        dz_vars = [var for var in all_vars if "Discriminator_Z" in var.name]
        dy_vars = [var for var in all_vars if "Discriminator_Y" in var.name]
        enc_vars = [var for var in all_vars if "Encoder" in var.name]

        step_tensor = tf.Variable(0, trainable=False, name="Step")
        learning_rate = 0.001
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

        with tf.control_dependencies(update_ops):
            ae_opt = tf.train.AdamOptimizer(learning_rate).minimize(ae_loss, global_step=step_tensor)
            dz_opt = tf.train.AdamOptimizer(learning_rate).minimize(dz_loss, var_list=dz_vars)
            dy_opt = tf.train.AdamOptimizer(learning_rate).minimize(dy_loss, var_list=dy_vars)
            gen_opt = tf.train.AdamOptimizer(learning_rate).minimize(gen_loss, var_list=enc_vars)
            clf_opt = tf.train.AdamOptimizer(learning_rate).minimize(clf_loss, var_list=enc_vars)
            train_ops = tf.group([ae_opt,dz_opt,dy_opt,gen_opt,clf_opt])

        ckpt_saver = tf.train.Saver()
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())

            sess.run(iterador.initializer, feed_dict={dset.x_input:X, dset.y_input:Y})
            n_steps = (len(X) // args.batch_size)*n_epochs
            # Operacion de entrenamiento:
            with tqdm(desc="Train", total=n_steps, unit="Steps", miniters=10) as pbar:
                try:
                    while True:
                        _, step, accuracy, msqer, _, _ = sess.run([train_ops, step_tensor, acc, mse, acc_op, mse_op])
                        pbar.update()
                        if step % 10 == 0:
                            pbar.set_postfix(Accuracy=accuracy, MSE=msqer, refresh=False)
                except tf.errors.OutOfRangeError:
                    pass
            ckpt_saver.save(sess, save_path=save_file)
        print("Training Done!")