Reputation: 443
I am doing transfer learning and I am using VGG16 model. I am fine tuning my model by feature extraction. Then I have trained my final model and I have pickled its weight. Here is my code
def prediction(array):
size = 224
array = image_array(filename , size)
print(array.shape)
array = np.array(array , dtype = np.float64)
array = np.reshape(array, (1,224,224,3))
print(array.shape)
final_array = preprocess_input(array)
vgg16 = VGG16(weights='imagenet', include_top=False)
features = vgg16.predict(final_array)
image = features.reshape(features.shape[0] , -1)
#return image
loaded_model = pickle.load(open('vgg16.sav', 'rb'))
#print(image.shape)
array = np.asarray(array)
y_predict = loaded_model.predict(array)
when i call this function I am getting error in line y_predict = loaded_model.predict(array) I am getting
AttributeError: 'Sequential' object has no attribute '_built'
Upvotes: 1
Views: 3662
Reputation: 11225
You shouldn't use picke.dump
to save the weights and load as a model. Instead use the provided functions model.save(filename)
or model.save_weights(filename)
to save the model or just the weights respectively. In your case you can do:
vgg16.save('vgg16.h5')
# ...
loaded_model = keras.models.load_model('vgg16.h5')
You'll need h5py
package to use these functions.
Upvotes: 3