We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Use cutout for image data


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `autokeras/constant.py`
Content:
```
1 class Constant:
2     # Data
3 
4     VALIDATION_SET_SIZE = 0.08333
5 
6     # Searcher
7 
8     MAX_MODEL_NUM = 1000
9     BETA = 2.576
10     KERNEL_LAMBDA = 0.1
11     T_MIN = 0.0001
12     N_NEIGHBOURS = 8
13     MAX_MODEL_SIZE = (1 << 25)
14     MAX_LAYER_WIDTH = 4096
15     MAX_LAYERS = 100
16 
17     # Model Defaults
18 
19     DENSE_DROPOUT_RATE = 0.5
20     CONV_DROPOUT_RATE = 0.25
21     MLP_DROPOUT_RATE = 0.25
22     CONV_BLOCK_DISTANCE = 2
23     DENSE_BLOCK_DISTANCE = 1
24     MODEL_LEN = 3
25     MLP_MODEL_LEN = 3
26     MLP_MODEL_WIDTH = 5
27     MODEL_WIDTH = 64
28 
29     # ModelTrainer
30 
31     DATA_AUGMENTATION = True
32     MAX_ITER_NUM = 200
33     MIN_LOSS_DEC = 1e-4
34     MAX_NO_IMPROVEMENT_NUM = 5
35     MAX_BATCH_SIZE = 128
36     LIMIT_MEMORY = False
37     SEARCH_MAX_ITER = 200
38 
39     # text preprocessor
40 
41     EMBEDDING_DIM = 100
42     MAX_SEQUENCE_LENGTH = 400
43     MAX_NB_WORDS = 5000
44     EXTRACT_PATH = "glove/"
45     # Download file name
46     FILE_PATH = "glove.zip"
47     PRE_TRAIN_FILE_LINK = "http://nlp.stanford.edu/data/glove.6B.zip"
48     PRE_TRAIN_FILE_NAME = "glove.6B.100d.txt"
49
```

