def inference(self, images, training=False): x = self.preprocess(images) vgg_fms = self.base_model(x, training=training) if cfg.MODEL.fpn: fpn_fms = self.fpn(vgg_fms, training=training) fpn_fms[0] = l2_normalization(fpn_fms[0], scale=cfg.MODEL.l2_norm[0]) fpn_fms[1] = l2_normalization(fpn_fms[1], scale=cfg.MODEL.l2_norm[1]) fpn_fms[2] = l2_normalization(fpn_fms[2], scale=cfg.MODEL.l2_norm[2]) if cfg.MODEL.cpm: for i in range(len(fpn_fms)): fpn_fms[i] = self.cpm_ops[i](fpn_fms[i], training=training) fpn_reg, fpn_cls = self.ssd_head_fem(fpn_fms, training=training) ###get anchor ###### adjust the anchors to the image shape, but it trains with a fixed h,w h = tf.shape(images)[1] w = tf.shape(images)[2] anchors_ = get_all_anchors_fpn(max_size=[h, w]) if cfg.MODEL.dual_mode: anchors_ = anchors_[0::2] else: anchors_ = anchors_ res = self.postprocess(fpn_reg, fpn_cls, anchors_) return res
def inference(self, images): x = self.preprocess(images) of1, of2, of3 = self.base_model(x, training=False) fms = [of1, of2, of3] if cfg.MODEL.fpn: fpn_fms = self.fpn(fms, training=False) else: fpn_fms = fms if cfg.MODEL.cpm: for i in range(len(fpn_fms)): fpn_fms[i] = self.cpm_ops[i](fpn_fms[i], training=False) fpn_reg, fpn_cls = self.ssd_head_fem(fpn_fms, training=False) ###get anchor ###### adjust the anchors to the image shape, but it trains with a fixed h,w h = tf.shape(images)[1] w = tf.shape(images)[2] anchors_ = get_all_anchors_fpn(max_size=[h, w]) if cfg.MODEL.dual_mode: anchors_ = anchors_[0::2] else: anchors_ = anchors_ res = self.postprocess(fpn_reg, fpn_cls, anchors_) return res
def forward(self, inputs, boxes, labels, l2_regulation, training_flag, with_loss=True): ###preprocess inputs = self.preprocess(inputs) ### extract feature maps origin_fms = self.ssd_backbone(inputs, l2_regulation, training_flag) print(origin_fms) ### head, regresssion and class #### train as a dsfd , anchor with 1 ratios per pixel , two shot logger.info('train with dsfd ') reg, cls = self.ssd_head(origin_fms, l2_regulation, training_flag, ratios_per_pixel=2) ### calculate loss reg_loss, cls_loss = ssd_loss(reg, cls, boxes, labels, 'focal_loss') ###### adjust the anchors to the image shape, but it trains with a fixed h,w ###adaptive anchor h = tf.shape(inputs)[1] w = tf.shape(inputs)[2] anchors_ = get_all_anchors_fpn(max_size=[h, w]) self.postprocess(reg, cls, anchors_) return reg_loss, cls_loss
def SSD(images,boxes,labels,L2_reg,training=True): images=preprocess(images) if 'MobilenetV1' in cfg.MODEL.net_structure: ssd_backbne=mobilenet_ssd elif 'resnet' in cfg.MODEL.net_structure: ssd_backbne = resnet_ssd elif 'vgg' in cfg.MODEL.net_structure: ssd_backbne = vgg_ssd elif 'efficientnet' in cfg.MODEL.net_structure: ssd_backbne= efficient_ssd else: ssd_backbne=None print('a net structure that not supported') origin_fms,enhanced_fms=ssd_backbne(images, L2_reg, training) print('origin_fms', origin_fms) print('enhanced_fms', enhanced_fms) with tf.variable_scope('ssd'): if not cfg.MODEL.fpn and not cfg.MODEL.dual_mode: logger.info('the model was trained as a plain ssd') reg_final, cla_final=ssd_out(origin_fms, L2_reg, training) reg_loss, cla_loss = ssd_loss(reg_final, cla_final, boxes, labels, 'ohem') elif cfg.MODEL.fpn and not cfg.MODEL.dual_mode: logger.info('the model was trained without dual shot') reg_final, cla_final = ssd_out(enhanced_fms, L2_reg, training) reg_loss, cla_loss = ssd_loss(reg_final, cla_final, boxes, labels, 'ohem') elif cfg.MODEL.dual_mode: logger.info('the model was trained with dual shot, FEM') reg, cla= ssd_out(origin_fms, L2_reg, training,1) boxes_small=boxes[:,1::2] label_small=labels[:,1::2] reg_loss, cla_loss = ssd_loss(reg, cla, boxes_small, label_small, 'ohem') with tf.variable_scope('dual'): reg_final, cla_final = ssd_out(enhanced_fms, L2_reg, training,1) boxes_norm = boxes[:, 0::2] label_norm = labels[:, 0::2] reg_loss_dual, cla_loss_dual = ssd_loss(reg_final, cla_final, boxes_norm, label_norm,'ohem') reg_loss=(reg_loss+reg_loss_dual) cla_loss=(cla_loss+cla_loss_dual) ###### make it easy to adjust the anchors, but it trains with a fixed h,w h = tf.shape(images)[1] w = tf.shape(images)[2] anchors_=get_all_anchors_fpn(max_size=[h,w]) if cfg.MODEL.dual_mode: anchors_ = anchors_[0::2] else: anchors_ = anchors_ get_predictions(reg_final,cla_final,anchors_) return reg_loss,cla_loss
def forward(self, inputs, boxes, labels, l2_regulation, training_flag, with_loss=True): ###preprocess inputs = self.preprocess(inputs) ### extract feature maps origin_fms, enhanced_fms = self.ssd_backbone(inputs, l2_regulation, training_flag) ### head, regresssion and class if cfg.MODEL.dual_mode and cfg.MODEL.fpn: #### train as a dsfd , anchor with 1 ratios per pixel , two shot logger.info('train with dsfd ') ###first shot origin_reg, origin_cls = self.ssd_head(origin_fms, l2_regulation, training_flag, ratios_per_pixel=1) ###second shot with tf.variable_scope('dual'): final_reg, final_cls = self.ssd_head(enhanced_fms, l2_regulation, training_flag, ratios_per_pixel=1) ### calculate loss if with_loss: ## first shot anchors boxes_small = boxes[:, 1::2] label_small = labels[:, 1::2] ## first shot loss reg_loss, cls_loss = ssd_loss(origin_reg, origin_cls, boxes_small, label_small, 'ohem') ## second shot anchors boxes_norm = boxes[:, 0::2] label_norm = labels[:, 0::2] ## second shot loss with tf.name_scope('dual'): final_reg_loss, final_cls_loss_dual = ssd_loss( final_reg, final_cls, boxes_norm, label_norm, 'ohem') reg_loss = (reg_loss + final_reg_loss) cls_loss = (cls_loss + final_cls_loss_dual) elif cfg.MODEL.fpn: #### train as a plain ssd with fpn , anchor with 2 ratios per pixel logger.info('train with a ssd with fpn ') with tf.variable_scope('dual'): final_reg, final_cls = self.ssd_head(enhanced_fms, l2_regulation, training_flag) ### calculate loss if with_loss: reg_loss, cls_loss = ssd_loss(final_reg, final_cls, boxes, labels, 'ohem') else: #### train as a plain ssd , anchor with 2 ratios per pixel logger.info('train with a plain ssd') final_reg, final_cls = self.ssd_head(origin_fms, l2_regulation, training_flag) ### calculate loss if with_loss: reg_loss, cls_loss = ssd_loss(final_reg, final_cls, boxes, labels, 'ohem') ###### adjust the anchors to the image shape, but it trains with a fixed h,w h = tf.shape(inputs)[1] w = tf.shape(inputs)[2] anchors_ = get_all_anchors_fpn(max_size=[h, w]) if cfg.MODEL.dual_mode: anchors_ = anchors_[0::2] else: anchors_ = anchors_ self.postprocess(final_reg, final_cls, anchors_) return reg_loss, cls_loss
def get_pre_define_anchors(self, h, w): anchors_ = get_all_anchors_fpn(max_size=[h, w]) anchors_ = anchors_ return anchors_