shuffleNet_flush privileges

shuffleNet_flush privilegeschannelshuffle:1)利用group,再组间进行深度卷积。优点:1)极大减小计算量(FLOPS)由于每个filter不再是和输入的全部featuremap做卷积,而是仅仅和一个group的featuremap做卷积。缺点:边界效应产生,即某个输出channel仅仅来自输入channel的一小部分细节:一般卷积操作中比如输入fe…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

channel shuffle:

1)利用group ,再组间进行深度卷积。

优点:1)极大减小计算量(FLOPS)

               由于每个filter不再是和输入的全部feature map做卷积,而是仅仅和一个group的feature map做卷积。

 缺点:边界效应产生,即某个输出channel仅仅来自输入channel的一小部分

细节:一般卷积操作中比如输入feature map的数量是N,该卷积层的filter数量是M,那么M个filter中的每一个filter都要和N个feature map的某个区域做卷积,然后相加作为一个卷积的结果。假设你引入group操作,设group为g,那么N个输入feature map就被分成g个group,M个filter就被分成g个group,然后在做卷积操作的时候,第一个group的M/g个filter中的每一个都和第一个group的N/g个输入feature map做卷积得到结果,第二个group同理,直到最后一个group.

2)为了抵消边界效应,提出channel shuffle

这里写图片描述

本质; 即进行GConv2之前,对其输入feature map做一个分配,使得GConv2的每一个group都能卷积输入的所有group的feature map

