Ejemplo n.º 1
0
    def _conv_layers(self,x):
        conv_layers = Layers(x)
        
        # Convolutional layers
        res_blocks = [1,3,4,23,3]
        output_channels = [64,256,512,1024,2048]
        
        with tf.variable_scope('scale0'):
            conv_layers.conv2d(filter_size=7,output_channels=output_channels[0],stride=2,padding='SAME',b_value=None)
            conv_layers.maxpool(k=3)
        with tf.variable_scope('scale1'):
            conv_layers.res_layer(filter_size=3, output_channels=output_channels[1], stride=2)
            for block in range(res_blocks[1]-1):
                conv_layers.conv_layers.res_layer(filter_size=3, output_channels=output_channels[1], stride=1)
        with tf.variable_scope('scale2'):
            conv_layers.res_layer(filter_size=3, output_channels=output_channels[2], stride=2)
            for block in range(res_blocks[2]-1):
                conv_layers.conv_layers.res_layer(filter_size=3, output_channels=output_channels[2], stride=1)
        with tf.variable_scope('scale3'):
            conv_layers.res_layer(filter_size=3, output_channels=output_channels[3], stride=2)
            for block in range(res_blocks[3]-1):
                conv_layers.conv_layers.res_layer(filter_size=3, output_channels=output_channels[3], stride=1)
        with tf.variable_scope('scale4'):
            conv_layers.res_layer(filter_size=3, output_channels=output_channels[4], stride=2)
            for block in range(res_blocks[4]-1):
                conv_layers.conv_layers.res_layer(filter_size=3, output_channels=output_channels[4], stride=1)
        
        conv_layers.avgpool(globe=True)
        
        # Fully Connected Layer
        conv_layers.fc(output_nodes=10)

        return conv_layers.get_output()
    def _conv_layers(self, x):
        conv_layers = Layers(x)

        # Convolutional layers
        res_blocks = [1, 3, 4, 23, 3]
        output_channels = [64, 256, 512, 1024, 2048]

        with tf.variable_scope('scale0'):
            conv_layers.conv2d(filter_size=7,
                               output_channels=output_channels[0],
                               stride=2,
                               padding='SAME',
                               b_value=None)
            conv_layers.maxpool(k=3)
        with tf.variable_scope('scale1'):
            conv_layers.res_layer(filter_size=3,
                                  output_channels=output_channels[1],
                                  stride=2)
            for block in range(res_blocks[1] - 1):
                conv_layers.conv_layers.res_layer(
                    filter_size=3,
                    output_channels=output_channels[1],
                    stride=1)
        with tf.variable_scope('scale2'):
            conv_layers.res_layer(filter_size=3,
                                  output_channels=output_channels[2],
                                  stride=2)
            for block in range(res_blocks[2] - 1):
                conv_layers.conv_layers.res_layer(
                    filter_size=3,
                    output_channels=output_channels[2],
                    stride=1)
        with tf.variable_scope('scale3'):
            conv_layers.res_layer(filter_size=3,
                                  output_channels=output_channels[3],
                                  stride=2)
            for block in range(res_blocks[3] - 1):
                conv_layers.conv_layers.res_layer(
                    filter_size=3,
                    output_channels=output_channels[3],
                    stride=1)
        with tf.variable_scope('scale4'):
            conv_layers.res_layer(filter_size=3,
                                  output_channels=output_channels[4],
                                  stride=2)
            for block in range(res_blocks[4] - 1):
                conv_layers.conv_layers.res_layer(
                    filter_size=3,
                    output_channels=output_channels[4],
                    stride=1)

        conv_layers.avgpool(globe=True)

        # Fully Connected Layer
        conv_layers.fc(output_nodes=10)

        return conv_layers.get_output()
