## Prerequisites

- Linux or macOS
- Python 3
- NVIDIA GPU + CUDA cuDNN
- PyTorch 0.4
但一般的话我们为了保护已有的环境,通常对于每一个项目新建一个虚拟环境,过程如下:

""" Linux 下安装虚拟环境 """
# 升级 pip
pip install --upgrade pip# 安装必要的库
sudo pip install virtualenv
sudo pip install virtualenvwrapper# 配置环境1.创建目录用来存放虚拟环境mkdir $HOME/.virtualenvs2.在~/.bashrc中添加行:export WORKON_HOME=$HOME/.virtualenvssource /usr/local/bin/virtualenvwrapper.sh3.运行:source ~/.bashrc# 查看虚拟机
virtualenv --version# 新建虚拟环境
mkvirtualenv <虚拟环境名称># 查看已存在的虚拟环境
workon# 使用指定的虚拟环境
workon <虚拟环境名称># 退出虚拟环境
deactivate# 有时候重启终端会出现:找不到 workon 的情况,
source virtualenvwrapper.sh
# 再输入 workon 就可以了# 删除虚拟环境
rmvirtualenv <虚拟环境名称>

## Getting Started
### Installation
- Install python libraries [dominate](https://github.com/Knio/dominate) and requests.
- 第一步,安装必要的库

pip install dominate requests

- If you plan to train with face datasets, please install dlib.

- * 如果想要在 face 的数据集上训练,安装额外的库

pip install dlib

- If you plan to train with pose datasets, please install [DensePose]

- 在 OpenPose 上训练

(https://github.com/facebookresearch/DensePose) and/or [OpenPose](https://github.com/CMU-Perceptual-Computing-Lab/openpose).
- Clone this repo:

- 下载项目

git clone https://github.com/NVIDIA/vid2vid
cd vid2vid

下面我们所有指令,除了特别说明外,都是在“./vid2vid”路径下执行的。

### 测试阶段

- Docker Image
If you have difficulty building the repo, a docker image can be found in the `docker` folder.

### Testing

- Please first download example dataset by running `python scripts/download_datasets.py`.

- 下载测试用的样本数据集

python scripts/download_datasets.py

我们不妨先看一下代码要做什么:

import os
from download_gdrive import *file_id = '1rPcbnanuApZeo2uc7h55OneBkbcFCnnf'
chpt_path = './datasets/'
# 定义路径
if not os.path.isdir(chpt_path):os.makedirs(chpt_path)
# 如果不存在此路径,则新建它
destination = os.path.join(chpt_path, 'datasets.zip')
# 定义目标路径
download_file_from_google_drive(file_id, destination)
# 下载文件到目标路径
unzip_file(destination, chpt_path)
# 解压缩文件

下载完毕后我们在“/vid2vid”目录下会有一个新的文件夹“datasets”,其中有三个新的文件夹。

以Cityscapes为例,我们看一下其训练测试数据集的组成。其中的配对应该是这样子的:

训练数据
______________________________________________
train_A        --实例分割mask,对应每一帧
|
train_B        --对应每个mask的真实视频帧(也就是目标生成)
|
train_inst     --区分视频帧的前景(如车、人等)与背景的mask测试数据
______________________________________________
test_A         --实例分割mask,对应每一帧
|
test_inst      --区分视频帧的前景(如车、人等)与背景的mask

其中,我们写一个函数来看看原视频(train_A/test_A)里的视频帧的格式。

我们可以看到,每一个实例分割的mask是一个灰度图,由描述object实例的色块组成,其中,不同的object的部分的灰度值不同。下面的代码计算了某个mask上不同的像素值,结果如下:

# ./vid2vid/data_understanding.py
import os
import numpy as np
from PIL import Imagedef understanding_cityscape(pth):# we will show the different pixel valuesdiff_pixels = []file = os.listdir(pth)[0]img = Image.open(os.path.join(pth, file)).convert('L')img_arr = np.array(img)H, W = img_arr.shapefor h in range(H):for w in range(W):pix = img_arr[h, w]if pix not in diff_pixels:diff_pixels.append(pix)print("There are totally {}/{} different values!".format(len(diff_pixels), H*W))print("They are: ")print(diff_pixels)if __name__ == '__main__':pth_cityscape = os.path.join(os.getcwd(), 'datasets', 'Cityscapes', 'train_A', 'seq0000')understanding_cityscape(pth_cityscape)

- Next, download and compile a snapshot of [FlowNet2](https://github.com/NVIDIA/flownet2-pytorch) by running `python scripts/download_flownet2.py`.

- 下面我们打算安装 FlowNet2 ,这应该是本项目最大的一个挑战。

1)下载 FlowNet2 项目;

python scripts/download_flownet2.py

结果,在vid2vid目录下会有一个新的文件夹“models”,里边有一个"flownet2_pytorch"的工程文件夹,如下图:

2) 下载 FlowNet2 的预训练模型

python scripts/download_models_flownet2.py

结果,在“vid2vid/models/flownet2_pytorch”会新增一个“FlowNet2_checkponts.pth.tar”的压缩包,注意我们不需要解压缩!!!

3)编译安装

