import numpy as np
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import tensorflow as tf
import random
import string

def name_generator(size=8, chars=string.ascii_uppercase + string.digits, extension=''):
    return ''.join(random.choice(chars) for _ in range(size)) + extension




def next_batch(num, data, labels, start, finish):
    '''
    Return a total of `num` random samples and labels.
    '''
    # np.random.shuffle(idx)
    d = data[start:finish, :]
    l = labels[start:finish, :]

    return np.asarray(d), np.asarray(l)

def plot_images(images, cls_true, cls_pred=None):
    assert len(images) == len(cls_true) == 9

    # Create figure with 3x3 sub-plots.
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)

    for i, ax in enumerate(axes.flat):
        # Plot image.
        ax.imshow(images[i].reshape(32, 64, 3), cmap='binary')

        # Show true and predicted classes.
        if cls_pred is None:
            xlabel = "True: {0}".format(cls_true[i])
        else:
            xlabel = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i])

        # Show the classes as the label on the x-axis.
        ax.set_xlabel(xlabel)

        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()

def plot_example_errors(cls_pred, correct, x_val, y_val):
    # This function is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # correct is a boolean array whether the predicted class
    # is equal to the true class for each image in the test-set.

    # Negate the boolean array.
    incorrect = (correct == False)

    # Get the images from the test-set that have been
    # incorrectly classified.
    images = x_val[incorrect]

    # Get the predicted classes for those images.
    cls_pred = cls_pred[incorrect]

    # Get the true classes for those images.
    cls_true = y_val[incorrect]

    # Plot the first 9 images.
    plot_images(images=images[0:9],
                cls_true=cls_true[0:9],
                cls_pred=cls_pred[0:9])


def plot_confusion_matrix(cls_pred, y_val):
    # This is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # Get the true classifications for the test-set.
    cls_true = y_val

    # Get the confusion matrix using sklearn.
    cm = confusion_matrix(y_true=cls_true,
                          y_pred=cls_pred)

    # Print the confusion matrix as text.
    print(cm)

    # Plot the confusion matrix as an image.
    plt.matshow(cm)

    # Make various adjustments to the plot.
    plt.colorbar()
    tick_marks = np.arange(10)  # 10's are number of classes
    plt.xticks(tick_marks, range(10))
    plt.yticks(tick_marks, range(10))
    plt.xlabel('Predicted')
    plt.ylabel('True')

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()


def print_test_accuracy(batch_size, x_val, y_val, y_val_oh,
                        x, y_true, y_pred_cls, sess,
                        show_example_errors=False, show_confusion_matrix=False):
    # Number of images in the test-set.
    num_test = len(x_val)

    # Allocate an array for the predicted classes which
    # will be calculated in batches and filled into this array.
    cls_pred = np.zeros(shape=num_test, dtype=np.int)

    # Now calculate the predicted classes for the batches.
    # We will just iterate through all the batches.
    # There might be a more clever and Pythonic way of doing this.

    # The starting index for the next batch is denoted i.
    i = 0

    while i < num_test:
        # The ending index for the next batch is denoted j.
        j = min(i + batch_size, num_test) #

        # Get the images from the test-set between index i and j.
        images = x_val[i:j, :]

        # Get the associated labels.
        labels = y_val_oh[i:j, :]

        # Create a feed-dict with these images and labels.
        feed_dict = {x: images,
                     y_true: labels}

        # Calculate the predicted class using TensorFlow.
        cls_pred[i:j] = sess.run(y_pred_cls, feed_dict=feed_dict)

        # Set the start-index for the next batch to the
        # end-index of the current batch.
        i = j

    # Convenience variable for the true class-numbers of the test-set.
    cls_true = y_val

    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    correct_sum = correct.sum()

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})".format(acc, correct_sum, num_test)
    #
    # Plot some examples of mis-classifications, if desired.
    if show_example_errors:
        print("Example errors:")
        plot_example_errors(cls_pred=cls_pred, correct=correct)

    # Plot the confusion matrix, if desired.
    if show_confusion_matrix:
        print("Confusion Matrix:")
        plot_confusion_matrix(cls_pred=cls_pred)

    return acc, msg

097ab447e970afb906a7ea906221541bdc8c5291
<mask token>


def next_batch(num, data, labels, start, finish):
    """
    Return a total of `num` random samples and labels.
    """
    d = data[start:finish, :]
    l = labels[start:finish, :]
    return np.asarray(d), np.asarray(l)


<mask token>


def plot_example_errors(cls_pred, correct, x_val, y_val):
    incorrect = correct == False
    images = x_val[incorrect]
    cls_pred = cls_pred[incorrect]
    cls_true = y_val[incorrect]
    plot_images(images=images[0:9], cls_true=cls_true[0:9], cls_pred=
        cls_pred[0:9])


