-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoxford_cnn.py
More file actions
executable file
·96 lines (77 loc) · 3.3 KB
/
oxford_cnn.py
File metadata and controls
executable file
·96 lines (77 loc) · 3.3 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
from __future__ import division, print_function, absolute_import
import tensorflow as tf
import tflearn
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 os import listdir
from os.path import isfile, join
from tflearn.layers.normalization import local_response_normalization
import ntpath
def load_images(image_path):
#list all filenames in image_path directory
filenames = [f for f in listdir(image_path) if isfile(join(image_path, f))]
num_images = len(filenames)
#prefix the filenames with image_path
filenames_full_path = [image_path + filename for filename in filenames]
image_queue = tf.train.string_input_producer(filenames_full_path)
reader = tf.WholeFileReader()
key, value = reader.read(image_queue)
images = tf.image.decode_jpeg(value, channels=3)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
imageList = []
labels = []
for _ in range(num_images): #length of your filename list
path = sess.run(key)
print(path)
firstLetterOfImageFile = ntpath.basename(path)[:1]
if(firstLetterOfImageFile == b'd'):
labels.append([0, 1])
else:
labels.append([1, 0])
currentImage = sess.run(images)
imageList.append(currentImage)
X = sess.run(tf.pack(imageList));
print(X.shape)
Y = sess.run(tf.pack(labels));
coord.request_stop()
coord.join(threads)
del images
del imageList
del labels
sess.close()
return X,Y, num_images
X,Y,_ = load_images("/home/emooo/PycharmProjects/machine_learning/alexnet/train/")
Xtest,Ytest,_ = load_images("/home/emooo/PycharmProjects/machine_learning/alexnet/test/")
print(Y)
print(Ytest)
network = input_data(shape=[None, 227, 227, 3])
network = conv_2d(network, 96, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='momentum',
loss='categorical_crossentropy',
learning_rate=0.001)
# Training
model = tflearn.DNN(network, checkpoint_path='model_alexnet',
max_checkpoints=1, tensorboard_verbose=2)
model.fit(X, Y, n_epoch=1000, validation_set=0.1, shuffle=True,
show_metric=True, batch_size=64, snapshot_step=200,
snapshot_epoch=False, run_id='alexnet_oxflowers17')
print(model.predict(Xtest))