-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprogram14_Speech.py
1117 lines (767 loc) · 33.8 KB
/
program14_Speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# we use: http://haythamfayek.com/2016/04/21/speech-processing-for-machine-learning.html
# we use Keras and TensorFlow
# we use: https://machinelearningmastery.com/how-to-make-classification-and-regression-predictions-for-deep-learning-models-in-keras/
# numpy
import numpy
# scipy
import scipy.io.wavfile
# we use DCT
from scipy.fftpack import dct
#sample_rate, signal = scipy.io.wavfile.read('/Users/dionelisnikolaos/Desktop/folder_desktop/MATLAB_Project2/TIMIT/TIMIT/TRAIN/DR1/FCJF0/SA1.WAV') # File assumed to be in the same directory
sample_rate, signal = scipy.io.wavfile.read('/Users/dionelisnikolaos/Desktop/folder_desktop/MATLAB_Project2/TIMIT/TIMIT/TRAIN/DR1/FCJF0/wavSA1') # File assumed to be in the same directory
# we keep the first 3.5 seconds
#signal = signal[0:int(3.5 * sample_rate)]
pre_emphasis = 0.97
emphasized_signal = numpy.append(signal[0], signal[1:] - pre_emphasis * signal[:-1])
frame_size = 0.025
frame_stride = 0.01
# we convert from seconds to samples
frame_length, frame_step = frame_size * sample_rate, frame_stride * sample_rate
# define the signal length
signal_length = len(emphasized_signal)
frame_length = int(round(frame_length))
frame_step = int(round(frame_step))
num_frames = int(numpy.ceil(float(numpy.abs(signal_length - frame_length)) / frame_step)) # Make sure that we have at least 1 frame
pad_signal_length = num_frames * frame_step + frame_length
z = numpy.zeros((pad_signal_length - signal_length))
pad_signal = numpy.append(emphasized_signal, z) # Pad Signal to make sure that all frames have equal number of samples without truncating any samples from the original signal
indices = numpy.tile(numpy.arange(0, frame_length), (num_frames, 1)) + numpy.tile(numpy.arange(0, num_frames * frame_step, frame_step), (frame_length, 1)).T
frames = pad_signal[indices.astype(numpy.int32, copy=False)]
# we use the hamming window
frames *= numpy.hamming(frame_length)
# frames *= 0.54 - 0.46 * numpy.cos((2 * numpy.pi * n) / (frame_length - 1)) # Explicit Implementation **
# STFT length
NFFT = 512
# magnitude spectrum
mag_frames = numpy.absolute(numpy.fft.rfft(frames, NFFT)) # Magnitude of the FFT
# power spectrum
pow_frames = ((1.0 / NFFT) * ((mag_frames) ** 2)) # Power Spectrum
nfilt = 40
low_freq_mel = 0
high_freq_mel = (2595 * numpy.log10(1 + (sample_rate / 2) / 700)) # Convert Hz to Mel
mel_points = numpy.linspace(low_freq_mel, high_freq_mel, nfilt + 2) # Equally spaced in Mel scale
hz_points = (700 * (10**(mel_points / 2595) - 1)) # Convert Mel to Hz
bin = numpy.floor((NFFT + 1) * hz_points / sample_rate)
fbank = numpy.zeros((nfilt, int(numpy.floor(NFFT / 2 + 1))))
for m in range(1, nfilt + 1):
f_m_minus = int(bin[m - 1]) # left
f_m = int(bin[m]) # center
f_m_plus = int(bin[m + 1]) # right
for k in range(f_m_minus, f_m):
fbank[m - 1, k] = (k - bin[m - 1]) / (bin[m] - bin[m - 1])
for k in range(f_m, f_m_plus):
fbank[m - 1, k] = (bin[m + 1] - k) / (bin[m + 1] - bin[m])
filter_banks = numpy.dot(pow_frames, fbank.T)
filter_banks = numpy.where(filter_banks == 0, numpy.finfo(float).eps, filter_banks) # Numerical Stability
filter_banks = 20 * numpy.log10(filter_banks) # dB
num_ceps = 12
mfcc = dct(filter_banks, type=2, axis=1, norm='ortho')[:, 1 : (num_ceps + 1)] # Keep 2-13
(nframes, ncoeff) = mfcc.shape
n = numpy.arange(ncoeff)
cep_lifter = 22
#cep_lifter = len(mfcc)
lift = 1 + (cep_lifter / 2) * numpy.sin(numpy.pi * n / cep_lifter)
mfcc *= lift #*
filter_banks -= (numpy.mean(filter_banks, axis=0) + 1e-8)
mfcc -= (numpy.mean(mfcc, axis=0) + 1e-8)
from __future__ import absolute_import
from __future__ import print_function
import pandas as pd
import numpy
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, zero_one_loss
from sklearn.model_selection import train_test_split
#data_dir="./datasets/KDD-CUP-99/"
#data_dir="./"
data_dir = "/Users/dionelisnikolaos/Downloads/"
raw_data_filename = data_dir + "kddcup.data"
#raw_data_filename = "/Users/dionelisnikolaos/Downloads/kddcup.data"
print ("Loading raw data")
raw_data = pd.read_csv(raw_data_filename, header=None)
print ("Transforming data")
# Categorize columns: "protocol", "service", "flag", "attack_type"
raw_data[1], protocols= pd.factorize(raw_data[1])
raw_data[2], services = pd.factorize(raw_data[2])
raw_data[3], flags = pd.factorize(raw_data[3])
raw_data[41], attacks = pd.factorize(raw_data[41])
# separate features (columns 1..40) and label (column 41)
features= raw_data.iloc[:,:raw_data.shape[1]-1]
labels= raw_data.iloc[:,raw_data.shape[1]-1:]
# convert them into numpy arrays
#features= numpy.array(features)
#labels= numpy.array(labels).ravel() # this becomes an 'horizontal' array
labels= labels.values.ravel() # this becomes a 'horizontal' array
# Separate data in train set and test set
df= pd.DataFrame(features)
# create training and testing vars
# Note: train_size + test_size < 1.0 means we are subsampling
# Use small numbers for slow classifiers, as KNN, Radius, SVC,...
X_train, X_test, y_train, y_test = train_test_split(df, labels, train_size=0.8, test_size=0.2)
print('')
print ("X_train, y_train:", X_train.shape, y_train.shape)
print ("X_test, y_test:", X_test.shape, y_test.shape)
print('')
print(X_train.shape)
print(y_train.shape)
print('')
print(X_train.shape)
print(X_test.shape)
print('')
# Training, choose model by commenting/uncommenting clf=
print ("Training model")
clf= RandomForestClassifier(n_jobs=-1, random_state=3, n_estimators=102)#, max_features=0.8, min_samples_leaf=3, n_estimators=500, min_samples_split=3, random_state=10, verbose=1)
#clf = DecisionTreeClassifier(criterion='gini', splitter='best', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, class_weight=None, presort=False)
trained_model= clf.fit(X_train, y_train)
print ("Score: ", trained_model.score(X_train, y_train))
# Predicting
print ("Predicting")
y_pred = clf.predict(X_test)
print ("Computing performance metrics")
results = confusion_matrix(y_test, y_pred)
error = zero_one_loss(y_test, y_pred)
print ("Confusion matrix:\n", results)
print ("Error: ", error)
# KDD99 Dataset
# use: https://github.com/ghuecas/kdd99ml
# https://github.com/ghuecas/kdd99ml
# we use: https://github.com/ghuecas/kdd99ml
import json
import datetime
import os
import numpy as np
# make keras deterministic
#np.random.seed(42)
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.callbacks import CallbackList, ModelCheckpoint
from keras.regularizers import l2
import os
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
#from keras.applications.inception_v3 import InceptionV3
#base_model = InceptionV3(weights='imagenet', include_top=True)
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
num_train_images = 1500
num_test_images = 100
#-------------------
# organize imports
#-------------------
import numpy as np
import os
import h5py
import glob
import cv2
# we use opencv-python
import cv2
# we use keras
from keras.preprocessing import image
#------------------------
# dataset pre-processing
#------------------------
#train_path = "G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\train"
#test_path = "G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\test"
#train_path = "G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\train"
train_path = "/Users/dionelisnikolaos/Downloads/dataset/train"
#test_path = "G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\test"
test_path = "/Users/dionelisnikolaos/Downloads/dataset/test"
train_labels = os.listdir(train_path)
test_labels = os.listdir(test_path)
# tunable parameters
image_size = (64, 64)
num_train_images = 1500
num_test_images = 100
num_channels = 3
# train_x dimension = {(64*64*3), 1500}
# train_y dimension = {1, 1500}
# test_x dimension = {(64*64*3), 100}
# test_y dimension = {1, 100}
train_x = np.zeros(((image_size[0]*image_size[1]*num_channels), num_train_images))
train_y = np.zeros((1, num_train_images))
test_x = np.zeros(((image_size[0]*image_size[1]*num_channels), num_test_images))
test_y = np.zeros((1, num_test_images))
#----------------
# TRAIN dataset
#----------------
count = 0
num_label = 0
for i, label in enumerate(train_labels):
cur_path = train_path + "\\" + label
for image_path in glob.glob(cur_path + "/*.jpg"):
img = image.load_img(image_path, target_size=image_size)
x = image.img_to_array(img)
x = x.flatten()
x = np.expand_dims(x, axis=0)
train_x[:,count] = x
train_y[:,count] = num_label
count += 1
num_label += 1
#--------------
# TEST dataset
#--------------
count = 0
num_label = 0
for i, label in enumerate(test_labels):
cur_path = test_path + "\\" + label
for image_path in glob.glob(cur_path + "/*.jpg"):
img = image.load_img(image_path, target_size=image_size)
x = image.img_to_array(img)
x = x.flatten()
x = np.expand_dims(x, axis=0)
test_x[:,count] = x
test_y[:,count] = num_label
count += 1
num_label += 1
#------------------
# standardization
#------------------
train_x = train_x/255.
test_x = test_x/255.
print ("train_labels : " + str(train_labels))
print ("train_x shape: " + str(train_x.shape))
print ("train_y shape: " + str(train_y.shape))
print ("test_x shape : " + str(test_x.shape))
print ("test_y shape : " + str(test_y.shape))
print('')
# train_x and test_x
print(train_x.shape)
print(test_x.shape)
# https://gogul09.github.io/software/neural-nets-logistic-regression
# use: https://gogul09.github.io/software/neural-nets-logistic-regression
#-----------------
# save using h5py
#-----------------
h5_train = h5py.File("train_x.h5", 'w')
h5_train.create_dataset("data_train", data=np.array(train_x))
h5_train.close()
h5_test = h5py.File("test_x.h5", 'w')
h5_test.create_dataset("data_test", data=np.array(test_x))
h5_test.close()
def sigmoid(z):
return (1/(1+np.exp(-z)))
def init_params(dimension):
w = np.zeros((dimension, 1))
b = 0
return w, b
def propagate(w, b, X, Y):
# num of training samples
m = X.shape[1]
# forward pass
A = sigmoid(np.dot(w.T,X) + b)
cost = (-1/m)*(np.sum(np.multiply(Y,np.log(A)) + np.multiply((1-Y),np.log(1-A))))
# back propagation
dw = (1/m)*(np.dot(X, (A-Y).T))
db = (1/m)*(np.sum(A-Y))
cost = np.squeeze(cost)
# gradient dictionary
grads = {"dw": dw, "db": db}
return grads, cost
def optimize(w, b, X, Y, epochs, lr):
costs = []
for i in range(epochs):
# calculate gradients
grads, cost = propagate(w, b, X, Y)
# get gradients
dw = grads["dw"]
db = grads["db"]
# update rule
w = w - (lr*dw)
b = b - (lr*db)
if i % 100 == 0:
costs.append(cost)
print ("cost after %i epochs: %f" %(i, cost))
# param dict
params = {"w": w, "b": b}
# gradient dict
grads = {"dw": dw, "db": db}
return params, grads, costs
def predict(w, b, X):
m = X.shape[1]
Y_predict = np.zeros((1,m))
w = w.reshape(X.shape[0], 1)
A = sigmoid(np.dot(w.T, X) + b)
for i in range(A.shape[1]):
if A[0, i] <= 0.5:
Y_predict[0, i] = 0
else:
Y_predict[0,i] = 1
return Y_predict
def predict_image(w, b, X):
Y_predict = None
w = w.reshape(X.shape[0], 1)
A = sigmoid(np.dot(w.T, X) + b)
for i in range(A.shape[1]):
if A[0, i] <= 0.5:
Y_predict = 0
else:
Y_predict = 1
return Y_predict
def model(X_train, Y_train, X_test, Y_test, epochs, lr):
w, b = init_params(X_train.shape[0])
params, grads, costs = optimize(w, b, X_train, Y_train, epochs, lr)
w = params["w"]
b = params["b"]
Y_predict_train = predict(w, b, X_train)
Y_predict_test = predict(w, b, X_test)
print ("train_accuracy: {} %".format(100-np.mean(np.abs(Y_predict_train - Y_train)) * 100))
print ("test_accuracy : {} %".format(100-np.mean(np.abs(Y_predict_test - Y_test)) * 100))
log_reg_model = {"costs": costs,
"Y_predict_test": Y_predict_test,
"Y_predict_train" : Y_predict_train,
"w" : w,
"b" : b,
"learning_rate" : lr,
"epochs": epochs}
return log_reg_model
# we use: https://gogul09.github.io/software/neural-nets-logistic-regression
#epochs = 100
epochs = 10
# lr, learning rate, step size
lr = 0.0003
# activate the logistic regression model
myModel = model(train_x, train_y, test_x, test_y, epochs, lr)
#test_img_paths = ["G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\test\\airplane\\image_0723.jpg",
# "G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\test\\airplane\\image_0713.jpg",
# "G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\test\\bike\\image_0782.jpg",
# "G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\test\\bike\\image_0799.jpg",
# "G:\\workspace\\machine-intelligence\\deep-learning\\logistic-regression\\dataset\\test\\bike\\test_1.jpg"]
# https://gogul09.github.io/software/neural-nets-logistic-regression
# use: https://gogul09.github.io/software/neural-nets-logistic-regression
test_img_paths = ["/Users/dionelisnikolaos/Downloads/dataset/test/airplane/image_0763.jpg",
"/Users/dionelisnikolaos/Downloads/dataset/test/airplane/image_0753.jpg",
"/Users/dionelisnikolaos/Downloads/dataset/test/bike/image_0782.jpg",
"/Users/dionelisnikolaos/Downloads/dataset/test/bike/image_0799.jpg",
"/Users/dionelisnikolaos/Downloads/dataset/test/bike/image_0751.jpg"]
for test_img_path in test_img_paths:
img_to_show = cv2.imread(test_img_path, -1)
img = image.load_img(test_img_path, target_size=image_size)
x = image.img_to_array(img)
x = x.flatten()
x = np.expand_dims(x, axis=1)
predict = predict_image(myModel["w"], myModel["b"], x)
predict_label = ""
if predict == 0:
predict_label = "airplane"
else:
predict_label = "bike"
# display the test image and the predicted label
cv2.putText(img_to_show, predict_label, (30,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
cv2.imshow("test_image", img_to_show)
key = cv2.waitKey(0) & 0xFF
if (key == 27):
cv2.destroyAllWindows()
import keras
import keras.datasets
# use datasets
import keras.datasets
from keras.datasets import cifar10
from keras.datasets import cifar100
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
(x_train, y_train), (x_test, y_test) = cifar100.load_data()
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
from keras.datasets import cifar100
(x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='fine')
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
from keras.datasets import fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
from keras.datasets import fashion_mnist
((trainX, trainY), (testX, testY)) = fashion_mnist.load_data()
# set the matplotlib backend so figures can be saved in the background
import matplotlib
#matplotlib.use("Agg")
# import the necessary packages
from sklearn.metrics import classification_report
from keras.optimizers import SGD
# use Fashion-MNIST
from keras.datasets import fashion_mnist
from keras.utils import np_utils
from keras import backend as K
#from imutils import build_montages
import numpy as np
# use matplotlib
import matplotlib.pyplot as plt
#image_index = 7777
image_index = 777
# ((trainX, trainY), (testX, testY))
# (x_train, y_train), (x_test, y_test)
y_train = trainY
x_train = trainX
# ((trainX, trainY), (testX, testY))
# (x_train, y_train), (x_test, y_test)
y_test = testY
x_test = testX
print(trainX.shape)
print(trainY.shape)
print(testX.shape)
print(testY.shape)
print(y_train[image_index].shape)
print(x_train[image_index].shape)
print(y_train[image_index])
plt.imshow(x_train[image_index], cmap='Greys')
#plt.imshow(x_train[image_index])
#plt.pause(5)
plt.pause(2)
#x_train.shape
print(x_train.shape)
# Reshaping the array to 4-dims so that it can work with the Keras API
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
# we define the input shape
input_shape = (28, 28, 1)
# import the necessary packages
from keras.models import Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dropout
from keras.layers.core import Dense
from keras import backend as K
class MiniVGGNet:
@staticmethod
def build(width, height, depth, classes):
# initialize the model along with the input shape to be
# "channels last" and the channels dimension itself
model = Sequential()
inputShape = (height, width, depth)
chanDim = -1
# if we are using "channels first", update the input shape
# and channels dimension
if K.image_data_format() == "channels_first":
inputShape = (depth, height, width)
chanDim = 1
# first CONV => RELU => CONV => RELU => POOL layer set
model.add(Conv2D(32, (3, 3), padding="same",
input_shape=inputShape))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(Conv2D(32, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# second CONV => RELU => CONV => RELU => POOL layer set
model.add(Conv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(Conv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# first (and only) set of FC => RELU layers
model.add(Flatten())
model.add(Dense(512))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))
# softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
# return the constructed network architecture
return model
# use numpy
import numpy as np
#matplotlib inline
import matplotlib.pyplot as plt
# use tensorflow
import tensorflow as tf
# we use the MNIST dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# https://towardsdatascience.com/image-classification-in-10-minutes-with-mnist-dataset-54c35b77a38d
# use: https://towardsdatascience.com/image-classification-in-10-minutes-with-mnist-dataset-54c35b77a38d
# use matplotlib
import matplotlib.pyplot as plt
image_index = 7777
# The label is 8
print(y_train[image_index])
plt.imshow(x_train[image_index], cmap='Greys')
#plt.pause(5)
plt.pause(2)
#x_train.shape
print(x_train.shape)
# Reshaping the array to 4-dims so that it can work with the Keras API
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
# we define the input shape
input_shape = (28, 28, 1)
# the values are float so that we can get decimal points after division
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Normalizing the RGB codes by dividing it to the max RGB value.
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print('Number of images in x_train', x_train.shape[0])
print('Number of images in x_test', x_test.shape[0])
# Importing the required Keras modules containing model and layers
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D
# Creating a Sequential Model and adding the layers
model = Sequential()
model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Flatten the 2D arrays for fully connected layers
model.add(Flatten())
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10,activation=tf.nn.softmax))
# compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# ADAM, adaptive momentum
# we use the Adam optimizer
# fit the model
#model.fit(x=x_train,y=y_train, epochs=10)
#model.fit(x=x_train,y=y_train, epochs=10)
model.fit(x=x_train,y=y_train, epochs=8)
# evaluate the model
model.evaluate(x_test, y_test)
# https://towardsdatascience.com/image-classification-in-10-minutes-with-mnist-dataset-54c35b77a38d
# use index 4444
image_index = 4444
plt.imshow(x_test[image_index].reshape(28, 28),cmap='Greys')
#plt.pause(5)
plt.pause(2)
#pred = model.predict(x_test[image_index].reshape(1, img_rows, img_cols, 1))
pred = model.predict(x_test[image_index].reshape(1, 28, 28, 1))
print(pred.argmax())
# Deep Generative Models
# GANs and VAEs, Generative Models
# random noise
# from random noise to a tensor
# We use batch normalisation.
# GANs are very difficult to train. Super-deep models. This is why we use batch normalisation.
# GANs and LSTM RNNs
# use LSTM RNNs together with GANs
# combine the power of LSTM RNNs and GANs
# it is possible to use LSTM RNN together with GANs
# https://github.com/life-efficient/Academy-of-AI/blob/master/Lecture%2013%20-%20Generative%20Models/GANs%20tutorial.ipynb
# https://github.com/life-efficient/Academy-of-AI/tree/master/Lecture%2013%20-%20Generative%20Models
# https://github.com/life-efficient/Academy-of-AI/blob/master/Lecture%2013%20-%20Generative%20Models/GANs%20tutorial.ipynb
# Anomaly detection (AD)
# Unsupervised machine learning
# GANs for super-resolution
# Generative Adversarial Networks, GANs
# the BigGAN dataset
# BigGAN => massive dataset
# latent space, BigGAN, GANs
# down-sampling, sub-sample, pooling
# throw away samples, pooling, max-pooling
# partial derivatives
# loss function and partial derivatives
# https://github.com/Students-for-AI/The-Academy-of-AI
# https://github.com/life-efficient/Academy-of-AI/tree/master/Lecture%2013%20-%20Generative%20Models
# Generator G and Discriminator D
# the loss function of the Generator G
# up-convolution
# We use a filter we do up-convolution with.
# use batch normalisation
# GANs are very difficult to train and this is why we use batch normalisation.
# We normalize across a batch.
# Mean across a batch. We use batches. Normalize across a batch.
# the ReLU activation function
# ReLU is the most common activation function. We use ReLU.
# use: https://github.com/life-efficient/Academy-of-AI/blob/master/Lecture%2013%20-%20Generative%20Models/GANs%20tutorial.ipynb
# we use PyTorch
import torch
#import torch
import torchvision
from torchvision import datasets, transforms
# use matplotlib
import matplotlib.pyplot as plt
#import torch
#import torchvision
#from torchvision import transforms, datasets
# use nn.functional
import torch.nn.functional as F
#import matplotlib.pyplot as plt
#batch_size = 128
# download the training dataset
#train_data = datasets.FashionMNIST(root='fashiondata/',
# transform=transforms.ToTensor(),
# train=True,
# download=True)
# we create the train data loader
#train_loader = torch.utils.data.DataLoader(train_data,
# shuffle=True,
# batch_size=batch_size)
# define the batch size
batch_size = 100
train_data = datasets.FashionMNIST(root='fashiondata/',
transform=transforms.ToTensor(),
train=True,
download=True
)
train_samples = torch.utils.data.DataLoader(dataset=train_data,
batch_size=batch_size,
shuffle=True
)
# combine the power of LSTM RNNs and GANs
# it is possible to use LSTM RNN together with GANs
# GANs and LSTM RNNs
# use LSTM RNNs together with GANs
# class for D and G
# we train the discriminator and the generator
# we make the discriminator
class discriminator(torch.nn.Module):
def __init__(self):
super().__init__()
self.conv1 = torch.nn.Conv2d(1, 64, kernel_size=4, stride=2, padding=1) # 1x28x28-> 64x14x14
self.conv2 = torch.nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1) # 64x14x14-> 128x7x7
self.dense1 = torch.nn.Linear(128 * 7 * 7, 1)
self.bn1 = torch.nn.BatchNorm2d(64)
self.bn2 = torch.nn.BatchNorm2d(128)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x))).view(-1, 128 * 7 * 7)
# use sigmoid for the output layer
x = F.sigmoid(self.dense1(x))
return x
# this was for the discriminator
# we now do the same for the generator
# Generator G
class generator(torch.nn.Module):
def __init__(self):
super().__init__()
self.dense1 = torch.nn.Linear(128, 256)
self.dense2 = torch.nn.Linear(256, 1024)
self.dense3 = torch.nn.Linear(1024, 128 * 7 * 7)
self.uconv1 = torch.nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1) # 128x7x7 -> 64x14x14
self.uconv2 = torch.nn.ConvTranspose2d(64, 1, kernel_size=4, stride=2, padding=1) # 64x14x14 -> 1x28x28
self.bn1 = torch.nn.BatchNorm1d(256)
self.bn2 = torch.nn.BatchNorm1d(1024)
self.bn3 = torch.nn.BatchNorm1d(128 * 7 * 7)
self.bn4 = torch.nn.BatchNorm2d(64)
def forward(self, x):
x = F.relu(self.bn1(self.dense1(x)))
x = F.relu(self.bn2(self.dense2(x)))
x = F.relu(self.bn3(self.dense3(x))).view(-1, 128, 7, 7)
x = F.relu(self.bn4(self.uconv1(x)))
x = F.sigmoid(self.uconv2(x))
return x
# https://github.com/life-efficient/Academy-of-AI/blob/master/Lecture%2013%20-%20Generative%20Models/GANs%20tutorial.ipynb
# use: https://github.com/life-efficient/Academy-of-AI/blob/master/Lecture%2013%20-%20Generative%20Models/GANs%20tutorial.ipynb
# instantiate the model
d = discriminator()
g = generator()
# training hyperparameters
#epochs = 100
#epochs = 100
epochs = 10
# learning rate
#dlr = 0.0003
#glr = 0.0003
dlr = 0.003
glr = 0.003
d_optimizer = torch.optim.Adam(d.parameters(), lr=dlr)
g_optimizer = torch.optim.Adam(g.parameters(), lr=glr)
dcosts = []
gcosts = []
plt.ion()
fig = plt.figure()
loss_ax = fig.add_subplot(121)
loss_ax.set_xlabel('Batch')
loss_ax.set_ylabel('Cost')
loss_ax.set_ylim(0, 0.2)
generated_img = fig.add_subplot(122)
plt.show()
# https://github.com/life-efficient/Academy-of-AI/blob/master/Lecture%2013%20-%20Generative%20Models/GANs%20tutorial.ipynb
# https://github.com/life-efficient/Academy-of-AI/tree/master/Lecture%2013%20-%20Generative%20Models