import os
import sys
import glob
import numpy as np

import keras
import tensorflow as tf
from keras import Model
from keras import backend as K
from keras.regularizers import l2
from keras.engine import Layer,InputSpec
from keras.layers.merge import concatenate
from keras.callbacks import TensorBoard,Callback
from keras.layers.advanced_activations import LeakyReLU
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras import initializers, regularizers, constraints,optimizers
from keras.callbacks import ModelCheckpoint, LearningRateScheduler,TensorBoard
from keras.layers import Add,Input,Conv3D,Convolution3D,Dropout,UpSampling3D,Concatenate,MaxPooling3D,\
GlobalAveragePooling3D,Dense,GlobalMaxPooling3D,Lambda,Activation,Reshape,Permute, PReLU, Deconvolution3D,Multiply,GlobalAveragePooling3D,Dense


sys.path.append('../baseLayers/')
from CommonLayers import NormActi,ConvUnit




def SEB_block(x,**kwargs):    
    kernel_initializer = kwargs.get('kernel_initializer','he_normal')
    kernel_regularizer = kwargs.get('kernel_regularizer',l2(1e-4))

    kernel_size = kwargs.get('kernel_size',3)
    norm_func = kwargs.get('norm_func')
    activation_func = kwargs.get('activation_func')
    
    num_filters = kwargs.get('num_filters')
    padding = kwargs.get('padding','same')
    
    atrous_rate = kwargs.get('atrous_rate',1)
    stride = kwargs.get('upsample_rate',2)
    conv_first = kwargs.get('conv_first',True)
    merge_axis = kwargs.get('merge_axis',-1)
    block_prefix = kwargs.get('block_prefix')
    
    block_prefix += '_SEB'
    
    x1_list,x2 = x

   
    norm_func,activation_func = None,None
    if len(x1_list) == 1:
        x1 = x1_list[0]
    else:
        x1 = Concatenate(axis=merge_axis,name='%s_concatenate'%(block_prefix))(x1_list)
    
 
    try:
        upsample_rate = int(x2._keras_shape[1]/x1._keras_shape[1])
    except:
        upsample_rate = stride
    x1 = ConvUnit(x1,norm_func=norm_func,activation_func=activation_func,num_filters=num_filters,
                    kernel_size=kernel_size,atrous_rate=atrous_rate,padding=padding,block_prefix=block_prefix,
                     kernel_initializer=kernel_initializer,kernel_regularizer=kernel_regularizer,
                    layer_idx=1,conv_first=conv_first)

    x1 = UpSampling3D(size=upsample_rate,name='%s_upsample'%(block_prefix))(x1)
    return Multiply()([x1,x2])