-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn.py
92 lines (58 loc) · 2.84 KB
/
cnn.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
import numpy as np
import tensorflow as tf
#import yolo.config_card as cfg
import IPython
slim = tf.contrib.slim
class CNN(object):
def __init__(self,classes,image_size):
'''
Initializes the size of the network
'''
self.classes = classes
self.num_class = len(self.classes)
self.image_size = image_size
self.output_size = self.num_class
self.batch_size = 40
self.images = tf.placeholder(tf.float32, [None, self.image_size,self.image_size,3], name='images')
self.logits = self.build_network(self.images, num_outputs=self.output_size)
self.labels = tf.placeholder(tf.float32, [None, self.num_class])
self.loss_layer(self.logits, self.labels)
self.total_loss = tf.losses.get_total_loss()
tf.summary.scalar('total_loss', self.total_loss)
def build_network(self,
images,
num_outputs,
scope='yolo'):
with tf.variable_scope(scope):
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_initializer=tf.truncated_normal_initializer(0.0, 0.01),
weights_regularizer=slim.l2_regularizer(0.0005)):
###SLIM SPECIFYING A CONV LAYER WITH 5 FILters as SIZE 15 by 15
net = slim.conv2d(images, 5, [15,15], scope='conv_0')
###SAVE RESPONSE MAP HERE FOR VIZ_FEATURES
self.response_map = net
### SLIM USING POOLING ON THE NETWORK. THE POOLING REGION CONSIDERED IS 3 by 3
net = slim.max_pool2d(net, [3, 3], scope='pool')
### TO GO FROM A CONVOLUTIONAL LAYER TO A FULLY CONNECTED YOU NEED TO FLATTEN THE ARRAY
net = slim.flatten(net, scope='flat')
###SLIM SPECIFYING A FULLY CONNECTED LAYER WHOSE OUT IS 512
net = slim.fully_connected(net, 512, scope='fc_2')
###SLIM SPECIFYING A FULLY CONNECTED LAYER WHOSE OUT IS 25
net = slim.fully_connected(net, 25, scope='fc_3')
return net
def get_acc(self,y_,y_out):
'''
compute accurracy given two tensorflows arrays
y_ (the true label) and y_out (the predict label)
'''
cp = tf.equal(tf.argmax(y_out,1), tf.argmax(y_,1))
ac = tf.reduce_mean(tf.cast(cp, tf.float32))
return ac
def loss_layer(self, predicts, classes, scope='loss_layer'):
'''
The loss layer of the network, which is written for you.
You need to fill in get_accuracy to report the performance
'''
with tf.variable_scope(scope):
self.class_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = classes,logits = predicts))
self.accurracy = self.get_acc(classes,predicts)