Path: `autokeras/preprocessor.py`
Content:
```
1 import os
2 import numpy as np
3 import torch
4 from torch.utils.data import Dataset, DataLoader
5 from torchvision.transforms import ToPILImage, RandomCrop, RandomHorizontalFlip, ToTensor, Normalize, Compose
6 from abc import ABC, abstractmethod
7 from autokeras.constant import Constant
8 from autokeras.utils import read_csv_file, read_image
9 
10 
11 class OneHotEncoder:
12     """A class that can format data.
13 
14     This class provides ways to transform data's classification label into vector.
15 
16     Attributes:
17           data: The input data
18           n_classes: The number of classes in the classification problem.
19           labels: The number of labels.
20           label_to_vec: Mapping from label to vector.
21           int_to_label: Mapping from int to label.
22     """
23 
24     def __init__(self):
25         """Initialize a OneHotEncoder"""
26         self.data = None
27         self.n_classes = 0
28         self.labels = None
29         self.label_to_vec = {}
30         self.int_to_label = {}
31 
32     def fit(self, data):
33         """Create mapping from label to vector, and vector to label."""
34         data = np.array(data).flatten()
35         self.labels = set(data)
36         self.n_classes = len(self.labels)
37         for index, label in enumerate(self.labels):
38             vec = np.array([0] * self.n_classes)
39             vec[index] = 1
40             self.label_to_vec[label] = vec
41             self.int_to_label[index] = label
42 
43     def transform(self, data):
44         """Get vector for every element in the data array."""
45         data = np.array(data)
46         if len(data.shape) > 1:
47             data = data.flatten()
48         return np.array(list(map(lambda x: self.label_to_vec[x], data)))
49 
50     def inverse_transform(self, data):
51         """Get label for every element in data."""
52         return np.array(list(map(lambda x: self.int_to_label[x], np.argmax(np.array(data), axis=1))))
53 
54 
55 class DataTransformer(ABC):
56     @abstractmethod
57     def transform_train(self, data, targets=None, batch_size=None):
58         raise NotImplementedError
59 
60     @abstractmethod
61     def transform_test(self, data, target=None, batch_size=None):
62         raise NotImplementedError
63 
64 
65 class TextDataTransformer(DataTransformer):
66 
67     def transform_train(self, data, targets=None, batch_size=None):
68         dataset = self._transform(compose_list=[], data=data, targets=targets)
69         if batch_size is None:
70             batch_size = Constant.MAX_BATCH_SIZE
71         batch_size = min(len(data), batch_size)
72 
73         return DataLoader(dataset, batch_size=batch_size, shuffle=True)
74 
75     def transform_test(self, data, targets=None, batch_size=None):
76         dataset = self._transform(compose_list=[], data=data, targets=targets)
77         if batch_size is None:
78             batch_size = Constant.MAX_BATCH_SIZE
79         batch_size = min(len(data), batch_size)
80 
81         return DataLoader(dataset, batch_size=batch_size, shuffle=True)
82 
83     def _transform(self, compose_list, data, targets):
84         data = torch.Tensor(data.transpose(0, 3, 1, 2))
85         data_transforms = Compose(compose_list)
86         return MultiTransformDataset(data, targets, data_transforms)
87 
88 
89 class ImageDataTransformer(DataTransformer):
90     def __init__(self, data, augment=Constant.DATA_AUGMENTATION):
91         self.max_val = data.max()
92         data = data / self.max_val
93         self.mean = np.mean(data, axis=(0, 1, 2), keepdims=True).flatten()
94         self.std = np.std(data, axis=(0, 1, 2), keepdims=True).flatten()
95         self.augment = augment
96 
97     def transform_train(self, data, targets=None, batch_size=None):
98         if not self.augment:
99             augment_list = []
100         else:
101             augment_list = [ToPILImage(),
102                             RandomCrop(data.shape[1:3], padding=4),
103                             RandomHorizontalFlip(),
104                             ToTensor()
105                             ]
106         common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]
107         compose_list = augment_list + common_list
108 
109         dataset = self._transform(compose_list, data, targets)
110 
111         if batch_size is None:
112             batch_size = Constant.MAX_BATCH_SIZE
113         batch_size = min(len(data), batch_size)
114 
115         return DataLoader(dataset, batch_size=batch_size, shuffle=True)
116 
117     def transform_test(self, data, targets=None, batch_size=None):
118         common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]
119         compose_list = common_list
120 
121         dataset = self._transform(compose_list, data, targets)
122 
123         if batch_size is None:
124             batch_size = Constant.MAX_BATCH_SIZE
125         batch_size = min(len(data), batch_size)
126 
127         return DataLoader(dataset, batch_size=batch_size, shuffle=False)
128 
129     def _transform(self, compose_list, data, targets):
130         data = data / self.max_val
131         data = torch.Tensor(data.transpose(0, 3, 1, 2))
132         data_transforms = Compose(compose_list)
133         return MultiTransformDataset(data, targets, data_transforms)
134 
135 
136 class MultiTransformDataset(Dataset):
137     def __init__(self, dataset, target, compose):
138         self.dataset = dataset
139         self.target = target
140         self.compose = compose
141 
142     def __getitem__(self, index):
143         feature = self.dataset[index]
144         if self.target is None:
145             return self.compose(feature)
146         return self.compose(feature), self.target[index]
147 
148     def __len__(self):
149         return len(self.dataset)
150 
151 
152 class BatchDataset(Dataset):
153     def __init__(self, csv_file_path, image_path, has_target=True):
154         file_names, target = read_csv_file(csv_file_path)
155 
156         self.y_encoder = OneHotEncoder()
157         self.y_encoder.fit(target)
158         target = self.y_encoder.transform(target)
159 
160         self.target = target
161         self.has_target = has_target
162         self.file_paths = list(map(lambda file_name: os.path.join(image_path, file_name), file_names))
163 
164     def __getitem__(self, index):
165         image = read_image(self.file_paths[index])
166         if len(image.shape) < 3:
167             image = image[..., np.newaxis]
168         image = torch.Tensor(image.transpose(2, 0, 1))
169         if self.has_target:
170             return image, self.target[index]
171         return image
172 
173     def __len__(self):
174         return len(self.file_paths)
175
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/autokeras/constant.py b/autokeras/constant.py
--- a/autokeras/constant.py
+++ b/autokeras/constant.py
@@ -2,6 +2,8 @@
     # Data
 
     VALIDATION_SET_SIZE = 0.08333
+    CUTOUT_HOLES = 1
+    CUTOUT_RATIO = 0.5
 
     # Searcher
 
diff --git a/autokeras/preprocessor.py b/autokeras/preprocessor.py
--- a/autokeras/preprocessor.py
+++ b/autokeras/preprocessor.py
@@ -52,6 +52,47 @@
         return np.array(list(map(lambda x: self.int_to_label[x], np.argmax(np.array(data), axis=1))))
 
 
+class Cutout(object):
+    """Randomly mask out one or more patches from an image.
+    Args:
+        n_holes (int): Number of patches to cut out of each image.
+        length (int): The length (in pixels) of each square patch.
+    """
+
+    def __init__(self, n_holes, length):
+        self.n_holes = n_holes
+        self.length = length
+
+    def __call__(self, img):
+        """
+        Args:
+            img (Tensor): Tensor image of size (C, H, W).
+        Returns:
+            Tensor: Image with n_holes of dimension length x length cut out of it.
+        """
+        h = img.size(1)
+        w = img.size(2)
+
+        mask = np.ones((h, w), np.float32)
+
+        for n in range(self.n_holes):
+            y = np.random.randint(h)
+            x = np.random.randint(w)
+
+            y1 = np.clip(y - self.length // 2, 0, h)
+            y2 = np.clip(y + self.length // 2, 0, h)
+            x1 = np.clip(x - self.length // 2, 0, w)
+            x2 = np.clip(x + self.length // 2, 0, w)
+
+            mask[y1: y2, x1: x2] = 0.
+
+        mask = torch.from_numpy(mask)
+        mask = mask.expand_as(img)
+        img = img * mask
+
+        return img
+
+
 class DataTransformer(ABC):
     @abstractmethod
     def transform_train(self, data, targets=None, batch_size=None):
@@ -95,16 +136,17 @@
         self.augment = augment
 
     def transform_train(self, data, targets=None, batch_size=None):
-        if not self.augment:
-            augment_list = []
-        else:
-            augment_list = [ToPILImage(),
+        short_edge_length = min(data.shape[1], data.shape[2])
+        common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]
+        if self.augment:
+            compose_list = [ToPILImage(),
                             RandomCrop(data.shape[1:3], padding=4),
                             RandomHorizontalFlip(),
                             ToTensor()
-                            ]
-        common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]
-        compose_list = augment_list + common_list
+                            ] + common_list + [Cutout(n_holes=Constant.CUTOUT_HOLES,
+                                                      length=int(short_edge_length * Constant.CUTOUT_RATIO))]
+        else:
+            compose_list = common_list
 
         dataset = self._transform(compose_list, data, targets)

{"golden_diff": "diff --git a/autokeras/constant.py b/autokeras/constant.py\n--- a/autokeras/constant.py\n+++ b/autokeras/constant.py\n@@ -2,6 +2,8 @@\n     # Data\n \n     VALIDATION_SET_SIZE = 0.08333\n+    CUTOUT_HOLES = 1\n+    CUTOUT_RATIO = 0.5\n \n     # Searcher\n \ndiff --git a/autokeras/preprocessor.py b/autokeras/preprocessor.py\n--- a/autokeras/preprocessor.py\n+++ b/autokeras/preprocessor.py\n@@ -52,6 +52,47 @@\n         return np.array(list(map(lambda x: self.int_to_label[x], np.argmax(np.array(data), axis=1))))\n \n \n+class Cutout(object):\n+    \"\"\"Randomly mask out one or more patches from an image.\n+    Args:\n+        n_holes (int): Number of patches to cut out of each image.\n+        length (int): The length (in pixels) of each square patch.\n+    \"\"\"\n+\n+    def __init__(self, n_holes, length):\n+        self.n_holes = n_holes\n+        self.length = length\n+\n+    def __call__(self, img):\n+        \"\"\"\n+        Args:\n+            img (Tensor): Tensor image of size (C, H, W).\n+        Returns:\n+            Tensor: Image with n_holes of dimension length x length cut out of it.\n+        \"\"\"\n+        h = img.size(1)\n+        w = img.size(2)\n+\n+        mask = np.ones((h, w), np.float32)\n+\n+        for n in range(self.n_holes):\n+            y = np.random.randint(h)\n+            x = np.random.randint(w)\n+\n+            y1 = np.clip(y - self.length // 2, 0, h)\n+            y2 = np.clip(y + self.length // 2, 0, h)\n+            x1 = np.clip(x - self.length // 2, 0, w)\n+            x2 = np.clip(x + self.length // 2, 0, w)\n+\n+            mask[y1: y2, x1: x2] = 0.\n+\n+        mask = torch.from_numpy(mask)\n+        mask = mask.expand_as(img)\n+        img = img * mask\n+\n+        return img\n+\n+\n class DataTransformer(ABC):\n     @abstractmethod\n     def transform_train(self, data, targets=None, batch_size=None):\n@@ -95,16 +136,17 @@\n         self.augment = augment\n \n     def transform_train(self, data, targets=None, batch_size=None):\n-        if not self.augment:\n-            augment_list = []\n-        else:\n-            augment_list = [ToPILImage(),\n+        short_edge_length = min(data.shape[1], data.shape[2])\n+        common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]\n+        if self.augment:\n+            compose_list = [ToPILImage(),\n                             RandomCrop(data.shape[1:3], padding=4),\n                             RandomHorizontalFlip(),\n                             ToTensor()\n-                            ]\n-        common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]\n-        compose_list = augment_list + common_list\n+                            ] + common_list + [Cutout(n_holes=Constant.CUTOUT_HOLES,\n+                                                      length=int(short_edge_length * Constant.CUTOUT_RATIO))]\n+        else:\n+            compose_list = common_list\n \n         dataset = self._transform(compose_list, data, targets)\n", "issue": "Use cutout for image data\n\n", "before_files": [{"content": "class Constant:\n    # Data\n\n    VALIDATION_SET_SIZE = 0.08333\n\n    # Searcher\n\n    MAX_MODEL_NUM = 1000\n    BETA = 2.576\n    KERNEL_LAMBDA = 0.1\n    T_MIN = 0.0001\n    N_NEIGHBOURS = 8\n    MAX_MODEL_SIZE = (1 << 25)\n    MAX_LAYER_WIDTH = 4096\n    MAX_LAYERS = 100\n\n    # Model Defaults\n\n    DENSE_DROPOUT_RATE = 0.5\n    CONV_DROPOUT_RATE = 0.25\n    MLP_DROPOUT_RATE = 0.25\n    CONV_BLOCK_DISTANCE = 2\n    DENSE_BLOCK_DISTANCE = 1\n    MODEL_LEN = 3\n    MLP_MODEL_LEN = 3\n    MLP_MODEL_WIDTH = 5\n    MODEL_WIDTH = 64\n\n    # ModelTrainer\n\n    DATA_AUGMENTATION = True\n    MAX_ITER_NUM = 200\n    MIN_LOSS_DEC = 1e-4\n    MAX_NO_IMPROVEMENT_NUM = 5\n    MAX_BATCH_SIZE = 128\n    LIMIT_MEMORY = False\n    SEARCH_MAX_ITER = 200\n\n    # text preprocessor\n\n    EMBEDDING_DIM = 100\n    MAX_SEQUENCE_LENGTH = 400\n    MAX_NB_WORDS = 5000\n    EXTRACT_PATH = \"glove/\"\n    # Download file name\n    FILE_PATH = \"glove.zip\"\n    PRE_TRAIN_FILE_LINK = \"http://nlp.stanford.edu/data/glove.6B.zip\"\n    PRE_TRAIN_FILE_NAME = \"glove.6B.100d.txt\"\n", "path": "autokeras/constant.py"}, {"content": "import os\nimport numpy as np\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.transforms import ToPILImage, RandomCrop, RandomHorizontalFlip, ToTensor, Normalize, Compose\nfrom abc import ABC, abstractmethod\nfrom autokeras.constant import Constant\nfrom autokeras.utils import read_csv_file, read_image\n\n\nclass OneHotEncoder:\n    \"\"\"A class that can format data.\n\n    This class provides ways to transform data's classification label into vector.\n\n    Attributes:\n          data: The input data\n          n_classes: The number of classes in the classification problem.\n          labels: The number of labels.\n          label_to_vec: Mapping from label to vector.\n          int_to_label: Mapping from int to label.\n    \"\"\"\n\n    def __init__(self):\n        \"\"\"Initialize a OneHotEncoder\"\"\"\n        self.data = None\n        self.n_classes = 0\n        self.labels = None\n        self.label_to_vec = {}\n        self.int_to_label = {}\n\n    def fit(self, data):\n        \"\"\"Create mapping from label to vector, and vector to label.\"\"\"\n        data = np.array(data).flatten()\n        self.labels = set(data)\n        self.n_classes = len(self.labels)\n        for index, label in enumerate(self.labels):\n            vec = np.array([0] * self.n_classes)\n            vec[index] = 1\n            self.label_to_vec[label] = vec\n            self.int_to_label[index] = label\n\n    def transform(self, data):\n        \"\"\"Get vector for every element in the data array.\"\"\"\n        data = np.array(data)\n        if len(data.shape) > 1:\n            data = data.flatten()\n        return np.array(list(map(lambda x: self.label_to_vec[x], data)))\n\n    def inverse_transform(self, data):\n        \"\"\"Get label for every element in data.\"\"\"\n        return np.array(list(map(lambda x: self.int_to_label[x], np.argmax(np.array(data), axis=1))))\n\n\nclass DataTransformer(ABC):\n    @abstractmethod\n    def transform_train(self, data, targets=None, batch_size=None):\n        raise NotImplementedError\n\n    @abstractmethod\n    def transform_test(self, data, target=None, batch_size=None):\n        raise NotImplementedError\n\n\nclass TextDataTransformer(DataTransformer):\n\n    def transform_train(self, data, targets=None, batch_size=None):\n        dataset = self._transform(compose_list=[], data=data, targets=targets)\n        if batch_size is None:\n            batch_size = Constant.MAX_BATCH_SIZE\n        batch_size = min(len(data), batch_size)\n\n        return DataLoader(dataset, batch_size=batch_size, shuffle=True)\n\n    def transform_test(self, data, targets=None, batch_size=None):\n        dataset = self._transform(compose_list=[], data=data, targets=targets)\n        if batch_size is None:\n            batch_size = Constant.MAX_BATCH_SIZE\n        batch_size = min(len(data), batch_size)\n\n        return DataLoader(dataset, batch_size=batch_size, shuffle=True)\n\n    def _transform(self, compose_list, data, targets):\n        data = torch.Tensor(data.transpose(0, 3, 1, 2))\n        data_transforms = Compose(compose_list)\n        return MultiTransformDataset(data, targets, data_transforms)\n\n\nclass ImageDataTransformer(DataTransformer):\n    def __init__(self, data, augment=Constant.DATA_AUGMENTATION):\n        self.max_val = data.max()\n        data = data / self.max_val\n        self.mean = np.mean(data, axis=(0, 1, 2), keepdims=True).flatten()\n        self.std = np.std(data, axis=(0, 1, 2), keepdims=True).flatten()\n        self.augment = augment\n\n    def transform_train(self, data, targets=None, batch_size=None):\n        if not self.augment:\n            augment_list = []\n        else:\n            augment_list = [ToPILImage(),\n                            RandomCrop(data.shape[1:3], padding=4),\n                            RandomHorizontalFlip(),\n                            ToTensor()\n                            ]\n        common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]\n        compose_list = augment_list + common_list\n\n        dataset = self._transform(compose_list, data, targets)\n\n        if batch_size is None:\n            batch_size = Constant.MAX_BATCH_SIZE\n        batch_size = min(len(data), batch_size)\n\n        return DataLoader(dataset, batch_size=batch_size, shuffle=True)\n\n    def transform_test(self, data, targets=None, batch_size=None):\n        common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]\n        compose_list = common_list\n\n        dataset = self._transform(compose_list, data, targets)\n\n        if batch_size is None:\n            batch_size = Constant.MAX_BATCH_SIZE\n        batch_size = min(len(data), batch_size)\n\n        return DataLoader(dataset, batch_size=batch_size, shuffle=False)\n\n    def _transform(self, compose_list, data, targets):\n        data = data / self.max_val\n        data = torch.Tensor(data.transpose(0, 3, 1, 2))\n        data_transforms = Compose(compose_list)\n        return MultiTransformDataset(data, targets, data_transforms)\n\n\nclass MultiTransformDataset(Dataset):\n    def __init__(self, dataset, target, compose):\n        self.dataset = dataset\n        self.target = target\n        self.compose = compose\n\n    def __getitem__(self, index):\n        feature = self.dataset[index]\n        if self.target is None:\n            return self.compose(feature)\n        return self.compose(feature), self.target[index]\n\n    def __len__(self):\n        return len(self.dataset)\n\n\nclass BatchDataset(Dataset):\n    def __init__(self, csv_file_path, image_path, has_target=True):\n        file_names, target = read_csv_file(csv_file_path)\n\n        self.y_encoder = OneHotEncoder()\n        self.y_encoder.fit(target)\n        target = self.y_encoder.transform(target)\n\n        self.target = target\n        self.has_target = has_target\n        self.file_paths = list(map(lambda file_name: os.path.join(image_path, file_name), file_names))\n\n    def __getitem__(self, index):\n        image = read_image(self.file_paths[index])\n        if len(image.shape) < 3:\n            image = image[..., np.newaxis]\n        image = torch.Tensor(image.transpose(2, 0, 1))\n        if self.has_target:\n            return image, self.target[index]\n        return image\n\n    def __len__(self):\n        return len(self.file_paths)\n", "path": "autokeras/preprocessor.py"}], "after_files": [{"content": "class Constant:\n    # Data\n\n    VALIDATION_SET_SIZE = 0.08333\n    CUTOUT_HOLES = 1\n    CUTOUT_RATIO = 0.5\n\n    # Searcher\n\n    MAX_MODEL_NUM = 1000\n    BETA = 2.576\n    KERNEL_LAMBDA = 0.1\n    T_MIN = 0.0001\n    N_NEIGHBOURS = 8\n    MAX_MODEL_SIZE = (1 << 25)\n    MAX_LAYER_WIDTH = 4096\n    MAX_LAYERS = 100\n\n    # Model Defaults\n\n    DENSE_DROPOUT_RATE = 0.5\n    CONV_DROPOUT_RATE = 0.25\n    MLP_DROPOUT_RATE = 0.25\n    CONV_BLOCK_DISTANCE = 2\n    DENSE_BLOCK_DISTANCE = 1\n    MODEL_LEN = 3\n    MLP_MODEL_LEN = 3\n    MLP_MODEL_WIDTH = 5\n    MODEL_WIDTH = 64\n\n    # ModelTrainer\n\n    DATA_AUGMENTATION = True\n    MAX_ITER_NUM = 200\n    MIN_LOSS_DEC = 1e-4\n    MAX_NO_IMPROVEMENT_NUM = 5\n    MAX_BATCH_SIZE = 128\n    LIMIT_MEMORY = False\n    SEARCH_MAX_ITER = 200\n\n    # text preprocessor\n\n    EMBEDDING_DIM = 100\n    MAX_SEQUENCE_LENGTH = 400\n    MAX_NB_WORDS = 5000\n    EXTRACT_PATH = \"glove/\"\n    # Download file name\n    FILE_PATH = \"glove.zip\"\n    PRE_TRAIN_FILE_LINK = \"http://nlp.stanford.edu/data/glove.6B.zip\"\n    PRE_TRAIN_FILE_NAME = \"glove.6B.100d.txt\"\n", "path": "autokeras/constant.py"}, {"content": "import os\nimport numpy as np\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.transforms import ToPILImage, RandomCrop, RandomHorizontalFlip, ToTensor, Normalize, Compose\nfrom abc import ABC, abstractmethod\nfrom autokeras.constant import Constant\nfrom autokeras.utils import read_csv_file, read_image\n\n\nclass OneHotEncoder:\n    \"\"\"A class that can format data.\n\n    This class provides ways to transform data's classification label into vector.\n\n    Attributes:\n          data: The input data\n          n_classes: The number of classes in the classification problem.\n          labels: The number of labels.\n          label_to_vec: Mapping from label to vector.\n          int_to_label: Mapping from int to label.\n    \"\"\"\n\n    def __init__(self):\n        \"\"\"Initialize a OneHotEncoder\"\"\"\n        self.data = None\n        self.n_classes = 0\n        self.labels = None\n        self.label_to_vec = {}\n        self.int_to_label = {}\n\n    def fit(self, data):\n        \"\"\"Create mapping from label to vector, and vector to label.\"\"\"\n        data = np.array(data).flatten()\n        self.labels = set(data)\n        self.n_classes = len(self.labels)\n        for index, label in enumerate(self.labels):\n            vec = np.array([0] * self.n_classes)\n            vec[index] = 1\n            self.label_to_vec[label] = vec\n            self.int_to_label[index] = label\n\n    def transform(self, data):\n        \"\"\"Get vector for every element in the data array.\"\"\"\n        data = np.array(data)\n        if len(data.shape) > 1:\n            data = data.flatten()\n        return np.array(list(map(lambda x: self.label_to_vec[x], data)))\n\n    def inverse_transform(self, data):\n        \"\"\"Get label for every element in data.\"\"\"\n        return np.array(list(map(lambda x: self.int_to_label[x], np.argmax(np.array(data), axis=1))))\n\n\nclass Cutout(object):\n    \"\"\"Randomly mask out one or more patches from an image.\n    Args:\n        n_holes (int): Number of patches to cut out of each image.\n        length (int): The length (in pixels) of each square patch.\n    \"\"\"\n\n    def __init__(self, n_holes, length):\n        self.n_holes = n_holes\n        self.length = length\n\n    def __call__(self, img):\n        \"\"\"\n        Args:\n            img (Tensor): Tensor image of size (C, H, W).\n        Returns:\n            Tensor: Image with n_holes of dimension length x length cut out of it.\n        \"\"\"\n        h = img.size(1)\n        w = img.size(2)\n\n        mask = np.ones((h, w), np.float32)\n\n        for n in range(self.n_holes):\n            y = np.random.randint(h)\n            x = np.random.randint(w)\n\n            y1 = np.clip(y - self.length // 2, 0, h)\n            y2 = np.clip(y + self.length // 2, 0, h)\n            x1 = np.clip(x - self.length // 2, 0, w)\n            x2 = np.clip(x + self.length // 2, 0, w)\n\n            mask[y1: y2, x1: x2] = 0.\n\n        mask = torch.from_numpy(mask)\n        mask = mask.expand_as(img)\n        img = img * mask\n\n        return img\n\n\nclass DataTransformer(ABC):\n    @abstractmethod\n    def transform_train(self, data, targets=None, batch_size=None):\n        raise NotImplementedError\n\n    @abstractmethod\n    def transform_test(self, data, target=None, batch_size=None):\n        raise NotImplementedError\n\n\nclass TextDataTransformer(DataTransformer):\n\n    def transform_train(self, data, targets=None, batch_size=None):\n        dataset = self._transform(compose_list=[], data=data, targets=targets)\n        if batch_size is None:\n            batch_size = Constant.MAX_BATCH_SIZE\n        batch_size = min(len(data), batch_size)\n\n        return DataLoader(dataset, batch_size=batch_size, shuffle=True)\n\n    def transform_test(self, data, targets=None, batch_size=None):\n        dataset = self._transform(compose_list=[], data=data, targets=targets)\n        if batch_size is None:\n            batch_size = Constant.MAX_BATCH_SIZE\n        batch_size = min(len(data), batch_size)\n\n        return DataLoader(dataset, batch_size=batch_size, shuffle=True)\n\n    def _transform(self, compose_list, data, targets):\n        data = torch.Tensor(data.transpose(0, 3, 1, 2))\n        data_transforms = Compose(compose_list)\n        return MultiTransformDataset(data, targets, data_transforms)\n\n\nclass ImageDataTransformer(DataTransformer):\n    def __init__(self, data, augment=Constant.DATA_AUGMENTATION):\n        self.max_val = data.max()\n        data = data / self.max_val\n        self.mean = np.mean(data, axis=(0, 1, 2), keepdims=True).flatten()\n        self.std = np.std(data, axis=(0, 1, 2), keepdims=True).flatten()\n        self.augment = augment\n\n    def transform_train(self, data, targets=None, batch_size=None):\n        short_edge_length = min(data.shape[1], data.shape[2])\n        common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]\n        if self.augment:\n            compose_list = [ToPILImage(),\n                            RandomCrop(data.shape[1:3], padding=4),\n                            RandomHorizontalFlip(),\n                            ToTensor()\n                            ] + common_list + [Cutout(n_holes=Constant.CUTOUT_HOLES,\n                                                      length=int(short_edge_length * Constant.CUTOUT_RATIO))]\n        else:\n            compose_list = common_list\n\n        dataset = self._transform(compose_list, data, targets)\n\n        if batch_size is None:\n            batch_size = Constant.MAX_BATCH_SIZE\n        batch_size = min(len(data), batch_size)\n\n        return DataLoader(dataset, batch_size=batch_size, shuffle=True)\n\n    def transform_test(self, data, targets=None, batch_size=None):\n        common_list = [Normalize(torch.Tensor(self.mean), torch.Tensor(self.std))]\n        compose_list = common_list\n\n        dataset = self._transform(compose_list, data, targets)\n\n        if batch_size is None:\n            batch_size = Constant.MAX_BATCH_SIZE\n        batch_size = min(len(data), batch_size)\n\n        return DataLoader(dataset, batch_size=batch_size, shuffle=False)\n\n    def _transform(self, compose_list, data, targets):\n        data = data / self.max_val\n        data = torch.Tensor(data.transpose(0, 3, 1, 2))\n        data_transforms = Compose(compose_list)\n        return MultiTransformDataset(data, targets, data_transforms)\n\n\nclass MultiTransformDataset(Dataset):\n    def __init__(self, dataset, target, compose):\n        self.dataset = dataset\n        self.target = target\n        self.compose = compose\n\n    def __getitem__(self, index):\n        feature = self.dataset[index]\n        if self.target is None:\n            return self.compose(feature)\n        return self.compose(feature), self.target[index]\n\n    def __len__(self):\n        return len(self.dataset)\n\n\nclass BatchDataset(Dataset):\n    def __init__(self, csv_file_path, image_path, has_target=True):\n        file_names, target = read_csv_file(csv_file_path)\n\n        self.y_encoder = OneHotEncoder()\n        self.y_encoder.fit(target)\n        target = self.y_encoder.transform(target)\n\n        self.target = target\n        self.has_target = has_target\n        self.file_paths = list(map(lambda file_name: os.path.join(image_path, file_name), file_names))\n\n    def __getitem__(self, index):\n        image = read_image(self.file_paths[index])\n        if len(image.shape) < 3:\n            image = image[..., np.newaxis]\n        image = torch.Tensor(image.transpose(2, 0, 1))\n        if self.has_target:\n            return image, self.target[index]\n        return image\n\n    def __len__(self):\n        return len(self.file_paths)\n", "path": "autokeras/preprocessor.py"}]}