Ejemplo n.º 1
0
    def sample(self, sess, md, std):

        seeds = np.random.normal(md, std, [BATCH, self.latent])
        out = sess.run(self.last, feed_dict={self.sampler: seeds})
        tt = np.reshape(out, [BATCH, leng, 5])
        tot = sketcher.save_batch_diff_z_axis(list((1 + tt[0:16]) * 128),
                                              list((1 + tt[0:16]) * 128),
                                              "out/" + str(self.dr) + "/imgs",
                                              "SAMPLING")
Ejemplo n.º 2
0
    def train(self, sess):

        for e in range(0, EPOCHS):
            g = utils.get_coord_drawings_z_axis(BATCH, leng)
            for i in range(0, 1000):
                x_, y_ = next(g)
                x_ = np.reshape(x_, [BATCH, -1])
                y_ = np.reshape(y_, [BATCH, -1])
                ls = sess.run([self.loss, self.min],
                              feed_dict={self.input: x_})

            out, m, avg, std = sess.run(
                [self.output, self.sampler, self.center, self.std],
                feed_dict={self.input: x_})

            print(avg, std)
            tt = np.reshape(out, [BATCH, leng, 5])
            y_draw = np.reshape(y_, [BATCH, leng, 5])
            self.sample(sess, avg, std)
            tot = sketcher.save_batch_diff_z_axis(
                list((1 + tt[0:16]) * 128), list((1 + y_draw[0:16]) * 128),
                "out/" + str(self.dr) + "/imgs", str(e))
Ejemplo n.º 3
0
def main():
    x_input = []
    gen = get_coord_drawings_z_axis()
    for i in range(0, 2048):
        x, y = next(gen)
        x_input.extend(x)

    x_input = np.asarray(x_input)
    print(x_input.shape)
    y_input = x_input
    input_dim = 5  # 13

    vae, enc, gen = create_lstm_vae(input_dim,
                                    timesteps=timesteps,
                                    batch_size=batch_size,
                                    intermediate_dim=32,
                                    latent_dim=100,
                                    epsilon_std=1.)

    for i in range(4):
        vae.fit(x_input, x_input, epochs=4, batch_size=batch_size)

        preds = vae.predict(x_input, batch_size=batch_size)

        # pick a column to plot.
        print("[plotting...]")
        print("x: %s, preds: %s" % (x_input.shape, preds.shape))
        tt = preds
        l = np.exp(tt[:, :, 2:5])
        ld = np.sum(np.exp(tt[:, :, 2:5]), axis=-1, keepdims=True)
        l = l / ld
        print(l[0])
        print(tt[0])
        print(y_input[0])
        tot = sketcher.save_batch_diff_z_axis(list((1 + tt[0:16]) * 128),
                                              list((1 + y_input[0:16]) * 128),
                                              "./",
                                              str(i) + "FINAL")
