code: def build_graph(self, image, label):
      fw, fa, fg = get_dorefa(BITW, BITA, BITG)
  
      # monkey-patch tf.get_variable to apply fw
      def binarize_weight(v):
          name = v.op.name
          # don't binarize first and last layer
          if not name.endswith("W") or "conv0" in name or "fc" in name:
              return v
          else:
              logger.info("Binarizing weight {}".format(v.op.name))
              return fw(v)
  
      def nonlin(x):
          if BITA == 32:
              return tf.nn.relu(x)
          return tf.clip_by_value(x, 0.0, 1.0)
  
      def activate(x):
          return fa(nonlin(x))
  
      image = image / 256.0
  
      with remap_variables(binarize_weight), argscope(
          BatchNorm, momentum=0.9, epsilon=1e-4
      ), argscope(Conv2D, use_bias=False):
          logits = (
              LinearWrap(image)
              .Conv2D("conv0", 48, 5, padding="VALID", use_bias=True)
              .MaxPooling("pool0", 2, padding="SAME")
              .apply(activate)
              # 18
              .Conv2D("conv1", 64, 3, padding="SAME")
              .apply(fg)
              .BatchNorm("bn1")
              .apply(activate)
              .Conv2D("conv2", 64, 3, padding="SAME")
              .apply(fg)
              .BatchNorm("bn2")
              .MaxPooling("pool1", 2, padding="SAME")
              .apply(activate)
              # 9
              .Conv2D("conv3", 128, 3, padding="VALID")
              .apply(fg)
              .BatchNorm("bn3")
              .apply(activate)
              # 7
              .Conv2D("conv4", 128, 3, padding="SAME")
              .apply(fg)
              .BatchNorm("bn4")
              .apply(activate)
              .Conv2D("conv5", 128, 3, padding="VALID")
              .apply(fg)
              .BatchNorm("bn5")
              .apply(activate)
              # 5
              .Dropout(rate=0.5 if self.training else 0.0)
              .Conv2D("conv6", 512, 5, padding="VALID")
              .apply(fg)
              .BatchNorm("bn6")
              .apply(nonlin)
              .FullyConnected("fc1", 10)()
          )
      tf.nn.softmax(logits, name="output")
  
      # compute the number of failed samples
      wrong = tf.cast(
          tf.logical_not(tf.nn.in_top_k(logits, label, 1)),
          tf.float32,
          name="wrong_tensor",
      )
      # monitor training error
      add_moving_summary(tf.reduce_mean(wrong, name="train_error"))
  
      cost = tf.nn.sparse_softmax_cross_entropy_with_logits(
          logits=logits, labels=label
      )
      cost = tf.reduce_mean(cost, name="cross_entropy_loss")
      # weight decay on all W of fc layers
      wd_cost = regularize_cost("fc.*/W", l2_regularizer(1e-7))
  
      add_param_summary((".*/W", ["histogram", "rms"]))
      total_cost = tf.add_n([cost, wd_cost], name="cost")
      add_moving_summary(cost, wd_cost, total_cost)
      return total_cost
  
file_path: tensorpack/examples/DoReFa-Net/svhn-digit-dorefa.py
imports: import argparse
  import os
  import tensorflow
  from tensorpack import *
  from tensorpack.dataflow import dataset
  from tensorpack.tfutils.summary import add_moving_summary, add_param_summary
  from tensorpack.tfutils.varreplace import remap_variables
  from dorefa import get_dorefa
label_desc: Use image node_id 2 for calling the Model obj's underlying member method code with example usage: obj.build_graph(image, label) and returns: total_cost, tf, fa, v, fw, tf