# Loading Tensorflow Models Using Opencv and C++

**URL:** <https://forum.opencv.org/t/loading-tensorflow-models-using-opencv-and-c/12878>\
**Category:** C++\
**Tags:** dnn, tensorflow\
**Created:** [April 20, 2023, 8:44am UTC](https://forum.opencv.org/t/loading-tensorflow-models-using-opencv-and-c/12878 "2023-04-20T08:44:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Swapnil\_Baad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/swapnil_baad/32/7723_2.png) [@Swapnil\_Baad](https://forum.opencv.org/u/Swapnil_Baad)\
**Post date:** [April 20, 2023, 8:44am UTC](https://forum.opencv.org/t/loading-tensorflow-models-using-opencv-and-c/12878/1 "2023-04-20T08:44:39Z")

</div>

I have trained a model in python tensorflow 2.5.0.  
I have to load this model and make classification using opencv and c++.

How can I do it? any suggestions?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 20, 2023, 2:50pm UTC](https://forum.opencv.org/t/loading-tensorflow-models-using-opencv-and-c/12878/2 "2023-04-20T14:50:37Z")

</div>

> [@Swapnil\_Baad](#):
>
> I have trained a model in python tensorflow 2.5.0.

what did you do, exactly ? please show !

> [@Swapnil\_Baad](#):
>
> I have to load this model

try a simple:

```
cv::dnn::readNet("mymodel.pb")

```

then worry about data processing  
(still, you need to find out about: image size, mean/std values, etc)

please also [look at our samples](https://github.com/opencv/opencv/tree/4.x/samples/dnn)

---

<div class="post-metadata">

**Author:** ![Swapnil\_Baad](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/swapnil_baad/32/7723_2.png) [@Swapnil\_Baad](https://forum.opencv.org/u/Swapnil_Baad)\
**Post date:** [April 20, 2023, 7:33pm UTC](https://forum.opencv.org/t/loading-tensorflow-models-using-opencv-and-c/12878/3 "2023-04-20T19:33:02Z")

</div>

Thanks for your reply.  
Regarding your question:  
I have done transfer learning on MobileNetV2 pre-trained model.  
I have followed the exact steps as mentioned here and saved the model: [https://www.tensorflow.org/tutorials/images/transfer\_learning](https://www.tensorflow.org/tutorials/images/transfer_learning)

I have saved the model using: model.save(‘path’)

Also, I am preparing the input image in python to classify as below:

```
        image = np.expand_dims(t_image, axis=0)
        image= img.astype('float32')
        image /= 255.0
        imgReshaped = image.reshape(-1, img_height, img_width, color_channel) 

```

The Model has 100x100 RGB image input.
