Convert model Pytorch->ONNX->NCNN - onnx

Trying to run this example on your custom trained Yolov8 model.
Field of study I have a model best.pt
ncnn exporting Insturction
I do the export to ONNX format
pip install ultralytics yolo mode=export model={HOME}/best.pt format=onnx
simplify onnx model
pip install onnxsim pip install onnxruntime python -m onnxsim {HOME}/best.onnx {HOME}/best-sim.onnx
Error: [1] 67272 segmentation fault python -m onnxsim best.onnx best-sim.onnx
But I found a solution
onnx to ncnn
onnx2ncnn best-sim.onnx best.param best.bin
I changing a new model path and CLASS_NAME in the example (file yoloV8.cpp)
yolo.load_param("best.param"); yolo.load_model("best.bin");
const char *class_names[] = {"bus", "car", "truck"};
I changing layer name (file yoloV8.cpp). I looked at the structural model https://netron.app/
ex.extract("output0", out); ... std::vector<int> strides = {32};
Run Test image
I get house, but in the process of training the model, it is working.

Related

How to convert the tensor model into an onnx file

I have a tensorrt engine file, a builder in jetson nx2. But my onnx file is missing. How to convert the tensor model to onnx file?
e.g: a.engine file -> a.onnx
Please give me a suggestion thanks
All I retrieved from the search engine are from onnx to tensor model.
You will need Python 3.7-3.10,
Install tf2onnx using pip
pip install -U tf2onnx
use to following command. You will need to provide the following:
the path to your TensorFlow model (where the model is in saved model format)
a name for the ONNX output file
python -m tf2onnx.convert --saved-model tensorflow-model-path --output model.onnx
The above command uses a default of 13 for the ONNX opset. If you need a newer opset, or want to limit your model to use an older opset then you can provide the --opset argument to the command.
python -m tf2onnx.convert --saved-model tensorflow-model-path --opset 17 --output model.onnx
For checkpoint format:
python -m tf2onnx.convert --checkpoint tensorflow-model-meta-file-path --output model.onnx --inputs input0:0,input1:0 --outputs output0:0
Follow the official tf2onxx repository to learn more: https://github.com/onnx/tensorflow-onnx

how to make yolov7 training with 1 class from cocodataset

I am working on yolov7, train.py files.
I want to use cocodataset, but take 1 class for training: person. Coco have 80 class.
Can i control this from train.py?
Train py has ;
parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
option. But i have no idea how can i use this command.
Also, train log says;
tensorboard: Start with 'tensorboard --logdir runs/train', view at http://localhost:6006/
But page gives nothing.
if you want to use all 80 classes you have to use detect.py. With train.py you can train your custom dataset to detect your custom object.
Tensorboard. First, you have to install with
$ pip install tensorboard
if you have trained with train.py, you can run tensorboard with this command:
$ tensorboard --logdir=runs/train
And you can see results in http://localhost:6006/

module 'tensorflow_hub' has no attribute 'KerasLayer'

