我正在尝试将 kaggle imagenet 对象本地化挑战数据下载到 google colab 中,以便我可以用它来训练我的模型。 Kaggle 使用 API 来轻松快速地访问其数据集。 (https://github.com/Kaggle/kaggle-api https://github.com/Kaggle/kaggle-api)但是,当在 google colab 中调用命令“kaggle racings download -c imagenet-object-localization-challenge”时,它找不到包含我的用户名和 api-key 的 kaggle.json 文件。
我在 Mac 上运行 jupyter 笔记本时没有遇到过这个问题,但由于我想为我的模型使用 google 的 gpu,所以我开始使用 google colab。因为 Kaggle API 期望用户名和 api-key 位于 .kaggle 目录中的 kaggle.json 文件中,所以我首先创建了目录 .kaggle,然后创建了文件 kaggle.json,在其中写入了我的用户名和 api- key(下面的示例不显示我的用户名和 api-key)。然后,我尝试配置 json 文件的路径,以供 kaggle 在调用 kaggle 下载命令时使用。
!pip install kaggle
!mkdir .kaggle
!touch .kaggle/kaggle.json
api_token = {"username":"username","key":"api-key"}
import json
import zipfile
import os
with open('/content/.kaggle/kaggle.json', 'w') as file:
json.dump(api_token, file)
!chmod 600 /content/.kaggle/kaggle.json
!kaggle config path -p /content
但是,当运行最后一个命令时,我收到错误:
IOError: Could not find kaggle.json. Make sure it's located in /root/.kaggle. Or use the environment method.
我的目标是使用以下命令从 kaggle 获取数据集:
!kaggle competitions download -c imagenet-object-localization-challenge
os.chdir('/content/competitions/imagenet-object-localization-challenge')
for file in os.listdir():
zip_ref = zipfile.ZipFile(file, 'r')
zip_ref.extractall()
zip_ref.close()
我不明白为什么 kaggle API 找不到我的 json 文件。如何使用 google colab 中的 API?