MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / Model

Class Model

examples/ResNet/cifar10-resnet.py:33–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31
32
33class Model(ModelDesc):
34
35 def __init__(self, n):
36 super(Model, self).__init__()
37 self.n = n
38
39 def inputs(self):
40 return [tf.TensorSpec([None, 32, 32, 3], tf.float32, 'input'),
41 tf.TensorSpec([None], tf.int32, 'label')]
42
43 def build_graph(self, image, label):
44 image = image / 128.0
45 assert tf.test.is_gpu_available()
46 image = tf.transpose(image, [0, 3, 1, 2])
47
48 def residual(name, l, increase_dim=False, first=False):
49 shape = l.get_shape().as_list()
50 in_channel = shape[1]
51
52 if increase_dim:
53 out_channel = in_channel * 2
54 stride1 = 2
55 else:
56 out_channel = in_channel
57 stride1 = 1
58
59 with tf.variable_scope(name):
60 b1 = l if first else BNReLU(l)
61 c1 = Conv2D('conv1', b1, out_channel, strides=stride1, activation=BNReLU)
62 c2 = Conv2D('conv2', c1, out_channel)
63 if increase_dim:
64 l = AvgPooling('pool', l, 2)
65 l = tf.pad(l, [[0, 0], [in_channel // 2, in_channel // 2], [0, 0], [0, 0]])
66
67 l = c2 + l
68 return l
69
70 with argscope([Conv2D, AvgPooling, BatchNorm, GlobalAvgPooling], data_format='channels_first'), \
71 argscope(Conv2D, use_bias=False, kernel_size=3,
72 kernel_initializer=tf.variance_scaling_initializer(scale=2.0, mode='fan_out')):
73 l = Conv2D('conv0', image, 16, activation=BNReLU)
74 l = residual('res1.0', l, first=True)
75 for k in range(1, self.n):
76 l = residual('res1.{}'.format(k), l)
77 # 32,c=16
78
79 l = residual('res2.0', l, increase_dim=True)
80 for k in range(1, self.n):
81 l = residual('res2.{}'.format(k), l)
82 # 16,c=32
83
84 l = residual('res3.0', l, increase_dim=True)
85 for k in range(1, self.n):
86 l = residual('res3.' + str(k), l)
87 l = BNReLU('bnlast', l)
88 # 8,c=64
89 l = GlobalAvgPooling('gap', l)
90

Callers 1

cifar10-resnet.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…