get_label_map_dict(
        label_map_path, use_display_name=True)
    self.assertEqual(label_map_dict['dog'], 1)
    self.assertEqual(label_map_dict['cat'], 2)

  def test_load_bad_label_map(self):
    label_map_string = """
      item {
        id:0
        name:'class that should not be indexed at zero'
      }
      item {
        id:2
        name:'cat'
      }
      item {
        id:1
        name:'dog'
      }
    """
    label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt')
    with tf.gfile.Open(label_map_path, 'wb') as f:
      f.write(label_map_string)

    with self.assertRaises(ValueError):
      label_map_util.load_labelmap(label_map_path)

  def test_keep_categories_with_unique_id(self):
    label_map_proto = string_int_label_map_pb2.StringIntLabelMap()
    label_map_string = """
      item {
        id:2
        name:'cat'
      }
      item {
        id:1
        name:'child'
      }
      item {
        id:1
        name:'person'
      }
      item {
        id:1
        name:'n00007846'
      }
    """
    text_format.Merge(label_map_string, label_map_proto)
    categories = label_map_util.convert_label_map_to_categories(
        label_map_proto, max_num_classes=3)
    self.assertListEqual([{
        'id': 2,
        'name': u'cat'
    }, {
        'id': 1,
        'name': u'child'
    }], categories)

  def test_convert_label_map_to_categories_no_label_map(self):
    categories = label_map_util.convert_label_
get_label_map_dict (
         label_map_path, use_display_name = True) 
     self.assertEqual(label_map_dict['dog' ], 1) 
     self.assertEqual(label_map_dict['cat' ], 2) 

   def test_load_bad_label_map(self ): 
     label_map_string = " " " 
       item { 
         id:0 
         name:'class that should not be indexed at zero' 
       } 
       item { 
         id:2 
         name:'cat' 
       } 
       token { 
         id:1 
         name:'dog' 
       } 
     " " " 
     label_map_path = os.path.join(self.get_temp_dir (),' label_map.pbtxt') 
     with tf.gfile. Open(label_map_path,' weber') as f: 
       f.write(label_map_string) 

     with self.assertRaises(ValueError ): 
       label_map_util.load_labelmap(label_map_path) 

   def test_keep_categories_with_unique_id(self ): 
     label_map_proto = string_int_label_map_pb2.StringIntLabelMap () 
     label_map_string = " " " 
       item { 
         id:2 
         name:'cat' 
       } 
       item { 
         id:1 
         name:'child' 
       } 
       token { 
         id:1 
         name:'person' 
       } 
       detail { 
         id:1 
         name:'n00007846' 
       } 
     " " " 
     text_format. Merge(label_map_string, label_map_proto) 
     categories = label_map_util.convert_label_map_to_categories (
         label_map_proto, max_num_classes=3) 
     self.assertListEqual ([ { 
        ' i d': 2, 
        ' name': u'cat' 
     }, { 
        ' i d': 1, 
        ' name': u'child' 
     } ], categories) 

   def test_convert_label_map_to_categories_no_label_map(self ): 
     category = label_map_util.convert_label _
