Exemplo n.º 1
0
        def train_step(inp, tar):
            tar_inp = tar[:, :-1]
            tar_real = tar[:, 1:]

            enc_padding_mask, combined_mask, dec_padding_mask = create_masks(
                inp, tar_inp)

            with tf.GradientTape() as tape:
                predictions, _ = self.transformer(inp, tar_inp, True,
                                                  enc_padding_mask,
                                                  combined_mask,
                                                  dec_padding_mask)
                loss = loss_function(tar_real, predictions)

            gradients = tape.gradient(loss,
                                      self.transformer.trainable_variables)
            self.optimizer.apply_gradients(
                zip(gradients, self.transformer.trainable_variables))

            self.train_loss(loss)
            self.train_accuracy(tar_real, predictions)
Exemplo n.º 2
0
def train(batch_num,
          points_glob,
          resolution=0.2,
          scale=4,
          lr=0.01,
          voxel_shape=(800, 800, 40),
          x=(0, 80),
          y=(-40, 40),
          z=(-2.5, 1.5),
          epochs=1,
          model_path=None,
          clear_graph=False,
          model_prefix="3dcnn_"):
    if clear_graph:
        clear_tf_graph()

    print("Training...")

    with tf.Session() as sess:
        model, voxel, phase_train = layers.get_model(sess,
                                                     CNNModel,
                                                     voxel_shape=voxel_shape,
                                                     activation=tf.nn.relu,
                                                     is_training=True)

        saver = tf.train.Saver()
        if model_path is not None:
            saver.restore(sess, model_path)
        # total_loss, obj_loss, cord_loss, g_map, g_cord = loss_func(model)
        total_loss, obj_loss, cord_loss, is_obj_loss, non_obj_loss, g_map, g_cord, y_pred = layers.loss_function(
            model)
        optimizer = layers.create_optimizer(total_loss, lr=lr)

        sess.run(tf.global_variables_initializer())

        for epoch in range(epochs):
            for (batch_x, batch_g_map,
                 batch_g_cord) in layers.lidar_generator(batch_num,
                                                         points_glob,
                                                         resolution=resolution,
                                                         scale=scale,
                                                         x=x,
                                                         y=y,
                                                         z=z):
                sess.run(optimizer,
                         feed_dict={
                             voxel: batch_x,
                             g_map: batch_g_map,
                             g_cord: batch_g_cord,
                             phase_train: True
                         })

                cc = sess.run(cord_loss,
                              feed_dict={
                                  voxel: batch_x,
                                  g_map: batch_g_map,
                                  g_cord: batch_g_cord,
                                  phase_train: True
                              })
                iol = sess.run(is_obj_loss,
                               feed_dict={
                                   voxel: batch_x,
                                   g_map: batch_g_map,
                                   g_cord: batch_g_cord,
                                   phase_train: True
                               })
                nol = sess.run(non_obj_loss,
                               feed_dict={
                                   voxel: batch_x,
                                   g_map: batch_g_map,
                                   g_cord: batch_g_cord,
                                   phase_train: True
                               })
                print("Epoch:", '%04d' % (epoch + 1), "cord_loss_cost=",
                      "{:.9f}".format(cc), "is_obj_loss_cost=",
                      "{:.9f}".format(iol), "non_obj_loss_cost=",
                      "{:.9f}".format(nol))

                # print("Epoch:", '%04d' % (epoch + 1), "total_loss=", "{:.9f}".format(ct), "obj_loss=", "{:.9f}".format(co), "coord_loss=", "{:.9f}".format(cc))

            print("Save epoch " + str(epoch + 1))
            checkpoint_name = model_prefix + ("%sx%sx%s_" % voxel_shape) + (
                "res%s_" %
                resolution) + ("sc%s" % scale) + "_" + str(epoch + 1) + ".ckpt"
            saver.save(sess, checkpoint_name)

        print("Training Complete!")
Exemplo n.º 3
0
test_size = len(X_test)
epochs = 500
batch_size = 128
boundaries = [int(.5 * epochs * train_size / batch_size),
              int(.75 * epochs * train_size / batch_size)]
values = [.1, .01, .001]
weight_decay = 1e-4
momentum = 0.9
shuffle_indices = np.arange(train_size)

labels = tf.one_hot(y_inputs, 10)

out = architecture(X_inputs, random_rolls, is_training, strategy='pad',
                   scope='stoch_depth', P=0.5, L=54)

loss = loss_function(logits=out, labels=labels, weight_decay=weight_decay,
                     arch_scope='stoch_depth')
training_summary = tf.summary.scalar('train_loss', loss)

global_step = tf.Variable(0, trainable=False)
learning_rate = tf.train.piecewise_constant(global_step, boundaries, values)
step = tf.train.MomentumOptimizer(learning_rate,
                                  momentum,
                                  use_nesterov=True).minimize(loss,
                                                              global_step=global_step)
pred = tf.argmax(out, axis=1)
error_num = tf.count_nonzero(pred - y_test_tf, name='error_num')
test_summary = tf.summary.scalar('Test_Error', test_error_tf)


# # Create unique log file name