{
  "id": 132976,
  "title": "How to use 3 channels?",
  "url": "/competitions/bengaliai-cv19/discussion/132976",
  "author_name": "",
  "post_date": "2020-02-29T03:21:03.922581100Z",
  "votes": 1,
  "comment_count": 8,
  "views": 0,
  "content": "<p>I saw too many participants use 3 channels input.But how?\nBecause the data load from the origin file only have 1 channel,Did you just transform the data from \"Grey\" to \"RGB\" or do some extra work?\nThank for your answer!</p>",
  "messages": [
    {
      "id": "759448",
      "postDate": "02/29/2020 03:21:03",
      "content": "<p>I saw too many participants use 3 channels input.But how?\nBecause the data load from the origin file only have 1 channel,Did you just transform the data from \"Grey\" to \"RGB\" or do some extra work?\nThank for your answer!</p>",
      "rawMarkdown": "I saw too many participants use 3 channels input.But how?\nBecause the data load from the origin file only have 1 channel,Did you just transform the data from \"Grey\" to \"RGB\" or do some extra work?\nThank for your answer!",
      "votes": null
    },
    {
      "id": "759450",
      "postDate": "02/29/2020 03:25:47",
      "content": "<p><code>\nimage = np.stack([image, image, image]).transpose(1,2,0)\n</code></p>",
      "rawMarkdown": "```\nimage = np.stack([image, image, image]).transpose(1,2,0)\n```",
      "votes": null
    },
    {
      "id": "759458",
      "postDate": "02/29/2020 04:03:37",
      "content": "<p>in your nn.Module model forward function:\n<code>\ndef forward(gray):\n       color=gray.repeat(1,3,1,1)\n</code></p>",
      "rawMarkdown": "in your nn.Module model forward function:\n```\ndef forward(gray):\n       color=gray.repeat(1,3,1,1)\n```",
      "votes": null
    },
    {
      "id": "759463",
      "postDate": "02/29/2020 04:11:06",
      "content": "<p><code>\nfrom PIL import Image\nimage = np.array(Image.fromarray(image).convert(\"RGB\"))\n</code></p>",
      "rawMarkdown": "```\nfrom PIL import Image\nimage = np.array(Image.fromarray(image).convert(\"RGB\"))\n```",
      "votes": null
    },
    {
      "id": "759560",
      "postDate": "02/29/2020 07:31:55",
      "content": "<p>What's the benefit ?</p>",
      "rawMarkdown": "What's the benefit ?",
      "votes": null
    },
    {
      "id": "759903",
      "postDate": "02/29/2020 15:53:40",
      "content": "<p>Or you can directly give the 1 channel input to A convnet of 3 filters. This transformation is done to implement Transfer Learning, As trained models have input of 3 channels.</p>",
      "rawMarkdown": "Or you can directly give the 1 channel input to A convnet of 3 filters. This transformation is done to implement Transfer Learning, As trained models have input of 3 channels.",
      "votes": null
    },
    {
      "id": "760048",
      "postDate": "02/29/2020 19:19:20",
      "content": "<p>Using PIL for that is inefficient. Better just repeat grayscale layer to 3 channels using numpy.\nSince I'm using keras+tf I'm usually adding Lambda layer with \"tf.image.grayscale_to_rgb\" function before the input of my pretrained model: <a href=\"https://www.tensorflow.org/api_docs/python/tf/image/grayscale_to_rgb\">https://www.tensorflow.org/api_docs/python/tf/image/grayscale_to_rgb</a></p>",
      "rawMarkdown": "Using PIL for that is inefficient. Better just repeat grayscale layer to 3 channels using numpy.\nSince I'm using keras+tf I'm usually adding Lambda layer with \"tf.image.grayscale_to_rgb\" function before the input of my pretrained model: https://www.tensorflow.org/api_docs/python/tf/image/grayscale_to_rgb",
      "votes": null
    },
    {
      "id": "760063",
      "postDate": "02/29/2020 19:55:50",
      "content": "<p>I added an extra layer after the 1 channel input to convert it to three identical layers. \n```\ninput = Input(shape = input_shape)</p>\n\n<p>tiledInput = keras.layers.Lambda(lambda s:K.tile(s,K.constant([1,1,1,3],dtype=tf.int32)),name='tileChannelDim3')(input)\n```</p>",
      "rawMarkdown": "I added an extra layer after the 1 channel input to convert it to three identical layers. \n```\ninput = Input(shape = input_shape)\n\ntiledInput = keras.layers.Lambda(lambda s:K.tile(s,K.constant([1,1,1,3],dtype=tf.int32)),name='tileChannelDim3')(input)\n```",
      "votes": null
    },
    {
      "id": "763890",
      "postDate": "03/05/2020 00:18:30",
      "content": "<p>I use the third method of <a href=\"/drhabib\">@drhabib</a> \n<a href=\"https://www.kaggle.com/c/bengaliai-cv19/discussion/130311#745589\">https://www.kaggle.com/c/bengaliai-cv19/discussion/130311#745589</a></p>",
      "rawMarkdown": "I use the third method of @drhabib \nhttps://www.kaggle.com/c/bengaliai-cv19/discussion/130311#745589",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 759450,
      "author_name": "appian",
      "author_url": "",
      "post_date": "02/29/2020 03:25:47",
      "content": "<p><code>\nimage = np.stack([image, image, image]).transpose(1,2,0)\n</code></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 759458,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "02/29/2020 04:03:37",
      "content": "<p>in your nn.Module model forward function:\n<code>\ndef forward(gray):\n       color=gray.repeat(1,3,1,1)\n</code></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 759463,
      "author_name": "kani23",
      "author_url": "",
      "post_date": "02/29/2020 04:11:06",
      "content": "<p><code>\nfrom PIL import Image\nimage = np.array(Image.fromarray(image).convert(\"RGB\"))\n</code></p>",
      "votes": null,
      "replies": [
        {
          "id": 759560,
          "author_name": "hassanamin",
          "author_url": "",
          "post_date": "02/29/2020 07:31:55",
          "content": "<p>What's the benefit ?</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 760048,
          "author_name": "karolzak",
          "author_url": "",
          "post_date": "02/29/2020 19:19:20",
          "content": "<p>Using PIL for that is inefficient. Better just repeat grayscale layer to 3 channels using numpy.\nSince I'm using keras+tf I'm usually adding Lambda layer with \"tf.image.grayscale_to_rgb\" function before the input of my pretrained model: <a href=\"https://www.tensorflow.org/api_docs/python/tf/image/grayscale_to_rgb\">https://www.tensorflow.org/api_docs/python/tf/image/grayscale_to_rgb</a></p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 759903,
      "author_name": "chittalpatel",
      "author_url": "",
      "post_date": "02/29/2020 15:53:40",
      "content": "<p>Or you can directly give the 1 channel input to A convnet of 3 filters. This transformation is done to implement Transfer Learning, As trained models have input of 3 channels.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 760063,
      "author_name": "barnwellguy",
      "author_url": "",
      "post_date": "02/29/2020 19:55:50",
      "content": "<p>I added an extra layer after the 1 channel input to convert it to three identical layers. \n```\ninput = Input(shape = input_shape)</p>\n\n<p>tiledInput = keras.layers.Lambda(lambda s:K.tile(s,K.constant([1,1,1,3],dtype=tf.int32)),name='tileChannelDim3')(input)\n```</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 763890,
      "author_name": "yuanlin08",
      "author_url": "",
      "post_date": "03/05/2020 00:18:30",
      "content": "<p>I use the third method of <a href=\"/drhabib\">@drhabib</a> \n<a href=\"https://www.kaggle.com/c/bengaliai-cv19/discussion/130311#745589\">https://www.kaggle.com/c/bengaliai-cv19/discussion/130311#745589</a></p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "759448": "I saw too many participants use 3 channels input.But how?\nBecause the data load from the origin file only have 1 channel,Did you just transform the data from \"Grey\" to \"RGB\" or do some extra work?\nThank for your answer!",
    "759450": "```\nimage = np.stack([image, image, image]).transpose(1,2,0)\n```",
    "759458": "in your nn.Module model forward function:\n```\ndef forward(gray):\n       color=gray.repeat(1,3,1,1)\n```",
    "759463": "```\nfrom PIL import Image\nimage = np.array(Image.fromarray(image).convert(\"RGB\"))\n```",
    "759560": "What's the benefit ?",
    "759903": "Or you can directly give the 1 channel input to A convnet of 3 filters. This transformation is done to implement Transfer Learning, As trained models have input of 3 channels.",
    "760048": "Using PIL for that is inefficient. Better just repeat grayscale layer to 3 channels using numpy.\nSince I'm using keras+tf I'm usually adding Lambda layer with \"tf.image.grayscale_to_rgb\" function before the input of my pretrained model: https://www.tensorflow.org/api_docs/python/tf/image/grayscale_to_rgb",
    "760063": "I added an extra layer after the 1 channel input to convert it to three identical layers. \n```\ninput = Input(shape = input_shape)\n\ntiledInput = keras.layers.Lambda(lambda s:K.tile(s,K.constant([1,1,1,3],dtype=tf.int32)),name='tileChannelDim3')(input)\n```",
    "763890": "I use the third method of @drhabib \nhttps://www.kaggle.com/c/bengaliai-cv19/discussion/130311#745589"
  },
  "source": "meta"
}