-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg_class.py
More file actions
94 lines (76 loc) · 2.64 KB
/
Copy pathimg_class.py
File metadata and controls
94 lines (76 loc) · 2.64 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
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
#importing the dataset
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt
#loading the training data and testing data :)
(train_x,train_y),(test_x,test_y)= cifar10.load_data()
#plotting some images to visualize the dataset
#n=6
#plt.figure(figsize=(20,10))
#for x in range (n) :
# plt.subplot(330+1+x)
# plt.imshow(train_x[x])
# plt.show()
#importing the required layers and modules to create our convultional neural netwrok architecture
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.constraints import MaxNorm
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from keras.utils import np_utils
#import np_utils
#converting the pixel values of the dataset to float type and normalizing the dataset
train_x = train_x.astype('float32')
test_x = test_x.astype('float32')
train_x = train_x/255.0
test_x = test_x/255.0
#encoding for target classes
train_y = np_utils.to_categorical(train_y)
test_y = np_utils.to_categorical(test_y)
num_classes = test_y.shape[1]
#creating the seauential model and adding the layers
model = Sequential()
model.add(Conv2D(32,(3,3),input_shape=(32,32,3),padding="same",activation="relu",kernel_constraint=MaxNorm(3)))
model.add(Dropout(0.2))
model.add(Conv2D(32,(3,3),activation="relu",padding="same",kernel_constraint=MaxNorm(3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(512,activation="relu",kernel_constraint=MaxNorm(3)))
model.add(Dropout(0.5))
model.add(Dense(num_classes,activation="softmax"))
#configuring the optimizer and compiling the model
sgd= SGD(lr=0.01,momentum=0.9,decay=(0.01/25),nesterov=False)
model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])
#viewing model summary
#model.summary()
#training the model
model.fit(train_x,train_y,validation_data = (test_x,test_y),epochs=25,batch_size=32)
#calculating the accuracy on testing data
_,acc = model.evaluate(test_x,test_y)
print(acc*100)
#saving the model
model.save("model_cifar_10epoch.h5")
# making dictionary to map the output classes
results={
0:'aeroplane',
1:'automobile',
2:'bird',
3:'cat',
4:'deer',
5:'dog',
6:'frog',
7:'horse',
8:'ship',
9:'truck'
}
from PIL import Image
import numpy as np
im=Image.open("__image_path__")
# the input image is required to be (32,32,3)
im=im.resize((32,32))
im=np.expand_dims(im,axis=0)
im=np.array(im)
pred=model.predict_classes([im])[0]
print(pred,results[pred])