<mask token>


def print_test_accuracy(batch_size, x_val, y_val, y_val_oh, x, y_true,
    y_pred_cls, sess, show_example_errors=False, show_confusion_matrix=False):
    num_test = len(x_val)
    cls_pred = np.zeros(shape=num_test, dtype=np.int)
    i = 0
    while i < num_test:
        j = min(i + batch_size, num_test)
        images = x_val[i:j, :]
        labels = y_val_oh[i:j, :]
        feed_dict = {x: images, y_true: labels}
        cls_pred[i:j] = sess.run(y_pred_cls, feed_dict=feed_dict)
        i = j
    cls_true = y_val
    correct = cls_true == cls_pred
    correct_sum = correct.sum()
    acc = float(correct_sum) / num_test
    msg = 'Accuracy on Test-Set: {0:.1%} ({1} / {2})'.format(acc,
        correct_sum, num_test)
    if show_example_errors:
        print('Example errors:')
        plot_example_errors(cls_pred=cls_pred, correct=correct)
    if show_confusion_matrix:
        print('Confusion Matrix:')
        plot_confusion_matrix(cls_pred=cls_pred)
    return acc, msg

<mask token>


def next_batch(num, data, labels, start, finish):
    """
    Return a total of `num` random samples and labels.
    """
    d = data[start:finish, :]
    l = labels[start:finish, :]
    return np.asarray(d), np.asarray(l)


def plot_images(images, cls_true, cls_pred=None):
    assert len(images) == len(cls_true) == 9
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)
    for i, ax in enumerate(axes.flat):
        ax.imshow(images[i].reshape(32, 64, 3), cmap='binary')
        if cls_pred is None:
            xlabel = 'True: {0}'.format(cls_true[i])
        else:
            xlabel = 'True: {0}, Pred: {1}'.format(cls_true[i], cls_pred[i])
        ax.set_xlabel(xlabel)
        ax.set_xticks([])
        ax.set_yticks([])
    plt.show()


def plot_example_errors(cls_pred, correct, x_val, y_val):
    incorrect = correct == False
    images = x_val[incorrect]
    cls_pred = cls_pred[incorrect]
    cls_true = y_val[incorrect]
    plot_images(images=images[0:9], cls_true=cls_true[0:9], cls_pred=
        cls_pred[0:9])


def plot_confusion_matrix(cls_pred, y_val):
    cls_true = y_val
    cm = confusion_matrix(y_true=cls_true, y_pred=cls_pred)
    print(cm)
    plt.matshow(cm)
    plt.colorbar()
    tick_marks = np.arange(10)
    plt.xticks(tick_marks, range(10))
    plt.yticks(tick_marks, range(10))
    plt.xlabel('Predicted')
    plt.ylabel('True')
    plt.show()


def print_test_accuracy(batch_size, x_val, y_val, y_val_oh, x, y_true,
    y_pred_cls, sess, show_example_errors=False, show_confusion_matrix=False):
    num_test = len(x_val)
    cls_pred = np.zeros(shape=num_test, dtype=np.int)
    i = 0
    while i < num_test:
        j = min(i + batch_size, num_test)
        images = x_val[i:j, :]
        labels = y_val_oh[i:j, :]
        feed_dict = {x: images, y_true: labels}
        cls_pred[i:j] = sess.run(y_pred_cls, feed_dict=feed_dict)
        i = j
    cls_true = y_val
    correct = cls_true == cls_pred
    correct_sum = correct.sum()
    acc = float(correct_sum) / num_test
    msg = 'Accuracy on Test-Set: {0:.1%} ({1} / {2})'.format(acc,
        correct_sum, num_test)
    if show_example_errors:
        print('Example errors:')
        plot_example_errors(cls_pred=cls_pred, correct=correct)
    if show_confusion_matrix:
        print('Confusion Matrix:')
        plot_confusion_matrix(cls_pred=cls_pred)
    return acc, msg

<mask token>


def name_generator(size=8, chars=string.ascii_uppercase + string.digits,
    extension=''):
    return ''.join(random.choice(chars) for _ in range(size)) + extension


def next_batch(num, data, labels, start, finish):
    """
    Return a total of `num` random samples and labels.
    """
    d = data[start:finish, :]
    l = labels[start:finish, :]
    return np.asarray(d), np.asarray(l)


def plot_images(images, cls_true, cls_pred=None):
    assert len(images) == len(cls_true) == 9
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)
    for i, ax in enumerate(axes.flat):
        ax.imshow(images[i].reshape(32, 64, 3), cmap='binary')
        if cls_pred is None:
            xlabel = 'True: {0}'.format(cls_true[i])
        else:
            xlabel = 'True: {0}, Pred: {1}'.format(cls_true[i], cls_pred[i])
        ax.set_xlabel(xlabel)
        ax.set_xticks([])
        ax.set_yticks([])
    plt.show()


def plot_example_errors(cls_pred, correct, x_val, y_val):
    incorrect = correct == False
    images = x_val[incorrect]
    cls_pred = cls_pred[incorrect]
    cls_true = y_val[incorrect]
    plot_images(images=images[0:9], cls_true=cls_true[0:9], cls_pred=
        cls_pred[0:9])


def plot_confusion_matrix(cls_pred, y_val):
    cls_true = y_val
    cm = confusion_matrix(y_true=cls_true, y_pred=cls_pred)
    print(cm)
    plt.matshow(cm)
    plt.colorbar()
    tick_marks = np.arange(10)
    plt.xticks(tick_marks, range(10))
    plt.yticks(tick_marks, range(10))
    plt.xlabel('Predicted')
    plt.ylabel('True')
    plt.show()


def print_test_accuracy(batch_size, x_val, y_val, y_val_oh, x, y_true,
    y_pred_cls, sess, show_example_errors=False, show_confusion_matrix=False):
    num_test = len(x_val)
    cls_pred = np.zeros(shape=num_test, dtype=np.int)
    i = 0
    while i < num_test:
        j = min(i + batch_size, num_test)
        images = x_val[i:j, :]
        labels = y_val_oh[i:j, :]
        feed_dict = {x: images, y_true: labels}
        cls_pred[i:j] = sess.run(y_pred_cls, feed_dict=feed_dict)
        i = j
    cls_true = y_val
    correct = cls_true == cls_pred
    correct_sum = correct.sum()
    acc = float(correct_sum) / num_test
    msg = 'Accuracy on Test-Set: {0:.1%} ({1} / {2})'.format(acc,
        correct_sum, num_test)
    if show_example_errors:
        print('Example errors:')
        plot_example_errors(cls_pred=cls_pred, correct=correct)
    if show_confusion_matrix:
        print('Confusion Matrix:')
        plot_confusion_matrix(cls_pred=cls_pred)
    return acc, msg

import numpy as np
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import tensorflow as tf
import random
import string


def name_generator(size=8, chars=string.ascii_uppercase + string.digits,
    extension=''):
    return ''.join(random.choice(chars) for _ in range(size)) + extension


def next_batch(num, data, labels, start, finish):
    """
    Return a total of `num` random samples and labels.
    """
    d = data[start:finish, :]
    l = labels[start:finish, :]
    return np.asarray(d), np.asarray(l)


def plot_images(images, cls_true, cls_pred=None):
    assert len(images) == len(cls_true) == 9
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)
    for i, ax in enumerate(axes.flat):
        ax.imshow(images[i].reshape(32, 64, 3), cmap='binary')
        if cls_pred is None:
            xlabel = 'True: {0}'.format(cls_true[i])
        else:
            xlabel = 'True: {0}, Pred: {1}'.format(cls_true[i], cls_pred[i])
        ax.set_xlabel(xlabel)
        ax.set_xticks([])
        ax.set_yticks([])
    plt.show()


def plot_example_errors(cls_pred, correct, x_val, y_val):
    incorrect = correct == False
    images = x_val[incorrect]
    cls_pred = cls_pred[incorrect]
    cls_true = y_val[incorrect]
    plot_images(images=images[0:9], cls_true=cls_true[0:9], cls_pred=
        cls_pred[0:9])


def plot_confusion_matrix(cls_pred, y_val):
    cls_true = y_val
    cm = confusion_matrix(y_true=cls_true, y_pred=cls_pred)
    print(cm)
    plt.matshow(cm)
    plt.colorbar()
    tick_marks = np.arange(10)
    plt.xticks(tick_marks, range(10))
    plt.yticks(tick_marks, range(10))
    plt.xlabel('Predicted')
    plt.ylabel('True')
    plt.show()


def print_test_accuracy(batch_size, x_val, y_val, y_val_oh, x, y_true,
    y_pred_cls, sess, show_example_errors=False, show_confusion_matrix=False):
    num_test = len(x_val)
    cls_pred = np.zeros(shape=num_test, dtype=np.int)
    i = 0
    while i < num_test:
        j = min(i + batch_size, num_test)
        images = x_val[i:j, :]
        labels = y_val_oh[i:j, :]
        feed_dict = {x: images, y_true: labels}
        cls_pred[i:j] = sess.run(y_pred_cls, feed_dict=feed_dict)
        i = j
    cls_true = y_val
    correct = cls_true == cls_pred
    correct_sum = correct.sum()
    acc = float(correct_sum) / num_test
    msg = 'Accuracy on Test-Set: {0:.1%} ({1} / {2})'.format(acc,
        correct_sum, num_test)
    if show_example_errors:
        print('Example errors:')
        plot_example_errors(cls_pred=cls_pred, correct=correct)
    if show_confusion_matrix:
        print('Confusion Matrix:')
        plot_confusion_matrix(cls_pred=cls_pred)
    return acc, msg

