简体   繁体   English

Tensorflow 1 层中的 Select 特定功能

[英]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.它看起来很复杂,但它所做的只是跟随。

  • Transpose the 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 的最后一个轴(即您要切片的轴)转置为第一个轴(因为它使切片更容易)
  • Do the slicing using tf.gather .使用tf.gather进行切片。 We are taking the 1,3,5 features.我们正在采用 1,3,5 功能。 And it could be a list of indices它可能是一个索引列表
  • Transpose the tensor back to the correct shape (ie send the first axis to the very back).将张量转回正确的形状(即将第一个轴发送到最后面)。

Hope that's clear.希望这很清楚。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow:如何从预先训练的CNN的特定层提取图像特征? - Tensorflow: How can I extract image features from a specific layer of a pre-trained CNN? tensorflow model 中的随机 select 层 - Randomly select layer in tensorflow model 使用PyQgis选择和缩放图层的要素 - Select and zoom features of a layer using PyQgis Tensorflow Keras-AttributeError:图层要素没有入站节点 - Tensorflow Keras - AttributeError: Layer features has no inbound nodes 如何为导出的 tensorflow 2.0 keras 模型的输入层设计/预处理特征以用于 tensorflow 服务 - How to engineer/preprocess features for the input layer of a exported tensorflow 2.0 keras model for tensorflow serving 将特定TensorFlow变量还原到特定层(按名称还原) - Restoring specific TensorFlow variables to a specific layer (Restore by name) 从 TensorFlow 2.x 中的特定层移除层/提取特征 - Removing layers/extracting features from specific layers in TensorFlow 2.x Tensorflow hub:从 Resnet50 的最顶层卷积层提取特征 - Tensorflow hub: Extracting features from the topmost convolutional layer of a Resnet50 尝试在 Tensorflow 中组合数字和文本特征:ValueError:层模型需要 2 个输入,但它接收到 1 个输入张量 - Attempting to Combine Numeric and Text Features in Tensorflow: ValueError: Layer model expects 2 input(s), but it received 1 input tensors TensorFlow:如何为表格(一维)特征制作卷积层? - TensorFlow: How do I use make a convolutional layer for tabular (1-D) features?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM