forked from pk00095/transfer-learning-with-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep_network.py
80 lines (58 loc) · 2.58 KB
/
deep_network.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
import tensorflow as tf
import tensorflow.contrib.slim as slim
num_columns=24
num_classes=4
def model(mode=tf.estimator.ModeKeys.TRAIN):
ground_truth = tf.placeholder(tf.float32,[None,4], name = 'GroundTruthInputPlaceholder') #onehotencoded with depth 4
input_tensor = tf.placeholder(tf.float32,[None,num_columns],name = 'BottlenecksPlaceholder') #num_columns=18 keypoint features
final_tensor_name = 'SoftmaxLayer'
with tf.name_scope('Network'):
WI = tf.truncated_normal_initializer(mean=0,stddev=0.001)
BI = tf.zeros_initializer()
with slim.arg_scope([slim.fully_connected],weights_initializer = WI, biases_initializer = BI ,trainable = True):
input_tensor = slim.flatten(input_tensor)
print(input_tensor.shape)
net = slim.fully_connected(input_tensor,128,scope='fc1')
print(net.shape)
net = slim.fully_connected(net,128,scope='fc2')
print(net.shape)
logits = slim.fully_connected(net,num_classes,activation_fn=None,scope='fc3')
print(logits.shape)
#net = slim.flatten(net,scope = 'flat')
#logits = tf.nn.softmax(net, name=final_tensor_name)
#print(logits.shape)
predicted_classes = tf.argmax(logits, 1)
# Create prediction ops.
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'class': predicted_classes,
'prob': tf.nn.softmax(logits)
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
# Compute loss.
loss = tf.nn.softmax_cross_entropy_with_logits_v2(labels=ground_truth, logits=logits)
loss_mean = tf.reduce_mean(loss)
#predicted_classes = tf.argmax(logits)
accuracy = tf.metrics.accuracy(labels=ground_truth,
predictions=logits,
name='acc_op')
metrics = {'accuracy': accuracy}
tf.summary.scalar('accuracy', accuracy[1])
# Create training ops.
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.MomentumOptimizer(
learning_rate=0.1,
use_nesterov=True,
momentum=0.9)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
train_op = optimizer.minimize(loss_mean, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode = mode, loss=loss_mean, train_op=train_op)
#creating test ops.
eval_metric_ops = {
'accuracy': tf.metrics.accuracy(
labels=tf.argmax(ground_truth,1), predictions=predicted_classes)
}
return tf.estimator.EstimatorSpec(
mode, loss=loss, eval_metric_ops=eval_metric_ops)
print model(tf.estimator.ModeKeys.EVAL)