Load error when loading h5 saved by simple dense model

I used following code 1 to train and save model and code #2 to load but loading results in error. Any idea? Thanks.,

code #1:

# Using neural net to do a classification task.

import tensorflow as tf
import pandas as pd
import matplotlib as plt
import sys
import time
import re
import numpy as np
import helper
from tensorflow import keras

DEBUG=0
CONFIG_ENABLE_PLOT=0
CONFIG_EPOCHS=30
CONFIG_BATCH_SIZE=32

for i in sys.argv:
    print("Processing ", i)
    try:
        if re.search("epochs=", i):
            CONFIG_EPOCHS=int(i.split('=')[1])

        if re.search("batch_size=", i):
            CONFIG_BATCH_SIZE=int(i.split('=')[1])

    except Exception as msg:
        print("No argument provided, default values will be used.")

print("epochs: ", CONFIG_EPOCHS)
print("batch_size: ", CONFIG_BATCH_SIZE)

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()
print("X_train_full/y_train_full/X_test/y_test: ", X_train_full.shape, y_train_full.shape, X_test.shape, y_test.shape)

SEPARATOR=10000
X_valid, X_train = X_train_full[:SEPARATOR] / 255.0, X_train_full[SEPARATOR:]/255.0
y_valid, y_train = y_train_full[:SEPARATOR], y_train_full[SEPARATOR:]
X_test = X_test / 255.0

print("X_valid/X_train/y_valid/y_train: ", X_valid.shape, X_train.shape, y_valid.shape, y_train.shape)

class_names = ["T-shirt/top","Trouser", "Pullover", "Dress", "Coat" , "Sandal", "Shirt", "Sneaker","Bad","Ankle boot"]

model=keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape = [28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(keras.layers.Dense(30, activation="softmax"))

print("model summary: ", model.summary())

model.compile(loss="sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])
history=model.fit(X_train, y_train, epochs=CONFIG_EPOCHS, batch_size=CONFIG_BATCH_SIZE, validation_data=(X_valid, y_valid))

if CONFIG_ENABLE_PLOT:
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.pyplot.grid(True)
    plt.pyplot.gca().set_ylim(0, 1)
    plt.pyplot.show()

model.evaluate(X_test, y_test)

if DEBUG:
    print("model layers: ", model.layers)

weights, biases  = model.layers[1].get_weights()

if DEBUG:
    print("weights, biases (shapes): ", weights, biases, weights.shape, biases.shape)

model.save("p297.h5")
X_new = X_test[:3]
print("X_new shape: ", X_new.shape)
y_proba = model.predict(X_new)
print("y_proba (predict)(value): ", y_proba.round(2), "\ny_proba(shape)", np.array(y_proba).shape)

y_pred = model.predict_classes(X_new)
print("y_pred (predict_classes): ", y_pred)
print("y_test: ", y_test[:3])

code #2

# Using neural net to do a classification task.
# This has similar objective as p297.py, the difference
# is p297.py saves the training model and this one load
# the model and predicts the one from p297.py

import tensorflow as tf
import pandas as pd
import matplotlib as plt

from tensorflow import keras
print(tf.__version__)
print(keras.__version__)

CONFIG_ENABLE_PLOT=0

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()
print("X_train_full.shape: ", X_train_full.shape)
print("X_train_full.dtype: ", X_train_full.dtype)

X_valid, X_train = X_train_full[:5000] / 255.0, X_train_full[5000:]/255.0
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
X_test - X_test / 255.0
class_names = ["T-shirt/top","Trouser", "Pullover", "Dress", "Coat" , "Sandal", "Shirt", "Sneaker","Bad","Ankle boot"]

model = keras.models.load_model("p297.h5")
model.evaluate(X_test, y_test)

print("model layers: ", model.layers)
weights, biases  = model.layers[1].get_weights()
print("weights, biases (shapes): ", weights, biases, weights.shape, biases.shape)
model.save("p297-2.h5")
X_new = X_test[:3]
y_proba = model.predict(X_new)
print(y_proba.round(2))

y_pred = model.predict_classes(X_new)

#output:

python3 p297-load.py
...
 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)])
