Beispiel #1
0
def main():
    """CapsNet run as module.

    Run full cycle when CapsNet is run as a module.
    """
    people = fetch_lfw_people(
        color=True,
        min_faces_per_person=25,
        # resize=1.,
        # slice_=(slice(48, 202), slice(48, 202))
    )

    data = preprocess(people)

    (x_train, y_train), (x_test, y_test) = data  # noqa

    model = CapsNet(x_train.shape[1:], len(np.unique(y_train, axis=0)))

    model.summary()

    # Start TensorBoard
    tensorboard = callbacks.TensorBoard('model/tensorboard_logs',
                                        batch_size=10,
                                        histogram_freq=1,
                                        write_graph=True,
                                        write_grads=True,
                                        write_images=True)
    model.train(data, batch_size=10, extra_callbacks=[tensorboard])
    model.save('/tmp')

    metrics = model.test(x_test, y_test)
    pprint(metrics)
Beispiel #2
0
def main(not_parsed_args):
    # we use a margin loss
    model = CapsNet()
    last_epoch = load_weights(model)
    model.compile(loss=margin_loss, optimizer=optimizers.Adam(FLAGS.lr), metrics=['accuracy'])
    model.summary()

    dataset = Dataset(FLAGS.dataset, FLAGS.batch_size)
    tensorboard = TensorBoard(log_dir='./tf_logs', batch_size=FLAGS.batch_size, write_graph=False, write_grads=True, write_images=True, update_freq='batch')
    tensorboard.set_model(model)

    for epoch in range(last_epoch, FLAGS.epochs):
        logging.info('Epoch %d' % epoch)
        model.fit_generator(generator=dataset,
            epochs=1,
            steps_per_epoch=len(dataset),
            verbose=1,
            validation_data=dataset.eval_dataset,
            validation_steps=len(dataset.eval_dataset))

        logging.info('Saving model')
        filename = 'model_%d.h5' % (epoch)
        path = os.path.join(FLAGS.model_dir, filename)
        path_info = os.path.join(FLAGS.model_dir, 'info')
        model.save_weights(path)
        f = open(path_info, 'w')
        f.write(filename)
        f.close()