-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kye
committed
Oct 11, 2023
1 parent
8ffaccb
commit ebcd79a
Showing
9 changed files
with
87 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import torch | ||
import torch.nn.functional as F | ||
|
||
|
||
def simple_attention(K, V, Q): | ||
_, n_channels, _ = K.shape | ||
A = torch.einsum("bct,bc1->bt1", [K, Q]) | ||
A = F.softmax(A * n_channels ** (-0.5), 1) | ||
R = torch.einsum("bct, bt1->bc1", [V, A]) | ||
return torch.cat((R, Q), dim=1) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import torch | ||
from torch import nn | ||
from einops.layers.torch import Rearrange | ||
from einops import rearrange | ||
|
||
|
||
def yolo(input, num_classes, num_anchors, anchors, stride_h, stride_w): | ||
raw_predictions = rearrange( | ||
input, | ||
"b (anchor prediction) h w -> prediction b anchor h w", | ||
anchor=num_anchors, | ||
prediction=5 + num_classes, | ||
) | ||
anchors = torch.FloatTensor(anchors).to(input.device) | ||
anchor_sizes = rearrange(anchors, "anchor dim -> dim () anchor () ()") | ||
|
||
_, _, _, in_h, in_w = raw_predictions.shape | ||
grid_h = rearrange(torch.arange(in_h).float(), "h -> () () h ()").to(input.device) | ||
grid_w = rearrange(torch.arange(in_w).float(), "w -> () () () w").to(input.device) | ||
|
||
predicted_bboxes = torch.zeros_like(raw_predictions) | ||
predicted_bboxes[0] = (raw_predictions[0].sigmoid() + grid_w) * stride_w # center x | ||
predicted_bboxes[1] = (raw_predictions[1].sigmoid() + grid_h) * stride_h # center y | ||
predicted_bboxes[2:4] = ( | ||
raw_predictions[2:4].exp() | ||
) * anchor_sizes # bbox width and height | ||
predicted_bboxes[4] = raw_predictions[4].sigmoid() # confidence | ||
predicted_bboxes[5:] = raw_predictions[5:].sigmoid() # class predictions | ||
# merging all predicted bboxes for each image | ||
return rearrange( | ||
predicted_bboxes, "prediction b anchor h w -> b (anchor h w) prediction" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters