Ejemplo n.º 1
0
def main(training_set_ratio):
    arrows = pd.DataFrame(np.zeros((3, 4), dtype=np.int32),
                          index=('hollow', 'full', 'thin'),
                          columns=('down', 'left', 'right', 'up'))

    images = common.get_files(common.SAMPLES_DIR)

    if images:
        for _, filename in images:
            arrow_direction, arrow_type = common.arrow_labels(filename)

            arrows[arrow_direction][arrow_type] += 1

        num_samples = int(arrows.min().min() * training_set_ratio)

        print("Samples per type: {}".format(num_samples * 4))

        for t, _ in arrows.iterrows():
            print("\nProcessing {} arrows...".format(t))

            for d in arrows:
                candidates = [(p, f) for p, f in images
                              if common.arrow_labels(f) == (d, t)]

                print("{}: {}".format(d, len(candidates)))

                training = random.sample(candidates, num_samples)
                for path, filename in training:
                    dst_dir = common.TRAINING_DIR + d + "/"
                    if not os.path.exists(dst_dir):
                        os.makedirs(dst_dir)

                    os.rename(path, dst_dir + filename)

                candidates = [c for c in candidates if c not in training]

                validation = random.sample(
                    candidates, int(len(candidates) * VALIDATION_SET_RATIO))
                for path, filename in validation:
                    dst_dir = common.VALIDATION_DIR + d + "/"
                    if not os.path.exists(dst_dir):
                        os.makedirs(dst_dir)

                    os.rename(path, dst_dir + filename)

                testing = [c for c in candidates if c not in validation]
                for path, filename in testing:
                    dst_dir = common.TESTING_DIR + d + "/"
                    if not os.path.exists(dst_dir):
                        os.makedirs(dst_dir)

                    os.rename(path, dst_dir + filename)

    show_summary()

    print("\nFinished!")
Ejemplo n.º 2
0
def main(training_set_ratio):
    common.create_directories()

    arrows = pd.DataFrame(np.zeros((3, 4), dtype=np.int32),
                          index=('round', 'wide', 'narrow'),
                          columns=('down', 'left', 'right', 'up'))

    images = [(p, f) for p, f in common.get_files(common.SAMPLES_DIR)
              if f[-5] != 'F']

    if images:
        for _, filename in images:
            arrow_direction, arrow_type = common.arrow_labels(filename)

            arrows[arrow_direction][arrow_type] += 1

        num_samples = int(arrows.min().min() * training_set_ratio)

        print("Samples per type: {}".format(num_samples * 4))

        for t, _ in arrows.iterrows():
            print("\nProcessing {} arrows...".format(t))

            for direction in arrows:
                candidates = [(p, f) for p, f in images
                              if common.arrow_labels(f) == (direction, t)]

                print("{}: {}".format(direction, len(candidates)))

                training = random.sample(candidates, num_samples)
                for path, filename in training:
                    dst_dir = common.TRAINING_DIR + direction + '/'
                    os.rename(path, dst_dir + filename)
                    os.rename(flipped(path), dst_dir + flipped(filename))

                candidates = [c for c in candidates if c not in training]

                validation = random.sample(
                    candidates, int(len(candidates) * VALIDATION_SET_RATIO))
                for path, filename in validation:
                    dst_dir = common.VALIDATION_DIR + direction + '/'
                    os.rename(path, dst_dir + filename)
                    os.rename(flipped(path), dst_dir + flipped(filename))

                testing = [c for c in candidates if c not in validation]
                for path, filename in testing:
                    dst_dir = common.TESTING_DIR + direction + '/'
                    os.rename(path, dst_dir + filename)
                    os.rename(flipped(path), dst_dir + flipped(filename))

    show_summary()

    print("\nFinished!")
Ejemplo n.º 3
0
def get_summary_matrix(directory):
    matrix = pd.DataFrame(np.zeros((4, 5), dtype=np.int32),
                          index=('round', 'wide', 'narrow', 'total'),
                          columns=('down', 'left', 'right', 'up', 'total'))

    images = common.get_files(directory)

    for _, filename in images:
        arrow_direction, arrow_type = common.arrow_labels(filename)

        matrix[arrow_direction][arrow_type] += 1

        matrix['total'][arrow_type] += 1
        matrix[arrow_direction]['total'] += 1
        matrix['total']['total'] += 1

    return matrix
Ejemplo n.º 4
0
def show_summary():
    matrix = pd.DataFrame(np.zeros((4, 5), dtype=np.int32), index=(
        'hollow', 'full', 'thin', 'total'), columns=('down', 'left', 'right', 'up', 'total'))

    images = common.get_files(common.SAMPLES_DIR)

    for _, filename in images:
        arrow_direction, arrow_type = common.arrow_labels(filename)

        matrix[arrow_direction][arrow_type] += 1

        matrix['total'][arrow_type] += 1
        matrix[arrow_direction]['total'] += 1
        matrix['total']['total'] += 1

    print(cf.salmon("Samples summary"))
    print(matrix, "\n")
Ejemplo n.º 5
0
def main(src_subdir, verbose, model_name):
    common.create_directories()

    src_dir = common.DATA_DIR + src_subdir + "/"

    model = tensorflow.keras.models.load_model(common.MODEL_DIR + model_name)

    # real (index) x predicted (column)
    confusion_matrix = pd.DataFrame(np.zeros((4, 4), dtype=np.int32),
                                    index=('down', 'left', 'right', 'up'),
                                    columns=('down', 'left', 'right', 'up'))

    classification_matrix = pd.DataFrame(np.zeros((4, 3)),
                                         index=('down', 'left', 'right', 'up'),
                                         columns=('precision', 'recall', 'f1'))

    type_matrix = pd.DataFrame(np.zeros((4, 2), dtype=np.int32),
                               index=('round', 'wide', 'narrow', 'total'),
                               columns=('correct', 'incorrect'))

    images = common.get_files(src_dir)

    print("Processing {} file(s) in {}/...\n".format(len(images), src_subdir))

    for path, filename in images:
        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)

        data = np.reshape(img, (1, ) + common.INPUT_SHAPE)
        prediction = model.predict(data)

        class_index = np.argmax(prediction)
        predicted_class = common.CLASSES[class_index]

        real_class, arrow_type = common.arrow_labels(filename)

        if verbose and real_class != predicted_class:
            print(path)
            print("Expected {} but got {}: {}\n".format(
                cf.lightGreen(real_class), cf.lightCoral(predicted_class),
                str(prediction[0])))

        confusion_matrix[predicted_class][real_class] += 1

        if real_class == predicted_class:
            type_matrix['correct'][arrow_type] += 1
            type_matrix['correct']['total'] += 1
        else:
            type_matrix['incorrect'][arrow_type] += 1
            type_matrix['incorrect']['total'] += 1

    print("\n" + cf.sandyBrown("Confusion matrix"))
    print(confusion_matrix)

    classification_matrix['precision'] = confusion_matrix.apply(precision)
    classification_matrix['recall'] = confusion_matrix.apply(recall, axis=1)

    classification_matrix['f1'] = classification_matrix.apply(f1, axis=1)

    print("\n" + cf.skyBlue("Classification summary"))
    print(classification_matrix)

    type_matrix['accuracy'] = type_matrix.apply(type_accuracy, axis=1)

    print("\n" + cf.plum("Accuracy by type"))
    print(type_matrix)

    print("\nFinished!")