Repository: keras-team/autokeras

Issue:
RuntimeError: CUDA error (3): initialization error (google colab)

### Bug Description
When i exported model with method ImageClassifier, i get an exception 

100%|██████████| 243/243 [00:04<00:00, 54.86it/s]
100%|██████████| 275/275 [00:05<00:00, 46.61it/s]
(518, 64, 64, 3) (518, 1)
Preprocessing the images.
Preprocessing finished.

Initializing search.
Initialization finished.


+----------------------------------------------+
|               Training model 0               |
+----------------------------------------------+
Process ForkProcess-8:
Traceback (most recent call last):
  File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/search.py", line 277, in train
    raise e
  File "/usr/local/lib/python3.6/dist-packages/autokeras/search.py", line 270, in train
    verbose=verbose).train_model(**trainer_args)
  File "/usr/local/lib/python3.6/dist-packages/autokeras/nn/model_trainer.py", line 77, in __init__
    self.model.to(self.device)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 379, in to
    return self._apply(convert)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 185, in _apply
    module._apply(fn)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 191, in _apply
    param.data = fn(param.data)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 377, in convert
    return t.to(device, dtype if t.is_floating_point() else None, non_blocking)
RuntimeError: CUDA error (3): initialization error


Discussion:
@boyuangong would you please look into this issue and try to solve it and include in the tutorial for export models? Thank you!

<sub>Sent with <a href="http://githawk.com">GitHawk</a></sub>

Resolving patch:
diff --git a/autokeras/__init__.py b/autokeras/__init__.py
--- a/autokeras/__init__.py
+++ b/autokeras/__init__.py
@@ -1,4 +1,4 @@
-from autokeras.image.image_supervised import ImageClassifier, ImageRegressor
+from autokeras.image.image_supervised import ImageClassifier, ImageRegressor, PortableImageSupervised
 from autokeras.text.text_supervised import TextClassifier, TextRegressor
 from autokeras.tabular.tabular_supervised import TabularClassifier, TabularRegressor
 from autokeras.net_module import CnnModule, MlpModule
\ No newline at end of file
diff --git a/autokeras/utils.py b/autokeras/utils.py
--- a/autokeras/utils.py
+++ b/autokeras/utils.py
@@ -18,7 +18,6 @@
 from scipy.ndimage import zoom
 
 
-
 class NoImprovementError(Exception):
     def __init__(self, message):
         self.message = message
@@ -49,12 +48,11 @@ def pickle_to_file(obj, path):
     """Save the pickle file to the specified path."""
     pickle.dump(obj, open(path, 'wb'))
 
+
 # TODO cannot detect nvidia-smi in Windows normally. We need a fall back for windows
 def get_device():
     """ If CUDA is available, use CUDA device, else use CPU device.
-
     When choosing from CUDA devices, this function will choose the one with max memory available.
-
     Returns: string device name.
     """
     # TODO: could use gputil in the future
@@ -140,7 +138,7 @@ def verbose_print(new_father_id, new_graph, new_model_id):
     """Print information about the operation performed on father model to obtain current model and father's id."""
     cell_size = [24, 49]
 
-    logging.info('New Model Id - '+str(new_model_id))
+    logging.info('New Model Id - ' + str(new_model_id))
     header = ['Father Model ID', 'Added Operation']
     line = '|'.join(str(x).center(cell_size[i]) for i, x in enumerate(header))
     logging.info('\n' + '+' + '-' * len(line) + '+')
@@ -155,6 +153,7 @@ def verbose_print(new_father_id, new_graph, new_model_id):
         logging.info('|' + line + '|')
     logging.info('+' + '-' * len(line) + '+')
 
+
 def validate_xy(x_train, y_train):
     """Validate `x_train`'s type and the shape of `x_train`, `y_train`."""
     try:
@@ -264,4 +263,5 @@ def get_system():
         return Constant.SYS_LINUX
     if os.name == 'nt':
         return Constant.SYS_WINDOWS
+
     raise EnvironmentError('Unsupported environment')
diff --git a/examples/portable_load.py b/examples/portable_load.py
new file mode 100644
--- /dev/null
+++ b/examples/portable_load.py
@@ -0,0 +1,22 @@
+import os
+
+from keras.datasets import mnist
+
+from autokeras import ImageClassifier
+from autokeras.utils import pickle_from_file
+
+# Customer temp dir by your own
+TEMP_DIR = '/tmp/autokeras_U8KEOQ'
+model_file_name = os.path.join(TEMP_DIR, 'test_autokeras_model.pkl')
+
+if __name__ == '__main__':
+    (x_train, y_train), (x_test, y_test) = mnist.load_data()
+    x_train = x_train.reshape(x_train.shape + (1,))
+    x_test = x_test.reshape(x_test.shape + (1,))
+    clf = ImageClassifier(verbose=True, augment=False, path=TEMP_DIR, resume=True)
+    clf.fit(x_train, y_train, time_limit=30 * 60)
+    clf.final_fit(x_train, y_train, x_test, y_test)
+    clf.export_autokeras_model(model_file_name)
+    model = pickle_from_file(model_file_name)
+    results = model.evaluate(x_test, y_test)
+    print(results)


Test patch: