Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To_fp16() #90

Closed
Cdk29 opened this issue Dec 22, 2020 · 10 comments
Closed

To_fp16() #90

Cdk29 opened this issue Dec 22, 2020 · 10 comments

Comments

@Cdk29
Copy link
Contributor

Cdk29 commented Dec 22, 2020

There is one callback describe here : https://docs.fast.ai/callback.fp16.html
Details here : https://docs.fast.ai/callback.fp16.html#Learner.to_fp16

It allow to use less GPU when training a CNN.
The book describe the use of the method when creating the learner, but it can also be used on the learner directly.
Running it in R however create a bug, and I don't see how to use it.

Precisely :

learnR <- dataloader %>% cnn_learner(xresnet50(), metrics = accuracy,  model_dir="fastai_model/") #prettier
learnR$to_fp16()

Then running :
learnR %>% lr_find()

Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'str' object has no attribute 'type'`

Because now the learner is a string.

@turgut090
Copy link
Member

turgut090 commented Dec 22, 2020

Hi.
This works for me:

library(fastai)
library(magrittr)

URLs_PETS()

path = 'oxford-iiit-pet'
path_anno = 'oxford-iiit-pet/annotations'
path_img = 'oxford-iiit-pet/images'
fnames = get_image_files(path_img)

item_tfms = list(Flip(), Dihedral(),Zoom_())

dls = ImageDataLoaders_from_name_re(
  path, fnames, pat='(.+)_\\d+.jpg$',
  item_tfms=Resize(300), bs = 10,
  batch_tfms=list(aug_transforms(size = 224, min_scale = 0.75,max_rotate = 180,
                                 max_zoom = 10, max_lighting = 0.9,max_warp = 0.8,
                                 flip_vert = TRUE),
                  Normalize_from_stats( imagenet_stats() )
  )
)

learn = cnn_learner(dls, xresnet50(), metrics = list(error_rate, accuracy),model_dir="fastai_model/")
learn$to_fp16()
lrs = learn %>% lr_find()
SuggestedLRs(lr_min=7.585775847473997e-08, lr_steep=2.2908675418875646e-06)

@Cdk29
Copy link
Contributor Author

Cdk29 commented Dec 22, 2020

@henry090 that is odd.

I got it to works. Below to full code from rmarkdown that created the bug :

path_img = 'cassava-leaf-disease-classification/train_images/'
#library(data.table)
labels<-read_csv('cassava-leaf-disease-classification//train.csv')
head(labels)
dataloader <- fastai::ImageDataLoaders_from_df(df=labels, path=path_img, bs=8, seed=6, device = 'cuda', 
                                               num_workers=0, 
                                               item_tfms = Resize(448),
                                               batch_tfms = aug_transforms(size=224, min_scale=0.75))

num_workers=0 is mandatory to not have the error "RuntimeError: DataLoader worker (pid(s) 482) exited unexpectedly".

dataloader %>% show_batch()
learnR <- dataloader %>% cnn_learner(xresnet50(), metrics = accuracy,  model_dir="fastai_model/") #prettier

To save computation power :

learnR$to_fp16()
learnR$freeze()
learnR %>% lr_find()
learnR %>% plot_lr_find(dpi = 200)

Here what was creating the bug :

device = 'cuda', inside the call to create dataloader.
Removing it make the code works as intended.

@turgut090
Copy link
Member

Could you share session info as well, please?

@Cdk29
Copy link
Contributor Author

Cdk29 commented Dec 22, 2020

@henry090 here it is :

R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.10

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
[1] LC_CTYPE=fr_FR.UTF-8 LC_NUMERIC=C LC_TIME=fr_FR.UTF-8 LC_COLLATE=fr_FR.UTF-8
[5] LC_MONETARY=fr_FR.UTF-8 LC_MESSAGES=fr_FR.UTF-8 LC_PAPER=fr_FR.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] fastai_2.0.3 forcats_0.5.0 stringr_1.4.0 dplyr_1.0.2 purrr_0.3.4 readr_1.4.0 tidyr_1.1.2
[8] tibble_3.0.4 ggplot2_3.3.2 tidyverse_1.3.0

loaded via a namespace (and not attached):
[1] httr_1.4.2 pkgload_1.1.0 jsonlite_1.7.2 modelr_0.1.8 assertthat_0.2.1 cellranger_1.1.0
[7] yaml_2.2.1 remotes_2.2.0 sessioninfo_1.1.1 pillar_1.4.7 backports_1.2.1 lattice_0.20-41
[13] glue_1.4.2 reticulate_1.18-9000 digest_0.6.27 rvest_0.3.6 colorspace_2.0-0 htmltools_0.5.0
[19] Matrix_1.2-18 pkgconfig_2.0.3 devtools_2.3.2 broom_0.7.2 haven_2.3.1 scales_1.1.1
[25] processx_3.4.5 generics_0.1.0 usethis_2.0.0 ellipsis_0.3.1 withr_2.3.0 cli_2.2.0
[31] magrittr_2.0.1 crayon_1.3.4 readxl_1.3.1 memoise_1.1.0 evaluate_0.14 ps_1.5.0
[37] fs_1.5.0 fansi_0.4.1 xml2_1.3.2 pkgbuild_1.1.0 tools_4.0.2 prettyunits_1.1.1
[43] hms_0.5.3 lifecycle_0.2.0 munsell_0.5.0 reprex_0.3.0 callr_3.5.1 compiler_4.0.2
[49] rlang_0.4.9 grid_4.0.2 rstudioapi_0.13 rappdirs_0.3.1 rmarkdown_2.5 testthat_3.0.0
[55] gtable_0.3.0 curl_4.3 DBI_1.1.0 R6_2.5.0 lubridate_1.7.9.2 knitr_1.30
[61] rprojroot_2.0.2 desc_1.2.0 stringi_1.5.3 Rcpp_1.0.5 vctrs_0.3.5 png_0.1-7
[67] dbplyr_2.0.0 tidyselect_1.1.0 xfun_0.19

@turgut090
Copy link
Member

Could you install reticulate from Github (then restart RStudio). Then remove num_workers argument and run again, please? Kevin from RStudio fixed that issue with reticulate.

@Cdk29
Copy link
Contributor Author

Cdk29 commented Dec 22, 2020

Okay. With or without device = 'cuda' ?

@turgut090
Copy link
Member

If you have Cuda, skip that as well.

@Cdk29
Copy link
Contributor Author

Cdk29 commented Dec 22, 2020

Okay. I wait for the current test to finish and then I will run it with the requested changes on more epoch.
I will add the link to the md document with the session infos when it will have finish running.

@Cdk29
Copy link
Contributor Author

Cdk29 commented Dec 22, 2020

@henry090 it works perfectly : https://github.com/Cdk29/Cassava/blob/main/trying-resnext-with-r-and-fastai.md

@turgut090
Copy link
Member

I close this issue, then. If you face an issue, again, please reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants