Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ S3method(draw_bounding_boxes,torch_tensor)
S3method(draw_segmentation_masks,default)
S3method(draw_segmentation_masks,image_with_segmentation_mask)
S3method(draw_segmentation_masks,torch_tensor)
S3method(get_image_size,"magick-image")
S3method(transform_adjust_brightness,default)
S3method(transform_adjust_brightness,torch_tensor)
S3method(transform_adjust_contrast,default)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Added article showcasing `model_fcn_resnet50()` with visualization utilities `draw_segmentation_masks()` and `vision_make_grid()` (@DerrickUnleashed, #281).
* Added collection dataset catalog with `search_collection()`, `get_collection_catalog()`, and `list_collection_datasets()` functions for discovering and exploring collections (#271, @ANAMASGARD).
* Added `target_transform_coco_masks()` and `target_transform_trimap_masks()` transformation functions for explicit segmentation mask generation (@ANAMASGARD).
* Added support for `connectivity` argument for drawing lines between keypoints in `draw_keypoints()` (@DerrickUnleashed #303)

## New models

Expand All @@ -26,11 +27,15 @@

## Bug fixes and improvements

* `mnist_datataset()` and derivatives now correctly return item x() values with a 1-channel dimension (@Chandraveersingh1717, #307).
* Fixed `draw_keypoints()` documentation and error message (@srishtiii28, #296).
* Standardized dataset messages: download messages now include split information, success messages show image count and class count for consistency.
* fix `model_fasterrcnn_*` did not provide boxes output normalized to image size, did not manage batches, fix performance of the `roi_align()` function (#284)
* fix rf100 collection bounding-box now consider the correct native COCO format being 'xywh' (#272)
* Remove `.getbatch` method from MNIST as it is providing inconsistent tensor dimensions with `.getitem` due
to non-vectorized `transform_` operations (#264)
* Added article for `draw_keypoints()` (@DerrickUnleashed #303)
* Fix typos and align model documentation for `model_deeplabv3_*` and `model_convnext_*_detection()` to ensure consistency.(@DerrickUnleashed #302)

# torchvision 0.8.0

Expand Down
165 changes: 93 additions & 72 deletions R/dataset-mnist.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ mnist_dataset <- dataset(
archive <- download_and_cache(r[1], prefix = class(self)[1])
fs::file_copy(archive, destpath)

if (!tools::md5sum(destpath) == r[2])
if (!(tools::md5sum(destpath) == r[2]))
runtime_error("Corrupt file! Delete the file in {archive} and try again.")

}
Expand Down Expand Up @@ -149,18 +149,36 @@ mnist_dataset <- dataset(
},

.getitem = function(index) {
x <- self$data[index, ,]
x <- self$data[index, , , drop = FALSE]
y <- self$targets[index]

if (!is.null(self$transform))
x <- self$transform(x)
if (!is.null(self$transform)) {
x <- self$transform(x)
}

if (!is.null(self$target_transform))
y <- self$target_transform(y)
if (!is.null(self$target_transform)) {
y <- self$target_transform(y)
}

list(x = x, y = y)
},

.getbatch = function(index) {
x_flat <- self$data[index, , , drop = FALSE]
# unsqueeze(2) on array
x <- array(x_flat, dim = c(length(index), 1, dim(x_flat)[2:3]))
y <- self$targets[index]

if (!is.null(self$transform)) {
x <- torch::torch_stack(lapply(seq_len(dim(x)[1]), function(i) self$transform(x[i,,,])))
}

if (!is.null(self$target_transform)) {
y <- self$target_transform(y)
}

list(x = x, y = y)
},
.length = function() {
dim(self$data)[1]
},
Expand All @@ -179,7 +197,7 @@ mnist_dataset <- dataset(
#' @describeIn mnist_dataset Kuzushiji-MNIST cursive Japanese character dataset.
#' @export
kmnist_dataset <- dataset(
name = "kminst_dataset",
name = "kmnist_dataset",
inherit = mnist_dataset,
archive_size = "21 MB",
resources = list(
Expand Down Expand Up @@ -252,55 +270,63 @@ qmnist_dataset <- dataset(
},

download = function() {
if (self$check_exists())
return(NULL)

fs::dir_create(self$raw_folder)
fs::dir_create(self$processed_folder)

cli_inform("Downloading {.cls {class(self)[[1]]}} ...")
for (r in self$resources[[self$split]]) {
filename <- basename(r[1])
destpath <- file.path(self$raw_folder, filename)

archive <- download_and_cache(r[1], prefix = glue::glue("qmnist-{self$split}"))
fs::file_copy(archive, destpath, overwrite = TRUE)

if (!tools::md5sum(destpath) == r[2])
runtime_error("Corrupt file! Delete the file in {archive} and try again.")
}

cli_inform("Processing {.cls {class(self)[[1]]}} ...")


if (self$split == "train") {
saveRDS(
list(
read_sn3_pascalvincent(file.path(self$raw_folder, "qmnist-train-images-idx3-ubyte.gz")),
read_sn3_pascalvincent(file.path(self$raw_folder, "qmnist-train-labels-idx2-int.gz"))
),
file.path(self$processed_folder, self$files$train)
)
split_exists <- function(split_name) {
fs::file_exists(file.path(self$processed_folder, self$files[[split_name]]))
}

if (self$split == "test") {
saveRDS(
list(
read_sn3_pascalvincent(file.path(self$raw_folder, "qmnist-test-images-idx3-ubyte.gz")),
read_sn3_pascalvincent(file.path(self$raw_folder, "qmnist-test-labels-idx2-int.gz"))
),
file.path(self$processed_folder, self$files$test)
)
}
if (all(vapply(names(self$resources), split_exists, logical(1))))
return(NULL)

if (self$split == "nist") {
saveRDS(
list(
read_sn3_pascalvincent(file.path(self$raw_folder, "xnist-images-idx3-ubyte.xz")),
read_sn3_pascalvincent(file.path(self$raw_folder, "xnist-labels-idx2-int.xz"))
),
file.path(self$processed_folder, self$files$nist)
)
cli_inform("Downloading {.cls {class(self)[[1]]}} ...")
for (split_name in names(self$resources)) {
if (split_exists(split_name))
next

for (r in self$resources[[split_name]]) {
filename <- basename(r[1])
destpath <- file.path(self$raw_folder, filename)

archive <- download_and_cache(r[1], prefix = glue::glue("qmnist-{split_name}"))
fs::file_copy(archive, destpath, overwrite = TRUE)

if (!(tools::md5sum(destpath) == r[2]))
runtime_error("Corrupt file! Delete the file in {archive} and try again.")
}

cli_inform("Processing {.val {split_name}} split for {.cls {class(self)[[1]]}} ...")

if (split_name == "train") {
saveRDS(
list(
read_sn3_pascalvincent(file.path(self$raw_folder, "qmnist-train-images-idx3-ubyte.gz")),
read_sn3_pascalvincent(file.path(self$raw_folder, "qmnist-train-labels-idx2-int.gz"))
),
file.path(self$processed_folder, self$files$train)
)
}

if (split_name == "test") {
saveRDS(
list(
read_sn3_pascalvincent(file.path(self$raw_folder, "qmnist-test-images-idx3-ubyte.gz")),
read_sn3_pascalvincent(file.path(self$raw_folder, "qmnist-test-labels-idx2-int.gz"))
),
file.path(self$processed_folder, self$files$test)
)
}

if (split_name == "nist") {
saveRDS(
list(
read_sn3_pascalvincent(file.path(self$raw_folder, "xnist-images-idx3-ubyte.xz")),
read_sn3_pascalvincent(file.path(self$raw_folder, "xnist-labels-idx2-int.xz"))
),
file.path(self$processed_folder, self$files$nist)
)
}
}

cli_inform("Dataset {.cls {class(self)[[1]]}} downloaded and extracted successfully.")
Expand Down Expand Up @@ -345,6 +371,7 @@ fashion_mnist_dataset <- dataset(
#' @export
emnist_collection <- dataset(
name = "emnist_collection",
inherit = mnist_dataset,
archive_size = "540 MB",

resources = list(
Expand Down Expand Up @@ -384,11 +411,10 @@ emnist_collection <- dataset(
self$split <- match.arg(split, choices = c("train", "test"))
self$dataset <- match.arg(dataset, choices = names(self$classes_all_dataset))
self$root_path <- root
self$raw_folder <- file.path(root, class(self)[1], "raw")
self$processed_folder <- file.path(root, class(self)[1], "processed")
self$transform <- transform
self$target_transform <- target_transform
self$class <- self$classes_all_dataset[[self$dataset]]
self$classes <- self$classes_all_dataset[[self$dataset]]
self$class <- self$classes

if (download) {
cli_inform("Dataset {.val {self$dataset}} split {.val {self$split}} of {.cls {class(self)[[1]]}} (~{.emph {self$archive_size}}) will be downloaded and processed if not already available.")
Expand All @@ -414,7 +440,7 @@ emnist_collection <- dataset(
url <- self$resources[[1]][1]
archive <- download_and_cache(url, prefix = class(self)[1])

if (!tools::md5sum(archive) == self$resources[[1]][2])
if (!(tools::md5sum(archive) == self$resources[[1]][2]))
runtime_error("Corrupt file! Delete the file in {archive} and try again.")

unzip_dir <- file.path(self$raw_folder, "unzipped")
Expand All @@ -436,28 +462,23 @@ emnist_collection <- dataset(
fs::file_exists(file.path(self$processed_folder, self$rds_file(self$split, self$dataset)))
},

.getitem = function(index) {

x <- self$data[index, , ]
y <- self$targets[index]

if (!is.null(self$transform))
x <- self$transform(x)

if (!is.null(self$target_transform))
y <- self$target_transform(y)

list(x = x, y = y)
},

.length = function() {
dim(self$data)[1]
}
active = list(
raw_folder = function() {
file.path(self$root_path, "emnist_collection", "raw")
},

processed_folder = function() {
file.path(self$root_path, "emnist_collection", "processed")
}
)
)

read_sn3_pascalvincent <- function(path) {
x <- gzfile(path, open = "rb")
x <- if (grepl("\\\\.xz$", path, ignore.case = TRUE)) {
xzfile(path, open = "rb")
} else {
gzfile(path, open = "rb")
}
Comment on lines +477 to +481

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise nice addition

on.exit({close(x)})

magic <- readBin(x, endian = "big", what = integer(), n = 1)
Expand Down
11 changes: 10 additions & 1 deletion R/transforms-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ transform_to_tensor.array <- function(img) {
if (length(dim(img)) == 2)
dim(img) <- c(dim(img), 1)

res <- torch::torch_tensor(img)$permute(c(3, 1, 2))
dims <- dim(img)
if (length(dims) != 3)
value_error("Expected a 2D or 3D array.")

# Support both HWC (default image arrays) and CHW (channel-first arrays).
if (dims[1] <= 4 && dims[3] > 4) {
res <- torch::torch_tensor(img)
} else {
res <- torch::torch_tensor(img)$permute(c(3, 1, 2))
}

if (res$dtype == torch::torch_long())
res <- res/255
Expand Down
2 changes: 2 additions & 0 deletions R/transforms-magick.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

# Utils -------------------------------------------------------------------

#' @method get_image_size magick-image
#' @export
`get_image_size.magick-image` <- function(img) {
info <- magick::image_info(img)
c(info$width, info$height)
Expand Down
Loading
Loading