使神经网络更易于解释,用于研究和艺术
torch-dreams的Python项目详细描述
火炬之梦
使神经网络更易于解释,用于研究和艺术。在
pip install torch-dreams
目录
快速入门
^{pr2}$Make sure you also check out the quick start colab notebook and the docs for more interesting examples.
可视化单个通道
这部分火炬梦的灵感来自Feature visualization by Olah, et al。我们基本上是对输入图像进行优化,以最大限度地激活神经网络中某一层的某个通道。在
首先,让我们选择要处理的层。随意玩玩其他图层。在
layers_to_use=[model.Mixed_6c.branch7x7_1.conv]
下一步是定义一个custom_func
,它将允许use有选择地优化单个通道。在
defmy_custom_func(layer_outputs):loss=layer_outputs[0][7].mean()## 7th channel of first layer from layers_to_usereturnloss
其余部分实际上与快速入门片段非常相似:
config={"image_path":"noise.jpg","layers":layers_to_use,"octave_scale":1.1,"num_octaves":20,"iterations":100,"lr":0.04,"max_rotation":0.7,"custom_func":my_custom_func,}out=dreamy_boi.deep_dream(config)plt.imshow(out)plt.show()
如果事情按计划进行,你最终会得到这样的结果:
仔细观察
在config
中,我们可以定制我们希望优化发生的确切方式。下面是一个不使用渐变遮罩的示例:
config={"image_path":"your_image.jpg","layers":[model.Mixed_6c.branch1x1],"octave_scale":1.2,"num_octaves":10,"iterations":20,"lr":0.03,"custom_func":None,"max_rotation":0.5,"gradient_smoothing_coeff":0.1,"gradient_smoothing_kernel_size":3}
- 在
在image_path
:指定到输入图像的相对路径。在 - 在
layers
:输出将被“存储”以供以后优化的层列表。例如,如果我们要使用2层:config["layers"]=[model.Mixed_6d.branch1x1,model.Mixed_5c]
在 - 在
在octave_scale
:在每一个倍频程后图像按比例放大的因子。在 - 在
在num_octaves
:为恢复原始大小而放大图像的次数。在 - 在
在iterations
:每倍频程的梯度上升步数。在 - 在
在lr
:在梯度上升的每个步骤中使用的学习速率。在 - 在
custom_func
(可选):使用它来构建您自己的自定义优化函数,以便对单个频道/单元等进行优化例如,如果我们想同时优化第一层的第47通道和第二层的22通道:
defmy_custom_func(layer_outputs):loss=layer_outputs[0][47].mean()+layer_outputs[1][22].mean()returnlossconfig["custom_func"]=my_custom_func
在 - 在
在max_rotation
(可选):大写要应用于图像的最大旋转。在 - 在
在gradient_smoothing_coeff
(可选):更高->更强的模糊。在 - 在
在gradient_smoothing_kernel_size
:(可选)应用高斯模糊时要使用的内核大小。在
重要环节:
- 项目
标签: