-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtfcnn.py
More file actions
executable file
·161 lines (117 loc) · 5.74 KB
/
tfcnn.py
File metadata and controls
executable file
·161 lines (117 loc) · 5.74 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
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
#! /usr/bin/env python2.7
import sys
sys.path.append('..')
import tensorflow as tf
from dataHandling import loadData
import numpy as np
import time
import math
fcneurons = 512
features = [16, 32]
class Model:
def get_data(self):
return loadData([ int(s) for s in sys.argv[1:4] ])
def initialize(self):
self.data = self.get_data()
self.height = self.data.get_height()
self.width = self.data.get_width()
# Semi-Constants
self.channels = 3
self.classes = 3
self.imageSize = self.height * self.width * self.channels
def run(self, kernellen, features, fcneurons, createMap=False, name='', verbose=False):
self.initialize()
#input data
x = tf.placeholder(tf.float32, [None, self.imageSize])
#labels
y_ = tf.placeholder(tf.float32, [None, self.classes])
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def softmax(inp, weights, bias):
return tf.nn.softmax(tf.matmul(inp, weights) + bias)
def fully_connected(inp, weights, bias, inWidth, inHeight, inFeat, outFeat):
inp_reshape = tf.reshape(inp, [-1 , inHeight * inWidth * inFeat])
return tf.nn.relu(tf.matmul(inp_reshape, weights) + bias)
def conv_relu(inp, weight, bias):
conv = tf.nn.conv2d(inp, weight, strides=[1, 1, 1, 1], padding='SAME')
return tf.nn.relu(conv + bias)
# NETWORK
x_image = tf.reshape(x, [-1, self.height, self.width, self.channels])
#first conv layer
with tf.variable_scope("incep1"):
with tf.variable_scope("conv1"):
weights = weight_variable([1, 1, self.channels, features[0]])
bias = bias_variable([features[0]])
conv1 = conv_relu(x_image, weights, bias)
with tf.variable_scope("conv3"):
weights = weight_variable([3, 3, self.channels, features[0]])
bias = bias_variable([features[0]])
conv3 = conv_relu(x_image, weights, bias)
with tf.variable_scope("conv5"):
weights = weight_variable([5, 5, self.channels, features[0]])
bias = bias_variable([features[0]])
conv5 = conv_relu(x_image, weights, bias)
pool = tf.nn.max_pool(x_image, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME')
with tf.variable_scope("pc"):
weights = weight_variable([1, 1, self.channels, features[0]])
bias = bias_variable([features[0]])
pc = conv_relu(pool, weights, bias)
incep1 = tf.concat(3, [conv1, conv3, conv5, pc])
with tf.variable_scope("incep2"):
with tf.variable_scope("conv1"):
weights = weight_variable([1, 1, 4 * features[0], features[1]])
bias = bias_variable([features[1]])
conv1 = conv_relu(incep1, weights, bias)
with tf.variable_scope("conv3"):
weights = weight_variable([3, 3, 4 * features[0], features[1]])
bias = bias_variable([features[1]])
conv3 = conv_relu(incep1, weights, bias)
with tf.variable_scope("conv5"):
weights = weight_variable([5, 5, 4 * features[0], features[1]])
bias = bias_variable([features[1]])
conv5 = conv_relu(incep1, weights, bias)
pool = tf.nn.max_pool(incep1, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME')
with tf.variable_scope("pc"):
weights = weight_variable([1, 1, 4 * features[0], features[1]])
bias = bias_variable([features[1]])
pc = conv_relu(pool, weights, bias)
incep2 = tf.concat(3, [conv1, conv3, conv5, pc])
#fully connected layer
with tf.variable_scope("fc1"):
weights = weight_variable([self.width * self.height * (features[-1] * 4), fcneurons])
bias = bias_variable([fcneurons])
fc1 = fully_connected(incep2, weights, bias, self.width, self.height , (features[-1] * 4), fcneurons)
#Drop out
keep_prob = tf.placeholder(tf.float32)
fc1_drop = tf.nn.dropout(fc1, keep_prob)
#softmax
with tf.variable_scope("sf1"):
weights = weight_variable([fcneurons, self.classes])
bias = bias_variable([self.classes])
y_conv = softmax(fc1_drop, weights, bias)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv + 1e-10), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(5e-5).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
test = self.data.get_test()
for i in range(3500):
batch = self.data.get_train(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:test[0], y_:test[1], keep_prob: 1.0})
if verbose:
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
return accuracy.eval(feed_dict={x: test[0], y_: test[1], keep_prob: 1.0})
fil = open(sys.argv[4], 'a')
test = Model()
out = 0
while out < .6:
out = test.run(5, features, fcneurons, verbose=False)
fil.write(str(out)+'\n')