attribute-aware-attention代码复现与解析

1. 运行环境

Python 2.7
pip install scikit-learn
pip install Pillow
pip install keras==1.2.1 (有版本要求,最好与这个一致)
pip install theano==0.9 (之前是1.0.4会报错)

1.1 keras中获取shape的正确方法

在keras的网络中,如果用layer_name.shape的方式获取shape信息将会返还tensorflow.python.framework.tensor_shape.TensorShape其中包含的是tensorflow.python.framework.tensor_shape.Dimension

正确的方式是使用

import keras.backend as K
K.int_shape(laye_name)

2. 代码解析

2.1 语句解析

代码:model_raw = eval(net)(input_tensor=inputs, include_top=False, weights='imagenet')
上一句代码执行时会下载如下文件(可以直接下载好,放到对应文件夹下):
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels_notop.h5

文件夹目录:C:\Users\xpb.keras\models

2.2

Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 3, 448, 448)   0                                            
____________________________________________________________________________________________________
block1_conv1 (Convolution2D)     (None, 64, 448, 448)  1792        input_1[0][0]      
____________________________________________________________________________________________________
..........................省略普通的网络结构.......................
____________________________________________________________________________________________________
block5_conv3 (Convolution2D)     (None, 512, 28, 28)   2359808     block5_conv2[0][0]               
____________________________________________________________________________________________________
block5_pool (MaxPooling2D)       (None, 512, 14, 14)   0           block5_conv3[0][0]               
____________________________________________________________________________________________________
reshape_layer (Reshape)          (None, 512, 196)      0           block5_pool[0][0]                
____________________________________________________________________________________________________
permute_1 (Permute)              (None, 196, 512)      0           reshape_layer[0][0]              
____________________________________________________________________________________________________
convolution1d_1 (Convolution1D)  (None, 196, 512)      262656      permute_1[0][0]    
____________________________________________________________________________________________________ 
activation_57 (Activation)       (None, 196, 512)      0           batchnormalization_57[0][0]      
____________________________________________________________________________________________________
p0_avg_pool (GlobalAveragePoolin (None, 512)           0           activation_1[0][0]               
____________________________________________________________________________________________________
attr0_avg_pool (GlobalAveragePoo (None, 512)           0           activation_3[0][0]               
____________________________________________________________________________________________________
attr1_avg_pool (GlobalAveragePoo (None, 512)           0           activation_5[0][0]          

region_attention (Activation)    (None, 196)         

3.

3.1 keras和TensorFlow版本不一致

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow

TensorFlow 1.10
pip install keras==2.2.0
pip install keras==1.2.1

3.2

ValueError: Negative dimension size caused by subtracting 2 from 1 for 'block2_pool/MaxPool' (op: 'MaxPool') with input shapes: [?,1,224,128].

3.3 如何将python的keras backend换为theano

keras backend默认为TensorFlow ,换为theano,

import os
os.environ['KERAS_BACKEND']='theano'
import keras
import keras.backend as K
K.set_image_dim_ordering('th')

3.4

IOError: Unable to open file (truncated file: eof = 294912, sblock->base_addr = 0, stored_eof = 58889096)

原因是,下载模型的时候未完成,重新下载就行了。

解决方案

找到.keras/model这个文件夹,将下载未完成的.h5文件删除

我的路径为C:\Users.keras\models

3.5

Traceback (most recent call last):
  File "F:/PycharmProjects/attribute-aware-attention-master/cub_demo.py", line 95, in 
    id_prob,id_pool,id_fea_map = init_classification(share_fea_map, emb_dim, nb_classes, name='p0')
  File "F:/PycharmProjects/attribute-aware-attention-master/cub_demo.py", line 66, in init_classification
    fea_map = BatchNormalization(axis=2)(fea_map)
  File "C:\Users\xpb\Anaconda3\envs\Python27\lib\site-packages\keras\engine\topology.py", line 572, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "C:\Users\xpb\Anaconda3\envs\Python27\lib\site-packages\keras\engine\topology.py", line 635, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "C:\Users\xpb\Anaconda3\envs\Python27\lib\site-packages\keras\engine\topology.py", line 166, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
  File "C:\Users\xpb\Anaconda3\envs\Python27\lib\site-packages\keras\layers\normalization.py", line 143, in call
    x_normed = K.in_train_phase(x_normed, x_normed_running)
  File "C:\Users\xpb\Anaconda3\envs\Python27\lib\site-packages\keras\backend\theano_backend.py", line 1168, in in_train_phase
    x = theano.ifelse.ifelse(_LEARNING_PHASE, x, alt)
AttributeError: 'module' object has no attribute 'ifelse'

降级所使用的theano版本,重新安装0.9版本的theano

参考资料

[1] https://keras.io/zh/applications/#resnet
[2] 【Keras】常用的预训练模型权重下载及使用
[3] keras 预训练模型的使用方法
[4] # 版本问题---keras和tensorflow的版本对应关系
[5] tensorflow和keras版本对应关系
[6] keras backend 简单介绍
[7] keras-import keras.backend as K的意义
[8] keras修改backend的方法 解决问题
[9] 解决OSError: Unable to open file (truncated file: eof = 84336640, sblock->base_addr = 0, stored_eof = 解决问题
[10] Keras深度学习框架学习笔记(3) - AttributeError:’module’ object has no attribute ‘ifelse’错误信息的解决方法 解决问题
[11] 详解keras的model.summary()输出参数Param计算过程
[12] keras 中获取张量 tensor 的维度大小。 解决问题
[13] keras 获取某层的输入/输出 tensor 尺寸
[14] keras获得model中某一层的某一个Tensor的输出维度

Keras的网络层相关操作

[1] AveragePooling1D和GlobalAveragePooling1D的区别

Keras的Dot类

[1] 代码系列——keras.layers.Dot()解析
[2] Keras的Dot类
[3] keras.layers层dot维度计算的一些介绍
[4] # 深度学习(六)keras常用函数学习
[5] Docs » Layers » 融合层 Merge

论文下载

[1] Attribute-Aware Attention Model for Fine-grained Representation Learning

预训练模型

[1] https://github.com/fchollet/deep-learning-models/releases/tag/v0.2
[2] https://github.com/fchollet/deep-learning-models/releases/tag/v0.1

代码

[1] # iamhankai/attribute-aware-attention

你可能感兴趣的:(attribute-aware-attention代码复现与解析)