tensorflow_addons.losses.GIoULoss Example - Program Talk

文章推薦指數: 80 %
投票人數:10人

giou_loss = tfa.losses.GIoULoss(reduction=tf.keras.losses.Reduction.NONE) self._add_loss(name, weight, lambda a, b: giou_loss(a, b) * 0.1 + tf.keras.losses. SkiptocontentHerearetheexamplesofthepythonapitensorflow_addons.losses.GIoULosstakenfromopensourceprojects.Byvotingupyoucanindicatewhichexamplesaremostusefulandappropriate.5Examples 7 3 ViewSourceFile:giou_loss_test.pyLicense:ApacheLicense2.0ProjectCreator:tensorflowdeftest_config(): gl_obj=GIoULoss(reduction=tf.keras.losses.Reduction.NONE,name="giou_loss") assertgl_obj.name=="giou_loss" assertgl_obj.reduction==tf.keras.losses.Reduction.NONE @pytest.mark.parametrize("dtype",[np.float16,np.float32,np.float64]) 3 ViewSourceFile:giou_loss_test.pyLicense:ApacheLicense2.0ProjectCreator:tensorflowdeftest_keras_model(dtype): boxes1=tf.constant([[4.0,3.0,7.0,5.0],[5.0,6.0,10.0,7.0]],dtype=dtype) boxes2=tf.constant([[3.0,4.0,6.0,8.0],[14.0,14.0,15.0,15.0]],dtype=dtype) expected_result=tf.constant(1.5041667222976685,dtype=dtype) model=tf.keras.Sequential() model.compile( optimizer="adam", loss=GIoULoss(reduction=tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE), ) loss=model.evaluate(boxes1,boxes2,batch_size=2,steps=1) test_utils.assert_allclose_according_to_type(loss,expected_result) @pytest.mark.usefixtures("maybe_run_functions_eagerly") 0 ViewSourceFile:losses.pyLicense:MITLicenseProjectCreator:leosampaiodefadd_giou_loss(self,name,weight=1.): giou_loss=tfa.losses.GIoULoss(reduction=tf.keras.losses.Reduction.NONE) self._add_loss(name,weight,lambdaa,b:giou_loss(a,b)*0.1+tf.keras.losses.MSE(a,b)) defadd_lsgan_fake_loss(self,name,weight=1., 0 ViewSourceFile:losses.pyLicense:MITLicenseProjectCreator:SchernHedefbbox_loss( target, output, indices, bbox_cost_factor=tf.constant(5.0,dtype=tf.float32), iou_cost_factor=tf.constant(2.0,dtype=tf.float32), ): """Calculateboundingboxloss. Parameters ---------- target:tf.Tensor Sampletargetboundingboxesofshape[#Queries,4]. output:tf.Tensor Sampleoutputboundingboxespredictionsofshape[#Queries,4]. indices:tf.Tensor Bipartitematchingindicesforthesampleofshape[2,max_obj]. Indicatingtheassignementbetweenqueriesandobjects.Notethat`max_obj`isspecified in`tf_linear_sum_assignment`andthetensorispaddedwith`-1`. bbox_cost_factor:tf.Tensor,optional CostfactorforL1-loss. iou_cost_factor:tf.Tensor,optional CostfactorforgeneralizedIoUloss. Returns ------- tf.Tensor Sampleboundingboxloss. """ #Retrievequeryandobjectidx query_idx,object_idx=filter_sample_indices(indices) #SelectOrderedTarget/Outputaccordingtothehungarianmatching ordered_target=tf.gather(target,object_idx) ordered_output=tf.gather(output,query_idx) #CalculateL1Loss l1_loss=tf.reduce_sum(tf.math.abs(ordered_target-ordered_output)) #CalculateGIoULoss giou_loss=tfa.losses.GIoULoss(reduction=tf.keras.losses.Reduction.NONE) giou_loss=tf.reduce_sum( giou_loss( y_true=box_cxcywh_to_xyxy(ordered_target), y_pred=box_cxcywh_to_xyxy(ordered_output), ) ) returnbbox_cost_factor*l1_loss+iou_cost_factor*giou_loss 0 ViewSourceFile:matcher.pyLicense:MITLicenseProjectCreator:SchernHedefprepare_cost_matrix(detr_scores,detr_bbox,batch_cls,batch_bbox): """Calculatethecostmatrixofthegivenmodeloutputs,requiredforthebipartite assignmentbetweenqueriesandobjects. Parameters ---------- detr_scores:tf.Tensor Batchdetrscoreoutputsofshape[BatchSize.#Queries,#Objects+1]. detr_bbox:tf.Tensor Batchdetrboundingboxoutputsofshape[BatchSize.#Queries,4]. batch_cls:tf.Tensor Batchclasstargetsofshape[BatchSize,#Queries,1]. batch_bbox:tf.Tensor Batchboundingboxtargetsofshape[BatchSize,#Queries,4]. Returns ------- cost_matrix:tf.Tensor Costmatrixofthegivenscoresintheform[BatchSize,#Queries,#BatchObjects]. Notethatthenumberofobjectsreferstotheobjectsinsidethebatch.Forthebipartiteassignment, thesegetslicedtomatchonlytheconsideredsample. """ config=DefaultDETRConfig() batch_size=tf.shape(detr_scores)[0] num_queries=config.num_queries num_classes=config.num_classes+1 #PrepareOutputs #[BS*#Queries,#Cls] #[BS*#Queries,#Coord] out_prob=tf.nn.softmax( tf.reshape(detr_scores,shape=(batch_size*num_queries,num_classes)),-1 ) out_bbox=tf.reshape(detr_bbox,shape=(batch_size*num_queries,4)) #ClsCosts #ObjektKlasseninBatch #[Class_Obj1,Class_Obj2,...] obj_classes=tf.gather_nd(batch_cls,tf.where(tf.not_equal(batch_cls,4))) obj_classes=tf.cast(obj_classes,dtype=tf.int64) num_objects=tf.size(obj_classes) one=tf.constant(1,dtype=tf.int32) #Computetheclassificationcost.Contrarytotheloss,wedon'tusetheNLL, #butapproximateitin1-proba[targetclass]. #The1isaconstantthatdoesn'tchangethematching,itcanbeommitted. cost_class=-tf.gather(out_prob,obj_classes,axis=1) cost_class=tf.reshape(cost_class,shape=(batch_size*num_queries,num_objects)) #BBOXCosts #ObjektBoundingBoxesinBatch #[#Obj,#Coord] obj_bboxes_xywh=tf.gather_nd(batch_bbox,tf.where(tf.not_equal(batch_bbox,4))) obj_bboxes_xywh=tf.reshape(obj_bboxes_xywh,shape=[num_objects,4]) obj_bboxes_xyxy=box_cxcywh_to_xyxy(obj_bboxes_xywh) #PrepareOutBBOX #[BS*#Queries,#Obj,#Coord] #1.InCentroidShapeforL1Loss out_bbox_xywh=tf.expand_dims(out_bbox,axis=1) out_bbox_xywh=tf.tile(out_bbox_xywh,[one,num_objects,one]) #2.InPositionalShapeforIoULoss out_bbox_xyxy=box_cxcywh_to_xyxy(out_bbox) out_bbox_xyxy=tf.tile( tf.expand_dims(out_bbox_xyxy,axis=1),[one,num_objects,one] ) #CalculateBoundingBoxMatchingCosts #L1Loss cost_bbox=tf.math.reduce_sum(tf.math.abs(out_bbox_xywh-obj_bboxes_xywh),2) #cost_bbox=tf.reshape(cost_bbox,shape=[batch_size*num_queries,num_objects]) #GIoULoss giou_loss=tfa.losses.GIoULoss(reduction=tf.keras.losses.Reduction.NONE) cost_giou=giou_loss(y_true=obj_bboxes_xyxy,y_pred=out_bbox_xyxy) cost_giou=tf.reshape(cost_giou,shape=[batch_size*num_queries,num_objects]) #CombinetoCostMatrix cost_matrix=cost_class+cost_bbox+cost_giou cost_matrix=tf.reshape(cost_matrix,shape=[batch_size,num_queries,num_objects]) returncost_matrix @tf.function reportthisadRelatedAPIstensorflow_addons.losses.TripletSemiHardLosstensorflow_addons.losses.SparsemaxLosstensorflow_addons.losses.SigmoidFocalCrossEntropytensorflow_addons.losses.GIoULosstensorflow_addons.losses.ContrastiveLosstensorflow_addons.losses.GIoULossmembersreportthisadMoreExamplessysdjangoRequestsScrapySQLAlchemyTwistedNumPymockreportthisadreportthisadx



請為這篇文章評分?