I'm trying to load a trained .onnx model (from a neural-style-transfer algorithm) into cv2. I've seen that there is a
cv.dnn.readNetFromONNX()
function, but there is no such function in cv2. I can't seem to import or load opencv as cv, and as such can't seem to load my model in cv2. Does anyone know a solution?
I've basically trained a model with
https://github.com/pytorch/examples/blob/master/fast_neural_style/neural_style/neural_style.py#L122-L150
this script, and made an export of an onnx model by adding
torch.onnx.export(style_model, dummy_input, "chipsoft_mod.onnx", verbose=True)
Now I want to run the trained model trough the cv2 reader, but I fail spectacularly.
Update your opencv to a newer version. It should help.
pip install opencv-python==4.1.0.25
Related
I am attempting to use a software called YOLOv5 in my python code for a Kaggle competition. The only thing is it requires you not to use the internet. I have the yolov5 file already loaded into the kaggle code. but am unsure how to call in in my code.
I've tried loading it in the following way but keep getting errors.
import torch
torch.save('yolov5x6.pt', 'yolov5')
yolov5x6_model = torch.load('yolov5')
Yolov5 is a follow up version of yolo which is a neural network library in c language, also known as Darknet created by pjreddie.
It is an object detector model which can be trained to recognise objects in images or videos.
If you just want to detect some daily life object then you can just run inference on images/videos using python and trained weights and config file. You will find these files under the pretrained checkpoints section at the following link.
https://github.com/ultralytics/yolov5
I use the Python 3.7.4 with TensorFlow 2.0 and Keras 2.2.4-tf to train my own CNN model. Everything goes fine. I can use e.g. model.save(my_model), and then use it in other Python scripts. Problem appears when I want to use trained model in OpenCV with its DNN module in C++. cv::dnn:readNetFromTensorflow(model.pb, model.pbtxt), takes as you can see two arguments, and I can't get the second .pbtxt file. So I decide to use .onnx format, because of its flexibility. The problem is that existing libraries keras2onnx takes only model from TensorFlow 1.*, and I want to avoid working with it. Example of code to convert it is presented below:
import tensorflow as tf
import onnx
import keras2onnx
model = tf.keras.models.load_model(my_model_folder_path)
onnx_model = keras2onnx.convert_keras(model, model.name)
onnx.save_model(onnx_model, model_name_onnx)
Is there some other ways to convert such model to onnx format?
The latest version of keras2onnx (in github master) supports TensorFlow 2.
You can install it like this:
pip install git+https://github.com/microsoft/onnxconverter-common
pip install git+https://github.com/onnx/keras-onnx
You need to create a file which can hold ONNX object. Visit https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb
import tensorflow as tf
import onnx
import keras2onnx
model = tf.keras.models.load_model('Model.h5')
onnx_model = keras2onnx.convert_keras(model, model.name)
file = open("Sample_model.onnx", "wb")
file.write(onnx_model.SerializeToString())
file.close()
I'm trying to finetune a GoogleNet network over a specific dataset but I'm having trouble loading it. What I try now is:
model = torchvision.models.googlenet(pretrained=True)
However I get an error:
AttributeError: module 'torchvision.models' has no attribute 'googlenet'
I have the latest version of torchvision but reinstalled just to be sure, the error is still there.
You can instead use the GoogLeNet inception_v3 model ("Rethinking the Inception Architecture for Computer Vision"):
import torchvision
google_net = torchvision.models.inception_v3(pretrained=True)
Working on TensorFlow object detection API, I trained faster RCNN model for ship recognition at my PC on tensorFlow (GPU) and generated frozen_inference_graph.pb file.
Now I want to use this model on my laptop which has tensorFlow(CPU).
How could I use this frozen_inference_graph.pb file.
Is there any other way?
what files do I need?
simply run the detector, you need to follow the guide
I am using Torch7 library for implementing neural networks. Mostly, I rely on pre-trained models. In Lua I use torch.load function to load a model saved as torch .t7 file. I am curious about switching to PyTorch( http://pytorch.org) and I read the documents. I couldn't find any information regarding the mechanisms to load a pre-trained model. The only relevant information I was able to find is this page:http://pytorch.org/docs/torch.html
But the function torch.load described in the page seems to load a file saved with pickle. If someone has additional information on loading .t7 models in PyTorch, please share it here.
The correct function is load_lua:
from torch.utils.serialization import load_lua
x = load_lua('x.t7')
As of PyTorch 1.0 torch.utils.serialization is completely removed. Hence no one can import models from Lua Torch into PyTorch anymore. Instead, I would suggest installing PyTorch 0.4.1 through pip in a conda environment (so that you can remove it after this) and use this repo to convert your Lua Torch model to PyTorch model, not just the torch.nn.legacy model that you cannot use for training. Then use PyTorch 1.xx to do whatever with it. You can also train your converted Lua Torch models in PyTorch this way :)