├── .Rbuildignore ├── .github ├── .gitignore └── workflows │ ├── R-CMD-check.yaml │ ├── models.yaml │ └── pkgdown.yaml ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── NEWS.md ├── R ├── conditions.R ├── dataset-cifar.R ├── dataset-mnist.R ├── folder-dataset.R ├── globals.R ├── models-alexnet.R ├── models-inception.R ├── models-mobilenetv2.R ├── models-resnet.R ├── models-vgg.R ├── tiny-imagenet-dataset.R ├── transforms-array.R ├── transforms-defaults.R ├── transforms-generics.R ├── transforms-magick.R ├── transforms-tensor.R ├── utils.R └── vision_utils.R ├── README.md ├── _pkgdown.yml ├── cran-comments.md ├── inst └── po │ └── fr │ └── LC_MESSAGES │ └── R-torchvision.mo ├── man ├── base_loader.Rd ├── cifar10_dataset.Rd ├── draw_bounding_boxes.Rd ├── draw_keypoints.Rd ├── draw_segmentation_masks.Rd ├── fashion_mnist_dataset.Rd ├── image_folder_dataset.Rd ├── kmnist_dataset.Rd ├── magick_loader.Rd ├── mnist_dataset.Rd ├── model_alexnet.Rd ├── model_inception_v3.Rd ├── model_mobilenet_v2.Rd ├── model_resnet.Rd ├── model_vgg.Rd ├── tensor_image_browse.Rd ├── tensor_image_display.Rd ├── tiny_imagenet_dataset.Rd ├── transform_adjust_brightness.Rd ├── transform_adjust_contrast.Rd ├── transform_adjust_gamma.Rd ├── transform_adjust_hue.Rd ├── transform_adjust_saturation.Rd ├── transform_affine.Rd ├── transform_center_crop.Rd ├── transform_color_jitter.Rd ├── transform_convert_image_dtype.Rd ├── transform_crop.Rd ├── transform_five_crop.Rd ├── transform_grayscale.Rd ├── transform_hflip.Rd ├── transform_linear_transformation.Rd ├── transform_normalize.Rd ├── transform_pad.Rd ├── transform_perspective.Rd ├── transform_random_affine.Rd ├── transform_random_apply.Rd ├── transform_random_choice.Rd ├── transform_random_crop.Rd ├── transform_random_erasing.Rd ├── transform_random_grayscale.Rd ├── transform_random_horizontal_flip.Rd ├── transform_random_order.Rd ├── transform_random_perspective.Rd ├── transform_random_resized_crop.Rd ├── transform_random_rotation.Rd ├── transform_random_vertical_flip.Rd ├── transform_resize.Rd ├── transform_resized_crop.Rd ├── transform_rgb_to_grayscale.Rd ├── transform_rotate.Rd ├── transform_ten_crop.Rd ├── transform_to_tensor.Rd ├── transform_vflip.Rd └── vision_make_grid.Rd ├── po ├── R-fr.po └── R-torchvision.pot ├── tests ├── testthat.R └── testthat │ ├── assets │ └── class │ │ ├── cat │ │ ├── cat.0.jpg │ │ ├── cat.1.jpg │ │ ├── cat.2.jpg │ │ ├── cat.3.jpg │ │ ├── cat.4.jpg │ │ ├── cat.5.jpg │ │ └── cat.txt │ │ └── dog │ │ ├── dog.0.jpg │ │ ├── dog.1.jpg │ │ ├── dog.2.jpg │ │ ├── dog.3.jpg │ │ ├── dog.4.jpg │ │ └── dog.5.jpg │ ├── helper-torchvision.R │ ├── test-dataset-cifar.R │ ├── test-dataset-mnist.R │ ├── test-folder-dataset.R │ ├── test-message-translations.R │ ├── test-models-alexnet.R │ ├── test-models-inception.R │ ├── test-models-mobilenetv2.R │ ├── test-models-resnet.R │ ├── test-models-vgg.R │ ├── test-transforms-defaults.R │ ├── test-transforms-magick.R │ ├── test-transforms.R │ ├── test-vision-utils.R │ └── torch.png ├── tools ├── convert-models.py └── document.R ├── torchvision.Rproj └── vignettes └── examples ├── assets ├── dancing.jpg └── picasso.jpg ├── mnist-cnn.R ├── mnist-cnn.Rmd ├── mnist-dcgan.R ├── mnist-dcgan.Rmd ├── mnist-mlp.R ├── mnist-mlp.Rmd ├── style-transfer.R ├── style-transfer.Rmd ├── texture-nca.R ├── texture-nca.Rmd ├── tinyimagenet-alexnet.R └── tinyimagenet-alexnet.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^torchvision\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^LICENSE\.md$ 4 | ^\.github$ 5 | ^_pkgdown\.yml$ 6 | ^docs$ 7 | ^pkgdown$ 8 | ^vignettes/examples/.* 9 | ^env$ 10 | ^models$ 11 | ^test.pth$ 12 | ^testing.pth$ 13 | ^x.pth$ 14 | ^cran-comments\.md$ 15 | ^CRAN-RELEASE$ 16 | ^CRAN-SUBMISSION$ 17 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - main 5 | pull_request: 6 | branches: 7 | - main 8 | schedule: 9 | - cron: "0 1 * * *" 10 | 11 | name: R-CMD-check 12 | 13 | jobs: 14 | R-CMD-check: 15 | runs-on: ${{ matrix.config.os }} 16 | 17 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 18 | 19 | strategy: 20 | fail-fast: false 21 | matrix: 22 | config: 23 | - {os: windows-latest, r: 'release'} 24 | - {os: macOS-latest, r: 'release'} 25 | - {os: ubuntu-22.04, r: 'release', rspm: "https://packagemanager.posit.co/cran/__linux__/jammy/latest"} 26 | 27 | env: 28 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 29 | RSPM: ${{ matrix.config.rspm }} 30 | TORCH_INSTALL: 1 31 | TORCH_TEST: 1 32 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 33 | 34 | steps: 35 | - uses: actions/checkout@v4 36 | 37 | - uses: r-lib/actions/setup-r@v2 38 | with: 39 | r-version: ${{ matrix.config.r }} 40 | 41 | - uses: r-lib/actions/setup-pandoc@v2 42 | 43 | - uses: r-lib/actions/setup-r-dependencies@v2 44 | with: 45 | extra-packages: | 46 | any::rcmdcheck 47 | 48 | - name: Check 49 | env: 50 | _R_CHECK_CRAN_INCOMING_REMOTE_: false 51 | run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran", "--no-multiarch"), error_on = "warning", check_dir = "check") 52 | shell: Rscript {0} 53 | 54 | - name: Upload check results 55 | if: failure() 56 | uses: actions/upload-artifact@v4 57 | with: 58 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 59 | path: check 60 | -------------------------------------------------------------------------------- /.github/workflows/models.yaml: -------------------------------------------------------------------------------- 1 | name: Convert and upload pre-trained models 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | upload: 8 | runs-on: macos-10.15 9 | steps: 10 | - uses: actions/checkout@v4 11 | - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master 12 | with: 13 | version: '290.0.1' 14 | project_id: ${{ secrets.GCP_PROJECT_ID }} 15 | service_account_key: ${{ secrets.GCP_SA_KEY }} 16 | export_default_credentials: true 17 | - uses: actions/setup-python@v2 18 | with: 19 | python-version: 3.7 20 | - run: | 21 | python -m pip install --upgrade pip 22 | pip install torch torchvision google-cloud-storage 23 | - run: | 24 | python tools/convert-models.py 25 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: main 4 | 5 | name: pkgdown 6 | 7 | jobs: 8 | pkgdown: 9 | runs-on: macOS-latest 10 | env: 11 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 12 | steps: 13 | - uses: actions/checkout@v4 14 | 15 | - uses: r-lib/actions/setup-r@v2 16 | 17 | - uses: r-lib/actions/setup-pandoc@v2 18 | 19 | - name: Install dependencies 20 | run: | 21 | install.packages("remotes") 22 | install.packages("processx") 23 | remotes::install_deps(dependencies = TRUE) 24 | install.packages("pkgdown") 25 | shell: Rscript {0} 26 | 27 | - name: Install package 28 | run: R CMD INSTALL . 29 | 30 | - name: Deploy package 31 | run: | 32 | git config --local user.email "actions@github.com" 33 | git config --local user.name "GitHub Actions" 34 | Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | models/ 3 | env/ 4 | test.pth 5 | testing.pth 6 | x.pth 7 | docs 8 | .Rhistory 9 | s.pth 10 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: torchvision 2 | Title: Models, Datasets and Transformations for Images 3 | Version: 0.6.0.9000 4 | Authors@R: c( 5 | person(given = "Daniel", 6 | family = "Falbel", 7 | role = c("aut", "cre"), 8 | email = "daniel@rstudio.com" 9 | ), 10 | person(given = "Christophe", 11 | family = "Regouby", 12 | role = c("ctb"), 13 | email = "christophe.regouby@free.fr" 14 | ), 15 | person(given = "Akanksha", 16 | family = "Koshti", 17 | role = c("ctb")), 18 | person(family = "RStudio", role = c("cph")) 19 | ) 20 | Description: Provides access to datasets, models and preprocessing 21 | facilities for deep learning with images. Integrates seamlessly 22 | with the 'torch' package and it's 'API' borrows heavily from 23 | 'PyTorch' vision package. 24 | License: MIT + file LICENSE 25 | Encoding: UTF-8 26 | Roxygen: list(markdown = TRUE) 27 | URL: https://torchvision.mlverse.org, https://github.com/mlverse/torchvision 28 | RoxygenNote: 7.3.2 29 | Imports: 30 | torch (>= 0.5.0), 31 | fs, 32 | rlang, 33 | rappdirs, 34 | utils, 35 | jpeg, 36 | magrittr, 37 | png, 38 | abind, 39 | withr, 40 | glue 41 | Suggests: 42 | magick, 43 | testthat, 44 | coro 45 | BugReports: https://github.com/mlverse/torchvision/issues 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2020 2 | COPYRIGHT HOLDER: RStudio 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2020 RStudio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(transform_adjust_brightness,default) 4 | S3method(transform_adjust_brightness,torch_tensor) 5 | S3method(transform_adjust_contrast,default) 6 | S3method(transform_adjust_contrast,torch_tensor) 7 | S3method(transform_adjust_gamma,default) 8 | S3method(transform_adjust_gamma,torch_tensor) 9 | S3method(transform_adjust_hue,default) 10 | S3method(transform_adjust_hue,torch_tensor) 11 | S3method(transform_adjust_saturation,default) 12 | S3method(transform_adjust_saturation,torch_tensor) 13 | S3method(transform_affine,default) 14 | S3method(transform_affine,torch_tensor) 15 | S3method(transform_center_crop,default) 16 | S3method(transform_color_jitter,default) 17 | S3method(transform_convert_image_dtype,default) 18 | S3method(transform_convert_image_dtype,torch_tensor) 19 | S3method(transform_crop,"magick-image") 20 | S3method(transform_crop,default) 21 | S3method(transform_crop,torch_tensor) 22 | S3method(transform_five_crop,default) 23 | S3method(transform_five_crop,torch_tensor) 24 | S3method(transform_grayscale,default) 25 | S3method(transform_hflip,"magick-image") 26 | S3method(transform_hflip,default) 27 | S3method(transform_hflip,torch_tensor) 28 | S3method(transform_linear_transformation,default) 29 | S3method(transform_linear_transformation,torch_tensor) 30 | S3method(transform_normalize,default) 31 | S3method(transform_normalize,torch_tensor) 32 | S3method(transform_pad,default) 33 | S3method(transform_pad,torch_tensor) 34 | S3method(transform_perspective,default) 35 | S3method(transform_perspective,torch_tensor) 36 | S3method(transform_random_affine,default) 37 | S3method(transform_random_apply,default) 38 | S3method(transform_random_choice,default) 39 | S3method(transform_random_crop,default) 40 | S3method(transform_random_erasing,default) 41 | S3method(transform_random_grayscale,default) 42 | S3method(transform_random_horizontal_flip,default) 43 | S3method(transform_random_order,default) 44 | S3method(transform_random_perspective,default) 45 | S3method(transform_random_resized_crop,default) 46 | S3method(transform_random_rotation,default) 47 | S3method(transform_random_vertical_flip,default) 48 | S3method(transform_resize,"magick-image") 49 | S3method(transform_resize,default) 50 | S3method(transform_resize,torch_tensor) 51 | S3method(transform_resized_crop,default) 52 | S3method(transform_rgb_to_grayscale,torch_tensor) 53 | S3method(transform_rotate,default) 54 | S3method(transform_rotate,torch_tensor) 55 | S3method(transform_ten_crop,default) 56 | S3method(transform_ten_crop,torch_tensor) 57 | S3method(transform_to_tensor,"magick-image") 58 | S3method(transform_to_tensor,array) 59 | S3method(transform_to_tensor,default) 60 | S3method(transform_to_tensor,matrix) 61 | S3method(transform_vflip,default) 62 | S3method(transform_vflip,torch_tensor) 63 | export(base_loader) 64 | export(cifar100_dataset) 65 | export(cifar10_dataset) 66 | export(draw_bounding_boxes) 67 | export(draw_keypoints) 68 | export(draw_segmentation_masks) 69 | export(fashion_mnist_dataset) 70 | export(image_folder_dataset) 71 | export(kmnist_dataset) 72 | export(magick_loader) 73 | export(mnist_dataset) 74 | export(model_alexnet) 75 | export(model_inception_v3) 76 | export(model_mobilenet_v2) 77 | export(model_resnet101) 78 | export(model_resnet152) 79 | export(model_resnet18) 80 | export(model_resnet34) 81 | export(model_resnet50) 82 | export(model_resnext101_32x8d) 83 | export(model_resnext50_32x4d) 84 | export(model_vgg11) 85 | export(model_vgg11_bn) 86 | export(model_vgg13) 87 | export(model_vgg13_bn) 88 | export(model_vgg16) 89 | export(model_vgg16_bn) 90 | export(model_vgg19) 91 | export(model_vgg19_bn) 92 | export(model_wide_resnet101_2) 93 | export(model_wide_resnet50_2) 94 | export(tensor_image_browse) 95 | export(tensor_image_display) 96 | export(tiny_imagenet_dataset) 97 | export(transform_adjust_brightness) 98 | export(transform_adjust_contrast) 99 | export(transform_adjust_gamma) 100 | export(transform_adjust_hue) 101 | export(transform_adjust_saturation) 102 | export(transform_affine) 103 | export(transform_center_crop) 104 | export(transform_color_jitter) 105 | export(transform_convert_image_dtype) 106 | export(transform_crop) 107 | export(transform_five_crop) 108 | export(transform_grayscale) 109 | export(transform_hflip) 110 | export(transform_linear_transformation) 111 | export(transform_normalize) 112 | export(transform_pad) 113 | export(transform_perspective) 114 | export(transform_random_affine) 115 | export(transform_random_apply) 116 | export(transform_random_choice) 117 | export(transform_random_crop) 118 | export(transform_random_erasing) 119 | export(transform_random_grayscale) 120 | export(transform_random_horizontal_flip) 121 | export(transform_random_order) 122 | export(transform_random_perspective) 123 | export(transform_random_resized_crop) 124 | export(transform_random_rotation) 125 | export(transform_random_vertical_flip) 126 | export(transform_resize) 127 | export(transform_resized_crop) 128 | export(transform_rgb_to_grayscale) 129 | export(transform_rotate) 130 | export(transform_ten_crop) 131 | export(transform_to_tensor) 132 | export(transform_vflip) 133 | export(vision_make_grid) 134 | importFrom(magrittr,"%>%") 135 | importFrom(torch,dataset) 136 | importFrom(utils,tail) 137 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # torchvision (development version) 2 | 3 | - fix `transform_rotation` wrongly using w x w for image size (@114, cregouby) 4 | - `tensor_image_display` and `tensor_image_browse` now accept all tensor_image dtypes. (#115, @cregouby) 5 | - fix `transform_affine` help to remove confusion with `transforme_random_affine` help (#116, @cregouby) 6 | - add message translation in french (#112, @cregouby) 7 | - Added support for the Fashion-MNIST dataset. (#148, @koshtiakanksha) 8 | 9 | # torchvision 0.6.0 10 | 11 | - Remove again dependency on `zip::unzip` added in version 0.4.0. (#89) 12 | - Improve performance on `tinyimagenet-alexnet` example (#90, @statist-bhfz) 13 | - Updated URL of downloaded resources to use the new torch CDN. (#109) 14 | 15 | # torchvision 0.5.1 16 | 17 | - Remove usage of `torch_lstsq` that was removed in torch v0.10.0 18 | 19 | # torchvision 0.5.0 20 | 21 | - Bugs fixed in `transform_adjust_hue()` and `transform_linear_transformation()` (#72, #73, @sebffischer) 22 | - add `draw_bounding_boxes()` , `draw_segmentation_masks()` and `draw_keypoints()` on top of image tensors, and add a convenience `tensor_image_browse()` and `tensor_image_display()` functions to visualize image tensors respectively in browser or in X11 device (#80, @cregouby) 23 | - Added the InceptionV3 model. (#82) 24 | 25 | # torchvision 0.4.1 26 | 27 | - Implemented MobileNetV2 (#60) 28 | - Improved vignettes so they use `nnf_cross_entropy` for numerical stability. (#61) 29 | - Implement the full list of ResNet model family (#66, @cregouby) 30 | - Improved how datasets and models are downloaded by using a large timeout by default and downloading to temporary file to avoid hard to debug errors when the files are corrupt. (#67) 31 | 32 | # torchvision 0.4.0 33 | 34 | - Added a dependency on `zip` to `zip::unzip` the tinyimagenet dataset. 35 | - Removed all usages of `torch::enumerate()` from docs and tests in favor of `coro::loop()` (#57) 36 | - Fixed non-namespaced calls to `torch`. (#58) 37 | 38 | # torchvision 0.3.0 39 | 40 | - Use a self hosted version of the MNIST dataset to avoid frequent download failures. (#48) 41 | - Fix `torch_arange` calls after breaking change in `torch`. (#47) 42 | - Fix bug in `transform_resize` when passing `size` with length 1. (#49) 43 | 44 | # torchvision 0.2.0 45 | 46 | - Fixed bugs in `transform_rotate`. (#31) 47 | - Fixed bugs in `transform_random_affine` and `transform_affine` (#32) 48 | - Added VGG model (#35) 49 | 50 | # torchvision 0.1.0 51 | 52 | - Added a `NEWS.md` file to track changes to the package. 53 | -------------------------------------------------------------------------------- /R/conditions.R: -------------------------------------------------------------------------------- 1 | type_error <- function(msg, env = rlang::caller_env()) { 2 | rlang::abort(glue::glue(gettext(msg), .envir = env), class = "type_error") 3 | } 4 | 5 | value_error <- function(..., env = rlang::caller_env()) { 6 | rlang::abort(glue::glue(gettext(..., domain = "R-torchvision"), .envir = env), class = "value_error") 7 | } 8 | 9 | runtime_error <- function(msg, env = rlang::caller_env()) { 10 | rlang::abort(glue::glue(gettext(msg), .envir = env), class = "runtime_error") 11 | } 12 | 13 | not_implemented_error <- function(msg, env = rlang::caller_env()) { 14 | rlang::abort(glue::glue(gettext(msg), .envir = env), class = "not_implemented_error") 15 | } 16 | 17 | -------------------------------------------------------------------------------- /R/dataset-cifar.R: -------------------------------------------------------------------------------- 1 | 2 | #' Cifar datasets 3 | #' 4 | #' [CIFAR10](https://www.cs.toronto.edu/~kriz/cifar.html) Dataset. 5 | #' 6 | #' @param root (string): Root directory of dataset where directory 7 | #' `cifar-10-batches-bin` exists or will be saved to if download is set to TRUE. 8 | #' @param train (bool, optional): If TRUE, creates dataset from training set, otherwise 9 | #' creates from test set. 10 | #' @param transform (callable, optional): A function/transform that takes in an PIL image 11 | #' and returns a transformed version. E.g, [transform_random_crop()] 12 | #' @param target_transform (callable, optional): A function/transform that takes in the 13 | #' target and transforms it. 14 | #' @param download (bool, optional): If true, downloads the dataset from the internet and 15 | #' puts it in root directory. If dataset is already downloaded, it is not 16 | #' downloaded again. 17 | #' 18 | #' @export 19 | cifar10_dataset <- torch::dataset( 20 | name = "cifar10_dataset", 21 | url = "https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz", 22 | md5 = "c32a1d4ab5d03f1284b67883e8d87530", 23 | fname = "cifar-10-batches-bin", 24 | type = 10, 25 | label_fname = "batches.meta.txt", 26 | initialize = function(root, train = TRUE, transform = NULL, target_transform = NULL, 27 | download = FALSE) { 28 | self$root <- root 29 | self$transform <- transform 30 | self$target_transform <- target_transform 31 | 32 | if (download) 33 | self$download() 34 | 35 | check <- self$check_files() 36 | 37 | if (!check) 38 | runtime_error("Files not found. Use download = TRUE") 39 | 40 | if (train) { 41 | files <- self$get_files()$train 42 | } else { 43 | files <- self$get_files()$test 44 | } 45 | 46 | batches <- lapply(files, function(x) read_batch(x, self$type)) 47 | 48 | if (self$type == 10) 49 | data <- combine_batches(batches) 50 | else 51 | data <- batches[[1]] 52 | 53 | self$.load_meta() 54 | 55 | self$x <- data$imgs 56 | self$y <- data$labels + 1L 57 | }, 58 | .load_meta = function() { 59 | cl <- readLines(fs::path(self$root, self$fname, self$label_fname)) 60 | self$class_to_idx <- setNames(seq_along(cl), cl) 61 | self$classes <- cl 62 | }, 63 | .getitem = function(i) { 64 | x <- self$x[i,,,] 65 | y <- self$y[i] 66 | 67 | if (!is.null(self$transform)) 68 | x <- self$transform(x) 69 | 70 | if (!is.null(self$target_transform)) 71 | y <- self$target_transform(y) 72 | 73 | list(x = x, y = y) 74 | }, 75 | .length = function() { 76 | length(self$y) 77 | }, 78 | download = function() { 79 | 80 | if(self$check_files()) 81 | return() 82 | 83 | p <- download_and_cache(self$url) 84 | 85 | if (!tools::md5sum(p) == self$md5) 86 | runtime_error("Corrupt file! Delete the file in {p} and try again.") 87 | 88 | utils::untar(p, exdir = self$root) 89 | }, 90 | check_files = function() { 91 | 92 | if (!fs::dir_exists(self$root)) 93 | return(FALSE) 94 | 95 | p <- fs::path(self$root, self$fname) 96 | if (!fs::dir_exists(p)) 97 | return(FALSE) 98 | 99 | f <- self$get_files() 100 | 101 | if (!length(f$train) == 5 && self$type == 10) 102 | return(FALSE) 103 | 104 | if (!length(f$train) == 1 && self$type == 100) 105 | return(FALSE) 106 | 107 | if (!length(f$test) == 1) 108 | return(FALSE) 109 | 110 | return(TRUE) 111 | }, 112 | get_files = function() { 113 | p <- fs::path(self$root, self$fname) 114 | 115 | if (self$type == 10) { 116 | list( 117 | train = fs::dir_ls(p, regexp = "data_batch"), 118 | test = fs::dir_ls(p, regexp = "test_batch") 119 | ) 120 | } else { 121 | list( 122 | train = fs::dir_ls(p, regexp = "train"), 123 | test = fs::dir_ls(p, regexp = "test") 124 | ) 125 | } 126 | } 127 | ) 128 | 129 | #' Cifar 100 dataset 130 | #' 131 | #' Downloads and prepares the CIFAR100 dataset. 132 | #' 133 | #' @rdname cifar10_dataset 134 | #' @export 135 | cifar100_dataset <- torch::dataset( 136 | name = "cifar100_dataset", 137 | inherit = cifar10_dataset, 138 | url = "https://www.cs.toronto.edu/~kriz/cifar-100-binary.tar.gz", 139 | md5 = "03b5dce01913d631647c71ecec9e9cb8", 140 | fname = "cifar-100-binary", 141 | type = 100, 142 | label_fname = "fine_label_names.txt" 143 | ) 144 | 145 | read_batch <- function(path, type = 10) { 146 | 147 | if (type == 10) 148 | n <- 10000 149 | else if (type == 100 && grepl("test", path)) 150 | n <- 10000 151 | else 152 | n <- 50000 153 | 154 | imgs <- array(dim = c(n, 32, 32, 3)) 155 | labels <- integer(length = n) 156 | if (type == 100) 157 | fine_labels <- integer(length = n) 158 | 159 | con <- file(path, open = "rb") 160 | on.exit({close(con)}, add = TRUE) 161 | 162 | for (i in seq_len(n)) { 163 | 164 | labels[i] <- readBin(con, integer(), size=1, n=1, endian="big") 165 | 166 | if (type == 100) { 167 | fine_labels[i] <- readBin(con, integer(), size=1, n=1, endian="big") 168 | } 169 | 170 | r <- as.integer(readBin(con, raw(), size=1, n=1024, endian="big")) 171 | g <- as.integer(readBin(con, raw(), size=1, n=1024, endian="big")) 172 | b <- as.integer(readBin(con, raw(), size=1, n=1024, endian="big")) 173 | 174 | imgs[i,,,1] <- matrix(r, ncol = 32, byrow = TRUE) 175 | imgs[i,,,2] <- matrix(g, ncol = 32, byrow = TRUE) 176 | imgs[i,,,3] <- matrix(b, ncol = 32, byrow = TRUE) 177 | } 178 | 179 | if (type == 100) 180 | list(imgs = imgs, labels = fine_labels) 181 | else 182 | list(imgs = imgs, labels = labels) 183 | } 184 | 185 | combine_batches <- function(batches) { 186 | 187 | n <- 10000 188 | 189 | imgs <- array(dim = c(length(batches)* n, 32, 32, 3)) 190 | labels <- integer(length = length(batches)* n) 191 | 192 | for (i in seq_along(batches)) { 193 | imgs[((i-1)*n + 1):(i*n),,,] <- batches[[i]]$imgs 194 | labels[((i-1)*n + 1):(i*n)] <- batches[[i]]$labels 195 | } 196 | 197 | list(imgs = imgs, labels = labels) 198 | } 199 | 200 | -------------------------------------------------------------------------------- /R/folder-dataset.R: -------------------------------------------------------------------------------- 1 | 2 | IMG_EXTENSIONS <- c('jpg', 'jpeg', 'png') # 'ppm', 'bmp', 'pgm', 'tif', 'tiff', 'webp' 3 | 4 | has_file_allowed_extension <- function(filename, extensions) { 5 | tolower(fs::path_ext(filename)) %in% tolower(extensions ) 6 | } 7 | 8 | is_image_file <- function(filename) { 9 | has_file_allowed_extension(filename, IMG_EXTENSIONS) 10 | } 11 | 12 | folder_make_dataset <- function(directory, class_to_idx, extensions = NULL, is_valid_file = NULL) { 13 | directory <- normalizePath(directory) 14 | 15 | both_none <- is.null(extensions) && is.null(is_valid_file) 16 | both_something <- !is.null(extensions) && ! is.null(is_valid_file) 17 | 18 | if (both_none || both_something) 19 | value_error("Both extensions and is_valid_file cannot be None or not None at the same time") 20 | 21 | if (!is.null(extensions)) { 22 | is_valid_file <- function(filename) { 23 | has_file_allowed_extension(filename, extensions) 24 | } 25 | } 26 | 27 | paths <- c() 28 | indexes <- c() 29 | 30 | for (target_class in sort(names(class_to_idx))) { 31 | 32 | class_index <- class_to_idx[target_class] 33 | target_dir <- fs::path_join(c(directory, target_class)) 34 | 35 | if (!fs::is_dir(target_dir)) 36 | next 37 | 38 | fnames <- fs::dir_ls(target_dir, recurse = TRUE) 39 | fnames <- fnames[is_valid_file(fnames)] 40 | 41 | paths <- c(paths, fnames) 42 | indexes <- c(indexes, rep(class_index, length(fnames))) 43 | } 44 | 45 | list( 46 | paths, 47 | indexes 48 | ) 49 | } 50 | 51 | folder_dataset <- torch::dataset( 52 | name = "folder", 53 | initialize = function(root, loader, extensions = NULL, transform = NULL, 54 | target_transform = NULL, is_valid_file = NULL) { 55 | 56 | self$root <- root 57 | self$transform <- transform 58 | self$target_transform <- target_transform 59 | 60 | class_to_idx <- self$.find_classes(root) 61 | samples <- folder_make_dataset(self$root, class_to_idx, extensions, is_valid_file) 62 | 63 | if (length(samples[[1]]) == 0) { 64 | 65 | msg <- glue::glue("Found 0 files in subfolders of {self$root}") 66 | if (!is.null(extensions)) { 67 | msg <- paste0(msg, glue::glue("\nSupported extensions are {paste(extensions, collapse=',')}")) 68 | } 69 | 70 | runtime_error(msg) 71 | } 72 | 73 | self$loader <- loader 74 | self$extensions <- extensions 75 | 76 | self$classes <- names(class_to_idx) 77 | self$class_to_idx <- class_to_idx 78 | self$samples <- samples 79 | self$targets <- samples[[2]] 80 | 81 | }, 82 | .find_classes = function(dir) { 83 | dirs <- fs::dir_ls(dir, recurse = FALSE, type = "directory") 84 | dirs <- sapply(fs::path_split(dirs), function(x) tail(x, 1)) 85 | class_too_idx <- seq_along(dirs) 86 | names(class_too_idx) <- sort(dirs) 87 | class_too_idx 88 | }, 89 | .getitem = function(index) { 90 | 91 | path <- self$samples[[1]][index] 92 | target <- self$samples[[2]][index] 93 | 94 | sample <- self$loader(path) 95 | 96 | if (!is.null(self$transform)) 97 | sample <- self$transform(sample) 98 | 99 | if (!is.null(self$target_transform)) 100 | target <- self$target_transform(target) 101 | 102 | list(x = sample, y = target) 103 | }, 104 | .length = function() { 105 | length(self$samples[[1]]) 106 | } 107 | ) 108 | 109 | #' Load an Image using ImageMagick 110 | #' 111 | #' Load an image located at `path` using the `{magick}` package. 112 | #' 113 | #' @param path path to the image to load from. 114 | #' 115 | #' @export 116 | magick_loader <- function(path) { 117 | magick::image_read(path) 118 | } 119 | 120 | 121 | #' Base loader 122 | #' 123 | #' Loads an image using `jpeg`, or `png` packages depending on the 124 | #' file extension. 125 | #' 126 | #' @param path path to the image to load from 127 | #' 128 | #' @export 129 | base_loader <- function(path) { 130 | 131 | ext <- tolower(fs::path_ext(path)) 132 | 133 | if (ext %in% c("jpg", "jpeg")) 134 | img <- jpeg::readJPEG(path) 135 | else if (ext %in% c("png")) 136 | img <- png::readPNG(path) 137 | else 138 | runtime_error("unknown extension {ext} in path {path}") 139 | 140 | if (length(dim(img)) == 2) 141 | img <- abind::abind(img, img, img, along = 3) 142 | else if (length(dim(img)) == 3 && dim(img)[1] == 1) 143 | img <- abind::abind(img, img, img, along = 1) 144 | 145 | img 146 | } 147 | 148 | 149 | #' Create an image folder dataset 150 | #' 151 | #' A generic data loader for images stored in folders. 152 | #' See `Details` for more information. 153 | #' 154 | #' @details This function assumes that the images for each class are contained 155 | #' in subdirectories of `root`. The names of these subdirectories are stored 156 | #' in the `classes` attribute of the returned object. 157 | #' 158 | #' An example folder structure might look as follows: 159 | #' 160 | #' ``` 161 | #' root/dog/xxx.png 162 | #' root/dog/xxy.png 163 | #' root/dog/xxz.png 164 | #' 165 | #' root/cat/123.png 166 | #' root/cat/nsdf3.png 167 | #' root/cat/asd932_.png 168 | #' ``` 169 | #' 170 | #' @param root Root directory path. 171 | #' @param loader A function to load an image given its path. 172 | #' @param transform A function/transform that takes in an PIL image and returns 173 | #' a transformed version. E.g, [transform_random_crop()]. 174 | #' @param target_transform A function/transform that takes in the target and 175 | #' transforms it. 176 | #' @param is_valid_file A function that takes path of an Image file and check if 177 | #' the file is a valid file (used to check of corrupt files) 178 | #' 179 | #' @family datasets 180 | #' 181 | #' @importFrom torch dataset 182 | #' @export 183 | image_folder_dataset <- dataset( 184 | "image_folder", 185 | inherit = folder_dataset, 186 | initialize = function(root, transform=NULL, target_transform=NULL, 187 | loader=NULL, is_valid_file=NULL) { 188 | 189 | if (is.null(loader)) 190 | loader <- base_loader 191 | 192 | if (!is.null(is_valid_file)) 193 | extensions <- NULL 194 | else 195 | extensions <- IMG_EXTENSIONS 196 | 197 | super$initialize(root, loader, extensions, transform=transform, 198 | target_transform=target_transform, 199 | is_valid_file=is_valid_file) 200 | self$imgs <- self$samples 201 | } 202 | ) 203 | -------------------------------------------------------------------------------- /R/globals.R: -------------------------------------------------------------------------------- 1 | utils::globalVariables(c("..")) 2 | -------------------------------------------------------------------------------- /R/models-alexnet.R: -------------------------------------------------------------------------------- 1 | 2 | alexnet <- torch::nn_module( 3 | "AlexNet", 4 | initialize = function(num_classes = 1000) { 5 | self$features <- torch::nn_sequential( 6 | torch::nn_conv2d(3, 64, kernel_size = 11, stride = 4, padding = 2), 7 | torch::nn_relu(inplace = TRUE), 8 | torch::nn_max_pool2d(kernel_size = 3, stride = 2), 9 | torch::nn_conv2d(64, 192, kernel_size = 5, padding = 2), 10 | torch::nn_relu(inplace = TRUE), 11 | torch::nn_max_pool2d(kernel_size = 3, stride = 2), 12 | torch::nn_conv2d(192, 384, kernel_size = 3, padding = 1), 13 | torch::nn_relu(inplace = TRUE), 14 | torch::nn_conv2d(384, 256, kernel_size = 3, padding = 1), 15 | torch::nn_relu(inplace = TRUE), 16 | torch::nn_conv2d(256, 256, kernel_size = 3, padding = 1), 17 | torch::nn_relu(inplace = TRUE), 18 | torch::nn_max_pool2d(kernel_size = 3, stride = 2) 19 | ) 20 | self$avgpool <- torch::nn_adaptive_avg_pool2d(c(6,6)) 21 | self$classifier <- torch::nn_sequential( 22 | torch::nn_dropout(), 23 | torch::nn_linear(256 * 6 * 6, 4096), 24 | torch::nn_relu(inplace = TRUE), 25 | torch::nn_dropout(), 26 | torch::nn_linear(4096, 4096), 27 | torch::nn_relu(inplace = TRUE), 28 | torch::nn_linear(4096, num_classes) 29 | ) 30 | }, 31 | forward = function(x) { 32 | x <- self$features(x) 33 | x <- self$avgpool(x) 34 | x <- torch::torch_flatten(x, start_dim = 2) 35 | x <- self$classifier(x) 36 | } 37 | ) 38 | 39 | #' AlexNet Model Architecture 40 | #' 41 | #' AlexNet model architecture from the 42 | #' [One weird trick...](https://arxiv.org/abs/1404.5997) paper. 43 | #' 44 | #' @param pretrained (bool): If TRUE, returns a model pre-trained on ImageNet. 45 | #' @param progress (bool): If TRUE, displays a progress bar of the download to 46 | #' stderr. 47 | #' @param ... other parameters passed to the model intializer. currently only 48 | #' `num_classes` is used. 49 | #' 50 | #' @family models 51 | #' 52 | #' @export 53 | model_alexnet <- function(pretrained = FALSE, progress = TRUE, ...) { 54 | 55 | model <- alexnet(...) 56 | 57 | if (pretrained) { 58 | state_dict_path <- download_and_cache( 59 | "https://torch-cdn.mlverse.org/models/vision/v1/models/alexnet.pth", 60 | ) 61 | state_dict <- torch::load_state_dict(state_dict_path) 62 | model$load_state_dict(state_dict) 63 | } 64 | 65 | model 66 | } 67 | -------------------------------------------------------------------------------- /R/models-vgg.R: -------------------------------------------------------------------------------- 1 | VGG <- torch::nn_module( 2 | "VGG", 3 | initialize = function(features, num_classes=1000, init_weights=TRUE) { 4 | self$features <- features 5 | self$avgpool <- torch::nn_adaptive_avg_pool2d(c(7,7)) 6 | self$classifier <- torch::nn_sequential( 7 | torch::nn_linear(512 * 7 * 7, 4096), 8 | torch::nn_relu(TRUE), 9 | torch::nn_dropout(), 10 | torch::nn_linear(4096, 4096), 11 | torch::nn_relu(TRUE), 12 | torch::nn_dropout(), 13 | torch::nn_linear(4096, num_classes), 14 | ) 15 | 16 | if (init_weights) 17 | self$.initialize_weights() 18 | 19 | }, 20 | forward = function(x) { 21 | x <- self$features(x) 22 | x <- self$avgpool(x) 23 | x <- torch::torch_flatten(x,start_dim = 2) 24 | x <- self$classifier(x) 25 | x 26 | }, 27 | .initialize_weights = function() { 28 | 29 | for (m in self$modules) { 30 | 31 | if (inherits(m, "nn_conv2d")) { 32 | torch::nn_init_kaiming_normal_(m$weight, mode = "fan_out", nonlinearity="relu") 33 | if (!is.null(m$bias)) 34 | torch::nn_init_constant_(m$bias, 0) 35 | } else if (inherits(m, "nn_batch_norm2d")) { 36 | torch::nn_init_constant_(m$weight, 1) 37 | torch::nn_init_constant_(m$bias, 0) 38 | } else if (inherits(m, "nn_linear")) { 39 | torch::nn_init_normal_(m$weight, 0, 0.01) 40 | torch::nn_init_constant_(m$bias, 0) 41 | } 42 | 43 | } 44 | 45 | } 46 | ) 47 | 48 | vgg_make_layers <- function(cfg, batch_norm) { 49 | layers <- list() 50 | in_channels <- 3 51 | for (v in cfg) { 52 | 53 | if (v == "M") { 54 | 55 | layers[[length(layers) + 1]] <- torch::nn_max_pool2d( 56 | kernel_size = 2, 57 | stride = 2 58 | ) 59 | 60 | } else { 61 | 62 | v <- as.integer(v) 63 | layers[[length(layers) + 1]] <- torch::nn_conv2d( 64 | in_channels = in_channels, out_channels = v, 65 | kernel_size = 3, padding = 1 66 | ) 67 | 68 | if (batch_norm) 69 | layers[[length(layers) + 1]] <- torch::nn_batch_norm2d(num_features = v) 70 | 71 | layers[[length(layers) + 1]] <- torch::nn_relu(inplace = TRUE) 72 | in_channels <- v 73 | 74 | } 75 | 76 | } 77 | torch::nn_sequential(!!!layers) 78 | } 79 | 80 | vgg_cfgs <- list( 81 | 'A' = list(64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'), 82 | 'B' = list(64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'), 83 | 'D' = list(64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'), 84 | 'E' = list(64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M') 85 | ) 86 | 87 | vgg_model_urls <- list( 88 | 'vgg11'= 'https://torch-cdn.mlverse.org/models/vision/v1/models/vgg11.pth', 89 | 'vgg13'= 'https://torch-cdn.mlverse.org/models/vision/v1/models/vgg13.pth', 90 | 'vgg16'= 'https://torch-cdn.mlverse.org/models/vision/v1/models/vgg16.pth', 91 | 'vgg19'= 'https://torch-cdn.mlverse.org/models/vision/v1/models/vgg19.pth', 92 | 'vgg11_bn'= 'https://torch-cdn.mlverse.org/models/vision/v1/models/vgg11_bn.pth', 93 | 'vgg13_bn'= 'https://torch-cdn.mlverse.org/models/vision/v1/models/vgg13_bn.pth', 94 | 'vgg16_bn'= 'https://torch-cdn.mlverse.org/models/vision/v1/models/vgg16_bn.pth', 95 | 'vgg19_bn'= 'https://torch-cdn.mlverse.org/models/vision/v1/models/vgg19_bn.pth' 96 | ) 97 | 98 | vgg <- function(arch, cfg, batch_norm, pretrained, progress, ...) { 99 | 100 | args <- rlang::list2(...) 101 | 102 | if (pretrained) 103 | args$init_weights <- FALSE 104 | 105 | layers <- vgg_make_layers(cfg = vgg_cfgs[[cfg]], batch_norm = batch_norm) 106 | args <- append(args, list(features = layers)) 107 | model <- do.call(VGG, args) 108 | 109 | if (pretrained) { 110 | 111 | state_dict_path <- download_and_cache(vgg_model_urls[[arch]]) 112 | state_dict <- torch::load_state_dict(state_dict_path) 113 | model$load_state_dict(state_dict) 114 | 115 | } 116 | 117 | model 118 | } 119 | 120 | #' VGG implementation 121 | #' 122 | #' 123 | #' VGG models implementations based on 124 | #' [Very Deep Convolutional Networks For Large-Scale Image Recognition](https://arxiv.org/pdf/1409.1556) 125 | #' 126 | #' @param pretrained (bool): If TRUE, returns a model pre-trained on ImageNet 127 | #' @param progress (bool): If TRUE, displays a progress bar of the download 128 | #' to stderr 129 | #' @param ... other parameters passed to the VGG model implementation. 130 | #' 131 | #' @name model_vgg 132 | #' @rdname model_vgg 133 | NULL 134 | 135 | #' @describeIn model_vgg VGG 11-layer model (configuration "A") 136 | #' 137 | #' @family models 138 | #' @export 139 | model_vgg11 <- function(pretrained = FALSE, progress = TRUE, ...) { 140 | vgg('vgg11', 'A', FALSE, pretrained, progress, ...) 141 | } 142 | 143 | #' @describeIn model_vgg VGG 11-layer model (configuration "A") with batch normalization 144 | #' @export 145 | model_vgg11_bn <- function(pretrained = FALSE, progress = TRUE, ...) { 146 | vgg("vgg11_bn", "A", TRUE, pretrained, progress, ...) 147 | } 148 | 149 | #' @describeIn model_vgg VGG 13-layer model (configuration "B") 150 | #' @export 151 | model_vgg13 <- function(pretrained = FALSE, progress = TRUE, ...) { 152 | vgg('vgg13', 'B', FALSE, pretrained, progress, ...) 153 | } 154 | 155 | #' @describeIn model_vgg VGG 13-layer model (configuration "B") with batch normalization 156 | #' @export 157 | model_vgg13_bn <- function(pretrained = FALSE, progress = TRUE, ...) { 158 | vgg("vgg13_bn", "B", TRUE, pretrained, progress, ...) 159 | } 160 | 161 | #' @describeIn model_vgg VGG 13-layer model (configuration "D") 162 | #' @export 163 | model_vgg16 <- function(pretrained = FALSE, progress = TRUE, ...) { 164 | vgg('vgg16', 'D', FALSE, pretrained, progress, ...) 165 | } 166 | 167 | #' @describeIn model_vgg VGG 13-layer model (configuration "D") with batch normalization 168 | #' @export 169 | model_vgg16_bn <- function(pretrained = FALSE, progress = TRUE, ...) { 170 | vgg("vgg16_bn", "D", TRUE, pretrained, progress, ...) 171 | } 172 | 173 | #' @describeIn model_vgg VGG 19-layer model (configuration "E") 174 | #' @export 175 | model_vgg19 <- function(pretrained = FALSE, progress = TRUE, ...) { 176 | vgg('vgg19', 'E', FALSE, pretrained, progress, ...) 177 | } 178 | 179 | #' @describeIn model_vgg VGG 19-layer model (configuration "E") with batch normalization 180 | #' @export 181 | model_vgg19_bn <- function(pretrained = FALSE, progress = TRUE, ...) { 182 | vgg("vgg19_bn", "E", TRUE, pretrained, progress, ...) 183 | } 184 | 185 | 186 | 187 | 188 | 189 | 190 | -------------------------------------------------------------------------------- /R/tiny-imagenet-dataset.R: -------------------------------------------------------------------------------- 1 | 2 | #' Tiny ImageNet dataset 3 | #' 4 | #' Prepares the Tiny ImageNet dataset and optionally downloads it. 5 | #' 6 | #' @param root directory path to download the dataset. 7 | #' @param split dataset split, `train`, `validation` or `test`. 8 | #' @param download whether to download or not the dataset. 9 | #' @param ... other arguments passed to [image_folder_dataset()]. 10 | #' 11 | #' @family dataset 12 | #' 13 | #' @export 14 | tiny_imagenet_dataset <- torch::dataset( 15 | "tiny_imagenet", 16 | inherit = image_folder_dataset, 17 | tar_name = "tiny-imagenet-200", 18 | url = "http://cs231n.stanford.edu/tiny-imagenet-200.zip", 19 | initialize = function(root, split='train', download = FALSE, ...) { 20 | 21 | root <- normalizePath(root, mustWork = FALSE) 22 | 23 | if (!fs::dir_exists(root)) 24 | fs::dir_create(root) 25 | 26 | self$root_path <- root 27 | 28 | if (download) 29 | self$download() 30 | 31 | super$initialize(root = fs::path_join(c(root, self$tar_name, split)), ...) 32 | 33 | }, 34 | download = function() { 35 | 36 | p <- fs::path_join(c(self$root_path, self$tar_name)) 37 | 38 | if (fs::dir_exists(p)) 39 | return(NULL) 40 | 41 | raw_path <- fs::path_join(c(self$root_path, "tiny-imagenet-200.zip")) 42 | 43 | rlang::inform("Downloading tiny imagenet dataset!") 44 | 45 | p <- download_and_cache(self$url) 46 | fs::file_copy(p, raw_path) 47 | 48 | rlang::inform("Download complete. Now unzipping.") 49 | 50 | utils::unzip(raw_path, exdir = self$root_path) 51 | 52 | # organize validation images 53 | val_path <- fs::path_join(c(self$root_path, self$tar_name, "val")) 54 | val_images <- read.table(fs::path_join(c(val_path, "val_annotations.txt"))) 55 | 56 | fs::dir_create( 57 | fs::path(val_path, unique(val_images$V2)) 58 | ) 59 | 60 | fs::file_move( 61 | fs::path(val_path, "images", val_images$V1), 62 | fs::path(val_path, val_images$V2, val_images$V1) 63 | ) 64 | 65 | fs::dir_delete(fs::path(val_path, "images")) 66 | 67 | rlang::inform("Done!") 68 | 69 | } 70 | ) 71 | -------------------------------------------------------------------------------- /R/transforms-array.R: -------------------------------------------------------------------------------- 1 | 2 | #' @export 3 | transform_to_tensor.array <- function(img) { 4 | 5 | if (length(dim(img)) == 2) 6 | dim(img) <- c(dim(img), 1) 7 | 8 | res <- torch::torch_tensor(img)$permute(c(3, 1, 2)) 9 | 10 | if (res$dtype == torch::torch_long()) 11 | res <- res/255 12 | 13 | res 14 | } 15 | 16 | #' @export 17 | transform_to_tensor.matrix <- transform_to_tensor.array 18 | -------------------------------------------------------------------------------- /R/transforms-magick.R: -------------------------------------------------------------------------------- 1 | #' @export 2 | `transform_to_tensor.magick-image` <- function(img) { 3 | img <- as.integer(magick::image_data(img, channels = "rgb")) 4 | img <- torch::torch_tensor(img)$permute(c(3,1,2)) 5 | img <- img$to(dtype = torch::torch_float32()) 6 | img <- img$contiguous() 7 | img <- img$div(255) 8 | 9 | img 10 | } 11 | 12 | #' @export 13 | `transform_resize.magick-image` <- function(img, size, interpolation = 2) { 14 | 15 | interpolation_modes <- c( 16 | "0" = "Pint", # nearest, 17 | "2" = "Triangle", # bilinear 18 | "3" = "Catrom" # bicubic 19 | ) 20 | 21 | if (is.numeric(interpolation)) 22 | interpolation <- interpolation_modes[names(interpolation_modes) == interpolation] 23 | 24 | if (length(size) == 1) { 25 | 26 | w <- magick::image_info(img)$width 27 | h <- magick::image_info(img)$height 28 | 29 | if (w < h) 30 | size <- paste0(size, "x") 31 | else 32 | size <- paste0("x", size) 33 | 34 | } else { 35 | size <- paste0(paste0(size, collapse = "x"), "!") 36 | } 37 | 38 | magick::image_resize(img, geometry = size, filter = interpolation) 39 | } 40 | 41 | #' @export 42 | `transform_crop.magick-image` <- function(img, top, left, height, width) { 43 | magick::image_crop( 44 | img, 45 | paste0(height, "x", width, "+", left, "+", top) 46 | ) 47 | } 48 | 49 | #' @export 50 | `transform_hflip.magick-image` <- function(img) { 51 | magick::image_flip(img) 52 | } 53 | 54 | # Utils ------------------------------------------------------------------- 55 | 56 | `get_image_size.magick-image` <- function(img) { 57 | info <- magick::image_info(img) 58 | c(info$width, info$height) 59 | } 60 | 61 | -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | 2 | download_and_cache <- function(url, redownload = FALSE, prefix = NULL) { 3 | 4 | cache_path <- rappdirs::user_cache_dir("torch") 5 | 6 | fs::dir_create(cache_path) 7 | if (!is.null(prefix)) { 8 | cache_path <- file.path(cache_path, prefix) 9 | } 10 | try(fs::dir_create(cache_path, recurse = TRUE), silent = TRUE) 11 | path <- file.path(cache_path, fs::path_file(url)) 12 | 13 | if (!file.exists(path) || redownload) { 14 | # we should first download to a temporary file because 15 | # download probalems could cause hard to debug errors. 16 | tmp <- tempfile(fileext = fs::path_ext(path)) 17 | on.exit({try({fs::file_delete(tmp)}, silent = TRUE)}, add = TRUE) 18 | 19 | withr::with_options( 20 | list(timeout = max(600, getOption("timeout", default = 0))), 21 | utils::download.file(url, tmp, mode = "wb") 22 | ) 23 | fs::file_move(tmp, path) 24 | } 25 | 26 | path 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # torchvision 3 | 4 | 5 | [![R build status](https://github.com/mlverse/torchvision/workflows/R-CMD-check/badge.svg)](https://github.com/mlverse/torchvision/actions) 6 | [![CRAN status](https://www.r-pkg.org/badges/version/torchvision)](https://CRAN.R-project.org/package=torchvision) 7 | [![](https://cranlogs.r-pkg.org/badges/torchvision)](https://cran.r-project.org/package=torchvision) 8 | 9 | 10 | torchvision is an extension for [torch](https://github.com/mlverse/torch) providing image loading, transformations, common architectures for computer vision, pre-trained weights and access to commonly used datasets. 11 | 12 | ## Installation 13 | 14 | The CRAN release can be installed with: 15 | 16 | ```r 17 | install.packages("torchvision") 18 | ``` 19 | 20 | You can install the development version from GitHub with: 21 | 22 | ``` r 23 | remotes::install_github("mlverse/torchvision@main") 24 | ``` 25 | 26 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | template: 2 | params: 3 | bootswatch: united 4 | ganalytics: 'G-HD24T4Z9Z3' 5 | 6 | development: 7 | mode: auto 8 | 9 | navbar: 10 | structure: 11 | left: [home, examples, reference, news] 12 | right: [github] 13 | components: 14 | articles: ~ 15 | examples: 16 | text: Examples 17 | menu: 18 | - text: mnist-mlp 19 | href: articles/examples/mnist-mlp.html 20 | - text: mnist-cnn 21 | href: articles/examples/mnist-cnn.html 22 | - text: mnist-dcgan 23 | href: articles/examples/mnist-dcgan.html 24 | - text: tinyimagenet-alexnet 25 | href: articles/examples/tinyimagenet-alexnet.html 26 | - text: style-transfer 27 | href: articles/examples/style-transfer.html 28 | - text: texture-nca 29 | href: articles/examples/texture-nca.html 30 | 31 | reference: 32 | - title: Transforms 33 | desc: Image transformation functions 34 | contents: 35 | - starts_with("transform_") 36 | - title: Models 37 | desc: Model architectures 38 | contents: 39 | - starts_with("model_") 40 | - title: Datasets 41 | desc: Datasets readily available 42 | contents: 43 | - ends_with("_dataset") 44 | - title: Displaying 45 | desc: Show images 46 | contents: 47 | - draw_bounding_boxes 48 | - draw_keypoints 49 | - draw_segmentation_masks 50 | - tensor_image_browse 51 | - tensor_image_display 52 | - title: Misc 53 | contents: 54 | - magick_loader 55 | - base_loader 56 | - vision_make_grid 57 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | Re-submission to fix function call removed from dependency. 2 | -------------------------------------------------------------------------------- /inst/po/fr/LC_MESSAGES/R-torchvision.mo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/inst/po/fr/LC_MESSAGES/R-torchvision.mo -------------------------------------------------------------------------------- /man/base_loader.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/folder-dataset.R 3 | \name{base_loader} 4 | \alias{base_loader} 5 | \title{Base loader} 6 | \usage{ 7 | base_loader(path) 8 | } 9 | \arguments{ 10 | \item{path}{path to the image to load from} 11 | } 12 | \description{ 13 | Loads an image using \code{jpeg}, or \code{png} packages depending on the 14 | file extension. 15 | } 16 | -------------------------------------------------------------------------------- /man/cifar10_dataset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dataset-cifar.R 3 | \name{cifar10_dataset} 4 | \alias{cifar10_dataset} 5 | \alias{cifar100_dataset} 6 | \title{Cifar datasets} 7 | \usage{ 8 | cifar10_dataset( 9 | root, 10 | train = TRUE, 11 | transform = NULL, 12 | target_transform = NULL, 13 | download = FALSE 14 | ) 15 | 16 | cifar100_dataset( 17 | root, 18 | train = TRUE, 19 | transform = NULL, 20 | target_transform = NULL, 21 | download = FALSE 22 | ) 23 | } 24 | \arguments{ 25 | \item{root}{(string): Root directory of dataset where directory 26 | \code{cifar-10-batches-bin} exists or will be saved to if download is set to TRUE.} 27 | 28 | \item{train}{(bool, optional): If TRUE, creates dataset from training set, otherwise 29 | creates from test set.} 30 | 31 | \item{transform}{(callable, optional): A function/transform that takes in an PIL image 32 | and returns a transformed version. E.g, \code{\link[=transform_random_crop]{transform_random_crop()}}} 33 | 34 | \item{target_transform}{(callable, optional): A function/transform that takes in the 35 | target and transforms it.} 36 | 37 | \item{download}{(bool, optional): If true, downloads the dataset from the internet and 38 | puts it in root directory. If dataset is already downloaded, it is not 39 | downloaded again.} 40 | } 41 | \description{ 42 | \href{https://www.cs.toronto.edu/~kriz/cifar.html}{CIFAR10} Dataset. 43 | 44 | Downloads and prepares the CIFAR100 dataset. 45 | } 46 | -------------------------------------------------------------------------------- /man/draw_bounding_boxes.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/vision_utils.R 3 | \name{draw_bounding_boxes} 4 | \alias{draw_bounding_boxes} 5 | \title{Draws bounding boxes on image.} 6 | \usage{ 7 | draw_bounding_boxes( 8 | image, 9 | boxes, 10 | labels = NULL, 11 | colors = NULL, 12 | fill = FALSE, 13 | width = 1, 14 | font = c("serif", "plain"), 15 | font_size = 10 16 | ) 17 | } 18 | \arguments{ 19 | \item{image}{: Tensor of shape (C x H x W) and dtype uint8.} 20 | 21 | \item{boxes}{: Tensor of size (N, 4) containing bounding boxes in (xmin, ymin, xmax, ymax) format. Note that 22 | the boxes are absolute coordinates with respect to the image. In other words: \verb{0 = xmin < xmax < W} and 23 | \verb{0 = ymin < ymax < H}.} 24 | 25 | \item{labels}{: character vector containing the labels of bounding boxes.} 26 | 27 | \item{colors}{: character vector containing the colors 28 | of the boxes or single color for all boxes. The color can be represented as 29 | strings e.g. "red" or "#FF00FF". By default, viridis colors are generated for boxes.} 30 | 31 | \item{fill}{: If \code{TRUE} fills the bounding box with specified color.} 32 | 33 | \item{width}{: Width of text shift to the bounding box.} 34 | 35 | \item{font}{: NULL for the current font family, or a character vector of length 2 for Hershey vector fonts.} 36 | 37 | \item{font_size}{: The requested font size in points.} 38 | } 39 | \value{ 40 | torch_tensor of size (C, H, W) of dtype uint8: Image Tensor with bounding boxes plotted. 41 | } 42 | \description{ 43 | Draws bounding boxes on top of one image tensor 44 | } 45 | \examples{ 46 | if (torch::torch_is_installed()) { 47 | \dontrun{ 48 | image <- torch::torch_randint(170, 250, size = c(3, 360, 360))$to(torch::torch_uint8()) 49 | x <- torch::torch_randint(low = 1, high = 160, size = c(12,1)) 50 | y <- torch::torch_randint(low = 1, high = 260, size = c(12,1)) 51 | boxes <- torch::torch_cat(c(x, y, x + 20, y + 10), dim = 2) 52 | bboxed <- draw_bounding_boxes(image, boxes, colors = "black", fill = TRUE) 53 | tensor_image_browse(bboxed) 54 | } 55 | } 56 | } 57 | \seealso{ 58 | Other image display: 59 | \code{\link{draw_keypoints}()}, 60 | \code{\link{draw_segmentation_masks}()}, 61 | \code{\link{tensor_image_browse}()}, 62 | \code{\link{tensor_image_display}()}, 63 | \code{\link{vision_make_grid}()} 64 | } 65 | \concept{image display} 66 | -------------------------------------------------------------------------------- /man/draw_keypoints.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/vision_utils.R 3 | \name{draw_keypoints} 4 | \alias{draw_keypoints} 5 | \title{Draws Keypoints} 6 | \usage{ 7 | draw_keypoints( 8 | image, 9 | keypoints, 10 | connectivity = NULL, 11 | colors = NULL, 12 | radius = 2, 13 | width = 3 14 | ) 15 | } 16 | \arguments{ 17 | \item{image}{: Tensor of shape (3, H, W) and dtype uint8} 18 | 19 | \item{keypoints}{: Tensor of shape (N, K, 2) the K keypoints location for each of the N detected poses instance,} 20 | 21 | \item{connectivity}{: Vector of pair of keypoints to be connected (currently unavailable)} 22 | 23 | \item{colors}{: character vector containing the colors 24 | of the boxes or single color for all boxes. The color can be represented as 25 | strings e.g. "red" or "#FF00FF". By default, viridis colors are generated for keypoints} 26 | 27 | \item{radius}{: radius of the plotted keypoint.} 28 | 29 | \item{width}{: width of line connecting keypoints.} 30 | } 31 | \value{ 32 | Image Tensor of dtype uint8 with keypoints drawn. 33 | } 34 | \description{ 35 | Draws Keypoints, an object describing a body part (like rightArm or leftShoulder), on given RGB tensor image. 36 | } 37 | \examples{ 38 | if (torch::torch_is_installed()) { 39 | \dontrun{ 40 | image <- torch::torch_randint(190, 255, size = c(3, 360, 360))$to(torch::torch_uint8()) 41 | keypoints <- torch::torch_randint(low = 60, high = 300, size = c(4, 5, 2)) 42 | keypoint_image <- draw_keypoints(image, keypoints) 43 | tensor_image_browse(keypoint_image) 44 | } 45 | } 46 | } 47 | \seealso{ 48 | Other image display: 49 | \code{\link{draw_bounding_boxes}()}, 50 | \code{\link{draw_segmentation_masks}()}, 51 | \code{\link{tensor_image_browse}()}, 52 | \code{\link{tensor_image_display}()}, 53 | \code{\link{vision_make_grid}()} 54 | } 55 | \concept{image display} 56 | -------------------------------------------------------------------------------- /man/draw_segmentation_masks.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/vision_utils.R 3 | \name{draw_segmentation_masks} 4 | \alias{draw_segmentation_masks} 5 | \title{Draw segmentation masks} 6 | \usage{ 7 | draw_segmentation_masks(image, masks, alpha = 0.8, colors = NULL) 8 | } 9 | \arguments{ 10 | \item{image}{: torch_tensor of shape (3, H, W) and dtype uint8.} 11 | 12 | \item{masks}{: torch_tensor of shape (num_masks, H, W) or (H, W) and dtype bool.} 13 | 14 | \item{alpha}{: number between 0 and 1 denoting the transparency of the masks.} 15 | 16 | \item{colors}{: character vector containing the colors 17 | of the boxes or single color for all boxes. The color can be represented as 18 | strings e.g. "red" or "#FF00FF". By default, viridis colors are generated for masks} 19 | } 20 | \value{ 21 | torch_tensor of shape (3, H, W) and dtype uint8 of the image with segmentation masks drawn on top. 22 | } 23 | \description{ 24 | Draw segmentation masks with their respective colors on top of a given RGB tensor image 25 | } 26 | \examples{ 27 | if (torch::torch_is_installed()) { 28 | image <- torch::torch_randint(170, 250, size = c(3, 360, 360))$to(torch::torch_uint8()) 29 | mask <- torch::torch_tril(torch::torch_ones(c(360, 360)))$to(torch::torch_bool()) 30 | masked_image <- draw_segmentation_masks(image, mask, alpha = 0.2) 31 | tensor_image_browse(masked_image) 32 | } 33 | } 34 | \seealso{ 35 | Other image display: 36 | \code{\link{draw_bounding_boxes}()}, 37 | \code{\link{draw_keypoints}()}, 38 | \code{\link{tensor_image_browse}()}, 39 | \code{\link{tensor_image_display}()}, 40 | \code{\link{vision_make_grid}()} 41 | } 42 | \concept{image display} 43 | -------------------------------------------------------------------------------- /man/fashion_mnist_dataset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dataset-mnist.R 3 | \name{fashion_mnist_dataset} 4 | \alias{fashion_mnist_dataset} 5 | \title{Fashion-MNIST dataset} 6 | \usage{ 7 | fashion_mnist_dataset( 8 | root, 9 | train = TRUE, 10 | transform = NULL, 11 | target_transform = NULL, 12 | download = FALSE 13 | ) 14 | } 15 | \arguments{ 16 | \item{root}{(string): Root directory of dataset where 17 | \code{FashionMNIST/processed/training.pt} and \code{FashionMNIST/processed/test.pt} exist.} 18 | 19 | \item{train}{(bool, optional): If TRUE, creates dataset from \code{training.pt}, 20 | otherwise from \code{test.pt}.} 21 | 22 | \item{transform}{(callable, optional): A function/transform that takes in an 23 | image and returns a transformed version. E.g., \code{\link[=transform_random_crop]{transform_random_crop()}}.} 24 | 25 | \item{target_transform}{(callable, optional): A function/transform that takes 26 | in the target and transforms it.} 27 | 28 | \item{download}{(bool, optional): If TRUE, downloads the dataset from the 29 | internet and puts it in root directory. If dataset is already downloaded, 30 | it is not downloaded again.} 31 | } 32 | \description{ 33 | Prepares the \href{https://github.com/zalandoresearch/fashion-mnist}{Fashion-MNIST} dataset 34 | and optionally downloads it. 35 | } 36 | \details{ 37 | Fashion-MNIST dataset 38 | } 39 | \seealso{ 40 | \code{\link[=mnist_dataset]{mnist_dataset()}}, \code{\link[=kmnist_dataset]{kmnist_dataset()}} 41 | } 42 | -------------------------------------------------------------------------------- /man/image_folder_dataset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/folder-dataset.R 3 | \name{image_folder_dataset} 4 | \alias{image_folder_dataset} 5 | \title{Create an image folder dataset} 6 | \usage{ 7 | image_folder_dataset( 8 | root, 9 | transform = NULL, 10 | target_transform = NULL, 11 | loader = NULL, 12 | is_valid_file = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{root}{Root directory path.} 17 | 18 | \item{transform}{A function/transform that takes in an PIL image and returns 19 | a transformed version. E.g, \code{\link[=transform_random_crop]{transform_random_crop()}}.} 20 | 21 | \item{target_transform}{A function/transform that takes in the target and 22 | transforms it.} 23 | 24 | \item{loader}{A function to load an image given its path.} 25 | 26 | \item{is_valid_file}{A function that takes path of an Image file and check if 27 | the file is a valid file (used to check of corrupt files)} 28 | } 29 | \description{ 30 | A generic data loader for images stored in folders. 31 | See \code{Details} for more information. 32 | } 33 | \details{ 34 | This function assumes that the images for each class are contained 35 | in subdirectories of \code{root}. The names of these subdirectories are stored 36 | in the \code{classes} attribute of the returned object. 37 | 38 | An example folder structure might look as follows: 39 | 40 | \if{html}{\out{
}}\preformatted{root/dog/xxx.png 41 | root/dog/xxy.png 42 | root/dog/xxz.png 43 | 44 | root/cat/123.png 45 | root/cat/nsdf3.png 46 | root/cat/asd932_.png 47 | }\if{html}{\out{
}} 48 | } 49 | \concept{datasets} 50 | -------------------------------------------------------------------------------- /man/kmnist_dataset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dataset-mnist.R 3 | \name{kmnist_dataset} 4 | \alias{kmnist_dataset} 5 | \title{Kuzushiji-MNIST} 6 | \usage{ 7 | kmnist_dataset( 8 | root, 9 | train = TRUE, 10 | transform = NULL, 11 | target_transform = NULL, 12 | download = FALSE 13 | ) 14 | } 15 | \arguments{ 16 | \item{root}{(string): Root directory of dataset where 17 | \code{KMNIST/processed/training.pt} and \code{KMNIST/processed/test.pt} exist.} 18 | 19 | \item{train}{(bool, optional): If TRUE, creates dataset from \code{training.pt}, 20 | otherwise from \code{test.pt}.} 21 | 22 | \item{transform}{(callable, optional): A function/transform that takes in an 23 | PIL image and returns a transformed version. E.g, \code{\link[=transform_random_crop]{transform_random_crop()}}.} 24 | 25 | \item{target_transform}{(callable, optional): A function/transform that takes 26 | in the target and transforms it.} 27 | 28 | \item{download}{(bool, optional): If true, downloads the dataset from the 29 | internet and puts it in root directory. If dataset is already downloaded, 30 | it is not downloaded again.} 31 | } 32 | \description{ 33 | Prepares the \href{https://github.com/rois-codh/kmnist}{Kuzushiji-MNIST} dataset 34 | and optionally downloads it. 35 | } 36 | -------------------------------------------------------------------------------- /man/magick_loader.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/folder-dataset.R 3 | \name{magick_loader} 4 | \alias{magick_loader} 5 | \title{Load an Image using ImageMagick} 6 | \usage{ 7 | magick_loader(path) 8 | } 9 | \arguments{ 10 | \item{path}{path to the image to load from.} 11 | } 12 | \description{ 13 | Load an image located at \code{path} using the \code{{magick}} package. 14 | } 15 | -------------------------------------------------------------------------------- /man/mnist_dataset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dataset-mnist.R 3 | \name{mnist_dataset} 4 | \alias{mnist_dataset} 5 | \title{MNIST dataset} 6 | \usage{ 7 | mnist_dataset( 8 | root, 9 | train = TRUE, 10 | transform = NULL, 11 | target_transform = NULL, 12 | download = FALSE 13 | ) 14 | } 15 | \arguments{ 16 | \item{root}{(string): Root directory of dataset where 17 | \code{MNIST/processed/training.pt} and \code{MNIST/processed/test.pt} exist.} 18 | 19 | \item{train}{(bool, optional): If True, creates dataset from 20 | \code{training.pt}, otherwise from \code{test.pt}.} 21 | 22 | \item{transform}{(callable, optional): A function/transform that takes in an 23 | PIL image and returns a transformed version. E.g, 24 | \code{\link[=transform_random_crop]{transform_random_crop()}}.} 25 | 26 | \item{target_transform}{(callable, optional): A function/transform that takes 27 | in the target and transforms it.} 28 | 29 | \item{download}{(bool, optional): If true, downloads the dataset from the 30 | internet and puts it in root directory. If dataset is already downloaded, 31 | it is not downloaded again.} 32 | } 33 | \description{ 34 | Prepares the MNIST dataset and optionally downloads it. 35 | } 36 | -------------------------------------------------------------------------------- /man/model_alexnet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/models-alexnet.R 3 | \name{model_alexnet} 4 | \alias{model_alexnet} 5 | \title{AlexNet Model Architecture} 6 | \usage{ 7 | model_alexnet(pretrained = FALSE, progress = TRUE, ...) 8 | } 9 | \arguments{ 10 | \item{pretrained}{(bool): If TRUE, returns a model pre-trained on ImageNet.} 11 | 12 | \item{progress}{(bool): If TRUE, displays a progress bar of the download to 13 | stderr.} 14 | 15 | \item{...}{other parameters passed to the model intializer. currently only 16 | \code{num_classes} is used.} 17 | } 18 | \description{ 19 | AlexNet model architecture from the 20 | \href{https://arxiv.org/abs/1404.5997}{One weird trick...} paper. 21 | } 22 | \seealso{ 23 | Other models: 24 | \code{\link{model_inception_v3}()}, 25 | \code{\link{model_mobilenet_v2}()}, 26 | \code{\link{model_resnet}}, 27 | \code{\link{model_vgg}} 28 | } 29 | \concept{models} 30 | -------------------------------------------------------------------------------- /man/model_inception_v3.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/models-inception.R 3 | \name{model_inception_v3} 4 | \alias{model_inception_v3} 5 | \title{Inception v3 model} 6 | \usage{ 7 | model_inception_v3(pretrained = FALSE, progress = TRUE, ...) 8 | } 9 | \arguments{ 10 | \item{pretrained}{(bool): If \code{TRUE}, returns a model pre-trained on ImageNet} 11 | 12 | \item{progress}{(bool): If \code{TRUE}, displays a progress bar of the download to stderr} 13 | 14 | \item{...}{Used to pass keyword arguments to the Inception module: 15 | \itemize{ 16 | \item aux_logits (bool): If \code{TRUE}, add an auxiliary branch that can improve training. 17 | Default: \emph{TRUE} 18 | \item transform_input (bool): If \code{TRUE}, preprocess the input according to the method with which it 19 | was trained on ImageNet. Default: \emph{FALSE} 20 | }} 21 | } 22 | \description{ 23 | Architecture from \href{https://arxiv.org/abs/1512.00567}{Rethinking the Inception Architecture for Computer Vision} 24 | The required minimum input size of the model is 75x75. 25 | } 26 | \note{ 27 | \strong{Important}: In contrast to the other models the inception_v3 expects tensors with a size of 28 | N x 3 x 299 x 299, so ensure your images are sized accordingly. 29 | } 30 | \seealso{ 31 | Other models: 32 | \code{\link{model_alexnet}()}, 33 | \code{\link{model_mobilenet_v2}()}, 34 | \code{\link{model_resnet}}, 35 | \code{\link{model_vgg}} 36 | } 37 | \concept{models} 38 | -------------------------------------------------------------------------------- /man/model_mobilenet_v2.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/models-mobilenetv2.R 3 | \name{model_mobilenet_v2} 4 | \alias{model_mobilenet_v2} 5 | \title{Constructs a MobileNetV2 architecture from 6 | \href{https://arxiv.org/abs/1801.04381}{MobileNetV2: Inverted Residuals and Linear Bottlenecks}.} 7 | \usage{ 8 | model_mobilenet_v2(pretrained = FALSE, progress = TRUE, ...) 9 | } 10 | \arguments{ 11 | \item{pretrained}{(bool): If TRUE, returns a model pre-trained on ImageNet.} 12 | 13 | \item{progress}{(bool): If TRUE, displays a progress bar of the download to 14 | stderr.} 15 | 16 | \item{...}{Other parameters passed to the model implementation.} 17 | } 18 | \description{ 19 | Constructs a MobileNetV2 architecture from 20 | \href{https://arxiv.org/abs/1801.04381}{MobileNetV2: Inverted Residuals and Linear Bottlenecks}. 21 | } 22 | \seealso{ 23 | Other models: 24 | \code{\link{model_alexnet}()}, 25 | \code{\link{model_inception_v3}()}, 26 | \code{\link{model_resnet}}, 27 | \code{\link{model_vgg}} 28 | } 29 | \concept{models} 30 | -------------------------------------------------------------------------------- /man/model_resnet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/models-resnet.R 3 | \name{model_resnet} 4 | \alias{model_resnet} 5 | \alias{model_resnet18} 6 | \alias{model_resnet34} 7 | \alias{model_resnet50} 8 | \alias{model_resnet101} 9 | \alias{model_resnet152} 10 | \alias{model_resnext50_32x4d} 11 | \alias{model_resnext101_32x8d} 12 | \alias{model_wide_resnet50_2} 13 | \alias{model_wide_resnet101_2} 14 | \title{ResNet implementation} 15 | \usage{ 16 | model_resnet18(pretrained = FALSE, progress = TRUE, ...) 17 | 18 | model_resnet34(pretrained = FALSE, progress = TRUE, ...) 19 | 20 | model_resnet50(pretrained = FALSE, progress = TRUE, ...) 21 | 22 | model_resnet101(pretrained = FALSE, progress = TRUE, ...) 23 | 24 | model_resnet152(pretrained = FALSE, progress = TRUE, ...) 25 | 26 | model_resnext50_32x4d(pretrained = FALSE, progress = TRUE, ...) 27 | 28 | model_resnext101_32x8d(pretrained = FALSE, progress = TRUE, ...) 29 | 30 | model_wide_resnet50_2(pretrained = FALSE, progress = TRUE, ...) 31 | 32 | model_wide_resnet101_2(pretrained = FALSE, progress = TRUE, ...) 33 | } 34 | \arguments{ 35 | \item{pretrained}{(bool): If TRUE, returns a model pre-trained on ImageNet.} 36 | 37 | \item{progress}{(bool): If TRUE, displays a progress bar of the download to 38 | stderr.} 39 | 40 | \item{...}{Other parameters passed to the resnet model.} 41 | } 42 | \description{ 43 | ResNet models implementation from 44 | \href{https://arxiv.org/pdf/1512.03385}{Deep Residual Learning for Image Recognition} and later 45 | related papers (see Functions) 46 | } 47 | \section{Functions}{ 48 | \itemize{ 49 | \item \code{model_resnet18()}: ResNet 18-layer model 50 | 51 | \item \code{model_resnet34()}: ResNet 34-layer model 52 | 53 | \item \code{model_resnet50()}: ResNet 50-layer model 54 | 55 | \item \code{model_resnet101()}: ResNet 101-layer model 56 | 57 | \item \code{model_resnet152()}: ResNet 152-layer model 58 | 59 | \item \code{model_resnext50_32x4d()}: ResNeXt-50 32x4d model from \href{https://arxiv.org/pdf/1611.05431}{"Aggregated Residual Transformation for Deep Neural Networks"} 60 | with 32 groups having each a width of 4. 61 | 62 | \item \code{model_resnext101_32x8d()}: ResNeXt-101 32x8d model from \href{https://arxiv.org/pdf/1611.05431}{"Aggregated Residual Transformation for Deep Neural Networks"} 63 | with 32 groups having each a width of 8. 64 | 65 | \item \code{model_wide_resnet50_2()}: Wide ResNet-50-2 model from \href{https://arxiv.org/pdf/1605.07146}{"Wide Residual Networks"} 66 | with width per group of 128. 67 | 68 | \item \code{model_wide_resnet101_2()}: Wide ResNet-101-2 model from \href{https://arxiv.org/pdf/1605.07146}{"Wide Residual Networks"} 69 | with width per group of 128. 70 | 71 | }} 72 | \seealso{ 73 | Other models: 74 | \code{\link{model_alexnet}()}, 75 | \code{\link{model_inception_v3}()}, 76 | \code{\link{model_mobilenet_v2}()}, 77 | \code{\link{model_vgg}} 78 | } 79 | \concept{models} 80 | -------------------------------------------------------------------------------- /man/model_vgg.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/models-vgg.R 3 | \name{model_vgg} 4 | \alias{model_vgg} 5 | \alias{model_vgg11} 6 | \alias{model_vgg11_bn} 7 | \alias{model_vgg13} 8 | \alias{model_vgg13_bn} 9 | \alias{model_vgg16} 10 | \alias{model_vgg16_bn} 11 | \alias{model_vgg19} 12 | \alias{model_vgg19_bn} 13 | \title{VGG implementation} 14 | \usage{ 15 | model_vgg11(pretrained = FALSE, progress = TRUE, ...) 16 | 17 | model_vgg11_bn(pretrained = FALSE, progress = TRUE, ...) 18 | 19 | model_vgg13(pretrained = FALSE, progress = TRUE, ...) 20 | 21 | model_vgg13_bn(pretrained = FALSE, progress = TRUE, ...) 22 | 23 | model_vgg16(pretrained = FALSE, progress = TRUE, ...) 24 | 25 | model_vgg16_bn(pretrained = FALSE, progress = TRUE, ...) 26 | 27 | model_vgg19(pretrained = FALSE, progress = TRUE, ...) 28 | 29 | model_vgg19_bn(pretrained = FALSE, progress = TRUE, ...) 30 | } 31 | \arguments{ 32 | \item{pretrained}{(bool): If TRUE, returns a model pre-trained on ImageNet} 33 | 34 | \item{progress}{(bool): If TRUE, displays a progress bar of the download 35 | to stderr} 36 | 37 | \item{...}{other parameters passed to the VGG model implementation.} 38 | } 39 | \description{ 40 | VGG models implementations based on 41 | \href{https://arxiv.org/pdf/1409.1556}{Very Deep Convolutional Networks For Large-Scale Image Recognition} 42 | } 43 | \section{Functions}{ 44 | \itemize{ 45 | \item \code{model_vgg11()}: VGG 11-layer model (configuration "A") 46 | 47 | \item \code{model_vgg11_bn()}: VGG 11-layer model (configuration "A") with batch normalization 48 | 49 | \item \code{model_vgg13()}: VGG 13-layer model (configuration "B") 50 | 51 | \item \code{model_vgg13_bn()}: VGG 13-layer model (configuration "B") with batch normalization 52 | 53 | \item \code{model_vgg16()}: VGG 13-layer model (configuration "D") 54 | 55 | \item \code{model_vgg16_bn()}: VGG 13-layer model (configuration "D") with batch normalization 56 | 57 | \item \code{model_vgg19()}: VGG 19-layer model (configuration "E") 58 | 59 | \item \code{model_vgg19_bn()}: VGG 19-layer model (configuration "E") with batch normalization 60 | 61 | }} 62 | \seealso{ 63 | Other models: 64 | \code{\link{model_alexnet}()}, 65 | \code{\link{model_inception_v3}()}, 66 | \code{\link{model_mobilenet_v2}()}, 67 | \code{\link{model_resnet}} 68 | } 69 | \concept{models} 70 | -------------------------------------------------------------------------------- /man/tensor_image_browse.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/vision_utils.R 3 | \name{tensor_image_browse} 4 | \alias{tensor_image_browse} 5 | \title{Display image tensor} 6 | \usage{ 7 | tensor_image_browse(image, browser = getOption("browser")) 8 | } 9 | \arguments{ 10 | \item{image}{\code{torch_tensor()} of shape (1, W, H) for grayscale image or (3, W, H) for 11 | color image to display} 12 | 13 | \item{browser}{argument passed to \link{browseURL}} 14 | } 15 | \description{ 16 | Display image tensor into browser 17 | } 18 | \seealso{ 19 | Other image display: 20 | \code{\link{draw_bounding_boxes}()}, 21 | \code{\link{draw_keypoints}()}, 22 | \code{\link{draw_segmentation_masks}()}, 23 | \code{\link{tensor_image_display}()}, 24 | \code{\link{vision_make_grid}()} 25 | } 26 | \concept{image display} 27 | -------------------------------------------------------------------------------- /man/tensor_image_display.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/vision_utils.R 3 | \name{tensor_image_display} 4 | \alias{tensor_image_display} 5 | \title{Display image tensor} 6 | \usage{ 7 | tensor_image_display(image, animate = TRUE) 8 | } 9 | \arguments{ 10 | \item{image}{\code{torch_tensor()} of shape (1, W, H) for grayscale image or (3, W, H) for 11 | color image to display} 12 | 13 | \item{animate}{support animations in the X11 display} 14 | } 15 | \description{ 16 | Display image tensor onto the X11 device 17 | } 18 | \seealso{ 19 | Other image display: 20 | \code{\link{draw_bounding_boxes}()}, 21 | \code{\link{draw_keypoints}()}, 22 | \code{\link{draw_segmentation_masks}()}, 23 | \code{\link{tensor_image_browse}()}, 24 | \code{\link{vision_make_grid}()} 25 | } 26 | \concept{image display} 27 | -------------------------------------------------------------------------------- /man/tiny_imagenet_dataset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tiny-imagenet-dataset.R 3 | \name{tiny_imagenet_dataset} 4 | \alias{tiny_imagenet_dataset} 5 | \title{Tiny ImageNet dataset} 6 | \usage{ 7 | tiny_imagenet_dataset(root, split = "train", download = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{root}{directory path to download the dataset.} 11 | 12 | \item{split}{dataset split, \code{train}, \code{validation} or \code{test}.} 13 | 14 | \item{download}{whether to download or not the dataset.} 15 | 16 | \item{...}{other arguments passed to \code{\link[=image_folder_dataset]{image_folder_dataset()}}.} 17 | } 18 | \description{ 19 | Prepares the Tiny ImageNet dataset and optionally downloads it. 20 | } 21 | \concept{dataset} 22 | -------------------------------------------------------------------------------- /man/transform_adjust_brightness.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_adjust_brightness} 4 | \alias{transform_adjust_brightness} 5 | \title{Adjust the brightness of an image} 6 | \usage{ 7 | transform_adjust_brightness(img, brightness_factor) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{brightness_factor}{(float): How much to adjust the brightness. Can be 13 | any non negative number. 0 gives a black image, 1 gives the 14 | original image while 2 increases the brightness by a factor of 2.} 15 | } 16 | \description{ 17 | Adjust the brightness of an image 18 | } 19 | \seealso{ 20 | Other transforms: 21 | \code{\link{transform_adjust_contrast}()}, 22 | \code{\link{transform_adjust_gamma}()}, 23 | \code{\link{transform_adjust_hue}()}, 24 | \code{\link{transform_adjust_saturation}()}, 25 | \code{\link{transform_affine}()}, 26 | \code{\link{transform_center_crop}()}, 27 | \code{\link{transform_color_jitter}()}, 28 | \code{\link{transform_convert_image_dtype}()}, 29 | \code{\link{transform_crop}()}, 30 | \code{\link{transform_five_crop}()}, 31 | \code{\link{transform_grayscale}()}, 32 | \code{\link{transform_hflip}()}, 33 | \code{\link{transform_linear_transformation}()}, 34 | \code{\link{transform_normalize}()}, 35 | \code{\link{transform_pad}()}, 36 | \code{\link{transform_perspective}()}, 37 | \code{\link{transform_random_affine}()}, 38 | \code{\link{transform_random_apply}()}, 39 | \code{\link{transform_random_choice}()}, 40 | \code{\link{transform_random_crop}()}, 41 | \code{\link{transform_random_erasing}()}, 42 | \code{\link{transform_random_grayscale}()}, 43 | \code{\link{transform_random_horizontal_flip}()}, 44 | \code{\link{transform_random_order}()}, 45 | \code{\link{transform_random_perspective}()}, 46 | \code{\link{transform_random_resized_crop}()}, 47 | \code{\link{transform_random_rotation}()}, 48 | \code{\link{transform_random_vertical_flip}()}, 49 | \code{\link{transform_resize}()}, 50 | \code{\link{transform_resized_crop}()}, 51 | \code{\link{transform_rgb_to_grayscale}()}, 52 | \code{\link{transform_rotate}()}, 53 | \code{\link{transform_ten_crop}()}, 54 | \code{\link{transform_to_tensor}()}, 55 | \code{\link{transform_vflip}()} 56 | } 57 | \concept{transforms} 58 | -------------------------------------------------------------------------------- /man/transform_adjust_contrast.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_adjust_contrast} 4 | \alias{transform_adjust_contrast} 5 | \title{Adjust the contrast of an image} 6 | \usage{ 7 | transform_adjust_contrast(img, contrast_factor) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{contrast_factor}{(float): How much to adjust the contrast. Can be any 13 | non negative number. 0 gives a solid gray image, 1 gives the 14 | original image while 2 increases the contrast by a factor of 2.} 15 | } 16 | \description{ 17 | Adjust the contrast of an image 18 | } 19 | \seealso{ 20 | Other transforms: 21 | \code{\link{transform_adjust_brightness}()}, 22 | \code{\link{transform_adjust_gamma}()}, 23 | \code{\link{transform_adjust_hue}()}, 24 | \code{\link{transform_adjust_saturation}()}, 25 | \code{\link{transform_affine}()}, 26 | \code{\link{transform_center_crop}()}, 27 | \code{\link{transform_color_jitter}()}, 28 | \code{\link{transform_convert_image_dtype}()}, 29 | \code{\link{transform_crop}()}, 30 | \code{\link{transform_five_crop}()}, 31 | \code{\link{transform_grayscale}()}, 32 | \code{\link{transform_hflip}()}, 33 | \code{\link{transform_linear_transformation}()}, 34 | \code{\link{transform_normalize}()}, 35 | \code{\link{transform_pad}()}, 36 | \code{\link{transform_perspective}()}, 37 | \code{\link{transform_random_affine}()}, 38 | \code{\link{transform_random_apply}()}, 39 | \code{\link{transform_random_choice}()}, 40 | \code{\link{transform_random_crop}()}, 41 | \code{\link{transform_random_erasing}()}, 42 | \code{\link{transform_random_grayscale}()}, 43 | \code{\link{transform_random_horizontal_flip}()}, 44 | \code{\link{transform_random_order}()}, 45 | \code{\link{transform_random_perspective}()}, 46 | \code{\link{transform_random_resized_crop}()}, 47 | \code{\link{transform_random_rotation}()}, 48 | \code{\link{transform_random_vertical_flip}()}, 49 | \code{\link{transform_resize}()}, 50 | \code{\link{transform_resized_crop}()}, 51 | \code{\link{transform_rgb_to_grayscale}()}, 52 | \code{\link{transform_rotate}()}, 53 | \code{\link{transform_ten_crop}()}, 54 | \code{\link{transform_to_tensor}()}, 55 | \code{\link{transform_vflip}()} 56 | } 57 | \concept{transforms} 58 | -------------------------------------------------------------------------------- /man/transform_adjust_gamma.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_adjust_gamma} 4 | \alias{transform_adjust_gamma} 5 | \title{Adjust the gamma of an RGB image} 6 | \usage{ 7 | transform_adjust_gamma(img, gamma, gain = 1) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{gamma}{(float): Non negative real number, same as \eqn{\gamma} in the 13 | equation. gamma larger than 1 make the shadows darker, while gamma smaller 14 | than 1 make dark regions lighter.} 15 | 16 | \item{gain}{(float): The constant multiplier.} 17 | } 18 | \description{ 19 | Also known as Power Law Transform. Intensities in RGB mode are adjusted 20 | based on the following equation: 21 | \deqn{ 22 | I_{\mbox{out}} = 255 \times \mbox{gain} \times \left 23 | (\frac{I_{\mbox{in}}}{255}\right)^{\gamma} 24 | } 25 | } 26 | \details{ 27 | See \href{https://en.wikipedia.org/wiki/Gamma_correction}{Gamma Correction} for more details. 28 | } 29 | \seealso{ 30 | Other transforms: 31 | \code{\link{transform_adjust_brightness}()}, 32 | \code{\link{transform_adjust_contrast}()}, 33 | \code{\link{transform_adjust_hue}()}, 34 | \code{\link{transform_adjust_saturation}()}, 35 | \code{\link{transform_affine}()}, 36 | \code{\link{transform_center_crop}()}, 37 | \code{\link{transform_color_jitter}()}, 38 | \code{\link{transform_convert_image_dtype}()}, 39 | \code{\link{transform_crop}()}, 40 | \code{\link{transform_five_crop}()}, 41 | \code{\link{transform_grayscale}()}, 42 | \code{\link{transform_hflip}()}, 43 | \code{\link{transform_linear_transformation}()}, 44 | \code{\link{transform_normalize}()}, 45 | \code{\link{transform_pad}()}, 46 | \code{\link{transform_perspective}()}, 47 | \code{\link{transform_random_affine}()}, 48 | \code{\link{transform_random_apply}()}, 49 | \code{\link{transform_random_choice}()}, 50 | \code{\link{transform_random_crop}()}, 51 | \code{\link{transform_random_erasing}()}, 52 | \code{\link{transform_random_grayscale}()}, 53 | \code{\link{transform_random_horizontal_flip}()}, 54 | \code{\link{transform_random_order}()}, 55 | \code{\link{transform_random_perspective}()}, 56 | \code{\link{transform_random_resized_crop}()}, 57 | \code{\link{transform_random_rotation}()}, 58 | \code{\link{transform_random_vertical_flip}()}, 59 | \code{\link{transform_resize}()}, 60 | \code{\link{transform_resized_crop}()}, 61 | \code{\link{transform_rgb_to_grayscale}()}, 62 | \code{\link{transform_rotate}()}, 63 | \code{\link{transform_ten_crop}()}, 64 | \code{\link{transform_to_tensor}()}, 65 | \code{\link{transform_vflip}()} 66 | } 67 | \concept{transforms} 68 | -------------------------------------------------------------------------------- /man/transform_adjust_hue.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_adjust_hue} 4 | \alias{transform_adjust_hue} 5 | \title{Adjust the hue of an image} 6 | \usage{ 7 | transform_adjust_hue(img, hue_factor) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{hue_factor}{(float): How much to shift the hue channel. Should be in 13 | \verb{[-0.5, 0.5]}. 0.5 and -0.5 give complete reversal of hue channel in 14 | HSV space in positive and negative direction respectively. 15 | 0 means no shift. Therefore, both -0.5 and 0.5 will give an image 16 | with complementary colors while 0 gives the original image.} 17 | } 18 | \description{ 19 | The image hue is adjusted by converting the image to HSV and 20 | cyclically shifting the intensities in the hue channel (H). 21 | The image is then converted back to original image mode. 22 | } 23 | \details{ 24 | \code{hue_factor} is the amount of shift in H channel and must be in the 25 | interval \verb{[-0.5, 0.5]}. 26 | 27 | See \href{https://en.wikipedia.org/wiki/Hue}{Hue} for more details. 28 | } 29 | \seealso{ 30 | Other transforms: 31 | \code{\link{transform_adjust_brightness}()}, 32 | \code{\link{transform_adjust_contrast}()}, 33 | \code{\link{transform_adjust_gamma}()}, 34 | \code{\link{transform_adjust_saturation}()}, 35 | \code{\link{transform_affine}()}, 36 | \code{\link{transform_center_crop}()}, 37 | \code{\link{transform_color_jitter}()}, 38 | \code{\link{transform_convert_image_dtype}()}, 39 | \code{\link{transform_crop}()}, 40 | \code{\link{transform_five_crop}()}, 41 | \code{\link{transform_grayscale}()}, 42 | \code{\link{transform_hflip}()}, 43 | \code{\link{transform_linear_transformation}()}, 44 | \code{\link{transform_normalize}()}, 45 | \code{\link{transform_pad}()}, 46 | \code{\link{transform_perspective}()}, 47 | \code{\link{transform_random_affine}()}, 48 | \code{\link{transform_random_apply}()}, 49 | \code{\link{transform_random_choice}()}, 50 | \code{\link{transform_random_crop}()}, 51 | \code{\link{transform_random_erasing}()}, 52 | \code{\link{transform_random_grayscale}()}, 53 | \code{\link{transform_random_horizontal_flip}()}, 54 | \code{\link{transform_random_order}()}, 55 | \code{\link{transform_random_perspective}()}, 56 | \code{\link{transform_random_resized_crop}()}, 57 | \code{\link{transform_random_rotation}()}, 58 | \code{\link{transform_random_vertical_flip}()}, 59 | \code{\link{transform_resize}()}, 60 | \code{\link{transform_resized_crop}()}, 61 | \code{\link{transform_rgb_to_grayscale}()}, 62 | \code{\link{transform_rotate}()}, 63 | \code{\link{transform_ten_crop}()}, 64 | \code{\link{transform_to_tensor}()}, 65 | \code{\link{transform_vflip}()} 66 | } 67 | \concept{transforms} 68 | -------------------------------------------------------------------------------- /man/transform_adjust_saturation.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_adjust_saturation} 4 | \alias{transform_adjust_saturation} 5 | \title{Adjust the color saturation of an image} 6 | \usage{ 7 | transform_adjust_saturation(img, saturation_factor) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{saturation_factor}{(float): How much to adjust the saturation. 0 will 13 | give a black and white image, 1 will give the original image while 14 | 2 will enhance the saturation by a factor of 2.} 15 | } 16 | \description{ 17 | Adjust the color saturation of an image 18 | } 19 | \seealso{ 20 | Other transforms: 21 | \code{\link{transform_adjust_brightness}()}, 22 | \code{\link{transform_adjust_contrast}()}, 23 | \code{\link{transform_adjust_gamma}()}, 24 | \code{\link{transform_adjust_hue}()}, 25 | \code{\link{transform_affine}()}, 26 | \code{\link{transform_center_crop}()}, 27 | \code{\link{transform_color_jitter}()}, 28 | \code{\link{transform_convert_image_dtype}()}, 29 | \code{\link{transform_crop}()}, 30 | \code{\link{transform_five_crop}()}, 31 | \code{\link{transform_grayscale}()}, 32 | \code{\link{transform_hflip}()}, 33 | \code{\link{transform_linear_transformation}()}, 34 | \code{\link{transform_normalize}()}, 35 | \code{\link{transform_pad}()}, 36 | \code{\link{transform_perspective}()}, 37 | \code{\link{transform_random_affine}()}, 38 | \code{\link{transform_random_apply}()}, 39 | \code{\link{transform_random_choice}()}, 40 | \code{\link{transform_random_crop}()}, 41 | \code{\link{transform_random_erasing}()}, 42 | \code{\link{transform_random_grayscale}()}, 43 | \code{\link{transform_random_horizontal_flip}()}, 44 | \code{\link{transform_random_order}()}, 45 | \code{\link{transform_random_perspective}()}, 46 | \code{\link{transform_random_resized_crop}()}, 47 | \code{\link{transform_random_rotation}()}, 48 | \code{\link{transform_random_vertical_flip}()}, 49 | \code{\link{transform_resize}()}, 50 | \code{\link{transform_resized_crop}()}, 51 | \code{\link{transform_rgb_to_grayscale}()}, 52 | \code{\link{transform_rotate}()}, 53 | \code{\link{transform_ten_crop}()}, 54 | \code{\link{transform_to_tensor}()}, 55 | \code{\link{transform_vflip}()} 56 | } 57 | \concept{transforms} 58 | -------------------------------------------------------------------------------- /man/transform_affine.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_affine} 4 | \alias{transform_affine} 5 | \title{Apply affine transformation on an image keeping image center invariant} 6 | \usage{ 7 | transform_affine( 8 | img, 9 | angle, 10 | translate, 11 | scale, 12 | shear, 13 | resample = 0, 14 | fillcolor = NULL 15 | ) 16 | } 17 | \arguments{ 18 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 19 | 20 | \item{angle}{(float or int): rotation angle value in degrees, 21 | counter-clockwise.} 22 | 23 | \item{translate}{(sequence of int) – horizontal and vertical translations 24 | (post-rotation translation)} 25 | 26 | \item{scale}{(float) – overall scale} 27 | 28 | \item{shear}{(float or sequence) – shear angle value in degrees between -180 to 180, 29 | clockwise direction. If a sequence is specified, the first value corresponds 30 | to a shear parallel to the x-axis, while the second value corresponds to a 31 | shear parallel to the y-axis.} 32 | 33 | \item{resample}{(int, optional): An optional resampling filter. See interpolation 34 | modes.} 35 | 36 | \item{fillcolor}{(tuple or int): Optional fill color (Tuple for RGB Image and 37 | int for grayscale) for the area outside the transform in the output image 38 | (Pillow>=5.0.0). This option is not supported for Tensor input. Fill value 39 | for the area outside the transform in the output image is always 0.} 40 | } 41 | \description{ 42 | Apply affine transformation on an image keeping image center invariant 43 | } 44 | \seealso{ 45 | Other transforms: 46 | \code{\link{transform_adjust_brightness}()}, 47 | \code{\link{transform_adjust_contrast}()}, 48 | \code{\link{transform_adjust_gamma}()}, 49 | \code{\link{transform_adjust_hue}()}, 50 | \code{\link{transform_adjust_saturation}()}, 51 | \code{\link{transform_center_crop}()}, 52 | \code{\link{transform_color_jitter}()}, 53 | \code{\link{transform_convert_image_dtype}()}, 54 | \code{\link{transform_crop}()}, 55 | \code{\link{transform_five_crop}()}, 56 | \code{\link{transform_grayscale}()}, 57 | \code{\link{transform_hflip}()}, 58 | \code{\link{transform_linear_transformation}()}, 59 | \code{\link{transform_normalize}()}, 60 | \code{\link{transform_pad}()}, 61 | \code{\link{transform_perspective}()}, 62 | \code{\link{transform_random_affine}()}, 63 | \code{\link{transform_random_apply}()}, 64 | \code{\link{transform_random_choice}()}, 65 | \code{\link{transform_random_crop}()}, 66 | \code{\link{transform_random_erasing}()}, 67 | \code{\link{transform_random_grayscale}()}, 68 | \code{\link{transform_random_horizontal_flip}()}, 69 | \code{\link{transform_random_order}()}, 70 | \code{\link{transform_random_perspective}()}, 71 | \code{\link{transform_random_resized_crop}()}, 72 | \code{\link{transform_random_rotation}()}, 73 | \code{\link{transform_random_vertical_flip}()}, 74 | \code{\link{transform_resize}()}, 75 | \code{\link{transform_resized_crop}()}, 76 | \code{\link{transform_rgb_to_grayscale}()}, 77 | \code{\link{transform_rotate}()}, 78 | \code{\link{transform_ten_crop}()}, 79 | \code{\link{transform_to_tensor}()}, 80 | \code{\link{transform_vflip}()} 81 | } 82 | \concept{transforms} 83 | -------------------------------------------------------------------------------- /man/transform_center_crop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_center_crop} 4 | \alias{transform_center_crop} 5 | \title{Crops the given image at the center} 6 | \usage{ 7 | transform_center_crop(img, size) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{size}{(sequence or int): Desired output size of the crop. If size is 13 | an int instead of sequence like c(h, w), a square crop (size, size) is 14 | made. If provided a tuple or list of length 1, it will be interpreted as 15 | \code{c(size, size)}.} 16 | } 17 | \description{ 18 | The image can be a Magick Image or a torch Tensor, in which case it is 19 | expected to have \verb{[..., H, W]} shape, where ... means an arbitrary number 20 | of leading dimensions. 21 | } 22 | \seealso{ 23 | Other transforms: 24 | \code{\link{transform_adjust_brightness}()}, 25 | \code{\link{transform_adjust_contrast}()}, 26 | \code{\link{transform_adjust_gamma}()}, 27 | \code{\link{transform_adjust_hue}()}, 28 | \code{\link{transform_adjust_saturation}()}, 29 | \code{\link{transform_affine}()}, 30 | \code{\link{transform_color_jitter}()}, 31 | \code{\link{transform_convert_image_dtype}()}, 32 | \code{\link{transform_crop}()}, 33 | \code{\link{transform_five_crop}()}, 34 | \code{\link{transform_grayscale}()}, 35 | \code{\link{transform_hflip}()}, 36 | \code{\link{transform_linear_transformation}()}, 37 | \code{\link{transform_normalize}()}, 38 | \code{\link{transform_pad}()}, 39 | \code{\link{transform_perspective}()}, 40 | \code{\link{transform_random_affine}()}, 41 | \code{\link{transform_random_apply}()}, 42 | \code{\link{transform_random_choice}()}, 43 | \code{\link{transform_random_crop}()}, 44 | \code{\link{transform_random_erasing}()}, 45 | \code{\link{transform_random_grayscale}()}, 46 | \code{\link{transform_random_horizontal_flip}()}, 47 | \code{\link{transform_random_order}()}, 48 | \code{\link{transform_random_perspective}()}, 49 | \code{\link{transform_random_resized_crop}()}, 50 | \code{\link{transform_random_rotation}()}, 51 | \code{\link{transform_random_vertical_flip}()}, 52 | \code{\link{transform_resize}()}, 53 | \code{\link{transform_resized_crop}()}, 54 | \code{\link{transform_rgb_to_grayscale}()}, 55 | \code{\link{transform_rotate}()}, 56 | \code{\link{transform_ten_crop}()}, 57 | \code{\link{transform_to_tensor}()}, 58 | \code{\link{transform_vflip}()} 59 | } 60 | \concept{transforms} 61 | -------------------------------------------------------------------------------- /man/transform_color_jitter.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_color_jitter} 4 | \alias{transform_color_jitter} 5 | \title{Randomly change the brightness, contrast and saturation of an image} 6 | \usage{ 7 | transform_color_jitter( 8 | img, 9 | brightness = 0, 10 | contrast = 0, 11 | saturation = 0, 12 | hue = 0 13 | ) 14 | } 15 | \arguments{ 16 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 17 | 18 | \item{brightness}{(float or tuple of float (min, max)): How much to jitter 19 | brightness. \code{brightness_factor} is chosen uniformly from 20 | \verb{[max(0, 1 - brightness), 1 + brightness]} or the given \verb{[min, max]}. 21 | Should be non negative numbers.} 22 | 23 | \item{contrast}{(float or tuple of float (min, max)): How much to jitter 24 | contrast. \code{contrast_factor} is chosen uniformly from 25 | \verb{[max(0, 1 - contrast), 1 + contrast]} or the given \verb{[min, max]}. Should 26 | be non negative numbers.} 27 | 28 | \item{saturation}{(float or tuple of float (min, max)): How much to jitter 29 | saturation. \code{saturation_factor} is chosen uniformly from 30 | \verb{[max(0, 1 - saturation), 1 + saturation]} or the given \verb{[min, max]}. 31 | Should be non negative numbers.} 32 | 33 | \item{hue}{(float or tuple of float (min, max)): How much to jitter hue. 34 | \code{hue_factor} is chosen uniformly from \verb{[-hue, hue]} or the given 35 | \verb{[min, max]}. Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.} 36 | } 37 | \description{ 38 | Randomly change the brightness, contrast and saturation of an image 39 | } 40 | \seealso{ 41 | Other transforms: 42 | \code{\link{transform_adjust_brightness}()}, 43 | \code{\link{transform_adjust_contrast}()}, 44 | \code{\link{transform_adjust_gamma}()}, 45 | \code{\link{transform_adjust_hue}()}, 46 | \code{\link{transform_adjust_saturation}()}, 47 | \code{\link{transform_affine}()}, 48 | \code{\link{transform_center_crop}()}, 49 | \code{\link{transform_convert_image_dtype}()}, 50 | \code{\link{transform_crop}()}, 51 | \code{\link{transform_five_crop}()}, 52 | \code{\link{transform_grayscale}()}, 53 | \code{\link{transform_hflip}()}, 54 | \code{\link{transform_linear_transformation}()}, 55 | \code{\link{transform_normalize}()}, 56 | \code{\link{transform_pad}()}, 57 | \code{\link{transform_perspective}()}, 58 | \code{\link{transform_random_affine}()}, 59 | \code{\link{transform_random_apply}()}, 60 | \code{\link{transform_random_choice}()}, 61 | \code{\link{transform_random_crop}()}, 62 | \code{\link{transform_random_erasing}()}, 63 | \code{\link{transform_random_grayscale}()}, 64 | \code{\link{transform_random_horizontal_flip}()}, 65 | \code{\link{transform_random_order}()}, 66 | \code{\link{transform_random_perspective}()}, 67 | \code{\link{transform_random_resized_crop}()}, 68 | \code{\link{transform_random_rotation}()}, 69 | \code{\link{transform_random_vertical_flip}()}, 70 | \code{\link{transform_resize}()}, 71 | \code{\link{transform_resized_crop}()}, 72 | \code{\link{transform_rgb_to_grayscale}()}, 73 | \code{\link{transform_rotate}()}, 74 | \code{\link{transform_ten_crop}()}, 75 | \code{\link{transform_to_tensor}()}, 76 | \code{\link{transform_vflip}()} 77 | } 78 | \concept{transforms} 79 | -------------------------------------------------------------------------------- /man/transform_convert_image_dtype.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_convert_image_dtype} 4 | \alias{transform_convert_image_dtype} 5 | \title{Convert a tensor image to the given \code{dtype} and scale the values accordingly} 6 | \usage{ 7 | transform_convert_image_dtype(img, dtype = torch::torch_float()) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{dtype}{(torch.dtype): Desired data type of the output.} 13 | } 14 | \description{ 15 | Convert a tensor image to the given \code{dtype} and scale the values accordingly 16 | } 17 | \note{ 18 | When converting from a smaller to a larger integer \code{dtype} the maximum 19 | values are \strong{not} mapped exactly. If converted back and forth, this 20 | mismatch has no effect. 21 | } 22 | \seealso{ 23 | Other transforms: 24 | \code{\link{transform_adjust_brightness}()}, 25 | \code{\link{transform_adjust_contrast}()}, 26 | \code{\link{transform_adjust_gamma}()}, 27 | \code{\link{transform_adjust_hue}()}, 28 | \code{\link{transform_adjust_saturation}()}, 29 | \code{\link{transform_affine}()}, 30 | \code{\link{transform_center_crop}()}, 31 | \code{\link{transform_color_jitter}()}, 32 | \code{\link{transform_crop}()}, 33 | \code{\link{transform_five_crop}()}, 34 | \code{\link{transform_grayscale}()}, 35 | \code{\link{transform_hflip}()}, 36 | \code{\link{transform_linear_transformation}()}, 37 | \code{\link{transform_normalize}()}, 38 | \code{\link{transform_pad}()}, 39 | \code{\link{transform_perspective}()}, 40 | \code{\link{transform_random_affine}()}, 41 | \code{\link{transform_random_apply}()}, 42 | \code{\link{transform_random_choice}()}, 43 | \code{\link{transform_random_crop}()}, 44 | \code{\link{transform_random_erasing}()}, 45 | \code{\link{transform_random_grayscale}()}, 46 | \code{\link{transform_random_horizontal_flip}()}, 47 | \code{\link{transform_random_order}()}, 48 | \code{\link{transform_random_perspective}()}, 49 | \code{\link{transform_random_resized_crop}()}, 50 | \code{\link{transform_random_rotation}()}, 51 | \code{\link{transform_random_vertical_flip}()}, 52 | \code{\link{transform_resize}()}, 53 | \code{\link{transform_resized_crop}()}, 54 | \code{\link{transform_rgb_to_grayscale}()}, 55 | \code{\link{transform_rotate}()}, 56 | \code{\link{transform_ten_crop}()}, 57 | \code{\link{transform_to_tensor}()}, 58 | \code{\link{transform_vflip}()} 59 | } 60 | \concept{transforms} 61 | -------------------------------------------------------------------------------- /man/transform_crop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_crop} 4 | \alias{transform_crop} 5 | \title{Crop the given image at specified location and output size} 6 | \usage{ 7 | transform_crop(img, top, left, height, width) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{top}{(int): Vertical component of the top left corner of the crop box.} 13 | 14 | \item{left}{(int): Horizontal component of the top left corner of the crop 15 | box.} 16 | 17 | \item{height}{(int): Height of the crop box.} 18 | 19 | \item{width}{(int): Width of the crop box.} 20 | } 21 | \description{ 22 | Crop the given image at specified location and output size 23 | } 24 | \seealso{ 25 | Other transforms: 26 | \code{\link{transform_adjust_brightness}()}, 27 | \code{\link{transform_adjust_contrast}()}, 28 | \code{\link{transform_adjust_gamma}()}, 29 | \code{\link{transform_adjust_hue}()}, 30 | \code{\link{transform_adjust_saturation}()}, 31 | \code{\link{transform_affine}()}, 32 | \code{\link{transform_center_crop}()}, 33 | \code{\link{transform_color_jitter}()}, 34 | \code{\link{transform_convert_image_dtype}()}, 35 | \code{\link{transform_five_crop}()}, 36 | \code{\link{transform_grayscale}()}, 37 | \code{\link{transform_hflip}()}, 38 | \code{\link{transform_linear_transformation}()}, 39 | \code{\link{transform_normalize}()}, 40 | \code{\link{transform_pad}()}, 41 | \code{\link{transform_perspective}()}, 42 | \code{\link{transform_random_affine}()}, 43 | \code{\link{transform_random_apply}()}, 44 | \code{\link{transform_random_choice}()}, 45 | \code{\link{transform_random_crop}()}, 46 | \code{\link{transform_random_erasing}()}, 47 | \code{\link{transform_random_grayscale}()}, 48 | \code{\link{transform_random_horizontal_flip}()}, 49 | \code{\link{transform_random_order}()}, 50 | \code{\link{transform_random_perspective}()}, 51 | \code{\link{transform_random_resized_crop}()}, 52 | \code{\link{transform_random_rotation}()}, 53 | \code{\link{transform_random_vertical_flip}()}, 54 | \code{\link{transform_resize}()}, 55 | \code{\link{transform_resized_crop}()}, 56 | \code{\link{transform_rgb_to_grayscale}()}, 57 | \code{\link{transform_rotate}()}, 58 | \code{\link{transform_ten_crop}()}, 59 | \code{\link{transform_to_tensor}()}, 60 | \code{\link{transform_vflip}()} 61 | } 62 | \concept{transforms} 63 | -------------------------------------------------------------------------------- /man/transform_five_crop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_five_crop} 4 | \alias{transform_five_crop} 5 | \title{Crop image into four corners and a central crop} 6 | \usage{ 7 | transform_five_crop(img, size) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{size}{(sequence or int): Desired output size. If size is a sequence 13 | like c(h, w), output size will be matched to this. If size is an int, 14 | smaller edge of the image will be matched to this number. 15 | i.e, if height > width, then image will be rescaled to 16 | (size * height / width, size).} 17 | } 18 | \description{ 19 | Crop the given image into four corners and the central crop. This transform 20 | returns a tuple of images and there may be a mismatch in the number of 21 | inputs and targets your Dataset returns. 22 | } 23 | \seealso{ 24 | Other transforms: 25 | \code{\link{transform_adjust_brightness}()}, 26 | \code{\link{transform_adjust_contrast}()}, 27 | \code{\link{transform_adjust_gamma}()}, 28 | \code{\link{transform_adjust_hue}()}, 29 | \code{\link{transform_adjust_saturation}()}, 30 | \code{\link{transform_affine}()}, 31 | \code{\link{transform_center_crop}()}, 32 | \code{\link{transform_color_jitter}()}, 33 | \code{\link{transform_convert_image_dtype}()}, 34 | \code{\link{transform_crop}()}, 35 | \code{\link{transform_grayscale}()}, 36 | \code{\link{transform_hflip}()}, 37 | \code{\link{transform_linear_transformation}()}, 38 | \code{\link{transform_normalize}()}, 39 | \code{\link{transform_pad}()}, 40 | \code{\link{transform_perspective}()}, 41 | \code{\link{transform_random_affine}()}, 42 | \code{\link{transform_random_apply}()}, 43 | \code{\link{transform_random_choice}()}, 44 | \code{\link{transform_random_crop}()}, 45 | \code{\link{transform_random_erasing}()}, 46 | \code{\link{transform_random_grayscale}()}, 47 | \code{\link{transform_random_horizontal_flip}()}, 48 | \code{\link{transform_random_order}()}, 49 | \code{\link{transform_random_perspective}()}, 50 | \code{\link{transform_random_resized_crop}()}, 51 | \code{\link{transform_random_rotation}()}, 52 | \code{\link{transform_random_vertical_flip}()}, 53 | \code{\link{transform_resize}()}, 54 | \code{\link{transform_resized_crop}()}, 55 | \code{\link{transform_rgb_to_grayscale}()}, 56 | \code{\link{transform_rotate}()}, 57 | \code{\link{transform_ten_crop}()}, 58 | \code{\link{transform_to_tensor}()}, 59 | \code{\link{transform_vflip}()} 60 | } 61 | \concept{transforms} 62 | -------------------------------------------------------------------------------- /man/transform_grayscale.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_grayscale} 4 | \alias{transform_grayscale} 5 | \title{Convert image to grayscale} 6 | \usage{ 7 | transform_grayscale(img, num_output_channels) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{num_output_channels}{(int): (1 or 3) number of channels desired for 13 | output image} 14 | } 15 | \description{ 16 | Convert image to grayscale 17 | } 18 | \seealso{ 19 | Other transforms: 20 | \code{\link{transform_adjust_brightness}()}, 21 | \code{\link{transform_adjust_contrast}()}, 22 | \code{\link{transform_adjust_gamma}()}, 23 | \code{\link{transform_adjust_hue}()}, 24 | \code{\link{transform_adjust_saturation}()}, 25 | \code{\link{transform_affine}()}, 26 | \code{\link{transform_center_crop}()}, 27 | \code{\link{transform_color_jitter}()}, 28 | \code{\link{transform_convert_image_dtype}()}, 29 | \code{\link{transform_crop}()}, 30 | \code{\link{transform_five_crop}()}, 31 | \code{\link{transform_hflip}()}, 32 | \code{\link{transform_linear_transformation}()}, 33 | \code{\link{transform_normalize}()}, 34 | \code{\link{transform_pad}()}, 35 | \code{\link{transform_perspective}()}, 36 | \code{\link{transform_random_affine}()}, 37 | \code{\link{transform_random_apply}()}, 38 | \code{\link{transform_random_choice}()}, 39 | \code{\link{transform_random_crop}()}, 40 | \code{\link{transform_random_erasing}()}, 41 | \code{\link{transform_random_grayscale}()}, 42 | \code{\link{transform_random_horizontal_flip}()}, 43 | \code{\link{transform_random_order}()}, 44 | \code{\link{transform_random_perspective}()}, 45 | \code{\link{transform_random_resized_crop}()}, 46 | \code{\link{transform_random_rotation}()}, 47 | \code{\link{transform_random_vertical_flip}()}, 48 | \code{\link{transform_resize}()}, 49 | \code{\link{transform_resized_crop}()}, 50 | \code{\link{transform_rgb_to_grayscale}()}, 51 | \code{\link{transform_rotate}()}, 52 | \code{\link{transform_ten_crop}()}, 53 | \code{\link{transform_to_tensor}()}, 54 | \code{\link{transform_vflip}()} 55 | } 56 | \concept{transforms} 57 | -------------------------------------------------------------------------------- /man/transform_hflip.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_hflip} 4 | \alias{transform_hflip} 5 | \title{Horizontally flip a PIL Image or Tensor} 6 | \usage{ 7 | transform_hflip(img) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | } 12 | \description{ 13 | Horizontally flip a PIL Image or Tensor 14 | } 15 | \seealso{ 16 | Other transforms: 17 | \code{\link{transform_adjust_brightness}()}, 18 | \code{\link{transform_adjust_contrast}()}, 19 | \code{\link{transform_adjust_gamma}()}, 20 | \code{\link{transform_adjust_hue}()}, 21 | \code{\link{transform_adjust_saturation}()}, 22 | \code{\link{transform_affine}()}, 23 | \code{\link{transform_center_crop}()}, 24 | \code{\link{transform_color_jitter}()}, 25 | \code{\link{transform_convert_image_dtype}()}, 26 | \code{\link{transform_crop}()}, 27 | \code{\link{transform_five_crop}()}, 28 | \code{\link{transform_grayscale}()}, 29 | \code{\link{transform_linear_transformation}()}, 30 | \code{\link{transform_normalize}()}, 31 | \code{\link{transform_pad}()}, 32 | \code{\link{transform_perspective}()}, 33 | \code{\link{transform_random_affine}()}, 34 | \code{\link{transform_random_apply}()}, 35 | \code{\link{transform_random_choice}()}, 36 | \code{\link{transform_random_crop}()}, 37 | \code{\link{transform_random_erasing}()}, 38 | \code{\link{transform_random_grayscale}()}, 39 | \code{\link{transform_random_horizontal_flip}()}, 40 | \code{\link{transform_random_order}()}, 41 | \code{\link{transform_random_perspective}()}, 42 | \code{\link{transform_random_resized_crop}()}, 43 | \code{\link{transform_random_rotation}()}, 44 | \code{\link{transform_random_vertical_flip}()}, 45 | \code{\link{transform_resize}()}, 46 | \code{\link{transform_resized_crop}()}, 47 | \code{\link{transform_rgb_to_grayscale}()}, 48 | \code{\link{transform_rotate}()}, 49 | \code{\link{transform_ten_crop}()}, 50 | \code{\link{transform_to_tensor}()}, 51 | \code{\link{transform_vflip}()} 52 | } 53 | \concept{transforms} 54 | -------------------------------------------------------------------------------- /man/transform_linear_transformation.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_linear_transformation} 4 | \alias{transform_linear_transformation} 5 | \title{Transform a tensor image with a square transformation matrix and a 6 | mean_vector computed offline} 7 | \usage{ 8 | transform_linear_transformation(img, transformation_matrix, mean_vector) 9 | } 10 | \arguments{ 11 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 12 | 13 | \item{transformation_matrix}{(Tensor): tensor \verb{[D x D]}, D = C x H x W.} 14 | 15 | \item{mean_vector}{(Tensor): tensor \link{D}, D = C x H x W.} 16 | } 17 | \description{ 18 | Given \code{transformation_matrix} and \code{mean_vector}, will flatten the 19 | \code{torch_tensor} and subtract \code{mean_vector} from it which is then followed by 20 | computing the dot product with the transformation matrix and then reshaping 21 | the tensor to its original shape. 22 | } 23 | \section{Applications}{ 24 | 25 | whitening transformation: Suppose X is a column vector zero-centered data. 26 | Then compute the data covariance matrix \verb{[D x D]} with torch.mm(X.t(), X), 27 | perform SVD on this matrix and pass it as \code{transformation_matrix}. 28 | } 29 | 30 | \seealso{ 31 | Other transforms: 32 | \code{\link{transform_adjust_brightness}()}, 33 | \code{\link{transform_adjust_contrast}()}, 34 | \code{\link{transform_adjust_gamma}()}, 35 | \code{\link{transform_adjust_hue}()}, 36 | \code{\link{transform_adjust_saturation}()}, 37 | \code{\link{transform_affine}()}, 38 | \code{\link{transform_center_crop}()}, 39 | \code{\link{transform_color_jitter}()}, 40 | \code{\link{transform_convert_image_dtype}()}, 41 | \code{\link{transform_crop}()}, 42 | \code{\link{transform_five_crop}()}, 43 | \code{\link{transform_grayscale}()}, 44 | \code{\link{transform_hflip}()}, 45 | \code{\link{transform_normalize}()}, 46 | \code{\link{transform_pad}()}, 47 | \code{\link{transform_perspective}()}, 48 | \code{\link{transform_random_affine}()}, 49 | \code{\link{transform_random_apply}()}, 50 | \code{\link{transform_random_choice}()}, 51 | \code{\link{transform_random_crop}()}, 52 | \code{\link{transform_random_erasing}()}, 53 | \code{\link{transform_random_grayscale}()}, 54 | \code{\link{transform_random_horizontal_flip}()}, 55 | \code{\link{transform_random_order}()}, 56 | \code{\link{transform_random_perspective}()}, 57 | \code{\link{transform_random_resized_crop}()}, 58 | \code{\link{transform_random_rotation}()}, 59 | \code{\link{transform_random_vertical_flip}()}, 60 | \code{\link{transform_resize}()}, 61 | \code{\link{transform_resized_crop}()}, 62 | \code{\link{transform_rgb_to_grayscale}()}, 63 | \code{\link{transform_rotate}()}, 64 | \code{\link{transform_ten_crop}()}, 65 | \code{\link{transform_to_tensor}()}, 66 | \code{\link{transform_vflip}()} 67 | } 68 | \concept{transforms} 69 | -------------------------------------------------------------------------------- /man/transform_normalize.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_normalize} 4 | \alias{transform_normalize} 5 | \title{Normalize a tensor image with mean and standard deviation} 6 | \usage{ 7 | transform_normalize(img, mean, std, inplace = FALSE) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{mean}{(sequence): Sequence of means for each channel.} 13 | 14 | \item{std}{(sequence): Sequence of standard deviations for each channel.} 15 | 16 | \item{inplace}{(bool,optional): Bool to make this operation in-place.} 17 | } 18 | \description{ 19 | Given mean: \verb{(mean[1],...,mean[n])} and std: \verb{(std[1],..,std[n])} for \code{n} 20 | channels, this transform will normalize each channel of the input 21 | \code{torch_tensor} i.e., 22 | \code{output[channel] = (input[channel] - mean[channel]) / std[channel]} 23 | } 24 | \note{ 25 | This transform acts out of place, i.e., it does not mutate the input tensor. 26 | } 27 | \seealso{ 28 | Other transforms: 29 | \code{\link{transform_adjust_brightness}()}, 30 | \code{\link{transform_adjust_contrast}()}, 31 | \code{\link{transform_adjust_gamma}()}, 32 | \code{\link{transform_adjust_hue}()}, 33 | \code{\link{transform_adjust_saturation}()}, 34 | \code{\link{transform_affine}()}, 35 | \code{\link{transform_center_crop}()}, 36 | \code{\link{transform_color_jitter}()}, 37 | \code{\link{transform_convert_image_dtype}()}, 38 | \code{\link{transform_crop}()}, 39 | \code{\link{transform_five_crop}()}, 40 | \code{\link{transform_grayscale}()}, 41 | \code{\link{transform_hflip}()}, 42 | \code{\link{transform_linear_transformation}()}, 43 | \code{\link{transform_pad}()}, 44 | \code{\link{transform_perspective}()}, 45 | \code{\link{transform_random_affine}()}, 46 | \code{\link{transform_random_apply}()}, 47 | \code{\link{transform_random_choice}()}, 48 | \code{\link{transform_random_crop}()}, 49 | \code{\link{transform_random_erasing}()}, 50 | \code{\link{transform_random_grayscale}()}, 51 | \code{\link{transform_random_horizontal_flip}()}, 52 | \code{\link{transform_random_order}()}, 53 | \code{\link{transform_random_perspective}()}, 54 | \code{\link{transform_random_resized_crop}()}, 55 | \code{\link{transform_random_rotation}()}, 56 | \code{\link{transform_random_vertical_flip}()}, 57 | \code{\link{transform_resize}()}, 58 | \code{\link{transform_resized_crop}()}, 59 | \code{\link{transform_rgb_to_grayscale}()}, 60 | \code{\link{transform_rotate}()}, 61 | \code{\link{transform_ten_crop}()}, 62 | \code{\link{transform_to_tensor}()}, 63 | \code{\link{transform_vflip}()} 64 | } 65 | \concept{transforms} 66 | -------------------------------------------------------------------------------- /man/transform_pad.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_pad} 4 | \alias{transform_pad} 5 | \title{Pad the given image on all sides with the given "pad" value} 6 | \usage{ 7 | transform_pad(img, padding, fill = 0, padding_mode = "constant") 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{padding}{(int or tuple or list): Padding on each border. If a single 13 | int is provided this is used to pad all borders. If tuple of length 2 is 14 | provided this is the padding on left/right and top/bottom respectively. 15 | If a tuple of length 4 is provided this is the padding for the left, right, 16 | top and bottom borders respectively.} 17 | 18 | \item{fill}{(int or str or tuple): Pixel fill value for constant fill. 19 | Default is 0. If a tuple of length 3, it is used to fill R, G, B channels 20 | respectively. This value is only used when the padding_mode is constant. 21 | Only int value is supported for Tensors.} 22 | 23 | \item{padding_mode}{Type of padding. Should be: constant, edge, reflect or 24 | symmetric. Default is constant. Mode symmetric is not yet supported for 25 | Tensor inputs. 26 | \itemize{ 27 | \item constant: pads with a constant value, this value is specified with fill 28 | \item edge: pads with the last value on the edge of the image 29 | \item reflect: pads with reflection of image (without repeating the last 30 | value on the edge) padding \verb{[1, 2, 3, 4]} with 2 elements on both sides 31 | in reflect mode will result in \verb{[3, 2, 1, 2, 3, 4, 3, 2]} 32 | \item symmetric: pads with reflection of image (repeating the last value on 33 | the edge) padding \verb{[1, 2, 3, 4]} with 2 elements on both sides in 34 | symmetric mode will result in \verb{[2, 1, 1, 2, 3, 4, 4, 3]} 35 | }} 36 | } 37 | \description{ 38 | The image can be a Magick Image or a torch Tensor, in which case it is 39 | expected to have \verb{[..., H, W]} shape, where ... means an arbitrary number 40 | of leading dimensions. 41 | } 42 | \seealso{ 43 | Other transforms: 44 | \code{\link{transform_adjust_brightness}()}, 45 | \code{\link{transform_adjust_contrast}()}, 46 | \code{\link{transform_adjust_gamma}()}, 47 | \code{\link{transform_adjust_hue}()}, 48 | \code{\link{transform_adjust_saturation}()}, 49 | \code{\link{transform_affine}()}, 50 | \code{\link{transform_center_crop}()}, 51 | \code{\link{transform_color_jitter}()}, 52 | \code{\link{transform_convert_image_dtype}()}, 53 | \code{\link{transform_crop}()}, 54 | \code{\link{transform_five_crop}()}, 55 | \code{\link{transform_grayscale}()}, 56 | \code{\link{transform_hflip}()}, 57 | \code{\link{transform_linear_transformation}()}, 58 | \code{\link{transform_normalize}()}, 59 | \code{\link{transform_perspective}()}, 60 | \code{\link{transform_random_affine}()}, 61 | \code{\link{transform_random_apply}()}, 62 | \code{\link{transform_random_choice}()}, 63 | \code{\link{transform_random_crop}()}, 64 | \code{\link{transform_random_erasing}()}, 65 | \code{\link{transform_random_grayscale}()}, 66 | \code{\link{transform_random_horizontal_flip}()}, 67 | \code{\link{transform_random_order}()}, 68 | \code{\link{transform_random_perspective}()}, 69 | \code{\link{transform_random_resized_crop}()}, 70 | \code{\link{transform_random_rotation}()}, 71 | \code{\link{transform_random_vertical_flip}()}, 72 | \code{\link{transform_resize}()}, 73 | \code{\link{transform_resized_crop}()}, 74 | \code{\link{transform_rgb_to_grayscale}()}, 75 | \code{\link{transform_rotate}()}, 76 | \code{\link{transform_ten_crop}()}, 77 | \code{\link{transform_to_tensor}()}, 78 | \code{\link{transform_vflip}()} 79 | } 80 | \concept{transforms} 81 | -------------------------------------------------------------------------------- /man/transform_perspective.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_perspective} 4 | \alias{transform_perspective} 5 | \title{Perspective transformation of an image} 6 | \usage{ 7 | transform_perspective( 8 | img, 9 | startpoints, 10 | endpoints, 11 | interpolation = 2, 12 | fill = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 17 | 18 | \item{startpoints}{(list of list of ints): List containing four lists of two 19 | integers corresponding to four corners 20 | \verb{[top-left, top-right, bottom-right, bottom-left]} of the original image.} 21 | 22 | \item{endpoints}{(list of list of ints): List containing four lists of two 23 | integers corresponding to four corners 24 | \verb{[top-left, top-right, bottom-right, bottom-left]} of the transformed 25 | image.} 26 | 27 | \item{interpolation}{(int, optional) Desired interpolation. An integer 28 | \code{0 = nearest}, \code{2 = bilinear}, and \code{3 = bicubic} or a name from 29 | \code{\link[magick:options]{magick::filter_types()}}.} 30 | 31 | \item{fill}{(int or str or tuple): Pixel fill value for constant fill. 32 | Default is 0. If a tuple of length 3, it is used to fill R, G, B channels 33 | respectively. This value is only used when the padding_mode is constant. 34 | Only int value is supported for Tensors.} 35 | } 36 | \description{ 37 | Perspective transformation of an image 38 | } 39 | \seealso{ 40 | Other transforms: 41 | \code{\link{transform_adjust_brightness}()}, 42 | \code{\link{transform_adjust_contrast}()}, 43 | \code{\link{transform_adjust_gamma}()}, 44 | \code{\link{transform_adjust_hue}()}, 45 | \code{\link{transform_adjust_saturation}()}, 46 | \code{\link{transform_affine}()}, 47 | \code{\link{transform_center_crop}()}, 48 | \code{\link{transform_color_jitter}()}, 49 | \code{\link{transform_convert_image_dtype}()}, 50 | \code{\link{transform_crop}()}, 51 | \code{\link{transform_five_crop}()}, 52 | \code{\link{transform_grayscale}()}, 53 | \code{\link{transform_hflip}()}, 54 | \code{\link{transform_linear_transformation}()}, 55 | \code{\link{transform_normalize}()}, 56 | \code{\link{transform_pad}()}, 57 | \code{\link{transform_random_affine}()}, 58 | \code{\link{transform_random_apply}()}, 59 | \code{\link{transform_random_choice}()}, 60 | \code{\link{transform_random_crop}()}, 61 | \code{\link{transform_random_erasing}()}, 62 | \code{\link{transform_random_grayscale}()}, 63 | \code{\link{transform_random_horizontal_flip}()}, 64 | \code{\link{transform_random_order}()}, 65 | \code{\link{transform_random_perspective}()}, 66 | \code{\link{transform_random_resized_crop}()}, 67 | \code{\link{transform_random_rotation}()}, 68 | \code{\link{transform_random_vertical_flip}()}, 69 | \code{\link{transform_resize}()}, 70 | \code{\link{transform_resized_crop}()}, 71 | \code{\link{transform_rgb_to_grayscale}()}, 72 | \code{\link{transform_rotate}()}, 73 | \code{\link{transform_ten_crop}()}, 74 | \code{\link{transform_to_tensor}()}, 75 | \code{\link{transform_vflip}()} 76 | } 77 | \concept{transforms} 78 | -------------------------------------------------------------------------------- /man/transform_random_affine.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_affine} 4 | \alias{transform_random_affine} 5 | \title{Random affine transformation of the image keeping center invariant} 6 | \usage{ 7 | transform_random_affine( 8 | img, 9 | degrees, 10 | translate = NULL, 11 | scale = NULL, 12 | shear = NULL, 13 | resample = 0, 14 | fillcolor = 0 15 | ) 16 | } 17 | \arguments{ 18 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 19 | 20 | \item{degrees}{(sequence or float or int): Range of degrees to select from. 21 | If degrees is a number instead of sequence like c(min, max), the range of 22 | degrees will be (-degrees, +degrees).} 23 | 24 | \item{translate}{(tuple, optional): tuple of maximum absolute fraction for 25 | horizontal and vertical translations. For example \code{translate=c(a, b)}, then 26 | horizontal shift is randomly sampled in the range 27 | -img_width * a < dx < img_width * a and vertical shift is randomly sampled 28 | in the range -img_height * b < dy < img_height * b. Will not translate by 29 | default.} 30 | 31 | \item{scale}{(tuple, optional): scaling factor interval, e.g c(a, b), then 32 | scale is randomly sampled from the range a <= scale <= b. Will keep 33 | original scale by default.} 34 | 35 | \item{shear}{(sequence or float or int, optional): Range of degrees to select 36 | from. If shear is a number, a shear parallel to the x axis in the range 37 | (-shear, +shear) will be applied. Else if shear is a tuple or list of 2 38 | values a shear parallel to the x axis in the range \verb{(shear[1], shear[2])} 39 | will be applied. Else if shear is a tuple or list of 4 values, a x-axis 40 | shear in \verb{(shear[1], shear[2])} and y-axis shear in \verb{(shear[3], shear[4])} 41 | will be applied. Will not apply shear by default.} 42 | 43 | \item{resample}{(int, optional): An optional resampling filter. See interpolation 44 | modes.} 45 | 46 | \item{fillcolor}{(tuple or int): Optional fill color (Tuple for RGB Image and 47 | int for grayscale) for the area outside the transform in the output image 48 | (Pillow>=5.0.0). This option is not supported for Tensor input. Fill value 49 | for the area outside the transform in the output image is always 0.} 50 | } 51 | \description{ 52 | Random affine transformation of the image keeping center invariant 53 | } 54 | \seealso{ 55 | Other transforms: 56 | \code{\link{transform_adjust_brightness}()}, 57 | \code{\link{transform_adjust_contrast}()}, 58 | \code{\link{transform_adjust_gamma}()}, 59 | \code{\link{transform_adjust_hue}()}, 60 | \code{\link{transform_adjust_saturation}()}, 61 | \code{\link{transform_affine}()}, 62 | \code{\link{transform_center_crop}()}, 63 | \code{\link{transform_color_jitter}()}, 64 | \code{\link{transform_convert_image_dtype}()}, 65 | \code{\link{transform_crop}()}, 66 | \code{\link{transform_five_crop}()}, 67 | \code{\link{transform_grayscale}()}, 68 | \code{\link{transform_hflip}()}, 69 | \code{\link{transform_linear_transformation}()}, 70 | \code{\link{transform_normalize}()}, 71 | \code{\link{transform_pad}()}, 72 | \code{\link{transform_perspective}()}, 73 | \code{\link{transform_random_apply}()}, 74 | \code{\link{transform_random_choice}()}, 75 | \code{\link{transform_random_crop}()}, 76 | \code{\link{transform_random_erasing}()}, 77 | \code{\link{transform_random_grayscale}()}, 78 | \code{\link{transform_random_horizontal_flip}()}, 79 | \code{\link{transform_random_order}()}, 80 | \code{\link{transform_random_perspective}()}, 81 | \code{\link{transform_random_resized_crop}()}, 82 | \code{\link{transform_random_rotation}()}, 83 | \code{\link{transform_random_vertical_flip}()}, 84 | \code{\link{transform_resize}()}, 85 | \code{\link{transform_resized_crop}()}, 86 | \code{\link{transform_rgb_to_grayscale}()}, 87 | \code{\link{transform_rotate}()}, 88 | \code{\link{transform_ten_crop}()}, 89 | \code{\link{transform_to_tensor}()}, 90 | \code{\link{transform_vflip}()} 91 | } 92 | \concept{transforms} 93 | -------------------------------------------------------------------------------- /man/transform_random_apply.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_apply} 4 | \alias{transform_random_apply} 5 | \title{Apply a list of transformations randomly with a given probability} 6 | \usage{ 7 | transform_random_apply(img, transforms, p = 0.5) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{transforms}{(list or tuple): list of transformations.} 13 | 14 | \item{p}{(float): probability.} 15 | } 16 | \description{ 17 | Apply a list of transformations randomly with a given probability 18 | } 19 | \seealso{ 20 | Other transforms: 21 | \code{\link{transform_adjust_brightness}()}, 22 | \code{\link{transform_adjust_contrast}()}, 23 | \code{\link{transform_adjust_gamma}()}, 24 | \code{\link{transform_adjust_hue}()}, 25 | \code{\link{transform_adjust_saturation}()}, 26 | \code{\link{transform_affine}()}, 27 | \code{\link{transform_center_crop}()}, 28 | \code{\link{transform_color_jitter}()}, 29 | \code{\link{transform_convert_image_dtype}()}, 30 | \code{\link{transform_crop}()}, 31 | \code{\link{transform_five_crop}()}, 32 | \code{\link{transform_grayscale}()}, 33 | \code{\link{transform_hflip}()}, 34 | \code{\link{transform_linear_transformation}()}, 35 | \code{\link{transform_normalize}()}, 36 | \code{\link{transform_pad}()}, 37 | \code{\link{transform_perspective}()}, 38 | \code{\link{transform_random_affine}()}, 39 | \code{\link{transform_random_choice}()}, 40 | \code{\link{transform_random_crop}()}, 41 | \code{\link{transform_random_erasing}()}, 42 | \code{\link{transform_random_grayscale}()}, 43 | \code{\link{transform_random_horizontal_flip}()}, 44 | \code{\link{transform_random_order}()}, 45 | \code{\link{transform_random_perspective}()}, 46 | \code{\link{transform_random_resized_crop}()}, 47 | \code{\link{transform_random_rotation}()}, 48 | \code{\link{transform_random_vertical_flip}()}, 49 | \code{\link{transform_resize}()}, 50 | \code{\link{transform_resized_crop}()}, 51 | \code{\link{transform_rgb_to_grayscale}()}, 52 | \code{\link{transform_rotate}()}, 53 | \code{\link{transform_ten_crop}()}, 54 | \code{\link{transform_to_tensor}()}, 55 | \code{\link{transform_vflip}()} 56 | } 57 | \concept{transforms} 58 | -------------------------------------------------------------------------------- /man/transform_random_choice.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_choice} 4 | \alias{transform_random_choice} 5 | \title{Apply single transformation randomly picked from a list} 6 | \usage{ 7 | transform_random_choice(img, transforms) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{transforms}{(list or tuple): list of transformations.} 13 | } 14 | \description{ 15 | Apply single transformation randomly picked from a list 16 | } 17 | \seealso{ 18 | Other transforms: 19 | \code{\link{transform_adjust_brightness}()}, 20 | \code{\link{transform_adjust_contrast}()}, 21 | \code{\link{transform_adjust_gamma}()}, 22 | \code{\link{transform_adjust_hue}()}, 23 | \code{\link{transform_adjust_saturation}()}, 24 | \code{\link{transform_affine}()}, 25 | \code{\link{transform_center_crop}()}, 26 | \code{\link{transform_color_jitter}()}, 27 | \code{\link{transform_convert_image_dtype}()}, 28 | \code{\link{transform_crop}()}, 29 | \code{\link{transform_five_crop}()}, 30 | \code{\link{transform_grayscale}()}, 31 | \code{\link{transform_hflip}()}, 32 | \code{\link{transform_linear_transformation}()}, 33 | \code{\link{transform_normalize}()}, 34 | \code{\link{transform_pad}()}, 35 | \code{\link{transform_perspective}()}, 36 | \code{\link{transform_random_affine}()}, 37 | \code{\link{transform_random_apply}()}, 38 | \code{\link{transform_random_crop}()}, 39 | \code{\link{transform_random_erasing}()}, 40 | \code{\link{transform_random_grayscale}()}, 41 | \code{\link{transform_random_horizontal_flip}()}, 42 | \code{\link{transform_random_order}()}, 43 | \code{\link{transform_random_perspective}()}, 44 | \code{\link{transform_random_resized_crop}()}, 45 | \code{\link{transform_random_rotation}()}, 46 | \code{\link{transform_random_vertical_flip}()}, 47 | \code{\link{transform_resize}()}, 48 | \code{\link{transform_resized_crop}()}, 49 | \code{\link{transform_rgb_to_grayscale}()}, 50 | \code{\link{transform_rotate}()}, 51 | \code{\link{transform_ten_crop}()}, 52 | \code{\link{transform_to_tensor}()}, 53 | \code{\link{transform_vflip}()} 54 | } 55 | \concept{transforms} 56 | -------------------------------------------------------------------------------- /man/transform_random_crop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_crop} 4 | \alias{transform_random_crop} 5 | \title{Crop the given image at a random location} 6 | \usage{ 7 | transform_random_crop( 8 | img, 9 | size, 10 | padding = NULL, 11 | pad_if_needed = FALSE, 12 | fill = 0, 13 | padding_mode = "constant" 14 | ) 15 | } 16 | \arguments{ 17 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 18 | 19 | \item{size}{(sequence or int): Desired output size. If size is a sequence 20 | like c(h, w), output size will be matched to this. If size is an int, 21 | smaller edge of the image will be matched to this number. 22 | i.e, if height > width, then image will be rescaled to 23 | (size * height / width, size).} 24 | 25 | \item{padding}{(int or tuple or list): Padding on each border. If a single 26 | int is provided this is used to pad all borders. If tuple of length 2 is 27 | provided this is the padding on left/right and top/bottom respectively. 28 | If a tuple of length 4 is provided this is the padding for the left, right, 29 | top and bottom borders respectively.} 30 | 31 | \item{pad_if_needed}{(boolean): It will pad the image if smaller than the 32 | desired size to avoid raising an exception. Since cropping is done 33 | after padding, the padding seems to be done at a random offset.} 34 | 35 | \item{fill}{(int or str or tuple): Pixel fill value for constant fill. 36 | Default is 0. If a tuple of length 3, it is used to fill R, G, B channels 37 | respectively. This value is only used when the padding_mode is constant. 38 | Only int value is supported for Tensors.} 39 | 40 | \item{padding_mode}{Type of padding. Should be: constant, edge, reflect or 41 | symmetric. Default is constant. Mode symmetric is not yet supported for 42 | Tensor inputs. 43 | \itemize{ 44 | \item constant: pads with a constant value, this value is specified with fill 45 | \item edge: pads with the last value on the edge of the image 46 | \item reflect: pads with reflection of image (without repeating the last 47 | value on the edge) padding \verb{[1, 2, 3, 4]} with 2 elements on both sides 48 | in reflect mode will result in \verb{[3, 2, 1, 2, 3, 4, 3, 2]} 49 | \item symmetric: pads with reflection of image (repeating the last value on 50 | the edge) padding \verb{[1, 2, 3, 4]} with 2 elements on both sides in 51 | symmetric mode will result in \verb{[2, 1, 1, 2, 3, 4, 4, 3]} 52 | }} 53 | } 54 | \description{ 55 | The image can be a Magick Image or a Tensor, in which case it is expected 56 | to have \verb{[..., H, W]} shape, where ... means an arbitrary number of leading 57 | dimensions. 58 | } 59 | \seealso{ 60 | Other transforms: 61 | \code{\link{transform_adjust_brightness}()}, 62 | \code{\link{transform_adjust_contrast}()}, 63 | \code{\link{transform_adjust_gamma}()}, 64 | \code{\link{transform_adjust_hue}()}, 65 | \code{\link{transform_adjust_saturation}()}, 66 | \code{\link{transform_affine}()}, 67 | \code{\link{transform_center_crop}()}, 68 | \code{\link{transform_color_jitter}()}, 69 | \code{\link{transform_convert_image_dtype}()}, 70 | \code{\link{transform_crop}()}, 71 | \code{\link{transform_five_crop}()}, 72 | \code{\link{transform_grayscale}()}, 73 | \code{\link{transform_hflip}()}, 74 | \code{\link{transform_linear_transformation}()}, 75 | \code{\link{transform_normalize}()}, 76 | \code{\link{transform_pad}()}, 77 | \code{\link{transform_perspective}()}, 78 | \code{\link{transform_random_affine}()}, 79 | \code{\link{transform_random_apply}()}, 80 | \code{\link{transform_random_choice}()}, 81 | \code{\link{transform_random_erasing}()}, 82 | \code{\link{transform_random_grayscale}()}, 83 | \code{\link{transform_random_horizontal_flip}()}, 84 | \code{\link{transform_random_order}()}, 85 | \code{\link{transform_random_perspective}()}, 86 | \code{\link{transform_random_resized_crop}()}, 87 | \code{\link{transform_random_rotation}()}, 88 | \code{\link{transform_random_vertical_flip}()}, 89 | \code{\link{transform_resize}()}, 90 | \code{\link{transform_resized_crop}()}, 91 | \code{\link{transform_rgb_to_grayscale}()}, 92 | \code{\link{transform_rotate}()}, 93 | \code{\link{transform_ten_crop}()}, 94 | \code{\link{transform_to_tensor}()}, 95 | \code{\link{transform_vflip}()} 96 | } 97 | \concept{transforms} 98 | -------------------------------------------------------------------------------- /man/transform_random_erasing.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_erasing} 4 | \alias{transform_random_erasing} 5 | \title{Randomly selects a rectangular region in an image and erases its pixel values} 6 | \usage{ 7 | transform_random_erasing( 8 | img, 9 | p = 0.5, 10 | scale = c(0.02, 0.33), 11 | ratio = c(0.3, 3.3), 12 | value = 0, 13 | inplace = FALSE 14 | ) 15 | } 16 | \arguments{ 17 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 18 | 19 | \item{p}{probability that the random erasing operation will be performed.} 20 | 21 | \item{scale}{range of proportion of erased area against input image.} 22 | 23 | \item{ratio}{range of aspect ratio of erased area.} 24 | 25 | \item{value}{erasing value. Default is 0. If a single int, it is used to 26 | erase all pixels. If a tuple of length 3, it is used to erase 27 | R, G, B channels respectively. 28 | If a str of 'random', erasing each pixel with random values.} 29 | 30 | \item{inplace}{boolean to make this transform inplace. Default set to FALSE.} 31 | } 32 | \description{ 33 | 'Random Erasing Data Augmentation' by Zhong \emph{et al.} 34 | See \url{https://arxiv.org/pdf/1708.04896} 35 | } 36 | \seealso{ 37 | Other transforms: 38 | \code{\link{transform_adjust_brightness}()}, 39 | \code{\link{transform_adjust_contrast}()}, 40 | \code{\link{transform_adjust_gamma}()}, 41 | \code{\link{transform_adjust_hue}()}, 42 | \code{\link{transform_adjust_saturation}()}, 43 | \code{\link{transform_affine}()}, 44 | \code{\link{transform_center_crop}()}, 45 | \code{\link{transform_color_jitter}()}, 46 | \code{\link{transform_convert_image_dtype}()}, 47 | \code{\link{transform_crop}()}, 48 | \code{\link{transform_five_crop}()}, 49 | \code{\link{transform_grayscale}()}, 50 | \code{\link{transform_hflip}()}, 51 | \code{\link{transform_linear_transformation}()}, 52 | \code{\link{transform_normalize}()}, 53 | \code{\link{transform_pad}()}, 54 | \code{\link{transform_perspective}()}, 55 | \code{\link{transform_random_affine}()}, 56 | \code{\link{transform_random_apply}()}, 57 | \code{\link{transform_random_choice}()}, 58 | \code{\link{transform_random_crop}()}, 59 | \code{\link{transform_random_grayscale}()}, 60 | \code{\link{transform_random_horizontal_flip}()}, 61 | \code{\link{transform_random_order}()}, 62 | \code{\link{transform_random_perspective}()}, 63 | \code{\link{transform_random_resized_crop}()}, 64 | \code{\link{transform_random_rotation}()}, 65 | \code{\link{transform_random_vertical_flip}()}, 66 | \code{\link{transform_resize}()}, 67 | \code{\link{transform_resized_crop}()}, 68 | \code{\link{transform_rgb_to_grayscale}()}, 69 | \code{\link{transform_rotate}()}, 70 | \code{\link{transform_ten_crop}()}, 71 | \code{\link{transform_to_tensor}()}, 72 | \code{\link{transform_vflip}()} 73 | } 74 | \concept{transforms} 75 | -------------------------------------------------------------------------------- /man/transform_random_grayscale.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_grayscale} 4 | \alias{transform_random_grayscale} 5 | \title{Randomly convert image to grayscale with a given probability} 6 | \usage{ 7 | transform_random_grayscale(img, p = 0.1) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{p}{(float): probability that image should be converted to grayscale 13 | (default 0.1).} 14 | } 15 | \description{ 16 | Convert image to grayscale with a probability of \code{p}. 17 | } 18 | \seealso{ 19 | Other transforms: 20 | \code{\link{transform_adjust_brightness}()}, 21 | \code{\link{transform_adjust_contrast}()}, 22 | \code{\link{transform_adjust_gamma}()}, 23 | \code{\link{transform_adjust_hue}()}, 24 | \code{\link{transform_adjust_saturation}()}, 25 | \code{\link{transform_affine}()}, 26 | \code{\link{transform_center_crop}()}, 27 | \code{\link{transform_color_jitter}()}, 28 | \code{\link{transform_convert_image_dtype}()}, 29 | \code{\link{transform_crop}()}, 30 | \code{\link{transform_five_crop}()}, 31 | \code{\link{transform_grayscale}()}, 32 | \code{\link{transform_hflip}()}, 33 | \code{\link{transform_linear_transformation}()}, 34 | \code{\link{transform_normalize}()}, 35 | \code{\link{transform_pad}()}, 36 | \code{\link{transform_perspective}()}, 37 | \code{\link{transform_random_affine}()}, 38 | \code{\link{transform_random_apply}()}, 39 | \code{\link{transform_random_choice}()}, 40 | \code{\link{transform_random_crop}()}, 41 | \code{\link{transform_random_erasing}()}, 42 | \code{\link{transform_random_horizontal_flip}()}, 43 | \code{\link{transform_random_order}()}, 44 | \code{\link{transform_random_perspective}()}, 45 | \code{\link{transform_random_resized_crop}()}, 46 | \code{\link{transform_random_rotation}()}, 47 | \code{\link{transform_random_vertical_flip}()}, 48 | \code{\link{transform_resize}()}, 49 | \code{\link{transform_resized_crop}()}, 50 | \code{\link{transform_rgb_to_grayscale}()}, 51 | \code{\link{transform_rotate}()}, 52 | \code{\link{transform_ten_crop}()}, 53 | \code{\link{transform_to_tensor}()}, 54 | \code{\link{transform_vflip}()} 55 | } 56 | \concept{transforms} 57 | -------------------------------------------------------------------------------- /man/transform_random_horizontal_flip.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_horizontal_flip} 4 | \alias{transform_random_horizontal_flip} 5 | \title{Horizontally flip an image randomly with a given probability} 6 | \usage{ 7 | transform_random_horizontal_flip(img, p = 0.5) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{p}{(float): probability of the image being flipped. 13 | Default value is 0.5} 14 | } 15 | \description{ 16 | Horizontally flip an image randomly with a given probability. The image can 17 | be a Magick Image or a torch Tensor, in which case it is expected to have 18 | \verb{[..., H, W]} shape, where ... means an arbitrary number of leading 19 | dimensions 20 | } 21 | \seealso{ 22 | Other transforms: 23 | \code{\link{transform_adjust_brightness}()}, 24 | \code{\link{transform_adjust_contrast}()}, 25 | \code{\link{transform_adjust_gamma}()}, 26 | \code{\link{transform_adjust_hue}()}, 27 | \code{\link{transform_adjust_saturation}()}, 28 | \code{\link{transform_affine}()}, 29 | \code{\link{transform_center_crop}()}, 30 | \code{\link{transform_color_jitter}()}, 31 | \code{\link{transform_convert_image_dtype}()}, 32 | \code{\link{transform_crop}()}, 33 | \code{\link{transform_five_crop}()}, 34 | \code{\link{transform_grayscale}()}, 35 | \code{\link{transform_hflip}()}, 36 | \code{\link{transform_linear_transformation}()}, 37 | \code{\link{transform_normalize}()}, 38 | \code{\link{transform_pad}()}, 39 | \code{\link{transform_perspective}()}, 40 | \code{\link{transform_random_affine}()}, 41 | \code{\link{transform_random_apply}()}, 42 | \code{\link{transform_random_choice}()}, 43 | \code{\link{transform_random_crop}()}, 44 | \code{\link{transform_random_erasing}()}, 45 | \code{\link{transform_random_grayscale}()}, 46 | \code{\link{transform_random_order}()}, 47 | \code{\link{transform_random_perspective}()}, 48 | \code{\link{transform_random_resized_crop}()}, 49 | \code{\link{transform_random_rotation}()}, 50 | \code{\link{transform_random_vertical_flip}()}, 51 | \code{\link{transform_resize}()}, 52 | \code{\link{transform_resized_crop}()}, 53 | \code{\link{transform_rgb_to_grayscale}()}, 54 | \code{\link{transform_rotate}()}, 55 | \code{\link{transform_ten_crop}()}, 56 | \code{\link{transform_to_tensor}()}, 57 | \code{\link{transform_vflip}()} 58 | } 59 | \concept{transforms} 60 | -------------------------------------------------------------------------------- /man/transform_random_order.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_order} 4 | \alias{transform_random_order} 5 | \title{Apply a list of transformations in a random order} 6 | \usage{ 7 | transform_random_order(img, transforms) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{transforms}{(list or tuple): list of transformations.} 13 | } 14 | \description{ 15 | Apply a list of transformations in a random order 16 | } 17 | \seealso{ 18 | Other transforms: 19 | \code{\link{transform_adjust_brightness}()}, 20 | \code{\link{transform_adjust_contrast}()}, 21 | \code{\link{transform_adjust_gamma}()}, 22 | \code{\link{transform_adjust_hue}()}, 23 | \code{\link{transform_adjust_saturation}()}, 24 | \code{\link{transform_affine}()}, 25 | \code{\link{transform_center_crop}()}, 26 | \code{\link{transform_color_jitter}()}, 27 | \code{\link{transform_convert_image_dtype}()}, 28 | \code{\link{transform_crop}()}, 29 | \code{\link{transform_five_crop}()}, 30 | \code{\link{transform_grayscale}()}, 31 | \code{\link{transform_hflip}()}, 32 | \code{\link{transform_linear_transformation}()}, 33 | \code{\link{transform_normalize}()}, 34 | \code{\link{transform_pad}()}, 35 | \code{\link{transform_perspective}()}, 36 | \code{\link{transform_random_affine}()}, 37 | \code{\link{transform_random_apply}()}, 38 | \code{\link{transform_random_choice}()}, 39 | \code{\link{transform_random_crop}()}, 40 | \code{\link{transform_random_erasing}()}, 41 | \code{\link{transform_random_grayscale}()}, 42 | \code{\link{transform_random_horizontal_flip}()}, 43 | \code{\link{transform_random_perspective}()}, 44 | \code{\link{transform_random_resized_crop}()}, 45 | \code{\link{transform_random_rotation}()}, 46 | \code{\link{transform_random_vertical_flip}()}, 47 | \code{\link{transform_resize}()}, 48 | \code{\link{transform_resized_crop}()}, 49 | \code{\link{transform_rgb_to_grayscale}()}, 50 | \code{\link{transform_rotate}()}, 51 | \code{\link{transform_ten_crop}()}, 52 | \code{\link{transform_to_tensor}()}, 53 | \code{\link{transform_vflip}()} 54 | } 55 | \concept{transforms} 56 | -------------------------------------------------------------------------------- /man/transform_random_perspective.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_perspective} 4 | \alias{transform_random_perspective} 5 | \title{Random perspective transformation of an image with a given probability} 6 | \usage{ 7 | transform_random_perspective( 8 | img, 9 | distortion_scale = 0.5, 10 | p = 0.5, 11 | interpolation = 2, 12 | fill = 0 13 | ) 14 | } 15 | \arguments{ 16 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 17 | 18 | \item{distortion_scale}{(float): argument to control the degree of distortion 19 | and ranges from 0 to 1. Default is 0.5.} 20 | 21 | \item{p}{(float): probability of the image being transformed. Default is 0.5.} 22 | 23 | \item{interpolation}{(int, optional) Desired interpolation. An integer 24 | \code{0 = nearest}, \code{2 = bilinear}, and \code{3 = bicubic} or a name from 25 | \code{\link[magick:options]{magick::filter_types()}}.} 26 | 27 | \item{fill}{(int or str or tuple): Pixel fill value for constant fill. 28 | Default is 0. If a tuple of length 3, it is used to fill R, G, B channels 29 | respectively. This value is only used when the padding_mode is constant. 30 | Only int value is supported for Tensors.} 31 | } 32 | \description{ 33 | Performs a random perspective transformation of the given image with a given 34 | probability 35 | } 36 | \seealso{ 37 | Other transforms: 38 | \code{\link{transform_adjust_brightness}()}, 39 | \code{\link{transform_adjust_contrast}()}, 40 | \code{\link{transform_adjust_gamma}()}, 41 | \code{\link{transform_adjust_hue}()}, 42 | \code{\link{transform_adjust_saturation}()}, 43 | \code{\link{transform_affine}()}, 44 | \code{\link{transform_center_crop}()}, 45 | \code{\link{transform_color_jitter}()}, 46 | \code{\link{transform_convert_image_dtype}()}, 47 | \code{\link{transform_crop}()}, 48 | \code{\link{transform_five_crop}()}, 49 | \code{\link{transform_grayscale}()}, 50 | \code{\link{transform_hflip}()}, 51 | \code{\link{transform_linear_transformation}()}, 52 | \code{\link{transform_normalize}()}, 53 | \code{\link{transform_pad}()}, 54 | \code{\link{transform_perspective}()}, 55 | \code{\link{transform_random_affine}()}, 56 | \code{\link{transform_random_apply}()}, 57 | \code{\link{transform_random_choice}()}, 58 | \code{\link{transform_random_crop}()}, 59 | \code{\link{transform_random_erasing}()}, 60 | \code{\link{transform_random_grayscale}()}, 61 | \code{\link{transform_random_horizontal_flip}()}, 62 | \code{\link{transform_random_order}()}, 63 | \code{\link{transform_random_resized_crop}()}, 64 | \code{\link{transform_random_rotation}()}, 65 | \code{\link{transform_random_vertical_flip}()}, 66 | \code{\link{transform_resize}()}, 67 | \code{\link{transform_resized_crop}()}, 68 | \code{\link{transform_rgb_to_grayscale}()}, 69 | \code{\link{transform_rotate}()}, 70 | \code{\link{transform_ten_crop}()}, 71 | \code{\link{transform_to_tensor}()}, 72 | \code{\link{transform_vflip}()} 73 | } 74 | \concept{transforms} 75 | -------------------------------------------------------------------------------- /man/transform_random_resized_crop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_resized_crop} 4 | \alias{transform_random_resized_crop} 5 | \title{Crop image to random size and aspect ratio} 6 | \usage{ 7 | transform_random_resized_crop( 8 | img, 9 | size, 10 | scale = c(0.08, 1), 11 | ratio = c(3/4, 4/3), 12 | interpolation = 2 13 | ) 14 | } 15 | \arguments{ 16 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 17 | 18 | \item{size}{(sequence or int): Desired output size. If size is a sequence 19 | like c(h, w), output size will be matched to this. If size is an int, 20 | smaller edge of the image will be matched to this number. 21 | i.e, if height > width, then image will be rescaled to 22 | (size * height / width, size).} 23 | 24 | \item{scale}{(tuple of float): range of size of the origin size cropped} 25 | 26 | \item{ratio}{(tuple of float): range of aspect ratio of the origin aspect 27 | ratio cropped.} 28 | 29 | \item{interpolation}{(int, optional) Desired interpolation. An integer 30 | \code{0 = nearest}, \code{2 = bilinear}, and \code{3 = bicubic} or a name from 31 | \code{\link[magick:options]{magick::filter_types()}}.} 32 | } 33 | \description{ 34 | Crop the given image to a random size and aspect ratio. The image can be a 35 | Magick Image or a Tensor, in which case it is expected to have 36 | \verb{[..., H, W]} shape, where ... means an arbitrary number of leading 37 | dimensions 38 | } 39 | \details{ 40 | A crop of random size (default: of 0.08 to 1.0) of the original size 41 | and a random aspect ratio (default: of 3/4 to 4/3) of the original aspect 42 | ratio is made. This crop is finally resized to given size. This is 43 | popularly used to train the Inception networks. 44 | } 45 | \seealso{ 46 | Other transforms: 47 | \code{\link{transform_adjust_brightness}()}, 48 | \code{\link{transform_adjust_contrast}()}, 49 | \code{\link{transform_adjust_gamma}()}, 50 | \code{\link{transform_adjust_hue}()}, 51 | \code{\link{transform_adjust_saturation}()}, 52 | \code{\link{transform_affine}()}, 53 | \code{\link{transform_center_crop}()}, 54 | \code{\link{transform_color_jitter}()}, 55 | \code{\link{transform_convert_image_dtype}()}, 56 | \code{\link{transform_crop}()}, 57 | \code{\link{transform_five_crop}()}, 58 | \code{\link{transform_grayscale}()}, 59 | \code{\link{transform_hflip}()}, 60 | \code{\link{transform_linear_transformation}()}, 61 | \code{\link{transform_normalize}()}, 62 | \code{\link{transform_pad}()}, 63 | \code{\link{transform_perspective}()}, 64 | \code{\link{transform_random_affine}()}, 65 | \code{\link{transform_random_apply}()}, 66 | \code{\link{transform_random_choice}()}, 67 | \code{\link{transform_random_crop}()}, 68 | \code{\link{transform_random_erasing}()}, 69 | \code{\link{transform_random_grayscale}()}, 70 | \code{\link{transform_random_horizontal_flip}()}, 71 | \code{\link{transform_random_order}()}, 72 | \code{\link{transform_random_perspective}()}, 73 | \code{\link{transform_random_rotation}()}, 74 | \code{\link{transform_random_vertical_flip}()}, 75 | \code{\link{transform_resize}()}, 76 | \code{\link{transform_resized_crop}()}, 77 | \code{\link{transform_rgb_to_grayscale}()}, 78 | \code{\link{transform_rotate}()}, 79 | \code{\link{transform_ten_crop}()}, 80 | \code{\link{transform_to_tensor}()}, 81 | \code{\link{transform_vflip}()} 82 | } 83 | \concept{transforms} 84 | -------------------------------------------------------------------------------- /man/transform_random_rotation.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_rotation} 4 | \alias{transform_random_rotation} 5 | \title{Rotate the image by angle} 6 | \usage{ 7 | transform_random_rotation( 8 | img, 9 | degrees, 10 | resample = 0, 11 | expand = FALSE, 12 | center = NULL, 13 | fill = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 18 | 19 | \item{degrees}{(sequence or float or int): Range of degrees to select from. 20 | If degrees is a number instead of sequence like c(min, max), the range of 21 | degrees will be (-degrees, +degrees).} 22 | 23 | \item{resample}{(int, optional): An optional resampling filter. See interpolation 24 | modes.} 25 | 26 | \item{expand}{(bool, optional): Optional expansion flag. If true, expands the 27 | output to make it large enough to hold the entire rotated image. If false 28 | or omitted, make the output image the same size as the input image. Note 29 | that the expand flag assumes rotation around the center and no translation.} 30 | 31 | \item{center}{(list or tuple, optional): Optional center of rotation, c(x, y). 32 | Origin is the upper left corner. Default is the center of the image.} 33 | 34 | \item{fill}{(n-tuple or int or float): Pixel fill value for area outside the 35 | rotated image. If int or float, the value is used for all bands 36 | respectively. Defaults to 0 for all bands. This option is only available 37 | for Pillow>=5.2.0. This option is not supported for Tensor input. Fill 38 | value for the area outside the transform in the output image is always 0.} 39 | } 40 | \description{ 41 | Rotate the image by angle 42 | } 43 | \seealso{ 44 | Other transforms: 45 | \code{\link{transform_adjust_brightness}()}, 46 | \code{\link{transform_adjust_contrast}()}, 47 | \code{\link{transform_adjust_gamma}()}, 48 | \code{\link{transform_adjust_hue}()}, 49 | \code{\link{transform_adjust_saturation}()}, 50 | \code{\link{transform_affine}()}, 51 | \code{\link{transform_center_crop}()}, 52 | \code{\link{transform_color_jitter}()}, 53 | \code{\link{transform_convert_image_dtype}()}, 54 | \code{\link{transform_crop}()}, 55 | \code{\link{transform_five_crop}()}, 56 | \code{\link{transform_grayscale}()}, 57 | \code{\link{transform_hflip}()}, 58 | \code{\link{transform_linear_transformation}()}, 59 | \code{\link{transform_normalize}()}, 60 | \code{\link{transform_pad}()}, 61 | \code{\link{transform_perspective}()}, 62 | \code{\link{transform_random_affine}()}, 63 | \code{\link{transform_random_apply}()}, 64 | \code{\link{transform_random_choice}()}, 65 | \code{\link{transform_random_crop}()}, 66 | \code{\link{transform_random_erasing}()}, 67 | \code{\link{transform_random_grayscale}()}, 68 | \code{\link{transform_random_horizontal_flip}()}, 69 | \code{\link{transform_random_order}()}, 70 | \code{\link{transform_random_perspective}()}, 71 | \code{\link{transform_random_resized_crop}()}, 72 | \code{\link{transform_random_vertical_flip}()}, 73 | \code{\link{transform_resize}()}, 74 | \code{\link{transform_resized_crop}()}, 75 | \code{\link{transform_rgb_to_grayscale}()}, 76 | \code{\link{transform_rotate}()}, 77 | \code{\link{transform_ten_crop}()}, 78 | \code{\link{transform_to_tensor}()}, 79 | \code{\link{transform_vflip}()} 80 | } 81 | \concept{transforms} 82 | -------------------------------------------------------------------------------- /man/transform_random_vertical_flip.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_random_vertical_flip} 4 | \alias{transform_random_vertical_flip} 5 | \title{Vertically flip an image randomly with a given probability} 6 | \usage{ 7 | transform_random_vertical_flip(img, p = 0.5) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{p}{(float): probability of the image being flipped. 13 | Default value is 0.5} 14 | } 15 | \description{ 16 | The image can be a PIL Image or a torch Tensor, in which case it is expected 17 | to have \verb{[..., H, W]} shape, where \code{...} means an arbitrary number of 18 | leading dimensions 19 | } 20 | \seealso{ 21 | Other transforms: 22 | \code{\link{transform_adjust_brightness}()}, 23 | \code{\link{transform_adjust_contrast}()}, 24 | \code{\link{transform_adjust_gamma}()}, 25 | \code{\link{transform_adjust_hue}()}, 26 | \code{\link{transform_adjust_saturation}()}, 27 | \code{\link{transform_affine}()}, 28 | \code{\link{transform_center_crop}()}, 29 | \code{\link{transform_color_jitter}()}, 30 | \code{\link{transform_convert_image_dtype}()}, 31 | \code{\link{transform_crop}()}, 32 | \code{\link{transform_five_crop}()}, 33 | \code{\link{transform_grayscale}()}, 34 | \code{\link{transform_hflip}()}, 35 | \code{\link{transform_linear_transformation}()}, 36 | \code{\link{transform_normalize}()}, 37 | \code{\link{transform_pad}()}, 38 | \code{\link{transform_perspective}()}, 39 | \code{\link{transform_random_affine}()}, 40 | \code{\link{transform_random_apply}()}, 41 | \code{\link{transform_random_choice}()}, 42 | \code{\link{transform_random_crop}()}, 43 | \code{\link{transform_random_erasing}()}, 44 | \code{\link{transform_random_grayscale}()}, 45 | \code{\link{transform_random_horizontal_flip}()}, 46 | \code{\link{transform_random_order}()}, 47 | \code{\link{transform_random_perspective}()}, 48 | \code{\link{transform_random_resized_crop}()}, 49 | \code{\link{transform_random_rotation}()}, 50 | \code{\link{transform_resize}()}, 51 | \code{\link{transform_resized_crop}()}, 52 | \code{\link{transform_rgb_to_grayscale}()}, 53 | \code{\link{transform_rotate}()}, 54 | \code{\link{transform_ten_crop}()}, 55 | \code{\link{transform_to_tensor}()}, 56 | \code{\link{transform_vflip}()} 57 | } 58 | \concept{transforms} 59 | -------------------------------------------------------------------------------- /man/transform_resize.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_resize} 4 | \alias{transform_resize} 5 | \title{Resize the input image to the given size} 6 | \usage{ 7 | transform_resize(img, size, interpolation = 2) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{size}{(sequence or int): Desired output size. If size is a sequence 13 | like c(h, w), output size will be matched to this. If size is an int, 14 | smaller edge of the image will be matched to this number. 15 | i.e, if height > width, then image will be rescaled to 16 | (size * height / width, size).} 17 | 18 | \item{interpolation}{(int, optional) Desired interpolation. An integer 19 | \code{0 = nearest}, \code{2 = bilinear}, and \code{3 = bicubic} or a name from 20 | \code{\link[magick:options]{magick::filter_types()}}.} 21 | } 22 | \description{ 23 | The image can be a Magic Image or a torch Tensor, in which case it is 24 | expected to have \verb{[..., H, W]} shape, where ... means an arbitrary number 25 | of leading dimensions 26 | } 27 | \seealso{ 28 | Other transforms: 29 | \code{\link{transform_adjust_brightness}()}, 30 | \code{\link{transform_adjust_contrast}()}, 31 | \code{\link{transform_adjust_gamma}()}, 32 | \code{\link{transform_adjust_hue}()}, 33 | \code{\link{transform_adjust_saturation}()}, 34 | \code{\link{transform_affine}()}, 35 | \code{\link{transform_center_crop}()}, 36 | \code{\link{transform_color_jitter}()}, 37 | \code{\link{transform_convert_image_dtype}()}, 38 | \code{\link{transform_crop}()}, 39 | \code{\link{transform_five_crop}()}, 40 | \code{\link{transform_grayscale}()}, 41 | \code{\link{transform_hflip}()}, 42 | \code{\link{transform_linear_transformation}()}, 43 | \code{\link{transform_normalize}()}, 44 | \code{\link{transform_pad}()}, 45 | \code{\link{transform_perspective}()}, 46 | \code{\link{transform_random_affine}()}, 47 | \code{\link{transform_random_apply}()}, 48 | \code{\link{transform_random_choice}()}, 49 | \code{\link{transform_random_crop}()}, 50 | \code{\link{transform_random_erasing}()}, 51 | \code{\link{transform_random_grayscale}()}, 52 | \code{\link{transform_random_horizontal_flip}()}, 53 | \code{\link{transform_random_order}()}, 54 | \code{\link{transform_random_perspective}()}, 55 | \code{\link{transform_random_resized_crop}()}, 56 | \code{\link{transform_random_rotation}()}, 57 | \code{\link{transform_random_vertical_flip}()}, 58 | \code{\link{transform_resized_crop}()}, 59 | \code{\link{transform_rgb_to_grayscale}()}, 60 | \code{\link{transform_rotate}()}, 61 | \code{\link{transform_ten_crop}()}, 62 | \code{\link{transform_to_tensor}()}, 63 | \code{\link{transform_vflip}()} 64 | } 65 | \concept{transforms} 66 | -------------------------------------------------------------------------------- /man/transform_resized_crop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_resized_crop} 4 | \alias{transform_resized_crop} 5 | \title{Crop an image and resize it to a desired size} 6 | \usage{ 7 | transform_resized_crop(img, top, left, height, width, size, interpolation = 2) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{top}{(int): Vertical component of the top left corner of the crop box.} 13 | 14 | \item{left}{(int): Horizontal component of the top left corner of the crop 15 | box.} 16 | 17 | \item{height}{(int): Height of the crop box.} 18 | 19 | \item{width}{(int): Width of the crop box.} 20 | 21 | \item{size}{(sequence or int): Desired output size. If size is a sequence 22 | like c(h, w), output size will be matched to this. If size is an int, 23 | smaller edge of the image will be matched to this number. 24 | i.e, if height > width, then image will be rescaled to 25 | (size * height / width, size).} 26 | 27 | \item{interpolation}{(int, optional) Desired interpolation. An integer 28 | \code{0 = nearest}, \code{2 = bilinear}, and \code{3 = bicubic} or a name from 29 | \code{\link[magick:options]{magick::filter_types()}}.} 30 | } 31 | \description{ 32 | Crop an image and resize it to a desired size 33 | } 34 | \seealso{ 35 | Other transforms: 36 | \code{\link{transform_adjust_brightness}()}, 37 | \code{\link{transform_adjust_contrast}()}, 38 | \code{\link{transform_adjust_gamma}()}, 39 | \code{\link{transform_adjust_hue}()}, 40 | \code{\link{transform_adjust_saturation}()}, 41 | \code{\link{transform_affine}()}, 42 | \code{\link{transform_center_crop}()}, 43 | \code{\link{transform_color_jitter}()}, 44 | \code{\link{transform_convert_image_dtype}()}, 45 | \code{\link{transform_crop}()}, 46 | \code{\link{transform_five_crop}()}, 47 | \code{\link{transform_grayscale}()}, 48 | \code{\link{transform_hflip}()}, 49 | \code{\link{transform_linear_transformation}()}, 50 | \code{\link{transform_normalize}()}, 51 | \code{\link{transform_pad}()}, 52 | \code{\link{transform_perspective}()}, 53 | \code{\link{transform_random_affine}()}, 54 | \code{\link{transform_random_apply}()}, 55 | \code{\link{transform_random_choice}()}, 56 | \code{\link{transform_random_crop}()}, 57 | \code{\link{transform_random_erasing}()}, 58 | \code{\link{transform_random_grayscale}()}, 59 | \code{\link{transform_random_horizontal_flip}()}, 60 | \code{\link{transform_random_order}()}, 61 | \code{\link{transform_random_perspective}()}, 62 | \code{\link{transform_random_resized_crop}()}, 63 | \code{\link{transform_random_rotation}()}, 64 | \code{\link{transform_random_vertical_flip}()}, 65 | \code{\link{transform_resize}()}, 66 | \code{\link{transform_rgb_to_grayscale}()}, 67 | \code{\link{transform_rotate}()}, 68 | \code{\link{transform_ten_crop}()}, 69 | \code{\link{transform_to_tensor}()}, 70 | \code{\link{transform_vflip}()} 71 | } 72 | \concept{transforms} 73 | -------------------------------------------------------------------------------- /man/transform_rgb_to_grayscale.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_rgb_to_grayscale} 4 | \alias{transform_rgb_to_grayscale} 5 | \title{Convert RGB Image Tensor to Grayscale} 6 | \usage{ 7 | transform_rgb_to_grayscale(img) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | } 12 | \description{ 13 | For RGB to Grayscale conversion, ITU-R 601-2 luma transform is performed 14 | which is L = R * 0.2989 + G * 0.5870 + B * 0.1140 15 | } 16 | \seealso{ 17 | Other transforms: 18 | \code{\link{transform_adjust_brightness}()}, 19 | \code{\link{transform_adjust_contrast}()}, 20 | \code{\link{transform_adjust_gamma}()}, 21 | \code{\link{transform_adjust_hue}()}, 22 | \code{\link{transform_adjust_saturation}()}, 23 | \code{\link{transform_affine}()}, 24 | \code{\link{transform_center_crop}()}, 25 | \code{\link{transform_color_jitter}()}, 26 | \code{\link{transform_convert_image_dtype}()}, 27 | \code{\link{transform_crop}()}, 28 | \code{\link{transform_five_crop}()}, 29 | \code{\link{transform_grayscale}()}, 30 | \code{\link{transform_hflip}()}, 31 | \code{\link{transform_linear_transformation}()}, 32 | \code{\link{transform_normalize}()}, 33 | \code{\link{transform_pad}()}, 34 | \code{\link{transform_perspective}()}, 35 | \code{\link{transform_random_affine}()}, 36 | \code{\link{transform_random_apply}()}, 37 | \code{\link{transform_random_choice}()}, 38 | \code{\link{transform_random_crop}()}, 39 | \code{\link{transform_random_erasing}()}, 40 | \code{\link{transform_random_grayscale}()}, 41 | \code{\link{transform_random_horizontal_flip}()}, 42 | \code{\link{transform_random_order}()}, 43 | \code{\link{transform_random_perspective}()}, 44 | \code{\link{transform_random_resized_crop}()}, 45 | \code{\link{transform_random_rotation}()}, 46 | \code{\link{transform_random_vertical_flip}()}, 47 | \code{\link{transform_resize}()}, 48 | \code{\link{transform_resized_crop}()}, 49 | \code{\link{transform_rotate}()}, 50 | \code{\link{transform_ten_crop}()}, 51 | \code{\link{transform_to_tensor}()}, 52 | \code{\link{transform_vflip}()} 53 | } 54 | \concept{transforms} 55 | -------------------------------------------------------------------------------- /man/transform_rotate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_rotate} 4 | \alias{transform_rotate} 5 | \title{Angular rotation of an image} 6 | \usage{ 7 | transform_rotate( 8 | img, 9 | angle, 10 | resample = 0, 11 | expand = FALSE, 12 | center = NULL, 13 | fill = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 18 | 19 | \item{angle}{(float or int): rotation angle value in degrees, 20 | counter-clockwise.} 21 | 22 | \item{resample}{(int, optional): An optional resampling filter. See interpolation 23 | modes.} 24 | 25 | \item{expand}{(bool, optional): Optional expansion flag. If true, expands the 26 | output to make it large enough to hold the entire rotated image. If false 27 | or omitted, make the output image the same size as the input image. Note 28 | that the expand flag assumes rotation around the center and no translation.} 29 | 30 | \item{center}{(list or tuple, optional): Optional center of rotation, c(x, y). 31 | Origin is the upper left corner. Default is the center of the image.} 32 | 33 | \item{fill}{(n-tuple or int or float): Pixel fill value for area outside the 34 | rotated image. If int or float, the value is used for all bands 35 | respectively. Defaults to 0 for all bands. This option is only available 36 | for Pillow>=5.2.0. This option is not supported for Tensor input. Fill 37 | value for the area outside the transform in the output image is always 0.} 38 | } 39 | \description{ 40 | Angular rotation of an image 41 | } 42 | \seealso{ 43 | Other transforms: 44 | \code{\link{transform_adjust_brightness}()}, 45 | \code{\link{transform_adjust_contrast}()}, 46 | \code{\link{transform_adjust_gamma}()}, 47 | \code{\link{transform_adjust_hue}()}, 48 | \code{\link{transform_adjust_saturation}()}, 49 | \code{\link{transform_affine}()}, 50 | \code{\link{transform_center_crop}()}, 51 | \code{\link{transform_color_jitter}()}, 52 | \code{\link{transform_convert_image_dtype}()}, 53 | \code{\link{transform_crop}()}, 54 | \code{\link{transform_five_crop}()}, 55 | \code{\link{transform_grayscale}()}, 56 | \code{\link{transform_hflip}()}, 57 | \code{\link{transform_linear_transformation}()}, 58 | \code{\link{transform_normalize}()}, 59 | \code{\link{transform_pad}()}, 60 | \code{\link{transform_perspective}()}, 61 | \code{\link{transform_random_affine}()}, 62 | \code{\link{transform_random_apply}()}, 63 | \code{\link{transform_random_choice}()}, 64 | \code{\link{transform_random_crop}()}, 65 | \code{\link{transform_random_erasing}()}, 66 | \code{\link{transform_random_grayscale}()}, 67 | \code{\link{transform_random_horizontal_flip}()}, 68 | \code{\link{transform_random_order}()}, 69 | \code{\link{transform_random_perspective}()}, 70 | \code{\link{transform_random_resized_crop}()}, 71 | \code{\link{transform_random_rotation}()}, 72 | \code{\link{transform_random_vertical_flip}()}, 73 | \code{\link{transform_resize}()}, 74 | \code{\link{transform_resized_crop}()}, 75 | \code{\link{transform_rgb_to_grayscale}()}, 76 | \code{\link{transform_ten_crop}()}, 77 | \code{\link{transform_to_tensor}()}, 78 | \code{\link{transform_vflip}()} 79 | } 80 | \concept{transforms} 81 | -------------------------------------------------------------------------------- /man/transform_ten_crop.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_ten_crop} 4 | \alias{transform_ten_crop} 5 | \title{Crop an image and the flipped image each into four corners and a central crop} 6 | \usage{ 7 | transform_ten_crop(img, size, vertical_flip = FALSE) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | 12 | \item{size}{(sequence or int): Desired output size. If size is a sequence 13 | like c(h, w), output size will be matched to this. If size is an int, 14 | smaller edge of the image will be matched to this number. 15 | i.e, if height > width, then image will be rescaled to 16 | (size * height / width, size).} 17 | 18 | \item{vertical_flip}{(bool): Use vertical flipping instead of horizontal} 19 | } 20 | \description{ 21 | Crop the given image into four corners and the central crop, plus the flipped 22 | version of these (horizontal flipping is used by default). This transform 23 | returns a tuple of images and there may be a mismatch in the number of 24 | inputs and targets your Dataset returns. 25 | } 26 | \seealso{ 27 | Other transforms: 28 | \code{\link{transform_adjust_brightness}()}, 29 | \code{\link{transform_adjust_contrast}()}, 30 | \code{\link{transform_adjust_gamma}()}, 31 | \code{\link{transform_adjust_hue}()}, 32 | \code{\link{transform_adjust_saturation}()}, 33 | \code{\link{transform_affine}()}, 34 | \code{\link{transform_center_crop}()}, 35 | \code{\link{transform_color_jitter}()}, 36 | \code{\link{transform_convert_image_dtype}()}, 37 | \code{\link{transform_crop}()}, 38 | \code{\link{transform_five_crop}()}, 39 | \code{\link{transform_grayscale}()}, 40 | \code{\link{transform_hflip}()}, 41 | \code{\link{transform_linear_transformation}()}, 42 | \code{\link{transform_normalize}()}, 43 | \code{\link{transform_pad}()}, 44 | \code{\link{transform_perspective}()}, 45 | \code{\link{transform_random_affine}()}, 46 | \code{\link{transform_random_apply}()}, 47 | \code{\link{transform_random_choice}()}, 48 | \code{\link{transform_random_crop}()}, 49 | \code{\link{transform_random_erasing}()}, 50 | \code{\link{transform_random_grayscale}()}, 51 | \code{\link{transform_random_horizontal_flip}()}, 52 | \code{\link{transform_random_order}()}, 53 | \code{\link{transform_random_perspective}()}, 54 | \code{\link{transform_random_resized_crop}()}, 55 | \code{\link{transform_random_rotation}()}, 56 | \code{\link{transform_random_vertical_flip}()}, 57 | \code{\link{transform_resize}()}, 58 | \code{\link{transform_resized_crop}()}, 59 | \code{\link{transform_rgb_to_grayscale}()}, 60 | \code{\link{transform_rotate}()}, 61 | \code{\link{transform_to_tensor}()}, 62 | \code{\link{transform_vflip}()} 63 | } 64 | \concept{transforms} 65 | -------------------------------------------------------------------------------- /man/transform_to_tensor.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_to_tensor} 4 | \alias{transform_to_tensor} 5 | \title{Convert an image to a tensor} 6 | \usage{ 7 | transform_to_tensor(img) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | } 12 | \description{ 13 | Converts a Magick Image or array (H x W x C) in the range \verb{[0, 255]} to a 14 | \code{torch_tensor} of shape (C x H x W) in the range \verb{[0.0, 1.0]}. In the 15 | other cases, tensors are returned without scaling. 16 | } 17 | \note{ 18 | Because the input image is scaled to \verb{[0.0, 1.0]}, this transformation 19 | should not be used when transforming target image masks. 20 | } 21 | \seealso{ 22 | Other transforms: 23 | \code{\link{transform_adjust_brightness}()}, 24 | \code{\link{transform_adjust_contrast}()}, 25 | \code{\link{transform_adjust_gamma}()}, 26 | \code{\link{transform_adjust_hue}()}, 27 | \code{\link{transform_adjust_saturation}()}, 28 | \code{\link{transform_affine}()}, 29 | \code{\link{transform_center_crop}()}, 30 | \code{\link{transform_color_jitter}()}, 31 | \code{\link{transform_convert_image_dtype}()}, 32 | \code{\link{transform_crop}()}, 33 | \code{\link{transform_five_crop}()}, 34 | \code{\link{transform_grayscale}()}, 35 | \code{\link{transform_hflip}()}, 36 | \code{\link{transform_linear_transformation}()}, 37 | \code{\link{transform_normalize}()}, 38 | \code{\link{transform_pad}()}, 39 | \code{\link{transform_perspective}()}, 40 | \code{\link{transform_random_affine}()}, 41 | \code{\link{transform_random_apply}()}, 42 | \code{\link{transform_random_choice}()}, 43 | \code{\link{transform_random_crop}()}, 44 | \code{\link{transform_random_erasing}()}, 45 | \code{\link{transform_random_grayscale}()}, 46 | \code{\link{transform_random_horizontal_flip}()}, 47 | \code{\link{transform_random_order}()}, 48 | \code{\link{transform_random_perspective}()}, 49 | \code{\link{transform_random_resized_crop}()}, 50 | \code{\link{transform_random_rotation}()}, 51 | \code{\link{transform_random_vertical_flip}()}, 52 | \code{\link{transform_resize}()}, 53 | \code{\link{transform_resized_crop}()}, 54 | \code{\link{transform_rgb_to_grayscale}()}, 55 | \code{\link{transform_rotate}()}, 56 | \code{\link{transform_ten_crop}()}, 57 | \code{\link{transform_vflip}()} 58 | } 59 | \concept{transforms} 60 | -------------------------------------------------------------------------------- /man/transform_vflip.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transforms-generics.R 3 | \name{transform_vflip} 4 | \alias{transform_vflip} 5 | \title{Vertically flip a PIL Image or Tensor} 6 | \usage{ 7 | transform_vflip(img) 8 | } 9 | \arguments{ 10 | \item{img}{A \code{magick-image}, \code{array} or \code{torch_tensor}.} 11 | } 12 | \description{ 13 | Vertically flip a PIL Image or Tensor 14 | } 15 | \seealso{ 16 | Other transforms: 17 | \code{\link{transform_adjust_brightness}()}, 18 | \code{\link{transform_adjust_contrast}()}, 19 | \code{\link{transform_adjust_gamma}()}, 20 | \code{\link{transform_adjust_hue}()}, 21 | \code{\link{transform_adjust_saturation}()}, 22 | \code{\link{transform_affine}()}, 23 | \code{\link{transform_center_crop}()}, 24 | \code{\link{transform_color_jitter}()}, 25 | \code{\link{transform_convert_image_dtype}()}, 26 | \code{\link{transform_crop}()}, 27 | \code{\link{transform_five_crop}()}, 28 | \code{\link{transform_grayscale}()}, 29 | \code{\link{transform_hflip}()}, 30 | \code{\link{transform_linear_transformation}()}, 31 | \code{\link{transform_normalize}()}, 32 | \code{\link{transform_pad}()}, 33 | \code{\link{transform_perspective}()}, 34 | \code{\link{transform_random_affine}()}, 35 | \code{\link{transform_random_apply}()}, 36 | \code{\link{transform_random_choice}()}, 37 | \code{\link{transform_random_crop}()}, 38 | \code{\link{transform_random_erasing}()}, 39 | \code{\link{transform_random_grayscale}()}, 40 | \code{\link{transform_random_horizontal_flip}()}, 41 | \code{\link{transform_random_order}()}, 42 | \code{\link{transform_random_perspective}()}, 43 | \code{\link{transform_random_resized_crop}()}, 44 | \code{\link{transform_random_rotation}()}, 45 | \code{\link{transform_random_vertical_flip}()}, 46 | \code{\link{transform_resize}()}, 47 | \code{\link{transform_resized_crop}()}, 48 | \code{\link{transform_rgb_to_grayscale}()}, 49 | \code{\link{transform_rotate}()}, 50 | \code{\link{transform_ten_crop}()}, 51 | \code{\link{transform_to_tensor}()} 52 | } 53 | \concept{transforms} 54 | -------------------------------------------------------------------------------- /man/vision_make_grid.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/vision_utils.R 3 | \name{vision_make_grid} 4 | \alias{vision_make_grid} 5 | \title{A simplified version of torchvision.utils.make_grid} 6 | \usage{ 7 | vision_make_grid( 8 | tensor, 9 | scale = TRUE, 10 | num_rows = 8, 11 | padding = 2, 12 | pad_value = 0 13 | ) 14 | } 15 | \arguments{ 16 | \item{tensor}{tensor to arrange in grid.} 17 | 18 | \item{scale}{whether to normalize (min-max-scale) the input tensor.} 19 | 20 | \item{num_rows}{number of rows making up the grid (default 8).} 21 | 22 | \item{padding}{amount of padding between batch images (default 2).} 23 | 24 | \item{pad_value}{pixel value to use for padding.} 25 | } 26 | \description{ 27 | Arranges a batch of (image) tensors in a grid, with optional padding between 28 | images. Expects a 4d mini-batch tensor of shape (B x C x H x W). 29 | } 30 | \seealso{ 31 | Other image display: 32 | \code{\link{draw_bounding_boxes}()}, 33 | \code{\link{draw_keypoints}()}, 34 | \code{\link{draw_segmentation_masks}()}, 35 | \code{\link{tensor_image_browse}()}, 36 | \code{\link{tensor_image_display}()} 37 | } 38 | \concept{image display} 39 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(torchvision) 3 | 4 | if (Sys.getenv("TORCH_TEST", unset = 0) == 1) 5 | test_check("torchvision") 6 | 7 | -------------------------------------------------------------------------------- /tests/testthat/assets/class/cat/cat.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/cat/cat.0.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/cat/cat.1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/cat/cat.1.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/cat/cat.2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/cat/cat.2.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/cat/cat.3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/cat/cat.3.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/cat/cat.4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/cat/cat.4.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/cat/cat.5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/cat/cat.5.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/cat/cat.txt: -------------------------------------------------------------------------------- 1 | test file to see if it's ignored by the dataset 2 | -------------------------------------------------------------------------------- /tests/testthat/assets/class/dog/dog.0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/dog/dog.0.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/dog/dog.1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/dog/dog.1.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/dog/dog.2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/dog/dog.2.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/dog/dog.3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/dog/dog.3.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/dog/dog.4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/dog/dog.4.jpg -------------------------------------------------------------------------------- /tests/testthat/assets/class/dog/dog.5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/assets/class/dog/dog.5.jpg -------------------------------------------------------------------------------- /tests/testthat/helper-torchvision.R: -------------------------------------------------------------------------------- 1 | library(torch) 2 | 3 | is_torch_tensor <- function(x) { 4 | inherits(x, "torch_tensor") 5 | } 6 | 7 | expect_no_error <- function(object, ...) { 8 | expect_error(object, NA, ...) 9 | } 10 | 11 | expect_tensor_shape <- function(object, expected) { 12 | expect_tensor(object) 13 | expect_equal(object$shape, expected) 14 | } 15 | 16 | expect_tensor_dtype <- function(object, expected_dtype) { 17 | expect_tensor(object) 18 | expect_true(object$dtype == expected_dtype) 19 | } 20 | 21 | expect_tensor <- function(object) { 22 | expect_true(is_torch_tensor(object)) 23 | expect_no_error(torch::as_array(object)) 24 | } 25 | 26 | expect_equal_to_r <- function(object, expected, ...) { 27 | expect_equal(torch::as_array(object), expected, ...) 28 | } 29 | -------------------------------------------------------------------------------- /tests/testthat/test-dataset-cifar.R: -------------------------------------------------------------------------------- 1 | test_that("cifar10", { 2 | 3 | t <- tempfile() 4 | 5 | expect_error( 6 | ds <- cifar10_dataset(root = t, train = TRUE), 7 | class = "runtime_error" 8 | ) 9 | 10 | ds <- cifar10_dataset(root = t, train = TRUE, download = TRUE) 11 | expect_equal(length(ds), 50000) 12 | el <- ds[1] 13 | expect_equal(dim(el[[1]]), c(32, 32, 3)) 14 | expect_equal(length(el[[2]]), 1) 15 | expect_named(el, c("x", "y")) 16 | expect_equal(ds$classes[el[[2]]], "frog") 17 | 18 | ds <- cifar10_dataset(root = t, train = FALSE, download = TRUE) 19 | expect_equal(length(ds), 10000) 20 | el <- ds[1] 21 | expect_equal(dim(el[[1]]), c(32, 32, 3)) 22 | expect_equal(length(el[[2]]), 1) 23 | expect_named(el, c("x", "y")) 24 | 25 | }) 26 | 27 | test_that("cifar100", { 28 | 29 | t <- tempfile() 30 | 31 | expect_error( 32 | ds <- cifar100_dataset(root = t, train = TRUE), 33 | class = "runtime_error" 34 | ) 35 | 36 | ds <- cifar100_dataset(root = t, train = TRUE, download = TRUE) 37 | expect_equal(length(ds), 50000) 38 | el <- ds[2500] 39 | expect_equal(dim(el[[1]]), c(32, 32, 3)) 40 | expect_equal(length(el[[2]]), 1) 41 | expect_named(el, c("x", "y")) 42 | expect_equal(ds$classes[el[[2]]], "motorcycle") 43 | 44 | ds <- cifar100_dataset(root = t, train = FALSE, download = TRUE) 45 | expect_equal(length(ds), 10000) 46 | el <- ds[502] 47 | expect_equal(dim(el[[1]]), c(32, 32, 3)) 48 | expect_equal(length(el[[2]]), 1) 49 | expect_named(el, c("x", "y")) 50 | expect_equal(ds$classes[el[[2]]], "mouse") 51 | 52 | }) 53 | -------------------------------------------------------------------------------- /tests/testthat/test-dataset-mnist.R: -------------------------------------------------------------------------------- 1 | context("dataset-mnist") 2 | 3 | test_that("tests for the mnist dataset", { 4 | 5 | dir <- tempfile(fileext = "/") 6 | 7 | expect_error( 8 | ds <- mnist_dataset(dir) 9 | ) 10 | 11 | ds <- mnist_dataset(dir, download = TRUE) 12 | 13 | i <- ds[1] 14 | expect_equal(dim(i[[1]]), c(28, 28)) 15 | expect_equal(i[[2]], 6) 16 | expect_equal(length(ds), 60000) 17 | 18 | ds <- mnist_dataset(dir, transform = transform_to_tensor) 19 | dl <- torch::dataloader(ds, batch_size = 32) 20 | expect_length(dl, 1875) 21 | iter <- dataloader_make_iter(dl) 22 | i <- dataloader_next(iter) 23 | expect_tensor_shape(i[[1]], c(32, 1, 28, 28)) 24 | expect_tensor_shape(i[[2]], 32) 25 | expect_true((torch_max(i[[1]]) <= 1)$item()) 26 | expect_named(i, c("x", "y")) 27 | 28 | }) 29 | 30 | 31 | test_that("tests for the kmnist dataset", { 32 | 33 | dir <- tempfile(fileext = "/") 34 | 35 | expect_error( 36 | ds <- kmnist_dataset(dir) 37 | ) 38 | 39 | ds <- kmnist_dataset(dir, download = TRUE) 40 | 41 | i <- ds[1] 42 | expect_equal(dim(i[[1]]), c(28, 28)) 43 | expect_equal(i[[2]], 9) 44 | expect_equal(length(ds), 60000) 45 | 46 | ds <- kmnist_dataset(dir, transform = transform_to_tensor) 47 | dl <- torch::dataloader(ds, batch_size = 32) 48 | expect_length(dl, 1875) 49 | iter <- dataloader_make_iter(dl) 50 | i <- dataloader_next(iter) 51 | expect_tensor_shape(i[[1]], c(32, 1, 28, 28)) 52 | expect_tensor_shape(i[[2]], 32) 53 | expect_true((torch_max(i[[1]]) <= 1)$item()) 54 | expect_named(i, c("x", "y")) 55 | }) 56 | 57 | 58 | test_that("fashion_mnist_dataset loads correctly", { 59 | dir <- tempfile() 60 | 61 | ds <- fashion_mnist_dataset( 62 | root = dir, 63 | train = TRUE, 64 | download = TRUE 65 | ) 66 | 67 | expect_s3_class(ds, "fashion_mnist_dataset") 68 | expect_type(ds$.getitem(1), "list") 69 | expect_named(ds$.getitem(1), c("x", "y")) 70 | expect_equal(dim(as.array(ds$.getitem(1)$x)), c(28, 28)) 71 | expect_true(ds$.getitem(1)$y >= 1 && ds$.getitem(1)$y <= 10) 72 | 73 | ds2 <- fashion_mnist_dataset(dir, transform = transform_to_tensor) 74 | dl <- torch::dataloader(ds2, batch_size = 32) 75 | iter <- dataloader_make_iter(dl) 76 | batch <- dataloader_next(iter) 77 | expect_tensor_shape(batch$x, c(32, 1, 28, 28)) 78 | expect_tensor_shape(batch$y, 32) 79 | expect_named(batch, c("x", "y")) 80 | }) 81 | -------------------------------------------------------------------------------- /tests/testthat/test-folder-dataset.R: -------------------------------------------------------------------------------- 1 | test_that("image_folder dataset", { 2 | 3 | ds <- image_folder_dataset( 4 | root = "assets/class", 5 | transform = . %>% transform_to_tensor %>% 6 | transform_resize(c(32,32)) 7 | ) 8 | expect_length(ds[1], 2) 9 | 10 | dl <- torch::dataloader(ds, batch_size = 2, drop_last = TRUE) 11 | coro::loop(for(batch in dl) { 12 | expect_tensor_shape(batch[[1]], c(2, 3, 32, 32)) 13 | expect_tensor_shape(batch[[2]], 2) 14 | expect_tensor_shape(batch$x, c(2, 3, 32, 32)) 15 | expect_tensor_shape(batch$y, 2) 16 | }) 17 | 18 | expect_length(ds, 12) 19 | 20 | }) 21 | -------------------------------------------------------------------------------- /tests/testthat/test-message-translations.R: -------------------------------------------------------------------------------- 1 | test_that("R-level type_error messages are correctly translated in FR", { 2 | withr::with_language(lang = "fr", 3 | expect_error( 4 | transform_adjust_gamma(torch::torch_rand_like(c(3, 5, 5)), gamma = 0.5), 5 | regexp = "Le tenseur n'est pas une image torch", 6 | fixed = TRUE 7 | ) 8 | ) 9 | }) 10 | 11 | test_that("R-level cli_warning messages are correctly translated in FR", { 12 | withr::with_language(lang = "fr", 13 | expect_warning( 14 | torchvision:::Inception3(), 15 | regexp = "L'initialisation des poids par défaut de inception_v3", 16 | fixed = TRUE 17 | ) 18 | ) 19 | }) 20 | 21 | test_that("R-level value_error messages are glued and correctly translated in FR", { 22 | withr::with_language(lang = "fr", 23 | expect_error( 24 | transform_normalize(torch::torch_rand(c(3,5,5)), 3, 0), 25 | regexp = "Après conversion en Float,", 26 | fixed = TRUE 27 | ) 28 | ) 29 | }) 30 | 31 | -------------------------------------------------------------------------------- /tests/testthat/test-models-alexnet.R: -------------------------------------------------------------------------------- 1 | test_that("alexnet", { 2 | 3 | m <- model_alexnet() 4 | input <- torch::torch_randn(1, 3, 256, 256) 5 | 6 | out <- m(input) 7 | 8 | expect_tensor_shape(out, c(1, 1000)) 9 | 10 | m <- model_alexnet(pretrained = TRUE) 11 | input <- torch::torch_randn(1, 3, 256, 256) 12 | 13 | out <- m(input) 14 | 15 | expect_tensor_shape(out, c(1, 1000)) 16 | 17 | }) 18 | -------------------------------------------------------------------------------- /tests/testthat/test-models-inception.R: -------------------------------------------------------------------------------- 1 | test_that("inception_v3 pretrained", { 2 | model <- model_inception_v3(pretrained = TRUE) 3 | model$eval() 4 | x <- model(torch_ones(2, 3, 299, 299)) 5 | # the value has been copied from running the same model on pytorch. 6 | expect_equal(as.numeric(x[1,1]), 0.18005196750164032, tol = 5e-6) 7 | expect_tensor_shape(x, c(2, 1000)) 8 | }) 9 | -------------------------------------------------------------------------------- /tests/testthat/test-models-mobilenetv2.R: -------------------------------------------------------------------------------- 1 | test_that("mobilenetv2 works", { 2 | model <- model_mobilenet_v2() 3 | input <- torch::torch_randn(1, 3, 256, 256) 4 | out <- model(input) 5 | 6 | expect_tensor_shape(out, c(1, 1000)) 7 | 8 | model <- model_mobilenet_v2(pretrained = TRUE) 9 | torch::torch_manual_seed(1) 10 | input <- torch::torch_randn(1, 3, 256, 256) 11 | out <- model(input) 12 | 13 | expect_tensor_shape(out, c(1, 1000)) 14 | # expect_equal_to_r(out[1,1], -1.1959798336029053, tolerance = 1e-5) # value taken from pytorch 15 | }) 16 | 17 | test_that("we can prune head of mobilenetv2 moels", { 18 | mobilenet <- model_mobilenet_v2(pretrained=TRUE) 19 | 20 | expect_no_error(prune <- nn_prune_head(mobilenet, 1)) 21 | expect_true(inherits(prune, "nn_sequential")) 22 | expect_equal(length(prune), 1) 23 | expect_true(inherits(prune[[length(prune)]], "nn_sequential")) 24 | 25 | input <- torch::torch_randn(1, 3, 256, 256) 26 | out <- prune(input) 27 | expect_tensor_shape(out, c(1, 1280, 8, 8)) 28 | }) 29 | -------------------------------------------------------------------------------- /tests/testthat/test-models-resnet.R: -------------------------------------------------------------------------------- 1 | test_that("resnet18", { 2 | 3 | model <- model_resnet18() 4 | input <- torch::torch_randn(1, 3, 256, 256) 5 | out <- model(input) 6 | 7 | expect_tensor_shape(out, c(1, 1000)) 8 | 9 | model <- model_resnet18(pretrained = TRUE) 10 | input <- torch::torch_randn(1, 3, 256, 256) 11 | out <- model(input) 12 | 13 | expect_tensor_shape(out, c(1, 1000)) 14 | 15 | }) 16 | 17 | test_that("resnet34", { 18 | skip_on_os(c("windows", "mac")) 19 | 20 | model <- model_resnet34() 21 | input <- torch::torch_randn(1, 3, 256, 256) 22 | out <- model(input) 23 | 24 | expect_tensor_shape(out, c(1, 1000)) 25 | 26 | model <- model_resnet34(pretrained = TRUE) 27 | input <- torch::torch_randn(1, 3, 256, 256) 28 | out <- model(input) 29 | 30 | expect_tensor_shape(out, c(1, 1000)) 31 | 32 | }) 33 | 34 | test_that("resnet50", { 35 | skip_on_os(c("windows", "mac")) 36 | 37 | model <- model_resnet50() 38 | input <- torch::torch_randn(1, 3, 256, 256) 39 | out <- model(input) 40 | 41 | expect_tensor_shape(out, c(1, 1000)) 42 | 43 | model <- model_resnet50(pretrained = TRUE) 44 | input <- torch::torch_randn(1, 3, 256, 256) 45 | out <- model(input) 46 | 47 | expect_tensor_shape(out, c(1, 1000)) 48 | 49 | }) 50 | 51 | test_that("resnet101", { 52 | skip_on_os(c("windows", "mac")) 53 | 54 | model <- model_resnet101() 55 | input <- torch::torch_randn(1, 3, 256, 256) 56 | out <- model(input) 57 | 58 | expect_tensor_shape(out, c(1, 1000)) 59 | 60 | withr::with_options(list(timeout = 360), 61 | model <- model_resnet101(pretrained = TRUE)) 62 | input <- torch::torch_randn(1, 3, 256, 256) 63 | out <- model(input) 64 | 65 | expect_tensor_shape(out, c(1, 1000)) 66 | 67 | }) 68 | 69 | test_that("resnet152", { 70 | skip_on_os(c("windows", "mac")) 71 | 72 | model <- model_resnet152() 73 | input <- torch::torch_randn(1, 3, 256, 256) 74 | out <- model(input) 75 | 76 | expect_tensor_shape(out, c(1, 1000)) 77 | 78 | withr::with_options(list(timeout = 360), 79 | model <- model_resnet152(pretrained = TRUE)) 80 | input <- torch::torch_randn(1, 3, 256, 256) 81 | out <- model(input) 82 | 83 | expect_tensor_shape(out, c(1, 1000)) 84 | 85 | }) 86 | 87 | test_that("resnext50_32x4d", { 88 | skip_on_os(c("windows", "mac")) 89 | 90 | model <- model_resnext50_32x4d() 91 | input <- torch::torch_randn(1, 3, 256, 256) 92 | out <- model(input) 93 | 94 | expect_tensor_shape(out, c(1, 1000)) 95 | 96 | withr::with_options(list(timeout = 360), 97 | model <- model_resnext50_32x4d(pretrained = TRUE)) 98 | input <- torch::torch_randn(1, 3, 256, 256) 99 | out <- model(input) 100 | 101 | expect_tensor_shape(out, c(1, 1000)) 102 | 103 | }) 104 | 105 | test_that("resnext50_32x4d", { 106 | skip_on_os(c("windows", "mac")) 107 | 108 | model <- model_resnext50_32x4d() 109 | input <- torch::torch_randn(1, 3, 256, 256) 110 | out <- model(input) 111 | 112 | expect_tensor_shape(out, c(1, 1000)) 113 | 114 | withr::with_options(list(timeout = 360), 115 | model <- model_resnext50_32x4d(pretrained = TRUE)) 116 | input <- torch::torch_randn(1, 3, 256, 256) 117 | out <- model(input) 118 | 119 | expect_tensor_shape(out, c(1, 1000)) 120 | 121 | }) 122 | 123 | test_that("resnext101_32x8d", { 124 | skip_on_os(c("windows", "mac")) 125 | 126 | model <- model_resnext101_32x8d() 127 | input <- torch::torch_randn(1, 3, 256, 256) 128 | out <- model(input) 129 | 130 | expect_tensor_shape(out, c(1, 1000)) 131 | 132 | withr::with_options(list(timeout = 360), 133 | model <- model_resnext101_32x8d(pretrained = TRUE)) 134 | input <- torch::torch_randn(1, 3, 256, 256) 135 | out <- model(input) 136 | 137 | expect_tensor_shape(out, c(1, 1000)) 138 | 139 | }) 140 | 141 | test_that("wide_resnet50_2", { 142 | skip_on_os(c("windows", "mac")) 143 | 144 | model <- model_wide_resnet50_2() 145 | input <- torch::torch_randn(1, 3, 256, 256) 146 | out <- model(input) 147 | 148 | expect_tensor_shape(out, c(1, 1000)) 149 | 150 | withr::with_options(list(timeout = 360), 151 | model <- model_wide_resnet50_2(pretrained = TRUE)) 152 | input <- torch::torch_randn(1, 3, 256, 256) 153 | out <- model(input) 154 | 155 | expect_tensor_shape(out, c(1, 1000)) 156 | 157 | }) 158 | 159 | test_that("wide_resnet101_2", { 160 | skip_on_os(c("windows", "mac")) 161 | 162 | model <- model_wide_resnet101_2() 163 | input <- torch::torch_randn(1, 3, 256, 256) 164 | out <- model(input) 165 | 166 | expect_tensor_shape(out, c(1, 1000)) 167 | 168 | withr::with_options(list(timeout = 360), 169 | model <- model_wide_resnet101_2(pretrained = TRUE)) 170 | input <- torch::torch_randn(1, 3, 256, 256) 171 | out <- model(input) 172 | 173 | expect_tensor_shape(out, c(1, 1000)) 174 | 175 | }) 176 | 177 | test_that("we can prune head of resnet34 moels", { 178 | resnet34 <- model_resnet34(pretrained=TRUE) 179 | 180 | expect_error(prune <- nn_prune_head(resnet34, 1), NA) 181 | # expect_true(inherits(prune, "nn_sequential")) 182 | expect_equal(length(prune), 9) 183 | expect_true(inherits(prune[[length(prune)]], "nn_adaptive_avg_pool2d")) 184 | 185 | input <- torch::torch_randn(1, 3, 256, 256) 186 | out <- prune(input) 187 | expect_tensor_shape(out, c(1, 512, 1, 1)) 188 | 189 | }) 190 | 191 | test_that("we can prune head of resnet50 moels", { 192 | resnet50 <- model_resnet50(pretrained=TRUE) 193 | 194 | expect_error(prune <- nn_prune_head(resnet50, 1), NA) 195 | expect_true(inherits(prune, "nn_sequential")) 196 | expect_equal(length(prune), 9) 197 | expect_true(inherits(prune[[length(prune)]], "nn_adaptive_avg_pool2d")) 198 | 199 | input <- torch::torch_randn(1, 3, 256, 256) 200 | out <- prune(input) 201 | expect_tensor_shape(out, c(1, 2048, 1, 1)) 202 | 203 | 204 | }) 205 | 206 | test_that("we can prune head of resnext101 moels", { 207 | resnext101 <- model_resnext101_32x8d(pretrained=TRUE) 208 | 209 | expect_error(prune <- torch:::nn_prune_head(resnext101, 1), NA) 210 | expect_true(inherits(prune, "nn_sequential")) 211 | expect_equal(length(prune), 9) 212 | expect_true(inherits(prune[[length(prune)]], "nn_adaptive_avg_pool2d")) 213 | 214 | input <- torch::torch_randn(1, 3, 256, 256) 215 | out <- prune(input) 216 | expect_tensor_shape(out, c(1, 2048, 1, 1)) 217 | 218 | 219 | }) 220 | -------------------------------------------------------------------------------- /tests/testthat/test-models-vgg.R: -------------------------------------------------------------------------------- 1 | test_that("vgg models works", { 2 | 3 | vggs <- list( 4 | model_vgg11, 5 | model_vgg11_bn, 6 | model_vgg13, 7 | model_vgg13_bn, 8 | model_vgg16, 9 | model_vgg16_bn, 10 | model_vgg19, 11 | model_vgg19_bn 12 | ) 13 | 14 | for (m in vggs) { 15 | 16 | model <- m() 17 | expect_tensor_shape(model(torch_ones(5, 3, 224, 224)), c(5, 1000)) 18 | 19 | } 20 | 21 | skip_on_ci() # unfortunatelly we don't have anough RAM on CI for that. 22 | #skip_on_os(os = "mac") # not downloading a bunch of files locally. 23 | #skip_on_os(os = "windows") # not downloading a bunch of files locally. 24 | 25 | for (m in vggs) { 26 | model <- m(pretrained = TRUE) 27 | expect_tensor_shape(model(torch_ones(1, 3, 224, 224)), c(1, 1000)) 28 | 29 | rm(model) 30 | gc() 31 | } 32 | 33 | }) 34 | -------------------------------------------------------------------------------- /tests/testthat/test-transforms-defaults.R: -------------------------------------------------------------------------------- 1 | test_that("random_resised_crop", { 2 | 3 | img <- torch::torch_randn(3, 224, 224) 4 | o <- transform_random_resized_crop(img, size = c(32, 32)) 5 | expect_tensor_shape(o, c(3, 32,32)) 6 | 7 | im <- magick::image_read("torch.png") 8 | o <- transform_random_resized_crop(im, size = c(32, 32)) 9 | expect_tensor_shape(transform_to_tensor(o), c(3, 32, 32)) 10 | 11 | }) 12 | -------------------------------------------------------------------------------- /tests/testthat/test-transforms-magick.R: -------------------------------------------------------------------------------- 1 | test_that("multiplication works", { 2 | im <- magick::image_read("torch.png") 3 | transform_crop(im, 1, 1, 500, 500) 4 | 5 | im <- transform_random_resized_crop(im, size = c(224, 224)) 6 | }) 7 | -------------------------------------------------------------------------------- /tests/testthat/test-vision-utils.R: -------------------------------------------------------------------------------- 1 | context("vision-utils") 2 | 3 | test_that("vision_make_grid", { 4 | 5 | images <- torch::torch_randn(c(4, 3, 16, 16)) 6 | 7 | grid <- vision_make_grid(images, num_rows = 2, padding = 0) 8 | 9 | 10 | expect_equal(grid$size(), c(3, 32, 32)) 11 | expect_equal(as.numeric(grid$max() - grid$min()), 1, tolerance = 1e-4) 12 | 13 | }) 14 | 15 | test_that("draw_bounding_boxes works", { 16 | 17 | image1 <- 1 - (torch::torch_randn(c(3, 360, 360)) / 20) 18 | image2 <- (255 - (torch::torch_randint(low = 1, high = 60, size = c(3, 360, 360))))$to(torch::torch_uint8()) 19 | x <- torch::torch_randint(low = 1, high = 160, size = c(12,1)) 20 | y <- torch::torch_randint(low = 1, high = 260, size = c(12,1)) 21 | boxes <- torch::torch_cat(c(x, y, x + runif(1, 5, 60), y + runif(1, 5, 10)), dim = 2) 22 | 23 | expect_error(bboxed_image <- draw_bounding_boxes(image1, boxes), "uint8") 24 | 25 | expect_no_error(bboxed_image <- draw_bounding_boxes(image2, boxes, labels = "dog")) 26 | expect_tensor_dtype(bboxed_image, torch::torch_uint8()) 27 | expect_tensor_shape(bboxed_image, c(3, 360, 360)) 28 | 29 | expect_no_error(bboxed_image <- draw_bounding_boxes(image2, boxes, color = "black", fill = TRUE)) 30 | }) 31 | 32 | test_that("draw_bounding_boxes correctly mask a complete image", { 33 | 34 | image <- torch::torch_randint(low = 1, high = 240, size = c(3, 360, 360))$to(torch::torch_uint8()) 35 | boxes <- torch::torch_tensor(c(0,0,360,360))$unsqueeze(1) 36 | 37 | expect_no_error(bboxed_image <- draw_bounding_boxes(image, boxes, color = "black", fill = TRUE)) 38 | # some invisible glitch remaains 39 | expect_lte(bboxed_image$sum() %>% as.numeric, 3000) 40 | }) 41 | 42 | test_that("draw_segmentation_masks works", { 43 | 44 | image <- torch::torch_randint(low = 190, high = 255, size = c(3, 360, 360))$to(torch::torch_uint8()) 45 | lower_mask <- torch::torch_tril(torch::torch_ones(c(360, 360)), diagonal = FALSE)$to(torch::torch_bool()) 46 | upper_mask <- torch::torch_triu(torch::torch_ones(c(360, 360)), diagonal = FALSE)$to(torch::torch_bool()) 47 | masks <- torch::torch_stack(c(lower_mask, upper_mask), dim = 1) 48 | 49 | expect_no_error(masked_image <- draw_segmentation_masks(image, masks)) 50 | expect_tensor_dtype(masked_image, torch::torch_uint8()) 51 | expect_tensor_shape(masked_image, c(3, 360, 360)) 52 | 53 | colors <- c("navyblue", "orange3") 54 | expect_no_error(masked_image <- draw_segmentation_masks(image, masks, colors = colors, alpha = 0.5)) 55 | }) 56 | 57 | test_that("draw_keypoints works", { 58 | 59 | image <- torch::torch_randint(low = 190, high = 255, size = c(3, 360, 360))$to(torch::torch_uint8()) 60 | keypoints <- torch::torch_randint(low = 60, high = 300, size = c(4, 5, 2)) 61 | expect_no_error(keypoint_image <- draw_keypoints(image, keypoints)) 62 | expect_tensor_dtype(keypoint_image, torch::torch_uint8()) 63 | expect_tensor_shape(keypoint_image, c(3, 360, 360)) 64 | 65 | colors <- hcl.colors(n = 5) 66 | expect_no_error(keypoint_image <- draw_keypoints(image, keypoints, colors = colors, radius = 7)) 67 | }) 68 | 69 | test_that("tensor_image_browse works", { 70 | skip_on_cran() 71 | skip_on_ci() 72 | # uint8 color image 73 | image <- (255 - (torch::torch_randint(low = 1, high = 200, size = c(3, 360, 360))))$to(torch::torch_uint8()) 74 | expect_no_error(tensor_image_browse(image)) 75 | # uint8 grayscale image 76 | image <- (255 - (torch::torch_randint(low = 1, high = 200, size = c(1, 360, 360))))$to(torch::torch_uint8()) 77 | expect_no_error(tensor_image_browse(image)) 78 | 79 | # float color image 80 | image <- torch::torch_rand(size = c(3, 360, 360)) 81 | expect_no_error(tensor_image_browse(image)) 82 | # float grayscale image 83 | image <- torch::torch_rand(size = c(1, 360, 360)) 84 | expect_no_error(tensor_image_browse(image)) 85 | 86 | # error cases : shape 87 | image <- torch::torch_randint(low = 1, high = 200, size = c(4, 3, 360, 360))$to(torch::torch_uint8()) 88 | expect_error(tensor_image_browse(image), "individual images") 89 | image <- torch::torch_randint(low = 1, high = 200, size = c(4, 360, 360))$to(torch::torch_uint8()) 90 | expect_error(tensor_image_browse(image), "Only grayscale and RGB") 91 | 92 | }) 93 | 94 | -------------------------------------------------------------------------------- /tests/testthat/torch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/tests/testthat/torch.png -------------------------------------------------------------------------------- /tools/convert-models.py: -------------------------------------------------------------------------------- 1 | import torchvision 2 | import torch 3 | from google.cloud import storage 4 | import os 5 | 6 | def upload_blob(bucket_name, source_file_name, destination_blob_name): 7 | """Uploads a file to the bucket.""" 8 | # bucket_name = "your-bucket-name" 9 | # source_file_name = "local/path/to/file" 10 | # destination_blob_name = "storage-object-name" 11 | 12 | storage_client = storage.Client() 13 | bucket = storage_client.bucket(bucket_name) 14 | blob = bucket.blob(destination_blob_name) 15 | 16 | blob.upload_from_filename(source_file_name) 17 | 18 | print( 19 | "File {} uploaded to {}.".format( 20 | source_file_name, destination_blob_name 21 | ) 22 | ) 23 | 24 | models = { 25 | 'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth', 26 | 'densenet121': 'https://download.pytorch.org/models/densenet121-a639ec97.pth', 27 | 'densenet169': 'https://download.pytorch.org/models/densenet169-b2777c0a.pth', 28 | 'densenet201': 'https://download.pytorch.org/models/densenet201-c1103571.pth', 29 | 'densenet161': 'https://download.pytorch.org/models/densenet161-8d451a50.pth', 30 | 'googlenet': 'https://download.pytorch.org/models/googlenet-1378be20.pth', 31 | 'inception_v3_google': 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth', 32 | "mnasnet0_5": "https://download.pytorch.org/models/mnasnet0.5_top1_67.823-3ffadce67e.pth", 33 | "mnasnet1_0": "https://download.pytorch.org/models/mnasnet1.0_top1_73.512-f206786ef8.pth", 34 | 'mobilenet_v2': 'https://download.pytorch.org/models/mobilenet_v2-b0353104.pth', 35 | 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 36 | 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth', 37 | 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 38 | 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 39 | 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', 40 | 'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth', 41 | 'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth', 42 | 'wide_resnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth', 43 | 'wide_resnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth', 44 | 'shufflenetv2_x0.5': 'https://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pth', 45 | 'shufflenetv2_x1.0': 'https://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pth', 46 | 'squeezenet1_0': 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth', 47 | 'squeezenet1_1': 'https://download.pytorch.org/models/squeezenet1_1-f364aa15.pth', 48 | 'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth', 49 | 'vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth', 50 | 'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth', 51 | 'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth', 52 | 'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth', 53 | 'vgg13_bn': 'https://download.pytorch.org/models/vgg13_bn-abd245e5.pth', 54 | 'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth', 55 | 'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth', 56 | 'r3d_18': 'https://download.pytorch.org/models/r3d_18-b3b3357e.pth', 57 | 'mc3_18': 'https://download.pytorch.org/models/mc3_18-a90a0ba3.pth', 58 | 'r2plus1d_18': 'https://download.pytorch.org/models/r2plus1d_18-91a641e6.pth', 59 | 'fcn_resnet50_coco': 'https://download.pytorch.org/models/fcn_resnet50_coco-1167a1af.pth', 60 | 'fcn_resnet101_coco': 'https://download.pytorch.org/models/fcn_resnet101_coco-7ecb50ca.pth', 61 | 'deeplabv3_resnet50_coco': 'https://download.pytorch.org/models/deeplabv3_resnet50_coco-cd0a2569.pth', 62 | 'deeplabv3_resnet101_coco': 'https://download.pytorch.org/models/deeplabv3_resnet101_coco-586e9e4e.pth', 63 | 'casia-webface': 'https://github.com/timesler/facenet-pytorch/releases/download/v2.2.9/20180408-102900-casia-webface.pt', 64 | 'vggface2': 'https://github.com/timesler/facenet-pytorch/releases/download/v2.2.9/20180402-114759-vggface2.pt' 65 | } 66 | 67 | os.mkdir("models/") 68 | 69 | for name, url in models.items(): 70 | m = torchvision.models.utils.load_state_dict_from_url(url, progress=False) 71 | converted = {} 72 | for nm, par in m.items(): 73 | converted.update([(nm, par.clone())]) 74 | fpath = "models/" + name + ".pth" 75 | torch.save(converted, fpath, _use_new_zipfile_serialization=True) 76 | upload_blob( 77 | "torchvision-models", 78 | fpath, 79 | "v2/" + fpath 80 | ) 81 | -------------------------------------------------------------------------------- /tools/document.R: -------------------------------------------------------------------------------- 1 | library(roxygen2) 2 | 3 | 4 | # Patch examples ---------------------------------------------------------- 5 | # we path examples so we add the `if` conditional. 6 | 7 | .S3method("roxy_tag_parse", "roxy_tag_examples", function(x) { 8 | roxygen2::tag_examples(x) 9 | }) 10 | 11 | .S3method("roxy_tag_rd", "roxy_tag_examples", function(x, base_path, env) { 12 | rd_section("examples", x$val) 13 | }) 14 | 15 | .S3method("format", "rd_section_examples", function (x, ...) { 16 | value <- paste0(x$value, collapse = "\n") 17 | roxygen2:::rd_macro("examples", 18 | c("if (torch_is_installed()) {", value, "}"), 19 | space = TRUE 20 | ) 21 | }) 22 | 23 | # Subsection -------------------------------------------------------------- 24 | 25 | .S3method("roxy_tag_parse", "roxy_tag_subsection", function(x) { 26 | roxygen2::tag_markdown(x) 27 | }) 28 | 29 | .S3method("roxy_tag_rd", "roxy_tag_subsection", function(x, base_path, env) { 30 | pieces <- stringr::str_split(x$val, ":", n = 2)[[1]] 31 | title <- stringr::str_split(pieces[1], "\n")[[1]] 32 | 33 | if (length(title) > 1) { 34 | roxygen2:::roxy_tag_warning(x, paste0( 35 | "Section title spans multiple lines.\n", 36 | "Did you forget a colon (:) at the end of the title?" 37 | )) 38 | return() 39 | } 40 | 41 | roxygen2:::rd_section_section(pieces[1], pieces[2]) 42 | }) 43 | 44 | .S3method("format", "rd_section_subsection", function(x, ...) { 45 | paste0( 46 | "\\subsection{", x$value$title, "}{\n", x$value$content, "\n}\n", 47 | collapse = "\n" 48 | ) 49 | }) 50 | 51 | # Params ------------------------------------------------------------------ 52 | # avoids using the \arguments{} so we can have two sections with parameters 53 | # for different signatures. 54 | 55 | 56 | 57 | 58 | devtools::document() 59 | -------------------------------------------------------------------------------- /torchvision.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | LineEndingConversion: Posix 18 | 19 | BuildType: Package 20 | PackageUseDevtools: Yes 21 | PackageInstallArgs: --no-multiarch --with-keep.source 22 | PackageRoxygenize: rd,collate,namespace 23 | -------------------------------------------------------------------------------- /vignettes/examples/assets/dancing.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/vignettes/examples/assets/dancing.jpg -------------------------------------------------------------------------------- /vignettes/examples/assets/picasso.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mlverse/torchvision/db888a5b7d3f034240007478d00e1ac7ea3215d5/vignettes/examples/assets/picasso.jpg -------------------------------------------------------------------------------- /vignettes/examples/mnist-cnn.R: -------------------------------------------------------------------------------- 1 | # Packages ---------------------------------------------------------------- 2 | library(torch) 3 | library(torchvision) 4 | 5 | 6 | # Datasets and loaders ---------------------------------------------------- 7 | 8 | dir <- "~/Downloads/mnist" #caching directory 9 | 10 | train_ds <- mnist_dataset( 11 | dir, 12 | download = TRUE, 13 | transform = transform_to_tensor 14 | ) 15 | 16 | test_ds <- mnist_dataset( 17 | dir, 18 | train = FALSE, 19 | transform = transform_to_tensor 20 | ) 21 | 22 | train_dl <- dataloader(train_ds, batch_size = 32, shuffle = TRUE) 23 | test_dl <- dataloader(test_ds, batch_size = 32) 24 | 25 | 26 | # Buildifng the network --------------------------------------------------- 27 | 28 | net <- nn_module( 29 | "Net", 30 | initialize = function() { 31 | self$conv1 <- nn_conv2d(1, 32, 3, 1) 32 | self$conv2 <- nn_conv2d(32, 64, 3, 1) 33 | self$dropout1 <- nn_dropout2d(0.25) 34 | self$dropout2 <- nn_dropout2d(0.5) 35 | self$fc1 <- nn_linear(9216, 128) 36 | self$fc2 <- nn_linear(128, 10) 37 | }, 38 | forward = function(x) { 39 | x <- self$conv1(x) 40 | x <- nnf_relu(x) 41 | x <- self$conv2(x) 42 | x <- nnf_relu(x) 43 | x <- nnf_max_pool2d(x, 2) 44 | x <- self$dropout1(x) 45 | x <- torch_flatten(x, start_dim = 2) 46 | x <- self$fc1(x) 47 | x <- nnf_relu(x) 48 | x <- self$dropout2(x) 49 | output <- self$fc2(x) 50 | output 51 | } 52 | ) 53 | 54 | model <- net() 55 | 56 | # ove model to cuda if it's available 57 | device <- if(cuda_is_available()) "cuda" else "cpu" 58 | model$to(device = device) 59 | 60 | # Training loop ----------------------------------------------------------- 61 | 62 | optimizer <- optim_sgd(model$parameters, lr = 0.01) 63 | 64 | epochs <- 10 65 | for (epoch in 1:10) { 66 | 67 | pb <- progress::progress_bar$new( 68 | total = length(train_dl), 69 | format = "[:bar] :eta Loss: :loss" 70 | ) 71 | 72 | train_losses <- c() 73 | test_losses <- c() 74 | 75 | coro::loop(for (b in train_dl) { 76 | optimizer$zero_grad() 77 | output <- model(b[[1]]$to(device = device)) 78 | loss <- nnf_cross_entropy(output, b[[2]]$to(device = device)) 79 | loss$backward() 80 | optimizer$step() 81 | train_losses <- c(train_losses, loss$item()) 82 | pb$tick(tokens = list(loss = mean(train_losses))) 83 | }) 84 | 85 | with_no_grad({ 86 | coro::loop(for (b in test_dl) { 87 | model$eval() 88 | output <- model(b[[1]]$to(device = device)) 89 | loss <- nnf_cross_entropy(output, b[[2]]$to(device = device)) 90 | test_losses <- c(test_losses, loss$item()) 91 | model$train() 92 | }) 93 | }) 94 | 95 | cat(sprintf("Loss at epoch %d [Train: %3f] [Test: %3f]\n", 96 | epoch, mean(train_losses), mean(test_losses))) 97 | } 98 | 99 | -------------------------------------------------------------------------------- /vignettes/examples/mnist-cnn.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: mnist-cnn 3 | type: docs 4 | --- 5 | 6 | ```{r, echo = FALSE} 7 | knitr::opts_chunk$set(eval = FALSE) 8 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 9 | ``` -------------------------------------------------------------------------------- /vignettes/examples/mnist-dcgan.R: -------------------------------------------------------------------------------- 1 | # Packages ---------------------------------------------------------------- 2 | 3 | library(torch) 4 | library(torchvision) 5 | 6 | # Utils ------------------------------------------------------------------- 7 | 8 | # plots an image generated given the 9 | # intermediate state 10 | plot_gen <- function(noise) { 11 | img <- G(noise) 12 | img <- img$cpu() 13 | img <- img[1,1,,,newaxis]/2 + 0.5 14 | img <- torch_stack(list(img, img, img), dim = 3)[..,1] 15 | img <- as.raster(as_array(img)) 16 | plot(img) 17 | } 18 | 19 | 20 | # Datasets and loaders ---------------------------------------------------- 21 | 22 | dir <- "~/Downloads/mnist" 23 | 24 | ds <- mnist_dataset( 25 | dir, 26 | transform = transform_to_tensor, 27 | download = TRUE, 28 | ) 29 | 30 | dl <- dataloader(ds, batch_size = 32, shuffle = TRUE) 31 | 32 | 33 | # Define the network ------------------------------------------------------ 34 | 35 | generator <- nn_module( 36 | "generator", 37 | initialize = function(latent_dim, out_channels) { 38 | self$main <- nn_sequential( 39 | nn_conv_transpose2d(latent_dim, 512, kernel_size = 4, 40 | stride = 1, padding = 0, bias = FALSE), 41 | nn_batch_norm2d(512), 42 | nn_relu(), 43 | nn_conv_transpose2d(512, 256, kernel_size = 4, 44 | stride = 2, padding = 1, bias = FALSE), 45 | nn_batch_norm2d(256), 46 | nn_relu(), 47 | nn_conv_transpose2d(256, 128, kernel_size = 4, 48 | stride = 2, padding = 1, bias = FALSE), 49 | nn_batch_norm2d(128), 50 | nn_relu(), 51 | nn_conv_transpose2d(128, out_channels, kernel_size = 4, 52 | stride = 2, padding = 3, bias = FALSE), 53 | nn_tanh() 54 | ) 55 | }, 56 | forward = function(input) { 57 | self$main(input) 58 | } 59 | ) 60 | 61 | discriminator <- nn_module( 62 | "discriminator", 63 | initialize = function(in_channels) { 64 | self$main <- nn_sequential( 65 | nn_conv2d(in_channels, 16, kernel_size = 4, stride = 2, padding = 1, bias = FALSE), 66 | nn_leaky_relu(0.2, inplace = TRUE), 67 | nn_conv2d(16, 32, kernel_size = 4, stride = 2, padding = 1, bias = FALSE), 68 | nn_batch_norm2d(32), 69 | nn_leaky_relu(0.2, inplace = TRUE), 70 | nn_conv2d(32, 64, kernel_size = 4, stride = 2, padding = 1, bias = FALSE), 71 | nn_batch_norm2d(64), 72 | nn_leaky_relu(0.2, inplace = TRUE), 73 | nn_conv2d(64, 128, kernel_size = 4, stride = 2, padding = 1, bias = FALSE), 74 | nn_leaky_relu(0.2, inplace = TRUE) 75 | ) 76 | self$linear <- nn_linear(128, 1) 77 | self$sigmoid <- nn_sigmoid() 78 | }, 79 | forward = function(input) { 80 | x <- self$main(input) 81 | x <- torch_flatten(x, start_dim = 2) 82 | x <- self$linear(x) 83 | self$sigmoid(x) 84 | } 85 | ) 86 | 87 | device <- torch_device(ifelse(cuda_is_available(), "cuda", "cpu")) 88 | 89 | G <- generator(latent_dim = 100, out_channels = 1) 90 | D <- discriminator(in_channels = 1) 91 | 92 | init_weights <- function(m) { 93 | if (grepl("conv", m$.classes[[1]])) { 94 | nn_init_normal_(m$weight$data(), 0.0, 0.02) 95 | } else if (grepl("batch_norm", m$.classes[[1]])) { 96 | nn_init_normal_(m$weight$data(), 1.0, 0.02) 97 | nn_init_constant_(m$bias$data(), 0) 98 | } 99 | } 100 | 101 | G[[1]]$apply(init_weights) 102 | D[[1]]$apply(init_weights) 103 | 104 | G$to(device = device) 105 | D$to(device = device) 106 | 107 | G_optimizer <- optim_adam(G$parameters, lr = 2 * 1e-4, betas = c(0.5, 0.999)) 108 | D_optimizer <- optim_adam(D$parameters, lr = 2 * 1e-4, betas = c(0.5, 0.999)) 109 | 110 | fixed_noise <- torch_randn(1, 100, 1, 1, device = device) 111 | 112 | 113 | # Training loop ----------------------------------------------------------- 114 | 115 | loss <- nn_bce_loss() 116 | 117 | for (epoch in 1:10) { 118 | 119 | pb <- progress::progress_bar$new( 120 | total = length(dl), 121 | format = "[:bar] :eta Loss D: :lossd Loss G: :lossg" 122 | ) 123 | lossg <- c() 124 | lossd <- c() 125 | 126 | coro::loop(for (b in dl) { 127 | 128 | y_real <- torch_ones(32, device = device) 129 | y_fake <- torch_zeros(32, device = device) 130 | 131 | noise <- torch_randn(32, 100, 1, 1, device = device) 132 | fake <- G(noise) 133 | 134 | img <- b[[1]]$to(device = device) 135 | 136 | # train the discriminator --- 137 | D_loss <- loss(D(img), y_real) + loss(D(fake$detach()), y_fake) 138 | 139 | D_optimizer$zero_grad() 140 | D_loss$backward() 141 | D_optimizer$step() 142 | 143 | # train the generator --- 144 | 145 | G_loss <- loss(D(fake), y_real) 146 | 147 | G_optimizer$zero_grad() 148 | G_loss$backward() 149 | G_optimizer$step() 150 | 151 | lossd <- c(lossd, D_loss$item()) 152 | lossg <- c(lossg, G_loss$item()) 153 | pb$tick(tokens = list(lossd = mean(lossd), lossg = mean(lossg))) 154 | }) 155 | 156 | with_no_grad({ 157 | plot_gen(fixed_noise) 158 | }) 159 | 160 | cat(sprintf("Epoch %d - Loss D: %3f Loss G: %3f\n", epoch, mean(lossd), mean(lossg))) 161 | } 162 | -------------------------------------------------------------------------------- /vignettes/examples/mnist-dcgan.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: mnist-dcgan 3 | type: docs 4 | --- 5 | 6 | ```{r, echo = FALSE} 7 | knitr::opts_chunk$set(eval = FALSE) 8 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 9 | ``` -------------------------------------------------------------------------------- /vignettes/examples/mnist-mlp.R: -------------------------------------------------------------------------------- 1 | # Packages ---------------------------------------------------------------- 2 | library(torch) 3 | library(torchvision) 4 | 5 | 6 | # Datasets and loaders ---------------------------------------------------- 7 | 8 | dir <- "~/Downloads/mnist" #caching directory 9 | 10 | train_ds <- mnist_dataset( 11 | dir, 12 | download = TRUE, 13 | transform = transform_to_tensor 14 | ) 15 | 16 | test_ds <- mnist_dataset( 17 | dir, 18 | train = FALSE, 19 | transform = transform_to_tensor 20 | ) 21 | 22 | train_dl <- dataloader(train_ds, batch_size = 32, shuffle = TRUE) 23 | test_dl <- dataloader(test_ds, batch_size = 32) 24 | 25 | 26 | # Buildifng the network --------------------------------------------------- 27 | 28 | net <- nn_module( 29 | "Net", 30 | initialize = function() { 31 | self$fc1 <- nn_linear(784, 128) 32 | self$fc2 <- nn_linear(128, 10) 33 | }, 34 | forward = function(x) { 35 | x %>% 36 | torch_flatten(start_dim = 2) %>% 37 | self$fc1() %>% 38 | nnf_relu() %>% 39 | self$fc2() 40 | } 41 | ) 42 | 43 | model <- net() 44 | 45 | # ove model to cuda if it's available 46 | device <- if(cuda_is_available()) "cuda" else "cpu" 47 | model$to(device = device) 48 | 49 | # Training loop ----------------------------------------------------------- 50 | 51 | optimizer <- optim_sgd(model$parameters, lr = 0.01) 52 | 53 | epochs <- 10 54 | for (epoch in 1:10) { 55 | 56 | pb <- progress::progress_bar$new( 57 | total = length(train_dl), 58 | format = "[:bar] :eta Loss: :loss" 59 | ) 60 | 61 | train_losses <- c() 62 | test_losses <- c() 63 | 64 | coro::loop(for (b in train_dl) { 65 | optimizer$zero_grad() 66 | output <- model(b[[1]]$to(device = device)) 67 | loss <- nnf_cross_entropy(output, b[[2]]$to(device = device)) 68 | loss$backward() 69 | optimizer$step() 70 | train_losses <- c(train_losses, loss$item()) 71 | pb$tick(tokens = list(loss = mean(train_losses))) 72 | }) 73 | 74 | with_no_grad({ 75 | coro::loop(for (b in test_dl) { 76 | model$eval() 77 | output <- model(b[[1]]$to(device = device)) 78 | loss <- nnf_cross_entropy(output, b[[2]]$to(device = device)) 79 | test_losses <- c(test_losses, loss$item()) 80 | model$train() 81 | }) 82 | }) 83 | 84 | cat(sprintf("Loss at epoch %d [Train: %3f] [Test: %3f]\n", 85 | epoch, mean(train_losses), mean(test_losses))) 86 | } 87 | 88 | -------------------------------------------------------------------------------- /vignettes/examples/mnist-mlp.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: mnist-mlp 3 | type: docs 4 | --- 5 | 6 | ```{r, echo = FALSE} 7 | knitr::opts_chunk$set(eval = FALSE) 8 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 9 | ``` -------------------------------------------------------------------------------- /vignettes/examples/style-transfer.R: -------------------------------------------------------------------------------- 1 | 2 | # Packages ---------------------------------------------------------------- 3 | 4 | library(torch) 5 | library(torchvision) 6 | 7 | device <- if (cuda_is_available()) "cuda" else "cpu" 8 | num_steps <- 4000 9 | content_weight <- 1e-5 10 | style_weight <- 1e2 11 | 12 | 13 | # Network definition ------------------------------------------------------ 14 | 15 | content_loss <- function(content, style) { 16 | nnf_mse_loss(content, style) 17 | } 18 | 19 | gram_matrix <- function(input) { 20 | size <- input$size() 21 | features <- input$view(c(size[1] * size[2], size[3] * size[4])) 22 | G <- torch_mm(features, features$t()) 23 | # we 'normalize' the values of the gram matrix 24 | # by dividing by the number of element in each feature maps. 25 | G$div(prod(size)) 26 | } 27 | 28 | style_loss <- function(content, style) { 29 | C <- gram_matrix(content) 30 | S <- gram_matrix(style) 31 | nnf_mse_loss(C, S) 32 | } 33 | 34 | cnn <- model_vgg19(pretrained = TRUE)$features$to(device = device) 35 | cnn$eval() 36 | 37 | # we create an nn_module that does the same as the sequential container but 38 | # returns the results of all convolutions. we also replace inplace operations 39 | # for copy on modify ones 40 | features <- nn_module( 41 | initialize = function(cnn) { 42 | self$cnn <- cnn 43 | }, 44 | forward = function(input) { 45 | conv_outs <- list() 46 | for (i in seq_along(self$cnn)) { 47 | layer <- self$cnn[[i]] 48 | 49 | if (inherits(layer, "nn_relu")) 50 | input <- nnf_relu(input) 51 | else 52 | input <- layer(input) 53 | 54 | if (inherits(layer, "nn_conv2d")) 55 | conv_outs[[length(conv_outs) + 1]] <- input 56 | 57 | } 58 | conv_outs 59 | } 60 | ) 61 | 62 | model <- features(cnn) 63 | 64 | # Loading images ---------------------------------------------------------- 65 | 66 | norm_mean <- c(0.485, 0.456, 0.406) 67 | norm_std <- c(0.229, 0.224, 0.225) 68 | 69 | normalize <- function(img) { 70 | transform_normalize(img, norm_mean, norm_std) 71 | } 72 | 73 | denormalize <- function(img) { 74 | transform_normalize(img, -norm_mean/norm_std, 1/norm_std) 75 | } 76 | 77 | load_image <- function(path) { 78 | x <- jpeg::readJPEG(path) %>% 79 | transform_to_tensor() %>% 80 | transform_resize(c(512, 512)) 81 | x <- x[newaxis,..] 82 | x <- normalize(x) 83 | x$to(device = device) 84 | } 85 | 86 | style_img <- load_image("vignettes/examples/assets/picasso.jpg") 87 | content_img <- load_image("vignettes/examples/assets/dancing.jpg") 88 | content_img <- content_img$requires_grad_(TRUE) 89 | 90 | 91 | # Optimization ------------------------------------------------------------ 92 | 93 | optimizer <- optim_adam(content_img, lr = 1) 94 | lr_scheduler <- lr_step(optimizer, 100, 0.96) 95 | 96 | for (step in seq_len(num_steps)) { 97 | 98 | gc() # we have to call gc otherwise R tensors are not disposed. 99 | 100 | optimizer$zero_grad() 101 | 102 | content_features <- model(content_img) 103 | style_features <- model(style_img) 104 | 105 | # compute the content loss 106 | l_content <- content_weight * content_loss(content_features[[4]], style_features[[4]]) 107 | 108 | # compute the style loss 109 | l_style <- torch_tensor(0, device = device) 110 | for (i in 1:5) { 111 | l_style <- l_style + style_loss(content_features[[i]], style_features[[i]]) 112 | } 113 | l_style <- style_weight * l_style 114 | 115 | # compute the final loss 116 | loss <- l_content + l_style 117 | 118 | loss$backward() 119 | 120 | # optimization step 121 | optimizer$step() 122 | lr_scheduler$step() 123 | 124 | if (step %% 100 == 0) 125 | cat( 126 | "[Step: ", step, "] ", 127 | "Loss: ", loss$item(), 128 | " Content loss: ", l_content$item(), 129 | "Style loss: ", l_style$item(), 130 | "\n" 131 | ) 132 | 133 | } 134 | 135 | # Visualize the final img 136 | im <- denormalize(content_img)[1,..]$ 137 | permute(c(2, 3, 1))$ 138 | to(device = "cpu")$ 139 | clamp(0,1) %>% # make it [0,1] 140 | as.array() 141 | 142 | plot(as.raster(im)) 143 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /vignettes/examples/style-transfer.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: style-transfer 3 | type: docs 4 | --- 5 | 6 | ```{r, echo = FALSE} 7 | knitr::opts_chunk$set(eval = FALSE) 8 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 9 | ``` 10 | -------------------------------------------------------------------------------- /vignettes/examples/texture-nca.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: texture-nca 3 | type: docs 4 | --- 5 | 6 | ```{r, echo = FALSE} 7 | knitr::opts_chunk$set(eval = FALSE) 8 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 9 | ``` 10 | -------------------------------------------------------------------------------- /vignettes/examples/tinyimagenet-alexnet.R: -------------------------------------------------------------------------------- 1 | # Packages ---------------------------------------------------------------- 2 | 3 | library(torch) 4 | library(torchvision) 5 | 6 | 7 | # Datasets ---------------------------------------------------------------- 8 | 9 | dir <- "~/Downloads/tiny-imagenet" 10 | 11 | device <- if(cuda_is_available()) "cuda" else "cpu" 12 | 13 | to_device <- function(x, device) { 14 | x$to(device = device) 15 | } 16 | 17 | train_ds <- tiny_imagenet_dataset( 18 | dir, 19 | download = TRUE, 20 | transform = function(x) { 21 | x %>% 22 | transform_to_tensor() %>% 23 | to_device(device) %>% 24 | transform_resize(c(64, 64)) 25 | } 26 | ) 27 | 28 | valid_ds <- tiny_imagenet_dataset( 29 | dir, 30 | download = TRUE, 31 | split = "val", 32 | transform = function(x) { 33 | x %>% 34 | transform_to_tensor() %>% 35 | to_device(device) %>% 36 | transform_resize(c(64,64)) 37 | } 38 | ) 39 | 40 | train_dl <- dataloader(train_ds, batch_size = 32, shuffle = TRUE, drop_last = TRUE) 41 | valid_dl <- dataloader(valid_ds, batch_size = 32, shuffle = FALSE, drop_last = TRUE) 42 | 43 | 44 | # Model ------------------------------------------------------------------- 45 | 46 | model <- model_alexnet(pretrained = FALSE, num_classes = length(train_ds$classes)) 47 | model$to(device = device) 48 | 49 | optimizer <- optim_adagrad(model$parameters, lr = 0.005) 50 | scheduler <- lr_step(optimizer, step_size = 1, 0.95) 51 | loss_fn <- nn_cross_entropy_loss() 52 | 53 | 54 | # Training loop ----------------------------------------------------------- 55 | 56 | train_step <- function(batch) { 57 | optimizer$zero_grad() 58 | output <- model(batch[[1]]$to(device = device)) 59 | loss <- loss_fn(output, batch[[2]]$to(device = device)) 60 | loss$backward() 61 | optimizer$step() 62 | loss 63 | } 64 | 65 | valid_step <- function(batch) { 66 | model$eval() 67 | pred <- model(batch[[1]]$to(device = device)) 68 | pred <- torch_topk(pred, k = 5, dim = 2, TRUE, TRUE)[[2]] 69 | pred <- pred$to(device = torch_device("cpu")) 70 | correct <- batch[[2]]$view(c(-1, 1))$eq(pred)$any(dim = 2) 71 | model$train() 72 | correct$to(dtype = torch_float32())$mean()$item() 73 | } 74 | 75 | for (epoch in 1:50) { 76 | 77 | pb <- progress::progress_bar$new( 78 | total = length(train_dl), 79 | format = "[:bar] :eta Loss: :loss" 80 | ) 81 | 82 | l <- c() 83 | coro::loop(for (b in train_dl) { 84 | loss <- train_step(b) 85 | l <- c(l, loss$item()) 86 | pb$tick(tokens = list(loss = mean(l))) 87 | }) 88 | 89 | acc <- c() 90 | with_no_grad({ 91 | coro::loop(for (b in valid_dl) { 92 | accuracy <- valid_step(b) 93 | acc <- c(acc, accuracy) 94 | }) 95 | }) 96 | 97 | scheduler$step() 98 | cat(sprintf("[epoch %d]: Loss = %3f, Acc= %3f \n", epoch, mean(l), mean(acc))) 99 | } 100 | -------------------------------------------------------------------------------- /vignettes/examples/tinyimagenet-alexnet.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: tinyimagenet-alexnet 3 | type: docs 4 | --- 5 | 6 | ```{r, echo = FALSE} 7 | knitr::opts_chunk$set(eval = FALSE) 8 | knitr::spin_child(paste0(rmarkdown::metadata$title, ".R")) 9 | ``` 10 | --------------------------------------------------------------------------------