-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnnmodel.py
170 lines (145 loc) · 5.41 KB
/
cnnmodel.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
168
169
170
# Imports
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import tensorflow as tf
from tensorflow import keras
tf.logging.set_verbosity(tf.logging.INFO)
# Our application logic will be added here
def cnn_model_fn(features, labels, mode):
"""Model function for CNN."""
# Input Layer
input_layer = tf.reshape(features["x"], [-1, 40, 30, 3])
print(input_layer)
input_layer = tf.cast(input_layer, dtype=tf.float32)
input_norm = tf.layers.batch_normalization(input_layer)
# print(input_norm)
# Convolutional Layer #1
conv1 = tf.layers.conv2d(
inputs=input_norm,
filters=16,
kernel_size=[5, 5],
padding='valid',
activation=tf.nn.relu,
name='conv1')
print(conv1)
# Pooling Layer #1
# conv1_norm = tf.layers.batch_normalization(conv1)
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[3, 3], strides=2)
# pool1 = tf.layers.batch_normalization(pool1)
# print(pool1)
# # Convolutional Layer #2 and Pooling Layer #2
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=32,
kernel_size=[5, 5],
padding="valid",
activation=tf.nn.relu)
print(conv2)
# conv2 = tf.layers.batch_normalization(conv2)
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
# pool2 = tf.layers.batch_normalization(pool2)
# print(pool2)
pool2_flat = tf.reshape(pool2, [-1, 4*6*32])
# dconv1=tf.layers.conv2d_transpose()
# Dense Layer
dense1 = tf.layers.dense(inputs=pool2_flat, units=256, activation=tf.nn.relu) # 36/2
# dropout1 = tf.layers.dropout(
# inputs=dense1, rate=0, training=mode == tf.estimator.ModeKeys.TRAIN)
dense2 = tf.layers.dense(inputs=dense1, units=80, activation=tf.nn.relu)
dense3 = tf.layers.dense(inputs=dense2, units=2, activation=tf.nn.relu)
# dropout = tf.layers.dropout(
# inputs=dense3, rate=0.2, training=mode == tf.estimator.ModeKeys.TRAIN)
output = dense3
logits=output
predictions = {
'val': output
}
predicted = tf.argmax(input=logits, axis=1)
lableclass = tf.argmax(input=labels, axis=1)
print(lableclass.shape)
print(predicted.shape)
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'class': predicted,
'probabilities': tf.nn.softmax(logits),
'logits': logits,
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
# Calculate Loss (for both TRAIN and EVAL modes)
loss = tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=output)
tf.summary.scalar('loss', loss)
if mode == tf.estimator.ModeKeys.TRAIN:
rate = tf.train.exponential_decay(learning_rate=0.01, global_step=tf.train.get_global_step(), decay_steps=100,
decay_rate=0.98, staircase=False)
train_op = tf.train.AdadeltaOptimizer(learning_rate=rate).minimize(
loss=loss,
global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
# Add evaluation metrics (for EVAL mode)
with tf.name_scope('accuracy'):
accuracy = tf.metrics.accuracy(labels=lableclass,
predictions=predicted,
name='acc_op')
tf.summary.scalar('accuracy', accuracy)
if mode == tf.estimator.ModeKeys.EVAL:
eval_metric_ops = {"accuricy":accuracy}
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss,eval_metric_ops=eval_metric_ops)
def main(unused_argv):
# Load training and eval data
datax = np.load('x.npy')
datay = np.load('y.npy')
# datax = datax.reshape([-1, 40, 40, 6])
# my_feature_columns = [tf.feature_column.numeric_column(key='x', shape=[14400])]
# train_data = datax[0:340] # Returns np.array
# train_labels = datay[0:340]
#
# eval_data = datax[300:340]
# eval_labels = datay[300:340]
train_data=[]
train_labels=[]
eval_data=[]
eval_labels=[]
for i in range(0,len(datax)):
if(i&4==0):
eval_data.append(datax[i])
eval_labels.append(datay[i])
else:
train_data.append(datax[i])
train_labels.append(datay[i])
eval_data=np.array(eval_data)
eval_labels=np.array(eval_labels)
train_data=np.array(train_data)
train_labels=np.array(train_labels)
estimator = tf.estimator.Estimator(
model_fn=cnn_model_fn,
model_dir='tensorboard/model'
)
# Set up logging for predictions
tensors_to_log = {"val"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": train_data},
y=train_labels,
batch_size=128,
num_epochs=None,
shuffle=True,
num_threads=4)
train_res = estimator.train(
input_fn=train_input_fn,
steps=1000
)
print('train result')
print(train_res)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": eval_data},
y=eval_labels,
num_epochs=64,
shuffle=False)
eval_results = estimator.evaluate(input_fn=eval_input_fn)
print(eval_results)
tf.logging.set_verbosity(tf.logging.INFO)
# Our application logic will be added here
if __name__ == "__main__":
tf.app.run()