From 967fa0644efb83f13ed81f5e8db7831c202069df Mon Sep 17 00:00:00 2001 From: interface_xiongtete <1144722582@qq.com> Date: Thu, 8 Sep 2022 17:34:11 +0800 Subject: [PATCH] =?UTF-8?q?NasNet=E6=9E=B6=E6=9E=84=E5=A4=8D=E7=8E=B0--CVP?= =?UTF-8?q?R2018?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\345\212\250\346\220\255\345\273\272.py" | 71 +++++++++++++------ ...01\347\247\273\345\255\246\344\271\240.py" | 6 ++ 2 files changed, 56 insertions(+), 21 deletions(-) rename "\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet.py" => "\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet--\346\211\213\345\212\250\346\220\255\345\273\272.py" (89%) create mode 100644 "\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet--\350\277\201\347\247\273\345\255\246\344\271\240.py" diff --git "a/\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet.py" "b/\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet--\346\211\213\345\212\250\346\220\255\345\273\272.py" similarity index 89% rename from "\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet.py" rename to "\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet--\346\211\213\345\212\250\346\220\255\345\273\272.py" index d60f6a5..2618a49 100644 --- "a/\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet.py" +++ "b/\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet--\346\211\213\345\212\250\346\220\255\345\273\272.py" @@ -2,7 +2,7 @@ import tensorflow as tf from tensorflow.keras import layers from tensorflow.keras.models import Model from tensorflow.keras import backend - +from plot_model import plot_model def correct_pad(inputs, kernel_size): """返回一个元组,用于带有下采样的2D卷积的零填充. @@ -33,7 +33,9 @@ def _adjust_block(p, # 需要修改的输入张量 block_id=None): channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1 img_dim = 2 if backend.image_data_format() == 'channels_first' else -2 + ip_shape = backend.int_shape(ip) + if p is not None: p_shape = backend.int_shape(p) @@ -88,6 +90,7 @@ def _separable_conv_block(ip, # 输入张量 kernel_size=(3, 3), # 可分离卷积的内核大小 strides=(1, 1), # 用于下采样的跨步卷积 block_id=None): + # channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1 with backend.name_scope('separable_conv_block_%s' % block_id): x = layers.Activation('relu')(ip) if strides == (2, 2): @@ -119,8 +122,8 @@ def _separable_conv_block(ip, # 输入张量 # Adds a Reduction cell for NASNet-A(原论文图4) -def _reduction_a_cell(ip, # 输入张量 - p, # 输入张量 +def _reduction_a_cell(ip, # 输入张量x + p, # 输入张量p filters, # 输出filters的数量 block_id=None): channel_dim = 1 if backend.image_data_format() == 'channels_first' else -1 @@ -204,6 +207,7 @@ def _normal_a_cell(ip, # 输入张量x h = layers.Activation('relu')(ip) h = layers.Conv2D(filters=filters, kernel_size=(1, 1), + strides=(1,1), padding='same', use_bias=False, kernel_initializer='he_normal', @@ -232,12 +236,13 @@ def _normal_a_cell(ip, # 输入张量x x4_1 = layers.AveragePooling2D(pool_size=(3, 3), strides=(1, 1), padding='same', - name='normal-left4_%s' % block_id)(p) + name='normal_left4_%s' % block_id)(p) x4_2 = layers.AveragePooling2D(pool_size=(3, 3), strides=(1, 1), padding='same', name='normal_right4_%s' % block_id)(p) x4 = layers.add([x4_1, x4_2], name='normal_add_4_%s' % block_id) + with backend.name_scope('block_5'): x5 = _separable_conv_block(h, filters, block_id='normal_left5_%s' % block_id) x5 = layers.add([x5, h], name='normal_add_5_%s' % block_id) @@ -262,7 +267,7 @@ def NASNet(input_shape=None, # 输入shape if default_size is None: default_size = 331 img_input = layers.Input(shape=input_shape) - inputs=img_input + inputs = img_input filters = penultimate_filters // 24 @@ -293,27 +298,28 @@ def NASNet(input_shape=None, # 输入shape p, filters * filter_multiplier ** 2, block_id='reduce_%d' % (2 * num_blocks)) - p=p0 if not skip_reduction else p + p = p0 if not skip_reduction else p for i in range(num_blocks): - x,p=_normal_a_cell(x, - p, - filters*filter_multiplier**2, - block_id='%d'%(2*num_blocks+i+1)) - x=layers.Activation('relu')(x) + x, p = _normal_a_cell(x, + p, + filters * filter_multiplier ** 2, + block_id='%d' % (2 * num_blocks + i + 1)) + x = layers.Activation('relu')(x) if include_top: - x=layers.GlobalAveragePooling2D()(x) - x=layers.Dense(classes,activation=classifier_activation, - name='predictions')(x) + x = layers.GlobalAveragePooling2D()(x) + x = layers.Dense(classes, activation=classifier_activation, + name='predictions')(x) else: - if pooling=='avg': - x=layers.GlobalAveragePooling2D()(x) - elif pooling=='max': - x=layers.GlobalMaxPooling2D()(x) - model=Model(inputs,x,name='NASNet') + if pooling == 'avg': + x = layers.GlobalAveragePooling2D()(x) + elif pooling == 'max': + x = layers.GlobalMaxPooling2D()(x) + model = Model(inputs, x, name='NASNet') return model + def NASNetMobile(input_shape=None, include_top=True, weights='imagenet', @@ -332,6 +338,29 @@ def NASNetMobile(input_shape=None, pooling=pooling, classes=classes, default_size=224) + +def NASNetLarge(input_shape=None, + include_top=True, + weights='imagenet', + input_tensor=None, + pooling=None, + classes=1000, + classifier_activation='softmax'): + return NASNet(input_shape, + penultimate_filters=4032, + num_blocks=6, + stem_block_filters=96, + skip_reduction=True, + filter_multiplier=2, + include_top=include_top, + weights=weights, + input_tensor=input_tensor, + pooling=pooling, + classes=classes, + default_size=331, + classifier_activation=classifier_activation) if __name__ == '__main__': - model=NASNetMobile(input_shape=(224,224,3),classes=1000) - model.summary() \ No newline at end of file + model = NASNetMobile(input_shape=(224, 224, 3), classes=1000) + # model=NASNetLarge(input_shape=(224,224,3),classes=1000) + model.summary() + # plot_model(model,to_file='img/NASNetMobile.pdf') diff --git "a/\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet--\350\277\201\347\247\273\345\255\246\344\271\240.py" "b/\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet--\350\277\201\347\247\273\345\255\246\344\271\240.py" new file mode 100644 index 0000000..dc621a2 --- /dev/null +++ "b/\347\273\217\345\205\270\347\275\221\347\273\234/NasNet--CVPR2018/NasNet--\350\277\201\347\247\273\345\255\246\344\271\240.py" @@ -0,0 +1,6 @@ +from tensorflow.keras.applications import NASNetMobile + +model=NASNetMobile(include_top=True, + input_shape=(224,224,3), + classes=1000) +model.summary() \ No newline at end of file -- GitLab