-
Notifications
You must be signed in to change notification settings - Fork 0
/
DL_waste_biodegradable_accuracy-78.py
337 lines (256 loc) · 10.7 KB
/
DL_waste_biodegradable_accuracy-78.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#imports
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
from sklearn.model_selection import train_test_split
from math import floor
IMG_S1,IMG_S2 = 80,80
X=[]
Y=[]
epsilon = 1e-3
def imgread_scale_resize(PATH,img):
img_array = cv2.imread(os.path.join(PATH,img),cv2.IMREAD_COLOR)
img_array = cv2.resize(img_array , (IMG_S1,IMG_S2))
img_array = cv2.GaussianBlur(img_array,(3,3),0)
return img_array
def createTrainingData():
datadir = "C:\\Users\\Piggy\\Downloads\\Compressed\\waste-classification-data\\DATASET\\TRAIN"
CATEGORIES = ["O","R"]
for ele in CATEGORIES:
a = 0
PATH = os.path.join(datadir,ele)
class_num = CATEGORIES.index(ele)
for img in os.listdir(PATH):
try:
img_array = imgread_scale_resize(PATH,img)
X.append(list(img_array))
Y.append(class_num)
print(a)
except Exception as e:
pass
a+=1
if(a>10000): break
createTrainingData()
#Reshape and flatten X and Y
X = np.array(X)
Y = np.array(Y)
X = X.reshape(X.shape[0],-1).T
Y = Y.T
X = X/255 #normalized
#Output config C: DEPth
def one_hottie(labels,C):
sess = tf.Session()
C = tf.constant(C) #four shapes
One_hot_matrix = tf.one_hot(labels,C,axis=0)
Y = sess.run(One_hot_matrix)
sess.close()
return Y
Y = one_hottie(Y,2)
#splitting
X_train, X_test, y_train, y_test = train_test_split(X.T, Y.T, test_size = 0.13, random_state = 0)
del X
del Y
X_train, X_test, y_train, y_test = X_train.T, X_test.T, y_train.T, y_test.T
#creating placeholders
def create_placeholders(n_x,n_y):
"""
Creates two placeholder of shape (n_x,none) and (n_y,none)
n_x : shape of 1 image vector - height * width * colors(RGB or grayscale etc)
n_y : no. of classes
"""
X = tf.placeholder(dtype = tf.float32, shape = (n_x,None),name = "X")
Y = tf.placeholder(dtype = tf.float32, shape = (n_y,None),name = "Y")
return X,Y
# SHAPE OF NET : LINEAR(Z1) => RELU(A1) => LINEAR(z2) => RELU(a2) => LINEAR(z3) => SOFTMAX(y)
#model functions
def initialize_parameters():
"""
W1 : [n[1], shape of 1 image vector]
b1 : [n[1], 1]
W2 : [n[2], n[1]]
b2 : [n[2], 1]
W3 : [y[1], n[2]]
b3 : [y[1], 1]
Returns:
parameters -- a dictionary of tensors containing W1, b1, W2, b2, W3, b3
"""
W1 = tf.get_variable(name= "W1", shape= [20,19200], initializer = tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable(name= "b1", shape= [20,1], initializer = tf.zeros_initializer())
W2 = tf.get_variable(name= "W2", shape= [9,20], initializer = tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable(name= "b2", shape= [9,1], initializer = tf.zeros_initializer())
W3 = tf.get_variable(name= "W3", shape= [2,9], initializer = tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable(name= "b3", shape= [2,1], initializer = tf.zeros_initializer())
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2":b2,
"W3": W3,
"b3":b3,}
return parameters
#forward prop
def forward_propagation(X, parameters):
"""
Arguments:
X : placeholder of shape (input size, number of examples)
parameters : dict of W and b
Returns:
Z3 -- the output of the last LINEAR unit
"""
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
Z1 = tf.add(tf.matmul(W1,X),b1)
A1 = tf.nn.relu(Z1)
Z2 = tf.add(tf.matmul(W2,A1),b2)
A2 = tf.nn.relu(Z2)
Z3 = tf.add(tf.matmul(W3,A2),b3)
return Z3 #Linear Z3
def compute_cost(Z3, Y):
"""
Computes the cost
Arguments:
Z3 : Linear unit output of forward prop ...of shape (6, number of examples)
Y : True vales placeholder, same shape as Z3
Returns:
cost - Tensor of the cost function
"""
# to fit the tensorflow requirement for tf.nn.softmax_cross_entropy_with_logits(...,...)
logits = tf.transpose(Z3)
labels = tf.transpose(Y)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits ,labels = labels))
return cost
def random_mini_batches(X_train, y_train, minibatch_size, seed):
m = X_train.shape[1] # number of training examples
mini_batches = []
np.random.seed(seed)
num_complete_minibatches = floor(m/minibatch_size) # number of mini batches of size minibatch_size in your partitionning
for k in range(0, num_complete_minibatches):
mini_batch_X = X_train[:, k * minibatch_size : k * minibatch_size + minibatch_size]
mini_batch_Y = y_train[:, k * minibatch_size : k * minibatch_size + minibatch_size]
mini_batch = (mini_batch_X, mini_batch_Y)
mini_batches.append(mini_batch)
# Handling the end case (last mini-batch < minibatch_size)
if m % minibatch_size != 0:
mini_batch_X = X_train[:, num_complete_minibatches * minibatch_size : m]
mini_batch_Y = y_train[:, num_complete_minibatches * minibatch_size : m]
mini_batch = (mini_batch_X, mini_batch_Y)
mini_batches.append(mini_batch)
return mini_batches
def model(X_train, y_train, X_test, y_test, learning_rate = 0.001,
num_epochs = 1000, minibatch_size = 100, print_cost = True):
"""
Arguments:
X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
y_train -- test set, of shape (output size = 6, number of training examples = 1080)
X_test -- training set, of shape (input size = 12288, number of training examples = 120)
y_test -- test set, of shape (output size = 6, number of test examples = 120)
learning_rate -- learning rate of the optimization
num_epochs -- number of epochs of the optimization loop
minibatch_size -- size of a minibatch
print_cost -- True to print the cost every 100 epochs
Returns:
parameters -- parameters learnt by the model. They can then be used to predict.
"""
ops.reset_default_graph() # to be able to rerun the model without overwriting tf variables
tf.set_random_seed(1) # to keep consistent results
seed = 3 # to keep consistent results
(n_x, m) = X_train.shape # (n_x: input size, m : number of examples in the train set)
n_y = y_train.shape[0] # n_y : output size
costs = [] # To keep track of the cost
X, Y = create_placeholders(n_x,n_y)
parameters = initialize_parameters()
Z3 = forward_propagation(X,parameters)
cost = compute_cost(Z3,Y)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(num_epochs):
epoch_cost = 0. # Defines a cost related to an epoch
num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
seed = seed + 1
minibatches = random_mini_batches(X_train, y_train, minibatch_size, seed)
for minibatch in minibatches:
(minibatch_X, minibatch_Y) = minibatch
_ , minibatch_cost = sess.run([optimizer,cost],feed_dict={X:minibatch_X, Y:minibatch_Y})
epoch_cost += minibatch_cost / num_minibatches
if print_cost == True and epoch % 25 == 0:
print ("Cost after epoch %i: %f" % (epoch, epoch_cost))
if print_cost == True and epoch % 5 == 0:
costs.append(epoch_cost)
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per fives)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
parameters = sess.run(parameters)
print ("Parameters have been trained!")
correct_prediction = tf.equal(tf.argmax(Z3), tf.argmax(Y))
# Calculate accuracy on the test set
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print ("Train Accuracy:", accuracy.eval({X: X_train, Y: y_train}))
print ("Test Accuracy:", accuracy.eval({X: X_test, Y: y_test}))
return parameters
parameters = model(X_train, y_train, X_test, y_test,num_epochs=1000,minibatch_size = 100,learning_rate = 0.0001)
#model ready
#def predict(X, parameters):
#
# W1 = tf.convert_to_tensor(parameters["W1"])
# b1 = tf.convert_to_tensor(parameters["b1"])
# W2 = tf.convert_to_tensor(parameters["W2"])
# b2 = tf.convert_to_tensor(parameters["b2"])
# W3 = tf.convert_to_tensor(parameters["W3"])
# b3 = tf.convert_to_tensor(parameters["b3"])
#
# params = {"W1": W1,
# "b1": b1,
# "W2": W2,
# "b2": b2,
# "W3": W3,
# "b3": b3}
#
# x = tf.placeholder("float", [6400, 1947])
#
# z3 = forward_propagation_for_predict(x, params)
# p = tf.argmax(z3)
#
# sess = tf.Session()
# prediction = sess.run(p, feed_dict = {x:X})
#
# return prediction
#
#def forward_propagation_for_predict(X, parameters):
# """
# Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX
#
# Arguments:
# X -- input dataset placeholder, of shape (input size, number of examples)
# parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"
# the shapes are given in initialize_parameters
#
# Returns:
# Z3 -- the output of the last LINEAR unit
# """
# W1 = parameters['W1']
# b1 = parameters['b1']
# W2 = parameters['W2']
# b2 = parameters['b2']
# W3 = parameters['W3']
# b3 = parameters['b3']
#
# Z1 = tf.add(tf.matmul(W1, X), b1)
# A1 = tf.nn.relu(Z1)
# Z2 = tf.add(tf.matmul(W2, A1), b2)
# A2 = tf.nn.relu(Z2)
# Z3 = tf.add(tf.matmul(W3, A2), b3)
#
# return Z3
#
#
#predictions = predict(X_test,parameters)