-
Notifications
You must be signed in to change notification settings - Fork 7
/
train_and_val.py
167 lines (128 loc) · 5.95 KB
/
train_and_val.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
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
#encoding=utf-8
import os
import numpy as np
import tensorflow as tf
import input_data
import model
# you need to change the directories to yours.
train_dir = '/Users/aria/MyDocs/cat_vs_dogs/train/'
test_dir = '/Users/aria/MyDocs/cat_vs_dogs/test/'
train_logs_dir = './logs/train/'
val_logs_dir = './logs/val'
N_CLASSES = 2
IMG_W = 208 # resize the image, if the input image is too large, training will be very slow.
IMG_H = 208
RATIO = 0.2 # take 20% of dataset as validation data
BATCH_SIZE = 64
CAPACITY = 2000
MAX_STEP = 10000 # with current parameters, it is suggested to use MAX_STEP>10k
learning_rate = 0.0001 # with current parameters, it is suggested to use learning rate<0.0001
def training():
train, train_label, val, val_label = input_data.get_files(train_dir, RATIO)
train_batch, train_label_batch = input_data.get_batch(train,
train_label,
IMG_W,
IMG_H,
BATCH_SIZE,
CAPACITY)
val_batch, val_label_batch = input_data.get_batch(val,
val_label,
IMG_W,
IMG_H,
BATCH_SIZE,
CAPACITY)
#val_batch[64,208,208,3] val_label_batch[64,]
logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
loss = model.losses(logits, train_label_batch)
train_op = model.trainning(loss, learning_rate)#获取成本函数
acc = model.evaluation(logits, train_label_batch)
x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
y_ = tf.placeholder(tf.int16, shape=[BATCH_SIZE])
with tf.Session() as sess:
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess= sess, coord=coord)
summary_op = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(train_logs_dir, sess.graph)
val_writer = tf.summary.FileWriter(val_logs_dir, sess.graph)
try:
for step in np.arange(MAX_STEP):
if coord.should_stop():
break
tra_images,tra_labels = sess.run([train_batch, train_label_batch])
# _, tra_loss, tra_acc = sess.run([train_op, loss, acc],
# feed_dict={x:tra_images, y_:tra_labels})
_, tra_loss, tra_acc = sess.run([train_op, loss, acc])
if step % 50 == 0:
print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0))
summary_str = sess.run(summary_op)
train_writer.add_summary(summary_str, step)
if step % 200 == 0 or (step + 1) == MAX_STEP:
val_images, val_labels = sess.run([val_batch, val_label_batch])
val_loss, val_acc = sess.run([loss, acc])
print('** Step %d, val loss = %.2f, val accuracy = %.2f%% **' %(step, val_loss, val_acc*100.0))
summary_str = sess.run(summary_op)
val_writer.add_summary(summary_str, step)
if step % 2000 == 0 or (step + 1) == MAX_STEP:
checkpoint_path = os.path.join(train_logs_dir, 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=step)
except tf.errors.OutOfRangeError:
print('Done training -- epoch limit reached')
finally:
coord.request_stop()
coord.join(threads)
#Test one image
def get_one_image(file_dir):
"""
Randomly pick one image from test data
Return: ndarray
"""
from PIL import Image
import matplotlib.pyplot as plt
test =[]
for file in os.listdir(file_dir):
test.append(file_dir + file)
print('There are %d test pictures\n' %(len(test)))
n = len(test)
ind = np.random.randint(0, n)
print(ind)
img_test = test[ind]
image = Image.open(img_test)
plt.imshow(image)
image = image.resize([208, 208])
image = np.array(image)
return image
def test_one_image():
"""
Test one image with the saved models and parameters
"""
test_image = get_one_image(test_dir)
with tf.Graph().as_default():
BATCH_SIZE = 1
N_CLASSES = 2
image = tf.cast(test_image, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1, 208, 208, 3])
logit = model.inference(image, BATCH_SIZE, N_CLASSES)
logit = tf.nn.softmax(logit)
x = tf.placeholder(tf.float32, shape=[208, 208, 3])
saver = tf.train.Saver()
with tf.Session() as sess:
print("Reading checkpoints...")
ckpt = tf.train.get_checkpoint_state(train_logs_dir)
if ckpt and ckpt.model_checkpoint_path:
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
saver.restore(sess, ckpt.model_checkpoint_path)
print('Loading success, global_step is %s' % global_step)
else:
print('No checkpoint file found')
prediction = sess.run(logit, feed_dict={x: test_image})
max_index = np.argmax(prediction)
if max_index==0:
print('This is a cat with possibility %.6f' %prediction[:, 0])
else:
print('This is a dog with possibility %.6f' %prediction[:, 1])
if __name__ == '__main__':
training()
#test_one_image()