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

Method build_graph

examples/DoReFa-Net/resnet-dorefa.py:39–110  ·  view source on GitHub ↗
(self, image, label)

Source from the content-addressed store, hash-verified

37 tf.TensorSpec([None], tf.int32, 'label')]
38
39 def build_graph(self, image, label):
40 image = image / 256.0
41
42 fw, fa, fg = get_dorefa(BITW, BITA, BITG)
43
44 def new_get_variable(v):
45 name = v.op.name
46 # don't binarize first and last layer
47 if not name.endswith('W') or 'conv1' in name or 'fct' in name:
48 return v
49 else:
50 logger.info("Binarizing weight {}".format(v.op.name))
51 return fw(v)
52
53 def nonlin(x):
54 return tf.clip_by_value(x, 0.0, 1.0)
55
56 def activate(x):
57 return fa(nonlin(x))
58
59 def resblock(x, channel, stride):
60 def get_stem_full(x):
61 return (LinearWrap(x)
62 .Conv2D('c3x3a', channel, 3)
63 .BatchNorm('stembn')
64 .apply(activate)
65 .Conv2D('c3x3b', channel, 3)())
66 channel_mismatch = channel != x.get_shape().as_list()[3]
67 if stride != 1 or channel_mismatch or 'pool1' in x.name:
68 # handling pool1 is to work around an architecture bug in our model
69 if stride != 1 or 'pool1' in x.name:
70 x = AvgPooling('pool', x, stride, stride)
71 x = BatchNorm('bn', x)
72 x = activate(x)
73 shortcut = Conv2D('shortcut', x, channel, 1)
74 stem = get_stem_full(x)
75 else:
76 shortcut = x
77 x = BatchNorm('bn', x)
78 x = activate(x)
79 stem = get_stem_full(x)
80 return shortcut + stem
81
82 def group(x, name, channel, nr_block, stride):
83 with tf.variable_scope(name + 'blk1'):
84 x = resblock(x, channel, stride)
85 for i in range(2, nr_block + 1):
86 with tf.variable_scope(name + 'blk{}'.format(i)):
87 x = resblock(x, channel, 1)
88 return x
89
90 with remap_variables(new_get_variable), \
91 argscope(BatchNorm, decay=0.9, epsilon=1e-4), \
92 argscope(Conv2D, use_bias=False, nl=tf.identity):
93 logits = (LinearWrap(image)
94 # use explicit padding here, because our private training framework has
95 # different padding mechanisms from TensorFlow
96 .tf.pad([[0, 0], [3, 2], [3, 2], [0, 0]])

Callers

nothing calls this directly

Calls 6

get_dorefaFunction · 0.90
remap_variablesFunction · 0.90
argscopeFunction · 0.85
LinearWrapClass · 0.85
applyMethod · 0.45

Tested by

no test coverage detected