From 9818b0b8283951526b9e06ba6ccb6de5ee080d37 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Fri, 8 Mar 2019 01:07:16 +0000 Subject: Use softmax back again in training lenet --- lenet.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 4950fe9..6bea8dd 100644 --- a/lenet.py +++ b/lenet.py @@ -64,8 +64,7 @@ def get_lenet(shape): model.add(Dense(units=120, activation='relu')) model.add(Dense(units=84, activation='relu')) - #model.add(Dense(units=10, activation = 'softmax')) - model.add(Dense(units=10, activation = 'relu')) + model.add(Dense(units=10, activation = 'softmax')) return model def get_lenet_icp(shape): -- cgit v1.2.3-54-g00ecf From e58605e30e90bbfcbfd37dcac57e9d97d4c17a85 Mon Sep 17 00:00:00 2001 From: nunzip Date: Fri, 8 Mar 2019 02:03:51 +0000 Subject: Add confusion matrix --- lenet.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 6bea8dd..3440af7 100644 --- a/lenet.py +++ b/lenet.py @@ -11,6 +11,7 @@ from tensorflow.keras.metrics import categorical_accuracy import numpy as np import random from sklearn.metrics import accuracy_score +from sklearn.metrics import confusion_matrix from sklearn.model_selection import train_test_split from classifier_metrics_impl import classifier_score_from_logits @@ -139,7 +140,7 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, model.save_weights('./weights.h5') return model -def test_classifier(model, x_test, y_true): +def test_classifier(model, x_test, y_true, conf_mat=False): x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant') y_pred = model.predict(x_test) logits = tf.convert_to_tensor(y_pred, dtype=tf.float32) @@ -147,6 +148,13 @@ def test_classifier(model, x_test, y_true): y_pred = np.argmax(y_pred, axis=1) y_true = np.argmax(y_true, axis=1) plot_example_errors(y_pred, y_true, x_test) + cm = confusion_matrix(y_true, y_pred) + if conf_mat: + plt.matshow(cm, cmap='Blues') + plt.colorbar() + plt.ylabel('Actual') + plt.xlabel('Predicted') + plt.show() return accuracy_score(y_true, y_pred), inception_score def mix_data(X_train, y_train, X_validation, y_validation, train_gen, tr_labels_gen, val_gen, val_labels_gen, split=0): -- cgit v1.2.3-54-g00ecf From 035a11b98dc4b78ccab8ddc35a7fceaea9bb00c6 Mon Sep 17 00:00:00 2001 From: nunzip Date: Fri, 8 Mar 2019 11:11:41 +0000 Subject: Add PCA representation --- lenet.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 3440af7..2ec7196 100644 --- a/lenet.py +++ b/lenet.py @@ -13,8 +13,9 @@ import random from sklearn.metrics import accuracy_score from sklearn.metrics import confusion_matrix from sklearn.model_selection import train_test_split - +from sklearn.decomposition import PCA from classifier_metrics_impl import classifier_score_from_logits +from sklearn.utils import shuffle def import_mnist(): from tensorflow.examples.tutorials.mnist import input_data @@ -82,6 +83,19 @@ def get_lenet_icp(shape): model.add(Dense(units=10, activation = 'relu')) return model +def get_lenet_pen(shape): + model = keras.Sequential() + model.add(Conv2D(filters=6, kernel_size=(3, 3), activation='relu', input_shape=(32,32,1))) + model.add(AveragePooling2D()) + + model.add(Conv2D(filters=16, kernel_size=(3, 3), activation='relu')) + model.add(AveragePooling2D()) + model.add(Flatten()) + + model.add(Dense(units=120, activation='relu')) + model.add(Dense(units=84, activation='relu')) + return model + def plot_history(history, metric = None): # Plots the loss history of training and validation (if existing) # and a given metric @@ -140,7 +154,7 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, model.save_weights('./weights.h5') return model -def test_classifier(model, x_test, y_true, conf_mat=False): +def test_classifier(model, x_test, y_true, conf_mat=False, pca=False): x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant') y_pred = model.predict(x_test) logits = tf.convert_to_tensor(y_pred, dtype=tf.float32) @@ -155,6 +169,15 @@ def test_classifier(model, x_test, y_true, conf_mat=False): plt.ylabel('Actual') plt.xlabel('Predicted') plt.show() + if pca: + set_pca = PCA(n_components=2) + pca_rep = set_pca.fit_transform(x_test) + pca_rep, y_tmp = shuffle(pca_rep, y_tmp, random_state=0) + plt.scatter(pca_rep[:100, 0], pca_rep[:100, 1], c=y_true, edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('spectral', 10)) + plt.xlabel('component 1') + plt.ylabel('component 2') + plt.colorbar(); + return accuracy_score(y_true, y_pred), inception_score def mix_data(X_train, y_train, X_validation, y_validation, train_gen, tr_labels_gen, val_gen, val_labels_gen, split=0): -- cgit v1.2.3-54-g00ecf From 54e248d0d28bb6faeaefa8b25195af236ec70150 Mon Sep 17 00:00:00 2001 From: nunzip Date: Fri, 8 Mar 2019 11:34:59 +0000 Subject: Fix PCA Bug --- lenet.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 2ec7196..7c4c48c 100644 --- a/lenet.py +++ b/lenet.py @@ -171,12 +171,16 @@ def test_classifier(model, x_test, y_true, conf_mat=False, pca=False): plt.show() if pca: set_pca = PCA(n_components=2) - pca_rep = set_pca.fit_transform(x_test) - pca_rep, y_tmp = shuffle(pca_rep, y_tmp, random_state=0) - plt.scatter(pca_rep[:100, 0], pca_rep[:100, 1], c=y_true, edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('spectral', 10)) + pca_rep = np.reshape(x_test, (x_test.shape[0], x_test.shape[1]*x_test.shape[2])) + print(pca_rep.shape) + pca_rep = set_pca.fit_transform(pca_rep) + print(pca_rep.shape) + pca_rep, y_tmp = shuffle(pca_rep, y_true, random_state=0) + plt.scatter(pca_rep[:100, 0], pca_rep[:100, 1], c=y_true[:100], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Spectral', 10)) plt.xlabel('component 1') plt.ylabel('component 2') plt.colorbar(); + plt.show() return accuracy_score(y_true, y_pred), inception_score @@ -211,4 +215,4 @@ if __name__ == '__main__': x_train, y_train, x_val, y_val, x_t, y_t = import_mnist() print(y_t.shape) model = train_classifier(x_train[:100], y_train[:100], x_val, y_val, epochs=3) - print(test_classifier(model, x_t, y_t)) + print(test_classifier(model, x_t, y_t, pca=True)) -- cgit v1.2.3-54-g00ecf From 4a0b1bd9b6a622007e9e5b5e99f49c3a5249d16b Mon Sep 17 00:00:00 2001 From: nunzip Date: Fri, 8 Mar 2019 11:43:11 +0000 Subject: Change PCA color --- lenet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 7c4c48c..39a5cd7 100644 --- a/lenet.py +++ b/lenet.py @@ -176,7 +176,7 @@ def test_classifier(model, x_test, y_true, conf_mat=False, pca=False): pca_rep = set_pca.fit_transform(pca_rep) print(pca_rep.shape) pca_rep, y_tmp = shuffle(pca_rep, y_true, random_state=0) - plt.scatter(pca_rep[:100, 0], pca_rep[:100, 1], c=y_true[:100], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Spectral', 10)) + plt.scatter(pca_rep[:100, 0], pca_rep[:100, 1], c=y_true[:100], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) plt.xlabel('component 1') plt.ylabel('component 2') plt.colorbar(); -- cgit v1.2.3-54-g00ecf From 97cc07ebc57a813f1fd4b32f314f455c033ab55a Mon Sep 17 00:00:00 2001 From: nunzip Date: Fri, 8 Mar 2019 18:30:14 +0000 Subject: Remove get_lenet_pen --- lenet.py | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 39a5cd7..3d388de 100644 --- a/lenet.py +++ b/lenet.py @@ -83,19 +83,6 @@ def get_lenet_icp(shape): model.add(Dense(units=10, activation = 'relu')) return model -def get_lenet_pen(shape): - model = keras.Sequential() - model.add(Conv2D(filters=6, kernel_size=(3, 3), activation='relu', input_shape=(32,32,1))) - model.add(AveragePooling2D()) - - model.add(Conv2D(filters=16, kernel_size=(3, 3), activation='relu')) - model.add(AveragePooling2D()) - model.add(Flatten()) - - model.add(Dense(units=120, activation='relu')) - model.add(Dense(units=84, activation='relu')) - return model - def plot_history(history, metric = None): # Plots the loss history of training and validation (if existing) # and a given metric -- cgit v1.2.3-54-g00ecf From da913f9a4dabab31698669b09b69a215d7947c4e Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Sun, 10 Mar 2019 17:01:42 +0000 Subject: Add TSNE and fix PCA --- lenet.py | 31 ++++++++++++++++++++----------- report/paper.md | 8 ++++++-- 2 files changed, 26 insertions(+), 13 deletions(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 3d388de..3d9ed20 100644 --- a/lenet.py +++ b/lenet.py @@ -16,6 +16,7 @@ from sklearn.model_selection import train_test_split from sklearn.decomposition import PCA from classifier_metrics_impl import classifier_score_from_logits from sklearn.utils import shuffle +from sklearn.manifold import TSNE def import_mnist(): from tensorflow.examples.tutorials.mnist import input_data @@ -141,12 +142,12 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, model.save_weights('./weights.h5') return model -def test_classifier(model, x_test, y_true, conf_mat=False, pca=False): +def test_classifier(model, x_test, y_true, conf_mat=False, pca=False, tsne=False): x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant') - y_pred = model.predict(x_test) - logits = tf.convert_to_tensor(y_pred, dtype=tf.float32) - inception_score = tf.keras.backend.eval(classifier_score_from_logits(logits)) - y_pred = np.argmax(y_pred, axis=1) + logits = model.predict(x_test) + tf_logits = tf.convert_to_tensor(logits, dtype=tf.float32) + inception_score = tf.keras.backend.eval(classifier_score_from_logits(tf_logits)) + y_pred = np.argmax(logits, axis=1) y_true = np.argmax(y_true, axis=1) plot_example_errors(y_pred, y_true, x_test) cm = confusion_matrix(y_true, y_pred) @@ -158,16 +159,24 @@ def test_classifier(model, x_test, y_true, conf_mat=False, pca=False): plt.show() if pca: set_pca = PCA(n_components=2) - pca_rep = np.reshape(x_test, (x_test.shape[0], x_test.shape[1]*x_test.shape[2])) - print(pca_rep.shape) - pca_rep = set_pca.fit_transform(pca_rep) - print(pca_rep.shape) + pca_rep = set_pca.fit_transform(logits) pca_rep, y_tmp = shuffle(pca_rep, y_true, random_state=0) - plt.scatter(pca_rep[:100, 0], pca_rep[:100, 1], c=y_true[:100], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) + plt.scatter(pca_rep[:1000, 0], pca_rep[:1000, 1], c=y_true[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) plt.xlabel('component 1') plt.ylabel('component 2') plt.colorbar(); plt.show() + if tsne: + tsne = TSNE(n_components=2, random_state=0) + components = tsne.fit_transform(logits) + print(components.shape) + components, y_tmp = shuffle(components, y_true, random_state=0) + plt.scatter(components[:1000, 0], components[:1000, 1], c=y_true[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) + plt.xlabel('component 1') + plt.ylabel('component 2') + plt.colorbar(); + plt.show() + return accuracy_score(y_true, y_pred), inception_score @@ -202,4 +211,4 @@ if __name__ == '__main__': x_train, y_train, x_val, y_val, x_t, y_t = import_mnist() print(y_t.shape) model = train_classifier(x_train[:100], y_train[:100], x_val, y_val, epochs=3) - print(test_classifier(model, x_t, y_t, pca=True)) + print(test_classifier(model, x_t, y_t, pca=False, tsne=True)) diff --git a/report/paper.md b/report/paper.md index 53cdb3f..e053353 100644 --- a/report/paper.md +++ b/report/paper.md @@ -151,7 +151,7 @@ architectures in Q2.** We measure the performance of the considered GAN's using the Inecption score [-inception], as calculated with L2-Net logits. -$$ \textrm{IS}(x) = \exp(\mathcal{E}_x \left( \textrm{KL} ( p(y\|x) \|\| p(y) ) \right) ) $$ +$$ \textrm{IS}(x) = \exp(\mathbb{E}_x \left( \textrm{KL} ( p(y\mid x) \| p(y) ) \right) ) $$ ``` \begin{table}[] @@ -252,7 +252,11 @@ as most of the testing images that got misclassified (mainly nines and fours) sh # Bonus -This is an open question. Do you have any other ideas to improve GANs or +## Relation to PCA + +Similarly to GAN's, PCA can be used to formulate **generative** models of a system. While GAN's are trained neural networks, PCA is a definite statistical procedure which perform orthogonal transformations of the data. While both attempt to identify the most important or *variant* features of the data (which we may then use to generate new data), PCA by itself is only able to extract linearly related features. In a purely linear system, a GAN would be converging to PCA. In a more complicated system, we would ndeed to identify relevant kernels in order to extract relevant features with PCA, while a GAN is able to leverage dense and convolutional neural network layers which may be trained to perform relevant transformations. + +* This is an open question. Do you have any other ideas to improve GANs or have more insightful and comparative evaluations of GANs? Ideas are not limited. For instance, \begin{itemize} -- cgit v1.2.3-54-g00ecf From 8a3c9ecfec05ce73f0fe69426bfc97110b6f189f Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Sun, 10 Mar 2019 17:39:06 +0000 Subject: Use correct label for colour --- lenet.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 3d9ed20..273053d 100644 --- a/lenet.py +++ b/lenet.py @@ -161,7 +161,7 @@ def test_classifier(model, x_test, y_true, conf_mat=False, pca=False, tsne=False set_pca = PCA(n_components=2) pca_rep = set_pca.fit_transform(logits) pca_rep, y_tmp = shuffle(pca_rep, y_true, random_state=0) - plt.scatter(pca_rep[:1000, 0], pca_rep[:1000, 1], c=y_true[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) + plt.scatter(pca_rep[:1000, 0], pca_rep[:1000, 1], c=y_tmp[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) plt.xlabel('component 1') plt.ylabel('component 2') plt.colorbar(); @@ -171,7 +171,7 @@ def test_classifier(model, x_test, y_true, conf_mat=False, pca=False, tsne=False components = tsne.fit_transform(logits) print(components.shape) components, y_tmp = shuffle(components, y_true, random_state=0) - plt.scatter(components[:1000, 0], components[:1000, 1], c=y_true[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) + plt.scatter(components[:1000, 0], components[:1000, 1], c=y_tmp[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) plt.xlabel('component 1') plt.ylabel('component 2') plt.colorbar(); -- cgit v1.2.3-54-g00ecf From f60cf0650f8a1a4872308edff8ab2deb8503bb76 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Sun, 10 Mar 2019 18:21:29 +0000 Subject: Slightly improve graphs --- lenet.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 273053d..9b4bb2e 100644 --- a/lenet.py +++ b/lenet.py @@ -161,9 +161,9 @@ def test_classifier(model, x_test, y_true, conf_mat=False, pca=False, tsne=False set_pca = PCA(n_components=2) pca_rep = set_pca.fit_transform(logits) pca_rep, y_tmp = shuffle(pca_rep, y_true, random_state=0) - plt.scatter(pca_rep[:1000, 0], pca_rep[:1000, 1], c=y_tmp[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) - plt.xlabel('component 1') - plt.ylabel('component 2') + plt.scatter(pca_rep[:5000, 0], pca_rep[:5000, 1], c=y_tmp[:5000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) + plt.xlabel('Feature 1') + plt.ylabel('Feature 2') plt.colorbar(); plt.show() if tsne: @@ -171,9 +171,9 @@ def test_classifier(model, x_test, y_true, conf_mat=False, pca=False, tsne=False components = tsne.fit_transform(logits) print(components.shape) components, y_tmp = shuffle(components, y_true, random_state=0) - plt.scatter(components[:1000, 0], components[:1000, 1], c=y_tmp[:1000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) - plt.xlabel('component 1') - plt.ylabel('component 2') + plt.scatter(components[:5000, 0], components[:5000, 1], c=y_tmp[:5000], edgecolor='none', alpha=0.5, cmap=plt.cm.get_cmap('Paired', 10)) + plt.xlabel('Feature 1') + plt.ylabel('Feature 2') plt.colorbar(); plt.show() -- cgit v1.2.3-54-g00ecf From 717da7a7e173cd971baddd9f8edb6de668b1e815 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Sun, 10 Mar 2019 18:30:33 +0000 Subject: Add plot probas function --- lenet.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 9b4bb2e..881cfd7 100644 --- a/lenet.py +++ b/lenet.py @@ -17,6 +17,7 @@ from sklearn.decomposition import PCA from classifier_metrics_impl import classifier_score_from_logits from sklearn.utils import shuffle from sklearn.manifold import TSNE +import scikitplot as skplt def import_mnist(): from tensorflow.examples.tutorials.mnist import input_data @@ -142,6 +143,13 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, model.save_weights('./weights.h5') return model +def plot_probas(model, x_test, y_true): + probas = model.predict(x_test) + skplt.metrics.plot_roc(y_true, probas) + plt.show() + skplt.metrics.plot_precision_recall_curve(y_true, probas) + plt.show() + def test_classifier(model, x_test, y_true, conf_mat=False, pca=False, tsne=False): x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant') logits = model.predict(x_test) -- cgit v1.2.3-54-g00ecf From 07ae3a62f460e8b1ae700194cd85f05b2d2f011d Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Sun, 10 Mar 2019 18:46:24 +0000 Subject: Add padding to plot_probas --- lenet.py | 1 + 1 file changed, 1 insertion(+) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 881cfd7..0fe8277 100644 --- a/lenet.py +++ b/lenet.py @@ -144,6 +144,7 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, return model def plot_probas(model, x_test, y_true): + x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant') probas = model.predict(x_test) skplt.metrics.plot_roc(y_true, probas) plt.show() -- cgit v1.2.3-54-g00ecf From f9c0139f63438a2574d9931f732cb3aecd172485 Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Sun, 10 Mar 2019 19:00:54 +0000 Subject: Fix y_true in plot_probas --- lenet.py | 1 + 1 file changed, 1 insertion(+) (limited to 'lenet.py') diff --git a/lenet.py b/lenet.py index 0fe8277..a94259b 100644 --- a/lenet.py +++ b/lenet.py @@ -144,6 +144,7 @@ def train_classifier(x_train, y_train, x_val, y_val, batch_size=128, epochs=100, return model def plot_probas(model, x_test, y_true): + y_true = np.argmax(y_true, axis=1) x_test = np.pad(x_test, ((0,0),(2,2),(2,2),(0,0)), 'constant') probas = model.predict(x_test) skplt.metrics.plot_roc(y_true, probas) -- cgit v1.2.3-54-g00ecf