# flownet2 中的 Readme 文件
# get flownet2-pytorch source
git clone https://github.com/NVIDIA/flownet2-pytorch.git
cd flownet2-pytorch# install custom layers
bash install.sh

我们看一下“install.sh”这个文件要做什么:

#!/bin/bash
cd ./networks/correlation_package
chmod u+x make.sh
./make.sh
cd ../resample2d_package
chmod u+x make.sh
./make.sh
cd ../channelnorm_package
chmod u+x make.sh
./make.sh
cd ..

原来是打算编译这三个文件夹下的C++ CUDA代码。

知道了目的后,我们为了防止出错,可以一个一个来,

cd ./networks/correlation_package
chmod u+x make.sh
./make.sh

在这里我执行到第三个命令的时候出现了以下的问题:

Compiling correlation kernels by nvcc...
rm: cannot remove '../_ext': No such file or directory
Traceback (most recent call last):
  File "build.py", line 3, in <module>
    import torch.utils.ffi
  File "/usr/local/lib/python2.7/dist-packages/torch/utils/ffi/__init__.py", line 1, in <module>
    raise ImportError("torch.utils.ffi is deprecated. Please use cpp extensions instead.")
ImportError: torch.utils.ffi is deprecated. Please use cpp extensions instead.

查了一些文章,说是要使用低于1.0.0版本的pytorch,果然还是打不过版本问题;

如果使用pytorch 0.4的话,还需要安装 cffi,

pip install cffi

cd ./networks/resample2d_package
chmod u+x make.sh
./make.sh

cd ./networks/channelnorm_package
chmod u+x make.sh
./make.sh

期间可能会提示类似于:

cannot remove 'ChannelNorm_kernel.o': No such file or directory
rm: cannot remove '../_ext': No such file or directory

不管它,本来就是要删除掉的。

- Cityscapes 在CityScapes下测试
- Please download the pre-trained Cityscapes model by:

- 下载预训练模型

- 下载关于道路(street)转换的与训练模型

我们不妨也先看看下载的代码:

# vid2vid/scripts/street/download_models.py
import os
from download_gdrive import *file_id = '1MKtImgtnGC28EPU7Nh9DfFpHW6okNVkl'
chpt_path = './checkpoints/'
if not os.path.isdir(chpt_path):os.makedirs(chpt_path)
destination = os.path.join(chpt_path, 'models.zip')
download_file_from_google_drive(file_id, destination)
unzip_file(destination, chpt_path)

这整一个过程需要翻墙到Google Drive,所以需要VPN的支持;下载需要时间,此件可以看看其他的论文。

python scripts/street/download_models.py

完了后,我们发现在“.vid2vid”目录下会有一个新的文件夹“checkpoints”,结构如下:

其中,label2city_2048是在多核训练的结果;label2city_single是在单核训练结果。

每个文件夹下又有三个模型(.pth文件) 。

- To test the model (`bash ./scripts/street/test_2048.sh`):

- 测试 Cityscapes 的模型

我们运行脚本:#!./scripts/street/test_2048.sh,其内容是一个Shell上运行Python文件的指令,如下:

python test.py --name label2city_2048 --label_nc 35 --loadSize 2048 --n_scales_spatial 3 --use_instance --fg --use_single_G# 其中,
# name指明保存文件夹名;
# label_nc指明类别数;
# loadSize是输入输出的图片长边大小;

其结果会保存在:`./results/label2city_2048/test_latest/`中。

到这里我们就基本完成了项目的“测试劫”。

- We also provide a smaller model trained with single GPU, which produces slightly worse performance at 1024 x 512 resolution.

- 我们也提供了一个更小的模型,处理图片大小为:1024x512.
- Please download the model by

python scripts/street/download_models_g1.py# 其代码内容如下:
"""
import os
from download_gdrive import *file_id = '1QoE1p3QikxNVbbTBWWRDtIspg-RcLE8y'
chpt_path = './checkpoints/'
if not os.path.isdir(chpt_path):os.makedirs(chpt_path)
destination = os.path.join(chpt_path, 'models_g1.zip')
download_file_from_google_drive(file_id, destination)
unzip_file(destination, chpt_path)
"""

- 首先下载对应的模型,同样我们还是在街道转换的任务上去做;这之后我们会下载一个新的模型,在路径“checkpoints”中。


- To test the model (`bash ./scripts/street/test_g1_1024.sh`):

- 完了后我们使用这么模型测试一下。运行脚本文件:"./scripts/street/test_g1_1024.sh"

python test.py --name label2city_1024_g1 --label_nc 35 --loadSize 1024 --n_scales_spatial 3 --use_instance --fg --n_downsample_G 2 --use_single_G

运行过程我们会发现很快!毕竟size变成了1/4.

结果如下,还是在results目录下,有一个新的文件夹“label2city_1024_g1”。


- You can find more example scripts in the `scripts/street/` directory.
- 你还可以找到更多的脚本。其实没有了,就这两个测试脚本;其他的是训练脚本。

在进入下一步之前,我们来写一个脚本,把输出的图片合成GIF,比较容易观看。

# ./vid2vid/results/gif.py
from PIL import Image
import os
import numpy as np
import imageioif __name__ == '__main__':pth = os.getcwd()DIRS = os.listdir(pth)for DIR in DIRS:if os.path.isfile(DIR):continuepath = os.path.join(pth, DIR, 'test_latest', 'stuttgart_00')# target directoryfiles = os.listdir(path)fakes = []reals = []for file in files:if file[0:4] == 'real':reals.append(file)elif file[0:4] == 'fake':fakes.append(file)a = 28b = 33reals.sort(key= lambda x:int(x[a:b]))fakes.sort(key= lambda x:int(x[a:b]))'''print(len(reals))print(len(fakse))for real in reals:print(real)for fake in fakes:print(fake)'''L = len(reals)frames = []for i in range(L):real = reals[i]fake = fakes[i]# 获取文件名real = Image.open(os.path.join(path, real)).convert('RGB')real = np.array(real)fake = Image.open(os.path.join(path, fake)).convert('RGB')fake = np.array(fake)   # 读入图片,转换为numpy数组H, W = real.shape[0], real.shape[1]frame = np.zeros((H, 2*W, 3))frame[:, 0:W, :] = realframe[:, W:2*W, :] = fake   # 数组合并frames.append(frame.astype(np.uint8))# 添加# real = imageio.imread(os.path.join(path, reals[i]))# print(real)frames.append(frame)imageio.mimsave(DIR+'.gif', frames, 'GIF', duration = 0.1)# baocunwei gif tupian

结果如下,由于图片比较大,这里只上传截图(30MB)。

- Faces

- 下面我们试着进行脸部生成的测试
- Please download the pre-trained model by:

- 首先是下载预训练的模型

python scripts/face/download_models.py


- To test the model (`bash ./scripts/face/test_512.sh`):

- 现在我们尝试测试这个模型,执行脚本文件:"#!./scripts/face/test_512.sh"

python test.py --name edge2face_512 --dataroot datasets/face/ --dataset_mode face --input_nc 15 --loadSize 512 --use_single_G

结果将会存储在"./results/edge2face_512/test_latest/"下

同样的,我们可以用前面的脚本生成GIF图,不过有些路径名需要修改;这里只给出一个截图。

- 至于第三个任务,从Pose热值图向真实帧的转换,作者在对应的“vid2vid/scripts/pose”中并没有放置模型下载脚本,所以此处忽略。

至此,我们完成了VID2VID的测试工作。

后面的工作我们在下一篇文章继续跟进。

