Пример #1
0
 def finetune(self, X):
     print('Fine Tuning')
     batches_per_epoch = int(len(X) / self.batch_size)
     with tf.Session() as sess:
         self.saver.restore(
             sess,
             tf.train.latest_checkpoint('./weights/sdae/' +
                                        self.scope_name))
         tmp = np.copy(X)
         tmp = utils.add_noise(tmp, self.noise[0])
         X = tmp
         for epoch in range(self.epoch[0]):
             avg_loss = 0.
             self.learning_rate = utils.get_learning_rate(
                 self.learning_rate_decay, self.initial_learning_rate,
                 epoch)
             for i in range(batches_per_epoch):
                 batch_x, batch_y = utils.get_batch(X, X, self.batch_size)
                 sess.run(self.finetuning_optimizer,
                          feed_dict={self.x[0]: batch_x})
                 loss = sess.run(self.finetuning_loss,
                                 feed_dict={self.x[0]: batch_x})
                 avg_loss += loss
             avg_loss /= batches_per_epoch
             print('epoch {0}: loss = {1:.6f}'.format(epoch, avg_loss))
         self.saver.save(sess,
                         './weights/sdae/' + self.scope_name +
                         '/checkpoint',
                         global_step=0)
Пример #2
0
    def train(self, X):
        batches_per_epoch = int(len(X) / self.batch_size)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for layer in range(self.depth):
                print('Layer {0}'.format(layer + 1))
                tmp = np.copy(X)
                tmp = utils.add_noise(tmp, self.noise[layer])
                X = tmp
                for epoch in range(self.epoch[layer]):
                    avg_loss = 0.
                    self.learning_rate = utils.get_learning_rate(
                        self.learning_rate_decay, self.initial_learning_rate,
                        epoch)
                    for i in range(batches_per_epoch):
                        batch_x, batch_y = utils.get_batch(
                            X, X, self.batch_size)
                        sess.run(self.layerwise_optimizers[layer],
                                 feed_dict={self.x[layer]: batch_x})
                        loss = sess.run(self.layerwise_losses[layer],
                                        feed_dict={self.x[layer]: batch_x})
                        avg_loss += loss
                    avg_loss /= batches_per_epoch
                    print("Epoch {0}: loss = {1:.6f}".format(epoch, avg_loss))
                X = sess.run(self.layerwise_encoded[layer],
                             feed_dict={self.x[layer]: X})
            self.saver.save(sess,
                            './weights/sdae/' + self.scope_name +
                            '/checkpoint',
                            global_step=0)
Пример #3
0
    def train(self, X, Y, lengths):
        batches_per_epoch = int(len(X) / self.batch_size)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for epoch in range(self.epoch):
                avg_loss = 0.
                avg_accuracy = 0.
                self.learning_rate = utils.get_learning_rate(self.learning_rate_decay, self.initial_learning_rate, epoch)
                for i in range(batches_per_epoch):
                    batch_x, batch_y, batch_length = utils.get_rnn_sequential_batch(X, Y, lengths, i * self.batch_size, self.batch_size)
                    batch_x = utils.add_noise(batch_x, self.noise)
                    sess.run(self.optimizer, feed_dict={self.x: batch_x, self.y: batch_y, self.sequence_length: batch_length})
                    loss, accuracy = sess.run([self.loss, self.accuracy], feed_dict={self.x: batch_x, self.y: batch_y, self.sequence_length: batch_length})
                    avg_loss += loss
                    avg_accuracy += accuracy
                avg_loss /= batches_per_epoch
                avg_accuracy /= batches_per_epoch
                print("Epoch {0}: loss = {1:.6f}, accuracy = {2:.2f}%".format(epoch, avg_loss, avg_accuracy * 100))
            self.saver.save(sess, './weights/lstm/' + self.scope_name + '/checkpoint', global_step=0)
    def train(self,
              content_img_path='images/content/content1.jpg',
              style_img_path='images/style/style1.jpg',
              noise_img_path='images/content/content1.jpg',
              output_img_path='results/anaoas',
              tensorboard_path='tensorboard/tensorboard_anaoas',
              show_img=None):
        summ = tf.summary.merge_all()
        with tf.Session() as sess:
            ut = Utils()
            noise_img_bytes = sess.run(
                self.file_bytes, feed_dict={self.name_file: noise_img_path})
            noise_img_np = np.fromstring(noise_img_bytes, np.uint8)
            noise_img = np.reshape(
                ut.add_noise(
                    ut.get_img(noise_img_np,
                               width=self.noise_img_width,
                               height=self.noise_img_height)),
                (1, self.noise_img_height, self.noise_img_width,
                 self.noise_img_channels))
            sess.run(tf.global_variables_initializer(),
                     feed_dict={self.noise_img_init: noise_img})

            writer = tf.summary.FileWriter(tensorboard_path)
            writer.add_graph(sess.graph)

            content_img_bytes = sess.run(
                self.file_bytes, feed_dict={self.name_file: content_img_path})
            content_img_np = np.fromstring(content_img_bytes, np.uint8)
            content_img = np.reshape(
                ut.get_img(content_img_np,
                           width=self.content_img_width,
                           height=self.content_img_height),
                (1, self.content_img_height, self.content_img_width,
                 self.content_img_channels))
            style_img_bytes = sess.run(
                self.file_bytes, feed_dict={self.name_file: style_img_path})
            style_img_np = np.fromstring(style_img_bytes, np.uint8)
            style_img = np.reshape(
                ut.get_img(style_img_np,
                           width=self.style_img_width,
                           height=self.style_img_height),
                (1, self.style_img_height, self.style_img_width,
                 self.style_img_channels))

            for i in range(self.num_iters):
                _, content_loss, style_loss, tv_loss, out_loss, out_img = sess.run(
                    [
                        self.optim, self.content_loss, self.style_loss,
                        self.total_variation_loss, self.total_loss,
                        self.noise_img
                    ],
                    feed_dict={
                        self.content_img: content_img,
                        self.style_img: style_img
                    })

                print('it: ', i)
                print('Content loss: ', content_loss)
                print('Style loss: ', style_loss)
                print('Total variation loss: ', tv_loss)
                print('Total loss: ', out_loss)

                if i % 50 == 0:
                    decoded_img = ut.denormalize_img(out_img[0])
                    decoded_img = cv2.cvtColor(decoded_img, cv2.COLOR_BGR2RGB)
                    sess.run(self.fwrite,
                             feed_dict={
                                 self.decoded_img:
                                 decoded_img,
                                 self.name_file:
                                 output_img_path + '/img' + str(i) + '.png'
                             })

                    if show_img:
                        plt.axis("off")
                        plt.imshow(decoded_img)
                        plt.show()

                    s = sess.run(summ,
                                 feed_dict={
                                     self.content_img: content_img,
                                     self.style_img: style_img
                                 })
                    writer.add_summary(s, i)