tensorflow model.load problems - python-3.x

I was working on tensorflow and tried to save and load a model. model resides in below file
model = keras.Sequential()
model.add(keras.layers.Dense(785, activation ='sigmoid' ))
model.add(keras.layers.Dense(25, activation = 'sigmoid'))
model.add(keras.layers.Dense(10, activation = 'sigmoid'))
model.compile(optimizer=tf.train.GradientDescentOptimizer(0.01),
loss='mse',
metrics=['mae'])
model.fit(X,Y,epochs = 20, callbacks=[history])
f = h5py.File(r'C:\Users\akash\Desktop\Big Data\Model\model1', "w")
tf.keras.models.save_model(
model,
f,
overwrite=True,
include_optimizer=True
)
and my load file is as below
model1 = tf.keras.models.load_model(
r'C:\Users\akash\Desktop\Big Data\Model\model1',
custom_objects=None,
compile=True
)
model1.compile(optimizer=tf.train.GradientDescentOptimizer(0.01),
loss='mse',
metrics=['mae'])
I had to compile my model again as tensorflow requires you to do so and does not allow optimisers to be saved
and due to this im getting the below error
Using TensorFlow backend.
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
Traceback (most recent call last):
File "C:/Users/akash/Desktop/Big Data/scripts/load_model.py", line 21, in <module>
metrics=['mae'])
File "C:\Python\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 426, in _method_wrapper
method(self, *args, **kwargs)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 525, in compile
metrics, self.output_names)
AttributeError: 'Sequential' object has no attribute 'output_names'

Maybe this helps you:
# MLP for Pima Indians Dataset Serialize to JSON and HDF5
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
import numpy
import os
# fix random seed for reproducibility
numpy.random.seed(7)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
# later...
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
# evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
score = loaded_model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))

Related

NotFoundError when dumping a model using pickle

I am trying to save a model in a new file if the model does not yet exists. When I try to dump the model in a new file, the file does get created but the trained model does not get dumped with it.
This is my code:
if os.path.exists("trained_model.pickle"):
print("loading trained model")
with open("trained_model.pickle","rb") as file:
pickle.load(model)
else:
print('creating and training a new model')
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=10, activation=tf.nn.softmax))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=4)
with open("trained_model.pickle","wb") as file:
pickle.dump(model, file))
The mistake in the above code is in the pickle.load(model) line. It should be pickle.load(file)
import tensorflow as tf
import os
import pickle
if os.path.exists("trained_model.pickle"):
print("loading trained model")
with open("trained_model.pickle","rb") as file:
pickle.load(file)
else:
print('creating and training a new model')
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=256, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=10, activation=tf.nn.softmax))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=4)
with open("trained_model.pickle","wb") as file:
pickle.dump(model, file)
with open("trained_model.pickle", "rb") as infile:
test_model = pickle.load(infile)
test_model.summary()

Why doesn't Python want to read pictures?

