18zuzu18
18zuzu18

Reputation: 33

How to fix 'AttributeError: 'list' object has no attribute 'shape'' error in python with Tensorflow / Keras when loading Model

Im trying to save my Model in Keras and then load it but when it try to use the loaded Model it trows an Error

Python Vesion: 3.6.8 Tensorflow Version: 2.0.0-beta1 Keras Version: 2.2.4-tf

Here is my Code:

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf
from tensorflow import keras

print("Tensorflow Version:")
print(tf.version.VERSION)
print("Keras Verstion:")
print(tf.keras.__version__)

minst = tf.keras.datasets.mnist # 28x28 0-9

(x_train, y_train), (x_test, y_test) = minst.load_data()

x_train = tf.keras.utils.normalize(x_train, axis=1)
x__test = x_test;
x_test = tf.keras.utils.normalize(x_test, axis=1)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=1)

val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss, val_acc)

model.save('epic_num.h5')
print("Saved")
# Loading

nmodel = keras.models.load_model("epic_num.h5")
import numpy as np;


# Test
predics = nmodel.predict([x_test])
print(predics)

import matplotlib.pyplot as plt

plt.imshow(x__test[11], cmap=plt.cm.binary)
plt.show()

print(np.argmax(predics[11]))

The Output:

C:\Users\minec\PycharmProjects\TFMLTest3\venv\Scripts\python.exe C:/Users/minec/PycharmProjects/TFMLTest3/main.py
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
Tensorflow Version:
2.0.0-beta1
Keras Verstion:
2.2.4-tf
2019-08-17 17:02:25.535385: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
WARNING: Logging before flag parsing goes to stderr.
W0817 17:02:25.636994 14512 deprecation.py:323] From C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\ops\math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Train on 60000 samples

   32/60000 [..............................] - ETA: 2:39 - loss: 2.3092 - accuracy: 0.0938
 1120/60000 [..............................] - ETA: 7s - loss: 1.7317 - accuracy: 0.5580  
...
accuracy: 0.9590
 9600/10000 [===========================>..] - ETA: 0s - loss: 0.1232 - accuracy: 0.9624
10000/10000 [==============================] - 0s 32us/sample - loss: 0.1271 - accuracy: 0.9610
0.1271383908316493 0.961
Saved
W0817 17:02:29.220302 14512 hdf5_format.py:197] Sequential models without an `input_shape` passed to the first layer cannot reload their optimizer state. As a result, your model isstarting with a freshly initialized optimizer.
Traceback (most recent call last):
  File "C:/Users/minec/PycharmProjects/TFMLTest3/main.py", line 41, in <module>
    predics = nmodel.predict([x_test])
  File "C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 821, in predict
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 705, in predict
    x, check_steps=True, steps_name='steps', steps=steps)
  File "C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2335, in _standardize_user_data
    self._set_inputs(cast_inputs)
  File "C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2553, in _set_inputs
    outputs = self(inputs, **kwargs)
  File "C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 662, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 262, in call
    outputs = layer(inputs, **kwargs)
  File "C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 662, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "C:\Users\minec\PycharmProjects\TFMLTest3\venv\lib\site-packages\tensorflow\python\keras\layers\core.py", line 580, in call
    inputs, (tensor_shape.dimension_value(inputs.shape[0]) or
AttributeError: 'list' object has no attribute 'shape'

Process finished with exit code 1

Upvotes: 3

Views: 4004

Answers (1)

Sam Chats
Sam Chats

Reputation: 2321

Try using

nmodel.predict(x_test)

instead of

nmodel.predict([x_test])

(remove the brackets).

Upvotes: 2

Related Questions