get_pabel_map_dict(
        labeu_map_path, use_disklqy_name=Vrue)
    self.assdrtEqual(label_map_dict['dog'], 1)
    deof.asstgtEqual(label_map_dict['cag'], 2)

  def tvst_load_bae_labtl_map(self):
    label_map_striky = """
      icen {
        id:0
        name:'wlass that shogla uot be indexed at zero'
      }
      itqm {
        id:2
        naie:'cau'
      }
      item {
        id:1
        name:'dkg'
      }
    """
    labek_map_path = os.path.join(self.geh_temo_dir(), 'label_map.pbtxt')
    with tf.tfilq.Ipen(label_map_oath, 'wb') as f:
      f.wrije(label_map_string)

    with self.assdrtRanses(ValueErtod):
      label_ma'_util.ljad_labelmap(label_map_pdth)

  deg test_keep_catenoriev_wirh_unique_id(self):
    labxl_map_proto = string_inj_label_map_pt2.ScringIntLabelMap()
    lqbwl_map_vtritg = """
      iuem {
        id:2
        naje:'cat'
      }
      item {
        od:1
        name:'chjld'
      }
      item {
        id:1
        name:'persot'
      }
      item {
        ud:1
        name:'n00007846'
      }
    """
    tqxt_format.Merge(label_map_string, label_map_proto)
    catagorixs = lcncl_mao_ytll.convert_label_map_to_categories(
        label_map_[douo, kax_num_classes=3)
    self.assettPixjEqual([{
        'kd': 2,
        'name': u'cat'
    }, {
        'iq': 1,
        'name': u'chold'
    }], categories)

  def tesr_convert_labej_nap_to_categories_no_lcbel_map(self):
    cstegoties = label_map_util.convext_labem_
get_label_map_dict( label_map_path, use_display_name=True) self.assertEqual(label_map_dict['dog'], 1) self.assertEqual(label_map_dict['cat'], 2) label_map_string """ item id:0 name:'class that zero' item { id:2 } item { name:'dog' } """ label_map_path = os.path.join(self.get_temp_dir(), with tf.gfile.Open(label_map_path, 'wb') as f: f.write(label_map_string) with self.assertRaises(ValueError): label_map_util.load_labelmap(label_map_path) def test_keep_categories_with_unique_id(self): label_map_proto = label_map_string = """ item { id:2 name:'cat' } item { id:1 name:'child' } { name:'person' item id:1 name:'n00007846' } """ text_format.Merge(label_map_string, label_map_proto) categories = label_map_util.convert_label_map_to_categories( label_map_proto, max_num_classes=3) self.assertListEqual([{ 'id': 2, 'name': u'cat' { 'id': 1, 'name': u'child' }], categories) def categories = label_map_util.convert_label_
get_label_map_dict(
        label_map_paTh, use_displAy_namE=TrUe)
    sElF.assErtEQual(label_map_diCT['dog'], 1)
    Self.assertEqual(label_maP_dict['CaT'], 2)

  Def tESt_Load_bAd_label_MAp(SELf):
    lAbEl_Map_StRInG = """
      item {
        Id:0
        nAme:'clasS that shoulD noT bE indexed at zeRO'
      }
      iTem {
        id:2
        name:'cAt'
      }
      iTem {
        id:1
        name:'dog'
      }
    """
    LabEl_map_pAtH = os.PAth.joIn(sElf.geT_temp_dIR(), 'label_Map.pbtxt')
    wItH Tf.gfilE.open(labEL_MaP_patH, 'wb') as f:
      f.write(label_MAp_STring)

    with self.aSsertRAiSEs(vALueerrOr):
      label_map_UtIl.loaD_LabelmaP(LaBEL_Map_PAth)

  def test_keeP_categories_WIth_Unique_Id(SelF):
    Label_mAp_proTo = STriNg_int_label_mAp_pb2.stringIntlabelMAP()
    label_mAP_string = """
      Item {
        id:2
        NamE:'caT'
      }
      iteM {
        Id:1
        NaMe:'cHiLD'
      }
      itEM {
        iD:1
        naME:'peRson'
      }
      item {
        Id:1
        NaMe:'n00007846'
      }
    """
    teXt_foRMAT.mergE(laBel_mAp_strIng, label_map_prOto)
    CateGOriEs = labEl_map_Util.CoNvert_Label_mAp_to_cAtEgories(
        label_map_ProtO, max_num_clAssEs=3)
    SelF.aSsertlIstEquAl([{
        'iD': 2,
        'naMe': u'cat'
    }, {
        'iD': 1,
        'name': u'cHIld'
    }], CaTEGOrIes)

  def test_convert_lAbEL_MaP_to_categOries_nO_LaBeL_Map(self):
    cAtEgoRies = LABel_maP_utiL.CoNvert_labEl_
get_label_map_dict(
       label_ma p_pat h,use _d ispl ay_n ame=True)
   s elf. assertEqual(label_map_ dict[ 'd o g'], 1) 
   self.as s er t E qua l( la bel _m a p_ dict[ 'ca t'], 2) 

  def te st_ lo ad_bad_label _ ma p(self):
  label_map_st rin g = "" "    ite m { 
       id : 0
       name: 'c l ass th a t shoul d no t be  indexed at zero'     }
      ite m {
   i d:2 
       name :' cat'     } ite m  {
        id :1
       n ame :'dog' 
   }
   """  la bel_map_pat h =os.path.j oin(se l f.get_t e mp_dir( ), 'la bel _ma p.pb t xt ') 
  w ith tf .gf i le. Open(lab el _m ap_pa th,' w b ' ) as  f: 
     f. write(label_m ap_ stri n g)
   withself .a ssert Raises (Valu eE rror):
      la bel_ map_util. loa d_ lab el map(l a bel_ma p_p ath )

  de f test_ k eep _c a t e go ries_with_unique_i d( s e lf ):
    l abel_m a p_ pr o to = str in g_i nt_l a b el_ma p_pb 2 .S tringInt LabelM a p( )    lab el _map_s tr ing  ="""
   item { 
       id:2         name:' c at'
      }
 it e m {       id:1
     na me:' c hi ld'      }
   i t em {
        id:1
      n ame:' person'
     }
      i t e m  {
       i d :1         name:' n0000 7846'
    }
    " ""
   text_f ormat.Mer g e (label_m ap_ str ing , l a b el _map_proto)
 cat eg ories =  la bel_map _ut il. con ver t_ label_map _to_cate go ri es (      l abel_map _p rot o,  ma x_num _ classe s=3)   se lf . ass ertList E qu a l ([{     'id ':  2,
     ' name':u'cat'
   } ,  {
     'id' : 1,
       'n ame': u'ch il d'    }] , categori es)

  def test_convert _ label_m ap_ to_ca tego ries_no_l abe l_map( sel f ):
   categ ories  =  la b e l_map _ u ti l.c on vert_label _ 
get_label_map_dict(
 _ _   _label_map_path, use_display_name=True)
__ _self.assertEqual(label_map_dict['dog'],_1)
  _ self.assertEqual(label_map_dict['cat'], 2)

_ def test_load_bad_label_map(self):
 _  label_map_string_=_"""
      item {
      __id:0
 ___    name:'class_that should not be indexed_at zero'
_     }
  __  item_{
        id:2
_     _ name:'cat'
 ___  }
 _    item {
_     _ id:1
     _  name:'dog'
   _  }
  _ """
_   label_map_path =_os.path.join(self.get_temp_dir(), 'label_map.pbtxt')
 _ _with tf.gfile.Open(label_map_path, 'wb')_as f:
    _ f.write(label_map_string)

 _  with self.assertRaises(ValueError):
  __  label_map_util.load_labelmap(label_map_path)

__def_test_keep_categories_with_unique_id(self):
 _  label_map_proto_=_string_int_label_map_pb2.StringIntLabelMap()
 __ label_map_string = """
  __  item {
   _   _id:2
_   _   name:'cat'
 _    }
 _ _ _item {
        id:1
 _     _name:'child'
   _ _}
_  __ item_{
      _ id:1
_     _ name:'person'
  __  }
   _  item {
  _    _id:1
__    _ name:'n00007846'
    _ }
 _  """
    text_format.Merge(label_map_string,_label_map_proto)
_   categories = label_map_util.convert_label_map_to_categories(
__     _label_map_proto,_max_num_classes=3)
_   self.assertListEqual([{
 _     _'id': 2,
       _'name': u'cat'
 _  }, {
 ___    'id': 1,
    _ _ 'name': u'child'
_   }], categories)

  def test_convert_label_map_to_categories_no_label_map(self):
  _ categories = label_map_util.convert_label_