ResNeXt　実装

#ResNeXt
・入力を分岐させて畳み込み、最後に足し合わせる
・分岐する数をcardinalityとよぶ



![image.png](https://qiita-image-store.s3.amazonaws.com/0/315063/8e516b83-f874-801a-388d-2477a94b7df1.png)


#実装

```python


def _resblock(n_filters1, n_filters2, strides=(1,1)):
  def f(input):    
    x = Convolution2D(n_filters1, (1,1), strides=strides,
                                      kernel_initializer='he_normal', padding='same')(input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Convolution2D(n_filters1, (3,3), strides=strides,
                                      kernel_initializer='he_normal', padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Convolution2D(n_filters2, (1,1), strides=strides,
                                      kernel_initializer='he_normal', padding='same')(x)
    x = BatchNormalization()(x)
 
    return x
    
  return f
   
def resnext():
  
  inputs = Input(shape=(32, 32, 3))
  x = Convolution2D(32, (7,7), strides=(1,1),
                    kernel_initializer='he_normal', padding='same')(inputs)
  x = BatchNormalization()(x)
  x = Activation('relu')(x)
  x = MaxPooling2D((3, 3), strides=(2,2), padding='same')(x)
  
  residual = Convolution2D(256, 1, strides=1, padding='same')(x)
  residual = BatchNormalization()(residual)
  
  x1 = _resblock(n_filters1=4, n_filters2=256)(x)
  x2 = _resblock(n_filters1=4, n_filters2=256)(x)
  x3 = _resblock(n_filters1=4, n_filters2=256)(x)
  x4 = _resblock(n_filters1=4, n_filters2=256)(x)
  x5 = _resblock(n_filters1=4, n_filters2=256)(x)
  x6 = _resblock(n_filters1=4, n_filters2=256)(x)
  x7 = _resblock(n_filters1=4, n_filters2=256)(x)
  x8 = _resblock(n_filters1=4, n_filters2=256)(x)
  x9 = _resblock(n_filters1=4, n_filters2=256)(x)
  x10 = _resblock(n_filters1=4, n_filters2=256)(x)
  x11 = _resblock(n_filters1=4, n_filters2=256)(x)
  x12 = _resblock(n_filters1=4, n_filters2=256)(x)
  x13 = _resblock(n_filters1=4, n_filters2=256)(x)
  x14 = _resblock(n_filters1=4, n_filters2=256)(x)
  x15 = _resblock(n_filters1=4, n_filters2=256)(x)
  x16 = _resblock(n_filters1=4, n_filters2=256)(x)
  x17 = _resblock(n_filters1=4, n_filters2=256)(x)
  x18 = _resblock(n_filters1=4, n_filters2=256)(x)
  x19 = _resblock(n_filters1=4, n_filters2=256)(x)
  x20 = _resblock(n_filters1=4, n_filters2=256)(x)
  x21 = _resblock(n_filters1=4, n_filters2=256)(x)
  x22 = _resblock(n_filters1=4, n_filters2=256)(x)
  x23 = _resblock(n_filters1=4, n_filters2=256)(x)
  x24 = _resblock(n_filters1=4, n_filters2=256)(x)
  x25 = _resblock(n_filters1=4, n_filters2=256)(x)
  x26 = _resblock(n_filters1=4, n_filters2=256)(x)
  x27 = _resblock(n_filters1=4, n_filters2=256)(x)
  x28 = _resblock(n_filters1=4, n_filters2=256)(x)
  x29 = _resblock(n_filters1=4, n_filters2=256)(x)
  x30 = _resblock(n_filters1=4, n_filters2=256)(x)
  x31 = _resblock(n_filters1=4, n_filters2=256)(x)
  x32 = _resblock(n_filters1=4, n_filters2=256)(x)
  
  
  x_all = add([x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
               x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
               x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,
               x31,x32])
  
 
  x = add([x_all, residual])
  
  x = MaxPooling2D((3, 3), strides=(2,2), padding='same')(x)
  residual = Convolution2D(512, 1, strides=1, padding='same')(x)
  residual = BatchNormalization()(residual)

  x1 = _resblock(n_filters1=8, n_filters2=512)(x)
  x2 = _resblock(n_filters1=8, n_filters2=512)(x)
  x3 = _resblock(n_filters1=8, n_filters2=512)(x)
  x4 = _resblock(n_filters1=8, n_filters2=512)(x)
  x5 = _resblock(n_filters1=8, n_filters2=512)(x)
  x6 = _resblock(n_filters1=8, n_filters2=512)(x)
  x7 = _resblock(n_filters1=8, n_filters2=512)(x)
  x8 = _resblock(n_filters1=8, n_filters2=512)(x)
  x9 = _resblock(n_filters1=8, n_filters2=512)(x)
  x10 = _resblock(n_filters1=8, n_filters2=512)(x)
  x11 = _resblock(n_filters1=8, n_filters2=512)(x)
  x12 = _resblock(n_filters1=8, n_filters2=512)(x)
  x13 = _resblock(n_filters1=8, n_filters2=512)(x)
  x14 = _resblock(n_filters1=8, n_filters2=512)(x)
  x15 = _resblock(n_filters1=8, n_filters2=512)(x)
  x16 = _resblock(n_filters1=8, n_filters2=512)(x)
  x17 = _resblock(n_filters1=8, n_filters2=512)(x)
  x18 = _resblock(n_filters1=8, n_filters2=512)(x)
  x19 = _resblock(n_filters1=8, n_filters2=512)(x)
  x20 = _resblock(n_filters1=8, n_filters2=512)(x)
  x21 = _resblock(n_filters1=8, n_filters2=512)(x)
  x22 = _resblock(n_filters1=8, n_filters2=512)(x)
  x23 = _resblock(n_filters1=8, n_filters2=512)(x)
  x24 = _resblock(n_filters1=8, n_filters2=512)(x)
  x25 = _resblock(n_filters1=8, n_filters2=512)(x)
  x26 = _resblock(n_filters1=8, n_filters2=512)(x)
  x27 = _resblock(n_filters1=8, n_filters2=512)(x)
  x28 = _resblock(n_filters1=8, n_filters2=512)(x)
  x29 = _resblock(n_filters1=8, n_filters2=512)(x)
  x30 = _resblock(n_filters1=8, n_filters2=512)(x)
  x31 = _resblock(n_filters1=8, n_filters2=512)(x)
  x32 = _resblock(n_filters1=8, n_filters2=512)(x)
  
  
  x_all = add([x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,
               x11,x12,x13,x14,x15,x16,x17,x18,x19,x20,
               x21,x22,x23,x24,x25,x26,x27,x28,x29,x30,
               x31,x32])
  
  x = add([x_all, residual])

  x = GlobalAveragePooling2D()(x)
  x = Dense(10, kernel_initializer='he_normal', activation='softmax')(x)
  
  
  model = Model(inputs=inputs, outputs=x)
  return model

model = resnext()

adam = Adam()
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])


model.summary()

```