Autokeras: Issue with the fit function of autokeras not running on the image classifier

Created on 31 Mar 2020  路  9Comments  路  Source: keras-team/autokeras

Bug Description

When I ran
auto_class.fit(X_train_tr,y_train_tr, validation_data=(X_train_v,y_train_v))
It gave me the printout following this description.. I was using the fit function which as I understand it takes in numpy arrays. I actually have video of this code working 3 weeks ago.
To simplify things if you remove the validation data section so it's just
auto_class.fit(X_train_tr,y_train_tr)
the code still won't run. It's not that tuple it's refering to by
AttributeError: 'tuple' object has no attribute 'shape'
shown at the end of this error message

AttributeError Traceback (most recent call last)

in ()
1 auto_class = ImageClassifier()
----> 2 auto_class.fit(X_train_tr,y_train_tr, validation_data=(X_train_v,y_train_v))
3 # default number of epochs is 1000 with early stopping w patience=10

17 frames

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(args, *kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise

AttributeError: in user code:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:505 train_function  *
    outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
    return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:477 train_step  **
    self.compiled_metrics.update_state(y, y_pred, sample_weight)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:386 update_state
    self._build(y_pred, y_true)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:317 _build
    self._metrics, y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1118 map_structure_up_to
    **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1214 map_structure_with_tuple_paths_up_to
    *flat_value_lists)]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1213 <listcomp>
    results = [func(*args, **kwargs) for args in zip(flat_path_list,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py:1116 <lambda>
    lambda _, *values: func(*values),  # Discards the path arg.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:416 _get_metric_objects
    return [self._get_metric_object(m, y_t, y_p) for m in metrics]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:416 <listcomp>
    return [self._get_metric_object(m, y_t, y_p) for m in metrics]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:437 _get_metric_object
    y_t_rank = len(y_t.shape.as_list())

AttributeError: 'tuple' object has no attribute 'shape'

Bug Reproduction

Code for reproducing the bug:
Here's a link to the colab I encountered the error in
https://colab.research.google.com/drive/1XrYigov2C_Zg5SuGbUeEJafC5KM3BhaI

If it doesn't work here's all the code that got run

install autokeras

! pip install autokeras
%tensorflow_version 2.x
import autokeras as ak
from keras.datasets import mnist
from autokeras import ImageClassifier

(X_train, y_train), (X_test, y_test) = mnist.load_data()
import matplotlib.pyplot as plt

X_train = X_train.reshape(X_train.shape + (1,))
X_test = X_test.reshape(X_test.shape + (1,))

from sklearn.model_selection import train_test_split
X_train_tr, X_train_v, y_train_tr, y_train_v = train_test_split(X_train, y_train,
test_size=5000,
random_state=42)

This is where it ran into the problem
auto_class = ImageClassifier()
auto_class.fit(X_train_tr,y_train_tr, validation_data=(X_train_v,y_train_v))

default number of epochs is 1000 with early stopping w patience=10

Data used by the code:
training and test

Expected Behavior

The expected behavior was that it would take in both numpy arrays pulled

Setup Details

Include the details about the versions of:

  • OS type and version:
    windows 10
  • Python:
    Python 3 in colab
  • autokeras:
    I don't know which pervious version was running but the video was done on March 9 or 10 2020
  • keras-tuner:
  • scikit-learn:
  • numpy:
  • pandas:
  • tensorflow:
    version 2.x

Anything else imported or used would have been on that same date range of march 9 or 10 most were not specified

Additional context

This is a screenshot of the code working from the video I mentioned. it was a video from my class so I'm not sure I have permission to share the video itself but this was the main jist of it.
Capture

bug report

All 9 comments

Hi, I have the same problem. The same exception is raised when using a tf.data.dataset.

This is a known issue. We have fixed it in the master branch. You may try it with tensorflow==2.2.0rc2. Thanks

Same issue here. Google Colab. Tried with %tensorflow_version 2.x and pip install tensorflow==2.2.0rc2.

EDIT: !pip install tensorflow==2.1.0fixed it for me.

Getting same issue with tensorflow=2.2.0

C:\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:571 train_function *
outputs = self.distribute_strategy.run(
C:\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run *
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
return fn(
args, *kwargs)
C:\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:543 train_step *

self.compiled_metrics.update_state(y, y_pred, sample_weight)
C:\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:391 update_state
self._build(y_pred, y_true)
C:\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:322 _build
self._metrics, y_true, y_pred)
C:\Python\Python37\lib\site-packages\tensorflow\python\util\nest.py:1118 map_structure_up_to
*kwargs)
C:\Python\Python37\lib\site-packages\tensorflow\python\util\nest.py:1214 map_structure_with_tuple_paths_up_to
*flat_value_lists)]
C:\Python\Python37\lib\site-packages\tensorflow\python\util\nest.py:1213
results = [func(
args, *kwargs) for args in zip(flat_path_list,
C:\Python\Python37\lib\site-packages\tensorflow\python\util\nest.py:1116
lambda _, *values: func(
values), # Discards the path arg.
C:\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:421 _get_metric_objects
return [self._get_metric_object(m, y_t, y_p) for m in metrics]
C:\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:421
return [self._get_metric_object(m, y_t, y_p) for m in metrics]
C:\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:442 _get_metric_object
y_t_rank = len(y_t.shape.as_list())

AttributeError: 'tuple' object has no attribute 'shape'

Any update on this issue please?

+1

Upgraded tensorflow==2.2.0rc2 but still facing same issue. Please look into this.

from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape) # (60000, 28, 28)
print(y_train.shape) # (60000,)
print(y_train[:3]) # array([7, 2, 1], dtype=uint8)

import autokeras as ak

# Initialize the image classifier.
clf = ak.ImageClassifier(max_trials=2) # It tries 10 different models.

# Feed the image classifier with training data.

clf.fit(x_train, y_train,epochs=3)
Epoch 1/3
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-48320bb5e5b1> in <module>
----> 1 clf.fit(x_train, y_train,epochs=3)

~/anaconda3/lib/python3.7/site-packages/autokeras/tasks/image.py in fit(self, x, y, epochs, callbacks, validation_split, validation_data, **kwargs)
    120                     validation_split=validation_split,
    121                     validation_data=validation_data,
--> 122                     **kwargs)
    123 
    124 

