Exemplo n.º 1
0
def train_model(params):
    """The function gets the training data from the training folder,
    the evaluation data from the eval folder and trains your solution
    from the model.py file with it.

    Parameters:
        params: parameters for training the model
    """
    (train_data, train_labels) = data.create_data_with_labels("data/train/")
    (eval_data, eval_labels) = data.create_data_with_labels("data/eval/")

    img_shape = train_data.shape[1:]
    input_layer = tf.keras.Input(shape=img_shape, name='input_image')

    ml_model = model.solution(input_layer)

    if ml_model is None:
        print("No model found. You need to implement one in model.py")
    else:
        ml_model.fit(train_data,
                     train_labels,
                     batch_size=model.get_batch_size(),
                     epochs=model.get_epochs(),
                     validation_data=(eval_data, eval_labels))

        ml_model.evaluate(eval_data, eval_labels, verbose=1)
        print(ml_model.history)
Exemplo n.º 2
0
def train_model(params):
    """The function gets the training data from the training folder,
    the evaluation data from the test folder and trains the CNN from the model.py file with it."""
    (train_data, train_labels) = data.create_data_with_labels("data/train/")

    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": train_data},
        y=train_labels,
        batch_size=model.get_batch_size(),
        num_epochs=None,
        shuffle=True)

    (eval_data, eval_labels) = data.create_data_with_labels("data/test/")

    eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": eval_data},
                                                       y=eval_labels,
                                                       num_epochs=1,
                                                       shuffle=False)

    estimator = tf.estimator.Estimator(model_fn=model.cnn)

    steps_per_eval = int(model.get_training_steps() / params.eval_steps)

    for _ in range(params.eval_steps):
        estimator.train(train_input_fn, steps=steps_per_eval)
        estimator.evaluate(eval_input_fn)
Exemplo n.º 3
0
def train_model(params):
    """The function gets the training data from the training folder and the test folder.
    The CNN from the model.py file is trained with this training data.
    The evaluation in this method is not important since all data was already used to train."""
    (train_data, train_labels) = data.create_data_with_labels("data/train/")
    (eval_data, eval_labels) = data.create_data_with_labels("data/test/")

    train_data = np.append(train_data, eval_data, axis=0)
    train_labels = np.append(train_labels, eval_labels, axis=0)

    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": train_data},
        y=train_labels,
        batch_size=model.get_batch_size(),
        num_epochs=None,
        shuffle=True)

    eval_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": eval_data},
        y=eval_labels,
        num_epochs=1,
        shuffle=False)

    exporter = tf.estimator.FinalExporter('exported', SERVING_FUNCTIONS[params.export_format])
    train_spec = tf.estimator.TrainSpec(train_input_fn, max_steps=model.get_training_steps())
    eval_spec = tf.estimator.EvalSpec(eval_input_fn,
                                      steps=params.eval_steps,
                                      exporters=[exporter],
                                      name='exported_eval')

    run_config = tf.estimator.RunConfig(session_config=_get_session_config_from_env_var())
    run_config = run_config.replace(model_dir=params.job_dir)

    estimator = tf.estimator.Estimator(model_fn=model.cnn, config=run_config)
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
Exemplo n.º 4
0
def train_and_export_model(params):
    """The function gets the training data from the training folder and
    the eval folder.
    Your solution in the model.py file is trained with this training data.
    The evaluation in this method is not important since all data was already
    used to train.

    Parameters:
        params: Parameters for training and exporting the model
    """
    (train_data, train_labels) = data.create_data_with_labels("data/train/")
    (eval_data, eval_labels) = data.create_data_with_labels("data/eval/")

    train_data = np.append(train_data, eval_data, axis=0)
    train_labels = np.append(train_labels, eval_labels, axis=0)

    img_shape = train_data.shape[1:]
    input_layer = tf.keras.Input(shape=img_shape, name='input_image')

    ml_model = model.solution(input_layer)

    ml_model.fit(train_data,
                 train_labels,
                 batch_size=model.get_batch_size(),
                 epochs=model.get_epochs())
    export_model(ml_model, export_dir=params.job_dir)
Exemplo n.º 5
0
def train_model(params):
    """The function gets the training data from the training folder,
    the evaluation data from the test folder and trains the CNN from the model.py file with it."""
    (train_data, train_labels) = data.create_data_with_labels("data/train/")

    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": train_data},
        y=train_labels,
        batch_size=model.get_batch_size(),
        num_epochs=None,
        shuffle=True)

    (eval_data, eval_labels) = data.create_data_with_labels("data/test/")

    eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": eval_data},
                                                       y=eval_labels,
                                                       num_epochs=1,
                                                       shuffle=False)

    estimator = tf.estimator.Estimator(model_fn=model.cnn)

    steps_per_eval = int(model.get_training_steps() / params.eval_steps)

    res = []

    for _ in range(params.eval_steps):
        estimator.train(train_input_fn, steps=steps_per_eval)
        res.append(estimator.evaluate(eval_input_fn))

    f1 = plt.figure(1)
    plt.plot(list(item["loss"] for item in res))
    plt.ylabel('loss')
    f1.show()

    f2 = plt.figure(2)
    plt.plot(list(item["accuracy"] for item in res))
    plt.ylabel('accuracy')
    f2.show()

    raw_input('Press enter to continue: ')