peanut
peanut

Reputation: 143

How to download TensorFlow trained model on Google Colab?

I am a newbie and researching about Machine Learning, I have created a model on Google Colab.

My goal is to use that model for offline predicting in an Android app. So I need to download the trained model.

The only thing I know is I need to save my model as a .pb file in order to make my Android app. I have searched around, maybe there are answers but they were too short for me to understand So need very detail answers.

This is my Test.ipynb file, Can someone spend a little bit time to train that model and see if we can download it to local drive.

Upvotes: 3

Views: 13761

Answers (4)

Micheal Don
Micheal Don

Reputation: 48

model.save("your_model.h5") #save your model as a file
#check if your_model.h5 is here with ls command then download
!ls
from google.colab import files
files.download('your_model.h5')

**NOTE: I imported following these before:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import load_model

Upvotes: 0

Mustafa Karakaş
Mustafa Karakaş

Reputation: 1

!mkdir -p saved_model
saved_model_dir = 'saved_model/my_model'
model.save(saved_model_dir) 

#Convert the model

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) 

tflite_model = converter.convert()

#Save the model.
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

From the tensorflow documentation.

Upvotes: 0

Jakob
Jakob

Reputation: 893

This should do it:

import tensorflow as tf
from google.colab import files


# Specify export directory and use tensorflow to save your_model
export_dir = './saved_model'
tf.saved_model.save(your_model, export_dir=export_dir)

Note that the export directory contains several files, but if you only want to download the .pb file the following should do it.

# Download the model
files.download(export_dir + '/saved_model.pb')

Upvotes: 0

tangy
tangy

Reputation: 3276

This is how I save and download model files from Collab.

  1. Save only the trainable variables(These are my store/restore fns):

code is as below :

def store(sess_var, model_path):        
    if model_path is not None:
        saver = tf.train.Saver(var_list=tf.trainable_variables())
        save_path = saver.save(sess_var, model_path)
        print("Model saved in path: %s" % save_path)
    else:
        print("Model path is None - Nothing to store")



def restore(sess_var, model_path):
    if model_path is not None:
        if os.path.exists("{}.index".format(model_path)):
            saver = tf.train.Saver(var_list=tf.trainable_variables())
            saver.restore(sess_var, model_path)
            print("Model at %s restored" % model_path)
        else:
            print("Model path does not exist, skipping...")
    else:
        print("Model path is None - Nothing to restore")
  1. Compress the directory where you stored your model - Make sure it contains nothing else: !tar -czvf model.tar.gz models/

  2. Download the model:

    from google.colab import files files.download('model.tar.gz')

Since your only storing the trainable variables instead of the entire session, the size of the model is small and thus it is able to download. Be sure to use Chrome - I've not been able to get the last snippet working on Firefox.

Upvotes: 2

Related Questions