import os
import sys
import glob

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('./backbones')
sys.path.append('./decoders')
from backbones import *
from decoders import *
from model_utils import get_block

def build_model(model_name,input_shape=None,parameter_list=None):
    if model_name == 'Res':
        resnet_builder = Resnet3DBuilder(**parameter_list[0])
        result = resnet_builder.build_resnet_50(input_shape)
    else:
        func = get_block(model_name)
        if model_name == 'NoduleSegDecoder_proxima':
            pre_result = parameter_list[0]
#             print ('pre_result',pre_result)
            result = func(pre_result,**parameter_list[1])
        else:
            result = func(input_shape,**parameter_list[0])
    return result
