-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
348 lines (252 loc) · 10.8 KB
/
Copy pathmain.py
File metadata and controls
348 lines (252 loc) · 10.8 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
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
338
339
340
341
342
343
344
345
346
# Artist classification by painting
import tensorflow.python.keras.preprocessing.image
from PIL import Image
import tensorflow as tf
import tensorflow.keras as tfk
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import pandas as pd
import os
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import numpy as np
from PIL import ImageFile
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.utils import to_categorical
from sklearn import tree
import io
import pydotplus
from IPython.display import Image
ImageDir = 'C:/Users/14794/Documents/NACME_capstone/train_1/train_1/'
# ending = '.jpg'
CSVDir = 'C:/Users/14794/Documents/NACME_capstone/all_data_info.csv/all_data_info.csv'
def data_preprocessing():
# Load all_data_info.csv
labels = pd.read_csv(CSVDir)
# Creating dataframe from Image directory
list_dir = os.listdir('train_1/train_1/')
list_df = pd.DataFrame(list_dir, columns = ['new_filename'])
for i in list_df['new_filename']:
list_df['path'] = ImageDir + i
# List df has 11025 rows
# Joining dataframes drop non matching
new_df = pd.merge(labels, list_df, on ="new_filename")
# new_df = new_df.dropna()
# x_train, x_test, y_train, y_test = train_test_split(FEATURES, TARGET, test_size = 0.15, random_state = 42, shuffle =True)
# print( x_train.shape, x_test.shape, y_train.shape, y_test.shape)
# You can see after the 90th image it gives you an error IOError: image file truncated (80 bytes not processed)
# This happens when the file is too big. Lets print out the image
# print(list_dir[90])
# This prints out image 100102.jpg
# To fix this jsut add this statement above your call stack
ImageFile.LOAD_TRUNCATED_IMAGES = True
# Turning it into a byte array
painting_images = []
for i in tqdm(range(list_df.shape[0])):
img = image.load_img('train_1/train_1/'+ new_df['new_filename'][i], target_size=(224, 224, 3))
img = image.img_to_array(img)
img = image.load_img('train_1/train_1/'+ new_df['new_filename'][i], target_size=(100, 100, 3))
img = image.img_to_array(img)
# img = keras.preprocessing.image.smart_resize(img, (100, 100))
img = img/255
painting_images.append(img)
# print('train_1/train_1/'+ new_df['new_filename'][i])
# print(img)
X = np.array(painting_images)
# Looking at other features
d = []
for a, b in new_df.iterrows():
array = []
g = str(b['genre'])
s = str(b['style'])
array.append(g)
array.append(s)
print(array)
d.append(array)
y = np.array(new_df['artist'])
return X, y, d
def cleaning_data(x_array, y_array, features):
# Create dataframe
dataframe_1 = pd.DataFrame(x_array.reshape((y_array.shape[0], -1)), columns=list(range(150528)))
dataframe_2 = pd.DataFrame(y_array, columns=["filename"])
dataframe_3 = pd.DataFrame(features, columns= ["genre", "style"])
df = pd.merge(dataframe_1, dataframe_3, left_index=True, right_index=True)
df = pd.merge(df, dataframe_2, left_index = True, right_index = True)
df = df.dropna()
# drop columns where there are less than 10 paintings
dataframe = df[df['count'] >= 10]
print(dataframe.head(10))
# see how many artists
labels = [x for x in dataframe["filename"].unique()]
num_artists = len(labels)
print(num_artists)
# turn back into arrays
X_array = dataframe.iloc[:,:150528].to_numpy()
Y_array = dataframe['filename'].to_numpy()
artists = list(np.unique(Y_array))
num_classes = len(artists)
Y_array = np.array([artists.index(i) for i in Y_array])
Y_array = to_categorical(Y_array)
# train test split
x_train, x_test, y_train, y_test = train_test_split(X_array, Y_array, random_state=42, test_size=0.15, shuffle=True)
print(x_train.shape, y_train.shape, y_train.shape, y_test.shape)
# reshape to correct dimensions
x_train = x_train.reshape((y_train.shape[0], 224, 224, 3))
x_test = x_test.reshape((y_test.shape[0], 224, 224, 3))
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
return x_train, x_test, y_train, y_test, num_artists, dataframe
# CNN method
def create_model(num_classes):
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(5, 5), activation="relu", input_shape=(224, 224, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=32, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
# print(plt.imshow(X[2]))
# print(new_df.columns)
# print(new_df[0])
# FEATURES = [new_df[['date', 'genre','style' ]], X]
# # print(FEATURES)
# TARGET = new_df[['artist']]
# # print(TARGET)
# YYYYYYYYYEEEETTTTT
y = np.array(new_df['artist'])
# y = np.array(new_df['date', 'genre', 'style'], X)
# x = []
# for a, b in new_df.iterrows():
# array = []
# for paintings in np.nditer(X):
# p = X(paintings)
#
# d = str(b['date'])
# g = str(b['genre'])
# s = str(b['style'])
# array.append(d)
# array.append(g)
# array.append(s)
# array.append(p)
# print(array)
# x.append(array)
labels = [x for x in new_df["artist"].unique()]
num_artists = len(labels)
# x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.15, random_state = 42, shuffle =True)
return X, y, num_artists
# return x_train, x_test, y_train, y_test, num_artists
def create_model(num_classes):
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(5, 5), activation="relu", input_shape=(100,100,3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=32, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
return model
# RESNET50 method
def RESNET50(num_classes):
resnet = Sequential()
pretrained = tensorflow.keras.applications.ResNet50(include_top = False,
input_shape = (100, 100, 3),
pooling = 'avg',
classes = 5,
weights = 'imagenet')
for layer in pretrained.layers:
layer.trainable = False
resnet.add(pretrained)
resnet.add(Flatten())
resnet.add(Dense(128, activation='relu'))
resnet.add(Dropout(0.5))
resnet.add(Dense(64, activation='relu'))
resnet.add(Dense(32, activation='relu'))
resnet.add(Dropout(0.5))
resnet.add(Dropout(0.25))
resnet.add(Dense(num_classes, activation='softmax'))
return resnet
if __name__ == "__main__":
x_array, y_array, feature_array = data_preprocessing()
x_train, x_test, y_train, y_test, num_classes, data = cleaning_data(x_array, y_array, feature_array)
print(data)
print(data.columns)
# Run CNN model
model = create_model(num_classes)
model.summary()
model.compile(optimizer = 'adam', loss = "categorical_crossentropy", metrics = ['accuracy'])
history = model.fit(x_train, y_train, epochs=250, batch_size = 18)
# Plot CNN model
epochs = range(250)
plt.plot(epochs, history.history['accuracy'])
plt.show()
plt.plot(epochs, history.history['loss'])
plt.show()
model.save('./ckpts/Model_1')
evaluate = model.evaluate(x_test, y_test)
print(evaluate)
# Run RESNET50
ResNet = RESNET50(num_classes)
ResNet.compile(optimizer = 'adam', loss = "categorical_crossentropy", metrics = ['accuracy'])
ResHistory = ResNet.fit(x_train, y_train, epochs = 100, batch_size = 15)
ResNet.save('.ckpts/ResNet')
# Plot RESNET
ResNet.evaluate(x_test, y_test)
plt.plot(epochs, ResHistory.history['accuracy'])
plt.show()
plt.plot(epochs, ResHistory.history['loss'])
plt.show()
if __name__ == "__main__":
x_array, y_array, num_classes = data_preprocessing()
print(x_array.shape)
print(y_array.shape)
# dataframe = pd.DataFrame(x_array.reshape((y_array.shape[0], -1)), y_array, columns=['image_bytes', 'filename'])
dataframe_1 = pd.DataFrame(x_array.reshape((y_array.shape[0], -1)), columns=list(range(30000)))
dataframe_2 = pd.DataFrame(y_array, columns=["filename"])
dataframe = pd.merge(dataframe_1, dataframe_2, left_index=True, right_index=True)
dataframe = dataframe.dropna()
# labels = [x for x in dataframe["artist"].unique()]
print(dataframe.isna().sum())
X_array = dataframe[list(range(30000))].to_numpy()
Y_array = dataframe['filename'].to_numpy()
artists = list(np.unique(Y_array))
num_classes = len(artists)
Y_array = np.array([artists.index(i) for i in Y_array])
Y_array = to_categorical(Y_array)
# for painters in y_array:
# if painters = naN
# print(x_array[0])
# print(y_array[0])
x_train, x_test, y_train, y_test = train_test_split(X_array, Y_array, random_state = 42, test_size = 0.15, shuffle = True)
x_train = x_train.reshape((y_train.shape[0], 100, 100, 3))
x_test = x_test.reshape((y_test.shape[0], 100, 100, 3))
# print(x_train, y_test, y_train, y_test)
print(x_train.shape, y_test.shape, y_train.shape, y_test.shape)
# x_train, x_test, y_train, y_test, num_classes = data_preprocessing()
model = create_model(num_classes)
model.summary()
model.compile(optimizer = 'adam', loss = "categorical_crossentropy", metrics = ['accuracy'])
#
history = model.fit(x_train, y_train, epochs=10, batch_size = 24, validation_split = 0.15)
model.save('./ckpts/epoch10')
#
# print(x_train.shape)
#
# model.fit(x_train, y_train, epochs = 10, batch_size= 64)