Tensorflow Faster RCNN Source Resolution (TFFRCNN) train.py

This blog is on github CharlesShang/TFFRCNN Edition Source Parsing Series Code Notes

--------------------------------------------------------------------------------------------------------------------------------------------

Wu Jiang, the author of this paper

------Click here to link to the original text of Blog Garden------

 

_ DEBUG defaults to False

1.SolverWrapper class

class SolverWrapper(object):
    # caffe in solver The model is optimized by coordinating forward reasoning and backward gradient propagation of the network, and the optimal algorithm for solving network loss is improved by updating the weight parameters.
    # and solver Learning tasks are divided into supervisory optimization and parameter updating, loss generation and gradient calculation.
    # It defines how the entire model works, either on the command line or on the command line. pycaffe It is necessary to train or test the network by means of interface. solver Configuration file
    """A simple wrapper around Caffe's solver.
    This wrapper gives us control over the snapshotting process, which we
    use to unnormalize the learned bounding-box regression weights.
    """

The following functions are defined in the class:

------------------------------------------------------__init__(...)---------------------------------------------------------

Def__init_ (self, sess, network, imdb, output_dir, logdir, pretrained_model=None) constructor

    def __init__(self, sess, network, imdb, roidb, output_dir, logdir, pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.net = network
        self.imdb = imdb
        self.roidb = roidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model

        print 'Computing bounding-box regression targets...'
        if cfg.TRAIN.BBOX_REG:  # default cfg.TRAIN.BBOX_REG=True
            # Not yet known?
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'

        # For checkpoint
        # Model Preservation and Restoration
        self.saver = tf.train.Saver(max_to_keep=100,write_version=saver_pb2.SaverDef.V1)
        # Specify a file save graph where, tf.get_default_graph()The graph to be recorded in the event file, that is tensorflow Default figure
        self.writer = tf.summary.FileWriter(logdir=logdir,
                                             graph=tf.get_default_graph(),
                                             flush_secs=5)   

Among them, call add_bbox_regression_targets(roidb) to return bbox regression targets (mean bbox_means and standard deviation bbox_stds) (in roi_data_layer/roidb.py) to participate in the calculation normalization of bbox_pred layer weights and bias?

def add_bbox_regression_targets(roidb):
    """
    Add information needed to train bounding-box regressors.
    For each roi find the corresponding gt box, and compute the distance.
    then normalize the distance into Gaussian by minus mean and divided by std
    """

tf.train.Saver(...) is related to model preservation and recovery, tf.summary.FileWriter(...) is related to saving computational graphs.

------------------------------------------------------snapshot(...)---------------------------------------------------------

def snapshot(self,sess,iter)

    def snapshot(self, sess, iter):
        """Take a snapshot of the network after unnormalizing the learned   Non-standard
        bounding-box regression weights. This enables easy use at test-time.
        """
        net = self.net

        if cfg.TRAIN.BBOX_REG and net.layers.has_key('bbox_pred') and cfg.TRAIN.BBOX_NORMALIZE_TARGETS:
            # net.layers Dictionary Records Output of Network Layers
            # default cfg.TRAIN.BBOX_REG=True  cfg.TRAIN.BBOX_NORMALIZE_TARGETS=True Meaning?
            # save original values,bbox_pred by VGGnet The last floor
            with tf.variable_scope('bbox_pred', reuse=True):
                weights = tf.get_variable("weights")
                biases = tf.get_variable("biases")

            # Not yet known eval()Function?
            orig_0 = weights.eval()
            orig_1 = biases.eval()

            # scale and shift with bbox reg unnormalization; then save snapshot
            weights_shape = weights.get_shape().as_list()
            # np.tile(array,reps) Press reps Specified axis for repetition array Construct new arrays
            # What is the meaning? Why is it restored later? weights and biases The original value?
            sess.run(weights.assign(orig_0 * np.tile(self.bbox_stds, (weights_shape[0],1))))
            sess.run(biases.assign(orig_1 * self.bbox_stds + self.bbox_means))

        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)

        # TRAIN.SNAPSHOT_INFIX = ''
        infix = ('_' + cfg.TRAIN.SNAPSHOT_INFIX
                 if cfg.TRAIN.SNAPSHOT_INFIX != '' else '')
        # TRAIN.SNAPSHOT_PREFIX = 'VGGnet_fast_rcnn'
        filename = (cfg.TRAIN.SNAPSHOT_PREFIX + infix +
                    '_iter_{:d}'.format(iter+1) + '.ckpt')
        filename = os.path.join(self.output_dir, filename)  #as E:\TFFRCNN\output\faster_rcnn_voc_vgg\voc_2007_trainval\VGGnet_fast_rcnn_iter_100.ckpt
        # storage.ckpt Model file
        self.saver.save(sess, filename)
        print 'Wrote snapshot to: {:s}'.format(filename)

        # Why is it restored here? bbox_pred Primitive weights and biases?
        if cfg.TRAIN.BBOX_REG and net.layers.has_key('bbox_pred'):
            # restore net to original state
            sess.run(weights.assign(orig_0))
            sess.run(biases.assign(orig_1))

It is related to the storage. ckpt training model. It involves normalizing the weights and biases values of bbox_pred layer and restoring the original values. It is not clear that the functions cfg.TRAIN.BBOX_NORMALIZE_TARGETS and eval() have unclear meanings and are called by train_model(...).

------------------------------------------------------build_image_summary(...)---------------------------------------------------------

def build_image_summary(self)

    def build_image_summary(self):
        """
        A simple graph for write image summary
        """
        log_image_data = tf.placeholder(tf.uint8, [None, None, 3])
        log_image_name = tf.placeholder(tf.string)
        # import tensorflow.python.ops.gen_logging_ops as logging_ops                
        from tensorflow.python.ops import gen_logging_ops
        from tensorflow.python.framework import ops as _ops
        log_image = gen_logging_ops._image_summary(log_image_name, tf.expand_dims(log_image_data, 0), max_images=1)
        _ops.add_to_collection(_ops.GraphKeys.SUMMARIES, log_image)
        # log_image = tf.summary.image(log_image_name, tf.expand_dims(log_image_data, 0), max_outputs=1)
        return log_image, log_image_data, log_image_name     

Generate summary log files related to images, image data and image names. Tenorflow mechanism is not clear, such as gen_logging_ops._image_summary(...) and _ops.add_to_collection(...).

Keywords: PHP network snapshot Python github

Added by DssTrainer on Wed, 31 Jul 2019 09:54:44 +0300