spacewalk01 / depth-anything-tensorrt

TensorRT implementation of Depth-Anything V1, V2
https://depth-anything.github.io/
MIT License
227 stars 28 forks source link

export works for vitb, but not for vitl #21

Closed antithing closed 3 months ago

antithing commented 3 months ago

Hi, and thank you for making this available!

exporting using the depth_anything_vitl14.pth model gives me this error:

size mismatch for depth_head.scratch.output_conv2.0.weight: copying a param with shape torch.Size([32, 128, 3, 3]) from checkpoint, the shape in current model is torch.Size([32, 64, 3, 3]).

depth_anything_vitb14.pthworks perfectly!

Also: Can we export a pointcloud / depth mesh from this repo? Do you support the metric depth models? Can I swap to greyscale depth maps?

Thanks again!

spacewalk01 commented 3 months ago

will you put the command here that produces the error

spacewalk01 commented 3 months ago

I used following command and generated onnx model without problem:

C:\depth-anything-tensorrt>git clone https://github.com/LiheYoung/Depth-Anything

Copy dpt.py in this repo to C:\depth-anything-tensorrt\Depth-Anything\depth_anything Copy export.py in this repo to C:\depth-anything-tensorrt\Depth-Anything

C:\depth-anything-tensorrt>cd Depth-Anything
C:\depth-anything-tensorrt\Depth-Anything>python export.py --encoder vitl --load_from depth_anything_vitl14.pth --image_shape 3 518 518

Result message: Model exported to depth_anything_vitl14.onnx

spacewalk01 commented 3 months ago

Don't forget that when you export depth_anything_vitl14.pth large model, you need to set the encoder argment to vitl: --encoder vitl

spacewalk01 commented 3 months ago

About your questions: Can we export a pointcloud / depth mesh from this repo? No Do you support the metric depth models? No Can I swap to greyscale depth maps? Yes

antithing commented 3 months ago

Don't forget that when you export depth_anything_vitl14.pth large model, you need to set the encoder argment to vitl: --encoder vitl

This was the issue. Thank you!

antithing commented 3 months ago

About your questions: Can we export a pointcloud / depth mesh from this repo? No Do you support the metric depth models? No Can I swap to greyscale depth maps? Yes

Sorry one more question: How can I either get the depth value from the color image, or convert to greyscale and get depth value there (from a selected pixel)?

Thanks!

spacewalk01 commented 3 months ago

Why not convert the depth result image to grayscale image first and then save/use it? Here is python example:

img_gray = cv2.cvtColor(depth_mat, cv2.COLOR_BGR2GRAY) cv2.imwrite("gray-depth.jpg", img_gray)

spacewalk01 commented 3 months ago

Or change inference post processing code in depth_anything.cpp as follows:

// Convert the entire depth_data vector to a CV_32FC1 Mat
cv::Mat depth_mat(input_h, input_w, CV_32FC1, depth_data);
cv::normalize(depth_mat, depth_mat, 0, 255, cv::NORM_MINMAX, CV_8U);

// Rescale the colormap
int limX, limY;
if (img_w > img_h)
{
    limX = input_w;
    limY = input_w * img_h / img_w;
}
else
{
    limX = input_w * img_w / img_h;
    limY = input_w;
}
cv::resize(depth_mat, depth_mat, cv::Size(img_w, img_h));

return depth_mat;
spacewalk01 commented 3 months ago

It will return depth values normalized between 0 and 255.

Alex12358 commented 1 week ago

Or change inference post processing code in depth_anything.cpp as follows:

// Convert the entire depth_data vector to a CV_32FC1 Mat
cv::Mat depth_mat(input_h, input_w, CV_32FC1, depth_data);
cv::normalize(depth_mat, depth_mat, 0, 255, cv::NORM_MINMAX, CV_8U);

// Rescale the colormap
int limX, limY;
if (img_w > img_h)
{
    limX = input_w;
    limY = input_w * img_h / img_w;
}
else
{
    limX = input_w * img_w / img_h;
    limY = input_w;
}
cv::resize(depth_mat, depth_mat, cv::Size(img_w, img_h));

return depth_mat;

I modified depth_anything.cpp with this code and encountered an error during inference:

OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(4.10.0) Error: Assertion failed (src[i].dims <= 2 && src[i].rows == src[0].rows && src[i].type() == src[0].type()) in cv::hconcat, file C:\GHA-OCV-1_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\core\src\matrix_operations.cpp, line 67