1

I was trying to predict classes but it is giving me this error.

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in 30 im=ImageGrab.grab(bbox=(205,45,585,555)) 31 im.save('img.jpg') ---> 32 predictions = new_model.predict([prepare('img.jpg')]) 33 y=CATEGORIES[np.argmax(predictions[0][0])] 34

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing) 1011
max_queue_size=max_queue_size, 1012 workers=workers, -> 1013 use_multiprocessing=use_multiprocessing) 1014 1015 def reset_metrics(self):

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in predict(self, model, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing, **kwargs) 496 model, ModeKeys.PREDICT, x=x, batch_size=batch_size, verbose=verbose, 497 steps=steps, callbacks=callbacks, max_queue_size=max_queue_size, --> 498 workers=workers, use_multiprocessing=use_multiprocessing, **kwargs) 499 500

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in _model_iteration(self, model, mode, x, y, batch_size, verbose, sample_weight, steps, callbacks, max_queue_size, workers, use_multiprocessing, **kwargs) 424 max_queue_size=max_queue_size, 425 workers=workers, --> 426 use_multiprocessing=use_multiprocessing) 427 total_samples = _get_total_number_of_samples(adapter) 428 use_sample = total_samples is not None

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py in _process_inputs(model, mode, x, y, batch_size, epochs, sample_weights, class_weights, shuffle, steps, distribution_strategy, max_queue_size, workers, use_multiprocessing) 644 standardize_function = None 645 x, y, sample_weights = standardize( --> 646 x, y, sample_weight=sample_weights) 647 elif adapter_cls is data_adapter.ListsOfScalarsDataAdapter: 648 standardize_function = standardize

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split, shuffle, extract_tensors_from_dataset) 2344 # First, we build the model on the fly if necessary. 2345 if not self.inputs: -> 2346 all_inputs, y_input, dict_inputs = self._build_model_with_inputs(x, y) 2347 is_build_called = True 2348 else:

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _build_model_with_inputs(self, inputs, targets) 2570 else:
2571 cast_inputs = inputs -> 2572 self._set_inputs(cast_inputs) 2573 return processed_inputs, targets, is_dict_inputs 2574

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _set_inputs(self, inputs, outputs, training) 2645 first layer isn't FeatureLayer. 2646 """ -> 2647 inputs = self._set_input_attrs(inputs) 2648 2649 if outputs is None:

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

~\anaconda3\envs\tf\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _set_input_attrs(self, inputs) 2684 input_shape = (None,) 2685 else: -> 2686 input_shape = (None,) + tuple(inputs.shape[1:]) 2687 self._build_input_shape = input_shape 2688

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

import cv2
import tensorflow as tf
import numpy as np

CATEGORIES = ["gas","back","both"]


def prepare(filepath):
    IMG_SIZE = 256
    img_array = cv2.imread(filepath)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    img = np.reshape(new_array,[1,256,256,3])
    return img


model = tf.keras.models.load_model("trained.model")

prediction = model.predict_classes([prepare('img.jpg')])
print(CATEGORIES[int(prediction[0])])

2
  • which line raises the exception? Commented Apr 21, 2020 at 9:22
  • Please post the error and the line producing error. There has to be a minimal reproducible example. Commented Apr 21, 2020 at 9:23

1 Answer 1

2

You pass a list to model.predict_classes try giving it a numpy array instead:

prediction = model.predict_classes(prepare('img.jpg'))

Edit: apparently, your prepare function already has a first dim for the batch size so I removed the [np.newaxis, :]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.