-
Notifications
You must be signed in to change notification settings - Fork 31
Fix grayscale channel dimension (1) lost in DataLoader #307
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
Changes from 2 commits
e14d80b
809c665
1e50c94
c6e98bd
44e4c87
ec658a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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.") | ||
|
|
||
| } | ||
|
|
@@ -149,8 +149,13 @@ mnist_dataset <- dataset( | |
| }, | ||
|
|
||
| .getitem = function(index) { | ||
| x <- self$data[index, ,] | ||
| y <- self$targets[index] | ||
| if (length(index) != 1) { | ||
| return(lapply(as.integer(index), function(i) self$.getitem(i))) | ||
| } | ||
|
|
||
| idx <- as.integer(index) | ||
| x <- self$data[idx, , , drop = FALSE] | ||
| y <- self$targets[idx] | ||
|
|
||
| if (!is.null(self$transform)) | ||
| x <- self$transform(x) | ||
|
|
@@ -179,7 +184,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( | ||
|
|
@@ -252,55 +257,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.") | ||
| split_exists <- function(split_name) { | ||
| fs::file_exists(file.path(self$processed_folder, self$files[[split_name]])) | ||
| } | ||
|
|
||
| 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) | ||
| ) | ||
| } | ||
|
|
||
| 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.") | ||
|
|
@@ -388,7 +401,8 @@ emnist_collection <- dataset( | |
| 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.") | ||
|
|
@@ -414,7 +428,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") | ||
|
|
@@ -437,9 +451,13 @@ emnist_collection <- dataset( | |
| }, | ||
|
|
||
| .getitem = function(index) { | ||
| if (length(index) != 1) { | ||
| return(lapply(as.integer(index), function(i) self$.getitem(i))) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion this is supposed to be the |
||
|
|
||
| x <- self$data[index, , ] | ||
| y <- self$targets[index] | ||
| idx <- as.integer(index) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion `.getitem() is mainly an internal function for dataloader to run. So index will always be integer. I'd prefer to avoid the perrformance impact and remove this line. |
||
| x <- self$data[idx, , , drop = FALSE] | ||
| y <- self$targets[idx] | ||
|
|
||
| if (!is.null(self$transform)) | ||
| x <- self$transform(x) | ||
|
|
@@ -457,7 +475,11 @@ emnist_collection <- dataset( | |
| ) | ||
|
|
||
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
| stop("Expected a 2D or 3D array.") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo Please rely on functions from |
||
|
|
||
| # 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo Same as for L454-L458