python神經(jīng)網(wǎng)絡Densenet模型復現(xiàn)詳解
什么是Densenet
據(jù)說Densenet比Resnet還要厲害,我決定好好學一下。
ResNet模型的出現(xiàn)使得深度學習神經(jīng)網(wǎng)絡可以變得更深,進而實現(xiàn)了更高的準確度。
ResNet模型的核心是通過建立前面層與后面層之間的短路連接(shortcuts),這有助于訓練過程中梯度的反向傳播,從而能訓練出更深的CNN網(wǎng)絡。
DenseNet模型,它的基本思路與ResNet一致,也是建立前面層與后面層的短路連接,不同的是,但是它建立的是前面所有層與后面層的密集連接。
DenseNet還有一個特點是實現(xiàn)了特征重用。
這些特點讓DenseNet在參數(shù)和計算成本更少的情形下實現(xiàn)比ResNet更優(yōu)的性能。
DenseNet示意圖如下:
Densenet
1、Densenet的整體結(jié)構(gòu)
如圖所示Densenet由DenseBlock和中間的間隔模塊Transition Layer組成。
1、DenseBlock:DenseBlock指的就是DenseNet特有的模塊,如下圖所示,前面所有層與后面層的具有密集連接,在同一個DenseBlock當中,特征層的高寬不會發(fā)生改變,但是通道數(shù)會發(fā)生改變。
2、Transition Layer:Transition Layer是將不同DenseBlock之間進行連接的模塊,主要功能是整合上一個DenseBlock獲得的特征,并且縮小上一個DenseBlock的寬高,在Transition Layer中,一般會使用一個步長為2的AveragePooling2D縮小特征層的寬高。
2、DenseBlock
DenseBlock的實現(xiàn)示意圖如圖所示:
以前獲得的特征會在保留后不斷的堆疊起來。
以一個簡單例子來表現(xiàn)一下具體的DenseBlock的流程:
假設(shè)輸入特征層為X0。
1、對x0進行一次1x1卷積調(diào)整通道數(shù)到4*32后,再利用3x3卷積獲得一個32通道的特征層,此時會獲得一個shape為(h,w,32)的特征層x1。
2、將獲得的x1和初始的x0堆疊,獲得一個新的特征層,這個特征層會同時保留初始x0的特征也會保留經(jīng)過卷積處理后的特征。
3、反復經(jīng)過步驟1、2的處理,原始的特征會一直得到保留,經(jīng)過卷積處理后的特征也會得到保留。當網(wǎng)絡程度不斷加深,就可以實現(xiàn)前面所有層與后面層的具有密集連接。
實現(xiàn)代碼為:
def dense_block(x, blocks, name): for i in range(blocks): x = conv_block(x, 32, name=name + '_block' + str(i + 1)) return x def conv_block(x, growth_rate, name): bn_axis = 3 x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_0_bn')(x) x1 = layers.Activation('relu', name=name + '_0_relu')(x1) x1 = layers.Conv2D(4 * growth_rate, 1, use_bias=False, name=name + '_1_conv')(x1) x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_1_bn')(x1) x1 = layers.Activation('relu', name=name + '_1_relu')(x1) x1 = layers.Conv2D(growth_rate, 3, padding='same', use_bias=False, name=name + '_2_conv')(x1) x = layers.Concatenate(axis=bn_axis, name=name + '_concat')([x, x1]) return x
3、Transition Layer
Transition Layer將不同DenseBlock之間進行連接的模塊,主要功能是整合上一個DenseBlock獲得的特征,并且縮小上一個DenseBlock的寬高,在Transition Layer中,一般會使用一個步長為2的AveragePooling2D縮小特征層的寬高。
實現(xiàn)代碼為:
def transition_block(x, reduction, name): bn_axis = 3 x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_bn')(x) x = layers.Activation('relu', name=name + '_relu')(x) x = layers.Conv2D(int(backend.int_shape(x)[bn_axis] * reduction), 1, use_bias=False, name=name + '_conv')(x) x = layers.AveragePooling2D(2, strides=2, name=name + '_pool')(x) return x
網(wǎng)絡實現(xiàn)代碼
from keras.preprocessing import image from keras.models import Model from keras import layers from keras.applications import imagenet_utils from keras.applications.imagenet_utils import decode_predictions from keras.utils.data_utils import get_file from keras import backend import numpy as np BASE_WEIGTHS_PATH = ( 'https://github.com/keras-team/keras-applications/' 'releases/download/densenet/') DENSENET121_WEIGHT_PATH = ( BASE_WEIGTHS_PATH + 'densenet121_weights_tf_dim_ordering_tf_kernels.h5') DENSENET169_WEIGHT_PATH = ( BASE_WEIGTHS_PATH + 'densenet169_weights_tf_dim_ordering_tf_kernels.h5') DENSENET201_WEIGHT_PATH = ( BASE_WEIGTHS_PATH + 'densenet201_weights_tf_dim_ordering_tf_kernels.h5') def dense_block(x, blocks, name): for i in range(blocks): x = conv_block(x, 32, name=name + '_block' + str(i + 1)) return x def conv_block(x, growth_rate, name): bn_axis = 3 x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_0_bn')(x) x1 = layers.Activation('relu', name=name + '_0_relu')(x1) x1 = layers.Conv2D(4 * growth_rate, 1, use_bias=False, name=name + '_1_conv')(x1) x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_1_bn')(x1) x1 = layers.Activation('relu', name=name + '_1_relu')(x1) x1 = layers.Conv2D(growth_rate, 3, padding='same', use_bias=False, name=name + '_2_conv')(x1) x = layers.Concatenate(axis=bn_axis, name=name + '_concat')([x, x1]) return x def transition_block(x, reduction, name): bn_axis = 3 x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_bn')(x) x = layers.Activation('relu', name=name + '_relu')(x) x = layers.Conv2D(int(backend.int_shape(x)[bn_axis] * reduction), 1, use_bias=False, name=name + '_conv')(x) x = layers.AveragePooling2D(2, strides=2, name=name + '_pool')(x) return x def DenseNet(blocks, input_shape=None, classes=1000, **kwargs): img_input = layers.Input(shape=input_shape) bn_axis = 3 # 224,224,3 -> 112,112,64 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x) x = layers.BatchNormalization( axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x) x = layers.Activation('relu', name='conv1/relu')(x) # 112,112,64 -> 56,56,64 x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x) x = layers.MaxPooling2D(3, strides=2, name='pool1')(x) # 56,56,64 -> 56,56,64+32*block[0] # Densenet121 56,56,64 -> 56,56,64+32*6 == 56,56,256 x = dense_block(x, blocks[0], name='conv2') # 56,56,64+32*block[0] -> 28,28,32+16*block[0] # Densenet121 56,56,256 -> 28,28,32+16*6 == 28,28,128 x = transition_block(x, 0.5, name='pool2') # 28,28,32+16*block[0] -> 28,28,32+16*block[0]+32*block[1] # Densenet121 28,28,128 -> 28,28,128+32*12 == 28,28,512 x = dense_block(x, blocks[1], name='conv3') # Densenet121 28,28,512 -> 14,14,256 x = transition_block(x, 0.5, name='pool3') # Densenet121 14,14,256 -> 14,14,256+32*block[2] == 14,14,1024 x = dense_block(x, blocks[2], name='conv4') # Densenet121 14,14,1024 -> 7,7,512 x = transition_block(x, 0.5, name='pool4') # Densenet121 7,7,512 -> 7,7,256+32*block[3] == 7,7,1024 x = dense_block(x, blocks[3], name='conv5') x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x) x = layers.Activation('relu', name='relu')(x) x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='fc1000')(x) inputs = img_input if blocks == [6, 12, 24, 16]: model = Model(inputs, x, name='densenet121') elif blocks == [6, 12, 32, 32]: model = Model(inputs, x, name='densenet169') elif blocks == [6, 12, 48, 32]: model = Model(inputs, x, name='densenet201') else: model = Model(inputs, x, name='densenet') return model def DenseNet121(input_shape=[224,224,3], classes=1000, **kwargs): return DenseNet([6, 12, 24, 16], input_shape, classes, **kwargs) def DenseNet169(input_shape=[224,224,3], classes=1000, **kwargs): return DenseNet([6, 12, 32, 32], input_shape, classes, **kwargs) def DenseNet201(input_shape=[224,224,3], classes=1000, **kwargs): return DenseNet([6, 12, 48, 32], input_shape, classes, **kwargs) def preprocess_input(x): x /= 255. mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] x[..., 0] -= mean[0] x[..., 1] -= mean[1] x[..., 2] -= mean[2] if std is not None: x[..., 0] /= std[0] x[..., 1] /= std[1] x[..., 2] /= std[2] return x if __name__ == '__main__': # model = DenseNet121() # weights_path = get_file( # 'densenet121_weights_tf_dim_ordering_tf_kernels.h5', # DENSENET121_WEIGHT_PATH, # cache_subdir='models', # file_hash='9d60b8095a5708f2dcce2bca79d332c7') model = DenseNet169() weights_path = get_file( 'densenet169_weights_tf_dim_ordering_tf_kernels.h5', DENSENET169_WEIGHT_PATH, cache_subdir='models', file_hash='d699b8f76981ab1b30698df4c175e90b') # model = DenseNet201() # weights_path = get_file( # 'densenet201_weights_tf_dim_ordering_tf_kernels.h5', # DENSENET201_WEIGHT_PATH, # cache_subdir='models', # file_hash='1ceb130c1ea1b78c3bf6114dbdfd8807') model.load_weights(weights_path) model.summary() img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) print('Input image shape:', x.shape) preds = model.predict(x) print(np.argmax(preds)) print('Predicted:', decode_predictions(preds))
以上就是python神經(jīng)網(wǎng)絡Densenet模型復現(xiàn)詳解的詳細內(nèi)容,更多關(guān)于Densenet模型復現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python數(shù)據(jù)庫格式化輸出文檔的思路與方法
這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)庫格式化輸出文檔的思路與方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03python pands實現(xiàn)execl轉(zhuǎn)csv 并修改csv指定列的方法
今天小編就為大家分享一篇python pands實現(xiàn)execl轉(zhuǎn)csv 并修改csv指定列的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12