When I'm trying to retrain the model with tensorflow it shows an error:
**error module 'tensorflow_hub' has no attribute 'KerasLayer'**
The code is:
print("Building model with", MODULE_HANDLE)
model = tf.keras.Sequential([
hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
trainable=do_fine_tuning),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(train_generator.num_classes,
activation='softmax',
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()
The error is like:
1 print("Building model with", MODULE_HANDLE)
2 model = tf.keras.Sequential([
----> 3 hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
4 trainable=do_fine_tuning),
5 tf.keras.layers.Dropout(rate=0.2),
AttributeError: module 'tensorflow_hub' has no attribute 'KerasLayer'
by using the tensorflow hub retrain the previous hub model by adding new dence fully connected layers.when run the code it show the above error.is any have idea about that.please help
Please check the tensorflow version. It should be a recent nightly version.
When I use a version like 1.13.1, I see the following warning before the error, no attribute 'KerasLayer':
W0423 20:04:16.453974 139707130586880 __init__.py:56] Some hub symbols are not available because TensorFlow version is less than 1.14
After, doing pip install "tf-nightly", everything works fine.
https://www.tensorflow.org/hub
For the BatchNormalizationv1 issue, you can use tf2.0 nightly which should also take care of the original issue.
pip install -U tf-nightly-2.0-preview
https://github.com/tensorflow/tfjs/issues/1255
hub.KerasLayer works with TF2 pre releases:
pip install tf-nightly-2.0-preview --quiet
pip install tensorflow==2.0.0-alpha
pre-release candidate for GPU:
pip install -U --pre tensorflow-gpu

keras -> mlmodel: coreml object has no attribute 'convert'

I am trying to convert my keras model into mlmodel using coreml. However, it is saying that coremltools module has no attribute 'convert'.
AttributeError: 'module' object has no attribute 'convert'
My coremltools, keras, tensorflow(tensorflow-gpu) modules are all up to date.
I am also using python 2.7.10.
I've used windows and mac, in which, neither worked. However, caffe.convert is working using a caffe model.
Code:
coreml_model = coremltools.converters.keras.convert(MODEL_PATH)
As per the documentation, I expected the converters.keras.convert method to be available in coremltools.
Documentation: https://apple.github.io/coremltools/generated/coremltools.converters.keras.convert.html
Please help, thanks in advance!
Edit:
import coremltools
# from keras.models import load_model
import keras
import sys
from keras.applications import MobileNet
from keras.utils.generic_utils import CustomObjectScope
with CustomObjectScope({'relu6': keras.applications.MobileNet.relu6, 'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
model = load_model('weights.hdf5')
MODEL_PATH = "data/model_wide_cifar-10_fruits_model.h5"
def main():
""" Takes in keras model and convert to .mlmodel"""
print(sys.version)
# Load in keras model.
# model = load_model(MODEL_PATH)
# load labels
labels=[]
label_handler = open("fruit-labels.txt", 'r')
for label in label_handler:
labels.append(label.rstrip())
label_handler.close()
print("[INFO] Labels: {0}".format(labels))
# Convert to .mlmodel
coreml_model = coremltools.converters.keras.convert(
model=MODEL_PATH,
input_names="image",
output_names="image",
class_labels=labels)
labels = 'fruit-labels.txt'
# Save .mlmodel
coreml_model.utils.save_spec('fruitclassifier.mlmodel')
The solution is to use virtualenv. Follow the instructions from the coremltools README:
Installation
We recommend using virtualenv to use, install, or build coremltools. Be
sure to install virtualenv using your system pip.
pip install virtualenv
The method for installing coremltools follows the
standard python package installation steps.
To create a Python virtual environment called pythonenv follow these steps:
# Create a folder for virtualenv
mkdir virtualenvs
cd virtualenvs
# Create a Python virtual environment for your Core ML project
virtualenv coremltools
To activate your new virtual environment and install coremltools in this environment, follow these steps:
# Active your virtual environment
source coremltools/bin/activate
# Install coremltools in the new virtual environment, pythonenv
pip install --upgrade pip
pip install -U coremltools==3.0b5
Install keras and tensorflow
pip install keras tensorflow
Now make sure it works. With the coremltools environment activated, run
>>> python
Python 3.7.4 (v3.7.4:e09359112e, Sep 5 2019, 14:54:52)
>>> import coremltools
>>> coremltools.converters.keras.convert()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: convert() missing 1 required positional argument: 'model'
coremltools documentation
Credit to this guys issue: https://github.com/apple/coremltools/issues/440
Communist Hacker's answer does not work for my current setup:
tensorflow 2.4.1
coremltools 4.1
Python 3.8.7
However, after reviewing the documentation for coremltools here, I was able to fix it by removing keras from the function and the call now works:
import coremltools
coreml_model = coremltools.converters.convert(model,
input_names="inputname",
output_names="outputname")
Running the above command now produces this in my Jupyter notebook:
Running TensorFlow Graph Passes: 100%|██████████| 5/5 [00:00<00:00, 37.53 passes/s]
Converting Frontend ==> MIL Ops: 100%|██████████| 6/6 [00:00<00:00, 5764.05 ops/s]
Running MIL optimization passes: 100%|██████████| 17/17 [00:00<00:00, 5633.05 passes/s]
Translating MIL ==> MLModel Ops: 100%|██████████| 3/3 [00:00<00:00, 6864.65 ops/s]
Note - I am a complete noob with this, so I probably described things
incorrectly.

How to use densenet in Keras

I notice densenet has been added to keras (https://github.com/keras-team/keras/tree/master/keras/applications)and I want to apply it in my project but when I tried to import it in jupyter anaconda, I got an error saying:
module 'keras.applications' has no attribute 'densenet'
it seems like densenet has not been incorporated into current version of keras.
Any idea how can I add it myself?
Densenet was added in keras version 2.1.3. What version of keras are you running?
Have you tried to update keras with pip install keras --upgrade since January?

Resources