def visualizeMnist():
    x_train, y_train, _, _ = getMnistData(reshaped=False)
    for i in range(0, 9):
        plt.subplot(330 + 1 + i)
        plt.imshow(x_train[i], cmap=plt.get_cmap('gray'))
        plt.text(0, 0, y_train[i], color='black',
                 bbox=dict(facecolor='white', alpha=1))
    plt.show()
Beispiel #2
0
def get_binary_dataset():
    x_train_orig, y_train_orig, x_test_orig, y_test_orig = getMnistData()
    # preparing training dataset
    index_5 = np.where(y_train_orig == 5)
    index_8 = np.where(y_train_orig == 8)
    index = np.concatenate([index_5[0], index_8[0]])
    np.random.seed(1)
    np.random.shuffle(index)
    y_train = y_train_orig[index]
    x_train = x_train_orig[index]
    y_train[np.where(y_train == 5)] = 0
    y_train[np.where(y_train == 8)] = 1

    # preparing test dataset
    index_5 = np.where(y_test_orig == 5)
    index_8 = np.where(y_test_orig == 8)
    index = np.concatenate([index_5[0], index_8[0]])
    np.random.shuffle(index)
    y_test = y_test_orig[index]
    x_test = x_test_orig[index]
    y_test[np.where(y_test == 5)] = 0
    y_test[np.where(y_test == 8)] = 1

    return x_train, y_train, x_test, y_test
    return x_train, y_train, x_test, y_test


def visualizeMnist():
    x_train, y_train, _, _ = getMnistData(reshaped=False)
    for i in range(0, 9):
        plt.subplot(330 + 1 + i)
        plt.imshow(x_train[i], cmap=plt.get_cmap('gray'))
        plt.text(0, 0, y_train[i], color='black',
                 bbox=dict(facecolor='white', alpha=1))
    plt.show()


if __name__ == '__main__':
    x_train, y_train, x_test, y_test = getMnistData(True)
    x_train, y_train, x_test, y_test = preprocess(
        x_train, y_train, x_test, y_test)
    visualizeMnist()

    x_train = x_train.T
    y_train = y_train.T
    x_test = x_test.T
    y_test = y_test.T

    print(f"x_train: {x_train.shape}")
    print(f"y_train: {y_train.shape}")
    print(f"x_test: {x_test.shape}")
    print(f"y_test: {y_test.shape}")
    layers_dims = [200, 50, 10]
from getdata import getMnistData
from conv import Conv3x3
from maxpool import MaxPool2
from softmax import Softmax
import numpy as np

train_images, train_labels, test_images, test_labels = getMnistData(
    reshaped=False)
train_images = train_images[:3000]
train_labels = train_labels[:3000]
test_images = test_images[:1000]
test_labels = test_labels[:1000]

conv_layer = Conv3x3(num_filters=8)  # 28x28x1 => 26x26x8
pooling_layer = MaxPool2()  # 26x26x8 => 13x13x8
softmax_layer = Softmax(13 * 13 * 8, 10)  # 13x13x8 => 10


def forward(image, label):
    '''
    Completes a forward pass of the CNN and
    calculates the accuracy and cross-entrop loss.
    - image is 2-d numpy array
    - label is a digit
    '''

    # Converting image from [0, 255] => [-0.5, 0.5]

    out = conv_layer.forward((image / 255) - 0.5)
    out = pooling_layer.forward(out)
    out = softmax_layer.forward(out)