We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Adding types information on the API surface.
Adding types on the public API surface would allow us to do some runtime type checking later on and would allow user's IDE to have more info for static analysis. 

The functions/signatures to type are the ones listed here https://github.com/keras-team/autokeras/blob/master/autokeras/__init__.py

For the context, see #856 where I add some type information on a ImageClassifier.

This issue can be considered easy to solve (good first issue), it's just long to do because the public API surface is big.

I'll do it but some help is welcome :) make sure to make small pull requests.

--- 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/utils/io_utils.py`
Content:
```
1 # Copyright 2020 The AutoKeras Authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import json
16 
17 import numpy as np
18 import tensorflow as tf
19 from tensorflow.python.keras.preprocessing import dataset_utils
20 
21 WHITELIST_FORMATS = (".bmp", ".gif", ".jpeg", ".jpg", ".png")
22 
23 
24 def save_json(path, obj):
25     obj = json.dumps(obj)
26     with tf.io.gfile.GFile(path, "w") as f:
27         f.write(obj)
28 
29 
30 def load_json(path):
31     with tf.io.gfile.GFile(path, "r") as f:
32         obj = f.read()
33     return json.loads(obj)
34 
35 
36 def get_training_or_validation_split(samples, labels, validation_split, subset):
37     """Potentially restict samples & labels to a training or validation split.
38 
39     # Arguments
40         samples: List of elements.
41         labels: List of corresponding labels.
42         validation_split: Float, fraction of data to reserve for validation.
43         subset: Subset of the data to return.
44             Either "training", "validation", or None.
45             If None, we return all of the data.
46 
47     # Returns
48         tuple (samples, labels), potentially restricted to the specified subset.
49     """
50     if not validation_split:
51         return samples, labels
52 
53     num_val_samples = int(validation_split * len(samples))
54     if subset == "training":
55         print("Using %d files for training." % (len(samples) - num_val_samples,))
56         samples = samples[:-num_val_samples]
57         labels = labels[:-num_val_samples]
58     elif subset == "validation":
59         print("Using %d files for validation." % (num_val_samples,))
60         samples = samples[-num_val_samples:]
61         labels = labels[-num_val_samples:]
62     else:
63         raise ValueError(
64             '`subset` must be either "training" '
65             'or "validation", received: %s' % (subset,)
66         )
67     return samples, labels
68 
69 
70 def text_dataset_from_directory(
71     directory,
72     batch_size=32,
73     max_length=None,
74     shuffle=True,
75     seed=None,
76     validation_split=None,
77     subset=None,
78 ):
79     """Generates a `tf.data.Dataset` from text files in a directory.
80 
81     If your directory structure is:
82 
83     ```
84     main_directory/
85     ...class_a/
86     ......a_text_1.txt
87     ......a_text_2.txt
88     ...class_b/
89     ......b_text_1.txt
90     ......b_text_2.txt
91     ```
92 
93     Then calling `text_dataset_from_directory(main_directory)`
94     will return a `tf.data.Dataset` that yields batches of texts from
95     the subdirectories `class_a` and `class_b`, together with labels
96     'class_a' and 'class_b'.
97 
98     Only `.txt` files are supported at this time.
99 
100     # Arguments
101         directory: Directory where the data is located.
102             If `labels` is "inferred", it should contain
103             subdirectories, each containing text files for a class.
104             Otherwise, the directory structure is ignored.
105         batch_size: Size of the batches of data. Defaults to 32.
106         max_length: Maximum size of a text string. Texts longer than this will
107             be truncated to `max_length`.
108         shuffle: Whether to shuffle the data. Default: True.
109             If set to False, sorts the data in alphanumeric order.
110         seed: Optional random seed for shuffling and transformations.
111         validation_split: Optional float between 0 and 1,
112             fraction of data to reserve for validation.
113         subset: One of "training" or "validation".
114             Only used if `validation_split` is set.
115 
116     # Returns
117         A `tf.data.Dataset` object, which yields a tuple `(texts, labels)`,
118             where both has shape `(batch_size,)` and type of tf.string.
119     """
120     if seed is None:
121         seed = np.random.randint(1e6)
122     file_paths, labels, class_names = dataset_utils.index_directory(
123         directory, "inferred", formats=(".txt",), shuffle=shuffle, seed=seed
124     )
125 
126     file_paths, labels = get_training_or_validation_split(
127         file_paths, labels, validation_split, subset
128     )
129 
130     strings = tf.data.Dataset.from_tensor_slices(file_paths)
131     strings = strings.map(tf.io.read_file)
132     if max_length is not None:
133         strings = strings.map(lambda x: tf.strings.substr(x, 0, max_length))
134 
135     labels = np.array(class_names)[np.array(labels)]
136     labels = tf.data.Dataset.from_tensor_slices(labels)
137 
138     dataset = tf.data.Dataset.zip((strings, labels))
139     dataset = dataset.batch(batch_size)
140     return dataset
141 
142 
143 def image_dataset_from_directory(
144     directory,
145     batch_size=32,
146     color_mode="rgb",
147     image_size=(256, 256),
148     interpolation="bilinear",
149     shuffle=True,
150     seed=None,
151     validation_split=None,
152     subset=None,
153 ):
154     """Generates a `tf.data.Dataset` from image files in a directory.
155     If your directory structure is:
156 
157     ```
158     main_directory/
159     ...class_a/
160     ......a_image_1.jpg
161     ......a_image_2.jpg
162     ...class_b/
163     ......b_image_1.jpg
164     ......b_image_2.jpg
165     ```
166 
167     Then calling `image_dataset_from_directory(main_directory)`
168     will return a `tf.data.Dataset` that yields batches of images from
169     the subdirectories `class_a` and `class_b`, together with labels
170     'class_a' and 'class_b'.
171 
172     Supported image formats: jpeg, png, bmp, gif.
173     Animated gifs are truncated to the first frame.
174 
175     # Arguments
176         directory: Directory where the data is located.
177             If `labels` is "inferred", it should contain
178             subdirectories, each containing images for a class.
179             Otherwise, the directory structure is ignored.
180         batch_size: Size of the batches of data. Default: 32.
181         color_mode: One of "grayscale", "rgb", "rgba". Default: "rgb".
182             Whether the images will be converted to
183             have 1, 3, or 4 channels.
184         image_size: Size to resize images to after they are read from disk.
185             Defaults to `(256, 256)`.
186             Since the pipeline processes batches of images that must all have
187             the same size, this must be provided.
188         interpolation: String, the interpolation method used when resizing images.
189           Defaults to `bilinear`. Supports `bilinear`, `nearest`, `bicubic`,
190           `area`, `lanczos3`, `lanczos5`, `gaussian`, `mitchellcubic`.
191         shuffle: Whether to shuffle the data. Default: True.
192             If set to False, sorts the data in alphanumeric order.
193         seed: Optional random seed for shuffling and transformations.
194         validation_split: Optional float between 0 and 1,
195             fraction of data to reserve for validation.
196         subset: One of "training" or "validation".
197             Only used if `validation_split` is set.
198 
199     # Returns
200         A `tf.data.Dataset` object, which yields a tuple `(texts, labels)`,
201         where `images` has shape `(batch_size, image_size[0], image_size[1],
202         num_channels)` where `labels` has shape `(batch_size,)` and type of
203         tf.string.
204         - if `color_mode` is `grayscale`, there's 1 channel in the image
205         tensors.
206         - if `color_mode` is `rgb`, there are 3 channel in the image tensors.
207         - if `color_mode` is `rgba`, there are 4 channel in the image tensors.
208     """
209     if color_mode == "rgb":
210         num_channels = 3
211     elif color_mode == "rgba":
212         num_channels = 4
213     elif color_mode == "grayscale":
214         num_channels = 1
215     else:
216         raise ValueError(
217             '`color_mode` must be one of {"rbg", "rgba", "grayscale"}. '
218             "Received: %s" % (color_mode,)
219         )
220 
221     if seed is None:
222         seed = np.random.randint(1e6)
223     image_paths, labels, class_names = dataset_utils.index_directory(
224         directory, "inferred", formats=WHITELIST_FORMATS, shuffle=shuffle, seed=seed
225     )
226 
227     image_paths, labels = get_training_or_validation_split(
228         image_paths, labels, validation_split, subset
229     )
230 
231     images = tf.data.Dataset.from_tensor_slices(image_paths)
232     images = images.map(
233         lambda img: path_to_image(img, num_channels, image_size, interpolation)
234     )
235 
236     labels = np.array(class_names)[np.array(labels)]
237     labels = tf.data.Dataset.from_tensor_slices(labels)
238 
239     dataset = tf.data.Dataset.zip((images, labels))
240     dataset = dataset.batch(batch_size)
241     return dataset
242 
243 
244 def path_to_image(image, num_channels, image_size, interpolation):
245     image = tf.io.read_file(image)
246     image = tf.io.decode_image(image, channels=num_channels, expand_animations=False)
247     image = tf.image.resize(image, image_size, method=interpolation)
248     image.set_shape((image_size[0], image_size[1], num_channels))
249     return image
250
```
--- 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/utils/io_utils.py b/autokeras/utils/io_utils.py
--- a/autokeras/utils/io_utils.py
+++ b/autokeras/utils/io_utils.py
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import json
+from typing import Optional
 
 import numpy as np
 import tensorflow as tf
@@ -68,14 +69,14 @@
 
 
 def text_dataset_from_directory(
-    directory,
-    batch_size=32,
-    max_length=None,
-    shuffle=True,
-    seed=None,
-    validation_split=None,
-    subset=None,
-):
+    directory: str,
+    batch_size: int = 32,
+    max_length: Optional[int] = None,
+    shuffle: bool = True,
+    seed: Optional[int] = None,
+    validation_split: Optional[float] = None,
+    subset: Optional[str] = None,
+) -> tf.data.Dataset:
     """Generates a `tf.data.Dataset` from text files in a directory.
 
     If your directory structure is:

{"golden_diff": "diff --git a/autokeras/utils/io_utils.py b/autokeras/utils/io_utils.py\n--- a/autokeras/utils/io_utils.py\n+++ b/autokeras/utils/io_utils.py\n@@ -13,6 +13,7 @@\n # limitations under the License.\n \n import json\n+from typing import Optional\n \n import numpy as np\n import tensorflow as tf\n@@ -68,14 +69,14 @@\n \n \n def text_dataset_from_directory(\n-    directory,\n-    batch_size=32,\n-    max_length=None,\n-    shuffle=True,\n-    seed=None,\n-    validation_split=None,\n-    subset=None,\n-):\n+    directory: str,\n+    batch_size: int = 32,\n+    max_length: Optional[int] = None,\n+    shuffle: bool = True,\n+    seed: Optional[int] = None,\n+    validation_split: Optional[float] = None,\n+    subset: Optional[str] = None,\n+) -> tf.data.Dataset:\n     \"\"\"Generates a `tf.data.Dataset` from text files in a directory.\n \n     If your directory structure is:\n", "issue": "Adding types information on the API surface.\nAdding types on the public API surface would allow us to do some runtime type checking later on and would allow user's IDE to have more info for static analysis. \r\n\r\nThe functions/signatures to type are the ones listed here https://github.com/keras-team/autokeras/blob/master/autokeras/__init__.py\r\n\r\nFor the context, see #856 where I add some type information on a ImageClassifier.\r\n\r\nThis issue can be considered easy to solve (good first issue), it's just long to do because the public API surface is big.\r\n\r\nI'll do it but some help is welcome :) make sure to make small pull requests.\n", "before_files": [{"content": "# Copyright 2020 The AutoKeras Authors.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport json\n\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.python.keras.preprocessing import dataset_utils\n\nWHITELIST_FORMATS = (\".bmp\", \".gif\", \".jpeg\", \".jpg\", \".png\")\n\n\ndef save_json(path, obj):\n    obj = json.dumps(obj)\n    with tf.io.gfile.GFile(path, \"w\") as f:\n        f.write(obj)\n\n\ndef load_json(path):\n    with tf.io.gfile.GFile(path, \"r\") as f:\n        obj = f.read()\n    return json.loads(obj)\n\n\ndef get_training_or_validation_split(samples, labels, validation_split, subset):\n    \"\"\"Potentially restict samples & labels to a training or validation split.\n\n    # Arguments\n        samples: List of elements.\n        labels: List of corresponding labels.\n        validation_split: Float, fraction of data to reserve for validation.\n        subset: Subset of the data to return.\n            Either \"training\", \"validation\", or None.\n            If None, we return all of the data.\n\n    # Returns\n        tuple (samples, labels), potentially restricted to the specified subset.\n    \"\"\"\n    if not validation_split:\n        return samples, labels\n\n    num_val_samples = int(validation_split * len(samples))\n    if subset == \"training\":\n        print(\"Using %d files for training.\" % (len(samples) - num_val_samples,))\n        samples = samples[:-num_val_samples]\n        labels = labels[:-num_val_samples]\n    elif subset == \"validation\":\n        print(\"Using %d files for validation.\" % (num_val_samples,))\n        samples = samples[-num_val_samples:]\n        labels = labels[-num_val_samples:]\n    else:\n        raise ValueError(\n            '`subset` must be either \"training\" '\n            'or \"validation\", received: %s' % (subset,)\n        )\n    return samples, labels\n\n\ndef text_dataset_from_directory(\n    directory,\n    batch_size=32,\n    max_length=None,\n    shuffle=True,\n    seed=None,\n    validation_split=None,\n    subset=None,\n):\n    \"\"\"Generates a `tf.data.Dataset` from text files in a directory.\n\n    If your directory structure is:\n\n    ```\n    main_directory/\n    ...class_a/\n    ......a_text_1.txt\n    ......a_text_2.txt\n    ...class_b/\n    ......b_text_1.txt\n    ......b_text_2.txt\n    ```\n\n    Then calling `text_dataset_from_directory(main_directory)`\n    will return a `tf.data.Dataset` that yields batches of texts from\n    the subdirectories `class_a` and `class_b`, together with labels\n    'class_a' and 'class_b'.\n\n    Only `.txt` files are supported at this time.\n\n    # Arguments\n        directory: Directory where the data is located.\n            If `labels` is \"inferred\", it should contain\n            subdirectories, each containing text files for a class.\n            Otherwise, the directory structure is ignored.\n        batch_size: Size of the batches of data. Defaults to 32.\n        max_length: Maximum size of a text string. Texts longer than this will\n            be truncated to `max_length`.\n        shuffle: Whether to shuffle the data. Default: True.\n            If set to False, sorts the data in alphanumeric order.\n        seed: Optional random seed for shuffling and transformations.\n        validation_split: Optional float between 0 and 1,\n            fraction of data to reserve for validation.\n        subset: One of \"training\" or \"validation\".\n            Only used if `validation_split` is set.\n\n    # Returns\n        A `tf.data.Dataset` object, which yields a tuple `(texts, labels)`,\n            where both has shape `(batch_size,)` and type of tf.string.\n    \"\"\"\n    if seed is None:\n        seed = np.random.randint(1e6)\n    file_paths, labels, class_names = dataset_utils.index_directory(\n        directory, \"inferred\", formats=(\".txt\",), shuffle=shuffle, seed=seed\n    )\n\n    file_paths, labels = get_training_or_validation_split(\n        file_paths, labels, validation_split, subset\n    )\n\n    strings = tf.data.Dataset.from_tensor_slices(file_paths)\n    strings = strings.map(tf.io.read_file)\n    if max_length is not None:\n        strings = strings.map(lambda x: tf.strings.substr(x, 0, max_length))\n\n    labels = np.array(class_names)[np.array(labels)]\n    labels = tf.data.Dataset.from_tensor_slices(labels)\n\n    dataset = tf.data.Dataset.zip((strings, labels))\n    dataset = dataset.batch(batch_size)\n    return dataset\n\n\ndef image_dataset_from_directory(\n    directory,\n    batch_size=32,\n    color_mode=\"rgb\",\n    image_size=(256, 256),\n    interpolation=\"bilinear\",\n    shuffle=True,\n    seed=None,\n    validation_split=None,\n    subset=None,\n):\n    \"\"\"Generates a `tf.data.Dataset` from image files in a directory.\n    If your directory structure is:\n\n    ```\n    main_directory/\n    ...class_a/\n    ......a_image_1.jpg\n    ......a_image_2.jpg\n    ...class_b/\n    ......b_image_1.jpg\n    ......b_image_2.jpg\n    ```\n\n    Then calling `image_dataset_from_directory(main_directory)`\n    will return a `tf.data.Dataset` that yields batches of images from\n    the subdirectories `class_a` and `class_b`, together with labels\n    'class_a' and 'class_b'.\n\n    Supported image formats: jpeg, png, bmp, gif.\n    Animated gifs are truncated to the first frame.\n\n    # Arguments\n        directory: Directory where the data is located.\n            If `labels` is \"inferred\", it should contain\n            subdirectories, each containing images for a class.\n            Otherwise, the directory structure is ignored.\n        batch_size: Size of the batches of data. Default: 32.\n        color_mode: One of \"grayscale\", \"rgb\", \"rgba\". Default: \"rgb\".\n            Whether the images will be converted to\n            have 1, 3, or 4 channels.\n        image_size: Size to resize images to after they are read from disk.\n            Defaults to `(256, 256)`.\n            Since the pipeline processes batches of images that must all have\n            the same size, this must be provided.\n        interpolation: String, the interpolation method used when resizing images.\n          Defaults to `bilinear`. Supports `bilinear`, `nearest`, `bicubic`,\n          `area`, `lanczos3`, `lanczos5`, `gaussian`, `mitchellcubic`.\n        shuffle: Whether to shuffle the data. Default: True.\n            If set to False, sorts the data in alphanumeric order.\n        seed: Optional random seed for shuffling and transformations.\n        validation_split: Optional float between 0 and 1,\n            fraction of data to reserve for validation.\n        subset: One of \"training\" or \"validation\".\n            Only used if `validation_split` is set.\n\n    # Returns\n        A `tf.data.Dataset` object, which yields a tuple `(texts, labels)`,\n        where `images` has shape `(batch_size, image_size[0], image_size[1],\n        num_channels)` where `labels` has shape `(batch_size,)` and type of\n        tf.string.\n        - if `color_mode` is `grayscale`, there's 1 channel in the image\n        tensors.\n        - if `color_mode` is `rgb`, there are 3 channel in the image tensors.\n        - if `color_mode` is `rgba`, there are 4 channel in the image tensors.\n    \"\"\"\n    if color_mode == \"rgb\":\n        num_channels = 3\n    elif color_mode == \"rgba\":\n        num_channels = 4\n    elif color_mode == \"grayscale\":\n        num_channels = 1\n    else:\n        raise ValueError(\n            '`color_mode` must be one of {\"rbg\", \"rgba\", \"grayscale\"}. '\n            \"Received: %s\" % (color_mode,)\n        )\n\n    if seed is None:\n        seed = np.random.randint(1e6)\n    image_paths, labels, class_names = dataset_utils.index_directory(\n        directory, \"inferred\", formats=WHITELIST_FORMATS, shuffle=shuffle, seed=seed\n    )\n\n    image_paths, labels = get_training_or_validation_split(\n        image_paths, labels, validation_split, subset\n    )\n\n    images = tf.data.Dataset.from_tensor_slices(image_paths)\n    images = images.map(\n        lambda img: path_to_image(img, num_channels, image_size, interpolation)\n    )\n\n    labels = np.array(class_names)[np.array(labels)]\n    labels = tf.data.Dataset.from_tensor_slices(labels)\n\n    dataset = tf.data.Dataset.zip((images, labels))\n    dataset = dataset.batch(batch_size)\n    return dataset\n\n\ndef path_to_image(image, num_channels, image_size, interpolation):\n    image = tf.io.read_file(image)\n    image = tf.io.decode_image(image, channels=num_channels, expand_animations=False)\n    image = tf.image.resize(image, image_size, method=interpolation)\n    image.set_shape((image_size[0], image_size[1], num_channels))\n    return image\n", "path": "autokeras/utils/io_utils.py"}], "after_files": [{"content": "# Copyright 2020 The AutoKeras Authors.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport json\nfrom typing import Optional\n\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.python.keras.preprocessing import dataset_utils\n\nWHITELIST_FORMATS = (\".bmp\", \".gif\", \".jpeg\", \".jpg\", \".png\")\n\n\ndef save_json(path, obj):\n    obj = json.dumps(obj)\n    with tf.io.gfile.GFile(path, \"w\") as f:\n        f.write(obj)\n\n\ndef load_json(path):\n    with tf.io.gfile.GFile(path, \"r\") as f:\n        obj = f.read()\n    return json.loads(obj)\n\n\ndef get_training_or_validation_split(samples, labels, validation_split, subset):\n    \"\"\"Potentially restict samples & labels to a training or validation split.\n\n    # Arguments\n        samples: List of elements.\n        labels: List of corresponding labels.\n        validation_split: Float, fraction of data to reserve for validation.\n        subset: Subset of the data to return.\n            Either \"training\", \"validation\", or None.\n            If None, we return all of the data.\n\n    # Returns\n        tuple (samples, labels), potentially restricted to the specified subset.\n    \"\"\"\n    if not validation_split:\n        return samples, labels\n\n    num_val_samples = int(validation_split * len(samples))\n    if subset == \"training\":\n        print(\"Using %d files for training.\" % (len(samples) - num_val_samples,))\n        samples = samples[:-num_val_samples]\n        labels = labels[:-num_val_samples]\n    elif subset == \"validation\":\n        print(\"Using %d files for validation.\" % (num_val_samples,))\n        samples = samples[-num_val_samples:]\n        labels = labels[-num_val_samples:]\n    else:\n        raise ValueError(\n            '`subset` must be either \"training\" '\n            'or \"validation\", received: %s' % (subset,)\n        )\n    return samples, labels\n\n\ndef text_dataset_from_directory(\n    directory: str,\n    batch_size: int = 32,\n    max_length: Optional[int] = None,\n    shuffle: bool = True,\n    seed: Optional[int] = None,\n    validation_split: Optional[float] = None,\n    subset: Optional[str] = None,\n) -> tf.data.Dataset:\n    \"\"\"Generates a `tf.data.Dataset` from text files in a directory.\n\n    If your directory structure is:\n\n    ```\n    main_directory/\n    ...class_a/\n    ......a_text_1.txt\n    ......a_text_2.txt\n    ...class_b/\n    ......b_text_1.txt\n    ......b_text_2.txt\n    ```\n\n    Then calling `text_dataset_from_directory(main_directory)`\n    will return a `tf.data.Dataset` that yields batches of texts from\n    the subdirectories `class_a` and `class_b`, together with labels\n    'class_a' and 'class_b'.\n\n    Only `.txt` files are supported at this time.\n\n    # Arguments\n        directory: Directory where the data is located.\n            If `labels` is \"inferred\", it should contain\n            subdirectories, each containing text files for a class.\n            Otherwise, the directory structure is ignored.\n        batch_size: Size of the batches of data. Defaults to 32.\n        max_length: Maximum size of a text string. Texts longer than this will\n            be truncated to `max_length`.\n        shuffle: Whether to shuffle the data. Default: True.\n            If set to False, sorts the data in alphanumeric order.\n        seed: Optional random seed for shuffling and transformations.\n        validation_split: Optional float between 0 and 1,\n            fraction of data to reserve for validation.\n        subset: One of \"training\" or \"validation\".\n            Only used if `validation_split` is set.\n\n    # Returns\n        A `tf.data.Dataset` object, which yields a tuple `(texts, labels)`,\n            where both has shape `(batch_size,)` and type of tf.string.\n    \"\"\"\n    if seed is None:\n        seed = np.random.randint(1e6)\n    file_paths, labels, class_names = dataset_utils.index_directory(\n        directory, \"inferred\", formats=(\".txt\",), shuffle=shuffle, seed=seed\n    )\n\n    file_paths, labels = get_training_or_validation_split(\n        file_paths, labels, validation_split, subset\n    )\n\n    strings = tf.data.Dataset.from_tensor_slices(file_paths)\n    strings = strings.map(tf.io.read_file)\n    if max_length is not None:\n        strings = strings.map(lambda x: tf.strings.substr(x, 0, max_length))\n\n    labels = np.array(class_names)[np.array(labels)]\n    labels = tf.data.Dataset.from_tensor_slices(labels)\n\n    dataset = tf.data.Dataset.zip((strings, labels))\n    dataset = dataset.batch(batch_size)\n    return dataset\n\n\ndef image_dataset_from_directory(\n    directory,\n    batch_size=32,\n    color_mode=\"rgb\",\n    image_size=(256, 256),\n    interpolation=\"bilinear\",\n    shuffle=True,\n    seed=None,\n    validation_split=None,\n    subset=None,\n):\n    \"\"\"Generates a `tf.data.Dataset` from image files in a directory.\n    If your directory structure is:\n\n    ```\n    main_directory/\n    ...class_a/\n    ......a_image_1.jpg\n    ......a_image_2.jpg\n    ...class_b/\n    ......b_image_1.jpg\n    ......b_image_2.jpg\n    ```\n\n    Then calling `image_dataset_from_directory(main_directory)`\n    will return a `tf.data.Dataset` that yields batches of images from\n    the subdirectories `class_a` and `class_b`, together with labels\n    'class_a' and 'class_b'.\n\n    Supported image formats: jpeg, png, bmp, gif.\n    Animated gifs are truncated to the first frame.\n\n    # Arguments\n        directory: Directory where the data is located.\n            If `labels` is \"inferred\", it should contain\n            subdirectories, each containing images for a class.\n            Otherwise, the directory structure is ignored.\n        batch_size: Size of the batches of data. Default: 32.\n        color_mode: One of \"grayscale\", \"rgb\", \"rgba\". Default: \"rgb\".\n            Whether the images will be converted to\n            have 1, 3, or 4 channels.\n        image_size: Size to resize images to after they are read from disk.\n            Defaults to `(256, 256)`.\n            Since the pipeline processes batches of images that must all have\n            the same size, this must be provided.\n        interpolation: String, the interpolation method used when resizing images.\n          Defaults to `bilinear`. Supports `bilinear`, `nearest`, `bicubic`,\n          `area`, `lanczos3`, `lanczos5`, `gaussian`, `mitchellcubic`.\n        shuffle: Whether to shuffle the data. Default: True.\n            If set to False, sorts the data in alphanumeric order.\n        seed: Optional random seed for shuffling and transformations.\n        validation_split: Optional float between 0 and 1,\n            fraction of data to reserve for validation.\n        subset: One of \"training\" or \"validation\".\n            Only used if `validation_split` is set.\n\n    # Returns\n        A `tf.data.Dataset` object, which yields a tuple `(texts, labels)`,\n        where `images` has shape `(batch_size, image_size[0], image_size[1],\n        num_channels)` where `labels` has shape `(batch_size,)` and type of\n        tf.string.\n        - if `color_mode` is `grayscale`, there's 1 channel in the image\n        tensors.\n        - if `color_mode` is `rgb`, there are 3 channel in the image tensors.\n        - if `color_mode` is `rgba`, there are 4 channel in the image tensors.\n    \"\"\"\n    if color_mode == \"rgb\":\n        num_channels = 3\n    elif color_mode == \"rgba\":\n        num_channels = 4\n    elif color_mode == \"grayscale\":\n        num_channels = 1\n    else:\n        raise ValueError(\n            '`color_mode` must be one of {\"rbg\", \"rgba\", \"grayscale\"}. '\n            \"Received: %s\" % (color_mode,)\n        )\n\n    if seed is None:\n        seed = np.random.randint(1e6)\n    image_paths, labels, class_names = dataset_utils.index_directory(\n        directory, \"inferred\", formats=WHITELIST_FORMATS, shuffle=shuffle, seed=seed\n    )\n\n    image_paths, labels = get_training_or_validation_split(\n        image_paths, labels, validation_split, subset\n    )\n\n    images = tf.data.Dataset.from_tensor_slices(image_paths)\n    images = images.map(\n        lambda img: path_to_image(img, num_channels, image_size, interpolation)\n    )\n\n    labels = np.array(class_names)[np.array(labels)]\n    labels = tf.data.Dataset.from_tensor_slices(labels)\n\n    dataset = tf.data.Dataset.zip((images, labels))\n    dataset = dataset.batch(batch_size)\n    return dataset\n\n\ndef path_to_image(image, num_channels, image_size, interpolation):\n    image = tf.io.read_file(image)\n    image = tf.io.decode_image(image, channels=num_channels, expand_animations=False)\n    image = tf.image.resize(image, image_size, method=interpolation)\n    image.set_shape((image_size[0], image_size[1], num_channels))\n    return image\n", "path": "autokeras/utils/io_utils.py"}]}