-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
55 lines (43 loc) · 1.95 KB
/
main.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
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras import datasets, layers, models
'''
get the data from the dataset from keras, and
store them in a testing and training tuple
'''
(training_images, training_labels), (testing_images, testing_labels) = datasets.cifar10.load_data()
'''
normalize the data. pixels are activated depending on brightness
from values 0 - 255. we want to scale the data to be from 0 - 1.
we do that by dividing by 255.
'''
training_images, testing_images = training_images / 255, testing_images / 255
'this concludes *getting* and *prepping* the data'
'defines class name list and visualizes 16 of the images from dataset'
class_names = ['Car', 'Plane', 'Bird', 'Cat', 'Deer', 'Dog', 'Frog', 'Horse', 'Ship', 'Truck']
for i in range(16):
plt.subplot(4,4, i+1)
plt.xticks([])
plt.yticks([])
plt.imshow(training_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[training_labels[i][0]])
'''gets label of specified image(number)and passes image number as the index for class list'''
plt.show()
'reduce amount of images fed into the neural network'
training_images = training_images[:20000]
training_labels = training_labels[:20000]
testing_images = testing_images[:4000]
testing_images = testing_images[:4000]
'data is now prepared and the next step is building the model - NN'
model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=10, validation_data=(testing_images, testing_labels))