-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_classification.py
More file actions
112 lines (95 loc) · 4.52 KB
/
Copy pathbinary_classification.py
File metadata and controls
112 lines (95 loc) · 4.52 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Topic: Binary classification of images
"""
"""
https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip
Download the sample dataset:
Train dataset path: /tmp/cats_and_dogs_filtered/cats_and_dogs_filtered/train
Validation Dataset path: /tmp/cats_and_dogs_filtered/cats_and_dogs_filtered/validation
"""
#This is part is optional-----------------------------
"""
import os
import zipfile
file = input("Enter the zip file to extract: ")
zf = zipfile.ZipFile(file, 'r')
zf.extractall(file)
zf.close()
"""
#-------------------------------------------------------
#You can start from here
#Import the dataset for training the model........................................
path1 = str(input("Enter the path to directory containing image dataset(Folder name must be named after image name): "))
train_dst = os.path.join(path1)
groups = os.listdir(train_dst)
if(len(groups) !=2):
exit(-1)
#For checking the model performance and increase it.......................................
dec = input("Do you have validation data?(y/n): ")
if(dec == 'y' or dec == 'Y'):
path2 = input("Enter the path to directory containing validation image dataset(Folder name must be named after image name): ")
validation_dst = os.path.join(path2)
if(len(os.listdir(validation_dst)) !=2):
exit(-1)
print("Total Training Dataset: ")
print(groups[0] + " = " + str(len(os.listdir(train_dst + "/" + groups[0]))))
print(groups[1] + " = " + str(len(os.listdir(train_dst + "/" + groups[1]))))
print("Total Validation Dataset:")
print(groups[0] + " = " + str(len(os.listdir(validation_dst + "/" + groups[0]))))
print(groups[1] + " = " + str(len(os.listdir(validation_dst + "/" + groups[1]))))
print()
import tensorflow as tf
model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape=(300,300,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512,activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
#RMSprop is efficient gradient descent algorithm
from tensorflow.keras.optimizers import RMSprop
model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.001),metrics=['acc'])
#Preprocessing of images
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# All images will be rescaled by 1./255
trainDataGen = ImageDataGenerator(rescale=1/255)
validationDataGen = ImageDataGenerator(rescale=1/255)
train_generator = trainDataGen.flow_from_directory(
path1,
target_size=(300,300),
batch_size=128,
class_mode='binary' # Since we use binary_crossentropy loss, we need binary labels
)
validation_generator = validationDataGen.flow_from_directory(
path2,
target_size=(300,300),
batch_size=32,
class_mode='binary' # Since we use binary_crossentropy loss, we need binary labels
)
history = model.fit_generator(train_generator,
steps_per_epoch=8,
epochs=15,
verbose=1,
validation_data=validation_generator,
validation_steps=8
)
import numpy as np
from keras.preprocessing import image
file = input("Enter an image to test: ")
x = image.load_img(file,target_size=(300,300)) #loading image of size 300x300
x = image.img_to_array(x) #loading
x = np.expand_dims(x,axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
if classes[0]>0.5:
print(file + ' is ' + groups[0] + ' image')
else:
print(file + ' is ' + groups[1] + ' image')