We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
feature_request: Support multicore/multiprocess for read_images function
### Feature Description
Support multicore for `read_images` function in `image/image_supervised.py`.

### Reason
As [read_images function in image_supervised.py](https://github.com/jhfjhfj1/autokeras/blob/36752fcbce58bd1a26c17144637cc0ecbef83da0/autokeras/image/image_supervised.py#L14) does not support multi-core(just read files one by one), so it's **too slow** when reading image files more than 10k files.

### Solution
Use multiprocessing or equivalent one in `read_images` function as a default, or optional.


--- 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/image/image_supervised.py`
Content:
```
1 import os
2 from abc import ABC
3 import numpy as np
4 
5 from autokeras.constant import Constant
6 from autokeras.nn.loss_function import classification_loss, regression_loss
7 from autokeras.nn.metric import Accuracy, MSE
8 from autokeras.preprocessor import OneHotEncoder, ImageDataTransformer
9 from autokeras.supervised import PortableDeepSupervised, DeepSupervised
10 from autokeras.utils import pickle_to_file, \
11     read_csv_file, read_image, compute_image_resize_params, resize_image_data
12 
13 
14 def read_images(img_file_names, images_dir_path):
15     """Read the images from the path and return their numpy.ndarray instance.
16         Return a numpy.ndarray instance containing the training data.
17 
18     Args:
19         img_file_names: List containing images names.
20         images_dir_path: Path to the directory containing images.
21     """
22     x_train = []
23     if os.path.isdir(images_dir_path):
24         for img_file in img_file_names:
25             img_path = os.path.join(images_dir_path, img_file)
26             if os.path.exists(img_path):
27                 img = read_image(img_path)
28                 if len(img.shape) < 3:
29                     img = img[..., np.newaxis]
30                 x_train.append(img)
31             else:
32                 raise ValueError("%s image does not exist" % img_file)
33     else:
34         raise ValueError("Directory containing images does not exist")
35     return np.asanyarray(x_train)
36 
37 
38 def load_image_dataset(csv_file_path, images_path):
39     """Load images from the files and labels from a csv file.
40 
41     Second, the dataset is a set of images and the labels are in a CSV file.
42     The CSV file should contain two columns whose names are 'File Name' and 'Label'.
43     The file names in the first column should match the file names of the images with extensions,
44     e.g., .jpg, .png.
45     The path to the CSV file should be passed through the `csv_file_path`.
46     The path to the directory containing all the images should be passed through `image_path`.
47 
48     Args:
49         csv_file_path: CSV file path.
50         images_path: Path where images exist.
51 
52     Returns:
53         x: Four dimensional numpy.ndarray. The channel dimension is the last dimension.
54         y: The labels.
55     """
56     img_file_name, y = read_csv_file(csv_file_path)
57     x = read_images(img_file_name, images_path)
58     return np.array(x), np.array(y)
59 
60 
61 class ImageSupervised(DeepSupervised, ABC):
62     """Abstract image supervised class.
63 
64     Attributes:
65         path: A path to the directory to save the classifier as well as intermediate results.
66         cnn: CNN module from net_module.py.
67         y_encoder: Label encoder, used in transform_y or inverse_transform_y for encode the label. For example,
68                     if one hot encoder needed, y_encoder can be OneHotEncoder.
69         data_transformer: A transformer class to process the data. See example as ImageDataTransformer.
70         verbose: A boolean value indicating the verbosity mode which determines whether the search process
71                 will be printed to stdout.
72         augment: A boolean value indicating whether the data needs augmentation.  If not define, then it
73                 will use the value of Constant.DATA_AUGMENTATION which is True by default.
74         searcher_args: A dictionary containing the parameters for the searcher's __init__ function.
75         resize_height: resize image height.
76         resize_width: resize image width.
77     """
78 
79     def __init__(self, augment=None, **kwargs):
80         """Initialize the instance.
81         The classifier will be loaded from the files in 'path' if parameter 'resume' is True.
82         Otherwise it would create a new one.
83         Args:
84             verbose: A boolean of whether the search process will be printed to stdout.
85             path: A string. The path to a directory, where the intermediate results are saved.
86             resume: A boolean. If True, the classifier will continue to previous work saved in path.
87                 Otherwise, the classifier will start a new search.
88             searcher_args: A dictionary containing the parameters for the searcher's __init__ function.
89             augment: A boolean value indicating whether the data needs augmentation. If not define, then it
90                 will use the value of Constant.DATA_AUGMENTATION which is True by default.
91         """
92         self.augment = augment if augment is not None else Constant.DATA_AUGMENTATION
93         self.resize_shape = []
94 
95         super().__init__(**kwargs)
96 
97     def fit(self, x, y, time_limit=None):
98         x = np.array(x)
99         y = np.array(y)
100 
101         if self.verbose:
102             print("Preprocessing the images.")
103 
104         self.resize_shape = compute_image_resize_params(x)
105 
106         x = resize_image_data(x, self.resize_shape)
107 
108         if self.verbose:
109             print("Preprocessing finished.")
110 
111         super().fit(x, y, time_limit)
112 
113     def init_transformer(self, x):
114         if self.data_transformer is None:
115             self.data_transformer = ImageDataTransformer(x, augment=self.augment)
116 
117     def preprocess(self, x):
118         return resize_image_data(x, self.resize_shape)
119 
120 
121 class ImageClassifier(ImageSupervised):
122     """ImageClassifier class.
123 
124     It is used for image classification. It searches convolutional neural network architectures
125     for the best configuration for the image dataset.
126     """
127 
128     @property
129     def loss(self):
130         return classification_loss
131 
132     @property
133     def metric(self):
134         return Accuracy
135 
136     def transform_y(self, y_train):
137         # Transform y_train.
138         if self.y_encoder is None:
139             self.y_encoder = OneHotEncoder()
140             self.y_encoder.fit(y_train)
141         y_train = self.y_encoder.transform(y_train)
142         return y_train
143 
144     def inverse_transform_y(self, output):
145         return self.y_encoder.inverse_transform(output)
146 
147     def get_n_output_node(self):
148         return self.y_encoder.n_classes
149 
150     def export_autokeras_model(self, model_file_name):
151         """ Creates and Exports the AutoKeras model to the given filename. """
152         portable_model = PortableImageClassifier(graph=self.cnn.best_model,
153                                                  y_encoder=self.y_encoder,
154                                                  data_transformer=self.data_transformer,
155                                                  resize_params=self.resize_shape,
156                                                  path=self.path)
157         pickle_to_file(portable_model, model_file_name)
158 
159 
160 class ImageClassifier1D(ImageClassifier):
161     """ ImageClassifier1D class.
162 
163     It is used for 1D image classification. It searches convolutional neural network architectures
164     for the best configuration for the 1D image dataset.
165     """
166 
167     def __init__(self, **kwargs):
168         kwargs['augment'] = False
169         super().__init__(**kwargs)
170 
171 
172 class ImageClassifier3D(ImageClassifier):
173     """ ImageClassifier3D class.
174 
175     It is used for 3D image classification. It searches convolutional neural network architectures
176     for the best configuration for the 1D image dataset.
177     """
178 
179     def __init__(self, **kwargs):
180         kwargs['augment'] = False
181         super().__init__(**kwargs)
182 
183 
184 class ImageRegressor(ImageSupervised):
185     """ImageRegressor class.
186 
187     It is used for image regression. It searches convolutional neural network architectures
188     for the best configuration for the image dataset.
189     """
190 
191     @property
192     def loss(self):
193         return regression_loss
194 
195     @property
196     def metric(self):
197         return MSE
198 
199     def get_n_output_node(self):
200         return 1
201 
202     def transform_y(self, y_train):
203         return y_train.flatten().reshape(len(y_train), 1)
204 
205     def inverse_transform_y(self, output):
206         return output.flatten()
207 
208     def export_autokeras_model(self, model_file_name):
209         """ Creates and Exports the AutoKeras model to the given filename. """
210         portable_model = PortableImageRegressor(graph=self.cnn.best_model,
211                                                 y_encoder=self.y_encoder,
212                                                 data_transformer=self.data_transformer,
213                                                 resize_params=self.resize_shape,
214                                                 path=self.path)
215         pickle_to_file(portable_model, model_file_name)
216 
217 
218 class ImageRegressor1D(ImageRegressor):
219     """ ImageRegressor1D class.
220 
221     It is used for 1D image regression. It searches convolutional neural network architectures
222     for the best configuration for the 1D image dataset.
223     """
224 
225     def __init__(self, **kwargs):
226         kwargs['augment'] = False
227         super().__init__(**kwargs)
228 
229 
230 class ImageRegressor3D(ImageRegressor):
231     """ ImageRegressor3D class.
232 
233     It is used for 3D image regression. It searches convolutional neural network architectures
234     for the best configuration for the 1D image dataset.
235     """
236 
237     def __init__(self, **kwargs):
238         kwargs['augment'] = False
239         super().__init__(**kwargs)
240 
241 
242 class PortableImageSupervised(PortableDeepSupervised):
243     def __init__(self, graph, y_encoder, data_transformer, resize_params, verbose=False, path=None):
244         """Initialize the instance.
245         Args:
246             graph: The graph form of the learned model
247         """
248         super().__init__(graph, y_encoder, data_transformer, verbose, path)
249         self.resize_shape = resize_params
250 
251     def preprocess(self, x):
252         return resize_image_data(x, self.resize_shape)
253 
254 
255 class PortableImageClassifier(PortableImageSupervised):
256     @property
257     def loss(self):
258         return classification_loss
259 
260     @property
261     def metric(self):
262         return Accuracy
263 
264     def transform_y(self, y_train):
265         return self.y_encoder.transform(y_train)
266 
267     def inverse_transform_y(self, output):
268         return self.y_encoder.inverse_transform(output)
269 
270 
271 class PortableImageRegressor(PortableImageSupervised):
272     @property
273     def loss(self):
274         return regression_loss
275 
276     @property
277     def metric(self):
278         return MSE
279 
280     def transform_y(self, y_train):
281         return y_train.flatten().reshape(len(y_train), 1)
282 
283     def inverse_transform_y(self, output):
284         return output.flatten()
285
```
--- 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/image/image_supervised.py b/autokeras/image/image_supervised.py
--- a/autokeras/image/image_supervised.py
+++ b/autokeras/image/image_supervised.py
@@ -1,6 +1,7 @@
 import os
 from abc import ABC
 import numpy as np
+from multiprocessing import Pool, cpu_count
 
 from autokeras.constant import Constant
 from autokeras.nn.loss_function import classification_loss, regression_loss
@@ -11,31 +12,48 @@
     read_csv_file, read_image, compute_image_resize_params, resize_image_data
 
 
-def read_images(img_file_names, images_dir_path):
+def _image_to_array(img_path):
+    """Read the image from the path and return image object.
+        Return an image object.
+
+    Args:
+        img_path: image file name in images_dir_path.
+    """
+    if os.path.exists(img_path):
+        img = read_image(img_path)
+        if len(img.shape) < 3:
+            img = img[..., np.newaxis]
+        return img
+    else:
+        raise ValueError("%s image does not exist" % img_path)
+
+
+def read_images(img_file_names, images_dir_path, parallel=True):
     """Read the images from the path and return their numpy.ndarray instance.
         Return a numpy.ndarray instance containing the training data.
 
     Args:
         img_file_names: List containing images names.
         images_dir_path: Path to the directory containing images.
+        parallel: (Default: True) Run _image_to_array will use multiprocessing.
     """
-    x_train = []
+    img_paths = [os.path.join(images_dir_path, img_file)
+                 for img_file in img_file_names]
+
     if os.path.isdir(images_dir_path):
-        for img_file in img_file_names:
-            img_path = os.path.join(images_dir_path, img_file)
-            if os.path.exists(img_path):
-                img = read_image(img_path)
-                if len(img.shape) < 3:
-                    img = img[..., np.newaxis]
-                x_train.append(img)
-            else:
-                raise ValueError("%s image does not exist" % img_file)
+        if parallel:
+            pool = Pool(processes=cpu_count())
+            x_train = pool.map(_image_to_array, img_paths)
+            pool.close()
+            pool.join()
+        else:
+            x_train = [_image_to_array(img_path) for img_path in img_paths]
     else:
         raise ValueError("Directory containing images does not exist")
     return np.asanyarray(x_train)
 
 
-def load_image_dataset(csv_file_path, images_path):
+def load_image_dataset(csv_file_path, images_path, parallel=True):
     """Load images from the files and labels from a csv file.
 
     Second, the dataset is a set of images and the labels are in a CSV file.
@@ -48,13 +66,14 @@
     Args:
         csv_file_path: CSV file path.
         images_path: Path where images exist.
+        parallel: (Default: True) Load dataset with multiprocessing.
 
     Returns:
         x: Four dimensional numpy.ndarray. The channel dimension is the last dimension.
         y: The labels.
     """
     img_file_name, y = read_csv_file(csv_file_path)
-    x = read_images(img_file_name, images_path)
+    x = read_images(img_file_name, images_path, parallel)
     return np.array(x), np.array(y)
 
 
@@ -112,7 +131,8 @@
 
     def init_transformer(self, x):
         if self.data_transformer is None:
-            self.data_transformer = ImageDataTransformer(x, augment=self.augment)
+            self.data_transformer = ImageDataTransformer(
+                x, augment=self.augment)
 
     def preprocess(self, x):
         return resize_image_data(x, self.resize_shape)

{"golden_diff": "diff --git a/autokeras/image/image_supervised.py b/autokeras/image/image_supervised.py\n--- a/autokeras/image/image_supervised.py\n+++ b/autokeras/image/image_supervised.py\n@@ -1,6 +1,7 @@\n import os\n from abc import ABC\n import numpy as np\n+from multiprocessing import Pool, cpu_count\n \n from autokeras.constant import Constant\n from autokeras.nn.loss_function import classification_loss, regression_loss\n@@ -11,31 +12,48 @@\n     read_csv_file, read_image, compute_image_resize_params, resize_image_data\n \n \n-def read_images(img_file_names, images_dir_path):\n+def _image_to_array(img_path):\n+    \"\"\"Read the image from the path and return image object.\n+        Return an image object.\n+\n+    Args:\n+        img_path: image file name in images_dir_path.\n+    \"\"\"\n+    if os.path.exists(img_path):\n+        img = read_image(img_path)\n+        if len(img.shape) < 3:\n+            img = img[..., np.newaxis]\n+        return img\n+    else:\n+        raise ValueError(\"%s image does not exist\" % img_path)\n+\n+\n+def read_images(img_file_names, images_dir_path, parallel=True):\n     \"\"\"Read the images from the path and return their numpy.ndarray instance.\n         Return a numpy.ndarray instance containing the training data.\n \n     Args:\n         img_file_names: List containing images names.\n         images_dir_path: Path to the directory containing images.\n+        parallel: (Default: True) Run _image_to_array will use multiprocessing.\n     \"\"\"\n-    x_train = []\n+    img_paths = [os.path.join(images_dir_path, img_file)\n+                 for img_file in img_file_names]\n+\n     if os.path.isdir(images_dir_path):\n-        for img_file in img_file_names:\n-            img_path = os.path.join(images_dir_path, img_file)\n-            if os.path.exists(img_path):\n-                img = read_image(img_path)\n-                if len(img.shape) < 3:\n-                    img = img[..., np.newaxis]\n-                x_train.append(img)\n-            else:\n-                raise ValueError(\"%s image does not exist\" % img_file)\n+        if parallel:\n+            pool = Pool(processes=cpu_count())\n+            x_train = pool.map(_image_to_array, img_paths)\n+            pool.close()\n+            pool.join()\n+        else:\n+            x_train = [_image_to_array(img_path) for img_path in img_paths]\n     else:\n         raise ValueError(\"Directory containing images does not exist\")\n     return np.asanyarray(x_train)\n \n \n-def load_image_dataset(csv_file_path, images_path):\n+def load_image_dataset(csv_file_path, images_path, parallel=True):\n     \"\"\"Load images from the files and labels from a csv file.\n \n     Second, the dataset is a set of images and the labels are in a CSV file.\n@@ -48,13 +66,14 @@\n     Args:\n         csv_file_path: CSV file path.\n         images_path: Path where images exist.\n+        parallel: (Default: True) Load dataset with multiprocessing.\n \n     Returns:\n         x: Four dimensional numpy.ndarray. The channel dimension is the last dimension.\n         y: The labels.\n     \"\"\"\n     img_file_name, y = read_csv_file(csv_file_path)\n-    x = read_images(img_file_name, images_path)\n+    x = read_images(img_file_name, images_path, parallel)\n     return np.array(x), np.array(y)\n \n \n@@ -112,7 +131,8 @@\n \n     def init_transformer(self, x):\n         if self.data_transformer is None:\n-            self.data_transformer = ImageDataTransformer(x, augment=self.augment)\n+            self.data_transformer = ImageDataTransformer(\n+                x, augment=self.augment)\n \n     def preprocess(self, x):\n         return resize_image_data(x, self.resize_shape)\n", "issue": "feature_request: Support multicore/multiprocess for read_images function\n### Feature Description\r\nSupport multicore for `read_images` function in `image/image_supervised.py`.\r\n\r\n### Reason\r\nAs [read_images function in image_supervised.py](https://github.com/jhfjhfj1/autokeras/blob/36752fcbce58bd1a26c17144637cc0ecbef83da0/autokeras/image/image_supervised.py#L14) does not support multi-core(just read files one by one), so it's **too slow** when reading image files more than 10k files.\r\n\r\n### Solution\r\nUse multiprocessing or equivalent one in `read_images` function as a default, or optional.\r\n\n", "before_files": [{"content": "import os\nfrom abc import ABC\nimport numpy as np\n\nfrom autokeras.constant import Constant\nfrom autokeras.nn.loss_function import classification_loss, regression_loss\nfrom autokeras.nn.metric import Accuracy, MSE\nfrom autokeras.preprocessor import OneHotEncoder, ImageDataTransformer\nfrom autokeras.supervised import PortableDeepSupervised, DeepSupervised\nfrom autokeras.utils import pickle_to_file, \\\n    read_csv_file, read_image, compute_image_resize_params, resize_image_data\n\n\ndef read_images(img_file_names, images_dir_path):\n    \"\"\"Read the images from the path and return their numpy.ndarray instance.\n        Return a numpy.ndarray instance containing the training data.\n\n    Args:\n        img_file_names: List containing images names.\n        images_dir_path: Path to the directory containing images.\n    \"\"\"\n    x_train = []\n    if os.path.isdir(images_dir_path):\n        for img_file in img_file_names:\n            img_path = os.path.join(images_dir_path, img_file)\n            if os.path.exists(img_path):\n                img = read_image(img_path)\n                if len(img.shape) < 3:\n                    img = img[..., np.newaxis]\n                x_train.append(img)\n            else:\n                raise ValueError(\"%s image does not exist\" % img_file)\n    else:\n        raise ValueError(\"Directory containing images does not exist\")\n    return np.asanyarray(x_train)\n\n\ndef load_image_dataset(csv_file_path, images_path):\n    \"\"\"Load images from the files and labels from a csv file.\n\n    Second, the dataset is a set of images and the labels are in a CSV file.\n    The CSV file should contain two columns whose names are 'File Name' and 'Label'.\n    The file names in the first column should match the file names of the images with extensions,\n    e.g., .jpg, .png.\n    The path to the CSV file should be passed through the `csv_file_path`.\n    The path to the directory containing all the images should be passed through `image_path`.\n\n    Args:\n        csv_file_path: CSV file path.\n        images_path: Path where images exist.\n\n    Returns:\n        x: Four dimensional numpy.ndarray. The channel dimension is the last dimension.\n        y: The labels.\n    \"\"\"\n    img_file_name, y = read_csv_file(csv_file_path)\n    x = read_images(img_file_name, images_path)\n    return np.array(x), np.array(y)\n\n\nclass ImageSupervised(DeepSupervised, ABC):\n    \"\"\"Abstract image supervised class.\n\n    Attributes:\n        path: A path to the directory to save the classifier as well as intermediate results.\n        cnn: CNN module from net_module.py.\n        y_encoder: Label encoder, used in transform_y or inverse_transform_y for encode the label. For example,\n                    if one hot encoder needed, y_encoder can be OneHotEncoder.\n        data_transformer: A transformer class to process the data. See example as ImageDataTransformer.\n        verbose: A boolean value indicating the verbosity mode which determines whether the search process\n                will be printed to stdout.\n        augment: A boolean value indicating whether the data needs augmentation.  If not define, then it\n                will use the value of Constant.DATA_AUGMENTATION which is True by default.\n        searcher_args: A dictionary containing the parameters for the searcher's __init__ function.\n        resize_height: resize image height.\n        resize_width: resize image width.\n    \"\"\"\n\n    def __init__(self, augment=None, **kwargs):\n        \"\"\"Initialize the instance.\n        The classifier will be loaded from the files in 'path' if parameter 'resume' is True.\n        Otherwise it would create a new one.\n        Args:\n            verbose: A boolean of whether the search process will be printed to stdout.\n            path: A string. The path to a directory, where the intermediate results are saved.\n            resume: A boolean. If True, the classifier will continue to previous work saved in path.\n                Otherwise, the classifier will start a new search.\n            searcher_args: A dictionary containing the parameters for the searcher's __init__ function.\n            augment: A boolean value indicating whether the data needs augmentation. If not define, then it\n                will use the value of Constant.DATA_AUGMENTATION which is True by default.\n        \"\"\"\n        self.augment = augment if augment is not None else Constant.DATA_AUGMENTATION\n        self.resize_shape = []\n\n        super().__init__(**kwargs)\n\n    def fit(self, x, y, time_limit=None):\n        x = np.array(x)\n        y = np.array(y)\n\n        if self.verbose:\n            print(\"Preprocessing the images.\")\n\n        self.resize_shape = compute_image_resize_params(x)\n\n        x = resize_image_data(x, self.resize_shape)\n\n        if self.verbose:\n            print(\"Preprocessing finished.\")\n\n        super().fit(x, y, time_limit)\n\n    def init_transformer(self, x):\n        if self.data_transformer is None:\n            self.data_transformer = ImageDataTransformer(x, augment=self.augment)\n\n    def preprocess(self, x):\n        return resize_image_data(x, self.resize_shape)\n\n\nclass ImageClassifier(ImageSupervised):\n    \"\"\"ImageClassifier class.\n\n    It is used for image classification. It searches convolutional neural network architectures\n    for the best configuration for the image dataset.\n    \"\"\"\n\n    @property\n    def loss(self):\n        return classification_loss\n\n    @property\n    def metric(self):\n        return Accuracy\n\n    def transform_y(self, y_train):\n        # Transform y_train.\n        if self.y_encoder is None:\n            self.y_encoder = OneHotEncoder()\n            self.y_encoder.fit(y_train)\n        y_train = self.y_encoder.transform(y_train)\n        return y_train\n\n    def inverse_transform_y(self, output):\n        return self.y_encoder.inverse_transform(output)\n\n    def get_n_output_node(self):\n        return self.y_encoder.n_classes\n\n    def export_autokeras_model(self, model_file_name):\n        \"\"\" Creates and Exports the AutoKeras model to the given filename. \"\"\"\n        portable_model = PortableImageClassifier(graph=self.cnn.best_model,\n                                                 y_encoder=self.y_encoder,\n                                                 data_transformer=self.data_transformer,\n                                                 resize_params=self.resize_shape,\n                                                 path=self.path)\n        pickle_to_file(portable_model, model_file_name)\n\n\nclass ImageClassifier1D(ImageClassifier):\n    \"\"\" ImageClassifier1D class.\n\n    It is used for 1D image classification. It searches convolutional neural network architectures\n    for the best configuration for the 1D image dataset.\n    \"\"\"\n\n    def __init__(self, **kwargs):\n        kwargs['augment'] = False\n        super().__init__(**kwargs)\n\n\nclass ImageClassifier3D(ImageClassifier):\n    \"\"\" ImageClassifier3D class.\n\n    It is used for 3D image classification. It searches convolutional neural network architectures\n    for the best configuration for the 1D image dataset.\n    \"\"\"\n\n    def __init__(self, **kwargs):\n        kwargs['augment'] = False\n        super().__init__(**kwargs)\n\n\nclass ImageRegressor(ImageSupervised):\n    \"\"\"ImageRegressor class.\n\n    It is used for image regression. It searches convolutional neural network architectures\n    for the best configuration for the image dataset.\n    \"\"\"\n\n    @property\n    def loss(self):\n        return regression_loss\n\n    @property\n    def metric(self):\n        return MSE\n\n    def get_n_output_node(self):\n        return 1\n\n    def transform_y(self, y_train):\n        return y_train.flatten().reshape(len(y_train), 1)\n\n    def inverse_transform_y(self, output):\n        return output.flatten()\n\n    def export_autokeras_model(self, model_file_name):\n        \"\"\" Creates and Exports the AutoKeras model to the given filename. \"\"\"\n        portable_model = PortableImageRegressor(graph=self.cnn.best_model,\n                                                y_encoder=self.y_encoder,\n                                                data_transformer=self.data_transformer,\n                                                resize_params=self.resize_shape,\n                                                path=self.path)\n        pickle_to_file(portable_model, model_file_name)\n\n\nclass ImageRegressor1D(ImageRegressor):\n    \"\"\" ImageRegressor1D class.\n\n    It is used for 1D image regression. It searches convolutional neural network architectures\n    for the best configuration for the 1D image dataset.\n    \"\"\"\n\n    def __init__(self, **kwargs):\n        kwargs['augment'] = False\n        super().__init__(**kwargs)\n\n\nclass ImageRegressor3D(ImageRegressor):\n    \"\"\" ImageRegressor3D class.\n\n    It is used for 3D image regression. It searches convolutional neural network architectures\n    for the best configuration for the 1D image dataset.\n    \"\"\"\n\n    def __init__(self, **kwargs):\n        kwargs['augment'] = False\n        super().__init__(**kwargs)\n\n\nclass PortableImageSupervised(PortableDeepSupervised):\n    def __init__(self, graph, y_encoder, data_transformer, resize_params, verbose=False, path=None):\n        \"\"\"Initialize the instance.\n        Args:\n            graph: The graph form of the learned model\n        \"\"\"\n        super().__init__(graph, y_encoder, data_transformer, verbose, path)\n        self.resize_shape = resize_params\n\n    def preprocess(self, x):\n        return resize_image_data(x, self.resize_shape)\n\n\nclass PortableImageClassifier(PortableImageSupervised):\n    @property\n    def loss(self):\n        return classification_loss\n\n    @property\n    def metric(self):\n        return Accuracy\n\n    def transform_y(self, y_train):\n        return self.y_encoder.transform(y_train)\n\n    def inverse_transform_y(self, output):\n        return self.y_encoder.inverse_transform(output)\n\n\nclass PortableImageRegressor(PortableImageSupervised):\n    @property\n    def loss(self):\n        return regression_loss\n\n    @property\n    def metric(self):\n        return MSE\n\n    def transform_y(self, y_train):\n        return y_train.flatten().reshape(len(y_train), 1)\n\n    def inverse_transform_y(self, output):\n        return output.flatten()\n", "path": "autokeras/image/image_supervised.py"}], "after_files": [{"content": "import os\nfrom abc import ABC\nimport numpy as np\nfrom multiprocessing import Pool, cpu_count\n\nfrom autokeras.constant import Constant\nfrom autokeras.nn.loss_function import classification_loss, regression_loss\nfrom autokeras.nn.metric import Accuracy, MSE\nfrom autokeras.preprocessor import OneHotEncoder, ImageDataTransformer\nfrom autokeras.supervised import PortableDeepSupervised, DeepSupervised\nfrom autokeras.utils import pickle_to_file, \\\n    read_csv_file, read_image, compute_image_resize_params, resize_image_data\n\n\ndef _image_to_array(img_path):\n    \"\"\"Read the image from the path and return image object.\n        Return an image object.\n\n    Args:\n        img_path: image file name in images_dir_path.\n    \"\"\"\n    if os.path.exists(img_path):\n        img = read_image(img_path)\n        if len(img.shape) < 3:\n            img = img[..., np.newaxis]\n        return img\n    else:\n        raise ValueError(\"%s image does not exist\" % img_path)\n\n\ndef read_images(img_file_names, images_dir_path, parallel=True):\n    \"\"\"Read the images from the path and return their numpy.ndarray instance.\n        Return a numpy.ndarray instance containing the training data.\n\n    Args:\n        img_file_names: List containing images names.\n        images_dir_path: Path to the directory containing images.\n        parallel: (Default: True) Run _image_to_array will use multiprocessing.\n    \"\"\"\n    img_paths = [os.path.join(images_dir_path, img_file)\n                 for img_file in img_file_names]\n\n    if os.path.isdir(images_dir_path):\n        if parallel:\n            pool = Pool(processes=cpu_count())\n            x_train = pool.map(_image_to_array, img_paths)\n            pool.close()\n            pool.join()\n        else:\n            x_train = [_image_to_array(img_path) for img_path in img_paths]\n    else:\n        raise ValueError(\"Directory containing images does not exist\")\n    return np.asanyarray(x_train)\n\n\ndef load_image_dataset(csv_file_path, images_path, parallel=True):\n    \"\"\"Load images from the files and labels from a csv file.\n\n    Second, the dataset is a set of images and the labels are in a CSV file.\n    The CSV file should contain two columns whose names are 'File Name' and 'Label'.\n    The file names in the first column should match the file names of the images with extensions,\n    e.g., .jpg, .png.\n    The path to the CSV file should be passed through the `csv_file_path`.\n    The path to the directory containing all the images should be passed through `image_path`.\n\n    Args:\n        csv_file_path: CSV file path.\n        images_path: Path where images exist.\n        parallel: (Default: True) Load dataset with multiprocessing.\n\n    Returns:\n        x: Four dimensional numpy.ndarray. The channel dimension is the last dimension.\n        y: The labels.\n    \"\"\"\n    img_file_name, y = read_csv_file(csv_file_path)\n    x = read_images(img_file_name, images_path, parallel)\n    return np.array(x), np.array(y)\n\n\nclass ImageSupervised(DeepSupervised, ABC):\n    \"\"\"Abstract image supervised class.\n\n    Attributes:\n        path: A path to the directory to save the classifier as well as intermediate results.\n        cnn: CNN module from net_module.py.\n        y_encoder: Label encoder, used in transform_y or inverse_transform_y for encode the label. For example,\n                    if one hot encoder needed, y_encoder can be OneHotEncoder.\n        data_transformer: A transformer class to process the data. See example as ImageDataTransformer.\n        verbose: A boolean value indicating the verbosity mode which determines whether the search process\n                will be printed to stdout.\n        augment: A boolean value indicating whether the data needs augmentation.  If not define, then it\n                will use the value of Constant.DATA_AUGMENTATION which is True by default.\n        searcher_args: A dictionary containing the parameters for the searcher's __init__ function.\n        resize_height: resize image height.\n        resize_width: resize image width.\n    \"\"\"\n\n    def __init__(self, augment=None, **kwargs):\n        \"\"\"Initialize the instance.\n        The classifier will be loaded from the files in 'path' if parameter 'resume' is True.\n        Otherwise it would create a new one.\n        Args:\n            verbose: A boolean of whether the search process will be printed to stdout.\n            path: A string. The path to a directory, where the intermediate results are saved.\n            resume: A boolean. If True, the classifier will continue to previous work saved in path.\n                Otherwise, the classifier will start a new search.\n            searcher_args: A dictionary containing the parameters for the searcher's __init__ function.\n            augment: A boolean value indicating whether the data needs augmentation. If not define, then it\n                will use the value of Constant.DATA_AUGMENTATION which is True by default.\n        \"\"\"\n        self.augment = augment if augment is not None else Constant.DATA_AUGMENTATION\n        self.resize_shape = []\n\n        super().__init__(**kwargs)\n\n    def fit(self, x, y, time_limit=None):\n        x = np.array(x)\n        y = np.array(y)\n\n        if self.verbose:\n            print(\"Preprocessing the images.\")\n\n        self.resize_shape = compute_image_resize_params(x)\n\n        x = resize_image_data(x, self.resize_shape)\n\n        if self.verbose:\n            print(\"Preprocessing finished.\")\n\n        super().fit(x, y, time_limit)\n\n    def init_transformer(self, x):\n        if self.data_transformer is None:\n            self.data_transformer = ImageDataTransformer(\n                x, augment=self.augment)\n\n    def preprocess(self, x):\n        return resize_image_data(x, self.resize_shape)\n\n\nclass ImageClassifier(ImageSupervised):\n    \"\"\"ImageClassifier class.\n\n    It is used for image classification. It searches convolutional neural network architectures\n    for the best configuration for the image dataset.\n    \"\"\"\n\n    @property\n    def loss(self):\n        return classification_loss\n\n    @property\n    def metric(self):\n        return Accuracy\n\n    def transform_y(self, y_train):\n        # Transform y_train.\n        if self.y_encoder is None:\n            self.y_encoder = OneHotEncoder()\n            self.y_encoder.fit(y_train)\n        y_train = self.y_encoder.transform(y_train)\n        return y_train\n\n    def inverse_transform_y(self, output):\n        return self.y_encoder.inverse_transform(output)\n\n    def get_n_output_node(self):\n        return self.y_encoder.n_classes\n\n    def export_autokeras_model(self, model_file_name):\n        \"\"\" Creates and Exports the AutoKeras model to the given filename. \"\"\"\n        portable_model = PortableImageClassifier(graph=self.cnn.best_model,\n                                                 y_encoder=self.y_encoder,\n                                                 data_transformer=self.data_transformer,\n                                                 resize_params=self.resize_shape,\n                                                 path=self.path)\n        pickle_to_file(portable_model, model_file_name)\n\n\nclass ImageClassifier1D(ImageClassifier):\n    \"\"\" ImageClassifier1D class.\n\n    It is used for 1D image classification. It searches convolutional neural network architectures\n    for the best configuration for the 1D image dataset.\n    \"\"\"\n\n    def __init__(self, **kwargs):\n        kwargs['augment'] = False\n        super().__init__(**kwargs)\n\n\nclass ImageClassifier3D(ImageClassifier):\n    \"\"\" ImageClassifier3D class.\n\n    It is used for 3D image classification. It searches convolutional neural network architectures\n    for the best configuration for the 1D image dataset.\n    \"\"\"\n\n    def __init__(self, **kwargs):\n        kwargs['augment'] = False\n        super().__init__(**kwargs)\n\n\nclass ImageRegressor(ImageSupervised):\n    \"\"\"ImageRegressor class.\n\n    It is used for image regression. It searches convolutional neural network architectures\n    for the best configuration for the image dataset.\n    \"\"\"\n\n    @property\n    def loss(self):\n        return regression_loss\n\n    @property\n    def metric(self):\n        return MSE\n\n    def get_n_output_node(self):\n        return 1\n\n    def transform_y(self, y_train):\n        return y_train.flatten().reshape(len(y_train), 1)\n\n    def inverse_transform_y(self, output):\n        return output.flatten()\n\n    def export_autokeras_model(self, model_file_name):\n        \"\"\" Creates and Exports the AutoKeras model to the given filename. \"\"\"\n        portable_model = PortableImageRegressor(graph=self.cnn.best_model,\n                                                y_encoder=self.y_encoder,\n                                                data_transformer=self.data_transformer,\n                                                resize_params=self.resize_shape,\n                                                path=self.path)\n        pickle_to_file(portable_model, model_file_name)\n\n\nclass ImageRegressor1D(ImageRegressor):\n    \"\"\" ImageRegressor1D class.\n\n    It is used for 1D image regression. It searches convolutional neural network architectures\n    for the best configuration for the 1D image dataset.\n    \"\"\"\n\n    def __init__(self, **kwargs):\n        kwargs['augment'] = False\n        super().__init__(**kwargs)\n\n\nclass ImageRegressor3D(ImageRegressor):\n    \"\"\" ImageRegressor3D class.\n\n    It is used for 3D image regression. It searches convolutional neural network architectures\n    for the best configuration for the 1D image dataset.\n    \"\"\"\n\n    def __init__(self, **kwargs):\n        kwargs['augment'] = False\n        super().__init__(**kwargs)\n\n\nclass PortableImageSupervised(PortableDeepSupervised):\n    def __init__(self, graph, y_encoder, data_transformer, resize_params, verbose=False, path=None):\n        \"\"\"Initialize the instance.\n        Args:\n            graph: The graph form of the learned model\n        \"\"\"\n        super().__init__(graph, y_encoder, data_transformer, verbose, path)\n        self.resize_shape = resize_params\n\n    def preprocess(self, x):\n        return resize_image_data(x, self.resize_shape)\n\n\nclass PortableImageClassifier(PortableImageSupervised):\n    @property\n    def loss(self):\n        return classification_loss\n\n    @property\n    def metric(self):\n        return Accuracy\n\n    def transform_y(self, y_train):\n        return self.y_encoder.transform(y_train)\n\n    def inverse_transform_y(self, output):\n        return self.y_encoder.inverse_transform(output)\n\n\nclass PortableImageRegressor(PortableImageSupervised):\n    @property\n    def loss(self):\n        return regression_loss\n\n    @property\n    def metric(self):\n        return MSE\n\n    def transform_y(self, y_train):\n        return y_train.flatten().reshape(len(y_train), 1)\n\n    def inverse_transform_y(self, output):\n        return output.flatten()\n", "path": "autokeras/image/image_supervised.py"}]}