-
Notifications
You must be signed in to change notification settings - Fork 27
feature/issue-256-convnext-detection #262
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
Merged
cregouby
merged 7 commits into
mlverse:main
from
ANAMASGARD:feature/issue-256-convnext-detection
Dec 6, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b06e940
test: add comprehensive tests for ConvNeXt object detection head
ANAMASGARD 214b9d5
feat: add ConvNeXt detection models
ANAMASGARD 663e0cd
Merge branch 'main' into feature/issue-256-convnext-detection
cregouby 9d80211
document functions and add NEWS
cregouby 211736b
address reviewer feedback: add contributor, use cli_abort, add example
ANAMASGARD 831c1b6
Fix detection example: clarify that predictions are random without pr…
ANAMASGARD d41b6e2
restore meaningfull example
cregouby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -20,6 +20,9 @@ Authors@R: c( | |
| role = c("ctb"), | ||
| email = "[email protected]" | ||
| ), | ||
| person(given = "ANAMASGARD", | ||
| role = c("ctb") | ||
| ), | ||
| person(family = "RStudio", role = c("cph")) | ||
| ) | ||
| Description: Provides access to datasets, models and preprocessing | ||
|
|
@@ -85,6 +88,7 @@ Collate: | |
| 'imagenet.R' | ||
| 'models-alexnet.R' | ||
| 'models-convnext.R' | ||
| 'models-convnext_detection.R' | ||
| 'models-deeplabv3.R' | ||
| 'models-efficientnet.R' | ||
| 'models-efficientnetv2.R' | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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,266 @@ | ||
| #' ConvNeXt Detection Models (Faster R-CNN style) | ||
| #' | ||
| #' @description | ||
| #' Object detection models that use a ConvNeXt backbone with a Feature | ||
| #' Pyramid Network (FPN) and the same detection head as the Faster R-CNN | ||
| #' models implemented in `model_fasterrcnn_*`. | ||
| #' | ||
| #' These helpers mirror the architecture used in | ||
| #' `model_fasterrcnn_resnet50_fpn()`, but swap the ResNet backbone for | ||
| #' ConvNeXt variants. | ||
| #' | ||
| #' @section Available Models: | ||
| #' \itemize{ | ||
| #' \item `model_convnext_tiny_detection()` | ||
| #' \item `model_convnext_small_detection()` | ||
| #' \item `model_convnext_base_detection()` | ||
| #' } | ||
| #' | ||
| #' @inheritParams model_fasterrcnn_resnet50_fpn | ||
| #' @param pretrained_backbone Logical, if `TRUE` the ConvNeXt backbone | ||
| #' weights are loaded from ImageNet pretraining. | ||
| #' | ||
| #' @note Currently, detection head weights are randomly initialized, so predicted | ||
| #' bounding-boxes are random. For meaningful results, you need to train the model | ||
| #' detection head on your data. | ||
| #' | ||
| #' @examples | ||
| #' \dontrun{ | ||
| #' library(magrittr) | ||
| #' norm_mean <- c(0.485, 0.456, 0.406) # ImageNet normalization constants | ||
| #' norm_std <- c(0.229, 0.224, 0.225) | ||
| #' | ||
| #' # Use a publicly available image | ||
| #' wmc <- "https://upload.wikimedia.org/wikipedia/commons/thumb/" | ||
| #' url <- "e/ea/Morsan_Normande_vache.jpg/120px-Morsan_Normande_vache.jpg" | ||
| #' img <- base_loader(paste0(wmc, url)) | ||
| #' | ||
| #' input <- img %>% | ||
| #' transform_to_tensor() %>% | ||
| #' transform_resize(c(520, 520)) %>% | ||
| #' transform_normalize(norm_mean, norm_std) | ||
| #' batch <- input$unsqueeze(1) # Add batch dimension (1, 3, H, W) | ||
| #' | ||
| #' # ConvNeXt Tiny detection | ||
| #' model <- model_convnext_tiny_detection(pretrained_backbone = TRUE) | ||
| #' model$eval() | ||
| #' pred <- model(batch)$detections | ||
| #' num_boxes <- as.integer(pred$boxes$size()[1]) | ||
| #' topk <- pred$scores$topk(k = 5)[[2]] | ||
| #' boxes <- pred$boxes[topk, ] | ||
| #' labels <- as.character(as.integer(pred$labels[topk])) | ||
| #' | ||
| #' # `draw_bounding_box()` may fail if bbox values are not consistent. | ||
| #' if (num_boxes > 0) { | ||
| #' boxed <- draw_bounding_boxes(input, boxes, labels = labels) | ||
| #' tensor_image_browse(boxed) | ||
| #' } | ||
| #' } | ||
| #' | ||
| #' @family object_detection_model | ||
| #' @name model_convnext_detection | ||
| NULL | ||
|
|
||
|
|
||
| convnext_fpn_backbone_tiny <- function(pretrained_backbone = FALSE, ...) { | ||
| convnext <- model_convnext_tiny_1k(pretrained = pretrained_backbone, ...) | ||
|
|
||
| convnext_body <- torch::nn_module( | ||
| initialize = function() { | ||
| self$model <- convnext | ||
| }, | ||
| forward = function(x) { | ||
| c2 <- x %>% | ||
| self$model$downsample_layers[[1]]() %>% | ||
| self$model$stages[[1]]() | ||
|
|
||
| c3 <- c2 %>% | ||
| self$model$downsample_layers[[2]]() %>% | ||
| self$model$stages[[2]]() | ||
|
|
||
| c4 <- c3 %>% | ||
| self$model$downsample_layers[[3]]() %>% | ||
| self$model$stages[[3]]() | ||
|
|
||
| c5 <- c4 %>% | ||
| self$model$downsample_layers[[4]]() %>% | ||
| self$model$stages[[4]]() | ||
|
|
||
| list(c2, c3, c4, c5) | ||
| } | ||
| ) | ||
|
|
||
| backbone_module <- torch::nn_module( | ||
| initialize = function() { | ||
| self$body <- convnext_body() | ||
| self$fpn <- fpn_module( | ||
| in_channels = c(96, 192, 384, 768), | ||
| out_channels = 256 | ||
| )() | ||
| }, | ||
| forward = function(x) { | ||
| c2_to_c5 <- self$body(x) | ||
| self$fpn(c2_to_c5) | ||
| } | ||
| ) | ||
|
|
||
| backbone <- backbone_module() | ||
| backbone$out_channels <- 256 | ||
| backbone | ||
| } | ||
|
|
||
|
|
||
| convnext_fpn_backbone_small <- function(pretrained_backbone = FALSE, ...) { | ||
| convnext <- model_convnext_small_22k(pretrained = pretrained_backbone, ...) | ||
|
|
||
| convnext_body <- torch::nn_module( | ||
| initialize = function() { | ||
| self$model <- convnext | ||
| }, | ||
| forward = function(x) { | ||
| c2 <- x %>% | ||
| self$model$downsample_layers[[1]]() %>% | ||
| self$model$stages[[1]]() | ||
|
|
||
| c3 <- c2 %>% | ||
| self$model$downsample_layers[[2]]() %>% | ||
| self$model$stages[[2]]() | ||
|
|
||
| c4 <- c3 %>% | ||
| self$model$downsample_layers[[3]]() %>% | ||
| self$model$stages[[3]]() | ||
|
|
||
| c5 <- c4 %>% | ||
| self$model$downsample_layers[[4]]() %>% | ||
| self$model$stages[[4]]() | ||
|
|
||
| list(c2, c3, c4, c5) | ||
| } | ||
| ) | ||
|
|
||
| backbone_module <- torch::nn_module( | ||
| initialize = function() { | ||
| self$body <- convnext_body() | ||
| self$fpn <- fpn_module( | ||
| in_channels = c(96, 192, 384, 768), | ||
| out_channels = 256 | ||
| )() | ||
| }, | ||
| forward = function(x) { | ||
| c2_to_c5 <- self$body(x) | ||
| self$fpn(c2_to_c5) | ||
| } | ||
| ) | ||
|
|
||
| backbone <- backbone_module() | ||
| backbone$out_channels <- 256 | ||
| backbone | ||
| } | ||
|
|
||
|
|
||
| convnext_fpn_backbone_base <- function(pretrained_backbone = FALSE, ...) { | ||
| convnext <- model_convnext_base_1k(pretrained = pretrained_backbone, ...) | ||
|
|
||
| convnext_body <- torch::nn_module( | ||
| initialize = function() { | ||
| self$model <- convnext | ||
| }, | ||
| forward = function(x) { | ||
| c2 <- x %>% | ||
| self$model$downsample_layers[[1]]() %>% | ||
| self$model$stages[[1]]() | ||
|
|
||
| c3 <- c2 %>% | ||
| self$model$downsample_layers[[2]]() %>% | ||
| self$model$stages[[2]]() | ||
|
|
||
| c4 <- c3 %>% | ||
| self$model$downsample_layers[[3]]() %>% | ||
| self$model$stages[[3]]() | ||
|
|
||
| c5 <- c4 %>% | ||
| self$model$downsample_layers[[4]]() %>% | ||
| self$model$stages[[4]]() | ||
|
|
||
| list(c2, c3, c4, c5) | ||
| } | ||
| ) | ||
|
|
||
| backbone_module <- torch::nn_module( | ||
| initialize = function() { | ||
| self$body <- convnext_body() | ||
| self$fpn <- fpn_module( | ||
| in_channels = c(128, 256, 512, 1024), | ||
| out_channels = 256 | ||
| )() | ||
| }, | ||
| forward = function(x) { | ||
| c2_to_c5 <- self$body(x) | ||
| self$fpn(c2_to_c5) | ||
| } | ||
| ) | ||
|
|
||
| backbone <- backbone_module() | ||
| backbone$out_channels <- 256 | ||
| backbone | ||
| } | ||
|
|
||
|
|
||
| validate_convnext_num_classes <- function(num_classes) { | ||
| if (num_classes <= 0) { | ||
| cli_abort("{.var num_classes} must be positive") | ||
| } | ||
| } | ||
|
|
||
|
|
||
| #' @describeIn model_convnext_detection ConvNeXt Tiny with FPN detection head | ||
| #' @export | ||
| model_convnext_tiny_detection <- function(num_classes = 91, | ||
| pretrained_backbone = FALSE, | ||
| ...) { | ||
| validate_convnext_num_classes(num_classes) | ||
|
|
||
| backbone <- convnext_fpn_backbone_tiny( | ||
| pretrained_backbone = pretrained_backbone, | ||
| ... | ||
| ) | ||
|
|
||
| model <- fasterrcnn_model(backbone, num_classes = num_classes)() | ||
| model | ||
| } | ||
|
|
||
|
|
||
| #' @describeIn model_convnext_detection ConvNeXt Small with FPN detection head | ||
| #' @export | ||
| model_convnext_small_detection <- function(num_classes = 91, | ||
| pretrained_backbone = FALSE, | ||
| ...) { | ||
| validate_convnext_num_classes(num_classes) | ||
|
|
||
| backbone <- convnext_fpn_backbone_small( | ||
| pretrained_backbone = pretrained_backbone, | ||
| ... | ||
| ) | ||
|
|
||
| model <- fasterrcnn_model(backbone, num_classes = num_classes)() | ||
| model | ||
| } | ||
|
|
||
|
|
||
| #' @describeIn model_convnext_detection ConvNeXt Base with FPN detection head | ||
| #' @export | ||
| model_convnext_base_detection <- function(num_classes = 91, | ||
| pretrained_backbone = FALSE, | ||
| ...) { | ||
| validate_convnext_num_classes(num_classes) | ||
|
|
||
| backbone <- convnext_fpn_backbone_base( | ||
| pretrained_backbone = pretrained_backbone, | ||
| ... | ||
| ) | ||
|
|
||
| model <- fasterrcnn_model(backbone, num_classes = num_classes)() | ||
| model | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -429,7 +429,7 @@ fasterrcnn_model <- function(backbone, num_classes) { | |
| ) | ||
| ) | ||
| } | ||
| ) # <- Removed the () here | ||
| ) | ||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.