-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
116 lines (83 loc) · 4.25 KB
/
model.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
import numpy as np
import tensorflow as tf
import utils
import tensorflow.keras.datasets as datasets
from sklearn.preprocessing import MinMaxScaler
padding = "SAME"
class RCNN(tf.Module):
def __init__(self, num_classes, num_layers, learning_rate, dropout_rate, leaky_relu_alpha, output_layer_1, output_layer_2, model_v):
self.num_classes = num_classes
self.num_layers = num_layers
self.dropout_rate = dropout_rate
self.leaky_relu_alpha = leaky_relu_alpha
self.model_v = model_v
self.initializer = tf.initializers.glorot_uniform()
self.optimizer = tf.optimizers.Adam(learning_rate)
self.output_layer_1 = output_layer_1
self.output_layer_2 = output_layer_2
self.w_conv1 = tf.Variable(tf.random.truncated_normal(
[8, 8, 3 + self.num_classes, self.output_layer_1], stddev=0.1))
self.b_conv1 = tf.Variable(tf.constant(
0.1, shape=[self.output_layer_1]))
self.w_conv2 = tf.Variable(tf.random.truncated_normal(
[8, 8, self.output_layer_1, self.output_layer_2], stddev=0.1))
self.b_conv2 = tf.Variable(tf.constant(
0.1, shape=[self.output_layer_2]))
self.w_conv3 = tf.Variable(tf.random.truncated_normal(
[1, 1, self.output_layer_2, self.num_classes], stddev=0.1))
self.b_conv3 = tf.Variable(tf.constant(0.1, shape=[self.num_classes]))
def conv2d(self, inputs, filters, stride_size):
out = tf.nn.conv2d(inputs, filters, strides=[
1, stride_size, stride_size, 1], padding=padding)
return out
def maxpool(self, inputs, pool_size, stride_size):
return tf.nn.max_pool2d(inputs, ksize=[1, pool_size, pool_size, 1], padding='SAME', strides=[1, stride_size, stride_size, 1])
def loss(self, pred, target):
return tf.losses.categorical_crossentropy(target, pred)
def get_weight(self, shape, name):
return tf.Variable(self.initializer(shape), name=name, trainable=True, dtype=tf.float32)
def model_v1(self, inpt):
self.input = tf.Variable(initial_value=inpt, dtype=tf.float32, shape=[
None, None, None, 3 + self.num_classes], trainable=False)
current_input = self.input
self.predictions = []
self.logits = []
for n_layer in range(self.num_layers):
h_conv1 = self.conv2d(
current_input, self.w_conv1, 1) + self.b_conv1
h_pool1 = self.maxpool(h_conv1, 2, 2)
tanh1 = tf.tan(h_pool1)
h_conv2 = self.conv2d(tanh1, self.w_conv2, 1) + self.b_conv2
h_pool2 = self.maxpool(h_conv2, 2, 2)
tanh2 = tf.tanh(h_pool2)
logits = self.conv2d(tanh2, self.w_conv3, 1) + self.b_conv3
predictions = tf.nn.softmax(logits, axis=None, name=None)
self.logits.append(logits)
self.predictions.append(predictions)
rgb = tf.strided_slice(current_input, [0, 0, 0, 0], [
0, 0, 0, 3], strides=[1, 4, 4, 1], end_mask=7)
current_input = tf.concat(values=[rgb, predictions], axis=3)
def model_v2(self, inpt):
self.input = tf.Variable(initial_value=inpt, dtype=tf.float32, shape=[
None, None, None, 3 + self.num_classes], trainable=False)
current_input = self.input
self.predictions = []
self.logits = []
for n_layer in range(self.num_layers):
h_conv1 = self.conv2d(
current_input, self.w_conv1, 1) + self.b_conv1
h_pool1 = self.maxpool(h_conv1, 2, 2)
tanh1 = tf.tan(h_pool1)
h_conv2 = self.conv2d(tanh1, self.w_conv2, 1) + self.b_conv2
logits = self.conv2d(h_conv2, self.w_conv3, 1) + self.b_conv3
predictions = tf.nn.softmax(logits, axis=None, name=None)
self.logits.append(logits)
self.predictions.append(predictions)
rgb = tf.strided_slice(current_input, [0, 0, 0, 0], [
0, 0, 0, 3], strides=[1, 2, 2, 1], end_mask=7)
current_input = tf.concat(values=[rgb, predictions], axis=3)
def __call__(self, inpt):
if(self.model_v == 1):
self.model_v1(inpt)
else:
self.model_v2(inpt)