Skip to content

Commit

Permalink
Overflow (#102)
Browse files Browse the repository at this point in the history
* temp folder path test fixed

* fixing the timout

* fix bug

* bug fixed
  • Loading branch information
haifeng-jin committed Aug 20, 2018
1 parent 5a696c0 commit 93fa8b4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 49 deletions.
3 changes: 3 additions & 0 deletions autokeras/bayesian.py
Expand Up @@ -270,6 +270,9 @@ def optimize_acq(self, model_ids, descriptors, timeout):

if remaining_time < 0:
raise TimeoutError
# Did not found a not duplicated architecture
if father_id is None:
return None, None
nm_graph = self.searcher.load_model_by_id(father_id)
for args in target_graph.operation_history:
getattr(nm_graph, args[0])(*list(args[1:]))
Expand Down
29 changes: 17 additions & 12 deletions autokeras/image_supervised.py
Expand Up @@ -185,7 +185,6 @@ def fit(self, x_train=None, y_train=None, time_limit=None):
y_train: A numpy.ndarray instance containing the label of the training data.
time_limit: The time limit for the search in seconds.
"""
start_time = time.time()
if y_train is None:
y_train = []
if x_train is None:
Expand Down Expand Up @@ -232,17 +231,23 @@ def fit(self, x_train=None, y_train=None, time_limit=None):
if time_limit is None:
time_limit = 24 * 60 * 60

time_elapsed = time.time() - start_time
time_remain = time_limit - time_elapsed
while time_remain > 0:
run_searcher_once(train_data, test_data, self.path, int(time_remain))
if len(self.load_searcher().history) >= Constant.MAX_MODEL_NUM:
break
time_elapsed = time.time() - start_time
time_remain = time_limit - time_elapsed
# if no search executed during the time_limit, then raise an error
if not len(self.load_searcher().history):
raise TimeoutError
start_time = time.time()
time_remain = time_limit
try:
while time_remain > 0:
run_searcher_once(train_data, test_data, self.path, int(time_remain))
if len(self.load_searcher().history) >= Constant.MAX_MODEL_NUM:
break
time_elapsed = time.time() - start_time
time_remain = time_limit - time_elapsed
# if no search executed during the time_limit, then raise an error
if time_remain <= 0:
raise TimeoutError
except TimeoutError:
if len(self.load_searcher().history) == 0:
raise TimeoutError("Search Time too short. No model was found during the search time.")
elif self.verbose:
print('Time is out.')

@abstractmethod
def get_n_output_node(self):
Expand Down
11 changes: 4 additions & 7 deletions autokeras/search.py
Expand Up @@ -173,6 +173,9 @@ def search(self, train_data, test_data, timeout=60 * 60 * 24):
new_graph, new_father_id = self.bo.optimize_acq(self.search_tree.adj_list.keys(),
self.descriptors,
timeout)
# Did not found a new architecture
if new_father_id is None:
return
new_model_id = self.model_count
self.model_count += 1
self.training_queue.append((new_graph, new_father_id, new_model_id))
Expand All @@ -187,13 +190,7 @@ def search(self, train_data, test_data, timeout=60 * 60 * 24):
else:
raise TimeoutError
except (multiprocessing.TimeoutError, TimeoutError) as e:
# if no model found in the time limit, raise TimeoutError
if self.model_count == 0:
# convert multiprocessing.TimeoutError to builtin TimeoutError for ux
raise TimeoutError("search Timeout") from e
# else return the result found in the time limit
else:
return
raise TimeoutError from e
finally:
# terminate and join the subprocess to prevent any resource leak
pool.terminate()
Expand Down
14 changes: 13 additions & 1 deletion tests/common.py
@@ -1,4 +1,6 @@
import os
from copy import deepcopy

import numpy as np

from autokeras.constant import Constant
Expand Down Expand Up @@ -210,8 +212,12 @@ def get_classification_data_loaders():

def clean_dir(path):
for f in os.listdir(path):
full_path = os.path.join(path, f)
if f != '.gitkeep':
os.remove(os.path.join(path, f))
if os.path.isfile(full_path):
os.remove(full_path)
else:
os.rmdir(full_path)


class MockProcess(object):
Expand All @@ -236,3 +242,9 @@ def get(self, timeout=None):

def terminate(self):
pass


def simple_transform(graph):
graph.to_wider_model(5, 64)
return [deepcopy(graph)]

18 changes: 5 additions & 13 deletions tests/test_image_supervised.py
@@ -1,11 +1,10 @@
from copy import deepcopy
from unittest.mock import patch


import pytest

from autokeras.image_supervised import *
from tests.common import clean_dir, MockProcess
from tests.common import clean_dir, MockProcess, simple_transform


def mock_train(**kwargs):
Expand Down Expand Up @@ -34,10 +33,6 @@ def test_x_float_exception():
assert str(info.value) == 'x_train should only contain numerical data.'


def simple_transform(graph):
return [deepcopy(graph), deepcopy(graph)]


@patch('multiprocessing.Pool', new=MockProcess)
@patch('autokeras.search.ModelTrainer.train_model', side_effect=mock_train)
def test_fit_predict(_):
Expand All @@ -58,11 +53,9 @@ def test_fit_predict(_):


@patch('multiprocessing.Pool', new=MockProcess)
@patch('autokeras.search.ModelTrainer.train_model', side_effect=mock_train)
def test_timeout(_):
Constant.MAX_ITER_NUM = 1
Constant.MAX_MODEL_NUM = 4
Constant.SEARCH_MAX_ITER = 1
def test_timeout():
# Constant.MAX_MODEL_NUM = 4
Constant.SEARCH_MAX_ITER = 1000
Constant.T_MIN = 0.8
Constant.DATA_AUGMENTATION = False
path = 'tests/resources/temp'
Expand All @@ -77,7 +70,7 @@ def test_timeout(_):

@patch('multiprocessing.Pool', new=MockProcess)
@patch('autokeras.search.ModelTrainer.train_model', side_effect=mock_train)
def test_timout_resume(_):
def test_timeout_resume(_):
Constant.MAX_ITER_NUM = 1
# make it impossible to complete within 10sec
Constant.MAX_MODEL_NUM = 1000
Expand All @@ -92,7 +85,6 @@ def test_timout_resume(_):
clf.n_epochs = 100
clf.fit(train_x, train_y, 15)
history_len = len(clf.load_searcher().history)
print(history_len)
assert history_len != 0
results = clf.predict(test_x)
assert len(results) == 100
Expand Down
15 changes: 2 additions & 13 deletions tests/test_search.py
@@ -1,21 +1,15 @@
from copy import deepcopy
from unittest.mock import patch

from autokeras.loss_function import classification_loss
from autokeras.metric import Accuracy
from autokeras.search import *

from tests.common import clean_dir, MockProcess, get_classification_data_loaders, get_add_skip_model, \
get_concat_skip_model
get_concat_skip_model, simple_transform

default_test_path = 'tests/resources/temp'


def simple_transform(graph):
graph.to_concat_skip_model(1, 5)
return [deepcopy(graph)]


def mock_train(**_):
return 1, 0

Expand Down Expand Up @@ -72,13 +66,8 @@ def test_graph_duplicate():
assert not same_graph(get_concat_skip_model().extract_descriptor(), get_add_skip_model().extract_descriptor())


def simple_transform2(graph):
graph.to_wider_model(5, 64)
return [deepcopy(graph)]


@patch('multiprocessing.Pool', new=MockProcess)
@patch('autokeras.bayesian.transform', side_effect=simple_transform2)
@patch('autokeras.bayesian.transform', side_effect=simple_transform)
@patch('autokeras.search.ModelTrainer.train_model', side_effect=mock_train)
def test_max_acq(_, _1):
train_data, test_data = get_classification_data_loaders()
Expand Down
10 changes: 7 additions & 3 deletions tests/test_utils.py
Expand Up @@ -4,7 +4,7 @@
from autokeras.loss_function import classification_loss, regression_loss
from autokeras.metric import Accuracy, MSE
from autokeras.utils import ModelTrainer, temp_folder_generator
from tests.common import get_classification_data_loaders, get_regression_data_loaders
from tests.common import get_classification_data_loaders, get_regression_data_loaders, clean_dir


def test_model_trainer_classification():
Expand All @@ -19,7 +19,11 @@ def test_model_trainer_regression():
ModelTrainer(model, train_data, test_data, MSE, regression_loss, False).train_model(max_iter_num=3)


@patch('tempfile.gettempdir', return_value="dummy_path/")
@patch('tempfile.gettempdir', return_value="tests/resources/temp/")
def test_temp_folder_generator(_):
path = 'tests/resources/temp'
clean_dir(path)
path = temp_folder_generator()
assert path == "dummy_path/autokeras"
assert path == "tests/resources/temp/autokeras"
path = 'tests/resources/temp'
clean_dir(path)

0 comments on commit 93fa8b4

Please sign in to comment.