Esempio n. 1
0
    def train(self, target_input_values, target_output_values):

        l_index = len(self.layers) - 1
        print(target_input_values)
        print(target_output_values)
        self.set_target_values(target_output_values, l_index)
        self.get_values(target_input_values)
        self.process_error()
        for layer in self.layers:
            layer.train()
Esempio n. 2
0
    def train(self, training_set):
        l_index = len(self.layers) - 1

        q_training = training_set.training_iterations
        training_iter = 0
        while training_iter < q_training:
            training_iter += 1

            self.set_target_values(training_set.output, l_index)
            self.get_values(training_set.input)
            self.process_error()
            for layer in self.layers:
                layer.train()
Esempio n. 3
0
def main(args=None):
    z = tf.placeholder(tf.float32, [flags.batch_size, flags.noise_size], name="z")
    image = tf.placeholder(tf.float32, [flags.batch_size, flags.image_height, flags.image_width, 3])
    label = tf.placeholder(tf.uint8, [flags.batch_size, 1])

    source_logits_real, class_logits_real, source_logits_fake, class_logits_fake, generate_image = layer.inference(
        image, label, z)

    d_loss, g_loss = layer.loss(label,
                                source_logits_real=source_logits_real, class_logits_real=class_logits_real,
                                source_logits_fake=source_logits_fake, class_logits_fake=class_logits_fake
                                )
    d_optimizer, g_optimazer = layer.train(d_loss, g_loss)

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    reader = read_records.ReadRecords(train_path=flags.buckets + '*.train.tfrecords',
                                      test_path=flags.buckets + '*.test.tfrecords')
    tf.train.start_queue_runners(sess=sess)
    g_loss_save = tf.summary.scalar('g_loss', g_loss)
    d_loss_save = tf.summary.scalar('d_loss', d_loss)
    saver = tf.train.Saver(var_list=[tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="discriminator"),
                                     tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="generator")],
                           keep_checkpoint_every_n_hours=0.5)
    # saver.restore(sess=sess, save_path=flags.checkpointDir)
    summary = tf.summary.FileWriter(flags.summaryDir)
    for step in xrange(flags.train_steps):
        random_z = np.random.uniform(-1, 1, size=[flags.batch_size, flags.noise_size]).astype(np.float32)
        image_read, label_read = sess.run(reader.read())
        sess.run([d_optimizer, g_optimazer, g_optimazer], feed_dict={z: random_z, image: image_read, label: label_read})

        if step % 50 == 0:
            image_save = tf.summary.image('generate_image', generate_image, max_outputs=50)
            merged = tf.summary.merge([image_save, g_loss_save, d_loss_save])
        else:
            merged = tf.summary.merge([g_loss_save, d_loss_save])
        merged = sess.run(merged, feed_dict={z: random_z, image: image_read, label: label_read})
        summary.add_summary(merged, step)
        print "train steep: {}".format(step)
        if step % 1000 == 0:
            saver.save(sess=sess, save_path=flags.checkpointDir, global_step=step)
