How to use an exported model from google colab in Pycharm - python

I have a LSTM Keras Tensorflow model trained and exported in .h5 (HDF5) format.
My local machine does not support keras tensorflow. I have tried installing. But does not work.
Therefore, i used google colabs and exported the model.
I would like to know, how i can use the exported model in pycharm
Edit : I just now installed tensorflow on my machine
Thanks in Advance

Found the answer :
I ve exported the model as follows
model.save('/content/drive/My Drive/Colab Notebooks/model.h5')
Then i downloaded the file and saved in the folder where my other codes are. I have installed tensorflow.
Next i load the code and predicted using the saved model as follows.
import keras
model=keras.models.load_model('/content/drive/My Drive/Colab Notebooks/model.h5')
model.predict(instace)

You still need keras and tensorflow to use the model.

The accepted answer is correct but it misses that you first need to mount the "/content/drive"
from google.colab import drive
drive.mount('/content/drive')
Then you can save the weights of the model:
model.save_weights('my_model_weights.h5')
..or even save the whole model :
model.save('my_model.h5')
Once done, disconnect your mounted point using:
drive.flush_and_unmount()

Related

Are tensorflow2 keras models compatible with the Edge TPU?

I am trying to convert the keras model located here: https://www.tensorflow.org/lite/performance/post_training_integer_quant to a model that can run on the Edge TPU. What this example fails to mention is that in order to compile the model to something that is runnable on the TPU it needs to get saved first as a "frozen model" to a .pb file. I tried doing that but the Edge tpu compiler still complains that the model's tensor size's are still not constant. I also read somewhere that tensorflow2 does not support frozen graphs yet. Is that true, and if so, how can I convert this keras model to something that is runnable on the TPU? Does a complete guide for how to write a TPU compatible model exist somewhere?

Load custom trained spaCy model

I am trying to load a spaCy text classification model that I trained previously. After training, the model was saved into the en_textcat_demo-0.0.0.tar.gz file.
I want to use this model in a jupyter notebook, but when I do
import spacy
spacy.load("spacy_files/en_textcat_demo-0.0.0.tar.gz")
I get
OSError: [E053] Could not read meta.json from spacy_files/en_textcat_demo-0.0.0.tar.gz
What is the correct way to load my model here?
You need to either unzip the tar.gz file or install it with pip.
If you unzip it, that will result in a directory, and you can give the directory name as an argument to spaCy load.
If you use pip install, it will be put with your other libraries, and you can use the model name like you would with a pretrained spaCy model.

Trying to convert .onnx model to .pb model using onnx-tf library

I am trying to convert .onnx model to .pb format by using onnx-tf package, however, after invoking onnx_tf.backend.prepare function, the python kernel crashes. I am using the code below:
import onnx
from onnx_tf.backend import prepare
import tensorflow
onnx_model = onnx.load(<path-to-model>) # load onnx model
tf_rep = prepare(onnx_model) # <------ That's where python crashes
tf_rep.export_graph(<output-path>) # export the model
I have double-checked package version dependencies, as wrong dependencies caused different errors while loading the .onnx model, and those are as follows:
onnx==1.7.0
onnx-tf==1.6.0
tensorflow==2.2.0
tensorflow-addons==0.10.0
torch==1.6.0+cu101
However, the dependencies seem to be correct, according to Github pages.
There were large changes between ONNX 1.6 and 1.7 that caused many dependency issues. Try with Onnx 1.6, which may be the source of your issue. Where did you find 1.7 mentioned as the required version?
Additionally, you mention the Python kernel crashing? Are you running in a Jupyter notebook?
I've found this package to be a lot more unstable in Juypter notebook, and not print out all of the relevant errors. If you could test your system in a standalone Python script you may get more information.
Importing onnx and onnx-tf related libraries before tf and pytorch should help. The problem is probably caused by a version clash since both onnx and onnx-tf use tf.
Can you install onnx-tf of this commit 7d8fa7d88fab469253d75e5e11cf9cdcb90104c4
Between order of imports and this commit the issue was solved for me

How to generate HDF5/H5 file in Keras with existing Python project

I am trying to get HDF5/H5 file from existing project in keras.
this attention_ocr is related to OCR written in python. I would like to generate HDF5/H5 file so I can convert that with tensorflowjs_converter[ref] and will use in browser.
Reference:
How to import a TensorFlow SavedModel into TensorFlow.js
Importing a Keras model into TensorFlow.js
I am looking for installing keras environment and generating HDF5/H5 file.
Once your model is trained in keras, saving it as an HDF5 is simply one line:
my_model.save('my_filename.h5')

Tensorflow with poets

I have questions to ask about why tensorflow with poets was not able to classify the image i want. I am using Ubuntu 14.04 with tensorflow installed using docker. Here is my story:
After a successful retrained on flower category following this link here. I wish to train on my own category as well, I have 10 classes of images and they are well organized according to the tutorial. My photos were also stored in the tf_files directory and following the guide i retrain the inception model on my category.
Everything on the retraining went well. However, as I tired to classify the image I want, I was unable to do so and I have this error. I also tried to look for py file in /usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/errors_impl.py but my dist-packages were empty! Can someone help me in this! Where can I find the py files? Thank you!
Your error indicates that it is unable to locate the file. I would suggest you to execute the following command in the directory where you have the graph and label files
python label_image.py exact_path_to_your_testimage_file.jpg

Categories

Resources