常用命令码住
%cd /content/drive/My\ Drive/ME-D2N-test # 进入文件夹`
%pwd`
!mkdir /content/datasets # 在当前目录下("/content")创建一个叫datasets的文件夹
1如何运行Github项目
# 克隆仓库到/content/my-repo目录下,在命令前加上感叹号“!”运行的就是linux命令
!git clone https://github.com/my-github-username/my-git-repo.git
%cd my-git-repo
!./train.py --logdir /my/log/path --data_root /my/data/root --resume
2如何运行本地项目
直接上传到谷歌云盘,在colab notebook 页面点击加载云盘就能在找到,同理 !python XX.py 即可
有些colab没有的包,!pip XX 即可
3如何使用存放在本地的数据集
将数据集以压缩包形式上传到谷歌云盘,然后解压到Colab实例空间
挂载云盘不消耗时间,解压所需的时间远远小于上传数据集的时间。
此外,由于实例空间会定期释放,因此模型训练完成后的日志也应该储存在谷歌云盘上。综上所述,谷歌云盘是使用Colab必不可少的一环,由于免费的云盘只有15个G,因此建议至少拓展到基本版。
解压
!unzip metallic-container.zip -d my_zip
!tar -xvf "/content/drive/MyDrive/mini-imagenet/train.tar" -C "/content/drive/MyDrive/ME-D2N-test/datasets/miniImagenet"
4如何在线下载数据集(推荐,亲测巨快)
(上传本地的太慢&占空间,一次性使用或低频率使用随时下载就行
打开 kaggle 网页的数据集 https://www.kaggle.com/datasets,搜索你需要下载的数据集
以下载 MARS 数据集为例,点击New Notebook - Copy API command
,会复制如下命令kaggle datasets download -d twoboysandhats/mars-motion-analysis-and-reidentification-set
登录 Kaggle 账户,点击头像->Account->Create New API Token,会生成一个 json 文件,里面包含 username 和 key 的键值对
在 Colab 中安装 kaggle 包,并生成 kaggle 用户名密码 json 文件 。其实,就是根据上面获取的 API Token 生成新的 json 文件,默认位置在 '/content/kaggle.json'。
在 Jupyter 中依次执行下面的命令,token = {"username":"XXX","key":"XXXXXXXXXXXX"}中的 xxxx 根据刚才生成的 API Token 进行填写。with执行不了也没关系,反正最后能成功下载
pip install kaggle
import json
token = {"username":"XXX","key":"XXXXXXXXXXXX"}
with open('/content/kaggle.json', 'w') as file:
json.dump(token, file)
!mkdir -p ~/.kaggle
!cp /content/kaggle.json ~/.kaggle/
!chmod 600 ~/.kaggle/kaggle.json
!kaggle config set -n path -v /content
然后在 Colab 的 Jupyter 中输入复制得到的 Copy API command
进行下载
!kaggle datasets download -d twoboysandhats/mars-motion-analysis-and-reidentification-set
如果需要把数据集保存到谷歌云盘中,可以使用 !cp /content/datasets/dataset.zip /content/drive/MyDrive,将数据集复制到谷歌云盘中。
5如何debug
import ipdb
在原来想要打断点的地方加入代码 ipdb.set_trace(context=10), 就会停在那里并显示上下5行代码
Command | Description |
---|---|
h(elp) | Show various commands supported by ipdb |
h(elp) COMMAND | Show description of the COMMAND specificed |
c(ontinue) | Continue executing till it hits another breakpoint |
n(ext) | Execute till next line in the same code frame. So if there is a function it wouldn't step into that function but execute it. |
s(tep) | Step to next code, so if its a function, it will step into the function. |
r(eturn) | Execute code till it returns from the current function or hits another breakpoint. |
l(ist) | Show more of the source code surrounding the line. |
w(here) | Shows the stacktrace i.e. the chain of functions that made it reach the current function |
a(rguments) | List of arguments passed and its values to the function |
q(uit) | Immediately stop execution and quit the debugger |