{
  "id": 241829,
  "title": "How can i implement TTA ",
  "url": "/competitions/seti-breakthrough-listen/discussion/241829",
  "author_name": "Mithil Salunkhe",
  "post_date": "2021-05-26T09:38:11.291000",
  "votes": 2,
  "comment_count": 5,
  "views": 0,
  "content": "<p>I am having some difficulties in implementing TTA. Can someone please help me out in how to implement TTA in tensorflow.Any help is appreciated </p>",
  "messages": [
    {
      "id": 1324182,
      "postDate": "2021-05-26T17:08:58.657Z",
      "content": "<p>You can create a TTA batch with Numpy, no need to use special libraries. Just take your input data, expand the dimension with one, and fill the batch with modified versions of the input data according to your needs. Run prediction on the batch, and then postprocess the predicted values according to needs. Here is an example  TTA function for images that flips and rotates:  </p>\n<pre><code>def create_TTA_batch(img):\n    if len(img.shape) &lt; 4:\n        img = np.expand_dims(img, 0) # expand data dimension to fit in batch\n\n    batch=np.zeros((img.shape[0]*8,img.shape[1],img.shape[2],img.shape[3]), dtype=np.float32)     \n    for i in range(img.shape[0]):\n        orig = tf.keras.preprocessing.image.img_to_array(img[i,:,:,:])/255. # un-augmented\n        batch[i*8,:,:,:] = orig\n        batch[i*8+1,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)\n        batch[i*8+2,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)\n        batch[i*8+3,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)\n        orig = orig[:, ::-1]\n        batch[i*8+4,:,:,:] = orig\n        batch[i*8+5,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)\n        batch[i*8+6,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)\n        batch[i*8+7,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)\n    return batch\n</code></pre>\n<p>You can used the same principle for any type of data.</p>",
      "rawMarkdown": "You can create a TTA batch with Numpy, no need to use special libraries. Just take your input data, expand the dimension with one, and fill the batch with modified versions of the input data according to your needs. Run prediction on the batch, and then postprocess the predicted values according to needs. Here is an example  TTA function for images that flips and rotates:  \n\n```\ndef create_TTA_batch(img):\n    if len(img.shape) < 4:\n        img = np.expand_dims(img, 0) # expand data dimension to fit in batch\n            \n    batch=np.zeros((img.shape[0]*8,img.shape[1],img.shape[2],img.shape[3]), dtype=np.float32)     \n    for i in range(img.shape[0]):\n        orig = tf.keras.preprocessing.image.img_to_array(img[i,:,:,:])/255. # un-augmented\n        batch[i*8,:,:,:] = orig\n        batch[i*8+1,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)\n        batch[i*8+2,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)\n        batch[i*8+3,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)\n        orig = orig[:, ::-1]\n        batch[i*8+4,:,:,:] = orig\n        batch[i*8+5,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)\n        batch[i*8+6,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)\n        batch[i*8+7,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)\n    return batch\n```\nYou can used the same principle for any type of data.\n",
      "votes": 1
    },
    {
      "id": 1323525,
      "postDate": "2021-05-26T09:38:11.293Z",
      "content": "<p>I am having some difficulties in implementing TTA. Can someone please help me out in how to implement TTA in tensorflow.Any help is appreciated </p>",
      "rawMarkdown": "I am having some difficulties in implementing TTA. Can someone please help me out in how to implement TTA in tensorflow.Any help is appreciated ",
      "votes": 2
    },
    {
      "id": 1324067,
      "postDate": "2021-05-26T15:34:57.090Z",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/mithilsalunkhe\" target=\"_blank\">@mithilsalunkhe</a> , Below are the steps that you can try.</p>\n<p>Step 1 : You can use ClassPredictor of the edafa library<br>\nStep 2 : Inherit predictor class and implement the main function predict_patches(self,patches)<br>\nclass myPredictor(ClassPredictor):</p>\n<pre><code>def __init__(self,model,*args,**kwargs):\n    super().__init__(*args,**kwargs)\n    self.model = model\ndef predict_patches(self,patches):\n    return self.model.predict(patches)\n</code></pre>\n<p>Step 3: Instantiate your class with configuration and whatever parameters needed<br>\nconf = '{\"augs\":[\"NO\",\\<br>\n                \"FLIP_LR\"],\\<br>\n        \"mean\":\"ARITH\"}<br>\nStep 4: Predict images.</p>",
      "rawMarkdown": "Hi @mithilsalunkhe , Below are the steps that you can try.\n\nStep 1 : You can use ClassPredictor of the edafa library\nStep 2 : Inherit predictor class and implement the main function predict_patches(self,patches)\nclass myPredictor(ClassPredictor):\n\n    def __init__(self,model,*args,**kwargs):\n        super().__init__(*args,**kwargs)\n        self.model = model\n    def predict_patches(self,patches):\n        return self.model.predict(patches)\n\nStep 3: Instantiate your class with configuration and whatever parameters needed\nconf = '{\"augs\":[\"NO\",\\\n                \"FLIP_LR\"],\\\n        \"mean\":\"ARITH\"}\nStep 4: Predict images.",
      "replies": [
        {
          "id": 1324082,
          "postDate": "2021-05-26T15:47:15.300Z",
          "content": "<p>Thank You for Replying To me. But  i think this steps are for pytorch. My question asked that how to implement in Tensorflow</p>",
          "rawMarkdown": "Thank You for Replying To me. But  i think this steps are for pytorch. My question asked that how to implement in Tensorflow"
        },
        {
          "id": 1324095,
          "postDate": "2021-05-26T15:58:34.757Z",
          "content": "<p>Yes, This is for TensorFlow only. You need to instantiate the ClassPrredictor.</p>",
          "rawMarkdown": "Yes, This is for TensorFlow only. You need to instantiate the ClassPrredictor."
        },
        {
          "id": 1324107,
          "postDate": "2021-05-26T16:14:49.147Z",
          "content": "<p>Check out the edafa page - <a href=\"https://pypi.org/project/edafa/\" target=\"_blank\">https://pypi.org/project/edafa/</a></p>",
          "rawMarkdown": "Check out the edafa page - [https://pypi.org/project/edafa/](https://pypi.org/project/edafa/)",
          "votes": 1
        }
      ]
    }
  ],
  "comments": [
    {
      "id": 1324182,
      "author_name": "Geir Drange",
      "author_url": "",
      "post_date": "2021-05-26T17:08:58.657000",
      "content": "<p>You can create a TTA batch with Numpy, no need to use special libraries. Just take your input data, expand the dimension with one, and fill the batch with modified versions of the input data according to your needs. Run prediction on the batch, and then postprocess the predicted values according to needs. Here is an example  TTA function for images that flips and rotates:  </p>\n<pre><code>def create_TTA_batch(img):\n    if len(img.shape) &lt; 4:\n        img = np.expand_dims(img, 0) # expand data dimension to fit in batch\n\n    batch=np.zeros((img.shape[0]*8,img.shape[1],img.shape[2],img.shape[3]), dtype=np.float32)     \n    for i in range(img.shape[0]):\n        orig = tf.keras.preprocessing.image.img_to_array(img[i,:,:,:])/255. # un-augmented\n        batch[i*8,:,:,:] = orig\n        batch[i*8+1,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)\n        batch[i*8+2,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)\n        batch[i*8+3,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)\n        orig = orig[:, ::-1]\n        batch[i*8+4,:,:,:] = orig\n        batch[i*8+5,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)\n        batch[i*8+6,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)\n        batch[i*8+7,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)\n    return batch\n</code></pre>\n<p>You can used the same principle for any type of data.</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 1324067,
      "author_name": "Ankit",
      "author_url": "",
      "post_date": "2021-05-26T15:34:57.090000",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/mithilsalunkhe\" target=\"_blank\">@mithilsalunkhe</a> , Below are the steps that you can try.</p>\n<p>Step 1 : You can use ClassPredictor of the edafa library<br>\nStep 2 : Inherit predictor class and implement the main function predict_patches(self,patches)<br>\nclass myPredictor(ClassPredictor):</p>\n<pre><code>def __init__(self,model,*args,**kwargs):\n    super().__init__(*args,**kwargs)\n    self.model = model\ndef predict_patches(self,patches):\n    return self.model.predict(patches)\n</code></pre>\n<p>Step 3: Instantiate your class with configuration and whatever parameters needed<br>\nconf = '{\"augs\":[\"NO\",\\<br>\n                \"FLIP_LR\"],\\<br>\n        \"mean\":\"ARITH\"}<br>\nStep 4: Predict images.</p>",
      "votes": 0,
      "replies": [
        {
          "id": 1324082,
          "author_name": "Mithil Salunkhe",
          "author_url": "",
          "post_date": "2021-05-26T15:47:15.300000",
          "content": "<p>Thank You for Replying To me. But  i think this steps are for pytorch. My question asked that how to implement in Tensorflow</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1324095,
          "author_name": "Ankit",
          "author_url": "",
          "post_date": "2021-05-26T15:58:34.757000",
          "content": "<p>Yes, This is for TensorFlow only. You need to instantiate the ClassPrredictor.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1324107,
          "author_name": "PC Jimmmy",
          "author_url": "",
          "post_date": "2021-05-26T16:14:49.147000",
          "content": "<p>Check out the edafa page - <a href=\"https://pypi.org/project/edafa/\" target=\"_blank\">https://pypi.org/project/edafa/</a></p>",
          "votes": 1,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1324182": "You can create a TTA batch with Numpy, no need to use special libraries. Just take your input data, expand the dimension with one, and fill the batch with modified versions of the input data according to your needs. Run prediction on the batch, and then postprocess the predicted values according to needs. Here is an example  TTA function for images that flips and rotates:  \n\n```\ndef create_TTA_batch(img):\n    if len(img.shape) < 4:\n        img = np.expand_dims(img, 0) # expand data dimension to fit in batch\n            \n    batch=np.zeros((img.shape[0]*8,img.shape[1],img.shape[2],img.shape[3]), dtype=np.float32)     \n    for i in range(img.shape[0]):\n        orig = tf.keras.preprocessing.image.img_to_array(img[i,:,:,:])/255. # un-augmented\n        batch[i*8,:,:,:] = orig\n        batch[i*8+1,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)\n        batch[i*8+2,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)\n        batch[i*8+3,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)\n        orig = orig[:, ::-1]\n        batch[i*8+4,:,:,:] = orig\n        batch[i*8+5,:,:,:] = np.rot90(orig, axes=(0, 1), k=1)\n        batch[i*8+6,:,:,:] = np.rot90(orig, axes=(0, 1), k=2)\n        batch[i*8+7,:,:,:] = np.rot90(orig, axes=(0, 1), k=3)\n    return batch\n```\nYou can used the same principle for any type of data.\n",
    "1323525": "I am having some difficulties in implementing TTA. Can someone please help me out in how to implement TTA in tensorflow.Any help is appreciated ",
    "1324067": "Hi @mithilsalunkhe , Below are the steps that you can try.\n\nStep 1 : You can use ClassPredictor of the edafa library\nStep 2 : Inherit predictor class and implement the main function predict_patches(self,patches)\nclass myPredictor(ClassPredictor):\n\n    def __init__(self,model,*args,**kwargs):\n        super().__init__(*args,**kwargs)\n        self.model = model\n    def predict_patches(self,patches):\n        return self.model.predict(patches)\n\nStep 3: Instantiate your class with configuration and whatever parameters needed\nconf = '{\"augs\":[\"NO\",\\\n                \"FLIP_LR\"],\\\n        \"mean\":\"ARITH\"}\nStep 4: Predict images."
  }
}