~/anaconda3/lib/python3.7/site-packages/autokeras/auto_model.py in fit(self, x, y, batch_size, epochs, callbacks, validation_split, validation_data, **kwargs)
    256                           validation_data=validation_data,
    257                           fit_on_val_data=self._split_dataset,
--> 258                           **kwargs)
    259 
    260     def _process_x(self, x, fit):

~/anaconda3/lib/python3.7/site-packages/autokeras/engine/tuner.py in search(self, callbacks, fit_on_val_data, **fit_kwargs)
    112             new_callbacks.append(tf.keras.callbacks.EarlyStopping(patience=10))
    113 
--> 114         super().search(callbacks=new_callbacks, **fit_kwargs)
    115 
    116         # Fully train the best model with original callbacks.

~/anaconda3/lib/python3.7/site-packages/kerastuner/engine/base_tuner.py in search(self, *fit_args, **fit_kwargs)
    128 
    129             self.on_trial_begin(trial)
--> 130             self.run_trial(trial, *fit_args, **fit_kwargs)
    131             self.on_trial_end(trial)
    132         self.on_search_end()

~/anaconda3/lib/python3.7/site-packages/autokeras/engine/tuner.py in run_trial(self, trial, x, *fit_args, **fit_kwargs)
     69             model = self.hypermodel.build(trial.hyperparameters)
     70             utils.adapt_model(model, x)
---> 71             history = model.fit(x, *fit_args, **copied_fit_kwargs)
     72             for metric, epoch_values in history.history.items():
     73                 if self.oracle.objective.direction == 'min':

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
     64   def _method_wrapper(self, *args, **kwargs):
     65     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
---> 66       return method(self, *args, **kwargs)
     67 
     68     # Running inside `run_distribute_coordinator` already.

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
    783                 batch_size=batch_size):
    784               callbacks.on_train_batch_begin(step)
