1

I followed the following tutorial in Google Colab to create a text generating RNN: https://www.tensorflow.org/tutorials/sequences/text_generation Then, I trained it with my own data. At the end, I also added the following code to save it to my Google Drive as a .h5 file, and it created a file in my drive.

model.save('my_model.h5')
uploaded = drive.CreateFile({'title': 'FILE NAME HERE.h5'})
uploaded.SetContentFile('my_model.h5')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

Then, I opened a new notebook and tried to load it like so:

downloaded = drive.CreateFile({'id': "FILE ID HERE"})
downloaded.GetContentFile('my_model.h5')
new_model = keras.models.load_model(downloaded)
new_model.summary()

However, it gives me the error, among others: "'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte"

I already tried following other articles that demonstrate how to achieve my goal, and this is what I got.

I would like to be able to continue training the model, without the original code if possible. How do I do that?

GoghDali
  • 21
  • 4

1 Answers1

0

Press Ctrl+Alt+P or look at the bottom left of the screen, there you can find both the upload and download snippets. Just adapt it to h5 files instead of txt files:

Upload:

# Import PyDrive and associated libraries.
# This only needs to be done once in a notebook.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once in a notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Create & upload a h5 file.
uploaded = drive.CreateFile({'title': 'my_keras_model.h5'})
uploaded.SetContentFile("my_keras_model.h5")
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

Download:

# Import PyDrive and associated libraries.
# This only needs to be done once per notebook.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Download a file based on its file ID.
#
# A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
file_id = 'laggVyWshwcyP6kEI-y_W3P8D26sz'
downloaded = drive.CreateFile({'id': file_id})
print('Downloaded content "{}"'.format(downloaded.GetContentFile("my_keras_model.h5")))

Hope this helps.

massigarg
  • 99
  • 7