1.14.0
2.2.4-tf
X_train_full.shape:  (60000, 28, 28)
X_train_full.dtype:  uint8
Traceback (most recent call last):
  File "p297-load.py", line 38, in <module>
    model = keras.models.load_model("p297.h5")
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py", line 146, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/hdf5_format.py", line 210, in load_model_from_hdf5
    model_config = json.loads(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'

What is your h5py version?

ok, I was not sure what h5py is and researched a bit and it shows:
h5py (3.1.0)

What is your TF version?
As I remember that we had something similar at:

2 Likes

I am showing
tensorflow-gpu (1.14.0)
which was installed by “pip3 install tensorflow-gpu”

TF 1.x i s end of life. Can you try with TF 2.6?

2 Likes

yeah however I am not sure if 2.6 available for gpu version?
This is what i am showing:
root@sriov-guest:~/dev-learn/gpu/pytorch/port-effort-from-tflow-2nd# pip3 install tensorflow-gpu==2.6
Collecting tensorflow-gpu==2.6
Could not find a version that satisfies the requirement tensorflow-gpu==2.6 (from versions: 0.12.1, 1.0.0, 1.0.1, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.4.0, 1.4.1, 1.5.0, 1.5.1, 1.6.0, 1.7.0, 1.7.1, 1.8.0, 1.9.0, 1.10.0, 1.10.1, 1.11.0, 1.12.0, 1.12.2, 1.12.3, 1.13.1, 1.13.2, 1.14.0, 2.0.0a0, 2.0.0b0, 2.0.0b1)
No matching distribution found for tensorflow-gpu==2.6

I am specificaly using either nvidia or radeon for computation.
thanks.,

GPU is now tensorflow CPU is tensorflow-cpu

1 Like

P.s. AMD instead is GitHub - ROCmSoftwarePlatform/tensorflow-upstream: TensorFlow ROCm port

i keep getting this, there is no 2.6 version available:
oot@nonroot-MS-7B22:~/dev-learn/gpu/tflow/tensorflow/tflow-2nded# pip3 install tensorflow==2.6
Collecting tensorflow==2.6
Could not find a version that satisfies the requirement tensorflow==2.6 (from versions: 0.12.1, 1.0.0, 1.0.1, 1.1.0rc0, 1.1.0rc1, 1.1.0rc2, 1.1.0, 1.2.0rc0, 1.2.0rc1, 1.2.0rc2, 1.2.0, 1.2.1, 1.3.0rc0, 1.3.0rc1, 1.3.0rc2, 1.3.0, 1.4.0rc0, 1.4.0rc1, 1.4.0, 1.4.1, 1.5.0rc0, 1.5.0rc1, 1.5.0, 1.5.1, 1.6.0rc0, 1.6.0rc1, 1.6.0, 1.7.0rc0, 1.7.0rc1, 1.7.0, 1.7.1, 1.8.0rc0, 1.8.0rc1, 1.8.0, 1.9.0rc0, 1.9.0rc1, 1.9.0rc2, 1.9.0, 1.10.0rc0, 1.10.0rc1, 1.10.0, 1.10.1, 1.11.0rc0, 1.11.0rc1, 1.11.0rc2, 1.11.0, 1.12.0rc0, 1.12.0rc1, 1.12.0rc2, 1.12.0, 1.12.2, 1.12.3, 1.13.0rc0, 1.13.0rc1, 1.13.0rc2, 1.13.1, 1.13.2, 1.14.0rc0, 1.14.0rc1, 1.14.0, 2.0.0a0, 2.0.0b0, 2.0.0b1)
No matching distribution found for tensorflow==2.6

after installing 2.6, it worked wonderfully, thanks!
tensorboard 2.7.0
tensorboard-data-server 0.6.1
tensorboard-plugin-wit 1.8.0
tensorflow 2.6.0
tensorflow-estimator 2.6.0

This error likely occurs because the pip resolver is old. pip install --upgrade pip setuptools should solve this particular scenario

2 Likes

+1 Old python versions will do that too.

(tensorflow will run on CPU or GPU, tensorflow-cpu is 1/8 the size but only runs on CPU.)

1 Like

Yes it was implict as CPU only but It was just the mapping on the old naming as standard tensorflow was Tensorflow CPU (only).

1 Like

One side quetsion, related but not exactly this issue:
on nvidia equipped machine i can run the training now with tf2.6 but nvidia-smi does not show anything. If the ML code is pushing kernel onto GPU it should be visible in nvidia-smi?
how do i verify that training is done on GPU or CPU?
As a comparison, I can run simple vector algebra kernel on GPU by explicitly pushing to GPU and I can see nvidia-smi rightly shows the name of the executable:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.57.02    Driver Version: 470.57.02    CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:01:00.0 Off |                  N/A |
| 40%   31C    P2    52W / 215W |    298MiB /  7981MiB |     49%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A       973      G   /usr/lib/xorg/Xorg                 29MiB |
|    0   N/A  N/A      1207      G   /usr/bin/gnome-shell                7MiB |
|    0   N/A  N/A      3085      C   ./a.out                           257MiB |
+-----------------------------------------------------------------------------+