Reputation: 21
I have implemented image classification machine learning model using tensorflow (python) the model is working properly and now i have to put model in production phase for that i am using sklearn joblib library and have also tried pickle library but i am getting error in both cases
model = Models.Sequential()
model.add(Layers.Conv2D(200,kernel_size=(5,5),activation='relu',input_shape=(150,150,3)))
model.add(Layers.Conv2D(180,kernel_size=(5,5),activation='relu'))
model.add(Layers.MaxPool2D(5,5))
model.add(Layers.Conv2D(50,kernel_size=(5,5),activation='relu'))
model.add(Layers.MaxPool2D(5,5))
model.add(Layers.Flatten())
model.add(Layers.Dense(180,activation='relu'))
model.add(Layers.Dense(100,activation='relu'))
model.add(Layers.Dense(50,activation='relu'))
model.add(Layers.Dropout(rate=0.5))
model.add(Layers.Dense(6,activation='softmax'))
model.compile(optimizer=Optimizer.Adam(lr=0.0001),loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.summary()
trained = model.fit(Images,Labels,epochs=25,validation_split=0.20)
test_images,test_labels = get_images('C:/Users/shrey/Desktop/img_classification/New folder/seg_test/seg_test/')
test_images = np.array(test_images)
test_labels = np.array(test_labels)
test_images = test_images / 255.0
model.evaluate(test_images,test_labels, verbose=1)
test_images,test_labels = get_images('C:/Users/shrey/Desktop/img_classification/New folder/seg_test/seg_test/')
test_images = np.array(test_images)
test_labels = np.array(test_labels)
test_images = test_images / 255.0
model.evaluate(test_images,test_labels, verbose=1)
#Lets predict the images from the "pred" folder.
In [12]:
pred_images,no_labels = get_images('C:/Users/shrey/Desktop/img_classification/New folder/seg_pred/')
#pred_images = tf.image.decode_jpeg(pred_images)
#pred_images = tf.cast(pred_images, tf.float32)
pred_images = np.array(pred_images)
pred_images.shape
from sklearn.externals import joblib
with open('model_pickle','wb') as f:
pickle.dump(model,f)
---------------------------------------------------------------------------
Type Error Trackback (most recent call last)
<ipython-input-43-5da5ca65d688> in <module>
1 with open('model_pickle','wb') as f:
----> 2 pickle.dump(model,f)
Type Error: can't pickle _thread._local objects
Upvotes: 2
Views: 1894
Reputation:
In the first program, we have built the model, fit the model and later saved the model as model.h5
to disk. In the next program, I am loading the saved model model.h5
and doing the prediction using the loaded model. You can download the dataset we are using in the program from here.
Build, Fit and Save the Model -
%tensorflow_version 2.x
print(tf.__version__)
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# define model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Model Summary
model.summary()
# 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))
# save model and architecture to single file
model.save("model.h5")
print("Saved model to disk")
Output -
2.2.0
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 12) 108
_________________________________________________________________
dense_1 (Dense) (None, 8) 104
_________________________________________________________________
dense_2 (Dense) (None, 1) 9
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
accuracy: 76.43%
Saved model to disk
Load the model and use for predict -
# load and evaluate a saved model
import tensorflow as tf
from numpy import loadtxt
from tensorflow.keras.models import load_model
# load model
model = load_model('model.h5')
# summarize model
model.summary()
# LOAD THE NEW DATASET HERE
dataset = loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# PREDICT
score = model.predict(X,verbose=0)
print(score.shape)
Output -
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 12) 108
_________________________________________________________________
dense_1 (Dense) (None, 8) 104
_________________________________________________________________
dense_2 (Dense) (None, 1) 9
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
(768, 1)
Hope this answers your question. Happy Learning.
Upvotes: 2