How to avoid converting model to Traced-model every time in YOLOv7? - python

I am using YOLOv7 for object detection. When I run the following command, object detection works fine:
python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg
However, every time I run this command, the model is converted to a Traced-model, which takes a few seconds.
Model Summary: 306 layers, 36905341 parameters, 6652669 gradients
Convert model to Traced-model...
traced_script_module saved!
model is traced!
Object detection takes only around 1 second. How can I avoid converting the model every time and just perform object detection?
I tried setting the output traced_model.pt as the weights, but I got the following error:
AttributeError: 'RecursiveScriptModule' object has no attribute 'get'

Related

Cant properly load saved model and call predict method

I am having trouble loading large model after saving.
have tried all below saveing methods:
tf.saved_model.save(model, model_save_path)
model.save(model_save_path+"_new_save")
tf.keras.models.save_model(model, model_save_path+"_v3")
error when loading :
method 1
m2=tf.keras.models.load_model(model_save_path+"_v3")
error:
__init__() got an unexpected keyword argument 'reduction'
method 2
m3=tf.keras.models.load_model(model_save_path
error:
ARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
ValueError: Unable to create a Keras model from SavedModel at xxxx . This SavedModel was exported with `tf.saved_model.save`, and lacks the Keras metadata file. Please save your Keras model by calling `model.save`or `tf.keras.models.save_model`. Note that you can still load this SavedModel with `tf.saved_model.load`.
method 3
m4=tf.saved_model.load(model_save_path)
this works but m4 object has no predict method
and not able to use
model.signatures["serving_default"](**input_data)
or
model.__call__(input_data,training=False)
to predict on data
any help would be appreciated
Adding compile=False to the load function will solve the issue:
m2=tf.keras.models.load_model(model_save_path+"_v3", compile=False)

Save finetuned XLM-RoBERTa using keras

I am finetuning XLM-RoBERTa for text classification. I am finetuning model on Tensorflow-keras. I have trained model on google colab GPU. I used save method from keras as shown below to save my model.
model.save("/content/drive/MyDrive/TrainedModel")
loaded_model = tf.keras.models.load_model("/content/drive/MyDrive/TrainedModel")
It saves saved model but gives below error when loading the model on CPU. How can I load model to run it?
/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/py_checkpoint_reader.py in get_tensor(self, tensor_str)
65 try:
66 return CheckpointReader.CheckpointReader_GetTensor(
---> 67 self, compat.as_bytes(tensor_str))
68 # TODO(b/143319754): Remove the RuntimeError casting logic once we resolve the
69 # issue with throwing python exceptions from C++.
** IndexError: Read less bytes than requested**
I got below warning when saving the model using above function. The saved model size is 7.43 MB.
WARNING:absl:Found untraced functions such as encoder_layer_call_fn, encoder_layer_call_and_return_conditional_losses, embeddings_layer_call_fn, embeddings_layer_call_and_return_conditional_losses, dense_layer_call_fn while saving (showing 5 of 422). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: /content/drive/MyDrive/TrainedModel/assets
INFO:tensorflow:Assets written to: /content/drive/MyDrive/TrainedModel/assets

AttributeError: 'Functional' object has no attribute 'predict_segmentation' When importing TensorFlow model Keras

I have successfully trained a Keras model like:
import tensorflow as tf
from keras_segmentation.models.unet import vgg_unet
# initaite the model
model = vgg_unet(n_classes=50, input_height=512, input_width=608)
# Train
model.train(
train_images=train_images,
train_annotations=train_annotations,
checkpoints_path="/tmp/vgg_unet_1", epochs=5
)
And saved it in hdf5 format with:
tf.keras.models.save_model(model,'my_model.hdf5')
Then I load my model with
model=tf.keras.models.load_model('my_model.hdf5')
Finally I want to make a segmentation prediction on a new image with
out = model.predict_segmentation(
inp=image_to_test,
out_fname="/tmp/out.png"
)
I am getting the following error:
AttributeError: 'Functional' object has no attribute 'predict_segmentation'
What am I doing wrong ?
Is it when I am saving my model or when I am loading it ?
Thanks !
predict_segmentation isn't a function available in normal Keras models. It looks like it was added after the model was created in the keras_segmentation library, which might be why Keras couldn't load it again.
I think you have 2 options for this.
You could use the line from the code I linked to manually add the function back to the model.
model.predict_segmentation = MethodType(keras_segmentation.predict.predict, model)
You could create a new vgg_unet with the same arguments when you reload the model, and transfer the weights from your hdf5 file to that model as suggested in the Keras documentation.
model = vgg_unet(n_classes=50, input_height=512, input_width=608)
model.load_weights('my_model.hdf5')

How to evaluate trained model Average Precison and Mean AP with IOU=0.3

I trained a model using Tensorflow object detection API using Faster-RCNN with Resnet architecture. I am using tensorflow 1.13.1, cudnn 7.6.5, protobuf 3.11.4, python 3.7.7, numpy 1.18.1 and I cannot upgrade the versions at the moment. I need to evaluate the accuracy (AP/mAP) of the trained model with the validation set for the IOU=0.3. I am using legacy/eval.py script on purpose since it calculates AP/mAP for IOU=0.5 only (instead of mAP:0.5:0.95)
python legacy/eval.py --logtostderr --pipeline_config_path=training/faster_rcnn_resnet152_coco.config --checkpoint_dir=training/ --eval_dir=eval/
I tried several things including updating pipeline config file to have min_score_threshold=0.3:
eval_config: {
num_examples: 60
min_score_threshold: 0.3
..
Updated the default value in the protos/eval.proto file and recompiled the proto file to generate new version of eval_pb2.py
// Minimum score threshold for a detected object box to be visualized
optional float min_score_threshold = 13 [default = 0.3];
However, eval.py still calculates/shows AP/mAP with IOU=0.5
The above configuration helped only to detect objects on the images with confidence level < 0.5 in the eval.py output images but this is not what i need.
Does anybody know how to evaluate the model with IOU=0.3?
I finally could solve it by modifing hardcoded matching_iou_threshold=0.5 argument value in multiple method arguments (especially def __init) in the ../object_detection/utils/object_detection_evaluation.py

Model object has no attribute save while reducing size of CoreML?

i am reducing the size of CoreML. I make this CoreML with Python Turicate but i am getting a error Model object has no attribute save. I have Python 2.7 and pip install coremltools==2.0b1 before execute python file. Here's my code -
import coremltools
from coremltools.models.neural_network.quantization_utils import *
model = coremltools.models.MLModel('/Users/Desktop/MLClassifier/animals.mlmodel')
lin_quant_model = quantize_weights(model, 16, "linear")
lin_quant_model.save('/Users/Desktop/animals2.mlmodel')
My guess is you're not on macOS 10.14 or later (Mojave), in which case you don't get an MLModel but the model specification when you call quantize_weights() (according to the docs). No idea why, but that's what it says.
I have also faced this problem, running model quantization on Ubuntu (Python 3.8, coremltools==4.1).
I don't know why exactly this error occurs (maybe it should be run on macOS), but as you are getting specs, you can get and save model as follows:
model_fp16_specs = quantization_utils.quantize_weights(model_fp32, nbits=16)
model_fp16 = ct.models.MLModel(model_fp16_specs)
model_fp16.save("model_quantized.mlmodel")

Categories

Resources