Example #1
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
Example #2
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