{
  "id": 37890,
  "title": "The pixels are numbered from top to bottom, then left to right?",
  "url": "/competitions/carvana-image-masking-challenge/discussion/37890",
  "author_name": "",
  "post_date": "2017-08-11T04:27:59.774942700Z",
  "votes": null,
  "comment_count": 2,
  "views": 0,
  "content": "<p>I tried to plot the mask and i find that it should be:\nThe pixels are numbered from <strong>left to right</strong>, then <strong>top to bottom</strong>?</p>\n\n<p>This is my decompress function which transform rle_mask to np array:</p>\n\n<pre><code>def decompressMask(mask):\n    img = np.zeros((1280 * 1918))\n    idx = list(map(int, mask.split(' ')))\n    for i in range(len(idx) // 2):\n        # one-indexed     \n        img[(idx[2*i]-1):(idx[2*i]-1+idx[2*i+1])] = 255 # \n    return np.reshape(img, (1280,1918))\n</code></pre>\n\n<p>Update: Solved - Should be 255 but not 1</p>",
  "messages": [
    {
      "id": "212252",
      "postDate": "08/11/2017 04:27:59",
      "content": "<p>I tried to plot the mask and i find that it should be:\nThe pixels are numbered from <strong>left to right</strong>, then <strong>top to bottom</strong>?</p>\n\n<p>This is my decompress function which transform rle_mask to np array:</p>\n\n<pre><code>def decompressMask(mask):\n    img = np.zeros((1280 * 1918))\n    idx = list(map(int, mask.split(' ')))\n    for i in range(len(idx) // 2):\n        # one-indexed     \n        img[(idx[2*i]-1):(idx[2*i]-1+idx[2*i+1])] = 255 # \n    return np.reshape(img, (1280,1918))\n</code></pre>\n\n<p>Update: Solved - Should be 255 but not 1</p>",
      "rawMarkdown": "I tried to plot the mask and i find that it should be:\nThe pixels are numbered from **left to right**, then **top to bottom**?\n\nThis is my decompress function which transform rle_mask to np array:\n\n    def decompressMask(mask):\n        img = np.zeros((1280 * 1918))\n        idx = list(map(int, mask.split(' ')))\n        for i in range(len(idx) // 2):\n            # one-indexed     \n            img[(idx[2*i]-1):(idx[2*i]-1+idx[2*i+1])] = 255 # \n        return np.reshape(img, (1280,1918))\n\nUpdate: Solved - Should be 255 but not 1",
      "votes": null
    },
    {
      "id": "212254",
      "postDate": "08/11/2017 05:06:40",
      "content": "<p>use these functions</p>\n\n<pre><code>#https://www.kaggle.com/stainsby/fast-tested-rle\ndef run_length_encode(mask):\n    '''\n   img: numpy array, 1 - mask, 0 - background\n   Returns run length as string formated\n    '''\n   inds = mask.flatten()\n   inds[ 0] = 0\n   inds[-1] = 0\n   runs = np.where(inds[1:] != inds[:-1])[0] + 2\n   runs[1::2] = runs[1::2] - runs[:-1:2]\n   rle = ' '.join([str(r) for r in runs])\n   return rle\n\n\ndef run_length_decode(rel, H, W, fill_value=255):\n   mask = np.zeros((H*W),np.uint8)\n   rel  = np.array([int(s) for s in rel.split(' ')]).reshape(-1,2)\n   for r in rel:\n           start = r[0]-1       #They are one-indexed\n           end   = start +r[1]\n           mask[start:end] = fill_value\n   mask = mask.reshape(H,W)\n   return mask\n\n## check function -----------------------------------------------------\n\n#check with train_masks.csv given\ncsv_file  = CARVANA_DIR + '/masks_train.csv'  # read all annotations\nmask_dir  = CARVANA_DIR + '/annotations/train_gif'  # read all annotations\n\n\ndf  = pd.read_csv(csv_file)\nfor n in range(20): #check 20\n    shortname = df.values[n][0].replace('.jpg','')\n    rle_hat   = df.values[n][1]\n\n    mask_file = mask_dir + '/' + shortname + '_mask.gif'\n    mask_hat = PIL.Image.open(mask_file)\n    mask_hat = np.array(mask_hat).astype(np.uint8)\n\n    # check encode\n    rle = run_length_encode(mask_hat)\n    match = rle == rle_hat\n    print('encode @%d : match=%s'%(n,match))\n\n    # check decode\n    mask = run_length_decode(rle, H=1280, W=1918, fill_value=1)\n    match = np.array_equal(mask, mask_hat)\n    print('decode @%d : match=%s'%(n,match))\n</code></pre>",
      "rawMarkdown": "use these functions\n\n\n    #https://www.kaggle.com/stainsby/fast-tested-rle\n    def run_length_encode(mask):\n        '''\n       img: numpy array, 1 - mask, 0 - background\n       Returns run length as string formated\n        '''\n       inds = mask.flatten()\n       inds[ 0] = 0\n       inds[-1] = 0\n       runs = np.where(inds[1:] != inds[:-1])[0] + 2\n       runs[1::2] = runs[1::2] - runs[:-1:2]\n       rle = ' '.join([str(r) for r in runs])\n       return rle\n\n\n    def run_length_decode(rel, H, W, fill_value=255):\n       mask = np.zeros((H*W),np.uint8)\n       rel  = np.array([int(s) for s in rel.split(' ')]).reshape(-1,2)\n       for r in rel:\n               start = r[0]-1       #They are one-indexed\n               end   = start +r[1]\n               mask[start:end] = fill_value\n       mask = mask.reshape(H,W)\n       return mask\n\n    ## check function -----------------------------------------------------\n \n    #check with train_masks.csv given\n    csv_file  = CARVANA_DIR + '/masks_train.csv'  # read all annotations\n    mask_dir  = CARVANA_DIR + '/annotations/train_gif'  # read all annotations\n\n\n    df  = pd.read_csv(csv_file)\n    for n in range(20): #check 20\n        shortname = df.values[n][0].replace('.jpg','')\n        rle_hat   = df.values[n][1]\n\n        mask_file = mask_dir + '/' + shortname + '_mask.gif'\n        mask_hat = PIL.Image.open(mask_file)\n        mask_hat = np.array(mask_hat).astype(np.uint8)\n\n        # check encode\n        rle = run_length_encode(mask_hat)\n        match = rle == rle_hat\n        print('encode @%d : match=%s'%(n,match))\n\n        # check decode\n        mask = run_length_decode(rle, H=1280, W=1918, fill_value=1)\n        match = np.array_equal(mask, mask_hat)\n        print('decode @%d : match=%s'%(n,match))",
      "votes": null
    },
    {
      "id": "212260",
      "postDate": "08/11/2017 05:29:42",
      "content": "<p>Thanks! Let me try.</p>",
      "rawMarkdown": "Thanks! Let me try.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 212254,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "08/11/2017 05:06:40",
      "content": "<p>use these functions</p>\n\n<pre><code>#https://www.kaggle.com/stainsby/fast-tested-rle\ndef run_length_encode(mask):\n    '''\n   img: numpy array, 1 - mask, 0 - background\n   Returns run length as string formated\n    '''\n   inds = mask.flatten()\n   inds[ 0] = 0\n   inds[-1] = 0\n   runs = np.where(inds[1:] != inds[:-1])[0] + 2\n   runs[1::2] = runs[1::2] - runs[:-1:2]\n   rle = ' '.join([str(r) for r in runs])\n   return rle\n\n\ndef run_length_decode(rel, H, W, fill_value=255):\n   mask = np.zeros((H*W),np.uint8)\n   rel  = np.array([int(s) for s in rel.split(' ')]).reshape(-1,2)\n   for r in rel:\n           start = r[0]-1       #They are one-indexed\n           end   = start +r[1]\n           mask[start:end] = fill_value\n   mask = mask.reshape(H,W)\n   return mask\n\n## check function -----------------------------------------------------\n\n#check with train_masks.csv given\ncsv_file  = CARVANA_DIR + '/masks_train.csv'  # read all annotations\nmask_dir  = CARVANA_DIR + '/annotations/train_gif'  # read all annotations\n\n\ndf  = pd.read_csv(csv_file)\nfor n in range(20): #check 20\n    shortname = df.values[n][0].replace('.jpg','')\n    rle_hat   = df.values[n][1]\n\n    mask_file = mask_dir + '/' + shortname + '_mask.gif'\n    mask_hat = PIL.Image.open(mask_file)\n    mask_hat = np.array(mask_hat).astype(np.uint8)\n\n    # check encode\n    rle = run_length_encode(mask_hat)\n    match = rle == rle_hat\n    print('encode @%d : match=%s'%(n,match))\n\n    # check decode\n    mask = run_length_decode(rle, H=1280, W=1918, fill_value=1)\n    match = np.array_equal(mask, mask_hat)\n    print('decode @%d : match=%s'%(n,match))\n</code></pre>",
      "votes": null,
      "replies": [
        {
          "id": 212260,
          "author_name": "alberthkcheng",
          "author_url": "",
          "post_date": "08/11/2017 05:29:42",
          "content": "<p>Thanks! Let me try.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "212252": "I tried to plot the mask and i find that it should be:\nThe pixels are numbered from **left to right**, then **top to bottom**?\n\nThis is my decompress function which transform rle_mask to np array:\n\n    def decompressMask(mask):\n        img = np.zeros((1280 * 1918))\n        idx = list(map(int, mask.split(' ')))\n        for i in range(len(idx) // 2):\n            # one-indexed     \n            img[(idx[2*i]-1):(idx[2*i]-1+idx[2*i+1])] = 255 # \n        return np.reshape(img, (1280,1918))\n\nUpdate: Solved - Should be 255 but not 1",
    "212254": "use these functions\n\n\n    #https://www.kaggle.com/stainsby/fast-tested-rle\n    def run_length_encode(mask):\n        '''\n       img: numpy array, 1 - mask, 0 - background\n       Returns run length as string formated\n        '''\n       inds = mask.flatten()\n       inds[ 0] = 0\n       inds[-1] = 0\n       runs = np.where(inds[1:] != inds[:-1])[0] + 2\n       runs[1::2] = runs[1::2] - runs[:-1:2]\n       rle = ' '.join([str(r) for r in runs])\n       return rle\n\n\n    def run_length_decode(rel, H, W, fill_value=255):\n       mask = np.zeros((H*W),np.uint8)\n       rel  = np.array([int(s) for s in rel.split(' ')]).reshape(-1,2)\n       for r in rel:\n               start = r[0]-1       #They are one-indexed\n               end   = start +r[1]\n               mask[start:end] = fill_value\n       mask = mask.reshape(H,W)\n       return mask\n\n    ## check function -----------------------------------------------------\n \n    #check with train_masks.csv given\n    csv_file  = CARVANA_DIR + '/masks_train.csv'  # read all annotations\n    mask_dir  = CARVANA_DIR + '/annotations/train_gif'  # read all annotations\n\n\n    df  = pd.read_csv(csv_file)\n    for n in range(20): #check 20\n        shortname = df.values[n][0].replace('.jpg','')\n        rle_hat   = df.values[n][1]\n\n        mask_file = mask_dir + '/' + shortname + '_mask.gif'\n        mask_hat = PIL.Image.open(mask_file)\n        mask_hat = np.array(mask_hat).astype(np.uint8)\n\n        # check encode\n        rle = run_length_encode(mask_hat)\n        match = rle == rle_hat\n        print('encode @%d : match=%s'%(n,match))\n\n        # check decode\n        mask = run_length_decode(rle, H=1280, W=1918, fill_value=1)\n        match = np.array_equal(mask, mask_hat)\n        print('decode @%d : match=%s'%(n,match))",
    "212260": "Thanks! Let me try."
  },
  "source": "meta"
}