From a77a1be3117b8018fdb10be3e85e258d5f5c53b4 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Wed, 27 Feb 2019 18:53:17 +0000 Subject: Move padding to train/test functions --- lenet.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lenet.py b/lenet.py index 37123bb..93a80cf 100644 --- a/lenet.py +++ b/lenet.py @@ -18,9 +18,6 @@ def import_mnist(): X_train, y_train = mnist.train.images, mnist.train.labels X_validation, y_validation = mnist.validation.images, mnist.validation.labels X_test, y_test = mnist.test.images, mnist.test.labels - X_train = np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant') - X_validation = np.pad(X_validation, ((0,0),(2,2),(2,2),(0,0)), 'constant') - X_test = np.pad(X_test, ((0,0),(2,2),(2,2),(0,0)), 'constant') y_train = keras.utils.to_categorical(y_train, 10) y_validation = keras.utils.to_categorical(y_validation, 10) return X_train, y_train, X_validation, y_validation, X_test, y_test @@ -108,6 +105,11 @@ def plot_history(history, metric = None): def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, metrics=['categorical_accuracy'], optimizer = None): shape = (32, 32, 1) + + # Pad data to 32x32 (MNIST is 28x28) + x_train = np.pad(x_train, ((0,0),(2,2),(2,2),(0,0)), 'constant') + x_val = np.pad(x_val, ((0,0),(2,2),(2,2),(0,0)), 'constant') + model = get_lenet(shape) if optimizer = None: @@ -120,6 +122,7 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, return model def test_classifier(model, x_test, y_true): + x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant') y_pred = model.predict(x_test) print(categorical_accuracy(y_true, y_pred)) plot_example_errors(y_pred, y_true, x_test) -- cgit v1.2.3 From f4344da5fe4eec07efd3a1cee245975402afcfb8 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Wed, 27 Feb 2019 18:54:05 +0000 Subject: Use == instead of = err --- lenet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lenet.py b/lenet.py index 93a80cf..fbcdd5d 100644 --- a/lenet.py +++ b/lenet.py @@ -112,7 +112,7 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, model = get_lenet(shape) - if optimizer = None: + if optimizer == None: optimizer = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', metrics=metrics, optimizer=optimizer) -- cgit v1.2.3 From dccbc5c219abdf90c4810eed4c2ec6231bf9f140 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Wed, 27 Feb 2019 18:57:30 +0000 Subject: Remove printing lines --- lenet.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/lenet.py b/lenet.py index fbcdd5d..a62138b 100644 --- a/lenet.py +++ b/lenet.py @@ -46,8 +46,6 @@ def plot_example_errors(y_pred, y_true, X_test): y_true = np.argmax(y_true, axis=1) correct_prediction = np.equal(y_pred, y_true) incorrect = np.equal(correct_prediction, False) - print(correct_prediction.shape) - print(incorrect[0]) images = X_test[incorrect] cls_pred = y_pred[incorrect] cls_true = y_true[incorrect] -- cgit v1.2.3 From fae3543a1a5ee731dd4f961a860c856521f7f3fd Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Wed, 27 Feb 2019 19:02:35 +0000 Subject: Fix categorical_accuracy --- lenet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lenet.py b/lenet.py index a62138b..982cd7e 100644 --- a/lenet.py +++ b/lenet.py @@ -101,7 +101,7 @@ def plot_history(history, metric = None): plt.ylabel('Loss') plt.xlabel('Epoch') -def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, metrics=['categorical_accuracy'], optimizer = None): +def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, metrics=[categorical_accuracy], optimizer = None): shape = (32, 32, 1) # Pad data to 32x32 (MNIST is 28x28) -- cgit v1.2.3