| 32 | |
| 33 | |
| 34 | class Model(ModelDesc): |
| 35 | def inputs(self): |
| 36 | return [tf.TensorSpec([None, 224, 224, 3], tf.float32, 'input'), |
| 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), \ |
no outgoing calls
no test coverage detected
searching dependent graphs…