I don’t understand why my program doesn’t read pictures. I tried many different ways, but they only solved a small part of the problems. The main one remained - OSError: Initializing from file failed.
Here is what the program shows after execution (full text):
Traceback (most recent call last):
File "C:\Users\sanch\Downloads\test.py", line 123, in <module>
train_data = pd.read_csv("C:\\Users\\sanch\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\tensorflow\\tensorflow-for-poets-2-master\\tf_files\\test_data", delimiter=',', nrows=nRowsRead)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 685, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 457, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 1135, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 1917, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 382, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 693, in pandas._libs.parsers.TextReader._setup_parser_source
OSError: Initializing from file failed
from mpl_toolkits.mplot3d import Axes3D # For Basic ploting
from sklearn.preprocessing import StandardScaler # Preprocessing
from sklearn import preprocessing # Preprocessing
from random import seed
import tensorflow as tf
from sklearn.naive_bayes import GaussianNB # import gaussian naive bayes model
from sklearn.tree import DecisionTreeClassifier # import Decision tree classifier
from sklearn import metrics # Import scikit - learn metrics module for accuracy calculation
import matplotlib.image as mpimg
import matplotlib.pyplot as plt# plotting
import numpy as np # linear algebra
import os # accessing directory structure
import pandas as pd # data processing, CSV file I / O(e.g.pd.read_csv)
for dirname, _, filenames in os.walk("C:\\Users\\sanch\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\tensorflow\\tensorflow-for-poets-2-master\\tf_files\\test_data"):
for filename in filenames:
print(os.path.join(dirname))
def create_model(lyrs=[8], act='linear', opt='Adam', dr=0.0):
# set random seed for reproducibility
seed(42)
tf.random.set_seed(42)
model = tf.keras.Sequential()
# create first hidden layer
model.add(tf.keras.Dense(lyrs[0], input_dim=X_train.shape[1], activation=act))
# create additional hidden layers
for i in range(1, len(lyrs)):
model.add(tf.keras.Dense(lyrs[i], activation=act))
# add dropout, default is none
model.add(tf.keras.Dropout(dr))
# create output layer
model.add(tf.keras.Dense(1, activation='sigmoid')) # output layer
model.complete(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
def create_model(lyrs=[5], act='linear', opt='Adam', dr=0.0):
# set random seed for reproducibility
seed(42)
tf.random.set_seed(42)
model = tf.keras.Sequential()
# create first hidden layer
model.add(tf.keras.Dense(lyrs[0], input_dim=X_train.shape[1], activation=act))
# create additional hidden layers
for i in range(1, len(lyrs)):
model.add(tf.keras.Dense(lyrs[i], activation=act))
# add dropout, default is none
model.add(tf.keras.Dropout(dr))
# create output layer
model.add(tf.keras.Dense(1, activation='sigmoid')) # output layer
model.complete(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
model = create_model()
print(model.summary())
# train model on full train set, witch 80/20 CV split
trainig = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2, verbose=0)
val_acc = np.mean(trainig.history['val_acc'])
print("\n%s: %.2f%%" % ('val_acc', val_acc * 100))
model = create_model()
print(model.summary())
# train model on full train set, witch 80/20 CV split
trainig = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2, verbose=0)
val_acc = np.mean(trainig.history['val_acc'])
print("\n%s: %.2f%%" % ('val_acc', val_acc * 100))
def create_model(lyrs=[10], act='linear', opt='Adam', dr=0.0):
# set random seed for reproducibility
seed(42)
tf.random.set_seed(42)
model = tf.keras.Sequential()
# create first hidden layer
model.add(tf.keras.Dense(lyrs[0], input_dim=X_train.shape[1], activation=act))
# create additional hidden layers
for i in range(1, len(lyrs)):
model.add(tf.keras.Dense(lyrs[i], activation=act))
# add dropout, default is none
model.add(tf.keras.Dropout(dr))
# create output layer
model.add(tf.keras.Dense(1, activation='sigmoid')) # output layer
model.complete(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
model = create_model()
print(model.summary())
# train model on full train set, witch 80/20 CV split
trainig = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2, verbose=0)
val_acc = np.mean(trainig.history['val_acc'])
print("\n%s: %.2f%%" % ('val_acc', val_acc * 100))
# create model
model = tf.keras.KerasClassifier(build_nf=create_model, verbose=0)
# define the grid search parameters
batch_size = [16, 32, 64]
epochs = [50, 100]
param_grid = dict(batch_size=batch_size, ephochs=epochs)
# search the grid
grid = tf.keras.GridSearchCV(estimator=model,
param_grid=param_grid,
cv=3,
verbose=2) # include n_jobs=-1 if you are using CPU
grid_result = grid.fit(X_train, y_train)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_["params"]
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
if __name__ == '__main__':
nRowsRead = None # specify No.of row. 'None' for whole data
# test_mosaic.scv may have more rows in reality, but we are only loading / previewing the first 1000 rows
train_data = pd.read_csv("C:\\Users\\sanch\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\tensorflow\\tensorflow-for-poets-2-master\\tf_files\\test_data", delimiter=',', nrows=nRowsRead)
nRow, nCol = train_data.shape
print(f'Yhere are {nRow} rows and {nCol} columns')
X_train = train_data.drop('Label', axis=1)
X_test = test_data.drop('Label', axis=1)
y_train = train_data['Label']
y_test = test_data['Label']
Perhaps I did not write everything correctly, I do not know. Please help, I have very little time to show this project. I don't know what to do anymore.

'KerasClassifier' object has no attribute 'loss'