vid2vid 代码调试+训练+测试(debug+train+test)(一)测试篇相关推荐

  1. vid2vid 代码调试+训练+测试(debug+train+test)(二)训练篇

    ### Training ### Training with Cityscapes dataset - First, download the FlowNet2 checkpoint file by ...

  2. JS And Vue代码调试——IDEA+JS+vue-devtools-dev+JetBrains IDE Support(Chrome插件)

    JS代码调试--IDEA+JavaScript Debug+JetBrains IDE Support(Chrome插件) 问题描述 解决方案 解决步骤 问题描述 调试JavaScript代码,前端框 ...

  3. PASCAL VOC数据集训练集、验证集、测试集的划分和提取,得到test.txt、train.txt、trainval.txt、val.txt文件代码

    训练集.验证集.测试集按比例精确划分 创建py文件,将下属代码放入所创建的文件里,VOC2007数据集与py文件在同一目录下 # 数据集划分 import os import randomroot_d ...

  4. ML之FE:数据处理—特征工程之数据集划分成训练集、验证集、测试集三部分简介、代码实现、案例应用之详细攻略

    ML之FE:数据处理-特征工程之数据集划分成训练集.验证集.测试集三部分简介.代码实现.案例应用之详细攻略 目录 数据集划分成训练.验证.测试三种数据的简介 1.训练集.验证集的作用 2.验证数据集 ...

  5. mysql debug log_PHP代码调试与日志

    封面.jpg 一.代码调试 由于PHP很少有类似java..NET的断点调试工具,因此通常都是要采用输出中间结果的方式进行调试,主要如下: 1.var_dump 对于可以直接打印的(如在control ...

  6. RAISR-master:google图像新压缩技术RAISR的测试代码调试记录(Python实现,没接触过python的小白,内含pip install解决方案)

    RAISR-master:google图像新压缩技术RAISR的测试代码调试记录(Python实现,没接触过python的小白,内含pip install解决方案) 参考文章: (1)RAISR-ma ...

  7. DEBUG 代码调试

    目录 1 常用调试指令 2 查看变量 ①在Variable栏:右键-->+New Watch添加一个变量,可以对变量进行持续监控 ②代码栏:右键-->Evaluate Expression ...

  8. 【YOLOv5 数据集划分】训练和验证、训练验证和测试(train、val)(train、val、test)

    [YOLOv5 数据集划分]训练和验证.训练验证和测试(train.val),(train.val.test) ①在已有测试集的情况下划分训练集和验证集 # 将图片和标注数据按比例切分为 训练集和测试 ...

  9. 11.树莓派博通BCM2835芯片手册导读与IO口驱动代码调试和测试

    11.树莓派博通BCM2835芯片手册导读与IO口驱动代码调试和测试 硬件地址的相关概念 总线地址 32位的操作系统 ,cpu最多只能访问2^32bit,即只能访问4G的内存 64位的操作系统 ,cp ...

最新文章

  1. 用大数据分析顾客会掏钱买你哪件商品
  2. 马虎的算式 - 蓝桥杯
  3. python画人口迁徙图_echarts 手把手教你画迁徙图(城市内部级别+百度地图支持)2...
  4. 【行业】点餐App未来必须面对的三大难题
  5. python 用户输入_Python 用户输入(input)
  6. 中级统计师基础知识中计算机,【2014年中级统计师《统计基础理论及相关知识》预习:计算机操作系统】- 环球网校...
  7. Microwindows及基于Nano-X的简单程序开发
  8. 7-25 朋友圈 (25 分)(详解+并查集的了解和应用)
  9. leetcode练习——栈(1)
  10. torch.nn.Parameter()
  11. Spring Security Oauth2系列(一)
  12. 大学英语 unit 2 第五题
  13. java excel 数组公式_Excel数组公式应用
  14. Matlab 实现串口助手
  15. [零基础深度学习环境配置一]基于python 3.7+anaconda 3+cuda 11.1+cuDNN v8.1.1+win10+TITAN XP
  16. guzzle php,PHP网络请求插件Guzzle使用
  17. 旅行comf HYSBZ - 1050
  18. cin gt gt n是c语言中的什么,c++中cinna是什么意思
  19. java 设置纸张大小设置_java 用itext设置pdf纸张大小操作
  20. 关闭微软Edge浏览器打开时使用推荐浏览器设置的弹窗

热门文章

  1. 慕课网 小慕机器人总结
  2. dwcs5这么写php,如何备份Dreamweaver CS5的人个设置信息
  3. Web2.0,欢迎进入加密世界 | Props 亚洲峰会
  4. 汉澳sinox运行photoshop8,dreamweaver8,flashmx2004,fireworks8
  5. 谷歌seo自建博客做外链有用吗?谷歌外链怎么做?
  6. 前后端分离项目线上部署
  7. 鸡兔同笼,共有头48个,脚132只,求鸡和兔各有多少只?
  8. MYSQL索引的作用以及如何创建索引
  9. 怎么把mysql 数据库上传到主机屋提供的空间里去?_2013.8,初次接触PHP+Mysql,在主机屋建立简单的blog的流水账...
  10. vulnhub Vegeta: 1