Giter VIP home page Giter VIP logo

Comments (4)

ClementPinard avatar ClementPinard commented on May 25, 2024 1

Well, this is much tougher than the main transformation ! It involves the function atan2 it's not trivial ;)
After a quick google search, you can try a variation of this proposed algorithm : https://www.learnopencv.com/rotation-matrix-to-euler-angles/
Fortunately, the atan2 function is available in pytorch ! see here https://pytorch.org/docs/stable/torch.html#torch.atan2

Good luck,

from sfmlearner-pytorch.

ClementPinard avatar ClementPinard commented on May 25, 2024 1

your code is unfortunately not differentiable becasue you create a new FloatTensor.
You should just have theta = torch.stack([x,y,z])

Other than that, the code is not very parallel, but I guess with the singular check, you cannot do much better. the sy can be computed batchwise however

def pose_mat2vec(Rt):
    t_all = Rt[:,:,-1]
    R_all = Rt[:,:,:3]
    sy = R[:,0,0]^2 + R[:,1,0]^2
    y_all = torch.atan2(-R[:,2,0], sy)
    theta = []
    for (R,t, s) in zip(R_all,t_all, sy, y_all):
        singular = s < 1e-6
        if not singular :
            x = torch.atan2(R[2,1] , R[2,2])
            z = torch.atan2(R[1,0], R[0,0])
        else :
            x = torch.atan2(-R[1,2], R[1,1])
            z = 0
        theta.append(torch.stack([x, y, z]))

    theta = torch.stack(theta)
    pose_all = torch.cat([theta,t_all], dim=1)
    return pose_all

from sfmlearner-pytorch.

shahabty avatar shahabty commented on May 25, 2024

Thank you very much for your quick reply.

from sfmlearner-pytorch.

shahabty avatar shahabty commented on May 25, 2024

I wrote this to convert mat to vec in pytorch. Could you please have a look at it and let me know if it is wrong or there is anyway to improve the performance.


def pose_mat2vec(Rt):
    t_all = Rt[:,:,-1]
    R_all = Rt[:,:,:3]

    pose_all = []
    for R,t in zip(R_all,t_all):
        sy = torch.sqrt(R[0,0] * R[0,0] +  R[1,0] * R[1,0])

        singular = sy < 1e-6
        if not singular :
            x = torch.atan2(R[2,1] , R[2,2])
            y = torch.atan2(-R[2,0], sy)
            z = torch.atan2(R[1,0], R[0,0])
        else :
            x = torch.atan2(-R[1,2], R[1,1])
            y = torch.atan2(-R[2,0], sy)
            z = 0
        theta = torch.FloatTensor([x, y, z]).cuda(0)
        pose_6 = torch.cat((theta, t),0)
        pose_all.append(pose_6)

    pose_all = torch.stack(pose_all,0)
    return pose_all

Thanks a lot.

from sfmlearner-pytorch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.