-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathastolfo_classifier.py
193 lines (147 loc) · 5.58 KB
/
astolfo_classifier.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
# -*- coding: utf-8 -*-
"""Astolfo Classifier.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1_3DdxTiDkR-wZ686vxmn8q1T8-fnk44m
"""
import pandas as pd
import numpy as np
import os
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import tensorflow as tf
import keras
from keras import layers
from keras.models import Model
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Flatten, Dense, InputLayer, BatchNormalization, Dropout
from tensorflow.keras.preprocessing.image import img_to_array
from keras import metrics
from sklearn.utils import shuffle
import cv2
import os
from tqdm import tqdm
import re
from google.colab import drive
drive.mount('/content/drive')
def sorted_alphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)',key)]
return sorted(data,key = alphanum_key)
# defining the size of the image
SIZE = 160
train_img = []
train_label = []
path = '/content/drive/My Drive/Images_all/Astolpho'
files = os.listdir(path)
files = sorted_alphanumeric(files)
for i in tqdm(files):
if i == '1000.jpg':
break
else:
img = cv2.imread(path + '/'+i,1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # open cv reads images in BGR format so we have to convert it to RGB
img = cv2.resize(img, (SIZE, SIZE)) #resizing image
img = img.astype('float32') / 255.0
train_img.append(img_to_array(img))
train_label.append('Astolpho');
path = '/content/drive/My Drive/Images_all/Non_Astolfo'
files = os.listdir(path)
files = sorted_alphanumeric(files)
for i in tqdm(files):
if i == '1000.jpg':
break
else:
img = cv2.imread(path + '/'+i,1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # open cv reads images in BGR format so we have to convert it to RGB
img = cv2.resize(img, (SIZE, SIZE)) #resizing image
img = img.astype('float32') / 255.0
train_img.append(img_to_array(img))
train_label.append('Non_Astolpho');
path = '/content/drive/My Drive/Images_all/Non_Anime'
files = os.listdir(path)
files = sorted_alphanumeric(files)
for i in tqdm(files):
if i == '1000.jpg':
break
else:
img = cv2.imread(path + '/'+i,1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # open cv reads images in BGR format so we have to convert it to RGB
img = cv2.resize(img, (SIZE, SIZE)) #resizing image
img = img.astype('float32') / 255.0
train_img.append(img_to_array(img))
train_label.append('Non_Anime');
# defining function to plot images pair
def plot_images(color):
plt.figure(figsize=(15,15))
plt.subplot(1,3,1)
plt.title('Astolpho', color = 'green', fontsize = 20)
plt.imshow(color)
plt.show()
for i in range(61,80):
plot_images(train_img[i])
x_train = np.array(train_img)
y_train = []
for i in train_label:
if i=="Astolpho":
y_train.append([1,0,0])
if i=="Non_Anime":
y_train.append([0,1,0])
if i=="Non_Astolpho":
y_train.append([0,0,1])
y_train = np.array(y_train)
y_train
"""## Saving and Loading Images"""
x_train.tofile('/content/drive/My Drive/Images_all/x_train.csv', sep = ',')
y_train.tofile('/content/drive/My Drive/Images_all/y_train.csv', sep = ',')
x_train = np.genfromtxt('/content/drive/My Drive/Images_all/x_train.csv', delimiter=',')
y_train = np.genfromtxt('/content/drive/My Drive/Images_all/y_train.csv', delimiter=',')
x_train = np.array(x_train)
y_train = np.array(y_train)
"""## Model"""
# from sklearn.preprocessing import LabelEncoder
# le = LabelEncoder()
# le.fit(y_train)
# # encode
# y_train = le.transform(y_train)
# !pip install category_encoders
# import category_encoders as ce
# enc=ce.OneHotEncoder().fit(y_train.astype(str))
# y_train=enc.transform(y_train.astype(str))
# # tf.keras.utils.to_categorical(y_train, num_classes=2, dtype='float32')
x_train, y_train = shuffle(x_train, y_train)
y_train
x_train.shape
# build a sequential model
model = Sequential()
model.add(InputLayer(input_shape=(160, 160, 3)))
model.add(Dense(units=100, activation='relu'))
# 1st conv block
model.add(Conv2D(25, (5, 5), activation='relu', strides=(1, 1), padding='same'))
model.add(MaxPool2D(pool_size=(2, 2), padding='same'))
model.add(BatchNormalization())
# # ANN block
model.add(Flatten())
model.add(Dense(units=100, activation='relu'))
model.add(Dropout(0.1))
# output layer
model.add(Dense(units=3, activation='softmax'))
model.summary()
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
# compile model
model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(learning_rate=0.001, name="Adam"), metrics=['accuracy'])
# fit on data for 30 epochs
reducelr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=0.0001)
es = EarlyStopping(monitor='val_loss', patience=5, min_delta=0.0001)
history = model.fit(x_train, y_train, epochs=30, validation_split = 0.2, verbose=1, callbacks=[reducelr, es])
img = cv2.imread('/content/drive/My Drive/Images_all/Randoms/lonely-japanese-cherry.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # open cv reads images in BGR format so we have to convert it to RGB
img = cv2.resize(img, (SIZE, SIZE)) #resizing image
img = img.astype('float32') / 255.0
test_img = img_to_array(img)
test_img = np.array(test_img)
test_img = np.reshape(test_img,(-1,SIZE,SIZE,3))
test_img.shape
model.predict(test_img)
model.save('/content/drive/My Drive/Images_all/saved_model/my_model.h5')