mirror of
https://github.com/opencv/opencv_contrib.git
synced 2026-10-28 08:46:19 +08:00
Merge pull request #4171 from SavyaSanchi-Sharma:extra_parsers
Removing TensorFlow Importer - #4171 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake Removed Tensorflow importers
This commit is contained in:
1 parent
eca97a4adb
commit
1e5704beb0
13 files changed
+44
-43
No files matched your search
@@ -16,9 +16,15 @@ Refer to the tutorials to understand how to use this module.
|
||||
|
||||
There are four models which are trained.
|
||||
|
||||
**Model format.** OpenCV 5 removed the TensorFlow importer, so `readModel()` accepts ONNX only.
|
||||
The repositories linked below publish TensorFlow `.pb` weights, which have to be converted to
|
||||
ONNX before they can be loaded. The converted models used by this module's tests live in
|
||||
`opencv_extra` under `testdata/cv/dnn_superres/`: `ESPCN_x2.onnx`, `FSRCNN_x2.onnx`,
|
||||
`FSRCNN_x3.onnx` and `LapSRN_x4.onnx`. No converted EDSR model is distributed yet.
|
||||
|
||||
#### EDSR
|
||||
|
||||
Trained models can be downloaded from [here](https://github.com/Saafke/EDSR_Tensorflow/tree/master/models).
|
||||
Trained weights (TensorFlow `.pb`) can be downloaded from [here](https://github.com/Saafke/EDSR_Tensorflow/tree/master/models).
|
||||
|
||||
- Size of the model: ~38.5MB. This is a quantized version, so that it can be uploaded to GitHub. (Original was 150MB.)
|
||||
- This model was trained for 3 days with a batch size of 16
|
||||
@@ -31,7 +37,7 @@ Trained models can be downloaded from [here](https://github.com/Saafke/EDSR_Tens
|
||||
|
||||
#### ESPCN
|
||||
|
||||
Trained models can be downloaded from [here](https://github.com/fannymonori/TF-ESPCN/tree/master/export).
|
||||
Trained weights (TensorFlow `.pb`) can be downloaded from [here](https://github.com/fannymonori/TF-ESPCN/tree/master/export).
|
||||
|
||||
- Size of the model: ~100kb
|
||||
- This model was trained for ~100 iterations with a batch size of 32
|
||||
@@ -44,7 +50,7 @@ Trained models can be downloaded from [here](https://github.com/fannymonori/TF-E
|
||||
|
||||
#### FSRCNN
|
||||
|
||||
Trained models can be downloaded from [here](https://github.com/Saafke/FSRCNN_Tensorflow/tree/master/models).
|
||||
Trained weights (TensorFlow `.pb`) can be downloaded from [here](https://github.com/Saafke/FSRCNN_Tensorflow/tree/master/models).
|
||||
|
||||
- Size of the model: ~40KB (~9kb for FSRCNN-small)
|
||||
- This model was trained for ~30 iterations with a batch size of 1
|
||||
@@ -57,7 +63,7 @@ Trained models can be downloaded from [here](https://github.com/Saafke/FSRCNN_Te
|
||||
|
||||
#### LapSRN
|
||||
|
||||
Trained models can be downloaded from [here](https://github.com/fannymonori/TF-LapSRN/tree/master/export).
|
||||
Trained weights (TensorFlow `.pb`) can be downloaded from [here](https://github.com/fannymonori/TF-LapSRN/tree/master/export).
|
||||
|
||||
- Size of the model: between 1-5Mb
|
||||
- This model was trained for ~50 iterations with a batch size of 32
|
||||
|
||||
@@ -80,9 +80,10 @@ public:
|
||||
|
||||
/** @brief Read the model from the given path
|
||||
@param weights Path to the model weights file.
|
||||
@param definition Path to the model definition file.
|
||||
@param definition Unused. Kept for backward compatibility.
|
||||
@deprecated ONNX models are single-file; use readModel(const String&) instead.
|
||||
*/
|
||||
void readModel(const String& weights, const String& definition);
|
||||
CV_DEPRECATED void readModel(const String& weights, const String& definition);
|
||||
|
||||
/** @brief Set desired model
|
||||
@param algo String containing one of the desired models:
|
||||
|
||||
@@ -12,7 +12,7 @@ class test_dnn_superres(NewOpenCVTests):
|
||||
# Get test data paths
|
||||
dnn_superres_test_path = os.environ['OPENCV_TEST_DATA_PATH'] + "/cv/dnn_superres/"
|
||||
img_path = dnn_superres_test_path + "butterfly.png"
|
||||
espcn_path = dnn_superres_test_path + "ESPCN_x2.pb"
|
||||
espcn_path = dnn_superres_test_path + "ESPCN_x2.onnx"
|
||||
|
||||
# Create an SR object
|
||||
sr = cv.dnn_superres.DnnSuperResImpl_create()
|
||||
|
||||
@@ -12,8 +12,8 @@ namespace opencv_test { namespace {
|
||||
|
||||
typedef perf::TestBaseWithParam<tuple<tuple<string,string,int>, string> > dnn_superres;
|
||||
|
||||
#define MODEL testing::Values(tuple<string,string,int> {"espcn","ESPCN_x2.pb",2}, \
|
||||
tuple<string,string,int> {"lapsrn","LapSRN_x4.pb",4})
|
||||
#define MODEL testing::Values(tuple<string,string,int> {"espcn","ESPCN_x2.onnx",2}, \
|
||||
tuple<string,string,int> {"lapsrn","LapSRN_x4.onnx",4})
|
||||
#define IMAGES testing::Values("cv/dnn_superres/butterfly.png", "cv/shared/baboon.png", "cv/shared/lena.png")
|
||||
|
||||
const string TEST_DIR = "cv/dnn_superres";
|
||||
|
||||
@@ -63,7 +63,7 @@ void DnnSuperResImpl::readModel(const String& path)
|
||||
{
|
||||
if ( path.size() )
|
||||
{
|
||||
this->net = dnn::readNetFromTensorflow(path); // engine classic removed
|
||||
this->net = dnn::readNetFromONNX(path);
|
||||
CV_LOG_INFO(NULL, "Successfully loaded model: " << path);
|
||||
}
|
||||
else
|
||||
@@ -74,15 +74,10 @@ void DnnSuperResImpl::readModel(const String& path)
|
||||
|
||||
void DnnSuperResImpl::readModel(const String& weights, const String& definition)
|
||||
{
|
||||
if ( weights.size() && definition.size() )
|
||||
{
|
||||
this->net = dnn::readNetFromTensorflow(weights, definition);
|
||||
CV_LOG_INFO(NULL, "Successfully loaded model: " << weights << " " << definition);
|
||||
}
|
||||
else
|
||||
{
|
||||
CV_Error(Error::StsBadArg, String("Could not load model: ") + weights + " " + definition);
|
||||
}
|
||||
CV_UNUSED(definition);
|
||||
CV_LOG_WARNING(NULL, "readModel(weights, definition) is deprecated: ONNX models are "
|
||||
"single-file, the 'definition' argument is ignored.");
|
||||
readModel(weights);
|
||||
}
|
||||
|
||||
void DnnSuperResImpl::setModel(const String& algo, int scale)
|
||||
@@ -126,9 +121,9 @@ void DnnSuperResImpl::upsample(InputArray img, OutputArray result)
|
||||
|
||||
Mat Y = ycbcr_channels[0];
|
||||
|
||||
//Create blob from image so it has size 1,1,Width,Height
|
||||
cv::Mat blob;
|
||||
dnn::blobFromImage(Y, blob, 1.0);
|
||||
// Models expect NHWC input [1, H, W, 1], reshape without copying data
|
||||
std::vector<int> nhwc_shape = {1, Y.rows, Y.cols, 1};
|
||||
cv::Mat blob = Y.reshape(1, nhwc_shape);
|
||||
|
||||
//Get the HR output
|
||||
this->net.setInput(blob);
|
||||
@@ -203,9 +198,9 @@ void DnnSuperResImpl::upsampleMultioutput(InputArray img, std::vector<Mat> &imgs
|
||||
|
||||
Mat Y = ycbcr_channels[0];
|
||||
|
||||
//Create blob from image so it has size 1,1,Width,Height
|
||||
cv::Mat blob;
|
||||
dnn::blobFromImage(Y, blob, 1.0);
|
||||
// Models expect NHWC input [1, H, W, 1], reshape without copying data
|
||||
std::vector<int> nhwc_shape = {1, Y.rows, Y.cols, 1};
|
||||
cv::Mat blob = Y.reshape(1, nhwc_shape);
|
||||
|
||||
//Get the HR outputs
|
||||
std::vector <Mat> outputs_blobs;
|
||||
|
||||
@@ -46,17 +46,17 @@ void runSingleModel(std::string algorithm, int scale, std::string model_filename
|
||||
|
||||
TEST(CV_DnnSuperResSingleOutputTest, accuracy_espcn_2)
|
||||
{
|
||||
runSingleModel("espcn", 2, "ESPCN_x2.pb");
|
||||
runSingleModel("espcn", 2, "ESPCN_x2.onnx");
|
||||
}
|
||||
|
||||
TEST(CV_DnnSuperResSingleOutputTest, accuracy_fsrcnn_2)
|
||||
{
|
||||
runSingleModel("fsrcnn", 2, "FSRCNN_x2.pb");
|
||||
runSingleModel("fsrcnn", 2, "FSRCNN_x2.onnx");
|
||||
}
|
||||
|
||||
TEST(CV_DnnSuperResSingleOutputTest, accuracy_fsrcnn_3)
|
||||
{
|
||||
runSingleModel("fsrcnn", 3, "FSRCNN_x3.pb");
|
||||
runSingleModel("fsrcnn", 3, "FSRCNN_x3.onnx");
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ void runMultiModel(std::string algorithm, int scale, std::string model_filename,
|
||||
Mat img = imread(path);
|
||||
ASSERT_FALSE(img.empty()) << "Test image can't be loaded: " << path;
|
||||
|
||||
std::string pb_path = cvtest::findDataFile(DNN_SUPERRES_DIR + "/" + model_filename);
|
||||
std::string pb_path = cvtest::findDataFile("models/onnx/" + model_filename);
|
||||
|
||||
dnn_sr->readModel(pb_path);
|
||||
|
||||
@@ -108,7 +108,7 @@ TEST(CV_DnnSuperResMultiOutputTest, accuracy)
|
||||
//x4
|
||||
std::vector<String> names_4x {"NCHW_output_2x", "NCHW_output_4x"};
|
||||
std::vector<int> scales_4x {2, 4};
|
||||
runMultiModel("lapsrn", 4, "LapSRN_x4.pb", scales_4x, names_4x);
|
||||
runMultiModel("lapsrn", 4, "LapSRN_x4.onnx", scales_4x, names_4x);
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -34,7 +34,7 @@ Run the sample code with the following command
|
||||
|
||||
```run
|
||||
./bin/example_dnn_superres_dnn_superres_multioutput path/to/image.png 2,4 NCHW_output_2x,NCHW_output_4x \
|
||||
path/to/opencv_contrib/modules/dnn_superres/models/LapSRN_x4.pb
|
||||
path/to/opencv_contrib/modules/dnn_superres/models/LapSRN_x4.onnx
|
||||
```
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ Explanation
|
||||
|
||||
-# **Read the model**
|
||||
@code{.cpp}
|
||||
path = "models/LapSRN_x8.pb"
|
||||
path = "models/LapSRN_x8.onnx"
|
||||
sr.readModel(path);
|
||||
@endcode
|
||||
|
||||
|
||||
@@ -26,13 +26,13 @@ Source Code of the sample
|
||||
You can run the sample code by doing
|
||||
|
||||
```run
|
||||
<path_of_your_opencv_build_directory>/bin/example_dnn_superres_dnn_superres <path_to_image.png> <algo_string> <upscale_int> <model_path.pb>
|
||||
<path_of_your_opencv_build_directory>/bin/example_dnn_superres_dnn_superres <path_to_image.png> <algo_string> <upscale_int> <model_path.onnx>
|
||||
```
|
||||
|
||||
Example:
|
||||
|
||||
```run
|
||||
/home/opencv/build/bin/example_dnn_superres_dnn_superres /home/image.png edsr 2 /home/EDSR_x2.pb
|
||||
/home/opencv/build/bin/example_dnn_superres_dnn_superres /home/image.png edsr 2 /home/EDSR_x2.onnx
|
||||
```
|
||||
|
||||
@includelineno dnn_superres/samples/dnn_superres.cpp
|
||||
@@ -58,17 +58,17 @@ Explanation
|
||||
This is just to create the object, register the custom dnn layers and get access to the class functions.
|
||||
-# **Read the model**
|
||||
@code{.cpp}
|
||||
path = "models/FSRCNN_x2.pb"
|
||||
path = "models/FSRCNN_x2.onnx"
|
||||
sr.readModel(path);
|
||||
@endcode
|
||||
|
||||
This reads the TensorFlow model from the .pb file. Here 'path' is one of the pre-trained Tensorflow models' path file. You can download the models from OpenCV's GitHub, in the 'dnn_superres' module.
|
||||
This reads the ONNX model from the .onnx file. Here 'path' is one of the pre-trained models' path file. You can download the models from OpenCV's GitHub, in the 'dnn_superres' module.
|
||||
-# **Set the model**
|
||||
@code{.cpp}
|
||||
sr.setModel("fsrcnn", 2);
|
||||
@endcode
|
||||
|
||||
Depending on the model you want to run, you have to set the algorithm and upscale factor. This is to know the desired algorithm and scale, even if you change the .pb file's name. For example: if you chose FSRCNN_x2.pb, your algorithm and scale will be 'fsrcnn' and 2, respectively. (Other algorithm options include "edsr", "espcn" and "lapsrn".)
|
||||
Depending on the model you want to run, you have to set the algorithm and upscale factor. This is to know the desired algorithm and scale, even if you change the .onnx file's name. For example: if you chose FSRCNN_x2.onnx, your algorithm and scale will be 'fsrcnn' and 2, respectively. (Other algorithm options include "edsr", "espcn" and "lapsrn".)
|
||||
-# **Upscale an image**
|
||||
@code{.cpp}
|
||||
Mat img = cv::imread(img_path);
|
||||
@@ -92,7 +92,7 @@ sr = dnn_superres.DnnSuperResImpl_create()
|
||||
image = cv2.imread('./image.png')
|
||||
|
||||
# Read the desired model
|
||||
path = "EDSR_x4.pb"
|
||||
path = "EDSR_x4.onnx"
|
||||
sr.readModel(path)
|
||||
|
||||
# Set the desired model and scale to get correct pre- and post-processing
|
||||
|
||||
@@ -42,7 +42,7 @@ Explanation
|
||||
Instantiate a dnn super-resolution object.
|
||||
-# **Read the model**
|
||||
@code{.cpp}
|
||||
path = "models/ESPCN_x2.pb"
|
||||
path = "models/ESPCN_x2.onnx"
|
||||
sr.readModel(path);
|
||||
sr.setModel("espcn", 2);
|
||||
@endcode
|
||||
|
||||
@@ -292,7 +292,6 @@ cv.dnn.Net.getFLOPS
|
||||
cv.dnn.Net.getMemoryConsumption
|
||||
cv.dnn.Net.enableFusion
|
||||
cv.dnn.Net.getPerfProfile
|
||||
cv.dnn.readNetFromTensorflow
|
||||
cv.dnn.readNetFromTorch
|
||||
cv.dnn.readNet
|
||||
cv.dnn.readTorchBlob
|
||||
|
||||
|
@@ -2,7 +2,7 @@ using OpenCV
|
||||
const cv = OpenCV
|
||||
size0 = Int32(300)
|
||||
# take the model from https://github.com/opencv/opencv_extra/tree/master/testdata/dnn
|
||||
net = cv.dnn_DetectionModel("opencv_face_detector.pbtxt", "opencv_face_detector_uint8.pb")
|
||||
net = cv.dnn_DetectionModel("opencv_face_detector_uint8.onnx")
|
||||
|
||||
cv.dnn.setPreferableTarget(net, cv.dnn.DNN_TARGET_CPU)
|
||||
cv.dnn.setInputMean(net, (104, 177, 123))
|
||||
|
||||
@@ -11,7 +11,7 @@ function IOU(boxA, boxB)
|
||||
end
|
||||
|
||||
const cv = OpenCV
|
||||
net = cv.dnn.DetectionModel(joinpath(ENV["OPENCV_TEST_DATA_PATH"], "dnn", "opencv_face_detector.pbtxt"),joinpath(ENV["OPENCV_TEST_DATA_PATH"], "dnn", "opencv_face_detector_uint8.pb"))
|
||||
net = cv.dnn.DetectionModel(joinpath(ENV["OPENCV_TEST_DATA_PATH"], "dnn", "onnx", "models", "opencv_face_detector_uint8.onnx"))
|
||||
size0 = 300
|
||||
|
||||
cv.dnn.setPreferableTarget(net, cv.dnn.DNN_TARGET_CPU)
|
||||
|
||||
@@ -126,7 +126,7 @@ Running The Included Sample
|
||||
-----------------------
|
||||
|
||||
|
||||
Let's try running one of the included samples now. In this tutorial we will see how to run the `face_detect_dnn.jl` sample. This samples uses a deep neural network to detect faces in the video stream by webcam. The screenshot is from a slightly edited version that reads an image instead. First navigate to `opencv_contrib/modules/julia/samples/`. Next, you need two files "opencv_face_detector.pbtxt" and "opencv_face_detector_uint8.pb" from [link](https://github.com/opencv/opencv_extra/tree/master/testdata/dnn);simply download and place them in the same directory as `face_detect_dnn.jl`. Now you're ready to run. Start a terminal and simply type:
|
||||
Let's try running one of the included samples now. In this tutorial we will see how to run the `face_detect_dnn.jl` sample. This samples uses a deep neural network to detect faces in the video stream by webcam. The screenshot is from a slightly edited version that reads an image instead. First navigate to `opencv_contrib/modules/julia/samples/`. Next, you need the file "opencv_face_detector_uint8.onnx" from [link](https://github.com/opencv/opencv_extra/tree/master/testdata/dnn);simply download and place it in the same directory as `face_detect_dnn.jl`. Now you're ready to run. Start a terminal and simply type:
|
||||
|
||||
```
|
||||
> julia face_detect_dnn.jl
|
||||
|
||||
Reference in new issue
Block a user