{
  "id": 27506,
  "title": "Submission code and errors ",
  "url": "/competitions/dstl-satellite-imagery-feature-detection/discussion/27506",
  "author_name": "",
  "post_date": "2017-01-09T13:39:27.437Z",
  "votes": 1,
  "comment_count": 7,
  "views": 411,
  "content": "<p>Hi all,</p>\n\n<p><strong>The following code is my submission code to this competition. However, there are some errors in the evaluation that says \"Evaluation Exception: premature end of enumerator \". Did someone else met this error? @Wendy Kan, @ Kyle @ Jianmin Sun @ ZFTurbo.  Or could someone else help me find out how to solve this problem? May this code can also help you.</strong> </p>\n\n<pre><code>  enter code here\n#!/usr/bin/env python\n\n# -*- coding: utf-8 -*-\n\nfrom __future__ import print_function\nfrom collections import namedtuple\nimport csv\nimport os.path\nimport random\nimport sys\nimport numpy as np\nimport PIL.Image\nimport shapely.geometry\nimport shapely.wkt\nimport skimage.segmentation\nimport skimage.measure as sk_measuer\nfrom osgeo import gdal\nfrom osgeo import osr\nfrom osgeo import ogr\nimport scipy.io as sio\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport types\nimport time\nfrom shapely import wkt\n\ndef _convert_coordinates_to_raster(img_size, xymax):\n    Xmax,Ymax = xymax\n    H,W = img_size\n    W1 = 1.0*W*W/(W+1)\n    H1 = 1.0*H*H/(H+1)\n    xf = Xmax/W1\n    yf = Ymax/H1\n    return (xf,yf)\n\ndef detect(image_path,xymax):\n    detections = []\n    image = sio.loadmat(image_path)\n    segments = image['L']\n    H=image['H']\n    W=image['W']\n    img_size=H[0,0],W[0,0]\n    print(img_size)\n    xf,yf=_convert_coordinates_to_raster(img_size, xymax)\n    imageName = image['imgName']\n    max_label = np.max(np.max(segments))\n    multipolygon=ogr.Geometry(ogr.wkbMultiPolygon)\n\n\n\nfor i in range(1, max_label + 1):\n        tmp_label_map = np.zeros(segments.shape, dtype=np.uint8)\n        x, y = np.nonzero(segments == i)\n        if len(x) &lt; 4:\n            continue\n        tmp_label_map[x, y] = 1\n        contours = sk_measuer.find_contours(tmp_label_map, 0)\n        if len(contours) == 0:\n            continue\n        contour = contours[0]\n        pt_sum=0\n        ring = ogr.Geometry(ogr.wkbLinearRing)\n        pt_num = contour.shape[0]\n        ring.AddPoint_2D(contour[0][1]*xf, contour[0][0]*yf) #(x, y)\n        pt_sum=pt_sum+1\n        for j in range(1, pt_num-1, 3):\n            ring.AddPoint_2D(contour[j][1]*xf, contour[j][0]*yf) #(x, y)\n            pt_sum=pt_sum+1\n        ring.AddPoint_2D(contour[pt_num-1][1]*xf, contour[pt_num-1][0]*yf) #(x, y)\n        pt_sum=pt_sum+1\n        ring.CloseRings()\n        pt_sum=pt_sum+1\n        poly = ogr.Geometry(ogr.wkbPolygon)\n        poly.AddGeometry(ring)\n        if pt_acc &gt; 4 :\n            multipolygon.AddGeometry(poly)\n    detections= multipolygon.ExportToWkt()\n    p=wkt.loads(detections)\n    detections = p.buffer(0)\n    print(detections.is_valid)\n    return detections\n\ndef main():\n    image_folder = '../kag_res/res50_p_weight/res_mat'  # folder with test images\n    f=open(\"test_img_id.txt\",\"r\") # the index of the test images\n\n    # create a csv file, which will comply with the contest format\n    with open('../new_sub/my_is_valid_results.csv', 'w') as dest:\n        writer = csv.writer(dest)\n        # header line\n        writer.writerow(['ImageId', 'ClassType', 'MultipolygonWKT'])\n        # loop over test images and detect every class based on model\n        image_name=f.readline()\n        print(image_name)\n        num=1\n        while image_name:\n            print(num)\n            num=num+1\n            with open('grid_sizes.csv','rb') as gs :\n                reader=csv.reader(gs)\n                for row in reader:\n                    if row[0] == image_name[0:8] :\n                        xymax=float(row[1]),float(row[2])\n                        print(xymax)\n                        break\n                    else :\n                        continue\n            for c in range(0,10):\n                detections = []\n                detections = detect(os.path.join(image_folder, image_name[0:8] +'_' +str(c)+'.mat'),xymax)\n                writer.writerow([image_name[0:8], int(c)+1, detections])\n            image_name = f.readline()\n            # summary data for the image\n\nif  __name__  ==  '__main__':\n    main()\n</code></pre>\n\n<p><strong>Thank you very much！！！</strong></p>",
  "messages": [
    {
      "id": "155084",
      "postDate": "01/09/2017 13:39:27",
      "content": "<p>Hi all,</p>\n\n<p><strong>The following code is my submission code to this competition. However, there are some errors in the evaluation that says \"Evaluation Exception: premature end of enumerator \". Did someone else met this error? @Wendy Kan, @ Kyle @ Jianmin Sun @ ZFTurbo.  Or could someone else help me find out how to solve this problem? May this code can also help you.</strong> </p>\n\n<pre><code>  enter code here\n#!/usr/bin/env python\n\n# -*- coding: utf-8 -*-\n\nfrom __future__ import print_function\nfrom collections import namedtuple\nimport csv\nimport os.path\nimport random\nimport sys\nimport numpy as np\nimport PIL.Image\nimport shapely.geometry\nimport shapely.wkt\nimport skimage.segmentation\nimport skimage.measure as sk_measuer\nfrom osgeo import gdal\nfrom osgeo import osr\nfrom osgeo import ogr\nimport scipy.io as sio\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport types\nimport time\nfrom shapely import wkt\n\ndef _convert_coordinates_to_raster(img_size, xymax):\n    Xmax,Ymax = xymax\n    H,W = img_size\n    W1 = 1.0*W*W/(W+1)\n    H1 = 1.0*H*H/(H+1)\n    xf = Xmax/W1\n    yf = Ymax/H1\n    return (xf,yf)\n\ndef detect(image_path,xymax):\n    detections = []\n    image = sio.loadmat(image_path)\n    segments = image['L']\n    H=image['H']\n    W=image['W']\n    img_size=H[0,0],W[0,0]\n    print(img_size)\n    xf,yf=_convert_coordinates_to_raster(img_size, xymax)\n    imageName = image['imgName']\n    max_label = np.max(np.max(segments))\n    multipolygon=ogr.Geometry(ogr.wkbMultiPolygon)\n\n\n\nfor i in range(1, max_label + 1):\n        tmp_label_map = np.zeros(segments.shape, dtype=np.uint8)\n        x, y = np.nonzero(segments == i)\n        if len(x) &lt; 4:\n            continue\n        tmp_label_map[x, y] = 1\n        contours = sk_measuer.find_contours(tmp_label_map, 0)\n        if len(contours) == 0:\n            continue\n        contour = contours[0]\n        pt_sum=0\n        ring = ogr.Geometry(ogr.wkbLinearRing)\n        pt_num = contour.shape[0]\n        ring.AddPoint_2D(contour[0][1]*xf, contour[0][0]*yf) #(x, y)\n        pt_sum=pt_sum+1\n        for j in range(1, pt_num-1, 3):\n            ring.AddPoint_2D(contour[j][1]*xf, contour[j][0]*yf) #(x, y)\n            pt_sum=pt_sum+1\n        ring.AddPoint_2D(contour[pt_num-1][1]*xf, contour[pt_num-1][0]*yf) #(x, y)\n        pt_sum=pt_sum+1\n        ring.CloseRings()\n        pt_sum=pt_sum+1\n        poly = ogr.Geometry(ogr.wkbPolygon)\n        poly.AddGeometry(ring)\n        if pt_acc &gt; 4 :\n            multipolygon.AddGeometry(poly)\n    detections= multipolygon.ExportToWkt()\n    p=wkt.loads(detections)\n    detections = p.buffer(0)\n    print(detections.is_valid)\n    return detections\n\ndef main():\n    image_folder = '../kag_res/res50_p_weight/res_mat'  # folder with test images\n    f=open(\"test_img_id.txt\",\"r\") # the index of the test images\n\n    # create a csv file, which will comply with the contest format\n    with open('../new_sub/my_is_valid_results.csv', 'w') as dest:\n        writer = csv.writer(dest)\n        # header line\n        writer.writerow(['ImageId', 'ClassType', 'MultipolygonWKT'])\n        # loop over test images and detect every class based on model\n        image_name=f.readline()\n        print(image_name)\n        num=1\n        while image_name:\n            print(num)\n            num=num+1\n            with open('grid_sizes.csv','rb') as gs :\n                reader=csv.reader(gs)\n                for row in reader:\n                    if row[0] == image_name[0:8] :\n                        xymax=float(row[1]),float(row[2])\n                        print(xymax)\n                        break\n                    else :\n                        continue\n            for c in range(0,10):\n                detections = []\n                detections = detect(os.path.join(image_folder, image_name[0:8] +'_' +str(c)+'.mat'),xymax)\n                writer.writerow([image_name[0:8], int(c)+1, detections])\n            image_name = f.readline()\n            # summary data for the image\n\nif  __name__  ==  '__main__':\n    main()\n</code></pre>\n\n<p><strong>Thank you very much！！！</strong></p>",
      "rawMarkdown": "Hi all,\r\n\r\n   **The following code is my submission code to this competition. However, there are some errors in the evaluation that says \"Evaluation Exception: premature end of enumerator \". Did someone else met this error? @Wendy Kan, @ Kyle @ Jianmin Sun @ ZFTurbo.  Or could someone else help me find out how to solve this problem? May this code can also help you.** \r\n\r\n  \r\n\r\n      enter code here\r\n    #!/usr/bin/env python\r\n    \r\n    # -*- coding: utf-8 -*-\r\n\r\n    from __future__ import print_function\r\n    from collections import namedtuple\r\n    import csv\r\n    import os.path\r\n    import random\r\n    import sys\r\n    import numpy as np\r\n    import PIL.Image\r\n    import shapely.geometry\r\n    import shapely.wkt\r\n    import skimage.segmentation\r\n    import skimage.measure as sk_measuer\r\n    from osgeo import gdal\r\n    from osgeo import osr\r\n    from osgeo import ogr\r\n    import scipy.io as sio\r\n    import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\r\n    import cv2\r\n    import types\r\n    import time\r\n    from shapely import wkt\r\n\r\n    def _convert_coordinates_to_raster(img_size, xymax):\r\n    \tXmax,Ymax = xymax\r\n    \tH,W = img_size\r\n    \tW1 = 1.0*W*W/(W+1)\r\n    \tH1 = 1.0*H*H/(H+1)\r\n    \txf = Xmax/W1\r\n    \tyf = Ymax/H1\r\n    \treturn (xf,yf)\r\n    \r\n    def detect(image_path,xymax):\r\n    \tdetections = []\r\n    \timage = sio.loadmat(image_path)\r\n    \tsegments = image['L']\r\n    \tH=image['H']\r\n    \tW=image['W']\r\n    \timg_size=H[0,0],W[0,0]\r\n    \tprint(img_size)\r\n    \txf,yf=_convert_coordinates_to_raster(img_size, xymax)\r\n    \timageName = image['imgName']\r\n    \tmax_label = np.max(np.max(segments))\r\n    \tmultipolygon=ogr.Geometry(ogr.wkbMultiPolygon)\r\n\r\n\t\r\n\r\n    for i in range(1, max_label + 1):\r\n    \t\ttmp_label_map = np.zeros(segments.shape, dtype=np.uint8)\r\n    \t\tx, y = np.nonzero(segments == i)\r\n    \t\tif len(x) < 4:\r\n    \t\t\tcontinue\r\n    \t\ttmp_label_map[x, y] = 1\r\n    \t\tcontours = sk_measuer.find_contours(tmp_label_map, 0)\r\n    \t\tif len(contours) == 0:\r\n    \t\t\tcontinue\r\n    \t\tcontour = contours[0]\r\n    \t\tpt_sum=0\r\n    \t\tring = ogr.Geometry(ogr.wkbLinearRing)\r\n    \t\tpt_num = contour.shape[0]\r\n    \t\tring.AddPoint_2D(contour[0][1]*xf, contour[0][0]*yf) #(x, y)\r\n    \t\tpt_sum=pt_sum+1\r\n    \t\tfor j in range(1, pt_num-1, 3):\r\n    \t\t\tring.AddPoint_2D(contour[j][1]*xf, contour[j][0]*yf) #(x, y)\r\n    \t\t\tpt_sum=pt_sum+1\r\n    \t\tring.AddPoint_2D(contour[pt_num-1][1]*xf, contour[pt_num-1][0]*yf) #(x, y)\r\n    \t\tpt_sum=pt_sum+1\r\n    \t\tring.CloseRings()\r\n    \t\tpt_sum=pt_sum+1\r\n    \t\tpoly = ogr.Geometry(ogr.wkbPolygon)\r\n    \t\tpoly.AddGeometry(ring)\r\n    \t\tif pt_acc > 4 :\r\n    \t\t\tmultipolygon.AddGeometry(poly)\r\n    \tdetections= multipolygon.ExportToWkt()\r\n    \tp=wkt.loads(detections)\r\n    \tdetections = p.buffer(0)\r\n    \tprint(detections.is_valid)\r\n    \treturn detections\r\n\t\t\r\n    def main():\r\n    \timage_folder = '../kag_res/res50_p_weight/res_mat'  # folder with test images\r\n    \tf=open(\"test_img_id.txt\",\"r\") # the index of the test images\r\n    \r\n        # create a csv file, which will comply with the contest format\r\n    \twith open('../new_sub/my_is_valid_results.csv', 'w') as dest:\r\n    \t\twriter = csv.writer(dest)\r\n            # header line\r\n    \t\twriter.writerow(['ImageId', 'ClassType', 'MultipolygonWKT'])\r\n            # loop over test images and detect every class based on model\r\n    \t\timage_name=f.readline()\r\n    \t\tprint(image_name)\r\n    \t\tnum=1\r\n    \t\twhile image_name:\r\n    \t\t\tprint(num)\r\n    \t\t\tnum=num+1\r\n    \t\t\twith open('grid_sizes.csv','rb') as gs :\r\n    \t\t\t\treader=csv.reader(gs)\r\n    \t\t\t\tfor row in reader:\r\n    \t\t\t\t\tif row[0] == image_name[0:8] :\r\n    \t\t\t\t\t\txymax=float(row[1]),float(row[2])\r\n    \t\t\t\t\t\tprint(xymax)\r\n    \t\t\t\t\t\tbreak\r\n    \t\t\t\t\telse :\r\n    \t\t\t\t\t\tcontinue\r\n    \t\t\tfor c in range(0,10):\r\n    \t\t\t\tdetections = []\r\n    \t\t\t\tdetections = detect(os.path.join(image_folder, image_name[0:8] +'_' +str(c)+'.mat'),xymax)\r\n    \t\t\t\twriter.writerow([image_name[0:8], int(c)+1, detections])\r\n    \t\t\timage_name = f.readline()\r\n                # summary data for the image\r\n    \r\n    if  __name__  ==  '__main__':\r\n    \tmain()\r\n\r\n\r\n**Thank you very much！！！**",
      "votes": null
    },
    {
      "id": "155123",
      "postDate": "01/09/2017 17:38:37",
      "content": "<p><strong>guangliang2016</strong>\nCheck if you have all IDs and all classes present in order like in sample submission.</p>",
      "rawMarkdown": "**guangliang2016**\r\nCheck if you have all IDs and all classes present in order like in sample submission.",
      "votes": null
    },
    {
      "id": "155161",
      "postDate": "01/09/2017 22:55:37",
      "content": "<p>@guangliang2016, </p>\n\n<p>Looks like there's an error from reading your 6010_0_1, class 4. You might want to check out the <a href=\"https://www.kaggle.com/c/dstl-satellite-imagery-feature-detection/discussion/27351\">tool</a> that @amaia created to verify your multipolygons prior to submitting. </p>\n\n<p>I'll make an update to the evaluation code to include imageId and classId for easier debugging. </p>",
      "rawMarkdown": "guangliang2016, \n\nLooks like there's an error from reading your 6010_0_1, class 4. You might want to check out the [tool][1] that @amaia created to verify your multipolygons prior to submitting. \n\n\nI'll make an update to the evaluation code to include imageId and classId for easier debugging. \n\n  [1]: https://www.kaggle.com/c/dstl-satellite-imagery-feature-detection/discussion/27351",
      "votes": null
    },
    {
      "id": "155179",
      "postDate": "01/10/2017 00:56:09",
      "content": "<p>Thanks, @ZFTurbo I have checked all the IDs and all the classes, my submission is the same with the sample submission.   I am not sure what caused the error \"\"Evaluation Exception: premature end of enumerator \"\". We have tried a lot of time to dug, but failed.  Do you have any good suggestions? Or how to correct my submission code above? Thank you very much!!!</p>",
      "rawMarkdown": "Thanks, @ZFTurbo I have checked all the IDs and all the classes, my submission is the same with the sample submission.   I am not sure what caused the error \"\"Evaluation Exception: premature end of enumerator \"\". We have tried a lot of time to dug, but failed.  Do you have any good suggestions? Or how to correct my submission code above? Thank you very much!!!",
      "votes": null
    },
    {
      "id": "155181",
      "postDate": "01/10/2017 01:04:32",
      "content": "<p>Thanks, @Wendy Kan, Actually, in our submission file, we just replaced the first 250lines in the sample submission. In other words, the polygon in the 6010_0_1, class 4 is the same with the original sample submission.  I am not sure what caused the error \"\"Evaluation Exception: premature end of enumerator \"\". I believe, there are many competitors facing a lot of submission issues, Could you or someone else kindly share the submission code in the Forum. So that we can focus on the methods and models, rather than the submission. </p>\n\n<p>Thank you very much. Looking forward to your replay.</p>",
      "rawMarkdown": "Thanks, @Wendy Kan, Actually, in our submission file, we just replaced the first 250lines in the sample submission. In other words, the polygon in the 6010_0_1, class 4 is the same with the original sample submission.  I am not sure what caused the error \"\"Evaluation Exception: premature end of enumerator \"\". I believe, there are many competitors facing a lot of submission issues, Could you or someone else kindly share the submission code in the Forum. So that we can focus on the methods and models, rather than the submission. \r\n\r\nThank you very much. Looking forward to your replay.",
      "votes": null
    },
    {
      "id": "155198",
      "postDate": "01/10/2017 03:54:26",
      "content": "<p>@guangliang2016 How big is your submission file? Could you try again with only replacing 50 lines in sample submission? </p>",
      "rawMarkdown": "guangliang2016 How big is your submission file? Could you try again with only replacing 50 lines in sample submission?",
      "votes": null
    },
    {
      "id": "155200",
      "postDate": "01/10/2017 04:01:24",
      "content": "<p>@guangliang2016 - I haven't seen this in a while (usually my problem stems from non-noded intersection), but can you try submitting only sample submission + only a class with simpler polygons (e.g. water classes?).  Also, there isn't really a standard submission script since all of us may be accumulating the masks differently...</p>",
      "rawMarkdown": "guangliang2016 - I haven't seen this in a while (usually my problem stems from non-noded intersection), but can you try submitting only sample submission + only a class with simpler polygons (e.g. water classes?).  Also, there isn't really a standard submission script since all of us may be accumulating the masks differently...",
      "votes": null
    },
    {
      "id": "155707",
      "postDate": "01/12/2017 15:33:04",
      "content": "<p>Take a look <a href=\"https://gist.github.com/wendykan/2fcbbf95945faa0f2c89895694069010\">here</a></p>",
      "rawMarkdown": "Take a look [here][1]\n\n\n  [1]: https://gist.github.com/wendykan/2fcbbf95945faa0f2c89895694069010",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 155123,
      "author_name": "zfturbo",
      "author_url": "",
      "post_date": "01/09/2017 17:38:37",
      "content": "<p><strong>guangliang2016</strong>\nCheck if you have all IDs and all classes present in order like in sample submission.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 155161,
      "author_name": "wendykan",
      "author_url": "",
      "post_date": "01/09/2017 22:55:37",
      "content": "<p>@guangliang2016, </p>\n\n<p>Looks like there's an error from reading your 6010_0_1, class 4. You might want to check out the <a href=\"https://www.kaggle.com/c/dstl-satellite-imagery-feature-detection/discussion/27351\">tool</a> that @amaia created to verify your multipolygons prior to submitting. </p>\n\n<p>I'll make an update to the evaluation code to include imageId and classId for easier debugging. </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 155179,
      "author_name": "guangliang2016",
      "author_url": "",
      "post_date": "01/10/2017 00:56:09",
      "content": "<p>Thanks, @ZFTurbo I have checked all the IDs and all the classes, my submission is the same with the sample submission.   I am not sure what caused the error \"\"Evaluation Exception: premature end of enumerator \"\". We have tried a lot of time to dug, but failed.  Do you have any good suggestions? Or how to correct my submission code above? Thank you very much!!!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 155181,
      "author_name": "guangliang2016",
      "author_url": "",
      "post_date": "01/10/2017 01:04:32",
      "content": "<p>Thanks, @Wendy Kan, Actually, in our submission file, we just replaced the first 250lines in the sample submission. In other words, the polygon in the 6010_0_1, class 4 is the same with the original sample submission.  I am not sure what caused the error \"\"Evaluation Exception: premature end of enumerator \"\". I believe, there are many competitors facing a lot of submission issues, Could you or someone else kindly share the submission code in the Forum. So that we can focus on the methods and models, rather than the submission. </p>\n\n<p>Thank you very much. Looking forward to your replay.</p>",
      "votes": null,
      "replies": [
        {
          "id": 155707,
          "author_name": "wendykan",
          "author_url": "",
          "post_date": "01/12/2017 15:33:04",
          "content": "<p>Take a look <a href=\"https://gist.github.com/wendykan/2fcbbf95945faa0f2c89895694069010\">here</a></p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 155198,
      "author_name": "jianminsun",
      "author_url": "",
      "post_date": "01/10/2017 03:54:26",
      "content": "<p>@guangliang2016 How big is your submission file? Could you try again with only replacing 50 lines in sample submission? </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 155200,
      "author_name": "kylelee",
      "author_url": "",
      "post_date": "01/10/2017 04:01:24",
      "content": "<p>@guangliang2016 - I haven't seen this in a while (usually my problem stems from non-noded intersection), but can you try submitting only sample submission + only a class with simpler polygons (e.g. water classes?).  Also, there isn't really a standard submission script since all of us may be accumulating the masks differently...</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "155084": "Hi all,\r\n\r\n   **The following code is my submission code to this competition. However, there are some errors in the evaluation that says \"Evaluation Exception: premature end of enumerator \". Did someone else met this error? @Wendy Kan, @ Kyle @ Jianmin Sun @ ZFTurbo.  Or could someone else help me find out how to solve this problem? May this code can also help you.** \r\n\r\n  \r\n\r\n      enter code here\r\n    #!/usr/bin/env python\r\n    \r\n    # -*- coding: utf-8 -*-\r\n\r\n    from __future__ import print_function\r\n    from collections import namedtuple\r\n    import csv\r\n    import os.path\r\n    import random\r\n    import sys\r\n    import numpy as np\r\n    import PIL.Image\r\n    import shapely.geometry\r\n    import shapely.wkt\r\n    import skimage.segmentation\r\n    import skimage.measure as sk_measuer\r\n    from osgeo import gdal\r\n    from osgeo import osr\r\n    from osgeo import ogr\r\n    import scipy.io as sio\r\n    import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\r\n    import cv2\r\n    import types\r\n    import time\r\n    from shapely import wkt\r\n\r\n    def _convert_coordinates_to_raster(img_size, xymax):\r\n    \tXmax,Ymax = xymax\r\n    \tH,W = img_size\r\n    \tW1 = 1.0*W*W/(W+1)\r\n    \tH1 = 1.0*H*H/(H+1)\r\n    \txf = Xmax/W1\r\n    \tyf = Ymax/H1\r\n    \treturn (xf,yf)\r\n    \r\n    def detect(image_path,xymax):\r\n    \tdetections = []\r\n    \timage = sio.loadmat(image_path)\r\n    \tsegments = image['L']\r\n    \tH=image['H']\r\n    \tW=image['W']\r\n    \timg_size=H[0,0],W[0,0]\r\n    \tprint(img_size)\r\n    \txf,yf=_convert_coordinates_to_raster(img_size, xymax)\r\n    \timageName = image['imgName']\r\n    \tmax_label = np.max(np.max(segments))\r\n    \tmultipolygon=ogr.Geometry(ogr.wkbMultiPolygon)\r\n\r\n\t\r\n\r\n    for i in range(1, max_label + 1):\r\n    \t\ttmp_label_map = np.zeros(segments.shape, dtype=np.uint8)\r\n    \t\tx, y = np.nonzero(segments == i)\r\n    \t\tif len(x) < 4:\r\n    \t\t\tcontinue\r\n    \t\ttmp_label_map[x, y] = 1\r\n    \t\tcontours = sk_measuer.find_contours(tmp_label_map, 0)\r\n    \t\tif len(contours) == 0:\r\n    \t\t\tcontinue\r\n    \t\tcontour = contours[0]\r\n    \t\tpt_sum=0\r\n    \t\tring = ogr.Geometry(ogr.wkbLinearRing)\r\n    \t\tpt_num = contour.shape[0]\r\n    \t\tring.AddPoint_2D(contour[0][1]*xf, contour[0][0]*yf) #(x, y)\r\n    \t\tpt_sum=pt_sum+1\r\n    \t\tfor j in range(1, pt_num-1, 3):\r\n    \t\t\tring.AddPoint_2D(contour[j][1]*xf, contour[j][0]*yf) #(x, y)\r\n    \t\t\tpt_sum=pt_sum+1\r\n    \t\tring.AddPoint_2D(contour[pt_num-1][1]*xf, contour[pt_num-1][0]*yf) #(x, y)\r\n    \t\tpt_sum=pt_sum+1\r\n    \t\tring.CloseRings()\r\n    \t\tpt_sum=pt_sum+1\r\n    \t\tpoly = ogr.Geometry(ogr.wkbPolygon)\r\n    \t\tpoly.AddGeometry(ring)\r\n    \t\tif pt_acc > 4 :\r\n    \t\t\tmultipolygon.AddGeometry(poly)\r\n    \tdetections= multipolygon.ExportToWkt()\r\n    \tp=wkt.loads(detections)\r\n    \tdetections = p.buffer(0)\r\n    \tprint(detections.is_valid)\r\n    \treturn detections\r\n\t\t\r\n    def main():\r\n    \timage_folder = '../kag_res/res50_p_weight/res_mat'  # folder with test images\r\n    \tf=open(\"test_img_id.txt\",\"r\") # the index of the test images\r\n    \r\n        # create a csv file, which will comply with the contest format\r\n    \twith open('../new_sub/my_is_valid_results.csv', 'w') as dest:\r\n    \t\twriter = csv.writer(dest)\r\n            # header line\r\n    \t\twriter.writerow(['ImageId', 'ClassType', 'MultipolygonWKT'])\r\n            # loop over test images and detect every class based on model\r\n    \t\timage_name=f.readline()\r\n    \t\tprint(image_name)\r\n    \t\tnum=1\r\n    \t\twhile image_name:\r\n    \t\t\tprint(num)\r\n    \t\t\tnum=num+1\r\n    \t\t\twith open('grid_sizes.csv','rb') as gs :\r\n    \t\t\t\treader=csv.reader(gs)\r\n    \t\t\t\tfor row in reader:\r\n    \t\t\t\t\tif row[0] == image_name[0:8] :\r\n    \t\t\t\t\t\txymax=float(row[1]),float(row[2])\r\n    \t\t\t\t\t\tprint(xymax)\r\n    \t\t\t\t\t\tbreak\r\n    \t\t\t\t\telse :\r\n    \t\t\t\t\t\tcontinue\r\n    \t\t\tfor c in range(0,10):\r\n    \t\t\t\tdetections = []\r\n    \t\t\t\tdetections = detect(os.path.join(image_folder, image_name[0:8] +'_' +str(c)+'.mat'),xymax)\r\n    \t\t\t\twriter.writerow([image_name[0:8], int(c)+1, detections])\r\n    \t\t\timage_name = f.readline()\r\n                # summary data for the image\r\n    \r\n    if  __name__  ==  '__main__':\r\n    \tmain()\r\n\r\n\r\n**Thank you very much！！！**",
    "155123": "**guangliang2016**\r\nCheck if you have all IDs and all classes present in order like in sample submission.",
    "155161": "guangliang2016, \n\nLooks like there's an error from reading your 6010_0_1, class 4. You might want to check out the [tool][1] that @amaia created to verify your multipolygons prior to submitting. \n\n\nI'll make an update to the evaluation code to include imageId and classId for easier debugging. \n\n  [1]: https://www.kaggle.com/c/dstl-satellite-imagery-feature-detection/discussion/27351",
    "155179": "Thanks, @ZFTurbo I have checked all the IDs and all the classes, my submission is the same with the sample submission.   I am not sure what caused the error \"\"Evaluation Exception: premature end of enumerator \"\". We have tried a lot of time to dug, but failed.  Do you have any good suggestions? Or how to correct my submission code above? Thank you very much!!!",
    "155181": "Thanks, @Wendy Kan, Actually, in our submission file, we just replaced the first 250lines in the sample submission. In other words, the polygon in the 6010_0_1, class 4 is the same with the original sample submission.  I am not sure what caused the error \"\"Evaluation Exception: premature end of enumerator \"\". I believe, there are many competitors facing a lot of submission issues, Could you or someone else kindly share the submission code in the Forum. So that we can focus on the methods and models, rather than the submission. \r\n\r\nThank you very much. Looking forward to your replay.",
    "155198": "guangliang2016 How big is your submission file? Could you try again with only replacing 50 lines in sample submission?",
    "155200": "guangliang2016 - I haven't seen this in a while (usually my problem stems from non-noded intersection), but can you try submitting only sample submission + only a class with simpler polygons (e.g. water classes?).  Also, there isn't really a standard submission script since all of us may be accumulating the masks differently...",
    "155707": "Take a look [here][1]\n\n\n  [1]: https://gist.github.com/wendykan/2fcbbf95945faa0f2c89895694069010"
  },
  "source": "meta"
}