I am doing churn prediction using keras. I have used column transformer from Sklearn. My code is--
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf
clf = KerasClassifier(keras_classifier_wrapper, epochs=20, batch_size=50, verbose=0)
categorical_pipe = Pipeline([
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
numerical_pipe = Pipeline([
('imputer', SimpleImputer(strategy='median'))
])
preprocessing = ColumnTransformer(
[('cat', categorical_pipe, cat_var1),
('num', numerical_pipe, num_var1)])
model3 = Pipeline([
('preprocess', preprocessing),
('keras_clf', clf)
])
model3.fit(X_train, y_train)
But it showing an error-
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-162-1f0472b386ae> in <module>()
----> 1 model3.fit(X_train, y_train)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/wrappers/scikit_learn.py in fit(self, x, y, **kwargs)
157 self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
158
--> 159 if (losses.is_categorical_crossentropy(self.model.loss) and
160 len(y.shape) != 2):
161 y = to_categorical(y)
AttributeError: 'KerasClassifier' object has no attribute 'loss'
Can you plz tell me why this error is showing and how to solve it.
Thanks in advance
problem is in your keras_classifier_wrapper function
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf # should be return classifier
you are trying to return clf but there is no clf it is defined afterwards. try to return classifier then it will work

Keras fit_generator raise You must compile your model before using it Error

I try to build a CNN + LSTM model by Keras to train a model for video classification task. Firstly, A simple model was built and trained with mock data, 'fit()' api, also it works!
But actually, what is used to train this model is a video dataset, it's so big that can't be loaded into memory. so I need a generator, in this place, I also write a mock generator which generate data with shape same as mock data method. Also, fit_generator API replaces fit.
When I run train_gen function, I get following error:
File "lstm.py", line 48, in <module>
train_gen()
File "lstm.py", line 45, in train_gen
model.fit_generator(data_generator.mock_generator(batch_size=32, num_classes=16), steps_per_epoch=1000, epochs=20)
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1415, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training_generator.py", line 39, in fit_generator
model._make_train_function()
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 485, in _make_train_function
raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.
I tried to solve this problem by searching in stack overflow and google, and do not find exact solution. There are some similar questions can be solved by specifying LSTM input_shape and output size, or add model.compile().
The following snippet code is runnable, and completely same as that mentioned above.
import keras
from keras.models import Sequential
from keras.layers import Input, Embedding, LSTM, Dense, Reshape
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.wrappers import Bidirectional
from keras.optimizers import Adam
import numpy as np
import os, random
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""
use_dropout = True
metrics = ['accuracy']
#mock data generator
def mock_generator(batch_size, input_length, input_dims, num_classes=16):
while True:
yield np.random.random((batch_size, input_length, input_dims)), keras.utils.to_categorical(np.random.randint(num_classes, size=(batch_size, 1)), num_classes=num_classes)
#mock data with shape as data generator
def mock_data(batch_size, input_length, input_dims, num_classes=16):
if True:
return np.random.random((batch_size, input_length, input_dims)), keras.utils.to_categorical(np.random.randint(num_classes, size=(batch_size, 1)), num_classes=num_classes)
#construct model, lstm units is fixed
def bi_lstm(input_shape, num_classes=16):
model = Sequential()
model.add(Bidirectional(LSTM(100, return_sequences=True, activation='relu', input_shape=input_shape), merge_mode='concat'))
model.add(Bidirectional(LSTM(100, activation='relu', input_shape=(input_shape[0],100), return_sequences=False), merge_mode='concat'))
if use_dropout:
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(BatchNormalization())
model.add(Activation('softmax'))
optimizer = Adam(lr=1e-5, decay=1e-6)
model.compile(loss='categorical_crossentropy', optimizer=optimizer,
metrics=metrics)
return model
#fit api run successfully
def train():
input_length=10
input_dims=128
num_classes=10
model = bi_lstm((input_length, input_dims), num_classes)
x_train, y_train = mock_data(32, input_length, input_dims, num_classes)
model.fit(x_train, y_train, epochs=20, batch_size=32)
#fit_generator api raise error!
def train_gen():
input_length=10
input_dims=128
num_classes=10
model = bi_lstm((input_length, input_dims), num_classes)
generator = mock_generator(32, input_length, input_dims, num_classes)
model.fit_generator(generator, steps_per_epoch=1000, epochs=20)
#test mock generator function
def test_mock_gen():
result = mock_generator(32,10,128,16)
for i in range(2):
x, y = result.next()
print x.shape
print y.shape
if __name__ == '__main__':
train()
train_gen()
#test_mock_gen()
The error you are getting couldn't be more explicit: model has been declared as a local object on the train() function and is not know by the function train_gen(). Define your model as a global variable (ex. in main) and it will work.

Saving Keras model fails after renaming layers

I have a problem with renaming layers. Below is the simplest example illustrating the problem:
from keras.layers import Dense, Conv2D, Flatten, Input
from keras.models import Model
inputs = Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
print(model.summary())
for i, layer in enumerate(model.layers):
layer.name = 'layer' + str(i)
print(model.summary())
model.save('temp')
It fails with the message:
Traceback (most recent call last):
File "scripts/save_load.py", line 24, in <module>
model.save('temp')
File "/lib/python3.6/site-packages/keras/engine/topology.py", line 2416, in save
save_model(self, filepath, overwrite)
File "/lib/python3.6/site-packages/keras/models.py", line 101, in save_model
'config': model.get_config()
File "/lib/python3.6/site-packages/keras/engine/topology.py", line 2281, in get_config
new_node_index = node_conversion_map[node_key]
KeyError: 'layer0_ib-0'
What am I doing wrong?
I know I can pass names to the layer constructor, it seems not to fail in this case, but is there any chance to improve my solution?

Resources