-
Notifications
You must be signed in to change notification settings - Fork 0
/
medical_waste_classification.py
301 lines (233 loc) · 10.9 KB
/
medical_waste_classification.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
# GENERAL WORKFLOW
# Examine and understand the data
# Build an input pipeline, in this case using Keras ImageDataGenerator
# Compose the model
# Load in the pretrained base model (and pretrained weights)
# Stack the classification layers on top
# Train the model
# Evaluate model
# from random import shuffle
# import matplotlib.pyplot as plt
# import numpy as np
# import os
# import tensorflow as tf
# from tensorflow.keras.applications.vgg16 import VGG16
# # preprocessing data
# # path_to_zip = tf.keras.utils.get_file('../content/drive/MyDrive/Colab Notebooks/cats_dogs/cats_and_dogs_filtered', extract=True)
# train_dir = os.path.join('../content/drive/MyDrive/Colab Notebooks/cats_dogs/dataset', 'train')
# validation_dir = os.path.join(
# '../content/drive/MyDrive/Colab Notebooks/cats_dogs/dataset', 'validation')
# BATCH_SIZE = 32
# IMG_SIZE = (160, 160)
# train_dataset = tf.keras.utils.image_dataset_from_directory(train_dir,
# shuffle=True,
# batch_size=BATCH_SIZE,
# image_size=IMG_SIZE)
# validation_dataset = tf.keras.utils.image_dataset_from_directory(validation_dir,
# shuffle=True,
# batch_size=BATCH_SIZE,
# image_size=IMG_SIZE)
# class_names = train_dataset.class_names
# plt.figure(figsize=(10, 10))
# for images, labels in train_dataset.take(1):
# for i in range(9):
# ax = plt.subplot(3, 3, i+1)
# plt.imshow(images[i].numpy().astype('uint8'))
# plt.title(class_names[labels[i]])
# plt.axis("off")
# val_batches = tf.data.experimental.cardinality(validation_dataset)
# test_dataset = validation_dataset.take(val_batches//5)
# validation_dataset = validation_dataset.skip(val_batches//5)
# print('number of validation batches: %d' %
# tf.data.experimental.cardinality(validation_dataset))
# print('number of test batches: %d' %
# tf.data.experimental.cardinality(test_dataset))
# # configure dataset for performance
# AUTOTUNE = tf.data.AUTOTUNE
# train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)
# validation_dataset = validation_dataset.prefetch(buffer_size=AUTOTUNE)
# test_dataset = test_dataset.prefetch(buffer_size=AUTOTUNE)
# # using data augmentation
# data_augmentation = tf.keras.Sequential([tf.keras.layers.RandomFlip('horizontal'),
# tf.keras.layers.RandomRotation(0.2),
# ])
# for image, _ in train_dataset.take(1):
# plt.figure(figsize=(10, 10))
# first_image = image[0]
# for i in range(9):
# ax = plt.subplot(3, 3, i + 1)
# augmented_image = data_augmentation(tf.expand_dims(first_image, 0))
# plt.imshow(augmented_image[0] / 255)
# plt.axis('off')
# preprocess_input = tf.keras.applications.MobileNetV3Large.preprocess_input
# # tf.keras.layers.Rescaling
# # alternative
# # create base model from pretrained converts
# IMG_SHAPE = IMG_SIZE + (3,)
# base_model = tf.keras.applications.MobileNetV3Large(input_shape = IMG_SHAPE,
# include_top = False,
# weights = 'imagenet')
# # example to get the block of features
# image_batch, label_batch = next(iter(train_dataset))
# feature_batch = base_model(image_batch)
# print(feature_batch.shape)
# # feature extraction
# # freezing the convolutional base
# base_model.trainable = False
# global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
# feature_batch_average = global_average_layer(feature_batch)
# print(feature_batch_average.shape)
# prediction_layer = tf.keras.layers.Dense(1)
# prediction_batch = prediction_layer(feature_batch_average)
# print(prediction_batch.shape)
# inputs = tf.keras.Input(shape=(160, 160, 3))
# x = data_augmentation(inputs)
# x = preprocess_input(x)
# x = base_model(x, training=False)
# x = global_average_layer(x)
# x = tf.keras.layers.Dropout(0.3)(x)
# outputs = prediction_layer(x)
# model = tf.keras.Model(inputs, outputs)
# base_learning_rate = 0.0001
# model.compile(optimizer=tf.keras.optimizers.Adam
# (learning_rate=base_learning_rate),
# loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
# metrics=['accuracy'])
# model.summary()
# len(model.trainable_variables)
# # TRAIN THE MODEL
# initial_epochs = 10
# loss0, accuracy0 = model.evaluate(validation_dataset)
# print("init loss : {:2f}".format(loss0))
# print("init accuracy : {:2f}".format(accuracy0))
# history = model.fit(train_dataset,
# epochs = initial_epochs,
# validation_data=validation_dataset)
# # plotting accuracy and loss
# acc = history.history['accuracy']
# val_acc = history.history['val_accuracy']
# loss = history.history['loss']
# val_loss = history.history['val_loss']
# plt.figure(figsize=(8, 8))
# plt.subplot(2, 1, 1)
# plt.plot(acc, label='Training Accuracy')
# plt.plot(val_acc, label='Validation Accuracy')
# plt.legend(loc='lower right')
# plt.ylabel('Accuracy')
# plt.ylim([min(plt.ylim()),1])
# plt.title('Training and Validation Accuracy')
# plt.subplot(2, 1, 2)
# plt.plot(loss, label='Training Loss')
# plt.plot(val_loss, label='Validation Loss')
# plt.legend(loc='upper right')
# plt.ylabel('Cross Entropy')
# plt.ylim([0,1.0])
# plt.title('Training and Validation Loss')
# plt.xlabel('epoch')
# plt.show()
# # finetuning
# base_model.trainable = True
# # Let's take a look to see how many layers are in the base model
# print("Number of layers in the base model: ", len(base_model.layers))
# # Fine-tune from this layer onwards
# fine_tune_at = 10
# # Freeze all the layers before the `fine_tune_at` layer
# for layer in base_model.layers[:fine_tune_at]:
# layer.trainable = False
# model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits = True),
# optimizer = tf.keras.optimizers.Adam(learning_rate=base_learning_rate/10),
# metrics = ['accuracy'])
# model.summary()
# len(model.trainable_variables)
# fine_tune_epochs = 30
# total_epochs = initial_epochs + fine_tune_epochs
# history_fine = model.fit(train_dataset,
# epochs=total_epochs,
# initial_epoch=history.epoch[-1],
# validation_data=validation_dataset)
# acc += history_fine.history['accuracy']
# val_acc += history_fine.history['val_accuracy']
# loss += history_fine.history['loss']
# val_loss += history_fine.history['val_loss']
# plt.figure(figsize=(8, 8))
# plt.subplot(2, 1, 1)
# plt.plot(acc, label='Training Accuracy')
# plt.plot(val_acc, label='Validation Accuracy')
# plt.ylim([0.8, 1])
# plt.plot([initial_epochs-1,initial_epochs-1],
# plt.ylim(), label='Start Fine Tuning')
# plt.legend(loc='lower right')
# plt.title('Training and Validation Accuracy')
# plt.subplot(2, 1, 2)
# plt.plot(loss, label='Training Loss')
# plt.plot(val_loss, label='Validation Loss')
# plt.ylim([0, 1.0])
# plt.plot([initial_epochs-1,initial_epochs-1],
# plt.ylim(), label='Start Fine Tuning')
# plt.legend(loc='upper right')
# plt.title('Training and Validation Loss')
# plt.xlabel('epoch')
# plt.show()
# loss, accuracy = model.evaluate(test_dataset)
# print('Test accuracy :', accuracy)
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random
import math
import os
import cv2 as cv
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures, StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split
DIR = "../content/drive/MyDrive/Colab Notebooks/cats_dogs/dataset"
train_dataset = tf.keras.preprocessing.image_dataset_from_directory(DIR, validation_split=0.1, subset="training", seed=42, batch_size=128, smart_resize=True, image_size=(256, 256))
test_dataset = tf.keras.preprocessing.image_dataset_from_directory(DIR, validation_split=0.1, subset="validation", seed=42, batch_size=128, smart_resize=True, image_size=(256, 256))
classes = train_dataset.class_names
numClasses = len(train_dataset.class_names)
print(classes)
AUTOTUNE = tf.data.AUTOTUNE
train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)
test_dataset = test_dataset.prefetch(buffer_size=AUTOTUNE)
baseModel = tf.keras.applications.MobileNetV3Large(input_shape=(256, 256,3), weights='imagenet', include_top=False, classes=numClasses)
for layers in baseModel.layers[:-6]:
layers.trainable=False
last_output = baseModel.layers[-1].output
x = tf.keras.layers.Dropout(0.45) (last_output)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.BatchNormalization() (x)
x = tf.keras.layers.Dense(256, activation = tf.keras.activations.elu, kernel_regularizer=tf.keras.regularizers.l1(0.045), activity_regularizer=tf.keras.regularizers.l1(0.045), kernel_initializer='he_normal')(x)
x = tf.keras.layers.Dense(128, activation = tf.keras.activations.elu, kernel_regularizer=tf.keras.regularizers.l1(0.045), activity_regularizer=tf.keras.regularizers.l1(0.045), kernel_initializer='he_normal')(x)
x = tf.keras.layers.Dense(128, activation = tf.keras.activations.elu, kernel_regularizer=tf.keras.regularizers.l1(0.045), activity_regularizer=tf.keras.regularizers.l1(0.045), kernel_initializer='he_normal')(x)
x = tf.keras.layers.Dense(64, activation = tf.keras.activations.elu, kernel_regularizer=tf.keras.regularizers.l1(0.045), activity_regularizer=tf.keras.regularizers.l1(0.045), kernel_initializer='he_normal')(x)
x = tf.keras.layers.Dropout(0.45) (x)
x = tf.keras.layers.Dense(numClasses, activation='softmax')(x)
model = tf.keras.Model(inputs=baseModel.input,outputs=x)
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.00125), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
epochs = 50
history = model.fit(train_dataset, validation_data=test_dataset, epochs=epochs)
# acc += history.history['accuracy']
# val_acc += history.history['val_accuracy']
# loss += history_fine.history['loss']
# val_loss += history_fine.history['val_loss']
# plt.figure(figsize=(8, 8))
# plt.subplot(2, 1, 1)
# plt.plot(acc, label='Training Accuracy')
# plt.plot(val_acc, label='Validation Accuracy')
# plt.ylim([0.8, 1])
# plt.plot([initial_epochs-1,initial_epochs-1],
# plt.ylim(), label='Start Fine Tuning')
# plt.legend(loc='lower right')
# plt.title('Training and Validation Accuracy')
# plt.subplot(2, 1, 2)
# plt.plot(loss, label='Training Loss')
# plt.plot(val_loss, label='Validation Loss')
# plt.ylim([0, 1.0])
# plt.plot([initial_epochs-1,initial_epochs-1],
# plt.ylim(), label='Start Fine Tuning')
# plt.legend(loc='upper right')
# plt.title('Training and Validation Loss')
# plt.xlabel('epoch')
# plt.show()
# loss, accuracy = model.evaluate(test_dataset)
# print('Test accuracy :', accuracy)