-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
95 lines (79 loc) · 2.82 KB
/
model.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
from keras._tf_keras.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from sklearn.model_selection import train_test_split
from keras._tf_keras.keras.models import Sequential
import matplotlib.pyplot as plt
from pathlib import Path
from PIL import Image
import numpy as nmp
import os
def labeling(positive_dir, negative_dir):
images, labels = [], []
def process_img(directroy_path, label):
for img_path in Path(directroy_path).glob('*'):
with Image.open(img_path) as img:
img = img.resize((32, 32))
img_array = nmp.array(img) / 255.0
images.append(img_array)
labels.append(label)
process_img(positive_dir, 1)
process_img(negative_dir, 0)
x = nmp.array(images)
y = nmp.array(labels)
# shuffle data
indices = nmp.arange(x.shape[0])
nmp.random.shuffle(indices)
x = x[indices]
y = y[indices]
return (x,y)
x, y = labeling(positive_folder, negative_folder)
# split data to train, test
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=42, test_size=0.01)
# building the model
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(32,32,3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.1))
model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.1))
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.1))
model.add(Flatten())
model.add(Dense(units=256, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(units=128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(units=64, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(units=1, activation='sigmoid'))
# compile model
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# train model
train = model.fit(x_train, y_train,
batch_size=15,
epochs=10,
validation_split=0.1)
# evulate model
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test doğruluğu: {test_acc} | Test kaybı: {test_loss}")
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(train.history['loss'], label='Training Loss')
plt.plot(train.history['val_loss'], label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.title('Loss')
plt.subplot(1, 2, 2)
plt.plot(train.history['accuracy'], label='Training Accuracy')
plt.plot(train.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Accuracy')
plt.show()
# save model
model.save("CNN-MODEL.h5")