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

## 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/.virtualenvs
    2.在~/.bashrc中添加行:
        export WORKON_HOME=$HOME/.virtualenvs
        source /usr/local/bin/virtualenvwrapper.sh
    3.运行:
        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 Image

def understanding_cityscape(pth):
    # we will show the different pixel values
    diff_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.shape
    for 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 imageio


if __name__ == '__main__':
    pth = os.getcwd()
    DIRS = os.listdir(pth)
    
    for DIR in DIRS:
        if os.path.isfile(DIR):
            continue
        path = os.path.join(pth, DIR, 'test_latest', 'stuttgart_00')# target directory
        files = 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 = 28
        b = 33
        reals.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, :] = real
            frame[:, 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的测试工作。

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

  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 44
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值