t-kalinowski / deep-learning-with-R-2nd-edition-code

Code from the book "Deep Learning with R, 2nd Edition"
https://blogs.rstudio.com/ai/posts/2022-05-31-deep-learning-with-r-2e/
41 stars 18 forks source link

Issue downloading Kaggle dogs-vs-cats file #11

Closed jonbry closed 5 months ago

jonbry commented 5 months ago

I'm having issues downloading the dog-vs-cat zip file for the example in Chapter 8 using the API. I accepted the T&Cs and verified the .kaggle folder is in the right place on my Mac, but I can't seem to get the actual download to work. I keep getting the following error:

system('kaggle competitions download -c dogs-vs-cats')

#> sh: kaggle: command not found
#> Warning: error in running command

I was able to download it once after a few tries, but never again. The code was the same so I'm not sure why it only worked once. For a weird set of circumstances I need to go back and redownload it, but I can't seem to get it to work. Here's the output of reticulate::py_list_packages() if it is helpful in diagnosing the issue:

> reticulate::py_list_packages()
              package     version
1              bleach       6.1.0
2             certifi    2024.2.2
3  charset-normalizer       3.3.2
4                idna         3.6
5              kaggle       1.6.8
6     python-dateutil 2.9.0.post0
7      python-slugify       8.0.4
8            requests      2.31.0
9                 six      1.16.0
10     text-unidecode         1.3
11               tqdm      4.66.2
12            urllib3       2.2.1
13       webencodings       0.5.1
                    requirement
1                 bleach==6.1.0
2             certifi==2024.2.2
3     charset-normalizer==3.3.2
4                     idna==3.6
5                 kaggle==1.6.8
6  python-dateutil==2.9.0.post0
7         python-slugify==8.0.4
8              requests==2.31.0
9                   six==1.16.0
10          text-unidecode==1.3
11                 tqdm==4.66.2
12               urllib3==2.2.1
13          webencodings==0.5.1

I know I can download the files directly, but I was hoping to resolve the issue for downloading more Kaggle Datasets in the future. Let me know what other information I can provide.

Thank you!

t-kalinowski commented 5 months ago

The kaggle command is placed on your PATH when Python is initialized (assuming you installed it into the same Python environment that keras was installed into). The following should work:

library(keras3)

# force reticulate to initialize Python
invisible(op_add(1, 1)) 

# Now 'kaggle' should be on the PATH
nzchar(Sys.which("kaggle"))
system('kaggle competitions download -c dogs-vs-cats') 
jonbry commented 5 months ago

Thanks for getting back to me so quickly! You and I both assumed that it was installed in the same python environment as keras, but it was not. I think I hadn't loaded keras yet, so it had defaulted to the r-reticulate environment to install kaggle.

If terminal shows (r-reticulate)(base), does that mean reticulate has initialized Python?

t-kalinowski commented 5 months ago

If terminal shows (r-reticulate)(base), does that mean reticulate has initialized Python?

The "base" I think only appears if you're using conda somewhere. The double ()() also indicates that there are two activation hooks being run somewhere. Best to get rid of those.

I would look in ~/.bashrc, ~/.zshrc, ~/.bash_profile, ~/.profile, etc. to see if there any conda activation hooks somewhere, and remove them or comment them out if possible.

Then, hopefully you would only see (r-reticulate). If you see that, then make sure you know what is activating it. If you're in the RStudio IDE, it will activate the inferred python venv if you have "Enable Python integration" checked in the Terminal options.


You can install kaggle into the keras venv with

reticulate::py_install("kaggle", envname = "r-keras")
# or, if keras3 is loaded, py_install should find the correct env

library(keras3)
reticulate::py_config() # initialize python, confirm it is 'r-keras'
reticulate::py_install("kaggle") # it should use 'r-keras' venv be default now
jonbry commented 5 months ago

Ah, that makes sense. I found the conda hook and I'm back to single (). Thanks for all of your help!