text: from keras.layers import GlobalAveragePooling2D, GlobalMaxPooling2D, AveragePooling2D, Reshape, Dense, multiply, Permute, Concatenate, Conv2D, Add, Activation, Lambda, Layer, Dot
  from keras import backend as K
  from keras.activations import sigmoid, softmax
  from keras import initializers
  import numpy as np
  import tensorflow as tf
  
  def channel_attention(input_feature, ratio=8):
  	
  	channel_axis = 1 if K.image_data_format() == "channels_first" else -1
  	channel = input_feature._keras_shape[channel_axis]
  	
  	shared_layer_one = Dense(channel//ratio,
  							 activation='relu',
  							 kernel_initializer='he_normal',
  							 use_bias=True,
  							 bias_initializer='zeros')
  	shared_layer_two = Dense(channel,
  							 kernel_initializer='he_normal',
  							 use_bias=True,
  							 bias_initializer='zeros')
  	
  	avg_pool = GlobalAveragePooling2D()(input_feature)    
  	avg_pool = Reshape((1,1,channel))(avg_pool)
  	assert avg_pool._keras_shape[1:] == (1,1,channel)
  	avg_pool = shared_layer_one(avg_pool)
  	assert avg_pool._keras_shape[1:] == (1,1,channel//ratio)
  	avg_pool = shared_layer_two(avg_pool)
  	assert avg_pool._keras_shape[1:] == (1,1,channel)
  	
  	max_pool = GlobalMaxPooling2D()(input_feature)
  	max_pool = Reshape((1,1,channel))(max_pool)
  	assert max_pool._keras_shape[1:] == (1,1,channel)
  	max_pool = shared_layer_one(max_pool)
  	assert max_pool._keras_shape[1:] == (1,1,channel//ratio)
  	max_pool = shared_layer_two(max_pool)
  	assert max_pool._keras_shape[1:] == (1,1,channel)
  	
  	cbam_feature = Add()([avg_pool,max_pool])
  	cbam_feature = Activation('sigmoid')(cbam_feature)
  	
  	if K.image_data_format() == "channels_first":
  		cbam_feature = Permute((3, 1, 2))(cbam_feature)
  	
  	return multiply([input_feature, cbam_feature])
  