-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKasturi-04-03.py
90 lines (69 loc) · 2.89 KB
/
Kasturi-04-03.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
# Kasturi, Chandra Shekhar
# 1001-825-454
# 2020-11-09
# Assignment-04-03
import pytest
import numpy as np
import os
import tensorflow as tf
import numpy as np
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import Dense, Flatten, InputLayer, Input
from tensorflow.keras.datasets import cifar10
from cnn import CNN
def test_train():
cnn = CNN()
batch_size=10
num_epochs=10
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
samples = 100
X_train = X_train[0:samples, :]
X_train = X_train.astype('float32') / 255
test_samples = 20
X_test = X_test[0:test_samples, :]
X_test = X_test.astype('float32') / 255
from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train, 10)
y_train = y_train[0:samples]
y_test = to_categorical(y_test, 10)
y_test = y_test[0:test_samples]
cnn.add_input_layer(shape=(32, 32, 3))
cnn.append_conv2d_layer(num_of_filters=64, kernel_size=(3, 3), activation='relu', name="conv1")
cnn.append_conv2d_layer(num_of_filters=32, kernel_size=(3, 3), activation='relu', name="conv2")
cnn.append_flatten_layer(name="flat1")
cnn.append_dense_layer(num_nodes=10, activation="relu", name="dense1")
cnn.set_optimizer(optimizer="SGD")
cnn.set_loss_function(loss="hinge")
cnn.set_metric(metric='accuracy')
LossList = cnn.train(X_train=X_train, y_train=y_train, batch_size=batch_size, num_epochs=num_epochs)
assert LossList is not None
def test_evaluate():
cnn = CNN()
batch_size = 10
num_epochs = 10
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
samples = 100
X_train = X_train[0:samples, :]
X_train = X_train.astype('float32') / 255
test_samples = 10
X_test = X_test[0:test_samples, :]
X_test = X_test.astype('float32') / 255
from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train, 10)
y_train = y_train[0:samples, :]
y_test = to_categorical(y_test, 10)
y_test = y_test[0:test_samples, :]
cnn.add_input_layer(shape=(32, 32, 3))
cnn.append_conv2d_layer(num_of_filters=64, kernel_size=(3, 3), activation='relu', name="conv1")
cnn.append_conv2d_layer(num_of_filters=32, kernel_size=(3, 3), activation='relu', name="conv2")
cnn.append_flatten_layer(name="flat1")
cnn.append_dense_layer(num_nodes=10, activation="relu", name="dense1")
cnn.set_optimizer(optimizer="SGD")
cnn.set_loss_function(loss="hinge")
cnn.set_metric(metric='accuracy')
ListofLoss = cnn.train(X_train=X_train, y_train=y_train, batch_size=None, num_epochs=num_epochs)
(loss, metric) = cnn.evaluate(X=X_test, y=y_test)
assert loss < 5
assert metric < 2