Repository: keras-team/autokeras

Issue:
TextClassifier preprocessing can't run on cpu
GPU is hardcoded: [text_preprocessor.py](https://github.com/jhfjhfj1/autokeras/blob/master/autokeras/text/text_preprocessor.py#L142)

line 142: device_id_list = GPUtil.getFirstAvailable()
error if no gpu

line 147: device = '/gpu:0'
please update


Discussion:

Resolving patch:
diff --git a/autokeras/text/text_preprocessor.py b/autokeras/text/text_preprocessor.py
--- a/autokeras/text/text_preprocessor.py
+++ b/autokeras/text/text_preprocessor.py
@@ -1,7 +1,6 @@
 import os
 import re
 
-import GPUtil
 import numpy as np
 
 from autokeras.constant import Constant
@@ -79,7 +78,7 @@ def read_embedding_index(extract_path):
         embedding_index: Dictionary contains word with pre trained index.
     """
     embedding_index = {}
-    f = open(os.path.join(extract_path, Constant.PRE_TRAIN_FILE_NAME))
+    f = open(os.path.join(extract_path, Constant.PRE_TRAIN_FILE_NAME), encoding="utf-8")
     for line in f:
         values = line.split()
         word = values[0]
@@ -138,34 +137,26 @@ def processing(path, word_index, input_length, x_train):
 
     embedding_matrix = load_pretrain(path=path, word_index=word_index)
 
-    # Get the first available GPU
-    device_id_list = GPUtil.getFirstAvailable()
-    device_id = device_id_list[0]  # grab first element from list
-
-    # Set CUDA_VISIBLE_DEVICES to mask out all other GPUs than the first available device id
-    os.environ["CUDA_VISIBLE_DEVICES"] = str(device_id)
-    device = '/gpu:0'
-    with tf.device(device):
-        from keras import Input, Model
-        from keras import backend
-        from keras.layers import Embedding
-        config = tf.ConfigProto(allow_soft_placement=True)
-        config.gpu_options.allow_growth = True
-        sess = tf.Session(config=config)
-        backend.set_session(sess)
-        print("generating preprocessing model...")
-        embedding_layer = Embedding(len(word_index) + 1,
-                                    Constant.EMBEDDING_DIM,
-                                    weights=[embedding_matrix],
-                                    input_length=input_length,
-                                    trainable=False)
-
-        sequence_input = Input(shape=(input_length,), dtype='int32')
-        embedded_sequences = embedding_layer(sequence_input)
-        model = Model(sequence_input, embedded_sequences)
-        print("converting text to vector...")
-        x_train = model.predict(x_train)
-        del model
+    from keras import Input, Model
+    from keras import backend
+    from keras.layers import Embedding
+    config = tf.ConfigProto(allow_soft_placement=True)
+    config.gpu_options.allow_growth = True
+    sess = tf.Session(config=config)
+    backend.set_session(sess)
+    print("generating preprocessing model...")
+    embedding_layer = Embedding(len(word_index) + 1,
+                                Constant.EMBEDDING_DIM,
+                                weights=[embedding_matrix],
+                                input_length=input_length,
+                                trainable=False)
+
+    sequence_input = Input(shape=(input_length,), dtype='int32')
+    embedded_sequences = embedding_layer(sequence_input)
+    model = Model(sequence_input, embedded_sequences)
+    print("converting text to vector...")
+    x_train = model.predict(x_train)
+    del model
 
     return x_train
 
diff --git a/examples/text_cnn/text.py b/examples/text_cnn/text.py
--- a/examples/text_cnn/text.py
+++ b/examples/text_cnn/text.py
@@ -27,4 +27,4 @@ def read_csv(file_path):
     file_path = "labeledTrainData.tsv"
     x_train, y_train = read_csv(file_path=file_path)
     clf = TextClassifier(verbose=True)
-    clf.fit(x=x_train, y=y_train, batch_size=10, time_limit=12 * 60 * 60)
+    clf.fit(x=x_train, y=y_train, time_limit=12 * 60 * 60)
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -15,7 +15,6 @@
                       'tensorflow==1.10.0',
                       'imageio==2.4.1',
                       'requests==2.20.1',
-                      'GPUtil==1.3.0',
                       'lightgbm==2.2.2',
                       'pandas==0.23.4',
                       'opencv-python==3.4.4.19'],


Test patch:
diff --git a/tests/text/test_text_preprocessor.py b/tests/text/test_text_preprocessor.py
--- a/tests/text/test_text_preprocessor.py
+++ b/tests/text/test_text_preprocessor.py
@@ -55,9 +55,8 @@ def test_load_pretrain(_, _1):
     assert (embedding_matrix[1] == embedding_index.get("bar")).all()
 
 
-@patch('autokeras.text.text_preprocessor.GPUtil.getFirstAvailable', return_value=[0])
 @patch('autokeras.text.text_preprocessor.load_pretrain', side_effect=mock_load_pretrain)
-def test_processing(_, _1):
+def test_processing(_):
     train_x = np.full((1, 2), 1)
     train_x = processing(TEST_TEMP_DIR, word_index, 2, train_x)
     assert np.allclose(train_x[0][0], embedding_matrix[1])