{
  "id": 167300,
  "title": "Code in TF for YCbCr conversion",
  "url": "/competitions/alaska2-image-steganalysis/discussion/167300",
  "author_name": "",
  "post_date": "2020-07-16T01:37:19.743042200Z",
  "votes": 4,
  "comment_count": 3,
  "views": 0,
  "content": "<p>I wrote this code for RGB2YCbCr conversion in tensorflow. However, the AUC obtained is very very low. I was wondering if there is a mistake:</p>\n\n<p><code>\ndef rgb2ycbcr(rgb):\n    m = tf.convert_to_tensor(np.array([[ 65.481, 128.553, 24.966],\n                  [-37.797, -74.203, 112],\n                  [ 112, -93.786, -18.214]]), dtype=tf.float32)\n    rgb = tf.cast(rgb, tf.float32)\n    shape = (512,512,3)#rgb.shape\n    if len(shape) == 3:\n        rgb = tf.reshape(rgb,(512 * 512, 3))\n    ycbcr = tf.tensordot(rgb, tf.transpose(m) / 255., 1)\n    ycbcr = tf.stack([ycbcr[:,0] + 16., ycbcr[:,1] + 128., ycbcr[:,2]+ 128.], axis = 0)\n    return tf.reshape(ycbcr,shape)\n</code></p>",
  "messages": [
    {
      "id": "931111",
      "postDate": "07/16/2020 01:37:19",
      "content": "<p>I wrote this code for RGB2YCbCr conversion in tensorflow. However, the AUC obtained is very very low. I was wondering if there is a mistake:</p>\n\n<p><code>\ndef rgb2ycbcr(rgb):\n    m = tf.convert_to_tensor(np.array([[ 65.481, 128.553, 24.966],\n                  [-37.797, -74.203, 112],\n                  [ 112, -93.786, -18.214]]), dtype=tf.float32)\n    rgb = tf.cast(rgb, tf.float32)\n    shape = (512,512,3)#rgb.shape\n    if len(shape) == 3:\n        rgb = tf.reshape(rgb,(512 * 512, 3))\n    ycbcr = tf.tensordot(rgb, tf.transpose(m) / 255., 1)\n    ycbcr = tf.stack([ycbcr[:,0] + 16., ycbcr[:,1] + 128., ycbcr[:,2]+ 128.], axis = 0)\n    return tf.reshape(ycbcr,shape)\n</code></p>",
      "rawMarkdown": "I wrote this code for RGB2YCbCr conversion in tensorflow. However, the AUC obtained is very very low. I was wondering if there is a mistake:\n\n```\ndef rgb2ycbcr(rgb):\n    m = tf.convert_to_tensor(np.array([[ 65.481, 128.553, 24.966],\n                  [-37.797, -74.203, 112],\n                  [ 112, -93.786, -18.214]]), dtype=tf.float32)\n    rgb = tf.cast(rgb, tf.float32)\n    shape = (512,512,3)#rgb.shape\n    if len(shape) == 3:\n        rgb = tf.reshape(rgb,(512 * 512, 3))\n    ycbcr = tf.tensordot(rgb, tf.transpose(m) / 255., 1)\n    ycbcr = tf.stack([ycbcr[:,0] + 16., ycbcr[:,1] + 128., ycbcr[:,2]+ 128.], axis = 0)\n    return tf.reshape(ycbcr,shape)\n```",
      "votes": null
    },
    {
      "id": "934549",
      "postDate": "07/18/2020 14:24:15",
      "content": "<p>Any luck here, this would be good when using TPU</p>",
      "rawMarkdown": "Any luck here, this would be good when using TPU",
      "votes": null
    },
    {
      "id": "935010",
      "postDate": "07/19/2020 02:34:04",
      "content": "<p>\"I was wondering if there is a mistake\"\nyou can compare your results with the open cv or PIL for rgb to ycrcb converison</p>\n\n<hr>\n\n<p>it is mentioned in the other post. it may be better to use ycbcr from decompress jpeg (and maybe to keep as float or short, i.e. not rounding to 0~255).  the reason we use ycrcb is to avoid rounding error from image reader that read jpeg as rgb (else there is no theoretical advantage of using ycbcr over rgb as the conversion is merely linear operations)</p>\n\n<p>you can decompress jpeg and store them as tensor and then put them in tfrecords.</p>\n\n<hr>\n\n<p>my experiments shows ycbcr from decompress jpeg is good for some splitting folds</p>",
      "rawMarkdown": "\"I was wondering if there is a mistake\"\nyou can compare your results with the open cv or PIL for rgb to ycrcb converison\n\n---\n\nit is mentioned in the other post. it may be better to use ycbcr from decompress jpeg (and maybe to keep as float or short, i.e. not rounding to 0~255).  the reason we use ycrcb is to avoid rounding error from image reader that read jpeg as rgb (else there is no theoretical advantage of using ycbcr over rgb as the conversion is merely linear operations)\n\nyou can decompress jpeg and store them as tensor and then put them in tfrecords.\n\n---\n\nmy experiments shows ycbcr from decompress jpeg is good for some splitting folds",
      "votes": null
    },
    {
      "id": "935016",
      "postDate": "07/19/2020 02:47:22",
      "content": "<p>Thanks for this, this makes a lot of sense, I was trying to understand why there was so much hype behind YCC when the transformation was so simple the model should be able to easily pick up on it.</p>",
      "rawMarkdown": "Thanks for this, this makes a lot of sense, I was trying to understand why there was so much hype behind YCC when the transformation was so simple the model should be able to easily pick up on it.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 934549,
      "author_name": "treadon",
      "author_url": "",
      "post_date": "07/18/2020 14:24:15",
      "content": "<p>Any luck here, this would be good when using TPU</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 935010,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "07/19/2020 02:34:04",
      "content": "<p>\"I was wondering if there is a mistake\"\nyou can compare your results with the open cv or PIL for rgb to ycrcb converison</p>\n\n<hr>\n\n<p>it is mentioned in the other post. it may be better to use ycbcr from decompress jpeg (and maybe to keep as float or short, i.e. not rounding to 0~255).  the reason we use ycrcb is to avoid rounding error from image reader that read jpeg as rgb (else there is no theoretical advantage of using ycbcr over rgb as the conversion is merely linear operations)</p>\n\n<p>you can decompress jpeg and store them as tensor and then put them in tfrecords.</p>\n\n<hr>\n\n<p>my experiments shows ycbcr from decompress jpeg is good for some splitting folds</p>",
      "votes": null,
      "replies": [
        {
          "id": 935016,
          "author_name": "treadon",
          "author_url": "",
          "post_date": "07/19/2020 02:47:22",
          "content": "<p>Thanks for this, this makes a lot of sense, I was trying to understand why there was so much hype behind YCC when the transformation was so simple the model should be able to easily pick up on it.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "931111": "I wrote this code for RGB2YCbCr conversion in tensorflow. However, the AUC obtained is very very low. I was wondering if there is a mistake:\n\n```\ndef rgb2ycbcr(rgb):\n    m = tf.convert_to_tensor(np.array([[ 65.481, 128.553, 24.966],\n                  [-37.797, -74.203, 112],\n                  [ 112, -93.786, -18.214]]), dtype=tf.float32)\n    rgb = tf.cast(rgb, tf.float32)\n    shape = (512,512,3)#rgb.shape\n    if len(shape) == 3:\n        rgb = tf.reshape(rgb,(512 * 512, 3))\n    ycbcr = tf.tensordot(rgb, tf.transpose(m) / 255., 1)\n    ycbcr = tf.stack([ycbcr[:,0] + 16., ycbcr[:,1] + 128., ycbcr[:,2]+ 128.], axis = 0)\n    return tf.reshape(ycbcr,shape)\n```",
    "934549": "Any luck here, this would be good when using TPU",
    "935010": "\"I was wondering if there is a mistake\"\nyou can compare your results with the open cv or PIL for rgb to ycrcb converison\n\n---\n\nit is mentioned in the other post. it may be better to use ycbcr from decompress jpeg (and maybe to keep as float or short, i.e. not rounding to 0~255).  the reason we use ycrcb is to avoid rounding error from image reader that read jpeg as rgb (else there is no theoretical advantage of using ycbcr over rgb as the conversion is merely linear operations)\n\nyou can decompress jpeg and store them as tensor and then put them in tfrecords.\n\n---\n\nmy experiments shows ycbcr from decompress jpeg is good for some splitting folds",
    "935016": "Thanks for this, this makes a lot of sense, I was trying to understand why there was so much hype behind YCC when the transformation was so simple the model should be able to easily pick up on it."
  },
  "source": "meta"
}