Ejemplo n.º 4
0
def test_better_model():
    #qui uso dati :[x,y,pen_down]
    dr = str(time.time())
    os.mkdir("out/" + dr)
    os.mkdir("out/" + dr + "/imgs")
    os.mkdir("out/" + dr + "/gene")
    x_in = tf.placeholder(tf.float32, shape=[BATCH, leng, 5])
    print(x_in)
    y_in = tf.placeholder(tf.float32, shape=[BATCH, leng, 5])
    print(y_in)
    z_in = tf.placeholder(tf.float32, shape=[BATCH, 256])
    pred_pos, pred_state, latent, mu, sigma = better_model(x_in)

    _, _, z, _, _ = better_model(x_in, z_in)
    ######################################################
    latent_losses = 0.5 * tf.reduce_sum(
        tf.square(mu) + tf.square(sigma) - tf.log(tf.square(sigma)) - 1,
        axis=1)

    #################################################
    loss = tf.losses.mean_squared_error(y_in[:, :, 0:2], pred_pos)
    cro = tf.losses.mean_squared_error(y_in[:, :, 2:5], pred_state)
    final = tf.reduce_mean(latent_losses) + (loss + cro)
    optimizer = tf.train.AdamOptimizer(learning_rate=0.005)
    #optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.002)
    #gvs = optimizer.compute_gradients(final)
    #capped_gvs = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gvs]
    #minimize = optimizer.apply_gradients(capped_gvs)
    minimize = optimizer.minimize(final)

    tf.summary.scalar("mse", loss)
    sess = tf.Session()
    train_writer = tf.summary.FileWriter(dr + "/", sess.graph)
    init_op = tf.initialize_all_variables()
    saver = tf.train.Saver()
    sess.run(init_op)

    idx = 0
    for i in range(EPOCHS):
        gene = get_coord_drawings_z_axis()
        print(":.......EPOCH " + str(i) + "........")

        merge = tf.summary.merge_all()
        for x, y in gene:
            #print("idx: "+str(idx))
            sess.run(minimize, {x_in: x, y_in: y})
            if (idx % 10 == 0):
                cros, lo, tots, summary = sess.run([cro, loss, final, merge], {
                    x_in: x,
                    y_in: y
                })

                #print("##################")
                #print("states",cros)
                #print("positions",lo)
                print("total", tots)
            #     train_writer.add_summary(summary,idx)
            # if(idx%100==0):
            #     #sanity check
            #     cords, states = sess.run([pred_pos, pred_state], {x_in: x, y_in: y})
            #     print(y[0,:,0:2],cords[0])

            if (idx % 500 == 0):
                print("Saving images...")
                #diff = sess.run(loss, {x_in: x, y_in: y})
                cords, states = sess.run([pred_pos, pred_state], {
                    x_in: x,
                    y_in: y
                })
                total = np.concatenate(
                    [cords, np.reshape(states, [BATCH, leng, 3])], -1)
                tt = total
                #print(tt[0])
                # print(y[0])
                # print(cords[0])
                # print(states[0])
                # print("##################")
                np.random.seed(i)
                img = np.random.randint(0, BATCH)
                #sketcher.save_tested(list((0.5 + tt[img])*256),"denseR",str(i)+str(idx))
                tot = sketcher.save_batch_diff_z_axis(
                    list((1 + tt[0:16]) * 128), list((1 + y[0:16]) * 128),
                    "out/" + dr + "/imgs",
                    str(i) + str(idx))
                #sketcher.save_batch(list((1 + y[0:16]) * 128), dr + "/imgs", str(i) + str(idx)+"gt")
                tot = np.array(tot)
                tot = np.expand_dims(tot, 0)
                tf.summary.image(str(i) + "_" + str(idx % 500), np.array(tot))

            if (idx % 1000 == 0):
                saver.save(sess, "out/" + dr + "/model.ckpt")
            #     inter=sess.run(latent, {x_in:x})
            #     intepolations=[gen_interpolation(inter[i],inter[i+1]) for i in range(0,4)]
            #     ltn=[]
            #     for r in intepolations:
            #         ltn.extend(r)
            #     ltn=np.array(ltn)
            #     tt = sess.run(z, {x_in:x,z_in: ltn})
            #     sketcher.save_batch_z_axis(list((1+ tt[0:16]) * 128),"out/"+dr+"/gene", str(i) + str(0))
            #     sketcher.save_batch_z_axis(list((1 + tt[16:32]) * 128), "out/"+dr+ "/gene", str(i) + str(1))
            #     sketcher.save_batch_z_axis(list((1 + tt[32:48]) * 128), "out/"+dr+ "/gene", str(i) + str(2))
            #     sketcher.save_batch_z_axis(list((1 + tt[48:64]) * 128), "out/"+dr+ "/gene", str(i) + str(3))

            idx += 1
