-
Notifications
You must be signed in to change notification settings - Fork 18
/
b-29-CNN.py
108 lines (81 loc) · 3.14 KB
/
b-29-CNN.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
# ilkleme
classifier = Sequential()
# Adım 1 - Convolution
classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu'))
# Adım 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# 2. convolution katmanı
classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adım 3 - Flattening
classifier.add(Flatten())
# Adım 4 - YSA
classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dense(output_dim = 1, activation = 'sigmoid'))
# CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# CNN ve resimler
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set = train_datagen.flow_from_directory('veriler/training_set',
target_size = (64, 64),
batch_size = 1,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('veriler/test_set',
target_size = (64, 64),
batch_size = 1,
class_mode = 'binary')
classifier.fit_generator(training_set,
samples_per_epoch = 8000,
nb_epoch = 1,
validation_data = test_set,
nb_val_samples = 2000)
import numpy as np
import pandas as pd
test_set.reset()
pred=classifier.predict_generator(test_set,verbose=1)
#pred = list(map(round,pred))
pred[pred > .5] = 1
pred[pred <= .5] = 0
print('prediction gecti')
#labels = (training_set.class_indices)
test_labels = []
for i in range(0,int(203)):
test_labels.extend(np.array(test_set[i][1]))
print('test_labels')
print(test_labels)
#labels = (training_set.class_indices)
'''
idx = []
for i in test_set:
ixx = (test_set.batch_index - 1) * test_set.batch_size
ixx = test_set.filenames[ixx : ixx + test_set.batch_size]
idx.append(ixx)
print(i)
print(idx)
'''
dosyaisimleri = test_set.filenames
#abc = test_set.
#print(idx)
#test_labels = test_set.
sonuc = pd.DataFrame()
sonuc['dosyaisimleri']= dosyaisimleri
sonuc['tahminler'] = pred
sonuc['test'] = test_labels
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(test_labels, pred)
print (cm)