1. 運(yùn)行環(huán)境
Python 2.7
pip install scikit-learn
pip install Pillow
pip install keras==1.2.1 (有版本要求翎卓,最好與這個(gè)一致)
pip install theano==0.9 (之前是1.0.4會(huì)報(bào)錯(cuò))
1.1 keras中獲取shape的正確方法
在keras的網(wǎng)絡(luò)中握侧,如果用layer_name.shape的方式獲取shape信息將會(huì)返還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')
上一句代碼執(zhí)行時(shí)會(huì)下載如下文件(可以直接下載好埋泵,放到對應(yīng)文件夾下):
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]
____________________________________________________________________________________________________
..........................省略普通的網(wǎng)絡(luò)結(jié)構(gòu).......................
____________________________________________________________________________________________________
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默認(rèn)為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)
原因是,下載模型的時(shí)候未完成蒂教,重新下載就行了追迟。
解決方案
找到.keras/model這個(gè)文件夾,將下載未完成的.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 <module>
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】常用的預(yù)訓(xùn)練模型權(quán)重下載及使用
[3] keras 預(yù)訓(xùn)練模型的使用方法
[4] # 版本問題---keras和tensorflow的版本對應(yīng)關(guān)系
[5] tensorflow和keras版本對應(yīng)關(guān)系
[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深度學(xué)習(xí)框架學(xué)習(xí)筆記(3) - AttributeError:’module’ object has no attribute ‘ifelse’錯(cuò)誤信息的解決方法 解決問題
[11] 詳解keras的model.summary()輸出參數(shù)Param計(jì)算過程
[12] keras 中獲取張量 tensor 的維度大小策橘。 解決問題
[13] keras 獲取某層的輸入/輸出 tensor 尺寸
[14] keras獲得model中某一層的某一個(gè)Tensor的輸出維度
Keras的網(wǎng)絡(luò)層相關(guān)操作
[1] AveragePooling1D和GlobalAveragePooling1D的區(qū)別
Keras的Dot類
[1] 代碼系列——keras.layers.Dot()解析
[2] Keras的Dot類
[3] keras.layers層dot維度計(jì)算的一些介紹
[4] # 深度學(xué)習(xí)(六)keras常用函數(shù)學(xué)習(xí)
[5] Docs ? Layers ? 融合層 Merge
論文下載
[1] Attribute-Aware Attention Model for Fine-grained Representation Learning
預(yù)訓(xùn)練模型
[1] https://github.com/fchollet/deep-learning-models/releases/tag/v0.2
[2] https://github.com/fchollet/deep-learning-models/releases/tag/v0.1