Ejemplo n.º 3
0
class fast_rcnn:
    '''
    Crop and resize areas from the feature-extracting CNN's feature maps
    according to the ROIs generated from the ROI proposal layer
    '''
    def __init__(self, featureMaps, roi_proposal_net, eval_mode):
        self.featureMaps = featureMaps
        self.roi_proposal_net = roi_proposal_net
        self.rois = roi_proposal_net.get_rois()
        self.im_dims = roi_proposal_net.im_dims
        self.num_classes = cfg.NUM_CLASSES
        self.eval_mode = eval_mode

        self._network()

    def _network(self):
        with tf.variable_scope('fast_rcnn'):
            # No dropout in evaluation mode
            keep_prob = cfg.FRCNN_DROPOUT_KEEP_RATE if self.eval_mode is False else 1.0

            # ROI pooling
            pooledFeatures = roi_pool(self.featureMaps, self.rois,
                                      self.im_dims)

            # Fully Connect layers (with dropout)
            with tf.variable_scope('fc'):
                self.rcnn_fc_layers = Layers(pooledFeatures)
                self.rcnn_fc_layers.flatten()
                for i in range(len(cfg.FRCNN_FC_HIDDEN)):
                    self.rcnn_fc_layers.fc(output_nodes=cfg.FRCNN_FC_HIDDEN[i],
                                           keep_prob=keep_prob)

                hidden = self.rcnn_fc_layers.get_output()

            # Classifier score
            with tf.variable_scope('cls'):
                self.rcnn_cls_layers = Layers(hidden)
                self.rcnn_cls_layers.fc(output_nodes=self.num_classes,
                                        activation_fn=None)

            # Bounding Box refinement
            with tf.variable_scope('bbox'):
                self.rcnn_bbox_layers = Layers(hidden)
                self.rcnn_bbox_layers.fc(output_nodes=self.num_classes * 4,
                                         activation_fn=None)

    # Get functions
    def get_cls_score(self):
        return self.rcnn_cls_layers.get_output()

    def get_cls_prob(self):
        logits = self.get_cls_score()
        return tf.nn.softmax(logits)

    def get_bbox_refinement(self):
        return self.rcnn_bbox_layers.get_output()

    # Loss functions
    def get_fast_rcnn_cls_loss(self):
        assert self.eval_mode is False, 'No Fast RCNN cls loss without ground truth boxes'
        fast_rcnn_cls_score = self.get_cls_score()
        labels = self.roi_proposal_net.get_labels()
        return fast_rcnn_cls_loss(fast_rcnn_cls_score, labels)

    def get_fast_rcnn_bbox_loss(self):
        assert self.eval_mode is False, 'No Fast RCNN bbox loss without ground truth boxes'
        fast_rcnn_bbox_pred = self.get_bbox_refinement()
        bbox_targets = self.roi_proposal_net.get_bbox_targets()
        roi_inside_weights = self.roi_proposal_net.get_bbox_inside_weights()
        roi_outside_weights = self.roi_proposal_net.get_bbox_outside_weights()
        return fast_rcnn_bbox_loss(fast_rcnn_bbox_pred, bbox_targets,
                                   roi_inside_weights, roi_outside_weights)
class fast_rcnn:
    '''
    Crop and resize areas from the feature-extracting CNN's feature maps 
    according to the ROIs generated from the ROI proposal layer
    '''
    def __init__(self,featureMaps, roi_proposal_net):
        self.featureMaps = featureMaps
        self.roi_proposal_net = roi_proposal_net
        self.rois = roi_proposal_net.get_rois()
        self.im_dims = roi_proposal_net.im_dims
        self.flags = roi_proposal_net.flags
        self._network()
            
    def _network(self):
        with tf.variable_scope('fast_rcnn'):
            # ROI pooling
            pooledFeatures = roi_pool(self.featureMaps,self.rois,self.im_dims)
            
            # Fully Connect layers (with dropout)
            with tf.variable_scope('fc'):
                self.rcnn_fc_layers = Layers(pooledFeatures)
                self.rcnn_fc_layers.flatten()
                self.rcnn_fc_layers.fc(output_nodes=4096, keep_prob=0.5)
                self.rcnn_fc_layers.fc(output_nodes=4096, keep_prob=0.5)
                
                hidden = self.rcnn_fc_layers.get_output()
                
            # Classifier score
            with tf.variable_scope('cls'):
                self.rcnn_cls_layers = Layers(hidden)
                self.rcnn_cls_layers.fc(output_nodes=self.flags['num_classes'],activation_fn=None)
    
            # Bounding Box refinement
            with tf.variable_scope('bbox'):
                self.rcnn_bbox_layers = Layers(hidden)
                self.rcnn_bbox_layers.fc(output_nodes=4*self.flags['num_classes'],activation_fn=None)
           
    # Get functions
    def get_cls_score(self):
        return self.rcnn_cls_layers.get_output()
        
    def get_cls_prob(self):
        logits = self.get_cls_score()
        return tf.nn.softmax(logits)
        
    def get_bbox_refinement(self):
        return self.rcnn_bbox_layers.get_output()
        
    # Loss functions
    def get_fast_rcnn_cls_loss(self):
        fast_rcnn_cls_score = self.get_cls_score()
        labels = self.roi_proposal_net.get_labels()
        return fast_rcnn_cls_loss(fast_rcnn_cls_score, labels)
    
    def get_fast_rcnn_bbox_loss(self):
        fast_rcnn_bbox_pred = self.get_bbox_refinement()
        bbox_targets = self.roi_proposal_net.get_bbox_targets()
        roi_inside_weights = self.roi_proposal_net.get_bbox_inside_weights()
        roi_outside_weights = self.roi_proposal_net.get_bbox_outside_weights()
        return fast_rcnn_bbox_loss(fast_rcnn_bbox_pred, bbox_targets, roi_inside_weights, roi_outside_weights)
        
