2021-07-02 20:53:00 +08:00
## Installation
There are two versions of MMCV:
2022-08-30 16:24:06 +08:00
- **mmcv**: comprehensive, with full features and various CUDA ops out of box. It takes longer time to build.
- **mmcv-lite**: lite, without CUDA ops but all other features, similar to mmcv\<1.0.0. It is useful when you do not need those CUDA ops.
2021-07-02 20:53:00 +08:00
2021-08-22 09:44:51 +08:00
```{warning}
2021-10-04 20:13:54 +08:00
Do not install both versions in the same environment, otherwise you may encounter errors like `ModuleNotFound` . You need to uninstall one before installing the other. `Installing the full version is highly recommended if CUDA is avaliable` .
2021-08-22 09:44:51 +08:00
```
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
### Install mmcv
2021-07-02 20:53:00 +08:00
2022-11-28 10:59:07 +08:00
Before installing mmcv, make sure that PyTorch has been successfully installed following the [PyTorch official installation guide ](https://pytorch.org/get-started/locally/#start-locally ). This can be verified using the following command
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
```bash
python -c 'import torch;print(torch.__version__)'
```
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
If version information is output, then PyTorch is installed.
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
#### Install with mim (recommended)
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
[mim ](https://github.com/open-mmlab/mim ) is the package management tool for the OpenMMLab projects, which makes it easy to install mmcv
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
```bash
pip install -U openmim
2023-04-06 19:22:57 +08:00
mim install mmcv
2021-07-02 20:53:00 +08:00
```
2022-10-22 12:43:27 +08:00
If you find that the above installation command does not use a pre-built package ending with `.whl` but a source package ending with `.tar.gz` , you may not have a pre-build package corresponding to the PyTorch or CUDA or mmcv version, in which case you can [build mmcv from source ](build.md ).
< details >
< summary > Installation log using pre-built packages< / summary >
Looking in links: https://download.openmmlab.com/mmcv/dist/cu102/torch1.8.0/index.html< br / >
Collecting mmcv< br / >
2023-04-06 19:22:57 +08:00
< b > Downloading https://download.openmmlab.com/mmcv/dist/cu102/torch1.8.0/mmcv-2.0.0-cp38-cp38-manylinux1_x86_64.whl< / b >
2022-10-22 12:43:27 +08:00
< / details >
< details >
< summary > Installation log using source packages< / summary >
Looking in links: https://download.openmmlab.com/mmcv/dist/cu102/torch1.8.0/index.html< br / >
2023-04-06 19:22:57 +08:00
Collecting mmcv==2.0.0< br / >
< b > Downloading mmcv-2.0.0.tar.gz< / b >
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
< / details >
2021-07-02 20:53:00 +08:00
2023-04-06 19:22:57 +08:00
To install a specific version of mmcv, for example, mmcv version 2.0.0, you can use the following command
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
```bash
2023-04-06 19:22:57 +08:00
mim install mmcv==2.0.0
2021-07-02 20:53:00 +08:00
```
2022-10-22 12:43:27 +08:00
:::{note}
If you would like to use `opencv-python-headless` instead of `opencv-python` ,
e.g., in a minimum container environment or servers without GUI,
you can first install it before installing MMCV to skip the installation of `opencv-python` .
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
Alternatively, if it takes too long to install a dependency library, you can specify the pypi source
```bash
2023-04-06 19:22:57 +08:00
mim install mmcv -i https://pypi.tuna.tsinghua.edu.cn/simple
2021-07-02 20:53:00 +08:00
```
2022-10-22 12:43:27 +08:00
:::
2023-04-06 19:22:57 +08:00
You can run [check_installation.py ](https://github.com/open-mmlab/mmcv/blob/main/.dev_scripts/check_installation.py ) to check the installation of mmcv-full after running the installation commands.
2022-10-22 12:43:27 +08:00
#### Install with pip
Use the following command to check the version of CUDA and PyTorch
```bash
python -c 'import torch;print(torch.__version__);print(torch.version.cuda)'
```
Select the appropriate installation command depending on the type of system, CUDA version, PyTorch version, and MMCV version
< html >
< body >
2023-10-18 18:46:27 +08:00
< style >
select {
/*z-index: 1000;*/
position: absolute;
top: 10px;
width: 6.7rem;
}
#select -container {
position: relative;
height: 30px;
}
#select -cmd {
background-color: #f5f6f7 ;
font-size: 14px;
margin-top: 20px;
}
/* 让每一个都间隔1.3rem */
#select -os {
/* left: 1.375rem; */
left: 0;
}
#select -cuda {
/* left: 9.375rem; 9.375 = 1.375 + 6.7 + 1.3 */
left: 8rem;
}
#select -torch {
/* left: 17.375rem; 17.375 = 9.375 + 6.7 + 1.3 */
left: 16rem;
}
#select -mmcv {
/* left: 25.375rem; 25.375 = 17.375 + 6.7 + 1.3 */
left: 24rem;
}
< / style >
< div id = "select-container" >
< select
size="1"
2022-10-22 12:43:27 +08:00
onmousedown="handleSelectMouseDown(this.id)"
2023-10-18 18:46:27 +08:00
onclick="clickOutside(this, () => handleSelectBlur(this.id))"
2022-10-22 12:43:27 +08:00
onchange="changeOS(this.value)"
id="select-os">
2023-10-18 18:46:27 +08:00
< / select >
< select
size="1"
2022-10-22 12:43:27 +08:00
onmousedown="handleSelectMouseDown(this.id)"
2023-10-18 18:46:27 +08:00
onclick="clickOutside(this, () => handleSelectBlur(this.is))"
2022-10-22 12:43:27 +08:00
onchange="changeCUDA(this.value)"
id="select-cuda">
2023-10-18 18:46:27 +08:00
< / select >
< select
size="1"
2022-10-22 12:43:27 +08:00
onmousedown="handleSelectMouseDown(this.id)"
2023-10-18 18:46:27 +08:00
onclick="clickOutside(this, () => handleSelectBlur(this.is))"
2022-10-22 12:43:27 +08:00
onchange="changeTorch(this.value)"
id="select-torch">
2023-10-18 18:46:27 +08:00
< / select >
< select
size="1"
2022-10-22 12:43:27 +08:00
onmousedown="handleSelectMouseDown(this.id)"
2023-10-18 18:46:27 +08:00
onclick="clickOutside(this, () => handleSelectBlur(this.is))"
2022-10-22 12:43:27 +08:00
onchange="changeMMCV(this.value)"
id="select-mmcv">
2023-10-18 18:46:27 +08:00
< / select >
< / div >
< pre id = "select-cmd" > < / pre >
2022-10-22 12:43:27 +08:00
< / body >
< script >
2023-10-18 18:46:27 +08:00
// 各个select当前的值
2022-10-22 12:43:27 +08:00
let osVal, cudaVal, torchVal, mmcvVal;
2023-10-18 18:46:27 +08:00
function clickOutside(targetDom, handler) {
const clickHandler = (e) => {
if (!targetDom || targetDom.contains(e.target)) return;
handler?.();
document.removeEventListener('click', clickHandler, false);
};
document.addEventListener('click', clickHandler, false);
}
2022-10-22 12:43:27 +08:00
function changeMMCV(val) {
mmcvVal = val;
change("select-mmcv");
}
function changeTorch(val) {
torchVal = val;
change("select-torch");
}
function changeCUDA(val) {
cudaVal = val;
change("select-cuda");
}
function changeOS(val) {
osVal = val;
change("select-os");
}
2023-10-18 18:46:27 +08:00
// 控制size大小相关的几个方法
2022-10-22 12:43:27 +08:00
function handleSelectMouseDown(id) {
const dom = document.getElementById(id);
if (!dom) return;
const len = dom?.options?.length;
2023-10-18 18:46:27 +08:00
if (len >= 10) {
2022-10-22 12:43:27 +08:00
dom.size = 10;
dom.style.zIndex = 100;
}
}
function handleSelectClick() {
const selects = Array.from(document.getElementsByTagName("select"));
selects.forEach(select => {
select.size = 1;
});
}
function handleSelectBlur(id) {
const dom = document.getElementById(id);
if (!dom) {
2023-10-18 18:46:27 +08:00
// 如果没有指定特定的id, 那就直接把所有的select都设置成size = 1
2022-10-22 12:43:27 +08:00
handleSelectClick();
return;
}
dom.size = 1;
dom.style.zIndex = 1;
}
function changeCmd() {
const cmd = document.getElementById("select-cmd");
let cmdString = "pip install mmcv=={mmcv_version} -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.html";
// e.g: pip install mmcv==2.0.0rc1 -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9/index.html
2022-11-28 10:59:07 +08:00
let cudaVersion;
if (cudaVal === "cpu" || cudaVal === "mps") {
cudaVersion = "cpu";
} else {
cudaVersion = `cu${cudaVal.split(".").join("")}` ;
}
2022-10-22 12:43:27 +08:00
const torchVersion = `torch${torchVal.substring(0, torchVal.length - 2)}` ;
cmdString = cmdString.replace("{cu_version}", cudaVersion).replace("{mmcv_version}", mmcvVal).replace("{torch_version}", torchVersion);
cmd.textContent = cmdString;
}
2023-10-18 18:46:27 +08:00
// string数组去重
2022-10-22 12:43:27 +08:00
function unique(arr) {
if (!arr || !Array.isArray(arr)) return [];
return [...new Set(arr)];
}
2023-10-18 18:46:27 +08:00
// 根据string数组生成option的DocumentFragment
2022-10-22 12:43:27 +08:00
function genOptionFragment(data, id) {
const name = id.includes("-")? id.split("-")[1] : id;
const fragment = new DocumentFragment();
data.forEach(option => {
const ele = document.createElement("option");
let text = `${name} ${option}` ;
2022-11-28 10:59:07 +08:00
if (name === "os" || option.toUpperCase() === "CPU" || option.toUpperCase() === "MPS") {
2022-10-22 12:43:27 +08:00
text = `${option}` ;
}
ele.textContent = text;
2023-10-18 18:46:27 +08:00
// 添加value属性, 方便下拉框选择时直接读到数据
2022-10-22 12:43:27 +08:00
ele.value = option;
2023-10-18 18:46:27 +08:00
// 添加点击事件监听
2022-10-22 12:43:27 +08:00
ele.addEventListener('click', handleSelectClick);
fragment.appendChild(ele);
});
return fragment;
}
2023-10-18 18:46:27 +08:00
// 在dom树中找到id对应的dom( select元素) , 并将生成的options添加到元素内
2022-10-22 12:43:27 +08:00
function findAndAppend(data, id) {
const fragment = genOptionFragment(data, id);
const dom = document.getElementById(id);
if (dom) dom.replaceChildren(fragment);
}
2023-10-18 18:46:27 +08:00
/**
* change方法的重点在于
* 1. 各个下拉框数据的联动
* OS ==> cuda ==> torch ==> mmcv
* 2. 命令行的修改
*/
2022-10-22 12:43:27 +08:00
function change(id) {
const order = ["select-mmcv", "select-torch", "select-cuda", "select-os"];
const idx = order.indexOf(id);
if (idx === -1) return;
const versionDetail = version[osVal];
if (idx >= 3) {
2023-10-18 18:46:27 +08:00
// 根据os修改cuda
2022-10-22 12:43:27 +08:00
let cuda = [];
versionDetail.forEach(v => {
cuda.push(v.cuda);
});
cuda = unique(cuda);
cudaVal = cuda[0];
findAndAppend(cuda, "select-cuda");
}
if (idx >= 2) {
2023-10-18 18:46:27 +08:00
// 根据cuda修改torch
2022-10-22 12:43:27 +08:00
const torch = [];
versionDetail.forEach(v => {
if (v.cuda === cudaVal) torch.push(v.torch);
});
torchVal = torch[0];
findAndAppend(torch, "select-torch");
}
if (idx >= 1) {
2023-10-18 18:46:27 +08:00
// 根据torch修改mmcv
2022-10-22 12:43:27 +08:00
let mmcv = [];
versionDetail.forEach(v => {
if (v.cuda === cudaVal & & v.torch === torchVal) mmcv = v.mmcv;
});
mmcvVal = mmcv[0];
findAndAppend(mmcv, "select-mmcv");
}
changeCmd();
}
2023-10-18 18:46:27 +08:00
// 初始化, 处理version数据, 并调用findAndAppend
2022-10-22 12:43:27 +08:00
function init() {
2023-10-18 18:46:27 +08:00
// 增加一个全局的click事件监听, 作为select onBlur事件失效的兜底
// document.addEventListener("click", handleSelectBlur);
2022-10-22 12:43:27 +08:00
const version = window.version;
2023-10-18 18:46:27 +08:00
// OS
2022-10-22 12:43:27 +08:00
const os = Object.keys(version);
osVal = os[0];
findAndAppend(os, "select-os");
change("select-os");
changeCmd();
}
2023-10-18 18:46:27 +08:00
// 利用xhr获取本地version数据, 如果作为html直接浏览的话需要使用本地服务器打开, 否则会有跨域问题
2022-10-22 12:43:27 +08:00
window.onload = function () {
const url = "../_static/version.json"
2023-10-18 18:46:27 +08:00
// 申明一个XMLHttpRequest
2022-10-22 12:43:27 +08:00
const request = new XMLHttpRequest();
2023-10-18 18:46:27 +08:00
// 设置请求方法与路径
2022-10-22 12:43:27 +08:00
request.open("get", url);
2023-10-18 18:46:27 +08:00
// 不发送数据到服务器
2022-10-22 12:43:27 +08:00
request.send(null);
2023-10-18 18:46:27 +08:00
//XHR对象获取到返回信息后执行
2022-10-22 12:43:27 +08:00
request.onload = function () {
2023-10-18 18:46:27 +08:00
// 返回状态为200, 即为数据获取成功
2022-10-22 12:43:27 +08:00
if (request.status !== 200) return;
const data = JSON.parse(request.responseText);
window.version = data;
init();
}
}
< / script >
< / html >
If you do not find a corresponding version in the dropdown box above, you probably do not have a pre-built package corresponding to the PyTorch or CUDA or mmcv version, at which point you can [build mmcv from source ](build.md ).
:::{note}
2022-08-30 16:24:06 +08:00
mmcv is only compiled on PyTorch 1.x.0 because the compatibility
2021-10-13 20:25:49 +08:00
usually holds between 1.x.0 and 1.x.1. If your PyTorch version is 1.x.1, you
2022-08-30 16:24:06 +08:00
can install mmcv compiled with PyTorch 1.x.0 and it usually works well.
2022-10-22 12:43:27 +08:00
For example, if your PyTorch version is 1.8.1, you can feel free to choose 1.8.x.
:::
:::{note}
If you would like to use `opencv-python-headless` instead of `opencv-python` ,
e.g., in a minimum container environment or servers without GUI,
you can first install it before installing MMCV to skip the installation of `opencv-python` .
2021-10-13 20:25:49 +08:00
2022-10-22 12:43:27 +08:00
Alternatively, if it takes too long to install a dependency library, you can specify the pypi source
```bash
2023-04-06 19:22:57 +08:00
mim install mmcv -i https://pypi.tuna.tsinghua.edu.cn/simple
2021-10-13 20:25:49 +08:00
```
2022-10-22 12:43:27 +08:00
:::
2021-07-02 20:53:00 +08:00
2023-04-06 19:22:57 +08:00
You can run [check_installation.py ](https://github.com/open-mmlab/mmcv/blob/main/.dev_scripts/check_installation.py ) to check the installation of mmcv after running the installation commands.
2022-10-22 12:43:27 +08:00
#### Using mmcv with Docker
Build with local repository
```bash
git clone https://github.com/open-mmlab/mmcv.git & & cd mmcv
docker build -t mmcv -f docker/release/Dockerfile .
2022-03-23 23:41:51 +08:00
```
2022-10-22 12:43:27 +08:00
Or build with remote repository
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
```bash
2023-04-06 19:22:57 +08:00
docker build -t mmcv https://github.com/open-mmlab/mmcv.git#main:docker/release
2021-07-02 20:53:00 +08:00
```
2022-10-22 12:43:27 +08:00
The [Dockerfile ](release/Dockerfile ) installs latest released version of mmcv-full by default, but you can specify mmcv versions to install expected versions.
2021-07-02 20:53:00 +08:00
2022-10-22 12:43:27 +08:00
```bash
2023-04-06 19:22:57 +08:00
docker image build -t mmcv -f docker/release/Dockerfile --build-arg MMCV=2.0.0 .
2022-10-22 12:43:27 +08:00
```
If you also want to use other versions of PyTorch and CUDA, you can also pass them when building docker images.
An example to build an image with PyTorch 1.11 and CUDA 11.3.
```bash
docker build -t mmcv -f docker/release/Dockerfile \
--build-arg PYTORCH=1.11.0 \
--build-arg CUDA=11.3 \
--build-arg CUDNN=8 \
2023-04-06 19:22:57 +08:00
--build-arg MMCV=2.0.0 .
2022-10-22 12:43:27 +08:00
```
More available versions of PyTorch and CUDA can be found at [dockerhub/pytorch ](https://hub.docker.com/r/pytorch/pytorch/tags ).
### Install mmcv-lite
If you need to use PyTorch-related modules, make sure PyTorch has been successfully installed in your environment by referring to the [PyTorch official installation guide ](https://github.com/pytorch/pytorch#installation ).
2021-07-02 20:53:00 +08:00
```python
2022-08-30 16:24:06 +08:00
pip install mmcv-lite
2021-07-02 20:53:00 +08:00
```