def train(model_checkpoint_path = 'log/model_dump/model_fix_input_bn_ema.ckpt',
          has_bn=True, qweight=False, qactivation=False, image_norm=False):
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False, name='global_step')
        learning_rate = tf.placeholder(dtype=tf.float32)

        with tf.device('/cpu:0'):
            train_iter = dataset.make_train_dataset()
            image_batch, label_batch = train_iter.get_next()

        print('images:', image_batch.shape, image_batch.dtype)
        # Build inference Graph.
        scale = None
        if qweight or qactivation:
            with open('log/scale', 'rb') as f:
                scale = pickle.load(f)
        logits = net.inference(image_batch, phase_train=True, has_bn=has_bn, image_norm=image_norm,
                               qactivation=qactivation, qweight=qweight, scale=scale)

        # Build the portion of the Graph calculating the losses. Note that we will
        # assemble the total_loss using a custom function below.
        total_loss, softmax_loss, acc = layer.loss(logits, label_batch)
        tf.summary.scalar('total_loss', total_loss)
        tf.summary.scalar('softmax_loss', softmax_loss)
        tf.summary.scalar('acc', acc)

        train_op = layer.train(total_loss, learning_rate, global_step)
        summary_op = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter(cfg.log_path, tf.get_default_graph())

        pre_saver = tf.train.Saver(tf.get_collection('params'))

        # Create a saver.
        saver = tf.train.Saver(max_to_keep=5000)

        init = tf.global_variables_initializer()

        sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                                log_device_placement=False,
                                                gpu_options=tf.GPUOptions(allow_growth=True)))
        sess.run(init)

        if model_checkpoint_path is not None:
            pre_saver.restore(sess, model_checkpoint_path)
            print('init model from {}'.format(model_checkpoint_path))

        best_val_acc = evaluate(model_checkpoint_path, has_bn=True,
                                qweight=True, qactivation=True, image_norm=False)
        if cfg.timeline_log:
            options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
            run_metadata = tf.RunMetadata()
        else:
            options = None
            run_metadata = None
        start_time = time.time()

        train_num_batch = cfg.train_num // cfg.batch_size
        train_log = open('log/work_log.txt', 'w')
        for epoch in range(cfg.max_epoch):
            for step in range(0, train_num_batch):
                lr = get_learning_rate(epoch, step, train_num_batch)
                feed_dict = {learning_rate: lr}
                if step % cfg.log_step == 0:
                    _, _total_loss, _softmax_loss, _acc, _summary = \
                        sess.run([train_op, total_loss, softmax_loss, acc, summary_op],
                                 feed_dict=feed_dict,
                                 options=options,
                                 run_metadata=run_metadata
                                 )
                    duration = float(time.time() - start_time) / cfg.log_step
                    examples_per_sec = cfg.batch_size / duration
                    log_line = "%s: Epoch=%d/%d, Step=%d/%d, lr=%.7f, total_loss=%.3f, softmax_loss=%.3f, " \
                               "acc=%.2f%%(%.1f examples/sec; %.3f sec/batch)" \
                               % (datetime.now().strftime('%m-%d %H:%M:%S'), epoch, cfg.max_epoch,
                                  step, train_num_batch, lr,
                                  _total_loss, _softmax_loss, _acc,
                                  examples_per_sec, duration)
                    train_log.write(log_line + '\n')
                    print(log_line)
                    summary_writer.add_summary(_summary, global_step=step)
                    start_time = time.time()

                    if cfg.timeline_log:
                        tl = timeline.Timeline(run_metadata.step_stats)
                        ctf = tl.generate_chrome_trace_format()
                        with open('timeline.json', 'w') as wd:
                            wd.write(ctf)

                else:
                    _ = sess.run(train_op, feed_dict=feed_dict)

            saver.save(sess, '{}/model.ckpt'.format(cfg.model_path), global_step=epoch)
            _val_acc = evaluate('{}/model.ckpt-{}'.format(cfg.model_path, epoch), has_bn=True,
                                qweight=True, qactivation=True, image_norm=False)

            if _val_acc > best_val_acc:
                pre_saver.save(sess, '{}/best_model.ckpt'.format(cfg.model_path), write_meta_graph=False,
                               write_state=False, global_step=None)
                best_val_acc = _val_acc

            val_log = 'epoch=%d, val_acc=%.3f%%, best_val_acc=%.3f%%' \
                      % (epoch, _val_acc, best_val_acc)
            train_log.write(val_log + '\n')
            print(val_log)
Esempio n. 5
0
        elif y == 0 and x > 0:
            output.append([1, 0, 0, 1])
        elif y == 0 and x < 0:
            output.append([0, 1, 1, 0])
        elif x == 0 and y == 0:
            output.append([0, 0, 0, 0])

input = np.array(input)
output = np.array(output)

layer1 = Layer(3, None, 10, input=input, inputLayer=True)
layer2 = Layer(3, layer1, .1)
layer3 = Layer(3, layer2, .001)
layer4 = Layer(4, layer3, None, outputLayer=True)

network.train(output, iterations=90000)

testCase = np.array([[1, 100, 100]])
print(getClassification(network.getHypothesis(testCase)))
testCase = np.array([[1, -100, 100]])
print(getClassification(network.getHypothesis(testCase)))
testCase = np.array([[1, -100, -100]])
print(getClassification(network.getHypothesis(testCase)))
testCase = np.array([[1, 100, -100]])
print(getClassification(network.getHypothesis(testCase)))

print("   ")

testCase = np.array([[1, 0, 100]])
print(getClassification(network.getHypothesis(testCase)))
testCase = np.array([[1, 0, -100]])
Esempio n. 6
0
f = open('im_data.txt', 'r')

datax = sj.loads(f.read())

f.close()

arr = v.vec_data('test.jpg')

if len(arr) != 0:
    for z in range(15):
        datax.append(arr)

    datay = []
    for z in range(30):
        if z < 15:
            arr = [0]
            datay.append(arr)
        else:
            arr = [1]
            datay.append(arr)

    net = l.init_network(128, 6, 1)
    l.train(net, datax, datay, .1, 1000)
    f = open('shihab_data.txt', 'w')
    sj.dump(net, f)
    f.close()

else:
    print 'no face found'