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'