Ejemplo n.º 5
0
    def train(self, sess):
        saver = tf.train.Saver()
        saver.restore(sess,
                      "./out/1548108293.8154037_100_256_128_NONE/model.ckpt")

        tiled_leng = np.tile(self.leng, self.batch_size)
        self.writer = tf.summary.FileWriter("./test/" + str(self.dr),
                                            sess.graph)
        alpha = 0.1
        al = alpha
        lrs = [0.14]
        iters = int((2 * 70000) / self.batch_size)
        m_iter = iters
        train_data, test_data = utils.get_train_test_gens(
            self.batch_size, leng)
        show_test = next(test_data)
        x_show, cat_x_show = show_test
        means = np.random.normal(0, 1.0, [self.batch_size, self.latent_size])
        y_show = x_show
        lr = 0.00001
        ot = sketcher.save_batch_diff_z_axis(np.zeros_like(y_show[0:64]),
                                             y_show[0:64],
                                             "out/" + str(self.dr) + "/imgs",
                                             str(0) + "_" + str(0))
        # gen = sketcher.save_batch_diff_z_axis(np.zeros_like(y_show[0:64]), np.zeros_like(y_show[0:64]), "out/" + str(self.dr) + "/gene",
        #                                       str(0) + "_" + str(0) + "generated")
        #
        # ot_t=tf.placeholder(dtype=tf.uint8)
        # gen_t = tf.placeholder(dtype=tf.uint8)
        # self.writer.add_summary(tf.summary.image("ot", ot_t)).eval()
        # self.writer.add_summary(tf.summary.image("gent", gen_t)).eval()

        for e in range(0, self.epochs):

            # if(e%2==0):
            #     al=min(1.0,alpha)
            #     alpha = al* self.accel

            if (e % 5 == 2):
                al = al + 0.05
            for i in range(0, iters):
                both = next(train_data)
                x_, cat_x = both
                y_ = x_

                ls, _ = sess.run(
                    [self.loss, self.minimize],
                    feed_dict={
                        self.dropout: 0.7,
                        self.input: x_,
                        self.target: y_,
                        self.category: cat_x,
                        self.input_size: tiled_leng,
                        self.alpha: al,
                        self.learning_rate: lr
                    })

                if (i % 1000 == 0):
                    saver.save(sess, "out/" + str(self.dr) + "/model.ckpt")

                if (i % 50 == 0):

                    both_test = next(test_data)
                    x_test, cat_x_test = both_test
                    y_test = x_test
                    #print("LOSS:", ls,"at iter:",str(i),"of ",str(m_iter), " EPOCH:", str(e), " ALPHA:", str(al),"LR: ",str(lr))
                    summary, cr, tst_ls = sess.run(
                        [self.merged, self.cr, self.loss],
                        feed_dict={
                            self.dropout: 1.0,
                            self.input: x_test,
                            self.category: cat_x_test,
                            self.target: y_test,
                            self.input_size: tiled_leng,
                            self.alpha: al,
                            self.learning_rate: lr
                        })
                    self.writer.add_summary(summary, i + (e * m_iter))
                    print("TEST_LOSS:", tst_ls, " LOSS:", ls,
                          "at iter:", str(i), "of ", str(m_iter), " EPOCH:",
                          str(e), " ALPHA:", str(al), "LR: ", str(lr))
                if (i % 200 == 0):
                    self.tot, s = sess.run(
                        [self.last, self.latent_h],
                        feed_dict={
                            self.dropout: 1.0,
                            self.input: x_show,
                            self.category: cat_x_show,
                            self.input_size: tiled_leng,
                            self.alpha: al,
                            self.learning_rate: lr
                        })
                    self.test = sess.run(self.last,
                                         feed_dict={
                                             self.dropout: 1.0,
                                             self.sampled_z_c: means,
                                             self.category: cat_x_show,
                                             self.input_size: tiled_leng,
                                             self.alpha: al,
                                             self.learning_rate: lr
                                         })
                    for f in range(4):
                        ot = sketcher.save_batch_diff_z_axis(
                            self.tot[f * 16:(f + 1) * 16],
                            y_show[f * 16:(f + 1) * 16],
                            "out/" + str(self.dr) + "/imgs",
                            str(e) + "_" + str(i) + "_" + str(f))

                        gen = sketcher.save_batch_diff_z_axis(
                            self.test[f * 16:(f + 1) * 16],
                            self.test[f * 16:(f + 1) * 16],
                            "out/" + str(self.dr) + "/gene",
                            str(e) + "_" + str(i) + "generated" + "_" + str(f))
                        self.tensor_image = self.make_image(np.asarray(ot))
                        self.writer.add_summary(
                            tf.Summary(value=[
                                tf.Summary.Value(tag='test_' + str(f),
                                                 image=self.tensor_image)
                            ]))
                        self.tensor_image = self.make_image(np.asarray(gen))
                        self.writer.add_summary(
                            tf.Summary(value=[
                                tf.Summary.Value(tag='gen_' + str(f),
                                                 image=self.tensor_image)
                            ]))