{
  "id": 162678,
  "title": "Can someone explain this augmentation matrix  code?",
  "url": "/competitions/siim-isic-melanoma-classification/discussion/162678",
  "author_name": "",
  "post_date": "2020-06-29T17:40:35.460348300Z",
  "votes": 3,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Can someone explain this augmentation matrix  code?</p>\n\n<p>`def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):\n    # returns 3x3 transformmatrix which transforms indicies</p>\n\n<pre><code># CONVERT DEGREES TO RADIANS\nrotation = math.pi * rotation / 180.\nshear    = math.pi * shear    / 180.\n\ndef get_3x3_mat(lst):\n    return tf.reshape(tf.concat([lst],axis=0), [3,3])\n\n# ROTATION MATRIX\nc1   = tf.math.cos(rotation)\ns1   = tf.math.sin(rotation)\none  = tf.constant([1],dtype='float32')\nzero = tf.constant([0],dtype='float32')\n\nrotation_matrix = get_3x3_mat([c1,   s1,   zero, \n                               -s1,  c1,   zero, \n                               zero, zero, one])    \n# SHEAR MATRIX\nc2 = tf.math.cos(shear)\ns2 = tf.math.sin(shear)    \n\nshear_matrix = get_3x3_mat([one,  s2,   zero, \n                            zero, c2,   zero, \n                            zero, zero, one])        \n# ZOOM MATRIX\nzoom_matrix = get_3x3_mat([one/height_zoom, zero,           zero, \n                           zero,            one/width_zoom, zero, \n                           zero,            zero,           one])    \n# SHIFT MATRIX\nshift_matrix = get_3x3_mat([one,  zero, height_shift, \n                            zero, one,  width_shift, \n                            zero, zero, one])\n\nreturn K.dot(K.dot(rotation_matrix, shear_matrix), \n             K.dot(zoom_matrix,     shift_matrix))`\n</code></pre>\n\n<p>Why SHEAR MATRIX is [[ 1, s2, 0], [ 0, c2, 0], [ 0, 0 ,  1]] ,\nnot    [[ 1, s2, 0],  [ 0, 1 , 0], [ 0, 0 , 1]] ?</p>",
  "messages": [
    {
      "id": "907131",
      "postDate": "06/29/2020 17:40:35",
      "content": "<p>Can someone explain this augmentation matrix  code?</p>\n\n<p>`def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):\n    # returns 3x3 transformmatrix which transforms indicies</p>\n\n<pre><code># CONVERT DEGREES TO RADIANS\nrotation = math.pi * rotation / 180.\nshear    = math.pi * shear    / 180.\n\ndef get_3x3_mat(lst):\n    return tf.reshape(tf.concat([lst],axis=0), [3,3])\n\n# ROTATION MATRIX\nc1   = tf.math.cos(rotation)\ns1   = tf.math.sin(rotation)\none  = tf.constant([1],dtype='float32')\nzero = tf.constant([0],dtype='float32')\n\nrotation_matrix = get_3x3_mat([c1,   s1,   zero, \n                               -s1,  c1,   zero, \n                               zero, zero, one])    \n# SHEAR MATRIX\nc2 = tf.math.cos(shear)\ns2 = tf.math.sin(shear)    \n\nshear_matrix = get_3x3_mat([one,  s2,   zero, \n                            zero, c2,   zero, \n                            zero, zero, one])        \n# ZOOM MATRIX\nzoom_matrix = get_3x3_mat([one/height_zoom, zero,           zero, \n                           zero,            one/width_zoom, zero, \n                           zero,            zero,           one])    \n# SHIFT MATRIX\nshift_matrix = get_3x3_mat([one,  zero, height_shift, \n                            zero, one,  width_shift, \n                            zero, zero, one])\n\nreturn K.dot(K.dot(rotation_matrix, shear_matrix), \n             K.dot(zoom_matrix,     shift_matrix))`\n</code></pre>\n\n<p>Why SHEAR MATRIX is [[ 1, s2, 0], [ 0, c2, 0], [ 0, 0 ,  1]] ,\nnot    [[ 1, s2, 0],  [ 0, 1 , 0], [ 0, 0 , 1]] ?</p>",
      "rawMarkdown": "Can someone explain this augmentation matrix  code?\n\n`def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):\n    # returns 3x3 transformmatrix which transforms indicies\n        \n    # CONVERT DEGREES TO RADIANS\n    rotation = math.pi * rotation / 180.\n    shear    = math.pi * shear    / 180.\n\n    def get_3x3_mat(lst):\n        return tf.reshape(tf.concat([lst],axis=0), [3,3])\n    \n    # ROTATION MATRIX\n    c1   = tf.math.cos(rotation)\n    s1   = tf.math.sin(rotation)\n    one  = tf.constant([1],dtype='float32')\n    zero = tf.constant([0],dtype='float32')\n    \n    rotation_matrix = get_3x3_mat([c1,   s1,   zero, \n                                   -s1,  c1,   zero, \n                                   zero, zero, one])    \n    # SHEAR MATRIX\n    c2 = tf.math.cos(shear)\n    s2 = tf.math.sin(shear)    \n    \n    shear_matrix = get_3x3_mat([one,  s2,   zero, \n                                zero, c2,   zero, \n                                zero, zero, one])        \n    # ZOOM MATRIX\n    zoom_matrix = get_3x3_mat([one/height_zoom, zero,           zero, \n                               zero,            one/width_zoom, zero, \n                               zero,            zero,           one])    \n    # SHIFT MATRIX\n    shift_matrix = get_3x3_mat([one,  zero, height_shift, \n                                zero, one,  width_shift, \n                                zero, zero, one])\n    \n    return K.dot(K.dot(rotation_matrix, shear_matrix), \n                 K.dot(zoom_matrix,     shift_matrix))`\nWhy SHEAR MATRIX is [[ 1, s2, 0], [ 0, c2, 0], [ 0, 0 ,  1]] ,\nnot    [[ 1, s2, 0],  [ 0, 1 , 0], [ 0, 0 , 1]] ?",
      "votes": null
    },
    {
      "id": "907239",
      "postDate": "06/29/2020 19:41:18",
      "content": "<p>This is augmentation using an affine transform. The different elements in the 3x3 matrix result in different augmentations. Have a look at this for some examples: <a href=\"https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation\">https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation</a></p>",
      "rawMarkdown": "This is augmentation using an affine transform. The different elements in the 3x3 matrix result in different augmentations. Have a look at this for some examples: https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation",
      "votes": null
    },
    {
      "id": "907312",
      "postDate": "06/29/2020 20:54:52",
      "content": "<p>The shear matrix does look strange. You might want to ask Chris Deotte -- I believe he is the one who made this code for the Flower Classification Competition.</p>",
      "rawMarkdown": "The shear matrix does look strange. You might want to ask Chris Deotte -- I believe he is the one who made this code for the Flower Classification Competition.",
      "votes": null
    },
    {
      "id": "907849",
      "postDate": "06/30/2020 07:46:20",
      "content": "<p>The shear matrix def is different from wiki.</p>",
      "rawMarkdown": "The shear matrix def is different from wiki.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 907239,
      "author_name": "anjum48",
      "author_url": "",
      "post_date": "06/29/2020 19:41:18",
      "content": "<p>This is augmentation using an affine transform. The different elements in the 3x3 matrix result in different augmentations. Have a look at this for some examples: <a href=\"https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation\">https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 907849,
          "author_name": "chihantsai",
          "author_url": "",
          "post_date": "06/30/2020 07:46:20",
          "content": "<p>The shear matrix def is different from wiki.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 907312,
      "author_name": "graf10a",
      "author_url": "",
      "post_date": "06/29/2020 20:54:52",
      "content": "<p>The shear matrix does look strange. You might want to ask Chris Deotte -- I believe he is the one who made this code for the Flower Classification Competition.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "907131": "Can someone explain this augmentation matrix  code?\n\n`def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):\n    # returns 3x3 transformmatrix which transforms indicies\n        \n    # CONVERT DEGREES TO RADIANS\n    rotation = math.pi * rotation / 180.\n    shear    = math.pi * shear    / 180.\n\n    def get_3x3_mat(lst):\n        return tf.reshape(tf.concat([lst],axis=0), [3,3])\n    \n    # ROTATION MATRIX\n    c1   = tf.math.cos(rotation)\n    s1   = tf.math.sin(rotation)\n    one  = tf.constant([1],dtype='float32')\n    zero = tf.constant([0],dtype='float32')\n    \n    rotation_matrix = get_3x3_mat([c1,   s1,   zero, \n                                   -s1,  c1,   zero, \n                                   zero, zero, one])    \n    # SHEAR MATRIX\n    c2 = tf.math.cos(shear)\n    s2 = tf.math.sin(shear)    \n    \n    shear_matrix = get_3x3_mat([one,  s2,   zero, \n                                zero, c2,   zero, \n                                zero, zero, one])        \n    # ZOOM MATRIX\n    zoom_matrix = get_3x3_mat([one/height_zoom, zero,           zero, \n                               zero,            one/width_zoom, zero, \n                               zero,            zero,           one])    \n    # SHIFT MATRIX\n    shift_matrix = get_3x3_mat([one,  zero, height_shift, \n                                zero, one,  width_shift, \n                                zero, zero, one])\n    \n    return K.dot(K.dot(rotation_matrix, shear_matrix), \n                 K.dot(zoom_matrix,     shift_matrix))`\nWhy SHEAR MATRIX is [[ 1, s2, 0], [ 0, c2, 0], [ 0, 0 ,  1]] ,\nnot    [[ 1, s2, 0],  [ 0, 1 , 0], [ 0, 0 , 1]] ?",
    "907239": "This is augmentation using an affine transform. The different elements in the 3x3 matrix result in different augmentations. Have a look at this for some examples: https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation",
    "907312": "The shear matrix does look strange. You might want to ask Chris Deotte -- I believe he is the one who made this code for the Flower Classification Competition.",
    "907849": "The shear matrix def is different from wiki."
  },
  "source": "meta"
}