def _shuffle(x, groups):
    def shuffle_layer(x):
        _, w, h, n = K.int_shape(x)
        nb_chan_per_grp = n // groups

        x = K.reshape(x, (-1, w, h, nb_chan_per_grp, groups))
        x = K.permute_dimensions(x, (0, 1, 2, 4, 3)) # Transpose only grps and chs(按照给定的模式重排一个张量的轴)
        x = K.reshape(x, (-1, w, h, n)
        return x

 

 

附代码:

 

from keras.models import Model
from keras.layers import *
from keras.activations import *
from keras.callbacks import *
import keras.backend as K
from keras.engine.topology import get_source_inputs
count=0
def _info(groups):
    return {
        1: [24, 144, 288, 576],
        2: [24, 200, 400, 800],
        3: [24, 240, 480, 960],
        4: [24, 272, 544, 1088],
        8: [24, 384, 768, 1536]
    }[groups], [None, 3, 7, 3]


def basemodel(input_tensor=None, input_shape=None,groups=8):

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    bn_axis = 3 if K.image_data_format() == 'channels_last' else 1

    x = Conv2D(24,
               kernel_size=(3, 3),
               strides=2,
               use_bias=False,
               padding='same')(img_input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPooling2D(pool_size=(3, 3),
                     strides=2,
                     padding='same',)(x)
    x = Activation('relu',name='pool1')(x)


    channels_list, repeat_list = _info(groups)   #  [24, 200, 400, 800],[None, 3, 7, 3]
   # import pdb;pdb.set_trace()

    for i, (out_channels, repeat) in enumerate(zip(channels_list[1:], repeat_list[1:]), start=1):

        x = _stage(x, groups, channels_list[i-1], out_channels, repeat)


 #8
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    model = Model(inputs, x, name='basemodel_dense')

    return model


def _stage(tensor, groups, in_channels, out_channels, repeat):#repeat=repeat_list[1:],[None, 3, 7, 3]
    x = _shufflenet_unit(tensor, groups, in_channels, out_channels, 2) #2

    for _ in range(repeat):
        x = _shufflenet_unit(x, groups, out_channels, out_channels, 1)


    return x

#out_i=[4,12,16]
#(name='stage%s'% k[0])

def _pw_group(tensor, groups, in_channels, out_channels):      #一个组,分通道卷积操作,之后cancat
    """Pointwise grouped convolution."""
    nb_chan_per_grp = in_channels // groups

    pw_convs = []
    for grp in range(groups):
        x = Lambda(lambda x: x[:, :, :, nb_chan_per_grp * grp: nb_chan_per_grp * (grp + 1)])(tensor)
        grp_out_chan = int(out_channels / groups + 0.5)

        pw_convs.append(
            Conv2D(grp_out_chan,
                   kernel_size=(1, 1),
                   padding='same',
                   use_bias=False,
                   strides=1)(x)
        )

    return Concatenate(axis=-1)(pw_convs)


def _shuffle(x, groups):            #通道间的打乱
    def shuffle_layer(x):
        _, w, h, n = K.int_shape(x)
        nb_chan_per_grp = n // groups

        x = K.reshape(x, (-1, w, h, nb_chan_per_grp, groups))
        x = K.permute_dimensions(x, (0, 1, 2, 4, 3)) # Transpose only grps and chs(按照给定的模式重排一个张量的轴)
        x = K.reshape(x, (-1, w, h, n))

        return x

    return Lambda(shuffle_layer)(x)


def _shufflenet_unit(tensor, groups, in_channels, out_channels, strides, shuffle=True, bottleneck=4):
    bottleneck_channels = out_channels // bottleneck

    x = _pw_group(tensor, groups, in_channels, bottleneck_channels)   #深度可分离卷积
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    if shuffle:
        x = _shuffle(x, groups)         #通道间的打乱

    x = DepthwiseConv2D(kernel_size=(3, 3),             #深度卷积
                        padding='same',
                        use_bias=False,
                        strides=strides)(x)
    x = BatchNormalization()(x)


    x = _pw_group(x, groups, bottleneck_channels,
                  out_channels if strides < 2 else out_channels - in_channels)
    x = BatchNormalization()(x)

    if strides < 2:
        x = Add()([tensor, x])
    else:
        avg = AveragePooling2D(pool_size=(3, 3),
                               strides=2,
                               padding='same')(tensor)

        x = Concatenate(axis=-1)([avg, x])
    global count
    count+=1
    x = Activation('relu',name='stage%s'% count)(x)



    return x



 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195795.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 活动图学习笔记

    活动图学习笔记活动图学习笔记活动图基本概念事件流除了用文本形式来表示外,还经常用活动图来表示。为什么有了文本形式以后还要开发这种框图形式呢?这是因为利用文本形式虽然很有用,但是如果事件流逻辑复杂,则文本形式比较难阅读和理解,利用框图将比文本形式来得更加有效。活动图显示与文本事件流相同的信息。我们在业务模型中用活动框图描述业务过程的工作流。活动图的组成要素活动图的组成要素主要有:起始点和终止点、活动、迁移、决策框、

  • 关于python中format占位符中的 {!} 参数[通俗易懂]

    关于python中format占位符中的 {!} 参数[通俗易懂]在看celery的时候,发现里面有这么一句print('Request:{0!r}'.format(self.request))关于里面的是什么意思翻了一下文档。文档里是这么

  • webpack版本区别_webpack和vue cli区别

    webpack版本区别_webpack和vue cli区别首先我们查看一下webpack的版本信息吧:现在已经更新到4.2.0版本了,理论上,我们可以选择任何一个版本,但是新的版本也不一定就是最好的选择,可能存在各种各样的问题,一旦出现了让人纠结的问题,解决麻烦还是挺浪费时间的,并且4.0以后的版本已经分离了webpack-cli,所以目前,无论是学习还是开发项目,我们可以先选择之前较为稳定的版本,个人还是用的3.0的版本;当然了,新的版本还是要测试着…

  • python的赋值功能很强大_python中赋值

    python的赋值功能很强大_python中赋值前言增强型赋值语句是经常被使用到的,因为从各种学习渠道中,我们能够得知i+=1的效率往往要比i=i+1更高一些(这里以+=为例,实际上增强型赋值语句不仅限于此)。所以我们会乐此不

  • Python中使用Flask、MongoDB搭建简易图片服务器

    Python中使用Flask、MongoDB搭建简易图片服务器

  • c++ so文件_C语言调用Python

    c++ so文件_C语言调用Python转自文章《编程基础—–c++与c调用so文件》http://blog.csdn.net/yf210yf/article/details/117129991.制作so文件:libadd_c.soadd.c:intadd(inta,intb){ returna+b;}编译:gcc-shared-fpic-lm-ldl-olibadd_c

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号