选择Tensorflow 1层中的特定特征

2024-07-04 05:40:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循this教程,使用Github page中的ipynb笔记本在Google Colaboratory中生成deepdream图像。本教程使用Inception5h网络。此模型中的12层通常用于生成图像。你知道吗

每一层由大约500个单独的特征组成,这些特征识别不同的模式。可以选择图层中的特定特征,从而产生不同的结果。我已经生成了第6层“mixed4a:0”中每个特征的图像。我现在要做的是混合这些特性。你知道吗

选择的特定图层如下所示:

layer_tensor = model.layer_tensors[5]
# selects layer 6 out of 12

我可以选择以下一系列功能:

layer_tensor = model.layer_tensors[5][:,:,:,0:3]
# selects feature 0 to 2 from layer 6

我要做的是选择特定的特性,而不是一系列特性。我试过几件显然不管用的事。你知道吗

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)]

为了弄清楚“层张量”是什么样的对象/对象/数据结构,我尝试用不同的参数打印它:

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)

我是张量流和神经网络的新手。有人知道如何选择不同的功能,而不仅仅是一系列的功能吗?提前谢谢!你知道吗

要运行代码,请使用_张量流.ipynb'文件到谷歌实验室。选择带有GPU后端的Python3运行时。上传文件的下载.py'和'inception5h.py'进入运行时,它应该可以正常工作。你知道吗


Tags: the图像功能layermodel特征特性prints
1条回答
网友
1楼 · 发布于 2024-07-04 05:40:38

您可以使用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)

它看起来很复杂,但它所做的只是跟随。你知道吗

  • 转换layer_tensors.t.最后一个轴(即您要切片的轴)是第一个轴(因为这样可以更容易地进行切片)
  • 使用tf.gather进行切片。我们采用1,3,5特征。它可能是一个索引列表
  • 把张量调回正确的形状(即把第一个轴移到最后面)。你知道吗

希望这是清楚的。你知道吗

相关问题 更多 >

    热门问题