def optmizers_search(name_tfrecords,
                     records,
                     height,
                     width,
                     channels,
                     architecture,
                     activations,
                     conv_architecture,
                     kernel_sizes,
                     pool_kernel,
                     batch_size,
                     epochs,
                     num_steps,
                     save_step,
                     learning_rate,
                     conv):
    """
    Script to run optmizers search,
    the result is saved on the file optmizers_results.txt

    :param name_tfrecords: name of the used tfrecords
    :type name_tfrecords: str
    :param records: list of paths to train, test, and valid tfrecords
    :type records: list of str
    :param height: image height
    :type heights: int
    :param width: image width
    :type width: int
    :param channels: image channels
    :type channels: int
    :param architecture: network architecture
    :type architecture: list of int
    :param activations: list of different tf functions
    :type activations: list of tf.nn.sigmoid, tf.nn.relu, tf.nn.tanh
    :param conv_architecture: convolutional architecture
    :type conv_architecture: list of int
    :param kernel_sizes: filter sizes
    :type kernel_sizes: list of int
    :param pool_kernel: pooling filter sizes
    :type pool_kernel: list of int
    :param batch_size: batch size for training
    :type batch_size: int
    :param epochs: number of epochs
    :type epochs: int
    :param num_steps: number of iterations for each epoch
    :type num_steps: int
    :param save_step: when step % save_step == 0, the model
                      parameters are saved.
    :type save_step: int
    :param learning_rate: learning rate for the optimizer
    :type learning_rate: float
    :param conv: param to control if the model will be a CNN
                 or DFN
    :type conv: bool
    """
    OT = [tf.train.GradientDescentOptimizer,
          tf.train.AdadeltaOptimizer,
          tf.train.AdagradOptimizer,
          tf.train.AdamOptimizer,
          tf.train.FtrlOptimizer,
          tf.train.ProximalGradientDescentOptimizer,
          tf.train.ProximalAdagradOptimizer,
          tf.train.RMSPropOptimizer]

    OT_name = ["GradientDescentOptimizer",
               "AdadeltaOptimizer",
               "AdagradOptimizer",
               "AdamOptimizer",
               "FtrlOptimizer",
               "ProximalGradientDescentOptimizer",
               "ProximalAdagradOptimizer",
               "RMSPropOptimizer"]
    numeric_result = []
    results = []
    info = []
    if conv:
        net_name = "CNN"
    else:
        net_name = "DFN"

    header = "\nSearching optimizer for the {} model in the {} data\n".format(net_name,  # noqa
                                                                              name_tfrecords)  # noqa
    print(header)

    for name, opt in zip(OT_name, OT):
        config = Config(height=height,
                        width=width,
                        channels=channels,
                        architecture=architecture,
                        activations=activations,
                        conv_architecture=conv_architecture,
                        kernel_sizes=kernel_sizes,
                        pool_kernel=pool_kernel,
                        batch_size=batch_size,
                        epochs=epochs,
                        num_steps=num_steps,
                        save_step=save_step,
                        learning_rate=learning_rate,
                        optimizer=opt)
        data = DataHolder(config,
                          records=records)
        print(name + ":\n")
        graph = tf.Graph()
        if conv:
            network = CNN(graph, config)
        else:
            network = DFN(graph, config)
        trainer = Trainer(graph, config, network, data)
        trainer.fit(verbose=True)
        valid_acc = trainer.get_valid_accuracy()
        numeric_result.append(valid_acc)
        name += ': valid_acc = {0:.6f} | '.format(valid_acc)
        test_images, test_labels, _ = reconstruct_from_record(data.get_test_tfrecord())  # noqa
        test_images = test_images.astype(np.float32) / 255
        test_pred = trainer.predict(test_images)
        acc_cat = accuracy_per_category(test_pred, test_labels, categories=3)
        for i, cat_result in enumerate(acc_cat):
            name += int2command[i] + ": = {0:.6f}, ".format(cat_result)
        results.append(name)
        if os.path.exists("checkpoints"):
            shutil.rmtree("checkpoints")
        info.append(str(config))

    best_result = max(list(zip(numeric_result, OT_name, info)))
    result_string = """In an experiment with different optmizers
    the best one is {0} with valid accuracy of {1}.
    \nThe training uses the following params:
    \n{2}\n""".format(best_result[1],
                      best_result[0],
                      best_result[2])
    file = open("optmizers_results.txt", "w")
    file.write(header)
    file.write("Results for different optmizers\n")
    for result in results:
        result += "\n"
        file.write(result)
    file.write("\n")
    file.write(result_string)
    file.close()
예제 #2
0
def acc(name_tfrecords,
        records,
        height,
        width,
        channels,
        architecture,
        activations,
        conv_architecture,
        kernel_sizes,
        pool_kernel,
        test,
        name,
        conv):
    """
    Checks model's accuracy

    :param name_tfrecords: name of the used tfrecords
    :type name_tfrecords: str
    :param records: list of paths to train, test, and valid tfrecords
    :type records: list of str
    :param height: image height
    :type heights: int
    :param width: image width
    :type width: int
    :param channels: image channels
    :type channels: int
    :param architecture: network architecture
    :type architecture: list of int
    :param activations: list of different tf functions
    :type activations: list of tf.nn.sigmoid, tf.nn.relu, tf.nn.tanh
    :param conv_architecture: convolutional architecture
    :type conv_architecture: list of int
    :param kernel_sizes: filter sizes
    :type kernel_sizes: list of int
    :param pool_kernel: pooling filter sizes
    :type pool_kernel: list of int
    :param test: param to control if the test accuracy will be printed.
    :type test: bool
    :param name: name to save the confusion matrix plot.
    :type name: str
    :param conv: param to control if the model will be a CNN
                 or DFN
    :type conv: bool
    """

    config = Config(height=height,
                    width=width,
                    channels=channels,
                    architecture=architecture,
                    activations=activations,
                    conv_architecture=conv_architecture,
                    kernel_sizes=kernel_sizes,
                    pool_kernel=pool_kernel)

    data = DataHolder(config,
                      records=records)

    graph = tf.Graph()
    if conv:
        net_name = "CNN"
        network = CNN(graph, config)
    else:
        net_name = "DFN"
        network = DFN(graph, config)
    trainer = Trainer(graph, config, network, data)
    print("\nAccuracy of the {} model in the {} data\n".format(net_name,
                                                               name_tfrecords))
    print("params:\n{}\n".format(str(config)))
    if not os.path.exists("checkpoints"):
        print("===Accuracy of a non trained model===")

    valid_images, valid_labels, _ = reconstruct_from_record(data.get_valid_tfrecord(), bound=10000)  # noqa
    valid_images = valid_images.astype(np.float32) / 255
    valid_pred = trainer.predict(valid_images)
    valid_labels = valid_labels.reshape((valid_labels.shape[0],))
    plotconfusion(valid_labels,
                  valid_pred,
                  name + "_valid.png",
                  int2command,
                  classes=["left", "right", "up"])

    if test:
        test_images, test_labels, _ = reconstruct_from_record(data.get_test_tfrecord(), bound=10000)  # noqa
        test_images = test_images.astype(np.float32) / 255
        test_pred = trainer.predict(test_images)
        test_labels = test_labels.reshape((test_labels.shape[0],))
        plotconfusion(test_labels,
                      test_pred,
                      name + "_test.png",
                      int2command,
                      classes=["left", "right", "up"])