-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrain.py
123 lines (93 loc) · 4.02 KB
/
train.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
import tensorflow as tf
import cv2
import sys
import numpy as np
sess = tf.Session()
sess.run(tf.global_variables_initializer())
def parser(record):
keys_to_features = {
"image_raw": tf.FixedLenFeature([], tf.string),
"label": tf.FixedLenFeature([], tf.int64)
}
parsed = tf.parse_single_example(record, keys_to_features)
image = tf.decode_raw(parsed["image_raw"], tf.uint8)
image = tf.cast(image, tf.float32)
#image = tf.reshape(image, shape=[224, 224, 3])
label = tf.cast(parsed["label"], tf.int32)
return {'image': image}, label
def input_fn(filenames):
dataset = tf.data.TFRecordDataset(filenames=filenames, num_parallel_reads=40)
dataset = dataset.apply(
tf.contrib.data.shuffle_and_repeat(1024, 1)
)
dataset = dataset.apply(
tf.contrib.data.map_and_batch(parser, 32)
)
#dataset = dataset.map(parser, num_parallel_calls=12)
#dataset = dataset.batch(batch_size=1000)
dataset = dataset.prefetch(buffer_size=2)
return dataset
def train_input_fn():
return input_fn(filenames=["train.tfrecords", "test.tfrecords"])
def val_input_fn():
return input_fn(filenames=["val.tfrecords"])
def model_fn(features, labels, mode, params):
num_classes = 3
net = features["image"]
net = tf.identity(net, name="input_tensor")
net = tf.reshape(net, [-1, 224, 224, 3])
net = tf.identity(net, name="input_tensor_after")
net = tf.layers.conv2d(inputs=net, name='layer_conv1',
filters=32, kernel_size=3,
padding='same', activation=tf.nn.relu)
net = tf.layers.max_pooling2d(inputs=net, pool_size=2, strides=2)
net = tf.layers.conv2d(inputs=net, name='layer_conv2',
filters=64, kernel_size=3,
padding='same', activation=tf.nn.relu)
net = tf.layers.max_pooling2d(inputs=net, pool_size=2, strides=2)
net = tf.layers.conv2d(inputs=net, name='layer_conv3',
filters=64, kernel_size=3,
padding='same', activation=tf.nn.relu)
net = tf.layers.max_pooling2d(inputs=net, pool_size=2, strides=2)
net = tf.contrib.layers.flatten(net)
net = tf.layers.dense(inputs=net, name='layer_fc1',
units=128, activation=tf.nn.relu)
net = tf.layers.dropout(net, rate=0.5, noise_shape=None,
seed=None, training=(mode == tf.estimator.ModeKeys.TRAIN))
net = tf.layers.dense(inputs=net, name='layer_fc_2',
units=num_classes)
logits = net
y_pred = tf.nn.softmax(logits=logits)
y_pred = tf.identity(y_pred, name="output_pred")
y_pred_cls = tf.argmax(y_pred, axis=1)
y_pred_cls = tf.identity(y_pred_cls, name="output_cls")
if mode == tf.estimator.ModeKeys.PREDICT:
spec = tf.estimator.EstimatorSpec(mode=mode,
predictions=y_pred_cls)
else:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels,
logits=logits)
loss = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate=params["learning_rate"])
train_op = optimizer.minimize(
loss=loss, global_step=tf.train.get_global_step())
metrics = {
"accuracy": tf.metrics.accuracy(labels, y_pred_cls)
}
spec = tf.estimator.EstimatorSpec(
mode=mode,
loss=loss,
train_op=train_op,
eval_metric_ops=metrics)
return spec
model = tf.estimator.Estimator(model_fn=model_fn,
params={"learning_rate": 1e-4},
model_dir="./model5/")
count = 0
while (count < 100000):
model.train(input_fn=train_input_fn, steps=1000)
result = model.evaluate(input_fn=val_input_fn)
print(result)
print("Classification accuracy: {0:.2%}".format(result["accuracy"]))
sys.stdout.flush()
count = count + 1