import numpy as np
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import tensorflow as tf
import random
import string

def name_generator(size=8, chars=string.ascii_uppercase + string.digits, extension=''):
    return ''.join(random.choice(chars) for _ in range(size)) + extension




def next_batch(num, data, labels, start, finish):
    '''
    Return a total of `num` random samples and labels.
    '''
    # np.random.shuffle(idx)
    d = data[start:finish, :]
    l = labels[start:finish, :]

    return np.asarray(d), np.asarray(l)

def plot_images(images, cls_true, cls_pred=None):
    assert len(images) == len(cls_true) == 9

    # Create figure with 3x3 sub-plots.
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)

    for i, ax in enumerate(axes.flat):
        # Plot image.
        ax.imshow(images[i].reshape(32, 64, 3), cmap='binary')

        # Show true and predicted classes.
        if cls_pred is None:
            xlabel = "True: {0}".format(cls_true[i])
        else:
            xlabel = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i])

        # Show the classes as the label on the x-axis.
        ax.set_xlabel(xlabel)

        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()

def plot_example_errors(cls_pred, correct, x_val, y_val):
    # This function is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # correct is a boolean array whether the predicted class
    # is equal to the true class for each image in the test-set.

    # Negate the boolean array.
    incorrect = (correct == False)

    # Get the images from the test-set that have been
    # incorrectly classified.
    images = x_val[incorrect]

    # Get the predicted classes for those images.
    cls_pred = cls_pred[incorrect]

    # Get the true classes for those images.
    cls_true = y_val[incorrect]

    # Plot the first 9 images.
    plot_images(images=images[0:9],
                cls_true=cls_true[0:9],
                cls_pred=cls_pred[0:9])


def plot_confusion_matrix(cls_pred, y_val):
    # This is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # Get the true classifications for the test-set.
    cls_true = y_val

    # Get the confusion matrix using sklearn.
    cm = confusion_matrix(y_true=cls_true,
                          y_pred=cls_pred)

    # Print the confusion matrix as text.
    print(cm)

    # Plot the confusion matrix as an image.
    plt.matshow(cm)

    # Make various adjustments to the plot.
    plt.colorbar()
    tick_marks = np.arange(10)  # 10's are number of classes
    plt.xticks(tick_marks, range(10))
    plt.yticks(tick_marks, range(10))
    plt.xlabel('Predicted')
    plt.ylabel('True')

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()


def print_test_accuracy(batch_size, x_val, y_val, y_val_oh,
                        x, y_true, y_pred_cls, sess,
                        show_example_errors=False, show_confusion_matrix=False):
    # Number of images in the test-set.
    num_test = len(x_val)

    # Allocate an array for the predicted classes which
    # will be calculated in batches and filled into this array.
    cls_pred = np.zeros(shape=num_test, dtype=np.int)

    # Now calculate the predicted classes for the batches.
    # We will just iterate through all the batches.
    # There might be a more clever and Pythonic way of doing this.

    # The starting index for the next batch is denoted i.
    i = 0

    while i < num_test:
        # The ending index for the next batch is denoted j.
        j = min(i + batch_size, num_test) #

        # Get the images from the test-set between index i and j.
        images = x_val[i:j, :]

        # Get the associated labels.
        labels = y_val_oh[i:j, :]

        # Create a feed-dict with these images and labels.
        feed_dict = {x: images,
                     y_true: labels}

        # Calculate the predicted class using TensorFlow.
        cls_pred[i:j] = sess.run(y_pred_cls, feed_dict=feed_dict)

        # Set the start-index for the next batch to the
        # end-index of the current batch.
        i = j

    # Convenience variable for the true class-numbers of the test-set.
    cls_true = y_val

    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    correct_sum = correct.sum()

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})".format(acc, correct_sum, num_test)
    #
    # Plot some examples of mis-classifications, if desired.
    if show_example_errors:
        print("Example errors:")
        plot_example_errors(cls_pred=cls_pred, correct=correct)

    # Plot the confusion matrix, if desired.
    if show_confusion_matrix:
        print("Confusion Matrix:")
        plot_confusion_matrix(cls_pred=cls_pred)

    return acc, msg
