├── LICENSE ├── README.md ├── chapter_03 └── numpy_speed_test.py ├── chapter_04 ├── box_plot.py ├── dataset_exact.py ├── dataset_random.py └── feature_stats.py ├── chapter_05 ├── build_mnist.py ├── cifar10_augment.py ├── iris_data_augmentation.py ├── iris_pca_plots.py ├── load_bc.py ├── load_cifar10.py └── load_iris.py ├── chapter_06 └── factorial.py ├── chapter_07 ├── bc_experiments.py ├── bc_kfold.py ├── bc_rbf_svm_search.py ├── iris_centroids.py ├── iris_experiments.py ├── mnist_experiments.py └── mnist_pca.py ├── chapter_08 ├── ._chapter_08 ├── nn_iris_dataset.py ├── nn_iris_evaluate.py └── nn_iris_mlpclassifier.py ├── chapter_10 ├── ._chapter_10 ├── README.txt ├── mnist_nn_experiments.py ├── mnist_nn_experiments_30k_momentum.py ├── mnist_nn_experiments_L2.py ├── mnist_nn_experiments_L2_plot.py ├── mnist_nn_experiments_base_lr.py ├── mnist_nn_experiments_base_lr_plot.py ├── mnist_nn_experiments_batch_size.py ├── mnist_nn_experiments_batch_size_plot.py ├── mnist_nn_experiments_init.py ├── mnist_nn_experiments_init_plot.py ├── mnist_nn_experiments_momentum.py ├── mnist_nn_experiments_momentum_plot.py ├── mnist_nn_experiments_relu.py ├── mnist_nn_experiments_retrain.py ├── mnist_nn_experiments_sample_plot.py ├── mnist_nn_experiments_samples.py └── mnist_nn_experiments_scatter_plot.py ├── chapter_11 ├── advanced_metrics.py ├── basic_metrics.py ├── bc_experiments.py ├── confusion_matrix.py ├── make_roc_points.py ├── make_roc_points_sklearn.py ├── mnist_2x2_tables.py ├── mnist_even_odd.py ├── mnist_even_odd_labels.py └── tally_predictions.py ├── chapter_13 ├── mnist_cnn_exp0.py ├── mnist_cnn_exp1.py ├── mnist_cnn_exp10.py ├── mnist_cnn_exp2.py ├── mnist_cnn_exp26.py ├── mnist_cnn_exp27.py ├── mnist_cnn_exp28.py ├── mnist_cnn_exp29.py ├── mnist_cnn_exp2A.py ├── mnist_cnn_exp2B.py ├── mnist_cnn_exp2C.py ├── mnist_cnn_exp2D.py ├── mnist_cnn_exp2E.py ├── mnist_cnn_exp3.py ├── mnist_cnn_exp30.py ├── mnist_cnn_exp31.py ├── mnist_cnn_exp32.py ├── mnist_cnn_exp33.py ├── mnist_cnn_exp34.py ├── mnist_cnn_exp35.py ├── mnist_cnn_exp36.py ├── mnist_cnn_exp37.py ├── mnist_cnn_exp38.py ├── mnist_cnn_exp39.py ├── mnist_cnn_exp4.py ├── mnist_cnn_exp40.py ├── mnist_cnn_exp41.py ├── mnist_cnn_exp42.py ├── mnist_cnn_exp43.py ├── mnist_cnn_exp44.py ├── mnist_cnn_exp45.py ├── mnist_cnn_exp46.py ├── mnist_cnn_exp47.py ├── mnist_cnn_exp5.py ├── mnist_cnn_exp6.py ├── mnist_cnn_exp7.py ├── mnist_cnn_exp8.py ├── mnist_cnn_exp9.py ├── mnist_large │ ├── README.txt │ ├── make_large_mnist_test_images.py │ ├── make_shifted_mnist_dataset.py │ ├── mnist_cnn.py │ ├── mnist_cnn_aug.py │ ├── mnist_cnn_aug_fcn.py │ ├── mnist_cnn_aug_fcn_heatmaps.py │ ├── mnist_cnn_aug_fcn_test.py │ ├── mnist_cnn_fcn.py │ ├── mnist_cnn_fcn_graymaps.py │ ├── mnist_cnn_fcn_single_digits.py │ └── mnist_cnn_fcn_test_large.py └── run_all ├── chapter_14 ├── cat_dog │ ├── cifar10_cnn_cat_dog_fine_tune_0.py │ ├── cifar10_cnn_cat_dog_fine_tune_1.py │ ├── cifar10_cnn_cat_dog_fine_tune_2.py │ ├── cifar10_cnn_cat_dog_fine_tune_3.py │ ├── cifar10_cnn_vehicles.py │ ├── make_small_cat_dog_dataset.py │ └── make_vehicle_dataset.py ├── cifar10_cnn.py ├── cifar10_cnn_SGD.py ├── cifar10_cnn_animal_vehicles_real_labels.py ├── cifar10_cnn_arbitrary.py ├── cifar10_make_animal_vehicle.py ├── cifar10_one_vs_many.py ├── make_label_files.py ├── roc_curve_area.py ├── tally_predictions.py ├── train_single_models └── transfer_learning │ └── transfer_learning.py ├── chapter_15 ├── ensemble.py ├── esc10_audio_classical.py ├── esc10_audio_cnn_deep0.py ├── esc10_audio_cnn_deep1.py ├── esc10_audio_cnn_deep2.py ├── esc10_audio_cnn_medium.py ├── esc10_audio_cnn_shallow.py ├── esc10_audio_mlp.py ├── esc10_cnn_deep.py ├── esc10_cnn_deep_bn.py ├── esc10_cnn_deep_he.py ├── esc10_cnn_deep_l2.py ├── extract_esc10.py ├── make_augmented_1d_dataset.py ├── make_augmented_audio.py ├── make_augmented_spectrograms.py └── stats.py ├── data ├── README.txt ├── audio │ └── README.txt ├── breast │ └── README.txt ├── cifar10 │ └── README.txt ├── iris │ └── README.txt └── mnist │ └── README.txt └── tutorial.pdf /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Ronald T Kneusel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Source code for "Practical Deep Learning: A Python-Based Introduction" 2 | ---------------------------------------------------------------------- 3 | 4 | You'll find the source code included or referenced in the book in this 5 | archive. The code is organized by chapter. If the chapter is not listed, 6 | there was no code to go with it. 7 | 8 | All the code is Python 3.X and requires the libraries installed in Chapter 1 9 | of the book. 10 | 11 | Please send questions, comments, or bugs to: 12 | 13 | rkneuselbooks@gmail.com 14 | 15 | Updates: 16 | 17 | page 84: the URL for the Iris dataset has changed: 18 | https://archive.ics.uci.edu/dataset/53/iris 19 | 20 | page 86: the URL for the Breast Cancer dataset has changed: 21 | https://archive.ics.uci.edu/dataset/15/breast+cancer+wisconsin+original 22 | 23 | TensorFlow issues addressable with: 24 | pip3 install tensorflow-cpu (TF 2.8) 25 | then update repo code to move 'keras' imports to 'tensorflow.keras' 26 | 27 | Moving from Adadelta to Adam: 28 | Adadelta appears to be broken in newer versions of TensorFlow. Therefore, if you 29 | are getting poor performance, I suggest moving all models to Adam as the optimizer. 30 | Simply replace "Adadelta" with "Adam" and you should be good to go. 31 | 32 | The file tutorial.pdf is a beginner's guide to NumPy, SciPy, Matplotlib, and Pillow. 33 | 34 | -------------------------------------------------------------------------------- /chapter_03/numpy_speed_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import time 3 | import random 4 | 5 | n = 1000000 6 | a = [random.random() for i in range(n)] 7 | b = [random.random() for i in range(n)] 8 | 9 | s = time.time() 10 | c = [a[i]*b[i] for i in range(n)] 11 | print("comprehension:", time.time()-s) 12 | 13 | s = time.time() 14 | c = [] 15 | for i in range(n): 16 | c.append(a[i]*b[i]) 17 | print("for loop:", time.time()-s) 18 | 19 | s = time.time() 20 | c = [0]*n 21 | for i in range(n): 22 | c[i] = a[i]*b[i] 23 | print("existing list:", time.time()-s) 24 | 25 | x = np.array(a) 26 | y = np.array(b) 27 | s = time.time() 28 | c = x*y 29 | print("NumPy time", time.time()-s) 30 | 31 | -------------------------------------------------------------------------------- /chapter_04/box_plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | d = [[ 0.6930, -1.1259, -1.5318, 0.9525, 1.1824], 5 | [ 0.5464, -0.0120, 0.5051, -0.0192, -0.1141], 6 | [ 0.8912, 1.3826, 1.5193, -1.1996, -1.1403], 7 | [ 1.1690, 0.4970, -0.1712, -0.5340, 0.3047], 8 | [-0.9221, -0.1071, 0.3079, -0.3885, -0.4753], 9 | [ 1.5699, -1.4767, 0.3327, 1.4714, 1.1807], 10 | [-0.3479, 0.4775, 1.8823, -1.4031, -0.7396], 11 | [ 0.0887, -0.4353, -1.7377, -1.2349, 1.7456], 12 | [ 1.0775, 0.9524, 1.2475, 0.7291, -1.1207], 13 | [-1.4657, 0.9250, -1.0446, 0.4262, -1.0279], 14 | [-1.3332, 1.4501, 0.0323, 1.1102, -0.8966], 15 | [ 0.3005, -1.4500, -0.2615, 1.7033, -0.2505], 16 | [-1.4377, -0.2472, -0.4340, -0.7032, 0.3362], 17 | [ 0.3016, -1.5527, -0.6213, 0.1780, -0.7517], 18 | [-1.1315, 0.7225, -0.0250, -1.0881, 1.7674]] 19 | d = np.array(d) 20 | plt.boxplot(d) 21 | plt.show() 22 | 23 | -------------------------------------------------------------------------------- /chapter_04/dataset_exact.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.datasets import make_classification 3 | 4 | a,b = make_classification(n_samples=10000, weights=(0.9,0.1)) 5 | idx = np.where(b == 0)[0] 6 | x0 = a[idx,:] 7 | y0 = b[idx] 8 | idx = np.where(b == 1)[0] 9 | x1 = a[idx,:] 10 | y1 = b[idx] 11 | 12 | idx = np.argsort(np.random.random(y0.shape)) 13 | y0 = y0[idx] 14 | x0 = x0[idx] 15 | idx = np.argsort(np.random.random(y1.shape)) 16 | y1 = y1[idx] 17 | x1 = x1[idx] 18 | 19 | ntrn0 = int(0.9*x0.shape[0]) 20 | ntrn1 = int(0.9*x1.shape[0]) 21 | xtrn = np.zeros((int(ntrn0+ntrn1),20)) 22 | ytrn = np.zeros(int(ntrn0+ntrn1)) 23 | xtrn[:ntrn0] = x0[:ntrn0] 24 | xtrn[ntrn0:] = x1[:ntrn1] 25 | ytrn[:ntrn0] = y0[:ntrn0] 26 | ytrn[ntrn0:] = y1[:ntrn1] 27 | 28 | n0 = int(x0.shape[0]-ntrn0) 29 | n1 = int(x1.shape[0]-ntrn1) 30 | xval = np.zeros((int(n0/2+n1/2),20)) 31 | yval = np.zeros(int(n0/2+n1/2)) 32 | xval[:(n0//2)] = x0[ntrn0:(ntrn0+n0//2)] 33 | xval[(n0//2):] = x1[ntrn1:(ntrn1+n1//2)] 34 | yval[:(n0//2)] = y0[ntrn0:(ntrn0+n0//2)] 35 | yval[(n0//2):] = y1[ntrn1:(ntrn1+n1//2)] 36 | 37 | xtst = np.concatenate((x0[(ntrn0+n0//2):],x1[(ntrn1+n1//2):])) 38 | ytst = np.concatenate((y0[(ntrn0+n0//2):],y1[(ntrn1+n1//2):])) 39 | 40 | -------------------------------------------------------------------------------- /chapter_04/dataset_random.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.datasets import make_classification 3 | 4 | x,y = make_classification(n_samples=10000, weights=(0.9,0.1)) 5 | idx = np.argsort(np.random.random(y.shape[0])) 6 | x = x[idx] 7 | y = y[idx] 8 | 9 | ntrn = int(0.9*y.shape[0]) 10 | nval = int(0.05*y.shape[0]) 11 | 12 | xtrn = x[:ntrn] 13 | ytrn = y[:ntrn] 14 | xval = x[ntrn:(ntrn+nval)] 15 | yval = y[ntrn:(ntrn+nval)] 16 | xtst = x[(ntrn+nval):] 17 | ytst = y[(ntrn+nval):] 18 | 19 | -------------------------------------------------------------------------------- /chapter_04/feature_stats.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | f = [0.3408,3.0150,4.3465,2.1271,2.7561, 4 | 2.7887,4.8231,0.0705,3.9897,0.9804, 5 | 2.3944,2.0085,1.7821,1.5362,2.3190] 6 | f = np.array(f) 7 | 8 | print 9 | print("mean = %0.4f" % f.mean()) 10 | print("std = %0.4f" % f.std()) 11 | print("SE = %0.4f" % (f.std()/np.sqrt(f.shape[0]))) 12 | print("median= %0.4f" % np.median(f)) 13 | print("min = %0.4f" % f.min()) 14 | print("max = %0.4f" % f.max()) 15 | 16 | -------------------------------------------------------------------------------- /chapter_05/build_mnist.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import keras 3 | from keras.datasets import mnist 4 | 5 | (xtrn, ytrn), (xtst, ytst) = mnist.load_data() 6 | idx = np.argsort(np.random.random(ytrn.shape[0])) 7 | xtrn = xtrn[idx] 8 | ytrn = ytrn[idx] 9 | idx = np.argsort(np.random.random(ytst.shape[0])) 10 | xtst = xtst[idx] 11 | ytst = ytst[idx] 12 | 13 | np.save("../data/mnist/mnist_train_images.npy", xtrn) 14 | np.save("../data/mnist/mnist_train_labels.npy", ytrn) 15 | np.save("../data/mnist/mnist_test_images.npy", xtst) 16 | np.save("../data/mnist/mnist_test_labels.npy", ytst) 17 | 18 | xtrnv = xtrn.reshape((60000,28*28)) 19 | xtstv = xtst.reshape((10000,28*28)) 20 | np.save("../data/mnist/mnist_train_vectors.npy", xtrnv) 21 | np.save("../data/mnist/mnist_test_vectors.npy", xtstv) 22 | 23 | idx = np.argsort(np.random.random(28*28)) 24 | for i in range(60000): 25 | xtrnv[i,:] = xtrnv[i,idx] 26 | for i in range(10000): 27 | xtstv[i,:] = xtstv[i,idx] 28 | np.save("../data/mnist/mnist_train_scrambled_vectors.npy", xtrnv) 29 | np.save("../data/mnist/mnist_test_scrambled_vectors.npy", xtstv) 30 | 31 | t = np.zeros((60000,28,28)) 32 | for i in range(60000): 33 | t[i,:,:] = xtrnv[i,:].reshape((28,28)) 34 | np.save("../data/mnist/mnist_train_scrambled_images.npy", t) 35 | t = np.zeros((10000,28,28)) 36 | for i in range(10000): 37 | t[i,:,:] = xtstv[i,:].reshape((28,28)) 38 | np.save("../data/mnist/mnist_test_scrambled_images.npy", t) 39 | 40 | -------------------------------------------------------------------------------- /chapter_05/cifar10_augment.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from PIL import Image 3 | 4 | def augment(im, dim): 5 | img = Image.fromarray(im) 6 | if (np.random.random() < 0.5): 7 | img = img.transpose(Image.FLIP_LEFT_RIGHT) 8 | if (np.random.random() < 0.3333): 9 | z = (32-dim)/2 10 | r = 10*np.random.random()-5 11 | img = img.rotate(r, resample=Image.BILINEAR) 12 | img = img.crop((z,z,32-z,32-z)) 13 | else: 14 | x = int((32-dim-1)*np.random.random()) 15 | y = int((32-dim-1)*np.random.random()) 16 | img = img.crop((x,y,x+dim,y+dim)) 17 | return np.array(img) 18 | 19 | def main(): 20 | x = np.load("../../../data/cifar10/cifar10_train_images.npy") 21 | y = np.load("../../../data/cifar10/cifar10_train_labels.npy") 22 | factor = 10 23 | dim = 28 24 | z = (32-dim)/2 25 | newx = np.zeros((x.shape[0]*factor, dim,dim,3), dtype="uint8") 26 | newy = np.zeros(y.shape[0]*factor, dtype="uint8") 27 | k=0 28 | for i in range(x.shape[0]): 29 | im = Image.fromarray(x[i,:]) 30 | im = im.crop((z,z,32-z,32-z)) 31 | newx[k,...] = np.array(im) 32 | newy[k] = y[i] 33 | k += 1 34 | for j in range(factor-1): 35 | newx[k,...] = augment(x[i,:], dim) 36 | newy[k] = y[i] 37 | k += 1 38 | idx = np.argsort(np.random.random(newx.shape[0])) 39 | newx = newx[idx] 40 | newy = newy[idx] 41 | np.save("cifar10_aug_train_images.npy", newx) 42 | np.save("cifar10_aug_train_labels.npy", newy) 43 | 44 | x = np.load("../../../data/cifar10/cifar10_test_images.npy") 45 | newx = np.zeros((x.shape[0], dim,dim,3), dtype="uint8") 46 | for i in range(x.shape[0]): 47 | im = Image.fromarray(x[i,:]) 48 | im = im.crop((z,z,32-z,32-z)) 49 | newx[i,...] = np.array(im) 50 | np.save("cifar10_aug_test_images.npy", newx) 51 | 52 | main() 53 | 54 | -------------------------------------------------------------------------------- /chapter_05/iris_data_augmentation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn import decomposition 3 | 4 | def generateData(pca, x, start): 5 | original = pca.components_.copy() 6 | ncomp = pca.components_.shape[0] 7 | a = pca.transform(x) 8 | for i in range(start, ncomp): 9 | pca.components_[i,:] += np.random.normal(scale=0.1, size=ncomp) 10 | b = pca.inverse_transform(a) 11 | pca.components_ = original.copy() 12 | return b 13 | 14 | def main(): 15 | x = np.load("../data/iris/iris_features.npy") 16 | y = np.load("../data/iris/iris_labels.npy") 17 | 18 | N = 120 19 | x_train = x[:N] 20 | y_train = y[:N] 21 | x_test = x[N:] 22 | y_test = y[N:] 23 | 24 | pca = decomposition.PCA(n_components=4) 25 | pca.fit(x) 26 | print(pca.explained_variance_ratio_) 27 | start = 2 28 | 29 | nsets = 10 30 | nsamp = x_train.shape[0] 31 | newx = np.zeros((nsets*nsamp, x_train.shape[1])) 32 | newy = np.zeros(nsets*nsamp, dtype="uint8") 33 | 34 | for i in range(nsets): 35 | if (i == 0): 36 | newx[0:nsamp,:] = x_train 37 | newy[0:nsamp] = y_train 38 | else: 39 | newx[(i*nsamp):(i*nsamp+nsamp),:] = generateData(pca, x_train, start) 40 | newy[(i*nsamp):(i*nsamp+nsamp)] = y_train 41 | 42 | idx = np.argsort(np.random.random(nsets*nsamp)) 43 | newx = newx[idx] 44 | newy = newy[idx] 45 | 46 | np.save("../data/iris/iris_train_features_augmented.npy", newx) 47 | np.save("../data/iris/iris_train_labels_augmented.npy", newy) 48 | np.save("../data/iris/iris_test_features_augmented.npy", x_test) 49 | np.save("../data/iris/iris_test_labels_augmented.npy", y_test) 50 | 51 | main() 52 | 53 | -------------------------------------------------------------------------------- /chapter_05/iris_pca_plots.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pylab as plt 3 | from sklearn import decomposition 4 | 5 | x = np.load("../data/iris/iris_features.npy")[:,:2] 6 | y = np.load("../data/iris/iris_labels.npy") 7 | idx = np.where(y != 0) 8 | x = x[idx] 9 | x[:,0] -= x[:,0].mean() 10 | x[:,1] -= x[:,1].mean() 11 | 12 | pca = decomposition.PCA(n_components=2) 13 | pca.fit(x) 14 | v = pca.explained_variance_ratio_ 15 | 16 | ax = plt.axes() 17 | plt.scatter(x[:,0],x[:,1],marker='o',color='b') 18 | x0 = v[0]*pca.components_[0,0] 19 | y0 = v[0]*pca.components_[0,1] 20 | ax.arrow(0, 0, x0, y0, head_width=0.05, head_length=0.1, fc='r', ec='r') 21 | x1 = v[1]*pca.components_[1,0] 22 | y1 = v[1]*pca.components_[1,1] 23 | ax.arrow(0, 0, x1, y1, head_width=0.05, head_length=0.1, fc='r', ec='r') 24 | plt.xlabel("$x_0$", fontsize=16) 25 | plt.ylabel("$x_1$", fontsize=16) 26 | plt.show() 27 | 28 | -------------------------------------------------------------------------------- /chapter_05/load_bc.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | with open("../data/breast/wdbc.data") as f: 5 | lines = [i[:-1] for i in f.readlines() if i != ""] 6 | 7 | n = ["B","M"] 8 | x = np.array([n.index(i.split(",")[1]) for i in lines],dtype="uint8") 9 | y = np.array([[float(j) for j in i.split(",")[2:]] for i in lines]) 10 | i = np.argsort(np.random.random(x.shape[0])) 11 | x = x[i] 12 | y = y[i] 13 | z = (y - y.mean(axis=0)) / y.std(axis=0) 14 | 15 | np.save("../data/breast/bc_features.npy", y) 16 | np.save("../data/breast/bc_features_standard.npy", z) 17 | np.save("../data/breast/bc_labels.npy", x) 18 | plt.boxplot(z) 19 | plt.show() 20 | 21 | -------------------------------------------------------------------------------- /chapter_05/load_cifar10.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import keras 4 | from keras.datasets import cifar10 5 | 6 | (xtrn, ytrn), (xtst, ytst) = cifar10.load_data() 7 | idx = np.argsort(np.random.random(ytrn.shape[0])) 8 | xtrn = xtrn[idx] 9 | ytrn = ytrn[idx] 10 | idx = np.argsort(np.random.random(ytst.shape[0])) 11 | xtst = xtst[idx] 12 | ytst = ytst[idx] 13 | 14 | os.system("mkdir ../data/cifar10/") 15 | np.save("../data/cifar10/cifar10_train_images.npy", xtrn) 16 | np.save("../data/cifar10/cifar10_train_labels.npy", ytrn) 17 | np.save("../data/cifar10/cifar10_test_images.npy", xtst) 18 | np.save("../data/cifar10/cifar10_test_labels.npy", ytst) 19 | 20 | xtrnv = xtrn.reshape((50000,32*32*3)) 21 | xtstv = xtst.reshape((10000,32*32*3)) 22 | np.save("../data/cifar10/cifar10_train_vectors.npy", xtrnv) 23 | np.save("../data/cifar10/cifar10_test_vectors.npy", xtstv) 24 | 25 | -------------------------------------------------------------------------------- /chapter_05/load_iris.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | with open("../data/iris/iris.data") as f: 4 | lines = [i[:-1] for i in f.readlines()] 5 | 6 | n = ["Iris-setosa","Iris-versicolor","Iris-virginica"] 7 | x = [n.index(i.split(",")[-1]) for i in lines if i != ""] 8 | x = np.array(x, dtype="uint8") 9 | 10 | y = [[float(j) for j in i.split(",")[:-1]] for i in lines if i != ""] 11 | y = np.array(y) 12 | 13 | i = np.argsort(np.random.random(x.shape[0])) 14 | x = x[i] 15 | y = y[i] 16 | 17 | np.save("../data/iris/iris_features.npy", y) 18 | np.save("../data/iris/iris_labels.npy", x) 19 | 20 | -------------------------------------------------------------------------------- /chapter_06/factorial.py: -------------------------------------------------------------------------------- 1 | def fact(n): 2 | if (n <= 1): 3 | return 1 4 | else: 5 | return n*fact(n-1) 6 | 7 | -------------------------------------------------------------------------------- /chapter_07/bc_experiments.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.neighbors import NearestCentroid 3 | from sklearn.neighbors import KNeighborsClassifier 4 | from sklearn.naive_bayes import GaussianNB, MultinomialNB 5 | from sklearn.tree import DecisionTreeClassifier 6 | from sklearn.ensemble import RandomForestClassifier 7 | from sklearn.svm import SVC 8 | 9 | def run(x_train, y_train, x_test, y_test, clf): 10 | clf.fit(x_train, y_train) 11 | print(" score = %0.4f" % clf.score(x_test, y_test)) 12 | print() 13 | 14 | def main(): 15 | x = np.load("../data/breast/bc_features_standard.npy") 16 | y = np.load("../data/breast/bc_labels.npy") 17 | N = 455 18 | x_train = x[:N]; x_test = x[N:] 19 | y_train = y[:N]; y_test = y[N:] 20 | 21 | print("Nearest centroid:") 22 | run(x_train, y_train, x_test, y_test, NearestCentroid()) 23 | print("k-NN classifier (k=3):") 24 | run(x_train, y_train, x_test, y_test, KNeighborsClassifier(n_neighbors=3)) 25 | print("k-NN classifier (k=7):") 26 | run(x_train, y_train, x_test, y_test, KNeighborsClassifier(n_neighbors=7)) 27 | print("Naive Bayes classifier (Gaussian):") 28 | run(x_train, y_train, x_test, y_test, GaussianNB()) 29 | print("Decision Tree classifier:") 30 | run(x_train, y_train, x_test, y_test, DecisionTreeClassifier()) 31 | print("Random Forest classifier (estimators=5):") 32 | run(x_train, y_train, x_test, y_test, RandomForestClassifier(n_estimators=5)) 33 | print("Random Forest classifier (estimators=50):") 34 | run(x_train, y_train, x_test, y_test, RandomForestClassifier(n_estimators=50)) 35 | print("SVM (linear, C=1.0):") 36 | run(x_train, y_train, x_test, y_test, SVC(kernel="linear", C=1.0)) 37 | print("SVM (RBF, C=1.0, gamma=0.03333):") 38 | run(x_train, y_train, x_test, y_test, SVC(kernel="rbf", C=1.0, gamma=0.03333)) 39 | 40 | main() 41 | 42 | -------------------------------------------------------------------------------- /chapter_07/bc_kfold.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.neighbors import NearestCentroid 3 | from sklearn.neighbors import KNeighborsClassifier 4 | from sklearn.naive_bayes import GaussianNB, MultinomialNB 5 | from sklearn.tree import DecisionTreeClassifier 6 | from sklearn.ensemble import RandomForestClassifier 7 | from sklearn.svm import SVC 8 | import sys 9 | 10 | def run(x_train, y_train, x_test, y_test, clf): 11 | clf.fit(x_train, y_train) 12 | return clf.score(x_test, y_test) 13 | 14 | def split(x,y,k,m): 15 | ns = int(y.shape[0]/m) 16 | s = [] 17 | for i in range(m): 18 | s.append([x[(ns*i):(ns*i+ns)], 19 | y[(ns*i):(ns*i+ns)]]) 20 | x_test, y_test = s[k] 21 | x_train = [] 22 | y_train = [] 23 | for i in range(m): 24 | if (i==k): 25 | continue 26 | else: 27 | a,b = s[i] 28 | x_train.append(a) 29 | y_train.append(b) 30 | x_train = np.array(x_train).reshape(((m-1)*ns,30)) 31 | y_train = np.array(y_train).reshape((m-1)*ns) 32 | return [x_train, y_train, x_test, y_test] 33 | 34 | def pp(z,k,s): 35 | m = z.shape[1] 36 | print("%-19s: %0.4f +/- %0.4f | " % (s, z[k].mean(), z[k].std()/np.sqrt(m)), end='') 37 | for i in range(m): 38 | print("%0.4f " % z[k,i], end='') 39 | print() 40 | 41 | def main(): 42 | x = np.load("../data/breast/bc_features_standard.npy") 43 | y = np.load("../data/breast/bc_labels.npy") 44 | idx = np.argsort(np.random.random(y.shape[0])) 45 | x = x[idx] 46 | y = y[idx] 47 | m = int(sys.argv[1]) 48 | z = np.zeros((8,m)) 49 | 50 | for k in range(m): 51 | x_train, y_train, x_test, y_test = split(x,y,k,m) 52 | z[0,k] = run(x_train, y_train, x_test, y_test, NearestCentroid()) 53 | z[1,k] = run(x_train, y_train, x_test, y_test, KNeighborsClassifier(n_neighbors=3)) 54 | z[2,k] = run(x_train, y_train, x_test, y_test, KNeighborsClassifier(n_neighbors=7)) 55 | z[3,k] = run(x_train, y_train, x_test, y_test, GaussianNB()) 56 | z[4,k] = run(x_train, y_train, x_test, y_test, DecisionTreeClassifier()) 57 | z[5,k] = run(x_train, y_train, x_test, y_test, RandomForestClassifier(n_estimators=5)) 58 | z[6,k] = run(x_train, y_train, x_test, y_test, RandomForestClassifier(n_estimators=50)) 59 | z[7,k] = run(x_train, y_train, x_test, y_test, SVC(kernel="linear", C=1.0)) 60 | 61 | pp(z,0,"Nearest"); pp(z,1,"3-NN") 62 | pp(z,2,"7-NN"); pp(z,3,"Naive Bayes") 63 | pp(z,4,"Decision Tree"); pp(z,5,"Random Forest (5)") 64 | pp(z,6,"Random Forest (50)"); pp(z,7,"SVM (linear)") 65 | 66 | main() 67 | 68 | -------------------------------------------------------------------------------- /chapter_07/bc_rbf_svm_search.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.svm import SVC 3 | 4 | def run(x_train, y_train, x_test, y_test, clf): 5 | clf.fit(x_train, y_train) 6 | return clf.score(x_test, y_test) 7 | 8 | def split(x,y,k,m): 9 | ns = int(y.shape[0]/m) 10 | s = [] 11 | for i in range(m): 12 | s.append([x[(ns*i):(ns*i+ns)], y[(ns*i):(ns*i+ns)]]) 13 | x_test, y_test = s[k] 14 | x_train = [] 15 | y_train = [] 16 | for i in range(m): 17 | if (i==k): 18 | continue 19 | else: 20 | a,b = s[i] 21 | x_train.append(a) 22 | y_train.append(b) 23 | x_train = np.array(x_train).reshape(((m-1)*ns,30)) 24 | y_train = np.array(y_train).reshape((m-1)*ns) 25 | return [x_train, y_train, x_test, y_test] 26 | 27 | def main(): 28 | m = 5 29 | x = np.load("../data/breast/bc_features_standard.npy") 30 | y = np.load("../data/breast/bc_labels.npy") 31 | idx = np.argsort(np.random.random(y.shape[0])) 32 | x = x[idx] 33 | y = y[idx] 34 | 35 | Cs = np.array([0.01,0.1,1.0,2.0,10.0,50.0,100.0]) 36 | gs = (1./30)*2.0**np.array([-4,-3,-2,-1,0,1,2,3]) 37 | zmax = 0.0 38 | for C in Cs: 39 | for g in gs: 40 | z = np.zeros(m) 41 | for k in range(m): 42 | x_train, y_train, x_test, y_test = split(x,y,k,m) 43 | z[k] = run(x_train, y_train, x_test, y_test, SVC(C=C,gamma=g,kernel="rbf")) 44 | if (z.mean() > zmax): 45 | zmax = z.mean() 46 | bestC = C 47 | bestg = g 48 | print("best C = %0.5f" % bestC) 49 | print(" gamma = %0.5f" % bestg) 50 | print(" accuracy= %0.5f" % zmax) 51 | 52 | main() 53 | 54 | -------------------------------------------------------------------------------- /chapter_07/iris_centroids.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def centroids(x,y): 4 | c0 = x[np.where(y==0)].mean(axis=0) 5 | c1 = x[np.where(y==1)].mean(axis=0) 6 | c2 = x[np.where(y==2)].mean(axis=0) 7 | return [c0,c1,c2] 8 | 9 | def predict(c0,c1,c2,x): 10 | p = np.zeros(x.shape[0], dtype="uint8") 11 | for i in range(x.shape[0]): 12 | d = [((c0-x[i])**2).sum(), 13 | ((c1-x[i])**2).sum(), 14 | ((c2-x[i])**2).sum()] 15 | p[i] = np.argmin(d) 16 | return p 17 | 18 | def main(): 19 | x = np.load("../data/iris/iris_features.npy") 20 | y = np.load("../data/iris/iris_labels.npy") 21 | N = 120 22 | x_train = x[:N]; x_test = x[N:] 23 | y_train = y[:N]; y_test = y[N:] 24 | c0, c1, c2 = centroids(x_train, y_train) 25 | p = predict(c0,c1,c2, x_test) 26 | nc = len(np.where(p == y_test)[0]) 27 | nw = len(np.where(p != y_test)[0]) 28 | acc = float(nc) / (float(nc)+float(nw)) 29 | print("predicted:", p) 30 | print("actual :", y_test) 31 | print("test accuracy = %0.4f" % acc) 32 | 33 | main() 34 | 35 | -------------------------------------------------------------------------------- /chapter_07/iris_experiments.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.neighbors.nearest_centroid import NearestCentroid 3 | from sklearn.neighbors import KNeighborsClassifier 4 | from sklearn.naive_bayes import GaussianNB, MultinomialNB 5 | from sklearn.tree import DecisionTreeClassifier 6 | from sklearn.ensemble import RandomForestClassifier 7 | from sklearn.svm import SVC 8 | 9 | def run(x_train, y_train, x_test, y_test, clf): 10 | clf.fit(x_train, y_train) 11 | print(" predictions :", clf.predict(x_test)) 12 | print(" actual labels:", y_test) 13 | print(" score = %0.4f" % clf.score(x_test, y_test)) 14 | print() 15 | 16 | def main(): 17 | x = np.load("../data/iris/iris_features.npy") 18 | y = np.load("../data/iris/iris_labels.npy") 19 | N = 120 20 | x_train = x[:N]; x_test = x[N:] 21 | y_train = y[:N]; y_test = y[N:] 22 | xa_train=np.load("../data/iris/iris_train_features_augmented.npy") 23 | ya_train=np.load("../data/iris/iris_train_labels_augmented.npy") 24 | xa_test =np.load("../data/iris/iris_test_features_augmented.npy") 25 | ya_test =np.load("../data/iris/iris_test_labels_augmented.npy") 26 | 27 | print("Nearest centroid:") 28 | run(x_train, y_train, x_test, y_test, NearestCentroid()) 29 | print("k-NN classifier (k=3):") 30 | run(x_train, y_train, x_test, y_test, KNeighborsClassifier(n_neighbors=3)) 31 | print("Naive Bayes classifier (Gaussian):") 32 | run(x_train, y_train, x_test, y_test, GaussianNB()) 33 | print("Naive Bayes classifier (Multinomial):") 34 | run(x_train, y_train, x_test, y_test, MultinomialNB()) 35 | print("Decision Tree classifier:") 36 | run(x_train, y_train, x_test, y_test, DecisionTreeClassifier()) 37 | print("Random Forest classifier (estimators=5):") 38 | run(xa_train, ya_train, xa_test, ya_test, RandomForestClassifier(n_estimators=5)) 39 | 40 | print("SVM (linear, C=1.0):") 41 | run(xa_train, ya_train, xa_test, ya_test, SVC(kernel="linear", C=1.0)) 42 | print("SVM (RBF, C=1.0, gamma=0.25):") 43 | run(xa_train, ya_train, xa_test, ya_test, SVC(kernel="rbf", C=1.0, gamma=0.25)) 44 | print("SVM (RBF, C=1.0, gamma=0.001, augmented)") 45 | run(xa_train, ya_train, xa_test, ya_test, SVC(kernel="rbf", C=1.0, gamma=0.001)) 46 | print("SVM (RBF, C=1.0, gamma=0.001, original)") 47 | run(x_train, y_train, x_test, y_test, SVC(kernel="rbf", C=1.0, gamma=0.001)) 48 | 49 | main() 50 | 51 | -------------------------------------------------------------------------------- /chapter_07/mnist_pca.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | from sklearn.naive_bayes import GaussianNB 4 | from sklearn.ensemble import RandomForestClassifier 5 | from sklearn.svm import LinearSVC 6 | from sklearn import decomposition 7 | 8 | def run(x_train, y_train, x_test, y_test, clf): 9 | s = time.time() 10 | clf.fit(x_train, y_train) 11 | e_train = time.time() - s 12 | s = time.time() 13 | score = clf.score(x_test, y_test) 14 | e_test = time.time() - s 15 | return [score, e_train, e_test] 16 | 17 | def main(): 18 | x_train = np.load("../data/mnist/mnist_train_vectors.npy").astype("float64") 19 | y_train = np.load("../data/mnist/mnist_train_labels.npy") 20 | x_test = np.load("../data/mnist/mnist_test_vectors.npy").astype("float64") 21 | y_test = np.load("../data/mnist/mnist_test_labels.npy") 22 | m = x_train.mean(axis=0) 23 | s = x_train.std(axis=0) + 1e-8 24 | x_ntrain = (x_train - m) / s 25 | x_ntest = (x_test - m) / s 26 | 27 | n = 78 28 | pcomp = np.linspace(10,780,n, dtype="int16") 29 | nb=np.zeros((n,4)) 30 | rf=np.zeros((n,4)) 31 | sv=np.zeros((n,4)) 32 | tv=np.zeros((n,2)) 33 | 34 | for i,p in enumerate(pcomp): 35 | pca = decomposition.PCA(n_components=p) 36 | pca.fit(x_ntrain) 37 | xtrain = pca.transform(x_ntrain) 38 | xtest = pca.transform(x_ntest) 39 | tv[i,:] = [p, pca.explained_variance_ratio_.sum()] 40 | sc,etrn,etst =run(xtrain, y_train, xtest, y_test, GaussianNB()) 41 | nb[i,:] = [p,sc,etrn,etst] 42 | sc,etrn,etst =run(xtrain, y_train, xtest, y_test, RandomForestClassifier(n_estimators=50)) 43 | rf[i,:] = [p,sc,etrn,etst] 44 | sc,etrn,etst =run(xtrain, y_train, xtest, y_test, LinearSVC(C=1.0)) 45 | sv[i,:] = [p,sc,etrn,etst] 46 | 47 | np.save("../data/mnist/mnist_pca_tv.npy", tv) 48 | np.save("../data/mnist/mnist_pca_nb.npy", nb) 49 | np.save("../data/mnist/mnist_pca_rf.npy", rf) 50 | np.save("../data/mnist/mnist_pca_sv.npy", sv) 51 | 52 | main() 53 | 54 | -------------------------------------------------------------------------------- /chapter_08/._chapter_08: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkneusel9/PracticalDeepLearningPython/584f08e32e31d3029feb5138adc89328457bf65f/chapter_08/._chapter_08 -------------------------------------------------------------------------------- /chapter_08/nn_iris_dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | d = np.load("../data/iris/iris_train_features_augmented.npy") 3 | l = np.load("../data/iris/iris_train_labels_augmented.npy") 4 | d1 = d[np.where(l==1)] 5 | d2 = d[np.where(l==2)] 6 | a=len(d1) 7 | b=len(d2) 8 | x = np.zeros((a+b,2)) 9 | x[:a,:] = d1[:,2:] 10 | x[a:,:] = d2[:,2:] 11 | y = np.array([0]*a+[1]*b) 12 | i = np.argsort(np.random.random(a+b)) 13 | x = x[i] 14 | y = y[i] 15 | np.save("../data/iris/iris2_train.npy", x) 16 | np.save("../data/iris/iris2_train_labels.npy", y) 17 | d = np.load("../data/iris/iris_test_features_augmented.npy") 18 | l = np.load("../data/iris/iris_test_labels_augmented.npy") 19 | d1 = d[np.where(l==1)] 20 | d2 = d[np.where(l==2)] 21 | a=len(d1) 22 | b=len(d2) 23 | x = np.zeros((a+b,2)) 24 | x[:a,:] = d1[:,2:] 25 | x[a:,:] = d2[:,2:] 26 | y = np.array([0]*a+[1]*b) 27 | i = np.argsort(np.random.random(a+b)) 28 | x = x[i] 29 | y = y[i] 30 | np.save("../data/iris/iris2_test.npy", x) 31 | np.save("../data/iris/iris2_test_labels.npy", y) 32 | 33 | -------------------------------------------------------------------------------- /chapter_08/nn_iris_evaluate.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | import sys 4 | 5 | def sigmoid(x): 6 | return 1.0 / (1.0 + np.exp(-x)) 7 | 8 | def evaluate(x, y, w): 9 | w12,b1,w23,b2,w34,b3 = w 10 | nc = nw = 0 11 | prob = np.zeros(len(y)) 12 | for i in range(len(y)): 13 | a1 = sigmoid(np.dot(x[i], w12) + b1) 14 | a2 = sigmoid(np.dot(a1, w23) + b2) 15 | prob[i] = sigmoid(np.dot(a2, w34) + b3) 16 | z = 0 if prob[i] < 0.5 else 1 17 | if (z == y[i]): 18 | nc += 1 19 | else: 20 | nw += 1 21 | return [float(nc) / float(nc + nw), prob] 22 | 23 | xtest = np.load("../data/iris/iris2_test.npy") 24 | ytest = np.load("../data/iris/iris2_test_labels.npy") 25 | weights = pickle.load(open("../data/iris/iris2_weights.pkl","rb")) 26 | score, prob = evaluate(xtest, ytest, weights) 27 | print() 28 | for i in range(len(prob)): 29 | print("%3d: actual: %d predict: %d prob: %0.7f" % (i, ytest[i], 0 if (prob[i] < 0.5) else 1, prob[i])) 30 | print("Score = %0.4f" % score) 31 | 32 | -------------------------------------------------------------------------------- /chapter_08/nn_iris_mlpclassifier.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pickle 3 | from sklearn.neural_network import MLPClassifier 4 | 5 | xtrain= np.load("../data/iris/iris2_train.npy") 6 | ytrain= np.load("../data/iris/iris2_train_labels.npy") 7 | xtest = np.load("../data/iris/iris2_test.npy") 8 | ytest = np.load("../data/iris/iris2_test_labels.npy") 9 | 10 | clf = MLPClassifier( 11 | hidden_layer_sizes=(3,2), 12 | activation="logistic", 13 | solver="adam", tol=1e-9, 14 | max_iter=5000, 15 | verbose=True) 16 | clf.fit(xtrain, ytrain) 17 | prob = clf.predict_proba(xtest) 18 | score = clf.score(xtest, ytest) 19 | 20 | w12 = clf.coefs_[0] 21 | w23 = clf.coefs_[1] 22 | w34 = clf.coefs_[2] 23 | b1 = clf.intercepts_[0] 24 | b2 = clf.intercepts_[1] 25 | b3 = clf.intercepts_[2] 26 | weights = [w12,b1,w23,b2,w34,b3] 27 | pickle.dump(weights, open("../data/iris/iris2_weights.pkl","wb")) 28 | 29 | print() 30 | print("Test results:") 31 | print(" Overall score: %0.7f" % score) 32 | print() 33 | for i in range(len(ytest)): 34 | p = 0 if (prob[i,1] < 0.5) else 1 35 | print("%03d: %d - %d, %0.7f" % (i, ytest[i], p, prob[i,1])) 36 | print() 37 | 38 | -------------------------------------------------------------------------------- /chapter_10/._chapter_10: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkneusel9/PracticalDeepLearningPython/584f08e32e31d3029feb5138adc89328457bf65f/chapter_10/._chapter_10 -------------------------------------------------------------------------------- /chapter_10/README.txt: -------------------------------------------------------------------------------- 1 | Code for Ch 10 2 | -------------- 3 | 4 | 'mnist_nn_experiments.py' is direct from the text of the 5 | book. 6 | 7 | The remaining files are the different experiment versions that 8 | are not called out explicitly in the text. Not all of these 9 | were necessarily used in the text, but they will help people 10 | who want to experiment. 11 | 12 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import time 3 | from sklearn.neural_network import MLPClassifier 4 | 5 | def run(x_train, y_train, x_test, y_test, clf): 6 | s = time.time() 7 | clf.fit(x_train, y_train) 8 | e = time.time()-s 9 | loss = clf.loss_ 10 | weights = clf.coefs_ 11 | biases = clf.intercepts_ 12 | params = 0 13 | for w in weights: 14 | params += w.shape[0]*w.shape[1] 15 | for b in biases: 16 | params += b.shape[0] 17 | return [clf.score(x_test, y_test), loss, params, e] 18 | 19 | def nn(layers, act): 20 | return MLPClassifier(solver="sgd", verbose=False, tol=1e-8, 21 | nesterovs_momentum=False, early_stopping=False, 22 | learning_rate_init=0.001, momentum=0.9, max_iter=200, 23 | hidden_layer_sizes=layers, activation=act) 24 | 25 | def main(): 26 | x_train = np.load("../data/mnist/mnist_train_vectors.npy").astype("float64")/256.0 27 | y_train = np.load("../data/mnist/mnist_train_labels.npy") 28 | x_test = np.load("../data/mnist/mnist_test_vectors.npy").astype("float64")/256.0 29 | y_test = np.load("../data/mnist/mnist_test_labels.npy") 30 | 31 | N = 1000 32 | x_train = x_train[:N] 33 | y_train = y_train[:N] 34 | x_test = x_test[:N] 35 | y_test = y_test[:N] 36 | 37 | layers = [ 38 | (1,), (500,), (800,), (1000,), (2000,), (3000,), 39 | (1000,500), (3000,1500), 40 | (2,2,2), (1000,500,250), (2000,1000,500), 41 | ] 42 | 43 | for act in ["relu", "logistic", "tanh"]: 44 | print("%s:" % act) 45 | for layer in layers: 46 | scores = [] 47 | loss = [] 48 | tm = [] 49 | for i in range(10): 50 | s,l,params,e = run(x_train, y_train, x_test, y_test, nn(layer,act)) 51 | scores.append(s) 52 | loss.append(l) 53 | tm.append(e) 54 | s = np.array(scores) 55 | l = np.array(loss) 56 | t = np.array(tm) 57 | n = np.sqrt(s.shape[0]) 58 | print(" layers: %14s, score= %0.4f +/- %0.4f, loss = %0.4f +/- %0.4f (params = %6d, time = %0.2f s)" % \ 59 | (str(layer), s.mean(), s.std()/n, l.mean(), l.std()/n, params, t.mean())) 60 | 61 | main() 62 | 63 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_L2_plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pylab as plt 3 | 4 | def main(): 5 | d0 = np.load("mnist_nn_experiments_L2/val_error_0.000000.npy") 6 | d1 = np.load("mnist_nn_experiments_L2/val_error_0.100000.npy") 7 | d2 = np.load("mnist_nn_experiments_L2/val_error_0.200000.npy") 8 | d3 = np.load("mnist_nn_experiments_L2/val_error_0.300000.npy") 9 | d4 = np.load("mnist_nn_experiments_L2/val_error_0.400000.npy") 10 | 11 | plt.plot(d0, color="k", linewidth=1, linestyle="-") 12 | plt.plot(d1, color="r", linewidth=1, linestyle="-") 13 | plt.plot(d2, color="g", linewidth=1, linestyle="-") 14 | plt.plot(d3, color="b", linewidth=1, linestyle="-") 15 | plt.plot(d4, color="c", linewidth=1, linestyle="-") 16 | plt.xlabel("Epochs") 17 | plt.ylabel("Validation Error") 18 | plt.ylim((0.05,0.1)) 19 | plt.tight_layout() 20 | plt.savefig("mnist_nn_experiments_L2_plot.pdf", type="pdf", dpi=600) 21 | plt.show() 22 | 23 | 24 | main() 25 | 26 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_base_lr.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_nn_experiments_base_lr.py 3 | # 4 | # Reduced MNIST + NN for Chapter 6. 5 | # 6 | # RTK, 15-Oct-2018 7 | # Last update: 15-Oct-2018 8 | # 9 | ############################################################### 10 | 11 | import numpy as np 12 | import time 13 | from sklearn.neural_network import MLPClassifier 14 | 15 | 16 | def run(x_train, y_train, x_test, y_test, clf): 17 | """Train and test""" 18 | 19 | s = time.time() 20 | clf.fit(x_train, y_train) 21 | e = time.time()-s 22 | loss = clf.loss_ 23 | return [clf.score(x_test, y_test), loss, e] 24 | 25 | 26 | def nn(base_lr, epochs): 27 | """Initialize a network""" 28 | 29 | return MLPClassifier(solver="sgd", verbose=False, tol=1e-8, 30 | nesterovs_momentum=False, early_stopping=False, 31 | learning_rate_init=base_lr, momentum=0.9, max_iter=epochs, 32 | hidden_layer_sizes=(1000,500), activation="relu", 33 | learning_rate="constant", batch_size=64) 34 | 35 | 36 | def main(): 37 | """Run the experiments for the MNIST vector data""" 38 | 39 | # Vector MNIST versions scaled [0,1) 40 | x_train = np.load("../data/mnist/mnist_train_vectors.npy").astype("float64")/256.0 41 | y_train = np.load("../data/mnist/mnist_train_labels.npy") 42 | x_test = np.load("../data/mnist/mnist_test_vectors.npy").astype("float64")/256.0 43 | y_test = np.load("../data/mnist/mnist_test_labels.npy") 44 | 45 | # training set samples 46 | N = 20000 47 | x = x_train[:N] 48 | y = y_train[:N] 49 | xt= x_test[:N] 50 | yt= y_test[:N] 51 | 52 | base_lr = [0.2,0.1,0.05,0.01,0.005,0.001,0.0005,0.0001] 53 | 54 | for lr in base_lr: 55 | s,l,e = run(x, y, xt, yt, nn(lr,50)) 56 | print("base_lr = %0.5f, score = %0.5f, loss = %0.5f, epochs = %d" % (lr,s,l,50)) 57 | print() 58 | 59 | # choose epochs so base_lr * epochs == 1.5 60 | epochs = [8, 15, 30, 150, 300, 1500, 3000, 15000] 61 | 62 | for i in range(len(base_lr)): 63 | s,l,e = run(x, y, xt, yt, nn(base_lr[i], epochs[i])) 64 | print("base_lr = %0.5f, score = %0.5f, loss = %0.5f, epochs = %d, time = %0.3f" % (base_lr[i],s,l,epochs[i],e)) 65 | print() 66 | 67 | main() 68 | 69 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_base_lr_plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pylab as plt 3 | 4 | def main(): 5 | # score by base_lr, fixed epochs 6 | sc0 = np.array([0.91870,0.95070,0.96050,0.97120,0.97260,0.97540,0.97630,0.94800]) 7 | lr = np.array([0.00010,0.00050,0.00100,0.00500,0.01000,0.05000,0.10000,0.20000]) 8 | 9 | # score by base_lr, lr * epochs = 1.5 10 | sc1 = np.array([0.96990,0.97030,0.97060,0.97240,0.97310,0.97590,0.97340,0.95550]) 11 | 12 | plt.semilogx(lr,sc0,marker='o',color='red') 13 | plt.semilogx(lr,sc1,marker='s',color='blue') 14 | plt.xlabel("Learning rate ($\eta$)") 15 | plt.ylabel("Test Score") 16 | plt.tight_layout() 17 | plt.savefig("mnist_nn_experiments_base_lr_plot.pdf", format="pdf", dpi=600) 18 | plt.show() 19 | 20 | main() 21 | 22 | 23 | #base_lr = 0.20000, score = 0.94800, loss = 0.09340, epochs = 50 24 | #base_lr = 0.10000, score = 0.97630, loss = 0.00207, epochs = 50 25 | #base_lr = 0.05000, score = 0.97540, loss = 0.00162, epochs = 50 26 | #base_lr = 0.01000, score = 0.97260, loss = 0.00229, epochs = 50 27 | #base_lr = 0.00500, score = 0.97120, loss = 0.00413, epochs = 50 28 | #base_lr = 0.00100, score = 0.96050, loss = 0.06542, epochs = 50 29 | #base_lr = 0.00050, score = 0.95070, loss = 0.13361, epochs = 50 30 | #base_lr = 0.00010, score = 0.91870, loss = 0.29111, epochs = 50 31 | # 32 | #base_lr = 0.20000, score = 0.95550, loss = 0.07414, epochs = 8, time = 71.445 33 | #base_lr = 0.10000, score = 0.97340, loss = 0.00946, epochs = 15, time = 132.122 34 | #base_lr = 0.05000, score = 0.97590, loss = 0.00168, epochs = 30, time = 268.132 35 | #base_lr = 0.01000, score = 0.97310, loss = 0.00163, epochs = 150, time = 1385.001 36 | #base_lr = 0.00500, score = 0.97240, loss = 0.00163, epochs = 300, time = 2741.335 37 | #base_lr = 0.00100, score = 0.97060, loss = 0.00163, epochs = 1500, time = 13232.182 38 | #base_lr = 0.00050, score = 0.97030, loss = 0.00163, epochs = 3000, time = 26478.357 39 | #base_lr = 0.00010, score = 0.96990, loss = 0.00162, epochs = 15000, time = 135642.231 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_batch_size_plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pylab as plt 3 | 4 | def main(): 5 | # epochs == 100 6 | bz = np.array([2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384]) 7 | sc = np.array([0.97174,0.96976,0.96876,0.96734,0.96596,0.96332,0.95684,0.94564,0.93312,0.91788,0.90046,0.87318,0.82926,0.76104]) 8 | ec = np.array([0.00006,0.00034,0.00007,0.00012,0.00011,0.00040,0.00042,0.00040,0.00038,0.00040,0.00078,0.00086,0.00203,0.00591]) 9 | 10 | # minibatches = 8192 11 | sc0= np.array([0.93658,0.94556,0.94856,0.94916,0.95012,0.94946,0.95068,0.95038,0.95112,0.95030,0.95066,0.95028,0.94992,0.94994]) 12 | ec0= np.array([0.00214,0.00078,0.00070,0.00115,0.00025,0.00028,0.00053,0.00041,0.00045,0.00023,0.00032,0.00058,0.00044,0.00022]) 13 | 14 | plt.errorbar(bz,sc,ec,marker='o',color='red') 15 | plt.errorbar(bz,sc0,ec0,marker='s',color='blue') 16 | plt.xlabel("Minibatch Size") 17 | plt.ylabel("Mean Score") 18 | plt.tight_layout() 19 | plt.savefig("mnist_nn_experiments_batch_size_plot.pdf", format="pdf", dpi=600) 20 | plt.show() 21 | 22 | 23 | main() 24 | 25 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_init_plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pylab as plt 3 | 4 | def smooth(x,window_len=11,window='hanning'): 5 | if x.ndim != 1: 6 | raise ValueError("smooth only accepts 1 dimension arrays.") 7 | 8 | if x.size < window_len: 9 | raise ValueError("Input vector needs to be bigger than window size.") 10 | 11 | if window_len<3: 12 | return x 13 | 14 | if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']: 15 | raise ValueError("Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'") 16 | 17 | s=np.r_[x[window_len-1:0:-1],x,x[-2:-window_len-1:-1]] 18 | if window == 'flat': #moving average 19 | w=np.ones(window_len,'d') 20 | else: 21 | w=eval('np.'+window+'(window_len)') 22 | y=np.convolve(w/w.sum(),s,mode='valid') 23 | return y 24 | 25 | 26 | def main(): 27 | d = np.load("mnist_nn_experiments_init_results.npy") 28 | d0 = d[:,0,:].mean(axis=0) 29 | d1 = d[:,1,:].mean(axis=0) 30 | d2 = d[:,2,:].mean(axis=0) 31 | d3 = d[:,3,:].mean(axis=0) 32 | d4 = d[:,4,:].mean(axis=0) 33 | 34 | plt.plot(smooth(d0,53,"flat"), color="k", linewidth=2, linestyle="-") 35 | plt.plot(smooth(d1,53,"flat"), color="r", linewidth=1, linestyle="-") 36 | plt.plot(smooth(d2,53,"flat"), color="g", linewidth=1, linestyle="-") 37 | plt.plot(smooth(d3,53,"flat"), color="b", linewidth=1, linestyle="-") 38 | plt.plot(smooth(d4,53,"flat"), color="c", linewidth=1, linestyle="-") 39 | plt.xlabel("Epochs", fontsize=16) 40 | plt.ylabel("Test Error", fontsize=16) 41 | plt.ylim((0.04,0.055)) 42 | plt.xlim((75,4000)) 43 | plt.tight_layout() 44 | plt.savefig("mnist_nn_experiments_init_plot.png", type="png", dpi=600) 45 | plt.savefig("mnist_nn_experiments_init_plot.pdf", type="pdf", dpi=600) 46 | plt.show() 47 | 48 | 49 | main() 50 | 51 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_momentum_plot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pylab as plt 3 | 4 | def main(): 5 | d0 = np.load("mnist_nn_experiments_momentum/val_error_0.00.npy") 6 | d1 = np.load("mnist_nn_experiments_momentum/val_error_0.30.npy") 7 | d2 = np.load("mnist_nn_experiments_momentum/val_error_0.50.npy") 8 | d3 = np.load("mnist_nn_experiments_momentum/val_error_0.70.npy") 9 | d4 = np.load("mnist_nn_experiments_momentum/val_error_0.90.npy") 10 | d5 = np.load("mnist_nn_experiments_momentum/val_error_0.99.npy") 11 | 12 | plt.plot(d0, color="k", linewidth=1, linestyle="-") 13 | plt.plot(d1, color="r", linewidth=1, linestyle="-") 14 | plt.plot(d2, color="g", linewidth=1, linestyle="-") 15 | plt.plot(d3, color="b", linewidth=1, linestyle="-") 16 | plt.plot(d4, color="c", linewidth=1, linestyle="-") 17 | plt.plot(d5, color="m", linewidth=1, linestyle="-") 18 | plt.xlabel("Epochs") 19 | plt.ylabel("Test Error") 20 | plt.ylim((0.05,0.1)) 21 | plt.tight_layout() 22 | plt.savefig("mnist_nn_experiments_momentum_plot.pdf", type="pdf", dpi=600) 23 | plt.savefig("mnist_nn_experiments_momentum_plot.png", type="png", dpi=600) 24 | plt.show() 25 | 26 | 27 | main() 28 | 29 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_retrain.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_nn_experiments_retrain.py 3 | # 4 | # Reduced MNIST + NN for Chapter 6. 5 | # 6 | # RTK, 15-Oct-2018 7 | # Last update: 15-Oct-2018 8 | # 9 | ############################################################### 10 | 11 | import numpy as np 12 | import time 13 | from sklearn.neural_network import MLPClassifier 14 | 15 | 16 | def run(x_train, y_train, x_test, y_test, clf): 17 | """Train and test""" 18 | 19 | s = time.time() 20 | clf.fit(x_train, y_train) 21 | e = time.time()-s 22 | loss = clf.loss_ 23 | return [clf.score(x_test, y_test), loss, e] 24 | 25 | 26 | def nn(): 27 | """Initialize a network""" 28 | 29 | return MLPClassifier(solver="sgd", verbose=False, tol=1e-8, 30 | nesterovs_momentum=False, early_stopping=False, 31 | learning_rate_init=0.001, momentum=0.9, max_iter=50, 32 | hidden_layer_sizes=(1000,500), activation="relu", 33 | batch_size=64) 34 | 35 | 36 | def main(): 37 | """Run the experiments for the iris data""" 38 | 39 | # Vector MNIST versions scaled [0,1) 40 | x_train = np.load("../data/mnist/mnist_train_vectors.npy").astype("float64")/256.0 41 | y_train = np.load("../data/mnist/mnist_train_labels.npy") 42 | x_test = np.load("../data/mnist/mnist_test_vectors.npy").astype("float64")/256.0 43 | y_test = np.load("../data/mnist/mnist_test_labels.npy") 44 | 45 | # training set samples 46 | N = 20000 47 | x = x_train[:N] 48 | y = y_train[:N] 49 | xt= x_test[:N] 50 | yt= y_test[:N] 51 | 52 | M = 20 53 | scores = np.zeros(M) 54 | losses = np.zeros(M) 55 | for i in range(M): 56 | s,l,e = run(x, y, xt, yt, nn()) 57 | print("%03i: score = %0.5f, loss = %0.5f" % (i,s,l)) 58 | scores[i] = s 59 | losses[i] = l 60 | 61 | print() 62 | print("Scores: min, max, mean+/-SE: %0.5f, %0.5f, %0.5f +/- %0.5f" % \ 63 | (scores.min(), scores.max(), scores.mean(), scores.std()/np.sqrt(scores.shape[0]))) 64 | print("Loss : min, max, mean+/-SE: %0.5f, %0.5f, %0.5f +/- %0.5f" % \ 65 | (losses.min(), losses.max(), losses.mean(), losses.std()/np.sqrt(losses.shape[0]))) 66 | print() 67 | 68 | 69 | main() 70 | 71 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_sample_plot.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pylab as plt 2 | 3 | def main(): 4 | x = [100, 200, 300, 400, 500, 600,700, 800, 900, 1000, 5 | 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 7500, 6 | 10000, 15000, 20000, 25000, 30000] 7 | y = [0.75330, 0.81608, 0.85652, 0.86402, 0.87372, 0.87764, 8 | 0.88382, 0.89334, 0.89644, 0.90168, 0.91856, 0.92490, 9 | 0.92966, 0.93690, 0.94190, 0.94424, 0.94964, 0.95032, 10 | 0.95862, 0.96526, 0.96664, 0.96944, 0.96980, 0.96926] 11 | 12 | plt.plot(x,y, marker="o", color="b") 13 | plt.xlabel("Number of training samples") 14 | plt.ylabel("Mean test score") 15 | plt.tight_layout() 16 | plt.savefig("mnist_nn_experiments_samples_plot.pdf", type="pdf", dpi=600) 17 | plt.show() 18 | 19 | 20 | main() 21 | 22 | -------------------------------------------------------------------------------- /chapter_10/mnist_nn_experiments_samples.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_nn_experiments_samples.py 3 | # 4 | # Reduced MNIST + NN for Chapter 6. 5 | # 6 | # RTK, 15-Oct-2018 7 | # Last update: 15-Oct-2018 8 | # 9 | ############################################################### 10 | 11 | import numpy as np 12 | import time 13 | from sklearn.neural_network import MLPClassifier 14 | 15 | 16 | def run(x_train, y_train, x_test, y_test, clf): 17 | """Train and test""" 18 | 19 | s = time.time() 20 | clf.fit(x_train, y_train) 21 | e = time.time()-s 22 | loss = clf.loss_ 23 | return [clf.score(x_test, y_test), loss, e] 24 | 25 | 26 | def nn(epochs): 27 | """Initialize a network""" 28 | 29 | return MLPClassifier(solver="sgd", verbose=False, tol=1e-8, 30 | nesterovs_momentum=False, early_stopping=False, 31 | learning_rate_init=0.05, momentum=0.9, max_iter=epochs, 32 | hidden_layer_sizes=(1000,500), activation="relu", 33 | learning_rate="constant", batch_size=100) 34 | 35 | 36 | def main(): 37 | """Run the experiments for the MNIST vector data""" 38 | 39 | # Vector MNIST versions scaled [0,1) 40 | x_train = np.load("../data/mnist/mnist_train_vectors.npy").astype("float64")/256.0 41 | y_train = np.load("../data/mnist/mnist_train_labels.npy") 42 | x_test = np.load("../data/mnist/mnist_test_vectors.npy").astype("float64")/256.0 43 | y_test = np.load("../data/mnist/mnist_test_labels.npy") 44 | 45 | # training set samples 46 | N = [100, 200, 300, 400, 500, 600, 47 | 700, 800, 900, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 48 | 4500, 5000, 7500, 10000, 15000, 20000, 25000, 30000] 49 | M = 5 50 | 51 | for n in N: 52 | scores = np.zeros(M) 53 | print("samples = %5d" % n) 54 | for i in range(M): 55 | idx = np.argsort(np.random.random(y_train.shape[0])) 56 | x_train = x_train[idx] 57 | y_train = y_train[idx] 58 | x = x_train[:n] 59 | y = y_train[:n] 60 | epochs = int((100.0/n)*1000) # epochs to take 1,000 SGD steps 61 | s,l,e = run(x, y, x_test, y_test, nn(epochs)) 62 | scores[i] = s 63 | print(" score = %0.5f, loss = %0.5f, epochs = %d, training time = %0.3f" % (s,l,epochs,e)) 64 | print(" mean score = %0.5f +/- %0.5f" % (scores.mean(), scores.std()/np.sqrt(M))) 65 | print() 66 | print() 67 | 68 | 69 | main() 70 | 71 | -------------------------------------------------------------------------------- /chapter_11/advanced_metrics.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | def advanced_metrics(tally, m): 3 | tp, tn, fp, fn, _ = tally 4 | n = tp+tn+fp+fn 5 | po = (tp+tn)/n 6 | pe = (tp+fn)*(tp+fp)/n**2 + (tn+fp)*(tn+fn)/n**2 7 | 8 | return { 9 | "F1": 2.0*m["PPV"]*m["TPR"] / (m["PPV"] + m["TPR"]), 10 | "MCC": (tp*tn - fp*fn) / sqrt((tp+fp)*(tp+fn)*(tn+fp)*(tn+fn)), 11 | "kappa": (po - pe) / (1.0 - pe), 12 | "informedness": m["TPR"] + m["TNR"] - 1.0, 13 | "markedness": m["PPV"] + m["NPV"] - 1.0 14 | } 15 | 16 | -------------------------------------------------------------------------------- /chapter_11/basic_metrics.py: -------------------------------------------------------------------------------- 1 | def basic_metrics(tally): 2 | tp, tn, fp, fn, _ = tally 3 | return { 4 | "TPR": tp / (tp + fn), 5 | "TNR": tn / (tn + fp), 6 | "PPV": tp / (tp + fp), 7 | "NPV": tn / (tn + fn), 8 | "FPR": fp / (fp + tn), 9 | "FNR": fn / (fn + tp) 10 | } 11 | 12 | -------------------------------------------------------------------------------- /chapter_11/bc_experiments.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: bc_experiments.py 3 | # 4 | # Different breast cancer dataset models for Chapter 4. 5 | # 6 | # RTK, 02-Jun-2018 7 | # Last update: 02-Jun-2018 8 | # 9 | ############################################################### 10 | 11 | import numpy as np 12 | from sklearn.neighbors import NearestCentroid 13 | from sklearn.neighbors import KNeighborsClassifier 14 | from sklearn.naive_bayes import GaussianNB, MultinomialNB 15 | from sklearn.tree import DecisionTreeClassifier 16 | from sklearn.ensemble import RandomForestClassifier 17 | from sklearn.svm import SVC 18 | 19 | def run(x_train, y_train, x_test, y_test, clf): 20 | """Train and test""" 21 | 22 | clf.fit(x_train, y_train) 23 | print(" score = %0.4f" % clf.score(x_test, y_test)) 24 | print() 25 | 26 | 27 | def main(): 28 | """Run the experiments for the iris data""" 29 | 30 | # Load the original data and build a train/test split 31 | x = np.load("../data/breast/bc_features_standard.npy") 32 | y = np.load("../data/breast/bc_labels.npy") 33 | 34 | np.random.seed(12345) 35 | idx = np.argsort(np.random.random(y.shape[0])) 36 | x = x[idx] 37 | y = y[idx] 38 | np.random.seed() 39 | 40 | N = 455 41 | x_train = x[:N]; x_test = x[N:] 42 | y_train = y[:N]; y_test = y[N:] 43 | 44 | # Nearest centroid 45 | print("Nearest centroid:") 46 | run(x_train, y_train, x_test, y_test, NearestCentroid()) 47 | 48 | # k-NN 49 | print("k-NN classifier (k=3):") 50 | run(x_train, y_train, x_test, y_test, KNeighborsClassifier(n_neighbors=3)) 51 | print("k-NN classifier (k=7):") 52 | run(x_train, y_train, x_test, y_test, KNeighborsClassifier(n_neighbors=7)) 53 | 54 | # Naive Bayes 55 | print("Naive Bayes classifier (Gaussian):") 56 | run(x_train, y_train, x_test, y_test, GaussianNB()) 57 | 58 | # Decision tree 59 | print("Decision Tree classifier:") 60 | run(x_train, y_train, x_test, y_test, DecisionTreeClassifier()) 61 | 62 | # Random forest 63 | print("Random Forest classifier (estimators=5):") 64 | run(x_train, y_train, x_test, y_test, RandomForestClassifier(n_estimators=5)) 65 | print("Random Forest classifier (estimators=50):") 66 | run(x_train, y_train, x_test, y_test, RandomForestClassifier(n_estimators=50)) 67 | 68 | # SVM - linear 69 | print("SVM (linear, C=1.0):") 70 | run(x_train, y_train, x_test, y_test, SVC(kernel="linear", C=1.0)) 71 | 72 | # SVM - RBF - gamma = 1/30 = 0.03333 73 | print("SVM (RBF, C=1.0, gamma=0.03333):") 74 | run(x_train, y_train, x_test, y_test, SVC(kernel="rbf", C=1.0, gamma=0.03333)) 75 | 76 | 77 | main() 78 | 79 | -------------------------------------------------------------------------------- /chapter_11/confusion_matrix.py: -------------------------------------------------------------------------------- 1 | def confusion_matrix(y_test, y_predict, n=10): 2 | cmat = np.zeros((n,n), dtype="uint32") 3 | for i,y in enumerate(y_test): 4 | cmat[y, y_predict[i]] += 1 5 | return cmat 6 | 7 | 8 | def weighted_mean_acc(cmat): 9 | N = cmat.sum() 10 | C = cmat.sum(axis=1) 11 | return ((C/N)*(100*np.diag(cmat)/C)).sum() 12 | 13 | -------------------------------------------------------------------------------- /chapter_11/make_roc_points_sklearn.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: make_roc_points_sklearn.py 3 | # 4 | # Generate the ROC curve points for the 5 | # given set of output probabilities. 6 | # 7 | # RTK, 18-Apr-2019 8 | # Last update: 21-Apr-2019 9 | # 10 | ############################################################### 11 | 12 | import os 13 | import sys 14 | import numpy as np 15 | import matplotlib.pylab as plt 16 | 17 | from sklearn.metrics import roc_auc_score, roc_curve 18 | 19 | def main(): 20 | if (len(sys.argv) == 1): 21 | print() 22 | print("make_roc_points_sklearn ") 23 | print() 24 | print(" - test labels (.npy)") 25 | print(" - test per class probabilities (.npy)") 26 | print(" - output ROC plot (.png)") 27 | print() 28 | return 29 | 30 | labels = np.load(sys.argv[1]) 31 | probs = np.load(sys.argv[2]) 32 | pname = sys.argv[3] 33 | 34 | auc = roc_auc_score(labels, probs[:,1]) 35 | roc = roc_curve(labels, probs[:,1]) 36 | print("AUC = %0.6f" % auc) 37 | print() 38 | 39 | plt.plot(roc[0], roc[1], color='r') 40 | plt.plot([0,1],[0,1], color='k', linestyle=':') 41 | plt.xlabel("FPR") 42 | plt.ylabel("TPR") 43 | plt.tight_layout(pad=0, w_pad=0, h_pad=0) 44 | plt.savefig(pname, dpi=300) 45 | plt.show() 46 | 47 | 48 | main() 49 | 50 | -------------------------------------------------------------------------------- /chapter_11/mnist_even_odd.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_even_odd.py 3 | # 4 | # Reduced MNIST + NN for Chapter 12. 5 | # 6 | # RTK, 12-Apr-2019 7 | # Last update: 12-Apr-2019 8 | # 9 | ############################################################### 10 | 11 | import numpy as np 12 | import time 13 | from sklearn.neural_network import MLPClassifier 14 | 15 | 16 | def run(x_train, y_train, x_test, y_test, clf): 17 | """Train and test""" 18 | 19 | s = time.time() 20 | clf.fit(x_train, y_train) 21 | e = time.time()-s 22 | loss = clf.loss_ 23 | score = clf.score(x_test, y_test) 24 | prob = clf.predict_proba(x_test) 25 | 26 | return [score, loss, prob, e] 27 | 28 | 29 | def nn(layers): 30 | """Initialize a network""" 31 | 32 | return MLPClassifier(solver="sgd", verbose=False, tol=1e-8, 33 | nesterovs_momentum=False, early_stopping=False, 34 | batch_size=64, 35 | learning_rate_init=0.001, momentum=0.9, max_iter=200, 36 | hidden_layer_sizes=layers, activation="relu") 37 | 38 | 39 | def main(): 40 | """Run the experiments for the MNIST data""" 41 | 42 | # Vector MNIST versions scaled [0,1) 43 | x_train = np.load("../data/mnist/mnist_train_vectors.npy").astype("float64")/256.0 44 | y_train = np.load("../data/mnist/mnist_train_even_odd_labels.npy") 45 | x_test = np.load("../data/mnist/mnist_test_vectors.npy").astype("float64")/256.0 46 | y_test = np.load("../data/mnist/mnist_test_even_odd_labels.npy") 47 | 48 | # Reduce the size of the train dataset 49 | N = 1000 50 | x_train = x_train[:N] 51 | y_train = y_train[:N] 52 | 53 | layers = [(2,), (100,), (100,50), (500,250)] 54 | mlayers = ["2", "100", "100x50", "500x250"] 55 | 56 | for i,layer in enumerate(layers): 57 | score,loss,prob,tm = run(x_train, y_train, x_test, y_test, nn(layer)) 58 | print("layers: %s, score= %0.6f, loss = %0.6f (time = %0.2f s)" % \ 59 | (mlayers[i], score, loss, tm)) 60 | np.save("mnist_even_odd_probs_%s.npy" % mlayers[i], prob) 61 | print() 62 | 63 | 64 | main() 65 | 66 | -------------------------------------------------------------------------------- /chapter_11/mnist_even_odd_labels.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def main(): 4 | old = np.load("../data/mnist/mnist_train_labels.npy") 5 | new = np.zeros(len(old), dtype="uint8") 6 | new[np.where((old % 2) == 0)] = 0 7 | new[np.where((old % 2) == 1)] = 1 8 | np.save("../data/mnist/mnist_train_even_odd_labels.npy", new) 9 | 10 | old = np.load("../data/mnist/mnist_test_labels.npy") 11 | new = np.zeros(len(old), dtype="uint8") 12 | new[np.where((old % 2) == 0)] = 0 13 | new[np.where((old % 2) == 1)] = 1 14 | np.save("../data/mnist/mnist_test_even_odd_labels.npy", new) 15 | 16 | 17 | main() 18 | 19 | -------------------------------------------------------------------------------- /chapter_11/tally_predictions.py: -------------------------------------------------------------------------------- 1 | def tally_predictions(clf, x, y): 2 | p = clf.predict(x) 3 | score = clf.score(x,y) 4 | tp = tn = fp = fn = 0 5 | for i in range(len(y)): 6 | if (p[i] == 0) and (y[i] == 0): 7 | tn += 1 8 | elif (p[i] == 0) and (y[i] == 1): 9 | fn += 1 10 | elif (p[i] == 1) and (y[i] == 0): 11 | fp += 1 12 | else: 13 | tp += 1 14 | return [tp, tn, fp, fn, score] 15 | 16 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp0.py: -------------------------------------------------------------------------------- 1 | import keras 2 | from keras.datasets import mnist 3 | from keras.models import Sequential 4 | from keras.layers import Dense, Dropout, Flatten 5 | from keras.layers import Conv2D, MaxPooling2D 6 | from keras import backend as K 7 | 8 | batch_size = 128 9 | num_classes = 10 10 | epochs = 12 11 | img_rows, img_cols = 28, 28 12 | 13 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 14 | 15 | if K.image_data_format() == 'channels_first': 16 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 17 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 18 | input_shape = (1, img_rows, img_cols) 19 | else: 20 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 21 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 22 | input_shape = (img_rows, img_cols, 1) 23 | 24 | x_train = x_train.astype('float32') 25 | x_test = x_test.astype('float32') 26 | x_train /= 255 27 | x_test /= 255 28 | 29 | y_train = keras.utils.to_categorical(y_train, num_classes) 30 | y_test = keras.utils.to_categorical(y_test, num_classes) 31 | 32 | model = Sequential() 33 | model.add(Conv2D(32, kernel_size=(3, 3), 34 | activation='relu', 35 | input_shape=input_shape)) 36 | model.add(Conv2D(64, (3, 3), activation='relu')) 37 | model.add(MaxPooling2D(pool_size=(2, 2))) 38 | model.add(Dropout(0.25)) 39 | model.add(Flatten()) 40 | model.add(Dense(128, activation='relu')) 41 | model.add(Dropout(0.5)) 42 | model.add(Dense(num_classes, activation='softmax')) 43 | 44 | model.compile(loss=keras.losses.categorical_crossentropy, 45 | optimizer=keras.optimizers.Adadelta(), 46 | metrics=['accuracy']) 47 | 48 | print("Model parameters = %d" % model.count_params()) 49 | print(model.summary()) 50 | 51 | history = model.fit(x_train, y_train, 52 | batch_size=batch_size, 53 | epochs=epochs, 54 | verbose=1, 55 | validation_data=(x_test, y_test)) 56 | score = model.evaluate(x_test, y_test, verbose=0) 57 | print('Test loss:', score[0]) 58 | print('Test accuracy:', score[1]) 59 | model.save("mnist_cnn_exp0_model.h5") 60 | 61 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp1.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | 35 | # convert class vectors to binary class matrices 36 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 37 | y_test = keras.utils.to_categorical(y_test, num_classes) 38 | 39 | model = Sequential() 40 | model.add(Conv2D(32, kernel_size=(3, 3), 41 | activation='relu', 42 | input_shape=input_shape)) 43 | model.add(Conv2D(64, (3, 3), activation='relu')) 44 | model.add(Conv2D(128, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | model.fit(x_train, y_train, 63 | batch_size=batch_size, 64 | epochs=epochs, 65 | verbose=1, 66 | validation_data=(x_test[:1000], y_test[:1000])) 67 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 68 | print('Test loss:', score[0]) 69 | print('Test accuracy:', score[1]) 70 | 71 | # Save for later use 72 | model.save("mnist_cnn_exp1_model.h5") 73 | 74 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp10.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | print('x_train shape:', x_train.shape) 35 | print(x_train.shape[0], 'train samples') 36 | print(x_test.shape[0], 'test samples') 37 | 38 | # convert class vectors to binary class matrices 39 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 40 | y_test = keras.utils.to_categorical(y_test, num_classes) 41 | 42 | model = Sequential() 43 | model.add(Conv2D(32, kernel_size=(3, 3), 44 | activation='relu', 45 | input_shape=input_shape)) 46 | #model.add(Conv2D(64, (3, 3), activation='relu')) 47 | model.add(MaxPooling2D(pool_size=(2, 2))) 48 | model.add(Dropout(0.25)) 49 | model.add(Flatten()) 50 | model.add(Dense(128, activation='relu')) 51 | model.add(Dropout(0.5)) 52 | model.add(Dense(num_classes, activation='softmax')) 53 | 54 | model.compile(loss=keras.losses.categorical_crossentropy, 55 | optimizer=keras.optimizers.Adadelta(), 56 | metrics=['accuracy']) 57 | 58 | print() 59 | print("Model parameters = %d" % model.count_params()) 60 | print() 61 | print(model.summary()) 62 | print() 63 | 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | 73 | # Save for later use 74 | model.save("mnist_cnn_exp10_model.h5") 75 | 76 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp2.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | 35 | # convert class vectors to binary class matrices 36 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 37 | y_test = keras.utils.to_categorical(y_test, num_classes) 38 | 39 | model = Sequential() 40 | model.add(Conv2D(32, kernel_size=(3, 3), 41 | activation='relu', 42 | input_shape=input_shape)) 43 | model.add(Conv2D(64, (3, 3), activation='relu')) 44 | model.add(MaxPooling2D(pool_size=(2, 2))) 45 | model.add(Dropout(0.25)) 46 | 47 | model.add(Conv2D(64, (3, 3), activation='relu')) 48 | model.add(MaxPooling2D(pool_size=(2, 2))) 49 | model.add(Dropout(0.25)) 50 | 51 | model.add(Flatten()) 52 | model.add(Dense(128, activation='relu')) 53 | model.add(Dropout(0.5)) 54 | model.add(Dense(num_classes, activation='softmax')) 55 | 56 | model.compile(loss=keras.losses.categorical_crossentropy, 57 | optimizer=keras.optimizers.Adadelta(), 58 | metrics=['accuracy']) 59 | 60 | print() 61 | print("Model parameters = %d" % model.count_params()) 62 | print() 63 | print(model.summary()) 64 | print() 65 | 66 | model.fit(x_train, y_train, 67 | batch_size=batch_size, 68 | epochs=epochs, 69 | verbose=1, 70 | validation_data=(x_test[:1000], y_test[:1000])) 71 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 72 | print('Test loss:', score[0]) 73 | print('Test accuracy:', score[1]) 74 | 75 | # Save for later use 76 | model.save("mnist_cnn_exp2_model.h5") 77 | 78 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp26.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 1 13 | num_classes = 10 14 | epochs = 1 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp26_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp27.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 16 13 | num_classes = 10 14 | epochs = 16 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp27_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp28.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 64 13 | num_classes = 10 14 | epochs = 64 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp28_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp29.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 256 13 | num_classes = 10 14 | epochs = 256 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp29_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp2A.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 2 13 | num_classes = 10 14 | epochs = 2 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp2A_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp2B.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 4 13 | num_classes = 10 14 | epochs = 4 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp2B_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp2C.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 8 13 | num_classes = 10 14 | epochs = 8 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp2C_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp2D.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 32 13 | num_classes = 10 14 | epochs = 32 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp2D_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp2E.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 128 13 | num_classes = 10 14 | epochs = 128 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp2E_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp3.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | 35 | # convert class vectors to binary class matrices 36 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 37 | y_test = keras.utils.to_categorical(y_test, num_classes) 38 | 39 | model = Sequential() 40 | model.add(Conv2D(32, kernel_size=(5, 5), 41 | activation='relu', 42 | input_shape=input_shape)) 43 | model.add(Conv2D(64, (3, 3), activation='relu')) 44 | model.add(MaxPooling2D(pool_size=(2, 2))) 45 | model.add(Dropout(0.25)) 46 | model.add(Flatten()) 47 | model.add(Dense(128, activation='relu')) 48 | model.add(Dropout(0.5)) 49 | model.add(Dense(num_classes, activation='softmax')) 50 | 51 | model.compile(loss=keras.losses.categorical_crossentropy, 52 | optimizer=keras.optimizers.Adadelta(), 53 | metrics=['accuracy']) 54 | 55 | print() 56 | print("Model parameters = %d" % model.count_params()) 57 | print() 58 | print(model.summary()) 59 | print() 60 | 61 | model.fit(x_train, y_train, 62 | batch_size=batch_size, 63 | epochs=epochs, 64 | verbose=1, 65 | validation_data=(x_test[:1000], y_test[:1000])) 66 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 67 | print('Test loss:', score[0]) 68 | print('Test accuracy:', score[1]) 69 | 70 | # Save for later use 71 | model.save("mnist_cnn_exp3_model.h5") 72 | 73 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp30.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 512 13 | num_classes = 10 14 | epochs = 512 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp30_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp31.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 1024 13 | num_classes = 10 14 | epochs = 1024 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp31_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp32.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 1024 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp32_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp33.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 512 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp33_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp34.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 256 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp34_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp35.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 128 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp35_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp36.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 64 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp36_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp37.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 32 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp37_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp38.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 16 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp38_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp39.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 8 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp39_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp4.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | print('x_train shape:', x_train.shape) 35 | print(x_train.shape[0], 'train samples') 36 | print(x_test.shape[0], 'test samples') 37 | 38 | # convert class vectors to binary class matrices 39 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 40 | y_test = keras.utils.to_categorical(y_test, num_classes) 41 | 42 | model = Sequential() 43 | model.add(Conv2D(32, kernel_size=(3, 3), 44 | activation='relu', 45 | input_shape=input_shape)) 46 | model.add(Conv2D(64, (3, 3), activation='relu')) 47 | model.add(MaxPooling2D(pool_size=(2, 2))) 48 | model.add(Dropout(0.25)) 49 | model.add(Flatten()) 50 | model.add(Dense(1024, activation='relu')) 51 | model.add(Dropout(0.5)) 52 | model.add(Dense(num_classes, activation='softmax')) 53 | 54 | model.compile(loss=keras.losses.categorical_crossentropy, 55 | optimizer=keras.optimizers.Adadelta(), 56 | metrics=['accuracy']) 57 | 58 | print() 59 | print("Model parameters = %d" % model.count_params()) 60 | print() 61 | print(model.summary()) 62 | print() 63 | 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | 73 | # Save for later use 74 | model.save("mnist_cnn_exp4_model.h5") 75 | 76 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp40.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 4 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp40_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp41.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 2 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp41_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp42.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | import time 9 | 10 | N = 1024 11 | 12 | batch_size = 1 13 | num_classes = 10 14 | epochs = 12 15 | 16 | # input image dimensions 17 | img_rows, img_cols = 28, 28 18 | 19 | # the data, split between train and test sets 20 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 21 | 22 | if K.image_data_format() == 'channels_first': 23 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 24 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 25 | input_shape = (1, img_rows, img_cols) 26 | else: 27 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 28 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 29 | input_shape = (img_rows, img_cols, 1) 30 | 31 | x_train = x_train.astype('float32')[:N] 32 | x_test = x_test.astype('float32') 33 | x_train /= 255 34 | x_test /= 255 35 | 36 | # convert class vectors to binary class matrices 37 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 38 | y_test = keras.utils.to_categorical(y_test, num_classes) 39 | 40 | model = Sequential() 41 | model.add(Conv2D(32, kernel_size=(3, 3), 42 | activation='relu', 43 | input_shape=input_shape)) 44 | model.add(Conv2D(64, (3, 3), activation='relu')) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | model.add(Dropout(0.25)) 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adadelta(), 54 | metrics=['accuracy']) 55 | 56 | print() 57 | print("Model parameters = %d" % model.count_params()) 58 | print() 59 | print(model.summary()) 60 | print() 61 | 62 | st = time.time() 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | en = time.time() 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | print('Train time:', en-st) 73 | 74 | # Save for later use 75 | model.save("mnist_cnn/mnist_cnn_exp42_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp43.py: -------------------------------------------------------------------------------- 1 | # exp43 - Adadelta 2 | from __future__ import print_function 3 | import keras 4 | from keras.datasets import mnist 5 | from keras.models import Sequential 6 | from keras.layers import Dense, Dropout, Flatten 7 | from keras.layers import Conv2D, MaxPooling2D, Activation 8 | from keras import backend as K 9 | import time 10 | 11 | N = 16384 12 | 13 | batch_size = 128 14 | num_classes = 10 15 | epochs = 12 16 | 17 | # input image dimensions 18 | img_rows, img_cols = 28, 28 19 | 20 | # the data, split between train and test sets 21 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 22 | 23 | if K.image_data_format() == 'channels_first': 24 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 25 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 26 | input_shape = (1, img_rows, img_cols) 27 | else: 28 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 29 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 30 | input_shape = (img_rows, img_cols, 1) 31 | 32 | x_train = x_train.astype('float32')[:N] 33 | x_test = x_test.astype('float32') 34 | x_train /= 255 35 | x_test /= 255 36 | 37 | # convert class vectors to binary class matrices 38 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 39 | y_test = keras.utils.to_categorical(y_test, num_classes) 40 | 41 | model = Sequential() 42 | model.add(Conv2D(32, kernel_size=(3, 3), 43 | activation='relu', 44 | input_shape=input_shape)) 45 | model.add(Conv2D(64, (3, 3), activation='relu')) 46 | model.add(MaxPooling2D(pool_size=(2, 2))) 47 | model.add(Dropout(0.25)) 48 | model.add(Flatten()) 49 | model.add(Dense(128, activation='relu')) 50 | model.add(Dropout(0.5)) 51 | model.add(Dense(num_classes, activation='softmax')) 52 | 53 | model.compile(loss=keras.losses.categorical_crossentropy, 54 | optimizer=keras.optimizers.Adadelta(), 55 | metrics=['accuracy']) 56 | 57 | print() 58 | print("Model parameters = %d" % model.count_params()) 59 | print() 60 | print(model.summary()) 61 | print() 62 | 63 | st = time.time() 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | en = time.time() 70 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 71 | print('Test loss:', score[0]) 72 | print('Test accuracy:', score[1]) 73 | print('Train time:', en-st) 74 | 75 | # Save for later use 76 | model.save("mnist_cnn/mnist_cnn_exp43_model.h5") 77 | 78 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp44.py: -------------------------------------------------------------------------------- 1 | # exp44 - SGD 2 | from __future__ import print_function 3 | import keras 4 | from keras.datasets import mnist 5 | from keras.models import Sequential 6 | from keras.layers import Dense, Dropout, Flatten 7 | from keras.layers import Conv2D, MaxPooling2D, Activation 8 | from keras import backend as K 9 | import time 10 | 11 | N = 16384 12 | 13 | batch_size = 128 14 | num_classes = 10 15 | epochs = 12 16 | 17 | # input image dimensions 18 | img_rows, img_cols = 28, 28 19 | 20 | # the data, split between train and test sets 21 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 22 | 23 | if K.image_data_format() == 'channels_first': 24 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 25 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 26 | input_shape = (1, img_rows, img_cols) 27 | else: 28 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 29 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 30 | input_shape = (img_rows, img_cols, 1) 31 | 32 | x_train = x_train.astype('float32')[:N] 33 | x_test = x_test.astype('float32') 34 | x_train /= 255 35 | x_test /= 255 36 | 37 | # convert class vectors to binary class matrices 38 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 39 | y_test = keras.utils.to_categorical(y_test, num_classes) 40 | 41 | model = Sequential() 42 | model.add(Conv2D(32, kernel_size=(3, 3), 43 | activation='relu', 44 | input_shape=input_shape)) 45 | model.add(Conv2D(64, (3, 3), activation='relu')) 46 | model.add(MaxPooling2D(pool_size=(2, 2))) 47 | model.add(Dropout(0.25)) 48 | model.add(Flatten()) 49 | model.add(Dense(128, activation='relu')) 50 | model.add(Dropout(0.5)) 51 | model.add(Dense(num_classes, activation='softmax')) 52 | 53 | model.compile(loss=keras.losses.categorical_crossentropy, 54 | optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9), 55 | metrics=['accuracy']) 56 | 57 | print() 58 | print("Model parameters = %d" % model.count_params()) 59 | print() 60 | print(model.summary()) 61 | print() 62 | 63 | st = time.time() 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | en = time.time() 70 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 71 | print('Test loss:', score[0]) 72 | print('Test accuracy:', score[1]) 73 | print('Train time:', en-st) 74 | 75 | # Save for later use 76 | model.save("mnist_cnn/mnist_cnn_exp44_model.h5") 77 | 78 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp45.py: -------------------------------------------------------------------------------- 1 | # exp45 - RMSprop 2 | from __future__ import print_function 3 | import keras 4 | from keras.datasets import mnist 5 | from keras.models import Sequential 6 | from keras.layers import Dense, Dropout, Flatten 7 | from keras.layers import Conv2D, MaxPooling2D, Activation 8 | from keras import backend as K 9 | import time 10 | 11 | N = 16384 12 | 13 | batch_size = 128 14 | num_classes = 10 15 | epochs = 12 16 | 17 | # input image dimensions 18 | img_rows, img_cols = 28, 28 19 | 20 | # the data, split between train and test sets 21 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 22 | 23 | if K.image_data_format() == 'channels_first': 24 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 25 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 26 | input_shape = (1, img_rows, img_cols) 27 | else: 28 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 29 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 30 | input_shape = (img_rows, img_cols, 1) 31 | 32 | x_train = x_train.astype('float32')[:N] 33 | x_test = x_test.astype('float32') 34 | x_train /= 255 35 | x_test /= 255 36 | 37 | # convert class vectors to binary class matrices 38 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 39 | y_test = keras.utils.to_categorical(y_test, num_classes) 40 | 41 | model = Sequential() 42 | model.add(Conv2D(32, kernel_size=(3, 3), 43 | activation='relu', 44 | input_shape=input_shape)) 45 | model.add(Conv2D(64, (3, 3), activation='relu')) 46 | model.add(MaxPooling2D(pool_size=(2, 2))) 47 | model.add(Dropout(0.25)) 48 | model.add(Flatten()) 49 | model.add(Dense(128, activation='relu')) 50 | model.add(Dropout(0.5)) 51 | model.add(Dense(num_classes, activation='softmax')) 52 | 53 | model.compile(loss=keras.losses.categorical_crossentropy, 54 | optimizer=keras.optimizers.RMSprop(lr=0.01), 55 | metrics=['accuracy']) 56 | 57 | print() 58 | print("Model parameters = %d" % model.count_params()) 59 | print() 60 | print(model.summary()) 61 | print() 62 | 63 | st = time.time() 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | en = time.time() 70 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 71 | print('Test loss:', score[0]) 72 | print('Test accuracy:', score[1]) 73 | print('Train time:', en-st) 74 | 75 | # Save for later use 76 | model.save("mnist_cnn/mnist_cnn_exp45_model.h5") 77 | 78 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp46.py: -------------------------------------------------------------------------------- 1 | # exp46 - Adagrad 2 | from __future__ import print_function 3 | import keras 4 | from keras.datasets import mnist 5 | from keras.models import Sequential 6 | from keras.layers import Dense, Dropout, Flatten 7 | from keras.layers import Conv2D, MaxPooling2D, Activation 8 | from keras import backend as K 9 | import time 10 | 11 | N = 16384 12 | 13 | batch_size = 128 14 | num_classes = 10 15 | epochs = 12 16 | 17 | # input image dimensions 18 | img_rows, img_cols = 28, 28 19 | 20 | # the data, split between train and test sets 21 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 22 | 23 | if K.image_data_format() == 'channels_first': 24 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 25 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 26 | input_shape = (1, img_rows, img_cols) 27 | else: 28 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 29 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 30 | input_shape = (img_rows, img_cols, 1) 31 | 32 | x_train = x_train.astype('float32')[:N] 33 | x_test = x_test.astype('float32') 34 | x_train /= 255 35 | x_test /= 255 36 | 37 | # convert class vectors to binary class matrices 38 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 39 | y_test = keras.utils.to_categorical(y_test, num_classes) 40 | 41 | model = Sequential() 42 | model.add(Conv2D(32, kernel_size=(3, 3), 43 | activation='relu', 44 | input_shape=input_shape)) 45 | model.add(Conv2D(64, (3, 3), activation='relu')) 46 | model.add(MaxPooling2D(pool_size=(2, 2))) 47 | model.add(Dropout(0.25)) 48 | model.add(Flatten()) 49 | model.add(Dense(128, activation='relu')) 50 | model.add(Dropout(0.5)) 51 | model.add(Dense(num_classes, activation='softmax')) 52 | 53 | model.compile(loss=keras.losses.categorical_crossentropy, 54 | optimizer=keras.optimizers.Adagrad(), 55 | metrics=['accuracy']) 56 | 57 | print() 58 | print("Model parameters = %d" % model.count_params()) 59 | print() 60 | print(model.summary()) 61 | print() 62 | 63 | st = time.time() 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | en = time.time() 70 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 71 | print('Test loss:', score[0]) 72 | print('Test accuracy:', score[1]) 73 | print('Train time:', en-st) 74 | 75 | # Save for later use 76 | model.save("mnist_cnn/mnist_cnn_exp46_model.h5") 77 | 78 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp47.py: -------------------------------------------------------------------------------- 1 | # exp47 - Adam 2 | from __future__ import print_function 3 | import keras 4 | from keras.datasets import mnist 5 | from keras.models import Sequential 6 | from keras.layers import Dense, Dropout, Flatten 7 | from keras.layers import Conv2D, MaxPooling2D, Activation 8 | from keras import backend as K 9 | import time 10 | 11 | N = 16384 12 | 13 | batch_size = 128 14 | num_classes = 10 15 | epochs = 12 16 | 17 | # input image dimensions 18 | img_rows, img_cols = 28, 28 19 | 20 | # the data, split between train and test sets 21 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 22 | 23 | if K.image_data_format() == 'channels_first': 24 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 25 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 26 | input_shape = (1, img_rows, img_cols) 27 | else: 28 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 29 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 30 | input_shape = (img_rows, img_cols, 1) 31 | 32 | x_train = x_train.astype('float32')[:N] 33 | x_test = x_test.astype('float32') 34 | x_train /= 255 35 | x_test /= 255 36 | 37 | # convert class vectors to binary class matrices 38 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 39 | y_test = keras.utils.to_categorical(y_test, num_classes) 40 | 41 | model = Sequential() 42 | model.add(Conv2D(32, kernel_size=(3, 3), 43 | activation='relu', 44 | input_shape=input_shape)) 45 | model.add(Conv2D(64, (3, 3), activation='relu')) 46 | model.add(MaxPooling2D(pool_size=(2, 2))) 47 | model.add(Dropout(0.25)) 48 | model.add(Flatten()) 49 | model.add(Dense(128, activation='relu')) 50 | model.add(Dropout(0.5)) 51 | model.add(Dense(num_classes, activation='softmax')) 52 | 53 | model.compile(loss=keras.losses.categorical_crossentropy, 54 | optimizer=keras.optimizers.Adam(), 55 | metrics=['accuracy']) 56 | 57 | print() 58 | print("Model parameters = %d" % model.count_params()) 59 | print() 60 | print(model.summary()) 61 | print() 62 | 63 | st = time.time() 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | en = time.time() 70 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 71 | print('Test loss:', score[0]) 72 | print('Test accuracy:', score[1]) 73 | print('Train time:', en-st) 74 | 75 | # Save for later use 76 | model.save("mnist_cnn/mnist_cnn_exp47_model.h5") 77 | 78 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp5.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | print('x_train shape:', x_train.shape) 35 | print(x_train.shape[0], 'train samples') 36 | print(x_test.shape[0], 'test samples') 37 | 38 | # convert class vectors to binary class matrices 39 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 40 | y_test = keras.utils.to_categorical(y_test, num_classes) 41 | 42 | model = Sequential() 43 | model.add(Conv2D(16, kernel_size=(3, 3), 44 | activation='relu', 45 | input_shape=input_shape)) 46 | model.add(Conv2D(32, (3, 3), activation='relu')) 47 | model.add(MaxPooling2D(pool_size=(2, 2))) 48 | model.add(Dropout(0.25)) 49 | model.add(Flatten()) 50 | model.add(Dense(128, activation='relu')) 51 | model.add(Dropout(0.5)) 52 | model.add(Dense(num_classes, activation='softmax')) 53 | 54 | model.compile(loss=keras.losses.categorical_crossentropy, 55 | optimizer=keras.optimizers.Adadelta(), 56 | metrics=['accuracy']) 57 | 58 | print() 59 | print("Model parameters = %d" % model.count_params()) 60 | print() 61 | print(model.summary()) 62 | print() 63 | 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | 73 | # Save for later use 74 | model.save("mnist_cnn_exp5_model.h5") 75 | 76 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp6.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | 35 | # convert class vectors to binary class matrices 36 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 37 | y_test = keras.utils.to_categorical(y_test, num_classes) 38 | 39 | model = Sequential() 40 | model.add(Conv2D(32, kernel_size=(3, 3), 41 | activation='relu', 42 | input_shape=input_shape)) 43 | model.add(Conv2D(64, (3, 3), activation='relu')) 44 | model.add(MaxPooling2D(pool_size=(2, 2))) 45 | model.add(Dropout(0.25)) 46 | model.add(Flatten()) 47 | model.add(Dense(128, activation='relu')) 48 | model.add(Dropout(0.5)) 49 | model.add(Dense(128, activation='relu')) 50 | model.add(Dropout(0.5)) 51 | model.add(Dense(num_classes, activation='softmax')) 52 | 53 | model.compile(loss=keras.losses.categorical_crossentropy, 54 | optimizer=keras.optimizers.Adadelta(), 55 | metrics=['accuracy']) 56 | 57 | print() 58 | print("Model parameters = %d" % model.count_params()) 59 | print() 60 | print(model.summary()) 61 | print() 62 | 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 69 | print('Test loss:', score[0]) 70 | print('Test accuracy:', score[1]) 71 | 72 | # Save for later use 73 | model.save("mnist_cnn_exp6_model.h5") 74 | 75 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp7.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | print('x_train shape:', x_train.shape) 35 | print(x_train.shape[0], 'train samples') 36 | print(x_test.shape[0], 'test samples') 37 | 38 | # convert class vectors to binary class matrices 39 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 40 | y_test = keras.utils.to_categorical(y_test, num_classes) 41 | 42 | model = Sequential() 43 | model.add(Conv2D(32, kernel_size=(3, 3), 44 | activation='relu', 45 | input_shape=input_shape)) 46 | model.add(Conv2D(64, (3, 3), activation='relu')) 47 | model.add(MaxPooling2D(pool_size=(2, 2))) 48 | model.add(Dropout(0.25)) 49 | model.add(Flatten()) 50 | model.add(Dense(32, activation='relu')) 51 | model.add(Dropout(0.5)) 52 | model.add(Dense(num_classes, activation='softmax')) 53 | 54 | model.compile(loss=keras.losses.categorical_crossentropy, 55 | optimizer=keras.optimizers.Adadelta(), 56 | metrics=['accuracy']) 57 | 58 | print() 59 | print("Model parameters = %d" % model.count_params()) 60 | print() 61 | print(model.summary()) 62 | print() 63 | 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test[:1000], y_test[:1000])) 69 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | 73 | # Save for later use 74 | model.save("mnist_cnn_exp7_model.h5") 75 | 76 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp8.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | 35 | # convert class vectors to binary class matrices 36 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 37 | y_test = keras.utils.to_categorical(y_test, num_classes) 38 | 39 | model = Sequential() 40 | model.add(Conv2D(32, kernel_size=(3, 3), 41 | activation='relu', 42 | input_shape=input_shape)) 43 | model.add(Conv2D(64, (3, 3), activation='relu')) 44 | #model.add(MaxPooling2D(pool_size=(2, 2))) 45 | model.add(Dropout(0.25)) 46 | model.add(Flatten()) 47 | model.add(Dense(128, activation='relu')) 48 | model.add(Dropout(0.5)) 49 | model.add(Dense(num_classes, activation='softmax')) 50 | 51 | model.compile(loss=keras.losses.categorical_crossentropy, 52 | optimizer=keras.optimizers.Adadelta(), 53 | metrics=['accuracy']) 54 | 55 | print() 56 | print("Model parameters = %d" % model.count_params()) 57 | print() 58 | print(model.summary()) 59 | print() 60 | 61 | model.fit(x_train, y_train, 62 | batch_size=batch_size, 63 | epochs=epochs, 64 | verbose=1, 65 | validation_data=(x_test[:1000], y_test[:1000])) 66 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 67 | print('Test loss:', score[0]) 68 | print('Test accuracy:', score[1]) 69 | 70 | # Save for later use 71 | model.save("mnist_cnn_exp8_model.h5") 72 | 73 | -------------------------------------------------------------------------------- /chapter_13/mnist_cnn_exp9.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import keras 3 | from keras.datasets import mnist 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Flatten 6 | from keras.layers import Conv2D, MaxPooling2D, Activation 7 | from keras import backend as K 8 | 9 | N = 1000 10 | 11 | batch_size = 128 12 | num_classes = 10 13 | epochs = 12 14 | 15 | # input image dimensions 16 | img_rows, img_cols = 28, 28 17 | 18 | # the data, split between train and test sets 19 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 20 | 21 | if K.image_data_format() == 'channels_first': 22 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 23 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 24 | input_shape = (1, img_rows, img_cols) 25 | else: 26 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 27 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 28 | input_shape = (img_rows, img_cols, 1) 29 | 30 | x_train = x_train.astype('float32')[:N] 31 | x_test = x_test.astype('float32') 32 | x_train /= 255 33 | x_test /= 255 34 | print('x_train shape:', x_train.shape) 35 | print(x_train.shape[0], 'train samples') 36 | print(x_test.shape[0], 'test samples') 37 | 38 | # convert class vectors to binary class matrices 39 | y_train = keras.utils.to_categorical(y_train[:N], num_classes) 40 | y_test = keras.utils.to_categorical(y_test, num_classes) 41 | 42 | model = Sequential() 43 | model.add(Conv2D(32, kernel_size=(3, 3), 44 | input_shape=input_shape)) 45 | model.add(Conv2D(64, (3, 3))) 46 | model.add(MaxPooling2D(pool_size=(2, 2))) 47 | model.add(Dropout(0.25)) 48 | model.add(Flatten()) 49 | model.add(Dense(128, activation='relu')) 50 | model.add(Dropout(0.5)) 51 | model.add(Dense(num_classes, activation='softmax')) 52 | 53 | model.compile(loss=keras.losses.categorical_crossentropy, 54 | optimizer=keras.optimizers.Adadelta(), 55 | metrics=['accuracy']) 56 | 57 | print() 58 | print("Model parameters = %d" % model.count_params()) 59 | print() 60 | print(model.summary()) 61 | print() 62 | 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test[:1000], y_test[:1000])) 68 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 69 | print('Test loss:', score[0]) 70 | print('Test accuracy:', score[1]) 71 | 72 | # Save for later use 73 | model.save("mnist_cnn_exp9_model.h5") 74 | 75 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/README.txt: -------------------------------------------------------------------------------- 1 | Source code for the fully convolutional experiments in Chapter 13 2 | ----------------------------------------------------------------- 3 | 4 | The first set of experiments use the standard MNIST data. Run 5 | the code in this order: 6 | 7 | 1. mnist_cnn.py (to create 'mnist_cnn_base_model.h5') 8 | 2. mnist_cnn_fcn.py (to create 'mnist_cnn_fcn_model.h5') 9 | 3. make_large_mnist_test_images.py (to create larger multidigit images) 10 | 4. mnist_cnn_fcn_single_digits.py (to verify the model works with single digits) 11 | 5. mnist_cnn_fcn_test_large.py (to classify the large images) 12 | 6. mnist_cnn_fcn_graymaps.py (to make heat maps) 13 | 14 | 15 | The second set of experiments uses an augmented and shifted MNIST dataset. 16 | Run the code in this order: 17 | 18 | 1. mnist_cnn_aug.py (makes 'mnist_cnn_base_aug_model.h5') 19 | 2. mnist_cnn_aug_fcn.py (makes 'mnist_cnn_aug_fcn_model.h5') 20 | 3. mnist_cnn_aug_fcn_test.py (tests single images & large images, 'results_aug') 21 | 4. mnist_cnn_aug_fcn_heatmaps.py (creates 'heatmaps_aug') 22 | 23 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/make_large_mnist_test_images.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: make_large_mnist_test_images.py 3 | # 4 | # Make some large MNIST test images. 5 | # 6 | # RTK, 19-Oct-2019 7 | # Last update: 20-Oct-2019 8 | # 9 | ################################################################ 10 | 11 | import os 12 | import sys 13 | import numpy as np 14 | import random 15 | from PIL import Image 16 | 17 | def main(): 18 | """Make large test images""" 19 | 20 | os.system("rm -rf images; mkdir images") 21 | 22 | if (len(sys.argv) > 1): 23 | N = int(sys.argv[1]) 24 | else: 25 | N = 10 26 | 27 | x_test = np.load("../../data/mnist/mnist_test_images.npy") 28 | 29 | for i in range(N): 30 | r,c = random.randint(6,12), random.randint(6,12) 31 | g = np.zeros(r*c) 32 | for j in range(r*c): 33 | if (random.random() < 0.15): 34 | g[j] = 1 35 | g = g.reshape((r,c)) 36 | g[:,0] = g[0,:] = g[:,-1] = g[-1,:] = 0 37 | 38 | img = np.zeros((28*r,28*c), dtype="uint8") 39 | for x in range(r): 40 | for y in range(c): 41 | if (g[x,y] == 1): 42 | n = random.randint(0, x_test.shape[0]) 43 | im = x_test[n] 44 | img[28*x:(28*x+28), 28*y:(28*y+28)] = im 45 | 46 | Image.fromarray(img).save("images/image_%04d.png" % i) 47 | 48 | 49 | if (__name__ == "__main__"): 50 | main() 51 | 52 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/make_shifted_mnist_dataset.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: make_shifted_mnist_dataset.py 3 | # 4 | # Augment by shifting for use with FCN 5 | # 6 | # RTK, 20-Oct-2019 7 | # Last update: 20-Oct-2019 8 | # 9 | ################################################################ 10 | 11 | import numpy as np 12 | import random 13 | 14 | def shifted(im): 15 | r,c = im.shape 16 | x = random.randint(-r//4, r//4) 17 | y = random.randint(-c//4, c//4) 18 | img = np.zeros((2*r,2*c), dtype="uint8") 19 | xoff = r//2 + x 20 | yoff = c//2 + y 21 | img[xoff:(xoff+r), yoff:(yoff+c)] = im 22 | img = img[r//2:(r//2+r),c//2:(c//2+c)] 23 | return img 24 | 25 | 26 | def main(): 27 | x_train = np.load("../../../../data/mnist/mnist_train_images.npy") 28 | x_test = np.load("../../../../data/mnist/mnist_test_images.npy") 29 | 30 | y_train = np.load("../../../../data/mnist/mnist_train_labels.npy") 31 | y_test = np.load("../../../../data/mnist/mnist_test_labels.npy") 32 | 33 | x_train_aug = np.zeros((5*60000,28,28), dtype="uint8") 34 | x_test_aug = np.zeros((5*10000,28,28), dtype="uint8") 35 | y_train_labels = np.zeros(5*60000, dtype="uint8") 36 | y_test_labels = np.zeros(5*10000, dtype="uint8") 37 | 38 | k = 0 39 | for i in range(60000): 40 | x_train_aug[k] = x_train[i] 41 | y_train_labels[k] = y_train[i] 42 | k += 1 43 | for j in range(4): 44 | x_train_aug[k] = shifted(x_train[i]) 45 | y_train_labels[k] = y_train[i] 46 | k += 1 47 | 48 | k = 0 49 | for i in range(10000): 50 | x_test_aug[k] = x_test[i] 51 | y_test_labels[k] = y_test[i] 52 | k += 1 53 | for j in range(4): 54 | x_test_aug[k] = shifted(x_test[i]) 55 | y_test_labels[k] = y_test[i] 56 | k += 1 57 | 58 | idx = np.argsort(np.random.random(5*60000)) 59 | x_train_aug = x_train_aug[idx] 60 | y_train_labels = y_train_labels[idx] 61 | 62 | idx = np.argsort(np.random.random(5*10000)) 63 | x_test_aug = x_test_aug[idx] 64 | y_test_labels = y_test_labels[idx] 65 | 66 | np.save("mnist_train_aug_images.npy", x_train_aug) 67 | np.save("mnist_train_aug_labels.npy", y_train_labels) 68 | np.save("mnist_test_aug_images.npy", x_test_aug) 69 | np.save("mnist_test_aug_labels.npy", y_test_labels) 70 | 71 | 72 | main() 73 | 74 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_cnn.py 3 | # 4 | # Train the base MNIST CNN model. Keras example. 5 | # 6 | # RTK, 20-Oct-2019 7 | # Last update: 04-Mar-2022 8 | # 9 | ################################################################ 10 | 11 | import tensorflow.keras as keras 12 | from tensorflow.keras.datasets import mnist 13 | from tensorflow.keras.models import Sequential 14 | from tensorflow.keras.layers import Dense, Dropout, Flatten 15 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Activation 16 | from tensorflow.keras import backend as K 17 | 18 | batch_size = 128 19 | num_classes = 10 20 | epochs = 24 21 | 22 | # input image dimensions 23 | img_rows, img_cols = 28, 28 24 | 25 | # the data, split between train and test sets 26 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 27 | 28 | if K.image_data_format() == 'channels_first': 29 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 30 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 31 | input_shape = (1, img_rows, img_cols) 32 | else: 33 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 34 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 35 | input_shape = (img_rows, img_cols, 1) 36 | 37 | x_train = x_train.astype('float32') 38 | x_test = x_test.astype('float32') 39 | x_train /= 255 40 | x_test /= 255 41 | 42 | y_train = keras.utils.to_categorical(y_train, num_classes) 43 | y_test = keras.utils.to_categorical(y_test, num_classes) 44 | 45 | model = Sequential() 46 | model.add(Conv2D(32, kernel_size=(3, 3), 47 | activation='relu', 48 | input_shape=input_shape)) 49 | model.add(Conv2D(64, (3, 3), activation='relu')) 50 | model.add(MaxPooling2D(pool_size=(2, 2))) 51 | model.add(Dropout(0.25)) 52 | model.add(Flatten()) 53 | model.add(Dense(128, activation='relu')) 54 | model.add(Dropout(0.5)) 55 | model.add(Dense(num_classes, activation='softmax')) 56 | 57 | model.compile(loss=keras.losses.categorical_crossentropy, 58 | optimizer=keras.optimizers.Adadelta(), 59 | metrics=['accuracy']) 60 | 61 | print() 62 | print("Model parameters = %d" % model.count_params()) 63 | print() 64 | print(model.summary()) 65 | print() 66 | 67 | model.fit(x_train, y_train, 68 | batch_size=batch_size, 69 | epochs=epochs, 70 | verbose=1, 71 | validation_data=(x_test, y_test)) 72 | score = model.evaluate(x_test, y_test, verbose=0) 73 | print('Test loss:', score[0]) 74 | print('Test accuracy:', score[1]) 75 | 76 | model.save("mnist_cnn_base_model.h5") 77 | 78 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn_aug.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_cnn_aug.py 3 | # 4 | # Train the base MNIST CNN augmented model. Keras example. 5 | # 6 | # RTK, 20-Oct-2019 7 | # Last update: 20-Oct-2019 8 | # 9 | ################################################################ 10 | 11 | import keras 12 | from keras.datasets import mnist 13 | from keras.models import Sequential 14 | from keras.layers import Dense, Dropout, Flatten 15 | from keras.layers import Conv2D, MaxPooling2D, Activation 16 | from keras import backend as K 17 | import numpy as np 18 | 19 | batch_size = 128 20 | num_classes = 10 21 | epochs = 12 22 | 23 | # input image dimensions 24 | img_rows, img_cols = 28, 28 25 | 26 | x_train = np.load("mnist_train_aug_images.npy") 27 | y_train = np.load("mnist_train_aug_labels.npy") 28 | x_test = np.load("mnist_test_aug_images.npy") 29 | y_test = np.load("mnist_test_aug_labels.npy") 30 | 31 | if K.image_data_format() == 'channels_first': 32 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 33 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 34 | input_shape = (1, img_rows, img_cols) 35 | else: 36 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 37 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 38 | input_shape = (img_rows, img_cols, 1) 39 | 40 | x_train = x_train.astype('float32') 41 | x_test = x_test.astype('float32') 42 | x_train /= 255 43 | x_test /= 255 44 | 45 | y_train = keras.utils.to_categorical(y_train, num_classes) 46 | y_test = keras.utils.to_categorical(y_test, num_classes) 47 | 48 | model = Sequential() 49 | model.add(Conv2D(32, kernel_size=(3, 3), 50 | activation='relu', 51 | input_shape=input_shape)) 52 | model.add(Conv2D(64, (3, 3), activation='relu')) 53 | model.add(MaxPooling2D(pool_size=(2, 2))) 54 | model.add(Dropout(0.25)) 55 | model.add(Flatten()) 56 | model.add(Dense(128, activation='relu')) 57 | model.add(Dropout(0.5)) 58 | model.add(Dense(num_classes, activation='softmax')) 59 | 60 | model.compile(loss=keras.losses.categorical_crossentropy, 61 | optimizer=keras.optimizers.Adadelta(), 62 | metrics=['accuracy']) 63 | 64 | model.fit(x_train, y_train, 65 | batch_size=batch_size, 66 | epochs=epochs, 67 | verbose=1, 68 | validation_data=(x_test, y_test)) 69 | score = model.evaluate(x_test, y_test, verbose=0) 70 | print('Test loss:', score[0]) 71 | print('Test accuracy:', score[1]) 72 | 73 | model.save("mnist_cnn_base_aug_model.h5") 74 | 75 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn_aug_fcn.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_cnn_fcn.py 3 | # 4 | # Create fully convolutional version of the MNIST CNN 5 | # and populate with the weights from the version trained 6 | # on the MNIST digits. 7 | # 8 | # RTK, 20-Oct-2019 9 | # Last update: 20-Oct-2019 10 | # 11 | ################################################################ 12 | 13 | import keras 14 | from keras.utils import plot_model 15 | from keras.datasets import mnist 16 | from keras.models import Sequential, load_model 17 | from keras.layers import Dense, Dropout, Flatten, Activation 18 | from keras.layers import Conv1D, Conv2D, MaxPooling2D 19 | from keras import backend as K 20 | import numpy as np 21 | 22 | # Load the weights from the base model 23 | weights = load_model('mnist_cnn_base_aug_model.h5').get_weights() 24 | 25 | # Build the same architecture replacing Dense layers 26 | # with equivalent fully convolutional layers 27 | model = Sequential() 28 | model.add(Conv2D(32, kernel_size=(3, 3), # input shape arbitrary 29 | activation='relu', # but grayscale 30 | input_shape=(None,None,1))) 31 | model.add(Conv2D(64, (3, 3), activation='relu')) 32 | model.add(MaxPooling2D(pool_size=(2, 2))) 33 | model.add(Dropout(0.25)) 34 | 35 | # Dense layer becomes Conv2D w/12x12 kernel, 128 filters 36 | model.add(Conv2D(128, (12,12), activation='relu')) 37 | model.add(Dropout(0.5)) 38 | 39 | # Output layer also Conv2D but 1x1 w/10 "filters" 40 | model.add(Conv2D(10, (1,1), activation='softmax')) 41 | 42 | # Copy the trained weights remapping as necessary 43 | model.layers[0].set_weights([weights[0], weights[1]]) 44 | model.layers[1].set_weights([weights[2], weights[3]]) 45 | model.layers[4].set_weights([weights[4].reshape([12,12,64,128]), weights[5]]) 46 | model.layers[6].set_weights([weights[6].reshape([1,1,128,10]), weights[7]]) 47 | 48 | # Output the fully convolutional model 49 | model.save('mnist_cnn_aug_fcn_model.h5') 50 | 51 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn_aug_fcn_heatmaps.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_cnn_fcn_heatmaps.py 3 | # 4 | # Generate per digit heatmaps 5 | # 6 | # RTK, 20-Oct-2019 7 | # Last update: 20-Oct-2019 8 | # 9 | ################################################################ 10 | 11 | import os 12 | import sys 13 | import numpy as np 14 | from PIL import Image 15 | 16 | def main(): 17 | if (len(sys.argv) > 1): 18 | threshold = float(sys.argv[1]) 19 | else: 20 | threshold = 0.99 21 | 22 | os.system("rm -rf heatmaps_aug; mkdir heatmaps_aug") 23 | 24 | # Process all large images 25 | inames = ["images/"+i for i in os.listdir("images")] 26 | rnames = ["results_aug/"+i for i in os.listdir("results_aug")] 27 | inames.sort() 28 | rnames.sort() 29 | 30 | # Heat map colors 31 | colors = [ 32 | [0xe6,0x19,0x4b],[0xf5,0x82,0x31],[0xff,0xe1,0x19],[0xbf,0xef,0x45],[0x3c,0xb4,0x4b], 33 | [0x42,0xd4,0xf4],[0x43,0x63,0xd8],[0x91,0x1e,0xb4],[0xf0,0x32,0xe6],[0xa9,0xa9,0xa9] 34 | ] 35 | 36 | for i,iname in enumerate(inames): 37 | rname = rnames[i] 38 | c,r = Image.open(iname).size 39 | m = np.load(rname) 40 | hmap = np.zeros((r,c,10)) 41 | res = np.load(rname) 42 | x,y,_ = res.shape 43 | xoff = (r - 2*x) // 2 44 | yoff = (c - 2*y) // 2 45 | 46 | # Create heatmaps for this input image 47 | for j in range(10): 48 | h = np.array(Image.fromarray(res[:,:,j]).resize((2*y,2*x))) 49 | hmap[xoff:(xoff+x*2), yoff:(yoff+y*2),j] = h 50 | 51 | # Store the raw heatmap 52 | np.save("heatmaps_aug/heatmap_%04d.npy" % i, hmap) 53 | 54 | # Apply the threshold 55 | hmap[np.where(hmap < threshold)] = 0.0 56 | 57 | # Convert heatmaps to RGB image 58 | cmap = np.zeros((r,c,3), dtype="uint8") 59 | for x in range(r): 60 | for y in range(c): 61 | if (hmap[x,y,:].max() >= threshold): 62 | n = np.argmax(hmap[x,y,:]) 63 | cmap[x,y,:] = colors[n] 64 | 65 | # Store the image as well 66 | cmap = Image.fromarray(cmap) 67 | cmap.save("heatmaps_aug/heatmap_%04d.png" % i) 68 | 69 | # Alpha-blend 70 | img = Image.blend(cmap.convert("RGBA"), Image.open(iname).convert("RGBA"), alpha=0.2) 71 | img.convert("RGB").save("heatmaps_aug/blend_%04d.png" % i) 72 | 73 | 74 | main() 75 | 76 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn_aug_fcn_test.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_cnn_fcn_test.py 3 | # 4 | # Test the fully convolutional version 5 | # 6 | # RTK, 20-Oct-2019 7 | # Last update: 20-Oct-2019 8 | # 9 | ################################################################ 10 | 11 | import os 12 | import time 13 | import numpy as np 14 | from keras.models import load_model 15 | from PIL import Image 16 | 17 | def main(): 18 | # Test individual MNIST digits 19 | x_test = np.load("../../../../data/mnist/mnist_test_images.npy")/255.0 20 | y_test = np.load("../../../../data/mnist/mnist_test_labels.npy") 21 | 22 | # Random subset 23 | N = 1000 24 | idx = np.argsort(np.random.random(N)) 25 | 26 | # The FCN model 27 | model = load_model("mnist_cnn_aug_fcn_model.h5") 28 | 29 | # Make predictions on the digits and track accuracy 30 | nc = nw = 0.0 31 | 32 | for i in idx: 33 | p = model.predict(x_test[i][np.newaxis,:,:,np.newaxis]) 34 | c = np.argmax(p) 35 | if (c == y_test[i]): 36 | nc += 1 37 | else: 38 | nw += 1 39 | print() 40 | print("Single MNIST digits, n=%d, accuracy = %0.2f%%" % (N, 100*nc/N)) 41 | print() 42 | 43 | # Test larger digit images and store the results 44 | os.system("rm -rf results_aug; mkdir results_aug") 45 | n = len(os.listdir("images")) 46 | 47 | print("Processing larger digit images... ") 48 | st = time.time() 49 | for i in range(n): 50 | f = "images/image_%04d.png" % i 51 | im = np.array(Image.open(f))/255.0 52 | p = model.predict(im[np.newaxis,:,:,np.newaxis]) 53 | np.save("results_aug/results_%04d.npy" % i, p[0,:,:,:]) 54 | print("done, time = %0.3f s" % (time.time()-st)) 55 | print() 56 | 57 | 58 | main() 59 | 60 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn_fcn.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_cnn_fcn.py 3 | # 4 | # Create fully convolutional version of the MNIST CNN 5 | # and populate with the weights from the version trained 6 | # on the MNIST digits. 7 | # 8 | # RTK, 20-Oct-2019 9 | # Last update: 04-Mar-2022 10 | # 11 | ################################################################ 12 | 13 | from tensorflow.keras.models import Sequential, load_model 14 | from tensorflow.keras.layers import Dense, Dropout, Flatten 15 | from tensorflow.keras.layers import Conv2D, MaxPooling2D 16 | 17 | # Load the weights from the base model 18 | weights = load_model('mnist_cnn_base_model.h5').get_weights() 19 | 20 | # Build the same architecture replacing Dense layers 21 | # with equivalent fully convolutional layers 22 | model = Sequential() 23 | model.add(Conv2D(32, kernel_size=(3, 3), # input shape arbitrary 24 | activation='relu', # but grayscale 25 | input_shape=(None,None,1))) 26 | model.add(Conv2D(64, (3, 3), activation='relu')) 27 | model.add(MaxPooling2D(pool_size=(2, 2))) 28 | model.add(Dropout(0.25)) 29 | 30 | # Dense layer becomes Conv2D w/12x12 kernel, 128 filters 31 | model.add(Conv2D(128, (12,12), activation='relu')) 32 | model.add(Dropout(0.5)) 33 | 34 | # Output layer also Conv2D but 1x1 w/10 "filters" 35 | model.add(Conv2D(10, (1,1), activation='softmax')) 36 | 37 | # Copy the trained weights remapping as necessary 38 | model.layers[0].set_weights([weights[0], weights[1]]) 39 | model.layers[1].set_weights([weights[2], weights[3]]) 40 | model.layers[4].set_weights([weights[4].reshape([12,12,64,128]), weights[5]]) 41 | model.layers[6].set_weights([weights[6].reshape([1,1,128,10]), weights[7]]) 42 | 43 | # Output the fully convolutional model 44 | model.compile(optimizer='adam', loss='binary_crossentropy') 45 | model.save('mnist_cnn_fcn_model.h5') 46 | 47 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn_fcn_graymaps.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: mnist_cnn_fcn_graymaps.py 3 | # 4 | # Generate per digit grayscale heat maps 5 | # for the given input image. 6 | # 7 | # RTK, 28-Oct-2019 8 | # Last update: 28-Oct-2019 9 | # 10 | ################################################################ 11 | 12 | import os 13 | import sys 14 | import numpy as np 15 | from PIL import Image 16 | 17 | def main(): 18 | if (len(sys.argv) != 5): 19 | print() 20 | print("mnist_cnn_fcn_graymaps ") 21 | print() 22 | print(" - heatmap threshold [0,1)") 23 | print(" - source image") 24 | print(" - results for image (.npy)") 25 | print(" - output graymap directory (overwritten)") 26 | print() 27 | return 28 | 29 | threshold = float(sys.argv[1]) 30 | iname = sys.argv[2] 31 | rname = sys.argv[3] 32 | outdir= sys.argv[4] 33 | 34 | os.system("rm -rf %s; mkdir %s" % (outdir, outdir)) 35 | 36 | res = np.load(rname) 37 | img = Image.open(iname) 38 | c,r = img.size 39 | 40 | 41 | # Process all large images 42 | inames = ["images/"+i for i in os.listdir("images")] 43 | rnames = ["results/"+i for i in os.listdir("results")] 44 | inames.sort() 45 | rnames.sort() 46 | 47 | hmap = np.zeros((r,c,10)) 48 | res = np.load(rname) 49 | x,y,_ = res.shape 50 | xoff = (r - 2*x) // 2 51 | yoff = (c - 2*y) // 2 52 | 53 | for j in range(10): 54 | h = np.array(Image.fromarray(res[:,:,j]).resize((2*y,2*x))) 55 | hmap[xoff:(xoff+x*2), yoff:(yoff+y*2),j] = h 56 | 57 | # Store the raw heatmap 58 | np.save("%s/graymaps.npy" % outdir, hmap) 59 | 60 | # Apply the threshold 61 | hmap[np.where(hmap < threshold)] = 0.0 62 | 63 | # Convert heatmaps to grayscale images 64 | for j in range(10): 65 | img = np.zeros((r,c), dtype="uint8") 66 | for x in range(r): 67 | for y in range(c): 68 | img[x,y] = int(255.0*hmap[x,y,j]) 69 | 70 | Image.fromarray(img).save("%s/graymap_digit_%d.png" % (outdir, j)) 71 | img = 255-img 72 | Image.fromarray(img).save("%s/graymap_inv_digit_%d.png" % (outdir, j)) 73 | 74 | 75 | 76 | main() 77 | 78 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn_fcn_single_digits.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from keras.models import load_model 3 | 4 | x_test = np.load("../../../../data/mnist/mnist_test_images.npy")/255.0 5 | y_test = np.load("../../../../data/mnist/mnist_test_labels.npy") 6 | 7 | model = load_model("mnist_cnn_fcn_model.h5") 8 | N = y_test.shape[0] 9 | nc = nw = 0.0 10 | for i in range(N): 11 | p = model.predict(x_test[i][np.newaxis,:,:,np.newaxis]) 12 | c = np.argmax(p) 13 | if (c == y_test[i]): 14 | nc += 1 15 | else: 16 | nw += 1 17 | print() 18 | print("Single MNIST digits, n=%d, accuracy = %0.2f%%" % (N, 100*nc/N)) 19 | print() 20 | 21 | -------------------------------------------------------------------------------- /chapter_13/mnist_large/mnist_cnn_fcn_test_large.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | from keras.models import load_model 4 | from PIL import Image 5 | 6 | model = load_model("mnist_cnn_fcn_model.h5") 7 | 8 | os.system("rm -rf results; mkdir results") 9 | n = len(os.listdir("images")) 10 | 11 | for i in range(n): 12 | f = "images/image_%04d.png" % i 13 | im = np.array(Image.open(f))/255.0 14 | p = model.predict(im[np.newaxis,:,:,np.newaxis]) 15 | np.save("results/results_%04d.npy" % i, p[0,:,:,:]) 16 | 17 | -------------------------------------------------------------------------------- /chapter_13/run_all: -------------------------------------------------------------------------------- 1 | python3 -W ignore mnist_cnn_exp0.py 2 | python3 -W ignore mnist_cnn_exp1.py 3 | python3 -W ignore mnist_cnn_exp2.py 4 | python3 -W ignore mnist_cnn_exp3.py 5 | python3 -W ignore mnist_cnn_exp4.py 6 | python3 -W ignore mnist_cnn_exp5.py 7 | python3 -W ignore mnist_cnn_exp6.py 8 | python3 -W ignore mnist_cnn_exp7.py 9 | python3 -W ignore mnist_cnn_exp8.py 10 | python3 -W ignore mnist_cnn_exp9.py 11 | python3 -W ignore mnist_cnn_exp10.py 12 | 13 | -------------------------------------------------------------------------------- /chapter_14/cat_dog/make_small_cat_dog_dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | x_train = np.load("../../data/cifar10/cifar10_train_images.npy")[:,2:30,2:30,:] 4 | y_train = np.load("../../data/cifar10/cifar10_train_labels.npy") 5 | x_test = np.load("../../data/cifar10/cifar10_test_images.npy")[:,2:30,2:30,:] 6 | y_test = np.load("../../data/cifar10/cifar10_test_labels.npy") 7 | xtrn = []; ytrn = [] 8 | xtst = []; ytst = [] 9 | 10 | for i in range(y_train.shape[0]): 11 | if (y_train[i]==3): 12 | xtrn.append(x_train[i]) 13 | ytrn.append(0) 14 | if (y_train[i]==5): 15 | xtrn.append(x_train[i]) 16 | ytrn.append(1) 17 | for i in range(y_test.shape[0]): 18 | if (y_test[i]==3): 19 | xtst.append(x_test[i]) 20 | ytst.append(0) 21 | if (y_test[i]==5): 22 | xtst.append(x_test[i]) 23 | ytst.append(1) 24 | 25 | np.save("../../data/cifar10/cifar10_train_cat_dog_small_images.npy", np.array(xtrn)[:1000]) 26 | np.save("../../data/cifar10/cifar10_train_cat_dog_small_labels.npy", np.array(ytrn)[:1000]) 27 | np.save("../../data/cifar10/cifar10_test_cat_dog_small_images.npy", np.array(xtst)[:1000]) 28 | np.save("../../data/cifar10/cifar10_test_cat_dog_small_labels.npy", np.array(ytst)[:1000]) 29 | 30 | -------------------------------------------------------------------------------- /chapter_14/cat_dog/make_vehicle_dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | x_train = np.load("../../data/cifar10/cifar10_aug_train_images.npy") 4 | y_train = np.load("../../data/cifar10/cifar10_aug_train_labels.npy") 5 | x_test = np.load("../../data/cifar10/cifar10_aug_test_images.npy") 6 | y_test = np.load("../../data/cifar10/cifar10_test_labels.npy") 7 | 8 | vehicles= [0,1,8,9] 9 | xv_train = []; xv_test = [] 10 | yv_train = []; yv_test = [] 11 | 12 | for i in range(y_train.shape[0]): 13 | if (y_train[i] in vehicles): 14 | xv_train.append(x_train[i]) 15 | yv_train.append(vehicles.index(y_train[i])) 16 | for i in range(y_test.shape[0]): 17 | if (y_test[i] in vehicles): 18 | xv_test.append(x_test[i]) 19 | yv_test.append(vehicles.index(y_test[i])) 20 | 21 | np.save("../../data/cifar10/cifar10_train_vehicles_images.npy", np.array(xv_train)) 22 | np.save("../../data/cifar10/cifar10_train_vehicles_labels.npy", np.array(yv_train)) 23 | np.save("../../data/cifar10/cifar10_test_vehicles_images.npy", np.array(xv_test)) 24 | np.save("../../data/cifar10/cifar10_test_vehicles_labels.npy", np.array(yv_test)) 25 | 26 | -------------------------------------------------------------------------------- /chapter_14/cifar10_cnn.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: cifar10_cnn.py 3 | # 4 | # MNIST architecture applied to CIFAR-10 5 | # 6 | # RTK, 20-Oct-2019 7 | # Last update: 02-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import keras 12 | from keras.models import Sequential 13 | from keras.layers import Dense, Dropout, Flatten 14 | from keras.layers import Conv2D, MaxPooling2D 15 | from keras import backend as K 16 | import numpy as np 17 | 18 | batch_size = 64 19 | num_classes = 10 20 | epochs = 60 21 | img_rows, img_cols = 32, 32 22 | 23 | x_train = np.load("../data/cifar10/cifar10_train_images.npy") 24 | y_train = np.load("../data/cifar10/cifar10_train_labels.npy") 25 | 26 | x_test = np.load("../data/cifar10/cifar10_test_images.npy") 27 | y_test = np.load("../data/cifar10/cifar10_test_labels.npy") 28 | 29 | if K.image_data_format() == 'channels_first': 30 | x_train = x_train.reshape(x_train.shape[0], 3, img_rows, img_cols) 31 | x_test = x_test.reshape(x_test.shape[0], 3, img_rows, img_cols) 32 | input_shape = (3, img_rows, img_cols) 33 | else: 34 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 3) 35 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 3) 36 | input_shape = (img_rows, img_cols, 3) 37 | 38 | x_train = x_train.astype('float32') 39 | x_test = x_test.astype('float32') 40 | x_train /= 255 41 | x_test /= 255 42 | 43 | y_train = keras.utils.to_categorical(y_train, num_classes) 44 | y_test = keras.utils.to_categorical(y_test, num_classes) 45 | 46 | model = Sequential() 47 | model.add(Conv2D(32, kernel_size=(3, 3), 48 | activation='relu', 49 | input_shape=input_shape)) 50 | model.add(Conv2D(64, (3, 3), activation='relu')) 51 | model.add(MaxPooling2D(pool_size=(2, 2))) 52 | model.add(Dropout(0.25)) 53 | model.add(Flatten()) 54 | model.add(Dense(128, activation='relu')) 55 | model.add(Dropout(0.5)) 56 | model.add(Dense(num_classes, activation='softmax')) 57 | 58 | model.compile(loss=keras.losses.categorical_crossentropy, 59 | optimizer=keras.optimizers.Adadelta(), 60 | metrics=['accuracy']) 61 | 62 | print("Model parameters = %d" % model.count_params()) 63 | print(model.summary()) 64 | 65 | history = model.fit(x_train, y_train, 66 | batch_size=batch_size, 67 | epochs=epochs, 68 | verbose=1, 69 | validation_data=(x_test[:1000], y_test[:1000])) 70 | 71 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 72 | print('Test loss:', score[0]) 73 | print('Test accuracy:', score[1]) 74 | 75 | model.save("cifar10_cnn_model.h5") 76 | 77 | -------------------------------------------------------------------------------- /chapter_14/cifar10_cnn_SGD.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: cifar10_cnn_SGD.py 3 | # 4 | # MNIST architecture applied to CIFAR-10 5 | # 6 | # RTK, 20-Oct-2019 7 | # Last update: 04-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import keras 12 | from keras.datasets import mnist 13 | from keras.models import Sequential 14 | from keras.layers import Dense, Dropout, Flatten 15 | from keras.layers import Conv2D, MaxPooling2D, Activation 16 | from keras import backend as K 17 | import numpy as np 18 | 19 | batch_size = 64 20 | num_classes = 10 21 | epochs = 60 22 | img_rows, img_cols = 32, 32 23 | 24 | x_train = np.load("../data/cifar10/cifar10_train_images.npy") 25 | y_train = np.load("../data/cifar10/cifar10_train_labels.npy") 26 | 27 | x_test = np.load("../data/cifar10/cifar10_test_images.npy") 28 | y_test = np.load("../data/cifar10/cifar10_test_labels.npy") 29 | 30 | if K.image_data_format() == 'channels_first': 31 | x_train = x_train.reshape(x_train.shape[0], 3, img_rows, img_cols) 32 | x_test = x_test.reshape(x_test.shape[0], 3, img_rows, img_cols) 33 | input_shape = (3, img_rows, img_cols) 34 | else: 35 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 3) 36 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 3) 37 | input_shape = (img_rows, img_cols, 3) 38 | 39 | x_train = x_train.astype('float32') 40 | x_test = x_test.astype('float32') 41 | x_train /= 255 42 | x_test /= 255 43 | 44 | y_train = keras.utils.to_categorical(y_train, num_classes) 45 | y_test = keras.utils.to_categorical(y_test, num_classes) 46 | 47 | model = Sequential() 48 | model.add(Conv2D(32, kernel_size=(3, 3), 49 | activation='relu', 50 | input_shape=input_shape)) 51 | model.add(Conv2D(64, (3, 3), activation='relu')) 52 | model.add(MaxPooling2D(pool_size=(2, 2))) 53 | model.add(Dropout(0.25)) 54 | model.add(Flatten()) 55 | model.add(Dense(128, activation='relu')) 56 | model.add(Dropout(0.5)) 57 | model.add(Dense(num_classes, activation='softmax')) 58 | 59 | model.compile(loss=keras.losses.categorical_crossentropy, 60 | optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9), 61 | metrics=['accuracy']) 62 | 63 | print("Model parameters = %d" % model.count_params()) 64 | print(model.summary()) 65 | 66 | history = model.fit(x_train, y_train, 67 | batch_size=batch_size, 68 | epochs=epochs, 69 | verbose=1, 70 | validation_data=(x_test[:1000], y_test[:1000])) 71 | 72 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 73 | print('Test loss:', score[0]) 74 | print('Test accuracy:', score[1]) 75 | 76 | model.save("cifar10_cnn_SGD_model.h5") 77 | 78 | -------------------------------------------------------------------------------- /chapter_14/cifar10_cnn_animal_vehicles_real_labels.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: cifar10_cnn_animal_vehicles_real_labels.py 3 | # 4 | # Report on animal vs vehicles predictions and actual 5 | # label of errors. 6 | # 7 | # RTK, 05-Nov-2019 8 | # Last update: 05-Nov-2019 9 | # 10 | ################################################################ 11 | 12 | import numpy as np 13 | from keras.models import load_model 14 | 15 | x_test = np.load("../data/cifar10/cifar10_test_images.npy")/255.0 16 | y_label= np.load("../data/cifar10/cifar10_test_labels.npy") 17 | y_test = np.load("../data/cifar10_test_animal_vehicles_labels.npy") 18 | model = load_model("../data/cifar10_cnn_animal_vehicles_model.h5") 19 | 20 | pp = model.predict(x_test) 21 | p = np.zeros(pp.shape[0], dtype="uint8") 22 | for i in range(pp.shape[0]): 23 | p[i] = 0 if (pp[i,0] > pp[i,1]) else 1 24 | 25 | hp = [] 26 | hn = [] 27 | for i in range(len(y_test)): 28 | if (p[i] == 0) and (y_test[i] == 1): 29 | hn.append(y_label[i]) 30 | elif (p[i] == 1) and (y_test[i] == 0): 31 | hp.append(y_label[i]) 32 | hp = np.array(hp) 33 | hn = np.array(hn) 34 | a = np.histogram(hp, bins=10, range=[0,9])[0] 35 | b = np.histogram(hn, bins=10, range=[0,9])[0] 36 | print("vehicles as animals: %s" % np.array2string(a)) 37 | print("animals as vehicles: %s" % np.array2string(b)) 38 | 39 | -------------------------------------------------------------------------------- /chapter_14/cifar10_cnn_arbitrary.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: cifar10_cnn_arbitrary.py 3 | # 4 | # MNIST architecture applied to CIFAR-10 5 | # using arbitrary binary label assignment 6 | # 7 | # RTK, 21-Oct-2019 8 | # Last update: 21-Oct-2019 9 | # 10 | ################################################################ 11 | 12 | import keras 13 | from keras.datasets import mnist 14 | from keras.models import Sequential 15 | from keras.layers import Dense, Dropout, Flatten 16 | from keras.layers import Conv2D, MaxPooling2D, Activation 17 | from keras import backend as K 18 | import numpy as np 19 | import sys 20 | 21 | if (len(sys.argv) == 1): 22 | print("cifar10_cnn_arbitrary ") 23 | exit(0) 24 | 25 | batch_size = 128 26 | num_classes = 2 27 | epochs = 12 28 | 29 | img_rows, img_cols = 32, 32 30 | 31 | x_train = np.load("../data/cifar10/cifar10_train_images.npy") 32 | y_train = np.load(sys.argv[1]) 33 | 34 | x_test = np.load("../data/cifar10/cifar10_test_images.npy") 35 | y_test = np.load(sys.argv[2]) 36 | 37 | if K.image_data_format() == 'channels_first': 38 | x_train = x_train.reshape(x_train.shape[0], 3, img_rows, img_cols) 39 | x_test = x_test.reshape(x_test.shape[0], 3, img_rows, img_cols) 40 | input_shape = (3, img_rows, img_cols) 41 | else: 42 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 3) 43 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 3) 44 | input_shape = (img_rows, img_cols, 3) 45 | 46 | x_train = x_train.astype('float32') 47 | x_test = x_test.astype('float32') 48 | x_train /= 255 49 | x_test /= 255 50 | 51 | y_train = keras.utils.to_categorical(y_train, num_classes) 52 | y_test = keras.utils.to_categorical(y_test, num_classes) 53 | 54 | model = Sequential() 55 | model.add(Conv2D(32, kernel_size=(3, 3), 56 | activation='relu', 57 | input_shape=input_shape)) 58 | model.add(Conv2D(64, (3, 3), activation='relu')) 59 | model.add(MaxPooling2D(pool_size=(2, 2))) 60 | model.add(Dropout(0.25)) 61 | model.add(Flatten()) 62 | model.add(Dense(128, activation='relu')) 63 | model.add(Dropout(0.5)) 64 | model.add(Dense(num_classes, activation='softmax')) 65 | 66 | model.compile(loss=keras.losses.categorical_crossentropy, 67 | optimizer=keras.optimizers.Adadelta(), 68 | metrics=['accuracy']) 69 | 70 | model.fit(x_train, y_train, 71 | batch_size=batch_size, 72 | epochs=epochs, 73 | verbose=1, 74 | validation_data=(x_test[:1000], y_test[:1000])) 75 | score = model.evaluate(x_test[1000:], y_test[1000:], verbose=0) 76 | print('Test loss:', score[0]) 77 | print('Test accuracy:', score[1]) 78 | 79 | model.save(sys.argv[3]) 80 | 81 | -------------------------------------------------------------------------------- /chapter_14/cifar10_make_animal_vehicle.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: cifar10_make_animal_vehicle.py 3 | # 4 | # New labels mapping CIFAR-10 images to two 5 | # classes - animals (1) or vehicles (0) 6 | # 7 | # RTK, 20-Oct-2019 8 | # Last update: 20-Oct-2019 9 | # 10 | ################################################################ 11 | 12 | import numpy as np 13 | 14 | y_train = np.load("../data/cifar10/cifar10_train_labels.npy") 15 | y_test = np.load("../data/cifar10/cifar10_test_labels.npy") 16 | 17 | for i in range(len(y_train)): 18 | if (y_train[i] in [0,1,8,9]): 19 | y_train[i] = 0 20 | else: 21 | y_train[i] = 1 22 | 23 | for i in range(len(y_test)): 24 | if (y_test[i] in [0,1,8,9]): 25 | y_test[i] = 0 26 | else: 27 | y_test[i] = 1 28 | 29 | np.save("../data/cifar10/cifar10_train_animal_vehicle_labels.npy", y_train) 30 | np.save("../data/cifar10/cifar10_test_animal_vehicle_labels.npy", y_test) 31 | 32 | -------------------------------------------------------------------------------- /chapter_14/cifar10_one_vs_many.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: cifar10_one_vs_many.py 3 | # 4 | # Compare a single multiclass model vs individual binary models 5 | # 6 | # RTK, 21-Oct-2019 7 | # Last update: 06-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import numpy as np 12 | from keras.models import load_model 13 | 14 | def main(): 15 | x_test = np.load("../data/cifar10/cifar10_test_images.npy")/255.0 16 | y_test = np.load("../data/cifar10/cifar10_test_labels.npy") 17 | 18 | # Load the models 19 | mm = load_model("cifar10_cnn_model.h5") 20 | m = [] 21 | for i in range(10): 22 | m.append(load_model("cifar10_cnn_%d_model.h5" % i)) 23 | 24 | # Multiclass predictions 25 | mp = np.argmax(mm.predict(x_test), axis=1) 26 | 27 | # Individual binary predictions 28 | p = np.zeros((10,10000), dtype="float32") 29 | 30 | for i in range(10): 31 | p[i,:] = m[i].predict(x_test)[:,1] 32 | 33 | bp = np.argmax(p, axis=0) 34 | 35 | # Confusion matrices 36 | cm = np.zeros((10,10), dtype="uint16") 37 | cb = np.zeros((10,10), dtype="uint16") 38 | 39 | for i in range(10000): 40 | cm[y_test[i],mp[i]] += 1 41 | cb[y_test[i],bp[i]] += 1 42 | 43 | np.save("cifar10_multiclass_conf_mat.npy", cm) 44 | np.save("cifar10_binary_conf_mat.npy", cb) 45 | 46 | # Confusion matrices 47 | print() 48 | print("One-vs-rest confusion matrix (rows true, cols predicted):") 49 | print("%s" % np.array2string(100*(cb/1000.0), precision=1)) 50 | print() 51 | print("Multiclass confusion matrix:") 52 | print("%s" % np.array2string(100*(cm/1000.0), precision=1)) 53 | 54 | # Report on differences 55 | db = np.diag(100*(cb/1000.0)) 56 | dm = np.diag(100*(cm/1000.0)) 57 | df = db - dm 58 | sb = np.array2string(db, precision=1)[1:-1] 59 | sm = np.array2string(dm, precision=1)[1:-1] 60 | sd = np.array2string(df, precision=1)[1:-1] 61 | print() 62 | print("Comparing per class accuracies, one-vs-rest or multiclass:") 63 | print() 64 | print(" one-vs-rest: %s" % sb) 65 | print(" multiclass : %s" % sm) 66 | print(" difference : %s" % sd) 67 | 68 | # Report overall accuracies 69 | print() 70 | print("Overall accuracy:") 71 | print(" one-vs-rest: %0.1f%%" % np.diag(100*(cb/1000.0)).mean()) 72 | print(" multiclass : %0.1f%%" % np.diag(100*(cm/1000.0)).mean()) 73 | print() 74 | 75 | main() 76 | 77 | -------------------------------------------------------------------------------- /chapter_14/make_label_files.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: make_label_files.py 3 | # 4 | # New labels mapping CIFAR-10 images to two 5 | # classes 6 | # 7 | # RTK, 21-Oct-2019 8 | # Last update: 21-Oct-2019 9 | # 10 | ################################################################ 11 | 12 | import numpy as np 13 | import sys 14 | 15 | if (len(sys.argv) == 1): 16 | print("make_label_files ") 17 | exit(0) 18 | 19 | class1 = eval("["+sys.argv[1]+"]") 20 | 21 | y_train = np.load("../data/cifar10/cifar10_train_labels.npy") 22 | y_test = np.load("../data/cifar10/cifar10_test_labels.npy") 23 | 24 | for i in range(len(y_train)): 25 | if (y_train[i] in class1): 26 | y_train[i] = 1 27 | else: 28 | y_train[i] = 0 29 | 30 | for i in range(len(y_test)): 31 | if (y_test[i] in class1): 32 | y_test[i] = 1 33 | else: 34 | y_test[i] = 0 35 | 36 | np.save(sys.argv[2], y_train) 37 | np.save(sys.argv[3], y_test) 38 | 39 | -------------------------------------------------------------------------------- /chapter_14/roc_curve_area.py: -------------------------------------------------------------------------------- 1 | from sklearn.metrics import roc_auc_score, roc_curve 2 | def roc_curve_area(model, x, y): 3 | pp = model.predict(x) 4 | p = np.zeros(pp.shape[0], dtype="uint8") 5 | for i in range(pp.shape[0]): 6 | p[i] = 0 if (pp[i,0] > pp[i,1]) else 1 7 | auc = roc_auc_score(y,p) 8 | roc = roc_curve(y,pp[:,1]) 9 | return [auc, roc] 10 | 11 | -------------------------------------------------------------------------------- /chapter_14/tally_predictions.py: -------------------------------------------------------------------------------- 1 | def tally_predictions(model, x, y): 2 | pp = model.predict(x) 3 | p = np.zeros(pp.shape[0], dtype="uint8") 4 | for i in range(pp.shape[0]): 5 | p[i] = 0 if (pp[i,0] > pp[i,1]) else 1 6 | tp = tn = fp = fn = 0 7 | for i in range(len(y)): 8 | if (p[i] == 0) and (y[i] == 0): 9 | tn += 1 10 | elif (p[i] == 0) and (y[i] == 1): 11 | fn += 1 12 | elif (p[i] == 1) and (y[i] == 0): 13 | fp += 1 14 | else: 15 | tp += 1 16 | score = float(tp+tn) / float(tp+tn+fp+fn) 17 | return [tp, tn, fp, fn, score] 18 | 19 | -------------------------------------------------------------------------------- /chapter_14/train_single_models: -------------------------------------------------------------------------------- 1 | # 2 | # file: train_single_models 3 | # 4 | # Train one class vs rest models 5 | # 6 | # RTK, 21-Oct-2019 7 | # Last update: 21-Oct-2019 8 | # 9 | ################################################################ 10 | 11 | # Make the label files 12 | python3 make_label_files.py 0 cifar10_train_0.npy cifar10_test_0.npy 13 | python3 make_label_files.py 1 cifar10_train_1.npy cifar10_test_1.npy 14 | python3 make_label_files.py 2 cifar10_train_2.npy cifar10_test_2.npy 15 | python3 make_label_files.py 3 cifar10_train_3.npy cifar10_test_3.npy 16 | python3 make_label_files.py 4 cifar10_train_4.npy cifar10_test_4.npy 17 | python3 make_label_files.py 5 cifar10_train_5.npy cifar10_test_5.npy 18 | python3 make_label_files.py 6 cifar10_train_6.npy cifar10_test_6.npy 19 | python3 make_label_files.py 7 cifar10_train_7.npy cifar10_test_7.npy 20 | python3 make_label_files.py 8 cifar10_train_8.npy cifar10_test_8.npy 21 | python3 make_label_files.py 9 cifar10_train_9.npy cifar10_test_9.npy 22 | 23 | # Train the individual models 24 | python3 cifar10_cnn_arbitrary.py cifar10_train_0.npy cifar10_test_0.npy cifar10_cnn_0_model.h5 >cifar10_cnn_0_results.txt 2>/dev/null 25 | python3 cifar10_cnn_arbitrary.py cifar10_train_1.npy cifar10_test_1.npy cifar10_cnn_1_model.h5 >cifar10_cnn_1_results.txt 2>/dev/null 26 | python3 cifar10_cnn_arbitrary.py cifar10_train_2.npy cifar10_test_2.npy cifar10_cnn_2_model.h5 >cifar10_cnn_2_results.txt 2>/dev/null 27 | python3 cifar10_cnn_arbitrary.py cifar10_train_3.npy cifar10_test_3.npy cifar10_cnn_3_model.h5 >cifar10_cnn_3_results.txt 2>/dev/null 28 | python3 cifar10_cnn_arbitrary.py cifar10_train_4.npy cifar10_test_4.npy cifar10_cnn_4_model.h5 >cifar10_cnn_4_results.txt 2>/dev/null 29 | python3 cifar10_cnn_arbitrary.py cifar10_train_5.npy cifar10_test_5.npy cifar10_cnn_5_model.h5 >cifar10_cnn_5_results.txt 2>/dev/null 30 | python3 cifar10_cnn_arbitrary.py cifar10_train_6.npy cifar10_test_6.npy cifar10_cnn_6_model.h5 >cifar10_cnn_6_results.txt 2>/dev/null 31 | python3 cifar10_cnn_arbitrary.py cifar10_train_7.npy cifar10_test_7.npy cifar10_cnn_7_model.h5 >cifar10_cnn_7_results.txt 2>/dev/null 32 | python3 cifar10_cnn_arbitrary.py cifar10_train_8.npy cifar10_test_8.npy cifar10_cnn_8_model.h5 >cifar10_cnn_8_results.txt 2>/dev/null 33 | python3 cifar10_cnn_arbitrary.py cifar10_train_9.npy cifar10_test_9.npy cifar10_cnn_9_model.h5 >cifar10_cnn_9_results.txt 2>/dev/null 34 | 35 | -------------------------------------------------------------------------------- /chapter_15/ensemble.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import numpy as np 3 | from sklearn.ensemble import RandomForestClassifier 4 | from sklearn.neural_network import MLPClassifier 5 | 6 | p0 = np.load("prob_run0.npy") 7 | p1 = np.load("prob_run1.npy") 8 | p2 = np.load("prob_run2.npy") 9 | p3 = np.load("prob_run3.npy") 10 | p4 = np.load("prob_run4.npy") 11 | p5 = np.load("prob_run5.npy") 12 | 13 | y_test = np.load("../data/audio/ESC-10/esc10_spect_test_labels.npy") 14 | 15 | prob = (p0+p1+p2+p3+p4+p5)/6.0 16 | p = np.argmax(prob, axis=1) 17 | 18 | cc = np.zeros((10,10)) 19 | for i in range(len(y_test)): 20 | cc[y_test[i],p[i]] += 1 21 | 22 | print() 23 | print("Ensemble average:") 24 | print() 25 | 26 | if (len(sys.argv) > 1): 27 | print(np.array2string(cc.astype("uint32"))) 28 | print() 29 | 30 | cp = 100.0 * cc / cc.sum(axis=1) 31 | print(np.array2string(cp, precision=1)) 32 | print() 33 | 34 | print("Overall accuracy = %0.2f%%" % (100.0*np.diag(cc).sum()/cc.sum(),)) 35 | print() 36 | 37 | print() 38 | print("Ensemble max:") 39 | print() 40 | 41 | p = np.zeros(len(y_test), dtype="uint8") 42 | for i in range(len(y_test)): 43 | mx = 0.0 44 | idx = 0 45 | t = np.array([p0[i],p1[i],p2[i],p3[i],p4[i],p5[i]]) 46 | p[i] = np.argmax(t.reshape(60)) % 10 47 | 48 | cc = np.zeros((10,10)) 49 | for i in range(len(y_test)): 50 | cc[y_test[i],p[i]] += 1 51 | 52 | if (len(sys.argv) > 1): 53 | print(np.array2string(cc.astype("uint32"))) 54 | print() 55 | 56 | cp = 100.0 * cc / cc.sum(axis=1) 57 | print(np.array2string(cp, precision=1)) 58 | print() 59 | 60 | print("Overall accuracy = %0.2f%%" % (100.0*np.diag(cc).sum()/cc.sum(),)) 61 | print() 62 | 63 | # Voting 64 | print() 65 | print("Ensemble voting:") 66 | print() 67 | t = np.zeros((6,len(y_test)), dtype="uint32") 68 | t[0,:] = np.argmax(p0, axis=1) 69 | t[1,:] = np.argmax(p1, axis=1) 70 | t[2,:] = np.argmax(p2, axis=1) 71 | t[3,:] = np.argmax(p3, axis=1) 72 | t[4,:] = np.argmax(p4, axis=1) 73 | t[5,:] = np.argmax(p5, axis=1) 74 | p = np.zeros(len(y_test), dtype="uint8") 75 | for i in range(len(y_test)): 76 | q = np.bincount(t[:,i]) 77 | p[i] = np.argmax(q) 78 | 79 | cc = np.zeros((10,10)) 80 | for i in range(len(y_test)): 81 | cc[y_test[i],p[i]] += 1 82 | 83 | if (len(sys.argv) > 1): 84 | print(np.array2string(cc.astype("uint32"))) 85 | print() 86 | 87 | cp = 100.0 * cc / cc.sum(axis=1) 88 | print(np.array2string(cp, precision=1)) 89 | print() 90 | 91 | print("Overall accuracy = %0.2f%%" % (100.0*np.diag(cc).sum()/cc.sum(),)) 92 | print() 93 | 94 | -------------------------------------------------------------------------------- /chapter_15/esc10_audio_cnn_deep0.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: esc10_audio_cnn_deep.py 3 | # 4 | # 1D convolutional network, deep depth 5 | # 6 | # RTK, 13-Nov-2019 7 | # Last update: 13-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import sys 12 | import keras 13 | from keras.models import Sequential 14 | from keras.layers import Dense, Dropout, Flatten 15 | from keras.layers import Conv1D, MaxPooling1D 16 | from keras import backend as K 17 | import numpy as np 18 | 19 | batch_size = 32 20 | num_classes = 10 21 | epochs = 16 22 | nsamp = (441*2,1) 23 | 24 | kstr = sys.argv[1] 25 | z=int(kstr) 26 | 27 | x_train = np.load("../data/audio/ESC-10/esc10_raw_train_audio.npy") 28 | y_train = np.load("../data/audio/ESC-10/esc10_raw_train_labels.npy") 29 | x_test = np.load("../data/audio/ESC-10/esc10_raw_test_audio.npy") 30 | y_test = np.load("../data/audio/ESC-10/esc10_raw_test_labels.npy") 31 | 32 | x_train = (x_train.astype('float32') + 32768) / 65536 33 | x_test = (x_test.astype('float32') + 32768) / 65536 34 | 35 | y_train = keras.utils.to_categorical(y_train, num_classes) 36 | y_test = keras.utils.to_categorical(y_test, num_classes) 37 | 38 | model = Sequential() 39 | model.add(Conv1D(32, kernel_size=z, 40 | activation='relu', 41 | input_shape=nsamp)) 42 | 43 | model.add(Conv1D(64, kernel_size=3, activation='relu')) 44 | model.add(Conv1D(64, kernel_size=3, activation='relu')) 45 | model.add(MaxPooling1D(pool_size=3)) 46 | model.add(Dropout(0.25)) 47 | 48 | model.add(Conv1D(64, kernel_size=3, activation='relu')) 49 | model.add(Conv1D(64, kernel_size=3, activation='relu')) 50 | model.add(MaxPooling1D(pool_size=3)) 51 | model.add(Dropout(0.25)) 52 | 53 | model.add(Flatten()) 54 | model.add(Dense(512, activation='relu')) 55 | model.add(Dropout(0.5)) 56 | model.add(Dense(num_classes, activation='softmax')) 57 | 58 | model.compile(loss=keras.losses.categorical_crossentropy, 59 | optimizer=keras.optimizers.Adam(), 60 | metrics=['accuracy']) 61 | 62 | print("Model parameters = %d" % model.count_params()) 63 | print(model.summary()) 64 | 65 | history = model.fit(x_train, y_train, 66 | batch_size=batch_size, 67 | epochs=epochs, 68 | verbose=1, 69 | validation_data=(x_test[:160], y_test[:160])) 70 | 71 | score = model.evaluate(x_test[160:], y_test[160:], verbose=0) 72 | print('Test loss:', score[0]) 73 | print('Test accuracy:', score[1]) 74 | 75 | model.save("esc10_audio_cnn_deep_%s_model.h5" % kstr) 76 | 77 | -------------------------------------------------------------------------------- /chapter_15/esc10_audio_cnn_medium.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: esc10_audio_cnn_medium.py 3 | # 4 | # 1D convolutional network, medium depth 5 | # 6 | # RTK, 13-Nov-2019 7 | # Last update: 13-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import sys 12 | import keras 13 | from keras.models import Sequential 14 | from keras.layers import Dense, Dropout, Flatten 15 | from keras.layers import Conv1D, MaxPooling1D 16 | from keras import backend as K 17 | import numpy as np 18 | 19 | batch_size = 32 20 | num_classes = 10 21 | epochs = 16 22 | nsamp = (441*2,1) 23 | 24 | kstr = sys.argv[1] 25 | z=int(kstr) 26 | 27 | x_train = np.load("../data/audio/ESC-10/esc10_raw_train_audio.npy") 28 | y_train = np.load("../data/audio/ESC-10/esc10_raw_train_labels.npy") 29 | x_test = np.load("../data/audio/ESC-10/esc10_raw_test_audio.npy") 30 | y_test = np.load("../data/audio/ESC-10/esc10_raw_test_labels.npy") 31 | 32 | x_train = (x_train.astype('float32') + 32768) / 65536 33 | x_test = (x_test.astype('float32') + 32768) / 65536 34 | 35 | y_train = keras.utils.to_categorical(y_train, num_classes) 36 | y_test = keras.utils.to_categorical(y_test, num_classes) 37 | 38 | model = Sequential() 39 | model.add(Conv1D(32, kernel_size=z, 40 | activation='relu', 41 | input_shape=nsamp)) 42 | model.add(Conv1D(64, kernel_size=3, activation='relu')) 43 | model.add(Conv1D(64, kernel_size=3, activation='relu')) 44 | model.add(MaxPooling1D(pool_size=3)) 45 | model.add(Dropout(0.25)) 46 | model.add(Flatten()) 47 | model.add(Dense(512, activation='relu')) 48 | model.add(Dropout(0.5)) 49 | model.add(Dense(num_classes, activation='softmax')) 50 | 51 | model.compile(loss=keras.losses.categorical_crossentropy, 52 | optimizer=keras.optimizers.Adam(), 53 | metrics=['accuracy']) 54 | 55 | print("Model parameters = %d" % model.count_params()) 56 | print(model.summary()) 57 | 58 | history = model.fit(x_train, y_train, 59 | batch_size=batch_size, 60 | epochs=epochs, 61 | verbose=1, 62 | validation_data=(x_test[:160], y_test[:160])) 63 | 64 | score = model.evaluate(x_test[160:], y_test[160:], verbose=0) 65 | print('Test loss:', score[0]) 66 | print('Test accuracy:', score[1]) 67 | 68 | model.save("esc10_audio_cnn_medium_%s_model.h5" % kstr) 69 | 70 | -------------------------------------------------------------------------------- /chapter_15/esc10_audio_cnn_shallow.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: esc10_audio_cnn_shallow.py 3 | # 4 | # 1D convolutional network, shallow 5 | # 6 | # RTK, 13-Nov-2019 7 | # Last update: 13-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import sys 12 | import keras 13 | from keras.models import Sequential 14 | from keras.layers import Dense, Dropout, Flatten 15 | from keras.layers import Conv1D, MaxPooling1D, AveragePooling1D 16 | from keras import backend as K 17 | import numpy as np 18 | 19 | batch_size = 32 20 | num_classes = 10 21 | epochs = 16 22 | nsamp = (441*2,1) 23 | 24 | kstr = sys.argv[1] 25 | z=int(kstr) 26 | 27 | x_train = np.load("../data/audio/ESC-10/esc10_raw_train_audio.npy") 28 | y_train = np.load("../data/audio/ESC-10/esc10_raw_train_labels.npy") 29 | x_test = np.load("../data/audio/ESC-10/esc10_raw_test_audio.npy") 30 | y_test = np.load("../data/audio/ESC-10/esc10_raw_test_labels.npy") 31 | 32 | x_train = (x_train.astype('float32') + 32768) / 65536 33 | x_test = (x_test.astype('float32') + 32768) / 65536 34 | 35 | y_train = keras.utils.to_categorical(y_train, num_classes) 36 | y_test = keras.utils.to_categorical(y_test, num_classes) 37 | 38 | model = Sequential() 39 | model.add(Conv1D(32, kernel_size=z, 40 | activation='relu', 41 | input_shape=nsamp)) 42 | model.add(MaxPooling1D(pool_size=3)) 43 | model.add(Dropout(0.25)) 44 | model.add(Flatten()) 45 | model.add(Dense(512, activation='relu')) 46 | model.add(Dropout(0.5)) 47 | model.add(Dense(num_classes, activation='softmax')) 48 | 49 | model.compile(loss=keras.losses.categorical_crossentropy, 50 | optimizer=keras.optimizers.Adam(), 51 | metrics=['accuracy']) 52 | 53 | print("Model parameters = %d" % model.count_params()) 54 | print(model.summary()) 55 | 56 | history = model.fit(x_train, y_train, 57 | batch_size=batch_size, 58 | epochs=epochs, 59 | verbose=1, 60 | validation_data=(x_test[:160], y_test[:160])) 61 | 62 | score = model.evaluate(x_test[160:], y_test[160:], verbose=0) 63 | print('Test loss:', score[0]) 64 | print('Test accuracy:', score[1]) 65 | 66 | model.save("esc10_audio_cnn_shallow_%s_model.h5" % kstr) 67 | 68 | -------------------------------------------------------------------------------- /chapter_15/esc10_audio_mlp.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: esc10_audio_mlp.py 3 | # 4 | # Traditional MLP 5 | # 6 | # RTK, 13-Nov-2019 7 | # Last update: 13-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import keras 12 | from keras.models import Sequential 13 | from keras.layers import Dense, Dropout, Flatten 14 | from keras import backend as K 15 | import numpy as np 16 | 17 | batch_size = 32 18 | num_classes = 10 19 | epochs = 16 20 | nsamp = (441*2,1) 21 | 22 | x_train = np.load("../data/audio/ESC-10/esc10_raw_train_audio.npy") 23 | y_train = np.load("../data/audio/ESC-10/esc10_raw_train_labels.npy") 24 | x_test = np.load("../data/audio/ESC-10/esc10_raw_test_audio.npy") 25 | y_test = np.load("../data/audio/ESC-10/esc10_raw_test_labels.npy") 26 | 27 | x_train = (x_train.astype('float32') + 32768) / 65536 28 | x_test = (x_test.astype('float32') + 32768) / 65536 29 | 30 | y_train = keras.utils.to_categorical(y_train, num_classes) 31 | y_test = keras.utils.to_categorical(y_test, num_classes) 32 | 33 | model = Sequential() 34 | model.add(Dense(1024, activation='relu', input_shape=nsamp)) 35 | model.add(Dropout(0.5)) 36 | model.add(Dense(512, activation='relu')) 37 | model.add(Dropout(0.5)) 38 | model.add(Flatten()) 39 | model.add(Dense(num_classes, activation='softmax')) 40 | 41 | model.compile(loss=keras.losses.categorical_crossentropy, 42 | optimizer=keras.optimizers.Adam(), 43 | metrics=['accuracy']) 44 | 45 | print("Model parameters = %d" % model.count_params()) 46 | print(model.summary()) 47 | 48 | history = model.fit(x_train, y_train, 49 | batch_size=batch_size, 50 | epochs=epochs, 51 | verbose=1, 52 | validation_data=(x_test, y_test)) 53 | 54 | score = model.evaluate(x_test, y_test, verbose=0) 55 | print('Test loss:', score[0]) 56 | print('Test accuracy:', score[1]) 57 | 58 | model.save("esc10_audio_mlp_model.h5") 59 | 60 | -------------------------------------------------------------------------------- /chapter_15/esc10_cnn_deep.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: esc10_cnn_deep.py 3 | # 4 | # Deeper architecture applied to augmented ESC-10 5 | # 6 | # RTK, 10-Nov-2019 7 | # Last update: 15-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import keras 12 | from keras.models import Sequential 13 | from keras.layers import Dense, Dropout, Flatten 14 | from keras.layers import Conv2D, MaxPooling2D 15 | import numpy as np 16 | 17 | batch_size = 16 18 | num_classes = 10 19 | epochs = 16 20 | img_rows, img_cols = 100, 160 21 | input_shape = (img_rows, img_cols, 3) 22 | 23 | x_train = np.load("../data/audio/ESC-10/esc10_spect_train_images.npy") 24 | y_train = np.load("../data/audio/ESC-10/esc10_spect_train_labels.npy") 25 | x_test = np.load("../data/audio/ESC-10/esc10_spect_test_images.npy") 26 | y_test = np.load("../data/audio/ESC-10/esc10_spect_test_labels.npy") 27 | 28 | x_train = x_train.astype('float32') / 255 29 | x_test = x_test.astype('float32') / 255 30 | 31 | y_train = keras.utils.to_categorical(y_train, num_classes) 32 | y_test = keras.utils.to_categorical(y_test, num_classes) 33 | 34 | model = Sequential() 35 | model.add(Conv2D(32, kernel_size=(3, 3), 36 | activation='relu', 37 | input_shape=input_shape)) 38 | 39 | model.add(Conv2D(64, (3, 3), activation='relu')) 40 | model.add(MaxPooling2D(pool_size=(2, 2))) 41 | model.add(Dropout(0.25)) 42 | 43 | model.add(Conv2D(64, (3, 3), activation='relu')) 44 | model.add(MaxPooling2D(pool_size=(2, 2))) 45 | model.add(Dropout(0.25)) 46 | 47 | model.add(Flatten()) 48 | model.add(Dense(128, activation='relu')) 49 | model.add(Dropout(0.5)) 50 | model.add(Dense(num_classes, activation='softmax')) 51 | 52 | model.compile(loss=keras.losses.categorical_crossentropy, 53 | optimizer=keras.optimizers.Adam(), 54 | metrics=['accuracy']) 55 | 56 | print("Model parameters = %d" % model.count_params()) 57 | print(model.summary()) 58 | 59 | history = model.fit(x_train, y_train, 60 | batch_size=batch_size, 61 | epochs=epochs, 62 | verbose=1, 63 | validation_data=(x_test, y_test)) 64 | 65 | score = model.evaluate(x_test, y_test, verbose=0) 66 | print('Test loss:', score[0]) 67 | print('Test accuracy:', score[1]) 68 | 69 | model.save("esc10_cnn_deep_3x3_model.h5") 70 | 71 | -------------------------------------------------------------------------------- /chapter_15/esc10_cnn_deep_bn.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: esc10_cnn_deep_bn.py 3 | # 4 | # Deeper architecture applied to augmented ESC-10 5 | # 6 | # RTK, 10-Nov-2019 7 | # Last update: 20-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import time 12 | import keras 13 | from keras.models import Sequential 14 | from keras.layers import Dense, Dropout, Flatten 15 | from keras.layers import Conv2D, MaxPooling2D 16 | from keras.layers import BatchNormalization 17 | from keras import backend as K 18 | import numpy as np 19 | 20 | batch_size = 16 21 | num_classes = 10 22 | epochs = 10 23 | img_rows, img_cols = 100, 160 24 | input_shape = (img_rows, img_cols, 3) 25 | 26 | x_train = np.load("../data/audio/ESC-10/esc10_spect_train_images.npy") 27 | y_train = np.load("../data/audio/ESC-10/esc10_spect_train_labels.npy") 28 | x_test = np.load("../data/audio/ESC-10/esc10_spect_test_images.npy") 29 | y_test = np.load("../data/audio/ESC-10/esc10_spect_test_labels.npy") 30 | 31 | x_train = x_train.astype('float32') / 255 32 | x_test = x_test.astype('float32') / 255 33 | 34 | y_train = keras.utils.to_categorical(y_train, num_classes) 35 | y_test = keras.utils.to_categorical(y_test, num_classes) 36 | 37 | model = Sequential() 38 | model.add(Conv2D(32, kernel_size=(3, 3), 39 | activation='relu', 40 | input_shape=input_shape)) 41 | model.add(BatchNormalization()) 42 | 43 | model.add(Conv2D(64, (3, 3), activation='relu')) 44 | model.add(BatchNormalization()) 45 | model.add(MaxPooling2D(pool_size=(2, 2))) 46 | 47 | model.add(Conv2D(64, (3, 3), activation='relu')) 48 | model.add(BatchNormalization()) 49 | model.add(MaxPooling2D(pool_size=(2, 2))) 50 | 51 | model.add(Flatten()) 52 | model.add(Dense(128, activation='relu')) 53 | model.add(BatchNormalization()) 54 | model.add(Dense(num_classes, activation='softmax')) 55 | 56 | model.compile(loss=keras.losses.categorical_crossentropy, 57 | optimizer=keras.optimizers.Adam(), 58 | metrics=['accuracy']) 59 | 60 | print("Model parameters = %d" % model.count_params()) 61 | print(model.summary()) 62 | st = time.time() 63 | history = model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test, y_test)) 68 | en = time.time() 69 | score = model.evaluate(x_test, y_test, verbose=0) 70 | print('Training time: %0.3f' % (en-st,)) 71 | print('Test loss:', score[0]) 72 | print('Test accuracy:', score[1]) 73 | 74 | model.save("esc10_cnn_deep_bn_3x3_model.h5") 75 | 76 | -------------------------------------------------------------------------------- /chapter_15/extract_esc10.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import shutil 4 | 5 | def main(): 6 | """Extract the 10 class subset""" 7 | 8 | classes = { 9 | "rain":0, 10 | "rooster":1, 11 | "crying_baby":2, 12 | "sea_waves":3, 13 | "clock_tick":4, 14 | "sneezing":5, 15 | "dog":6, 16 | "crackling_fire":7, 17 | "helicopter":8, 18 | "chainsaw":9, 19 | } 20 | 21 | with open("../data/audio/ESC-50-master/meta/esc50.csv") as f: 22 | lines = [i[:-1] for i in f.readlines()] 23 | lines = lines[1:] 24 | 25 | os.system("rm -rf ../data/audio/ESC-10") 26 | os.system("mkdir ../data/audio/ESC-10") 27 | os.system("mkdir ../data/audio/ESC-10/audio") 28 | 29 | meta = [] 30 | for line in lines: 31 | t = line.split(",") 32 | if (t[-3] == 'True'): 33 | meta.append("../data/audio/ESC-10/audio/%s %d" % (t[0],classes[t[3]])) 34 | src = "../data/audio/ESC-50-master/audio/"+t[0] 35 | dst = "../data/audio/ESC-10/audio/"+t[0] 36 | shutil.copy(src,dst) 37 | 38 | with open("../data/audio/ESC-10/filelist.txt","w") as f: 39 | for m in meta: 40 | f.write(m+"\n") 41 | 42 | 43 | main() 44 | 45 | -------------------------------------------------------------------------------- /chapter_15/make_augmented_1d_dataset.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: make_augmented_1d_dataset.py 3 | # 4 | # RTK, 13-Nov-2019 5 | # Last update: 13-Nov-2019 6 | # 7 | # Use the augmented .wav files. 8 | # 9 | ################################################################ 10 | 11 | import os 12 | import random 13 | import numpy as np 14 | from scipy.io.wavfile import read 15 | 16 | sr = 44100 # Hz 17 | N = 2*sr # number of samples to keep 18 | w = 100 # every 100 (0.01 s) 19 | 20 | # train 21 | afiles = [i[:-1] for i in open("../data/audio/ESC-10/augmented_train_filelist.txt")] 22 | trn = np.zeros((len(afiles),N//w,1), dtype="int16") 23 | lbl = np.zeros(len(afiles), dtype="uint8") 24 | 25 | for i,t in enumerate(afiles): 26 | f,c = t.split() 27 | trn[i,:,0] = read(f)[1][:N:w] 28 | lbl[i] = int(c) 29 | 30 | np.save("../data/audio/ESC-10/esc10_raw_train_audio.npy", trn) 31 | np.save("../data/audio/ESC-10/esc10_raw_train_labels.npy", lbl) 32 | 33 | # test 34 | afiles = [i[:-1] for i in open("../data/audio/ESC-10/augmented_test_filelist.txt")] 35 | tst = np.zeros((len(afiles),N//w,1), dtype="int16") 36 | lbl = np.zeros(len(afiles), dtype="uint8") 37 | 38 | for i,t in enumerate(afiles): 39 | f,c = t.split() 40 | tst[i,:,0] = read(f)[1][:N:w] 41 | lbl[i] = int(c) 42 | 43 | np.save("../data/audio/ESC-10/esc10_raw_test_audio.npy", tst) 44 | np.save("../data/audio/ESC-10/esc10_raw_test_labels.npy", lbl) 45 | 46 | -------------------------------------------------------------------------------- /chapter_15/make_augmented_spectrograms.py: -------------------------------------------------------------------------------- 1 | # 2 | # file: make_augmented_spectrograms.py 3 | # 4 | # Use sox to make the spectrogram images. 5 | # 6 | # RTK, 11-Nov-2019 7 | # Last update: 11-Nov-2019 8 | # 9 | ################################################################ 10 | 11 | import os 12 | import numpy as np 13 | from PIL import Image 14 | 15 | rows = 100 16 | cols = 160 17 | 18 | # train 19 | flist = [i[:-1] for i in open("../data/audio/ESC-10/augmented_train_filelist.txt")] 20 | N = len(flist) 21 | img = np.zeros((N,rows,cols,3), dtype="uint8") 22 | lbl = np.zeros(N, dtype="uint8") 23 | p = [] 24 | 25 | for i,f in enumerate(flist): 26 | src, c = f.split() 27 | os.system("sox %s -n spectrogram" % src) 28 | im = np.array(Image.open("spectrogram.png").convert("RGB")) 29 | im = im[42:542,58:858,:] 30 | im = Image.fromarray(im).resize((cols,rows)) 31 | img[i,:,:,:] = np.array(im) 32 | lbl[i] = int(c) 33 | p.append(os.path.abspath(src)) 34 | 35 | os.system("rm -rf spectrogram.png") 36 | p = np.array(p) 37 | idx = np.argsort(np.random.random(N)) 38 | img = img[idx] 39 | lbl = lbl[idx] 40 | p = p[idx] 41 | np.save("../data/audio/ESC-10/esc10_spect_train_images.npy", img) 42 | np.save("../data/audio/ESC-10/esc10_spect_train_labels.npy", lbl) 43 | np.save("../data/audio/ESC-10/esc10_spect_train_paths.npy", p) 44 | 45 | # test 46 | flist = [i[:-1] for i in open("../data/audio/ESC-10/augmented_test_filelist.txt")] 47 | N = len(flist) 48 | img = np.zeros((N,rows,cols,3), dtype="uint8") 49 | lbl = np.zeros(N, dtype="uint8") 50 | p = [] 51 | 52 | for i,f in enumerate(flist): 53 | src, c = f.split() 54 | os.system("sox %s -n spectrogram" % src) 55 | im = np.array(Image.open("spectrogram.png").convert("RGB")) 56 | im = im[42:542,58:858,:] 57 | im = Image.fromarray(im).resize((cols,rows)) 58 | img[i,:,:,:] = np.array(im) 59 | lbl[i] = int(c) 60 | p.append(os.path.abspath(src)) 61 | 62 | os.system("rm -rf spectrogram.png") 63 | p = np.array(p) 64 | idx = np.argsort(np.random.random(N)) 65 | img = img[idx] 66 | lbl = lbl[idx] 67 | p = p[idx] 68 | np.save("../data/audio/ESC-10/esc10_spect_test_images.npy", img) 69 | np.save("../data/audio/ESC-10/esc10_spect_test_labels.npy", lbl) 70 | np.save("../data/audio/ESC-10/esc10_spect_test_paths.npy", p) 71 | 72 | -------------------------------------------------------------------------------- /chapter_15/stats.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import numpy as np 3 | from keras.models import load_model 4 | 5 | model = load_model(sys.argv[1]) 6 | x_test = np.load("../data/audio/ESC-10/esc10_spect_test_images.npy")/255.0 7 | y_test = np.load("../data/audio/ESC-10/esc10_spect_test_labels.npy") 8 | 9 | prob = model.predict(x_test) 10 | p = np.argmax(prob, axis=1) 11 | 12 | cc = np.zeros((10,10)) 13 | for i in range(len(y_test)): 14 | cc[y_test[i],p[i]] += 1 15 | 16 | print() 17 | print(np.array2string(cc.astype("uint32"))) 18 | print() 19 | 20 | cp = 100.0 * cc / cc.sum(axis=1) 21 | print(np.array2string(cp, precision=1)) 22 | print() 23 | 24 | print("Overall accuracy = %0.2f%%" % (100.0*np.diag(cc).sum()/cc.sum(),)) 25 | print() 26 | 27 | # if second arg, output name for actual predictions 28 | if (len(sys.argv) > 2): 29 | np.save(sys.argv[2], prob) 30 | 31 | -------------------------------------------------------------------------------- /data/README.txt: -------------------------------------------------------------------------------- 1 | The datasets used in the book go in this directory. 2 | 3 | -------------------------------------------------------------------------------- /data/audio/README.txt: -------------------------------------------------------------------------------- 1 | The ESC-10 audio data goes here. 2 | 3 | -------------------------------------------------------------------------------- /data/breast/README.txt: -------------------------------------------------------------------------------- 1 | The breast cancer dataset goes here. 2 | 3 | -------------------------------------------------------------------------------- /data/cifar10/README.txt: -------------------------------------------------------------------------------- 1 | The CIFAR-10 data goes here. 2 | 3 | -------------------------------------------------------------------------------- /data/iris/README.txt: -------------------------------------------------------------------------------- 1 | The iris dataset goes here. 2 | 3 | -------------------------------------------------------------------------------- /data/mnist/README.txt: -------------------------------------------------------------------------------- 1 | The MNIST data goes here. 2 | 3 | -------------------------------------------------------------------------------- /tutorial.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkneusel9/PracticalDeepLearningPython/584f08e32e31d3029feb5138adc89328457bf65f/tutorial.pdf --------------------------------------------------------------------------------