def __init__(self, modelh5=None): if modelh5 == None: mpath = os.path.join(os.getcwd(), 'CNNevaluation', 'regular', 'regi=1', 'SPEC_Regular_8_epoch13.h5') else: mpath = modelh5 self.model = load_model(mpath, custom_objects={ 'FocalLoss': focal_loss, 'focal_loss_fixed': focal_loss() }) optimizer = optimizers.Adadelta() self.model.compile(optimizer=optimizer, loss=[focal_loss(alpha=0.25, gamma=2)])
def loadbyFolder(self, foldername): filename = [ os.path.join(foldername, file) for file in os.listdir(foldername) if 'weights' not in file ] if len(filename) == 1: print('Start building model..') self.model = load_model(filename[0], custom_objects={ 'FocalLoss': focal_loss, 'focal_loss_fixed': focal_loss() }) optimizer = optimizers.Adadelta() self.model.compile(optimizer=optimizer, loss=[focal_loss(alpha=0.25, gamma=2)]) else: print('Zero or multiple files are detected!') assert (0)
def CNNForest(self, feature_type, module_type, layer_counts): feature_length = 200 if feature_type == 'SPEC' else 26 input_shape = (AUDIO_LENGTH, feature_length, 1) dictmap = { 'Regular': ReguBlock, 'Residual': ResiBlock, 'Inception': XcepBlock } Module = dictmap[module_type] X_input = Input(name='the_input', shape=input_shape) level_h1 = Module(32)(X_input) level_m1 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(level_h1) # 池化层 level_h2 = Module(64)(level_m1) level_m2 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(level_h2) # 池化层 level_h3 = Module(128)(level_m2) level_m3 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(level_h3) # 池化层 level_h4 = Module(256)(level_m3) level_m4 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(level_h4) # 池化层 level_s5 = Module(512)(level_m4) if feature_type == 'SPEC': level_s5 = MaxPooling2D(pool_size=2, strides=None, padding="valid")(level_s5) # 池化层 layers = [level_m1, level_m2, level_m3, level_m4, level_s5] output = layers[layer_counts // 2 - 1] flayer = GlobalAveragePooling2D()(output) fc2 = Dense(CLASS_NUM, use_bias=True, kernel_initializer='he_normal')(flayer) # 全连接层 y_pred = Activation('softmax', name='Activation0')(fc2) model = Model(inputs=X_input, outputs=y_pred) optimizer = optimizers.Adadelta() model.compile(optimizer=optimizer, loss=[focal_loss(alpha=0.25, gamma=2)]) print('{} cnn model with the {} feature and {} layers are estabished.'. format(module_type, feature_type, str(layer_counts))) modelname = feature_type + '_' + module_type + '_' + str(layer_counts) return model, modelname
three.append(spath) spath = os.path.join(os.getcwd(),'log&&materials','20843728498872_2018_01_08_10_42_58.wav') three.append(spath) spath = os.path.join(os.getcwd(),'log&&materials','199481741554988_2018_10_20_00_04_25.wav') three.append(spath) # n1path = os.path.join(os.getcwd(),'log&&materials','noisy_sounds','199481741554988_2018_10_20_00_00_00.wav') for spath in three: signal,fs = read_wav_data(spath) spec = GetFrequencyFeatures(signal,fs) img = spec.reshape(1,spec.shape[0], spec.shape[1], 1) #2. load model # mpath = os.path.join(os.getcwd(),'CNNevaluation','residual','resi=1','SPEC_Residual_8_epoch14.h5') # mpath = os.path.join(os.getcwd(),'CNNdesign','spec+inception','10','SPEC_Inception_10_epoch4.h5') mpath = os.path.join(os.getcwd(),'CNNevaluation','regular','regi=0','spec_Regular_8_epoch12.h5') model = load_model(mpath,custom_objects={'FocalLoss': focal_loss,'focal_loss_fixed': focal_loss()}) optimizer = optimizers.Adadelta() model.compile(optimizer=optimizer, loss=[focal_loss(alpha=0.25, gamma=2)]) #3. try to predictverbose=0 print(np.argmax(model.predict(img))) print(model.predict(img,verbose=1)) # print(np.argmax(model.predict(img)[0])) # print(model.layers[27]) # print(model.layers[28]) # print(model.layers[29]) bowel_output = model.output[:,1] # print(bowel_output) last_conv_layer = model.layers[28] grads = K.gradients(bowel_output, last_conv_layer.output)[0] pooled_grads = K.mean(grads, axis=(0, 1, 2)) iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
def ModelTrainingSetting(self, model): optimizer = optimizers.Adadelta() model.compile(optimizer=optimizer, loss=[focal_loss(alpha=0.25, gamma=2)])