{
  "id": 408177,
  "title": "Data augmentation in pre-processing",
  "url": "/competitions/benetech-making-graphs-accessible/discussion/408177",
  "author_name": "Tomonori Sasaki",
  "post_date": "2023-05-09T21:16:08.763000",
  "votes": 2,
  "comment_count": 4,
  "views": 0,
  "content": "<p>To improve prediction accuracy, it may be useful to use data extensions within the pre-processing function. If this idea is correct, what would be the variety of images that the model should address in a more appropriate way?<br>\nI assume a random Since it is a graph, inversions and rotations may not have much significance.<br>\nAs a test, we modified the preprocessing function in <a href=\"https://www.kaggle.com/code/nbroad/donut-infer-lb-0-44-benetech\" target=\"_blank\">🍩 donut-infer (LB 0.44) [benetech]</a> as follows</p>\n<pre><code> ():\n    pixel_values = []\n\n    \n    transform = transforms.Compose([\n        transforms.RandomHorizontalFlip(),\n        transforms.RandomRotation(),\n        transforms.RandomResizedCrop((, ), scale=(, )),\n        transforms.ColorJitter(brightness=, contrast=, saturation=, hue=),\n    ])\n\n     sample  examples[]:\n        arr = np.array(sample)\n\n        \n        \n         (arr.shape) == :\n            ()\n            (arr.shape)\n            arr = np.stack([arr]*, axis=-)\n\n        \n        arr = Image.fromarray(arr)\n\n        \n        arr = transform(arr)\n\n        \n        arr = np.array(arr)\n\n        pixel_values.append(processor(arr, random_padding=).pixel_values)\n\n     {\n        : torch.tensor(np.vstack(pixel_values)),\n    }\n</code></pre>\n<p>However, although we tried and saw this correction with data expansion, the scores deteriorated rapidly. Is it wrong that we did it with trained data?Is it not a good idea to begin with?<br>\nI would be grateful for your wisdom</p>",
  "messages": [
    {
      "id": 2252106,
      "postDate": "2023-05-09T23:11:01.847Z",
      "content": "<p>If you crop the image, the x or y-axis values could be gone.<br>\nIn real life, we don't have any rotated charts.<br>\nAlso, it's rare to see a flipped plot in the test images.<br>\nI believe the y-axis would be on the left side of the plot. <br>\nThat's just my opinion.</p>",
      "rawMarkdown": "If you crop the image, the x or y-axis values could be gone.\nIn real life, we don't have any rotated charts.\nAlso, it's rare to see a flipped plot in the test images.\nI believe the y-axis would be on the left side of the plot. \nThat's just my opinion.",
      "votes": 3,
      "replies": [
        {
          "id": 2254643,
          "postDate": "2023-05-11T06:23:49.863Z",
          "content": "<p>I appreciate your comments. It is certainly pointless for general data expansion.</p>",
          "rawMarkdown": "I appreciate your comments. It is certainly pointless for general data expansion."
        }
      ]
    },
    {
      "id": 2252063,
      "postDate": "2023-05-09T21:16:08.763Z",
      "content": "<p>To improve prediction accuracy, it may be useful to use data extensions within the pre-processing function. If this idea is correct, what would be the variety of images that the model should address in a more appropriate way?<br>\nI assume a random Since it is a graph, inversions and rotations may not have much significance.<br>\nAs a test, we modified the preprocessing function in <a href=\"https://www.kaggle.com/code/nbroad/donut-infer-lb-0-44-benetech\" target=\"_blank\">🍩 donut-infer (LB 0.44) [benetech]</a> as follows</p>\n<pre><code> ():\n    pixel_values = []\n\n    \n    transform = transforms.Compose([\n        transforms.RandomHorizontalFlip(),\n        transforms.RandomRotation(),\n        transforms.RandomResizedCrop((, ), scale=(, )),\n        transforms.ColorJitter(brightness=, contrast=, saturation=, hue=),\n    ])\n\n     sample  examples[]:\n        arr = np.array(sample)\n\n        \n        \n         (arr.shape) == :\n            ()\n            (arr.shape)\n            arr = np.stack([arr]*, axis=-)\n\n        \n        arr = Image.fromarray(arr)\n\n        \n        arr = transform(arr)\n\n        \n        arr = np.array(arr)\n\n        pixel_values.append(processor(arr, random_padding=).pixel_values)\n\n     {\n        : torch.tensor(np.vstack(pixel_values)),\n    }\n</code></pre>\n<p>However, although we tried and saw this correction with data expansion, the scores deteriorated rapidly. Is it wrong that we did it with trained data?Is it not a good idea to begin with?<br>\nI would be grateful for your wisdom</p>",
      "rawMarkdown": "To improve prediction accuracy, it may be useful to use data extensions within the pre-processing function. If this idea is correct, what would be the variety of images that the model should address in a more appropriate way?\nI assume a random Since it is a graph, inversions and rotations may not have much significance.\nAs a test, we modified the preprocessing function in [🍩 donut-infer (LB 0.44) [benetech]](https://www.kaggle.com/code/nbroad/donut-infer-lb-0-44-benetech) as follows\n\n```python\ndef preprocess(examples, processor):\n    pixel_values = []\n\n    # Data augmentation\n    transform = transforms.Compose([\n        transforms.RandomHorizontalFlip(),\n        transforms.RandomRotation(10),\n        transforms.RandomResizedCrop((224, 224), scale=(0.8, 1.0)),\n        transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1),\n    ])\n\n    for sample in examples[\"image_path\"]:\n        arr = np.array(sample)\n        \n        # There are some grayscale images that were making this fail\n        # This prevents that.\n        if len(arr.shape) == 2:\n            print(\"Changing grayscale to 3 channel format\")\n            print(arr.shape)\n            arr = np.stack([arr]*3, axis=-1)\n        \n        # Convert numpy array to PIL Image\n        arr = Image.fromarray(arr)\n        \n        # Apply data augmentation\n        arr = transform(arr)\n        \n        # Convert PIL Image back to numpy array\n        arr = np.array(arr)\n        \n        pixel_values.append(processor(arr, random_padding=True).pixel_values)\n        \n    return {\n        \"pixel_values\": torch.tensor(np.vstack(pixel_values)),\n    }\n```\nHowever, although we tried and saw this correction with data expansion, the scores deteriorated rapidly. Is it wrong that we did it with trained data?Is it not a good idea to begin with?\nI would be grateful for your wisdom",
      "votes": 1
    },
    {
      "id": 2254259,
      "postDate": "2023-05-10T19:04:52.800Z",
      "content": "<p>In the train set, generated and extracted images could be very different. This has already been discussed somewhere. </p>\n<p>For example, the scatter plots, when you look at maximum and minimum x-axis values of data ('data-series'), the ~100 extracted images have a very different distribution than ~11k generated images.<br>\nMaybe it would be good to generate more scatter images such that their x-values more resemble the x-values of the ~100 extracted images. I guess this can be done by simply duplicating some images or adding noise to them.</p>",
      "rawMarkdown": "In the train set, generated and extracted images could be very different. This has already been discussed somewhere. \n\nFor example, the scatter plots, when you look at maximum and minimum x-axis values of data ('data-series'), the ~100 extracted images have a very different distribution than ~11k generated images.\nMaybe it would be good to generate more scatter images such that their x-values more resemble the x-values of the ~100 extracted images. I guess this can be done by simply duplicating some images or adding noise to them.",
      "votes": 1,
      "isDeleted": true,
      "replies": [
        {
          "id": 2254645,
          "postDate": "2023-05-11T06:24:54.803Z",
          "content": "<p>We appreciate your comments. We will read the other discussions carefully. Thank you also for your excellent ideas on data extensions.</p>",
          "rawMarkdown": "We appreciate your comments. We will read the other discussions carefully. Thank you also for your excellent ideas on data extensions."
        }
      ]
    }
  ],
  "comments": [
    {
      "id": 2252106,
      "author_name": "june",
      "author_url": "",
      "post_date": "2023-05-09T23:11:01.847000",
      "content": "<p>If you crop the image, the x or y-axis values could be gone.<br>\nIn real life, we don't have any rotated charts.<br>\nAlso, it's rare to see a flipped plot in the test images.<br>\nI believe the y-axis would be on the left side of the plot. <br>\nThat's just my opinion.</p>",
      "votes": 3,
      "replies": [
        {
          "id": 2254643,
          "author_name": "Tomonori Sasaki",
          "author_url": "",
          "post_date": "2023-05-11T06:23:49.863000",
          "content": "<p>I appreciate your comments. It is certainly pointless for general data expansion.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 2254259,
      "author_name": "",
      "author_url": "",
      "post_date": "2023-05-10T19:04:52.800000",
      "content": "<p>In the train set, generated and extracted images could be very different. This has already been discussed somewhere. </p>\n<p>For example, the scatter plots, when you look at maximum and minimum x-axis values of data ('data-series'), the ~100 extracted images have a very different distribution than ~11k generated images.<br>\nMaybe it would be good to generate more scatter images such that their x-values more resemble the x-values of the ~100 extracted images. I guess this can be done by simply duplicating some images or adding noise to them.</p>",
      "votes": 1,
      "replies": [
        {
          "id": 2254645,
          "author_name": "Tomonori Sasaki",
          "author_url": "",
          "post_date": "2023-05-11T06:24:54.803000",
          "content": "<p>We appreciate your comments. We will read the other discussions carefully. Thank you also for your excellent ideas on data extensions.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2252106": "If you crop the image, the x or y-axis values could be gone.\nIn real life, we don't have any rotated charts.\nAlso, it's rare to see a flipped plot in the test images.\nI believe the y-axis would be on the left side of the plot. \nThat's just my opinion.",
    "2252063": "To improve prediction accuracy, it may be useful to use data extensions within the pre-processing function. If this idea is correct, what would be the variety of images that the model should address in a more appropriate way?\nI assume a random Since it is a graph, inversions and rotations may not have much significance.\nAs a test, we modified the preprocessing function in [🍩 donut-infer (LB 0.44) [benetech]](https://www.kaggle.com/code/nbroad/donut-infer-lb-0-44-benetech) as follows\n\n```python\ndef preprocess(examples, processor):\n    pixel_values = []\n\n    # Data augmentation\n    transform = transforms.Compose([\n        transforms.RandomHorizontalFlip(),\n        transforms.RandomRotation(10),\n        transforms.RandomResizedCrop((224, 224), scale=(0.8, 1.0)),\n        transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1),\n    ])\n\n    for sample in examples[\"image_path\"]:\n        arr = np.array(sample)\n        \n        # There are some grayscale images that were making this fail\n        # This prevents that.\n        if len(arr.shape) == 2:\n            print(\"Changing grayscale to 3 channel format\")\n            print(arr.shape)\n            arr = np.stack([arr]*3, axis=-1)\n        \n        # Convert numpy array to PIL Image\n        arr = Image.fromarray(arr)\n        \n        # Apply data augmentation\n        arr = transform(arr)\n        \n        # Convert PIL Image back to numpy array\n        arr = np.array(arr)\n        \n        pixel_values.append(processor(arr, random_padding=True).pixel_values)\n        \n    return {\n        \"pixel_values\": torch.tensor(np.vstack(pixel_values)),\n    }\n```\nHowever, although we tried and saw this correction with data expansion, the scores deteriorated rapidly. Is it wrong that we did it with trained data?Is it not a good idea to begin with?\nI would be grateful for your wisdom",
    "2254259": "In the train set, generated and extracted images could be very different. This has already been discussed somewhere. \n\nFor example, the scatter plots, when you look at maximum and minimum x-axis values of data ('data-series'), the ~100 extracted images have a very different distribution than ~11k generated images.\nMaybe it would be good to generate more scatter images such that their x-values more resemble the x-values of the ~100 extracted images. I guess this can be done by simply duplicating some images or adding noise to them."
  }
}