-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_code.py
More file actions
55 lines (48 loc) · 1.68 KB
/
final_code.py
File metadata and controls
55 lines (48 loc) · 1.68 KB
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
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
import seaborn as sn
import matplotlib.pyplot as plt
# we receive the train and test data
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()
# normalization of the pixels(0 to 1 range)
X_train = X_train/255
X_test = X_test/255
# changing it to to a list form from the matrix form
y_train = y_train.reshape(-1,)
y_test = y_test.reshape(-1,)
# building the model
model = keras.Sequential([
layers.Conv2D(64, (3, 3), padding='same', activation='relu',
input_shape=(32, 32, 3)),
layers.MaxPooling2D(),
layers.Conv2D(128, (3, 3), padding='same',
activation='relu'),
layers.MaxPooling2D(),
# dense network layer(for classification)
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='sigmoid')
])
# using tensorboard for plotting (loss vs epochs) and (accuracy vs epochs)
tb_callback=tf.keras.callbacks.TensorBoard(log_dir = 'logs/', histogram_freq = 1)
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(X_train, y_train, epochs=5, callbacks=[tb_callback])
model.summary()
print('this is the accuracy of the test data: ')
model.evaluate(X_test, y_test)
y_pred = model.predict(X_test)
yp = [np.argmax(i) for i in y_pred]
# different graphs and summary
print(classification_report(y_test, yp))
cm = confusion_matrix(y_test, yp)
sn.heatmap(cm, annot=True, fmt='d')
plt.xlabel('predicted')
plt.ylabel('true')
plt.title('confusion matrix')
plt.show()