-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Training code #6
Comments
Haha the only other issue here seems to be asking for the exact same thing. Ok maybe not the training code but definitely the encoder. From browsing the code it appears to be the Maybe we can reconstruct it ourselves though? Because I'm impatient ;). I must admit I find the paper a little bit vague with regards to the actual encoding procedure. I'm trying to understand how this, translates to actual encoding; is a random |
I still don't know how the paper's encoder works exactly, but based on the heatmap2coord method in the code I created a def coord2heatmap(x, y, w, h, ow, oh, softmax=True):
"""
Turns an (x,y) coordinate into a lossless heatmap. Arguments:
x: x coordinate
y: y coordinate
w: original width
h: original height
ow: output heatmap width
oh: output heatmap height
"""
# Get scale
sx = ow / w
sy = oh / h
# Unrounded target points
px = x * sx
py = y * sy
# Truncated coordinates
nx,ny = int(px), int(py)
# Coordinate error
ex,ey = px - nx, py - ny
# Multiplication factors required so the
# heatmap intensities multiplied by their coordinates
# result in the output coordinate.
rr = tensor([[1-ey, ey]]).T @ tensor([[1-ex, ex]])
heatmap = torch.zeros(oh, ow)
if softmax:
# Take the log of our coefficients so
# we get our output predictions after
# softmax.
rr = torch.log(rr)
# We can add any constant to rr for an
# identical softmax, so we're making sure
# every value is > 0. torch.min(rr) is
# negative since everything is in (0, 1)
rr -= torch.min(rr) - 0.5
heatmap[ny:ny+2,nx:nx+2] = rr
return heatmap Testing it: # Some random points
w,h=2500,1500
x,y=163,1342
ow,oh=256,384
# Expected output:
expected = tensor([x*ow/w, y*oh/h])
# Note coord2heatmap expects N,C,H,W. Also, the result
# won't be accurate here for topk other than 4 (because the rest
# is zeros that could be anywhere.)
assert torch.allclose(expected, heatmap2coord(coord2heatmap(x,y,w,h,ow,oh,softmax=True).view(1, 1, oh, ow), topk=4) One last thing: just realized that points within 1 pixel of the edge of the output heatmap will currently crash the code. Avoid those ;). |
Same, I still don't know how the paper's encoder works exactly. But it's great when you comment that makes me understand the problem better. Don't know if you have reproduced yet? If so, are the results good? |
I keep going down rabbit holes but I'm hoping to run an experiment today or tomorrow. As I commented on another issue earlier today the output of the head block in this repository seems to imply that xx = ex >= torch.rand(1)
yy = ey >= torch.rand(1)
rr = torch.zeros(2, 2)
rr[yy.long(), xx.long()] = 1 (use your imagination to decide where exactly this ends up please 😉), so you randomly assign the point to one of the four neighbors with a probability based on how big the error is, with enough heat maps generated the average result will be the same. So I'm going to try that, combined with cross entropy loss, on a simple dataset, and see what happens 🤷♂️. |
Hi @ElteHupkes, |
@vuthede Glad to help, hope you're building something great 🙃. Would you mind sharing your precise approach if you have something that works?
I'll be trying the cross entropy approach soon, if something comes out of it I'll report back. EDIT: Point (2) also applies to point (1), if there isn't that much activation the model may just be tempted to learn a large field of zeros (they can't actually be zeros with softmax, but depending on the size of the heatmap everything can be pretty close to zero regardless). |
Hello @ElteHupkes ,
EDIT: I just figure out that the box annnotation policies author mentioned also affect alot to the result too. |
@vuthede I don't know... the fact that it trains at all is probably a good sign, but maybe the encoding mechanism has some inherent limitations. Just out of curiosity, what architecture are you using? Using any pretrained weights? FWIW I just tried the Cross Entropy approach with HRNet last night on a much simpler dataset unrelated to facial landmarks, and it most definitely trains, got some pretty decent accuracy in just a few epochs without any pretrained weights. I've no experience with LaPa but maybe I can have a look at it later. |
@ElteHupkes I used the MobileNetV2 with input size 256x256 with Imagenet pretrained weights as in paper mentioned. |
@vuthede @ElteHupkes Hello, I also implemented a function named coord2heatmap to construct a binary heatmap, and the code as follws:
And I also tried to train a model on WFLW with BinaryCrossEntropy, but got pretty low acuracy. Can you share your code with us and figure it out together? @vuthede 😊 |
@PuNeal I got sidetracked a bit, but my initial experiments with Binary Cross Entropy actually look rather promising on LaPa. I'm only experimenting with a fraction of the data and points though because I simply don't have the time / GPU power available to take it much further than that. I've decided to write a blog post about this subject, which will cover heat map regression in general and this paper/repo in particular. I'm writing it up using the Jupyter notebook I've used for experimenting, so all my code will be in there. It'll hopefully be done today, I'll make sure to share when it's online. |
@ElteHupkes Good job! I'm looking forward to it.:satisfied: |
Here it is: https://elte.me/2021-03-10-keypoint-regression-fastai. Bit rough around the edges, but you should be able to get the things I've tried from the part starting here: https://elte.me/2021-03-10-keypoint-regression-fastai#random-rounding-and-high-resolution-net. Let me know your thoughts! |
Thanks @ElteHupkes, It is great blog. |
@vuthede cool! You're using a MobileNetV2 I see, are you already running it on a phone? If so, how is it performing? I'm really curious about this Adaptive Wing Loss, going to have a read through that later. Would be interesting to see how heatmaps + adaptive wing loss compare to binary maps + cross entropy. |
@ElteHupkes Yeah I am going to bring it on phone to see how it will perform. |
Hi author,
Can you share H3R training code? I look forward to testing your model on 68 landmarks.
Thank you very much!
The text was updated successfully, but these errors were encountered: