Skip to content

Commit 5d57edf

Browse files
committed
added layer backbones
1 parent aaf772e commit 5d57edf

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

test/layer_backbones.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import tensorgraph as tg
2+
from tensorgraph.layers.backbones import *
3+
from tensorgraph.layers import Softmax, Flatten, Linear, MaxPooling
4+
import tensorflow as tf
5+
import os
6+
from tensorgraph.trainobject import train as mytrain
7+
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
8+
9+
10+
X_train = np.random.rand(100, 96, 96, 3)
11+
y_train = np.random.rand(100, 10)
12+
_, h, w, c = X_train.shape
13+
_, nclass = y_train.shape
14+
X_ph = tf.placeholder('float32', [None, h, w, c])
15+
y_ph = tf.placeholder('float32', [None, nclass])
16+
17+
18+
def train(seq):
19+
y_train_sb = seq.train_fprop(X_ph)
20+
y_test_sb = seq.test_fprop(X_ph)
21+
train_cost_sb = tg.cost.entropy(y_ph, y_train_sb)
22+
optimizer = tf.train.AdamOptimizer(0.0001)
23+
test_accu_sb = tg.cost.accuracy(y_ph, y_test_sb)
24+
with tf.Session() as sess:
25+
mytrain(session=sess,
26+
feed_dict={X_ph:X_train, y_ph:y_train},
27+
train_cost_sb=train_cost_sb,
28+
valid_cost_sb=-test_accu_sb,
29+
optimizer=optimizer,
30+
epoch_look_back=5, max_epoch=1,
31+
percent_decrease=0, train_valid_ratio=[5,1],
32+
batchsize=64, randomize_split=False)
33+
34+
35+
def test_VGG16():
36+
seq = tg.Sequential()
37+
vgg = VGG16(input_channels=c, input_shape=(h, w))
38+
print('output channels:', vgg.output_channels)
39+
print('output shape:', vgg.output_shape)
40+
out_dim = np.prod(vgg.output_shape) * vgg.output_channels
41+
seq.add(vgg)
42+
seq.add(Flatten())
43+
seq.add(Linear(int(out_dim), nclass))
44+
seq.add(Softmax())
45+
train(seq)
46+
47+
48+
def test_VGG19():
49+
seq = tg.Sequential()
50+
vgg = VGG19(input_channels=c, input_shape=(h, w))
51+
print('output channels:', vgg.output_channels)
52+
print('output shape:', vgg.output_shape)
53+
out_dim = np.prod(vgg.output_shape) * vgg.output_channels
54+
seq.add(vgg)
55+
seq.add(Flatten())
56+
seq.add(Linear(int(out_dim), nclass))
57+
seq.add(Softmax())
58+
train(seq)
59+
60+
61+
def test_ResNetSmall():
62+
seq = tg.Sequential()
63+
model = ResNetSmall(input_channels=c, input_shape=(h, w), config=[1,1])
64+
model = ResNetBase(input_channels=c, input_shape=(h, w), config=[1,1,1,1])
65+
print('output channels:', model.output_channels)
66+
print('output shape:', model.output_shape)
67+
seq.add(model)
68+
seq.add(MaxPooling(poolsize=tuple(model.output_shape), stride=(1,1), padding='VALID'))
69+
outshape = valid_nd(model.output_shape, kernel_size=model.output_shape, stride=(1,1))
70+
print(outshape)
71+
out_dim = model.output_channels
72+
seq.add(Flatten())
73+
seq.add(Linear(int(out_dim), nclass))
74+
seq.add(Softmax())
75+
train(seq)
76+
77+
78+
def test_ResNetBase():
79+
seq = tg.Sequential()
80+
model = ResNetBase(input_channels=c, input_shape=(h, w), config=[1,1,1,1])
81+
print('output channels:', model.output_channels)
82+
print('output shape:', model.output_shape)
83+
seq.add(model)
84+
seq.add(MaxPooling(poolsize=tuple(model.output_shape), stride=(1,1), padding='VALID'))
85+
86+
outshape = valid_nd(model.output_shape, kernel_size=model.output_shape, stride=(1,1))
87+
print(outshape)
88+
out_dim = model.output_channels
89+
seq.add(Flatten())
90+
seq.add(Linear(int(out_dim), nclass))
91+
seq.add(Softmax())
92+
train(seq)
93+
94+
95+
def test_DenseNet():
96+
seq = tg.Sequential()
97+
model = DenseNet(input_channels=c, input_shape=(h, w), ndense=1, growth_rate=1, nlayer1blk=1)
98+
print('output channels:', model.output_channels)
99+
print('output shape:', model.output_shape)
100+
out_dim = np.prod(model.output_shape) * model.output_channels
101+
seq.add(model)
102+
seq.add(MaxPooling(poolsize=tuple(model.output_shape), stride=(1,1), padding='VALID'))
103+
seq.add(Flatten())
104+
seq.add(Linear(model.output_channels, nclass))
105+
seq.add(Softmax())
106+
train(seq)
107+
108+
109+
def test_UNet():
110+
seq = tg.Sequential()
111+
model = UNet(input_channels=c, input_shape=(h, w))
112+
print('output channels:', model.output_channels)
113+
print('output shape:', model.output_shape)
114+
out_dim = np.prod(model.output_shape) * model.output_channels
115+
seq.add(model)
116+
seq.add(MaxPooling(poolsize=tuple(model.output_shape), stride=(1,1), padding='VALID'))
117+
seq.add(Flatten())
118+
seq.add(Linear(model.output_channels, nclass))
119+
seq.add(Softmax())
120+
train(seq)
121+
122+
123+
if __name__ == '__main__':
124+
print('runtime test')
125+
test_VGG16()
126+
print('..VGG16 running test done')
127+
test_VGG19()
128+
print('..VGG19 running test done')
129+
test_ResNetSmall()
130+
print('..ResNetSmall running test done')
131+
test_ResNetBase()
132+
print('..ResNetBase running test done')
133+
test_DenseNet()
134+
print('..DenseNet running test done')
135+
test_UNet()
136+
print('..UNet running test done')

0 commit comments

Comments
 (0)