项目备忘录(一)

Conda 操作

均在 WSL 下操作

创建环境

1
conda create --name py36 python=3.6

激活环境

1
source activate py36

取消激活

1
conda deactivate py36

安装 pytorch(CUDA=11.7)

1
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

导出环境

1
conda env export > filename

导入环境

1
conda env create -n wbw_tgan -f tgan.env

Progressive GAN

相关资源

Github

数据集下载:https://nihcc.app.box.com/v/ChestXray-NIHCC/folder/37178474737

Conda 环境:

代码修改

因为其要求的 torch 是 1.10,而已经配置了的 torch 版本是 2.0.1,无法用 pip 运行,所以直接使用其源代码运行

  • train.py 添加了 import sys

  • parser--num_channels 参数的默认值改成了 1,因为是灰度图像。

  • pro_gan_pytorch.custom_layer.EqualizedConvTranspose2d 中的 forward 函数使用的 _output_padding 函数添加参数 self.stride[0] - 1。因为新版的 pytorch 取消了 num_spatial_dims 参数的默认值。

  • 加载训练数据时,添加了 transforms.Grayscale(num_output_channels=1) 用于将图片中一些 rgba 图像转化为灰度图像。修改前报错如下:

    1
    Runtime Error: stack expects each tensor to be equal size, but got [1, 1024, 1024] at entry 0 and [4, 1024, 1024] at entry 2

断点训练

修改 retain 参数,模型路径在输出文件夹中,generator 和 discriminator 在同一个文件中。

记得修改 --start-depth 参数,使其从模型断点开始。

Resolution=2depth×2depth\text{Resolution} = 2^{\text{depth}} \times 2^{\text{depth}}

不用 XShell 情况下的代理配置

直连代理服务器

在用户文件夹的 .ssh 文件夹中新建 config 文件添加:

1
2
3
4
HostName hostname
Port port
User username
ProxyCommand "D:\Program Files\Git\mingw64\bin\connect.exe" -S proxy_server_name:proxy_server_port hostname port

也可以通过这种方法用 VS Code 连接,注意如果修改了保存位置,则要修改的 config 文件在对应的保存位置中。

如果在连接 ssh 的时候提示 bad permission,则参考:

https://www.shellhacks.com/bad-owner-or-permissions-on-ssh-config-solved/

最后使用 ssh 指令连接:

1
ssh username@hostname -p port -A

通过 Clash

使用 Clash 添加配置文件

1
2
3
4
5
6
7
proxies:
# socks5
- name: "socks"
type: socks5
server: server_name
port: port
socks-port: 7891

启用该配置文件,切换 Proxy 至 socks5,不用打开 System Proxy(打开也无妨)

在用户文件夹的 .ssh 文件夹中新建 config 文件:

1
2
3
4
5
Host hostname
HostName hostname
Port port
User username
ProxyCommand "D:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:7890 hostname port

hostname、port、username 修改成要连接的服务器的信息。

其中的路径根据电脑上 Git 的路径来换(因为借助了 Git 的代理功能)

用 Pycharm 连接

配置一个 SSH 解释器即可,PyCharm 有代理功能,记得配置。

Python 后台运行

1
nohup python -u train.py > test.out 2>&1 &

实时查看输出

1
tail -f filename

设置 Python 运行时的代理

1
os.environ["http_proxy"] = "http://127.0.0.1:7890"

胸部 X 光反事实图像生成的一些设置

Progan 最终分辨率:256,起始分辨率 16

Classifier:Densenet 121,输入图像 3*224*224

一些 Pytorch 函数

tensor.all()

tensor 的 all() 函数不带参数的时候,整个 tensor 均为 True 时其返回 True,否则返回 False。

若填充 dim 参数即 all(dim=n) 时,则当第 n 维均为 True 时返回 True,否则返回 False

假设 n==0,则仅当 tensor[i][j]=True,i=1,2,3,\text{tensor}[i][j]=\text{True}, i=1,2,3, \dots 时,返回值的 tensor[j]\text{tensor}[j] 才会为 True

tensor.nonzero()

tensor 的 nonzero() 函数返回 tensor 中非 0 元素的下标。

若为 1 维 tensor,则直接返回下标

若为 2 维以上数组,则返回一个二维数组,二维数组中的每一行表示一个非 0 元素在 tensor 中各个维度的位置

NIH Chest X-ray 数据集的数据不平衡问题

该数据集内的图像类别不平衡,导致训练的多标签分类器在 Exact match ratio 指标上难以提升。

Python 字典的一些操作

1
2
3
4
5
6
7
8
9
10
11
12
dict = {...}

# 将字典中的所有键组织成一个 list
list(dict.keys())

# 遍历字典
for k, v in dict.items():
print(k, v)

# 不能在遍历字典的时候修改字典,可以等遍历完一起修改
# 删除某个键
del dict[k]