首页 > 解决方案 > TypeError:尝试训练 MASK_RCNN 实现时,字符串索引必须是整数

问题描述

我在 Why am I see "TypeError: string indices must be integers" 中查看了类似的问题?但我不确定我应该对代码进行什么样的更改。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-a47aee8a70a4> in <module>()
      3 # Training dataset.
      4 dataset_train = RoadDataset()
----> 5 dataset_train.load_road("/host/Mask_RCNN_karol_majek_Practice_2/road","train")
      6 dataset_train.prepare()
      7 

<ipython-input-48-5f64614b7c47> in load_road(self, dataset_dir, subset)
     35         # The VIA tool saves images in the JSON even if they don't have any
     36         # annotations. Skip unannotated images.
---> 37         annotations = [a for a in annotations if a['regions']]
     38 
     39         # Add images

<ipython-input-48-5f64614b7c47> in <listcomp>(.0)
     35         # The VIA tool saves images in the JSON even if they don't have any
     36         # annotations. Skip unannotated images.
---> 37         annotations = [a for a in annotations if a['regions']]
     38 
     39         # Add images

TypeError: string indices must be integers

这是错误指向的块:

        # Train or validation dataset?
        assert subset in ["train", "val"]
        dataset_dir = os.path.join(dataset_dir, subset)

     
        # We mostly care about the x and y coordinates of each region
        # Note: In VIA 2.0, regions was changed from a dict to a list.
        annotations = json.load(open(os.path.join(dataset_dir, "via_project_train.json")))
        annotations = list(annotations.values())  # don't need the dict keys

        # The VIA tool saves images in the JSON even if they don't have any
        # annotations. Skip unannotated images.
        annotations = [a for a in annotations if a['regions']]

        # Add images
        for a in annotations:
            # Get the x, y coordinaets of points of the polygons that make up
            # the outline of each object instance. These are stores in the
            # shape_attributes (see json format above)
            # The if condition is needed to support VIA versions 1.x and 2.x.
            if type(a['regions']) is dict:
                polygons = [r['shape_attributes'] for r in a['regions'].values()]
            else:
                polygons = [r['shape_attributes'] for r in a['regions']] 

            # load_mask() needs the image size to convert polygons to masks.
            # Unfortunately, VIA doesn't include it in JSON, so we must read
            # the image. This is only managable since the dataset is tiny.
            image_path = os.path.join(dataset_dir, a['filename'])
            image = skimage.io.imread(image_path)
            height, width = image.shape[:2]

            self.add_image(
                "road",
                image_id=a['filename'],  # use file name as a unique image id
                path=image_path,
                width=width, height=height,
                polygons=polygons)
            

json文件:

{"_via_settings":{"ui":{"annotation_editor_height":25,"annotation_editor_fontsize":0.8,"leftsidebar_width":18,"image_grid":{"img_height":80,"rshape_fill":"none","rshape_fill_opacity":0.3,"rshape_stroke":"yellow","rshape_stroke_width":2,"show_region_shape":true,"show_image_policy":"all"},"image":{"region_label":"__via_region_id__","region_color":"__via_default_region_color__","region_label_font":"10px Sans","on_image_annotation_editor_placement":"NEAR_REGION"}},"core":{"buffer_size":"18","filepath":{},"default_filepath":"G:\\My Drive\\Sivaraj\\FYP Part 2\\Classification Model\\mask_rcnn\\dataset\\train"},"project":{"name":"via_project_train"}},"_via_img_metadata":{"roAd1.jpg11726":{"filename":"roAd1.jpg","size":11726,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[59,58,78,78],"all_points_y":[29,47,46,28]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[75,75,62,62],"all_points_y":[28,24,25,29]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[82,102,102,80],"all_points_y":[25,24,44,45]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[110,110,159,158],"all_points_y":[24,43,44,24]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd2.jpg11853":{"filename":"roAd2.jpg","size":11853,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[175,186,199,186],"all_points_y":[67,56,68,78]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[187,198,187,175],"all_points_y":[82,94,105,93]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[55,70,71,66,64,55,55,54],"all_points_y":[30,35,55,56,54,55,58,56]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[96,100,104,101],"all_points_y":[36,32,36,40]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[167,167,143,143,149,167],"all_points_y":[42,28,29,40,45,44]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd3.jpg8500":{"filename":"roAd3.jpg","size":8500,"regions":[],"file_attributes":{}},"roAd4.jpg10458":{"filename":"roAd4.jpg","size":10458,"regions":[],"file_attributes":{}},"roAd5.jpg10578":{"filename":"roAd5.jpg","size":10578,"regions":[],"file_attributes":{}},"roAd6.jpg19852":{"filename":"roAd6.jpg","size":19852,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[89,121,145,117],"all_points_y":[132,107,135,157]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[289,289,294,294],"all_points_y":[206,200,201,206]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[324,329,329,323,323],"all_points_y":[202,202,195,195,201]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[357,366,365,356,358],"all_points_y":[204,204,192,193,203]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd7.jpg11029":{"filename":"roAd7.jpg","size":11029,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[135,141,141,144,143,136,136],"all_points_y":[88,88,90,90,85,85,88]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[130,130,123,124,127,127],"all_points_y":[84,90,90,76,76,84]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[64,69,69,64],"all_points_y":[64,64,79,78]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[71,81,81,72,72,71,71],"all_points_y":[79,79,71,71,64,64,79]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[82,85,91,92,81,82],"all_points_y":[66,66,68,80,79,66]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd8.jpg11339":{"filename":"roAd8.jpg","size":11339,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[69,124,123,67],"all_points_y":[56,49,110,109]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd9.jpg8522":{"filename":"roAd9.jpg","size":8522,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[74,155,153,71],"all_points_y":[8,5,129,128]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd10.jpg17990":{"filename":"roAd10.jpg","size":17990,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[135,136,48,42,41,47],"all_points_y":[45,63,71,66,57,52]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd11.jpg29255":{"filename":"roAd11.jpg","size":29255,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[74,92,91,72],"all_points_y":[86,86,101,102]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[106,188,188,106],"all_points_y":[40,42,59,59]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[23,70,69,21],"all_points_y":[50,43,59,65]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[229,229,286,285],"all_points_y":[47,63,69,56]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[250,271,272,277,277,280,280,252],"all_points_y":[16,22,25,25,23,25,39,32]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[294,298,299,296],"all_points_y":[60,61,73,73]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[21,51,50,21],"all_points_y":[24,16,31,40]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[0,14,14,1],"all_points_y":[32,28,41,45]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[88,92,95,96,93,89,86,84,82,84],"all_points_y":[64,65,67,72,76,78,78,76,71,67]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[76,81,87,90,90,84,79,75,72,74],"all_points_y":[114,112,116,123,129,135,134,129,123,116]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[0,4,7,6,3,0],"all_points_y":[111,112,118,122,127,123]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[212,216,223,226,226,222,218,213,209,208,208,208,212],"all_points_y":[112,110,111,116,122,129,131,130,128,125,122,117,113]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[16,19,19,18,17,15,13,12,12,13],"all_points_y":[47,48,52,55,56,57,57,56,53,49]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[136,138,138,140,140,141,141,136],"all_points_y":[97,97,97,97,97,97,99,99]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd12.jpg7271":{"filename":"roAd12.jpg","size":7271,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[122,144,144,124],"all_points_y":[106,105,115,118]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[146,163,164,146],"all_points_y":[102,101,113,115]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd13.jpg102933":{"filename":"roAd13.jpg","size":102933,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[457,557,556,460],"all_points_y":[115,119,164,168]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[557,748,747,559],"all_points_y":[55,55,172,172]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[399,415,400,383],"all_points_y":[256,275,288,275]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[421,431,421,413],"all_points_y":[224,237,244,237]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[601,660,659,603],"all_points_y":[244,248,268,264]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[363,385,384,362],"all_points_y":[235,237,246,246]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[373,381,383,383,381,375,369,365,365,371],"all_points_y":[225,225,219,214,209,206,210,214,221,226]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[372,382,383,371],"all_points_y":[229,229,234,234]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[385,390,395,394,391,384,385,381,378,380],"all_points_y":[229,228,225,221,213,213,219,225,225,227]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[677,683,680],"all_points_y":[255,255,261]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[661,665,664,661],"all_points_y":[261,261,267,267]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd14.jpg48044":{"filename":"roAd14.jpg","size":48044,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[0,90,103,1],"all_points_y":[179,182,107,121]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd16.jpg251580":{"filename":"roAd16.jpg","size":251580,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[325,325,486,493],"all_points_y":[592,720,720,599]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[515,805,807,515],"all_points_y":[598,603,722,722]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[408,453,448,410],"all_points_y":[798,800,811,809]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[410,424,450,450,413],"all_points_y":[830,836,838,814,809]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd17.jpg54627":{"filename":"roAd17.jpg","size":54627,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[29,196,194,30],"all_points_y":[90,85,144,146]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[268,269,468,469],"all_points_y":[81,139,137,75]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[226,243,255,255,254,250,245,238,228,220,214,209,217],"all_points_y":[122,126,137,145,156,160,166,169,170,166,160,149,133]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[192,248,247,190],"all_points_y":[181,183,233,232]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd27.jpg14717":{"filename":"roAd27.jpg","size":14717,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[166,245,248,169],"all_points_y":[41,39,55,57]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[165,247,248,167],"all_points_y":[57,58,68,70]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[166,245,247,168],"all_points_y":[74,72,86,89]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[254,293,293,265,260,254],"all_points_y":[76,77,89,89,86,85]},"region_attributes":{"type":"sign","name":"sign"}},{"shape_attributes":{"name":"polygon","all_points_x":[166,251,250,171],"all_points_y":[91,92,109,112]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}},"roAd28.jpg10231":{"filename":"roAd28.jpg","size":10231,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[0,147,145,3],"all_points_y":[28,60,96,83]},"region_attributes":{"type":"sign","name":"sign"}}],"file_attributes":{}}},"_via_attributes":{"region":{"name":{"type":"text","description":"name of the object","default_value":"not_defined"},"type":{"type":"dropdown","description":"category of the object","options":{"sign":"Sign"},"default_options":{"sign":true}}},"file":{}},"_via_data_format_version":"2.0.10","_via_image_id_list":["roAd1.jpg11726","roAd2.jpg11853","roAd3.jpg8500","roAd4.jpg10458","roAd5.jpg10578","roAd6.jpg19852","roAd7.jpg11029","roAd8.jpg11339","roAd9.jpg8522","roAd10.jpg17990","roAd11.jpg29255","roAd12.jpg7271","roAd13.jpg102933","roAd14.jpg48044","roAd16.jpg251580","roAd17.jpg54627","roAd27.jpg14717","roAd28.jpg10231"]}

在实施答案的编辑版本后,这是收到的新错误:

  File "road.py", line 370, in <module>
    train(model)
  File "road.py", line 189, in train
    dataset_train.load_road(args.dataset, "train")
  File "road.py", line 120, in load_road
    annotations = annotations["_via_img_metadata"]
TypeError: list indices must be integers or slices, not str

标签: pythonfaster-rcnn

解决方案


这意味着并非所有annotations的都是字典,其中可能有字符串。

您可以通过替换此行来过滤所有非 dict 类型

annotations = [a for a in annotations if a['regions']]

有了这个

annotations = [a for a in annotations if isinstance(a, dict)]
annotations = [a for a in annotations if "regions" in a.keys()]]

编辑

查看提供的数据后,密钥regions实际上在另一个 dict in 中_via_img_metadata,因此注释应该是:

 annotations = annotations["_via_img_metadata"]
 annotations = list(annotations.values())

然后运行之前的代码


推荐阅读