-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclassifier.py
106 lines (83 loc) · 3.07 KB
/
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
from skimage import color, io
from scipy.misc import imresize
import numpy as np
from sklearn.cross_validation import train_test_split
import matplotlib.pyplot as plt
import os
from glob import glob
import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
from tflearn.metrics import Accuracy
files_path = './train'
cat_files_path = os.path.join(files_path, 'cat*.jpg')
dog_files_path = os.path.join(files_path, 'dog*.jpg')
cat_files = sorted(glob(cat_files_path))
dog_files = sorted(glob(dog_files_path))
print(dog_files_path)
n_files = len(cat_files) + len(dog_files)
print(n_files)
size_image = 64
X = np.zeros((n_files, size_image, size_image, 3), dtype='float64')
y = np.zeros(n_files)
count = 0
for f in cat_files:
try:
img = io.imread(f)
new_img = imresize(img, (size_image, size_image, 3))
X[count] = np.array(new_img)
y[count] = 0
count += 1
except:
continue
for f in dog_files:
try:
img = io.imread(f)
new_img = imresize(img, (size_image, size_image, 3))
X[count] = np.array(new_img)
y[count] = 1
count += 1
except:
continue
X, X_test, Y, Y_test = train_test_split(X, y, test_size=0.1, random_state=42)
Y = to_categorical(Y, 2)
Y_test = to_categorical(Y_test, 2)
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
network = input_data(shape=[None, 64, 64, 3],
data_preprocessing=img_prep,
data_augmentation=img_aug)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
acc = Accuracy(name="Accuracy")
network = regression(network, optimizer='adam',
loss='categorical_crossentropy',
learning_rate=0.0005, metric=acc)
# Wrap the network in a model object
model = tflearn.DNN(network, checkpoint_path='model_cat_dog_6.tflearn', max_checkpoints = 3,
tensorboard_verbose = 3, tensorboard_dir='tmp/tflearn_logs/')
###################################
# Train model for 100 epochs
###################################
model.fit(X, Y, validation_set=(X_test, Y_test), batch_size=500,
n_epoch=100, run_id='model_cat_dog_6', show_metric=True)
model.save('model_cat_dog_6_final.tflearn')
plt.imshow(X[1])
plt.axis('off')
plt.title('Sample image with label {}'.format(Y[1]))
plt.show()
print(model.predict(X_test))