简体   繁体   中英

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. The tutorial uses the Inception5h network. 12 layers in this model are commonly used for generating images.

Each layer consists of approximately 500 individual features, which recognize different patterns. It is possible to select specific features in a layer, which yield different results. I've generated images of each feature in layer 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:

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. 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 = 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. Does anyone know how to select different features, not just a range of features? Thanks in advance!

To run the code, load the 'DeepDream_using_tensorflow.ipynb' file into Google Colaboratory. Select the Python3 runtime with GPU backend. Upload the files 'download.py' and 'inception5h.py' into the runtime and it should work properly.

You can use tf.gather to achieve what you need the following way.

# 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)
  • Do the slicing using tf.gather . We are taking the 1,3,5 features. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM