コード例 #1
0
def ResNet_1(train_ds, classes=10, batch_size=32, epochs=3, depth=20): #32x32

  if (depth - 2) % 6 != 0:
    raise ValueError('depth should be 6n+2 (eg 20, 32, 44 in [a])')

  num_filters = 16
  num_res_blocks = int((depth - 2) / 6)

  inputs = Input(shape=[32,32,3])
  x = resnet_layer(inputs=inputs)

  for stack in range(3):
    for res_block in range(num_res_blocks):
      strides = 1

      if stack > 0 and res_block == 0:  
        strides = 2
      y = resnet_layer(inputs=x,num_filters=num_filters,strides=strides)
      y = resnet_layer(inputs=y,num_filters=num_filters,activation=None)
      x = resnet_layer(inputs=x,num_filters=num_filters,kernel_size=1,strides=strides,activation=None,batch_normalization=False)
      x = add([x, y])
      x = Activation('relu')(x)
    num_filters *= 2

  x = AveragePooling2D(pool_size=8)(x)
  y = Flatten()(x)
  outputs = Dense(classes,
                  activation='softmax',
                  kernel_initializer='he_normal')(y)

  model = Model(inputs=inputs, outputs=outputs)
  model.compile(loss='sparse_categorical_crossentropy', optimizer=tf.optimizers.SGD(lr=0.001), metrics=['accuracy'])
  model.summary()
  model.fit(train_ds, batch_size=batch_size, epochs=epochs)
  model.save('ResNet_1.model')
コード例 #2
0
ファイル: model.py プロジェクト: chiachunfu/test_sr
 def res_chan_attention_blocks(res_in, num_chans, reduction_ratio):
     x = resnet_layer(inputs=res_in,
                      num_filters=num_chans,
                      kernel_initializer=test_initializer
                      )
     x = attention_layer(x, 4)
     return x
コード例 #3
0
ファイル: model.py プロジェクト: chiachunfu/test_sr
 def res_blocks(res_in, num_chans):
     x = resnet_layer(inputs=res_in,
                      num_filters=num_chans,
                      kernel_initializer=test_initializer
                      )
     return x
コード例 #4
0
ファイル: model.py プロジェクト: chiachunfu/test_sr
 def res_chan_attention_blocks(res_in, num_chans, reduction_ratio):
     x = resnet_layer(inputs=res_in,
                      num_filters=num_chans
                      )
     x = attention_layer(x, 4)
     return x
コード例 #5
0
ファイル: model.py プロジェクト: chiachunfu/test_sr
 def res_blocks(res_in, num_chans):
     x = resnet_layer(inputs=res_in,
                      num_filters=num_chans
                      )
     return x
コード例 #6
0
def ResNet_2(train_ds, classes=10, batch_size=32, epochs=3, depth=20): #32x32

  if (depth - 2) % 9 != 0:
    raise ValueError('depth should be 9n+2 (eg 56 or 110 in [b])')
  num_filters_in = 16
  num_res_blocks = int((depth - 2) / 9)

  inputs = Input(shape=[32,32,3])
  x = resnet_layer(inputs=inputs,
                    num_filters=num_filters_in,
                    conv_first=True)

  for stage in range(3):
      for res_block in range(num_res_blocks):
          activation = 'relu'
          batch_normalization = True
          strides = 1
          if stage == 0:
              num_filters_out = num_filters_in * 4
              if res_block == 0:
                  activation = None
                  batch_normalization = False
          else:
              num_filters_out = num_filters_in * 2
              if res_block == 0:
                  strides = 2

          y = resnet_layer(inputs=x,
                            num_filters=num_filters_in,
                            kernel_size=1,
                            strides=strides,
                            activation=activation,
                            batch_normalization=batch_normalization,
                            conv_first=False)
          y = resnet_layer(inputs=y,
                            num_filters=num_filters_in,
                            conv_first=False)
          y = resnet_layer(inputs=y,
                            num_filters=num_filters_out,
                            kernel_size=1,
                            conv_first=False)
          if res_block == 0:

              x = resnet_layer(inputs=x,
                                num_filters=num_filters_out,
                                kernel_size=1,
                                strides=strides,
                                activation=None,
                                batch_normalization=False)
          x = add([x, y])

      num_filters_in = num_filters_out

  x = BatchNormalization()(x)
  x = Activation('relu')(x)
  x = AveragePooling2D(pool_size=8)(x)
  y = Flatten()(x)
  outputs = Dense(classes,activation='softmax',kernel_initializer='he_normal')(y)

  model = Model(inputs=inputs, outputs=outputs)
  model.compile(loss='sparse_categorical_crossentropy', optimizer=tf.optimizers.SGD(lr=0.001), metrics=['accuracy'])
  model.summary()
  model.fit(train_ds, batch_size=batch_size, epochs=epochs)
  model.save('ResNet_2.model')