[英]Select specific features in a Tensorflow 1 layer
I followed this tutorial and used the ipynb notebook from the Github page to generate deepdream images in Google Colaboratory.我遵循本教程并使用Github 页面中的 ipynb notebook 在 Google Colaboratory 中生成 deepdream 图像。 The tutorial uses the Inception5h network.
本教程使用 Inception5h 网络。 12 layers in this model are commonly used for generating images.
此 model 中的 12 层通常用于生成图像。
Each layer consists of approximately 500 individual features, which recognize different patterns.每层由大约 500 个单独的特征组成,它们识别不同的模式。 It is possible to select specific features in a layer, which yield different results.
select 可以在一个层中使用特定的特征,从而产生不同的结果。 I've generated images of each feature in layer 6, 'mixed4a:0'.
我已经在第 6 层“mixed4a:0”中生成了每个特征的图像。 What I'm trying to do now is mix these features.
我现在要做的是混合这些功能。
A specific layer is selected like this:像这样选择特定层:
layer_tensor = model.layer_tensors[5]
# selects layer 6 out of 12
I can select a range of features like this:我可以 select 的一系列功能是这样的:
layer_tensor = model.layer_tensors[5][:,:,:,0:3]
# selects feature 0 to 2 from layer 6
What I'm trying to do is select specific features instead of a range of features.我想做的是 select 特定功能而不是一系列功能。 I've tried a couple of things which obviously don't work.
我已经尝试了一些显然不起作用的东西。
layer_tensor = model.layer_tensors[5][:,:,:,0,2,4]
layer_tensor = model.layer_tensors[5][:,:,:,0:2:4]
layer_tensor = model.layer_tensors[5][:,:,:,[0,2,4]]
layer_tensor = model.layer_tensors[5][:,:,:,[0:2:4]]
layer_tensor = model.layer_tensors[5][:,:,:,(0,2,4)]
Trying to figure out what kind of object/thing/data structure 'layer_tensor' is, I tried printing it with different parameters:为了弄清楚“layer_tensor”是什么类型的对象/事物/数据结构,我尝试使用不同的参数打印它:
layer_tensor = model.layer_tensors[5]
# prints Tensor("mixed4a:0", shape=(?, ?, ?, 508), dtype=float32, device=/device:CPU:0)
# "mixed4a:0" is the layer name. 508 in 'shape' is the number of features in the layer
layer_tensor = model.layer_tensors[5][:,:,:,0:100]
# prints: Tensor("strided_slice_127:0", shape=(?, ?, ?, 100), dtype=float32)
layer_tensor = model.layer_tensors[5][:,:,:,0]
# prints: Tensor("strided_slice_128:0", shape=(?, ?, ?), dtype=float32)
I'm new with Tensorflow and neural networks in general.我是 Tensorflow 和神经网络的新手。 Does anyone know how to select different features, not just a range of features?
有谁知道如何 select 不同的功能,而不仅仅是一系列功能? Thanks in advance!
提前致谢!
To run the code, load the 'DeepDream_using_tensorflow.ipynb' file into Google Colaboratory.要运行代码,请将“DeepDream_using_tensorflow.ipynb”文件加载到 Google Colaboratory。 Select the Python3 runtime with GPU backend.
Select 带有 GPU 后端的 Python3 运行时。 Upload the files 'download.py' and 'inception5h.py' into the runtime and it should work properly.
将文件“download.py”和“inception5h.py”上传到运行时,它应该可以正常工作。
You can use tf.gather to achieve what you need the following way.您可以使用tf.gather通过以下方式实现您所需要的。
# don't forget to have a look over the deep_dream_final_output.html file to see the processed outputs.
indices = [1,3,5]
layer_tensor = tf.transpose(
tf.gather(
tf.transpose(model.layer_tensors[10], [3,0,1,2]), indices
),
[1,2,3,0])
print(layer_tensor)
img_result = recursive_optimize(layer_tensor=layer_tensor, image=image,
num_iterations=10, step_size=3.0, rescale_factor=0.7,
num_repeats=4, blend=0.2)
It looks complex but all it does is following.它看起来很复杂,但它所做的只是跟随。
layer_tensor
st the last axis (ie the axis you want to slice on) is the first axis (because it makes so much easier to do the slicing)layer_tensor
st 的最后一个轴(即您要切片的轴)转置为第一个轴(因为它使切片更容易)tf.gather
.tf.gather
进行切片。 We are taking the 1,3,5 features. Hope that's clear.希望这很清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.