lmoroney / dlaicourse

Notebooks for learning deep learning
5.66k stars 5.36k forks source link

how to save and load models that was trained using fit_generator #45

Closed sleekmike closed 4 years ago

sleekmike commented 4 years ago

Hello, I have been trying to reproduce the code(human or horse classification) in tensor flow in practise course (/Course 1 - Part 8 - Lesson 4 - Notebook.ipynb ) locally on my pc. It works so far but when I save a model with this line:

history = model.fit_generator( train_generator, steps_per_epoch=8,
epochs=15, verbose=1, validation_data = validation_generator, validation_steps=8)

try: model.save_weights('human_horse1.h5') except Exception as e: print("error in saving model: ", e)

and to load the saved: new_model = tf.keras.models.load_model('human_horse3.h5')

if I try to used the saved model to predict with the code:

new_model.predict('./Users/macbook/Desktop/image_classifier/human01-04.png')

I normally get this error: AttributeError: 'str' object has no attribute 'shape'

please how do I properly load and save models and use the model to predict locally on my pc. Kind regards.

JonathanSum commented 4 years ago

I am not really sure this can solve your issue. "shape" attribute is from numpy. Have you installed numpy yet?

sleekmike commented 4 years ago

Yes I have numpy and tensorflow 2.0 installed on my system.

On Fri, 4 Oct 2019 at 1:36 PM, Jonathan Sum notifications@github.com wrote:

I am not really sure this can solve your issue. "shape" attribute is from numpy. Have you installed numpy yet?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lmoroney/dlaicourse/issues/45?email_source=notifications&email_token=AI5IK7A5J4XAZDLEEJG6CT3QM42GPA5CNFSM4I5CNMUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEALQDTY#issuecomment-538378703, or mute the thread https://github.com/notifications/unsubscribe-auth/AI5IK7EJRYS5RM7XFM4ND73QM42GPANCNFSM4I5CNMUA .

cicimeng commented 4 years ago
    why i can't install tensorflow 2.0 upgrade doesn't work 

    Get Outlook for iOS

On Fri, Oct 4, 2019 at 9:40 PM +0800, "Mike Ohanu" notifications@github.com wrote:

Yes I have numpy and tensorflow 2.0 installed on my system.

On Fri, 4 Oct 2019 at 1:36 PM, Jonathan Sum notifications@github.com

wrote:

I am not really sure this can solve your issue. "shape" attribute is from

numpy. Have you installed numpy yet?

You are receiving this because you authored the thread.

Reply to this email directly, view it on GitHub

https://github.com/lmoroney/dlaicourse/issues/45?email_source=notifications&email_token=AI5IK7A5J4XAZDLEEJG6CT3QM42GPA5CNFSM4I5CNMUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEALQDTY#issuecomment-538378703,

or mute the thread

https://github.com/notifications/unsubscribe-auth/AI5IK7EJRYS5RM7XFM4ND73QM42GPANCNFSM4I5CNMUA

.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

JonathanSum commented 4 years ago

I think the problem is this code: new_model.predict('./Users/macbook/Desktop/image_classifier/human01-04.png'), and you should remove it.

And add these codes and use them: import numpy as np from keras.preprocessing import image

img_width, img_height = 150, 150 img = image.load_img('./Users/macbook/Desktop/image_classifier/human01-04.png', target_size = (img_width, img_height)) img = image.img_to_array(img) img = np.expand_dims(img, axis = 0) model.predict(img) If that does work, I hope someone who can come here and help you. Or you can show us your code, and I will try to fix it with my cpu only laptop this week.

sleekmike commented 4 years ago

Thanks a lot @JonathanSum, I used your code and it works like magic :).