--> 785               tmp_logs = train_function(iterator)
    786               # Catch OutOfRangeError for Datasets of unknown size.
    787               # This blocks until the batch has finished executing.

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    578         xla_context.Exit()
    579     else:
--> 580       result = self._call(*args, **kwds)
    581 
    582     if tracing_count == self._get_tracing_count():

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    625       # This is the first call of __call__, so we have to initialize.
    626       initializers = []
--> 627       self._initialize(args, kwds, add_initializers_to=initializers)
    628     finally:
    629       # At this point we know that the initialization is complete (or less

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
    504     self._concrete_stateful_fn = (
    505         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
--> 506             *args, **kwds))
    507 
    508     def invalid_creator_scope(*unused_args, **unused_kwds):

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   2444       args, kwargs = None, None
   2445     with self._lock:
-> 2446       graph_function, _, _ = self._maybe_define_function(args, kwargs)
   2447     return graph_function
   2448 

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   2775 
   2776       self._function_cache.missed.add(call_context_key)
-> 2777       graph_function = self._create_graph_function(args, kwargs)
   2778       self._function_cache.primary[cache_key] = graph_function
   2779       return graph_function, args, kwargs

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2665             arg_names=arg_names,
   2666             override_flat_arg_shapes=override_flat_arg_shapes,
-> 2667             capture_by_value=self._capture_by_value),
   2668         self._function_attributes,
   2669         # Tell the ConcreteFunction to clean up its graph once it goes out of

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    979         _, original_func = tf_decorator.unwrap(python_func)
    980 
--> 981       func_outputs = python_func(*func_args, **func_kwargs)
    982 
    983       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
    439         # __wrapped__ allows AutoGraph to swap in a converted function. We give
    440         # the function a weak reference to itself to avoid a reference cycle.
--> 441         return weak_wrapped_fn().__wrapped__(*args, **kwds)
    442     weak_wrapped_fn = weakref.ref(wrapped_fn)
    443 

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint:disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

AttributeError: in user code:

    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:505 train_function  *
        outputs = self.distribute_strategy.run(
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:477 train_step  **
        self.compiled_metrics.update_state(y, y_pred, sample_weight)
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/compile_utils.py:386 update_state
        self._build(y_pred, y_true)
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/compile_utils.py:317 _build
        self._metrics, y_true, y_pred)
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/nest.py:1118 map_structure_up_to
        **kwargs)
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/nest.py:1214 map_structure_with_tuple_paths_up_to
        *flat_value_lists)]
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/nest.py:1213 <listcomp>
        results = [func(*args, **kwargs) for args in zip(flat_path_list,
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/nest.py:1116 <lambda>
        lambda _, *values: func(*values),  # Discards the path arg.
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/compile_utils.py:416 _get_metric_objects
        return [self._get_metric_object(m, y_t, y_p) for m in metrics]
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/compile_utils.py:416 <listcomp>
        return [self._get_metric_object(m, y_t, y_p) for m in metrics]
    /Users/madhubandru/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/compile_utils.py:437 _get_metric_object
        y_t_rank = len(y_t.shape.as_list())

    AttributeError: 'tuple' object has no attribute 'shape'

Sorry should have checked the comment above that its been fixed in master branch. The current solution untill 1.0.3 is released is below:

@deepakkumar1984 Thank you for the suggestion, even I overlooked that fix is pushed to master.

I have uninstalled AutoKeras and installed the new version 1.0.3 from git using below command.

! pip install git+https://github.com/keras-team/autokeras

Then I started executing my code to fit the model.

from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape) # (60000, 28, 28)
print(y_train.shape) # (60000,)
print(y_train[:3]) # array([7, 2, 1], dtype=uint8)

import autokeras as ak

# Initialize the image classifier.
clf = ak.ImageClassifier(max_trials=2) # It tries 10 different models.

# Feed the image classifier with training data.

clf.fit(x_train, y_train,epochs=3)

There is no ERROR but,clf.fit(x_train, y_train,epochs=3) running for longer time and its more than 20mins+ and still running. Any suggestions please? I am using MNIST data.

Was this page helpful?
0 / 5 - 0 ratings