Ejemplo n.º 5
0
class fast_rcnn:
    '''
    Crop and resize areas from the feature-extracting CNN's feature maps
    according to the ROIs generated from the ROI proposal layer
    '''

    def __init__(self, featureMaps, roi_proposal_net, eval_mode):
        self.featureMaps = featureMaps
        self.roi_proposal_net = roi_proposal_net
        self.rois = roi_proposal_net.get_rois()
        self.im_dims = roi_proposal_net.im_dims
        self.num_classes = cfg.NUM_CLASSES
        self.eval_mode = eval_mode
        
        self._network()

    def _network(self):
        with tf.variable_scope('fast_rcnn'):
            # No dropout in evaluation mode
            keep_prob = cfg.FRCNN_DROPOUT_KEEP_RATE if self.eval_mode is False else 1.0

            # ROI pooling
            pooledFeatures = roi_pool(self.featureMaps, self.rois, self.im_dims)

            # Fully Connect layers (with dropout)
            with tf.variable_scope('fc'):
                self.rcnn_fc_layers = Layers(pooledFeatures)
                self.rcnn_fc_layers.flatten()
                for i in range(len(cfg.FRCNN_FC_HIDDEN)):
                    self.rcnn_fc_layers.fc(output_nodes=cfg.FRCNN_FC_HIDDEN[i], keep_prob=keep_prob)

                hidden = self.rcnn_fc_layers.get_output()

            # Classifier score
            with tf.variable_scope('cls'):
                self.rcnn_cls_layers = Layers(hidden)
                self.rcnn_cls_layers.fc(output_nodes=self.num_classes, activation_fn=None)

            # Bounding Box refinement
            with tf.variable_scope('bbox'):
                self.rcnn_bbox_layers = Layers(hidden)
                self.rcnn_bbox_layers.fc(output_nodes=self.num_classes*4, activation_fn=None)

    # Get functions
    def get_cls_score(self):
        return self.rcnn_cls_layers.get_output()

    def get_cls_prob(self):
        logits = self.get_cls_score()
        return tf.nn.softmax(logits)

    def get_bbox_refinement(self):
        return self.rcnn_bbox_layers.get_output()

    # Loss functions
    def get_fast_rcnn_cls_loss(self):
        assert self.eval_mode is False, 'No Fast RCNN cls loss without ground truth boxes'
        fast_rcnn_cls_score = self.get_cls_score()
        labels = self.roi_proposal_net.get_labels()
        return fast_rcnn_cls_loss(fast_rcnn_cls_score, labels)

    def get_fast_rcnn_bbox_loss(self):
        assert self.eval_mode is False, 'No Fast RCNN bbox loss without ground truth boxes'
        fast_rcnn_bbox_pred = self.get_bbox_refinement()
        bbox_targets = self.roi_proposal_net.get_bbox_targets()
        roi_inside_weights = self.roi_proposal_net.get_bbox_inside_weights()
        roi_outside_weights = self.roi_proposal_net.get_bbox_outside_weights()
        return fast_rcnn_bbox_loss(fast_rcnn_bbox_pred, bbox_targets, roi_inside_weights, roi_outside_weights)