-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimageClassification.py
More file actions
96 lines (65 loc) · 2.13 KB
/
imageClassification.py
File metadata and controls
96 lines (65 loc) · 2.13 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
95
96
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import random
import pickle
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation,Flatten, Conv2D, MaxPooling2D
DATADIR="C://Users/Ayushi Sharma/Pictures/potholes/pothole_image_data"
CATEGORIES=["with", "without"] # 0-with 1-without
training_data=[]
IMG_SIZE = 250
def create_training_data():
for category in CATEGORIES:
path = os.path.join(DATADIR, category) # path to with or without
class_num=CATEGORIES.index(category)
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data.append([new_array, class_num])
except Exception as e:
pass
create_training_data()
random.shuffle(training_data)
# print(len(training_data))
# for sample in training_data[:10]:
# print(sample[1])
# import numpy as np
X=[] #feature set
y=[] #labels
for features, labels in training_data:
X.append(features)
y.append(labels)
X=np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
pickle_out=open("X.pickle", "wb")
pickle.dump(X, pickle_out)
pickle_out.close()
pickle_out=open("y.pickle", "wb")
pickle.dump(y, pickle_out)
pickle_out.close()
pickle_in=open("X.pickle", "rb")
X=pickle.load(pickle_in)
# print(X[1])
X = pickle.load(open("X.pickle", "rb"))
y = np.array(pickle.load(open("y.pickle", "rb")))
X = X/255.0
model = keras.Sequential()
model.add(Conv2D(64, (3,3), input_shape=X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss="binary_crossentropy",
optimizer="adam",
metrics=['accuracy'])
model.fit(X,y, batch_size=32, validation_split=0.1)
model.save('64X3-CNN.model1')