MCPcopy Create free account
hub / github.com/PABannier/sam3.cpp / sam3_sine_encode_box

Function sam3_sine_encode_box

sam3.cpp:7578–7601  ·  view source on GitHub ↗

Sinusoidal positional encoding for box coordinates. Matches Python PositionEmbeddingSine.encode_boxes(cx, cy, w, h). Output: [258] = [pos_y(128), pos_x(128), h, w]

Source from the content-addressed store, hash-verified

7576// Matches Python PositionEmbeddingSine.encode_boxes(cx, cy, w, h).
7577// Output: [258] = [pos_y(128), pos_x(128), h, w]
7578static void sam3_sine_encode_box(float* out, float cx, float cy, float w, float h,
7579 int num_pos_feats, int temperature) {
7580 const float scale = 2.0f * (float)M_PI;
7581 float x_embed = cx * scale;
7582 float y_embed = cy * scale;
7583
7584 // dim_t[i] = temperature^(2*(i//2)/num_pos_feats)
7585 for (int i = 0; i < num_pos_feats; ++i) {
7586 int div_idx = 2 * (i / 2);
7587 float dim_t = powf((float)temperature, (float)div_idx / (float)num_pos_feats);
7588 float px = x_embed / dim_t;
7589 float py = y_embed / dim_t;
7590 // Interleaved sin/cos: even indices get sin, odd get cos
7591 if (i % 2 == 0) {
7592 out[i] = sinf(py); // pos_y first
7593 out[num_pos_feats + i] = sinf(px); // pos_x second
7594 } else {
7595 out[i] = cosf(py);
7596 out[num_pos_feats + i] = cosf(px);
7597 }
7598 }
7599 out[2 * num_pos_feats] = h; // h
7600 out[2 * num_pos_feats + 1] = w; // w
7601}
7602
7603// CPU-side ROI Align matching torchvision.ops.roi_align behavior.
7604// Features in ggml [C, W, H] layout. Box in XYXY format scaled to feature grid coords.

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected