MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / Model

Class Model

examples/DoReFa-Net/alexnet-dorefa.py:62–137  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60
61
62class Model(ImageNetModel):
63 weight_decay = 5e-6
64 weight_decay_pattern = 'fc.*/W'
65
66 def get_logits(self, image):
67 if BITW == 't':
68 fw, fa, fg = get_dorefa(32, 32, 32)
69 fw = ternarize
70 else:
71 fw, fa, fg = get_dorefa(BITW, BITA, BITG)
72
73 # monkey-patch tf.get_variable to apply fw
74 def new_get_variable(v):
75 name = v.op.name
76 # don't binarize first and last layer
77 if not name.endswith('W') or 'conv0' in name or 'fct' in name:
78 return v
79 else:
80 logger.info("Quantizing weight {}".format(v.op.name))
81 return fw(v)
82
83 def nonlin(x):
84 if BITA == 32:
85 return tf.nn.relu(x) # still use relu for 32bit cases
86 return tf.clip_by_value(x, 0.0, 1.0)
87
88 def activate(x):
89 return fa(nonlin(x))
90
91 with remap_variables(new_get_variable), \
92 argscope([Conv2D, BatchNorm, MaxPooling], data_format='channels_first'), \
93 argscope(BatchNorm, momentum=0.9, epsilon=1e-4), \
94 argscope(Conv2D, use_bias=False):
95 logits = (LinearWrap(image)
96 .Conv2D('conv0', 96, 12, strides=4, padding='VALID', use_bias=True)
97 .apply(activate)
98 .Conv2D('conv1', 256, 5, padding='SAME', split=2)
99 .apply(fg)
100 .BatchNorm('bn1')
101 .MaxPooling('pool1', 3, 2, padding='SAME')
102 .apply(activate)
103
104 .Conv2D('conv2', 384, 3)
105 .apply(fg)
106 .BatchNorm('bn2')
107 .MaxPooling('pool2', 3, 2, padding='SAME')
108 .apply(activate)
109
110 .Conv2D('conv3', 384, 3, split=2)
111 .apply(fg)
112 .BatchNorm('bn3')
113 .apply(activate)
114
115 .Conv2D('conv4', 256, 3, split=2)
116 .apply(fg)
117 .BatchNorm('bn4')
118 .MaxPooling('pool4', 3, 2, padding='VALID')
119 .apply(activate)

Callers 2

get_configFunction · 0.70
alexnet-dorefa.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…