def res_with_center_loss_model(shape=(32, 1024), num_classes=500, n=1, feature_length=100, l2_sm=15, lambda_c=0.2): origin_model = full_res_net_model(shape, num_classes, n, feature_length, l2_sm) origin_input = origin_model.input origin_feature_output = origin_model.get_layer('feature_layer').output origin_output = origin_model.get_layer('output_layer').output input_target = Input(shape=(1, )) centers = Embedding(num_classes, feature_length, name='embedding_layer')(input_target) l2_loss = Lambda( lambda x: K.sum(K.square(x[0] - x[1][:, 0]), 1, keepdims=True), name='l2_loss')([origin_feature_output, centers]) model_center_loss = Model(inputs=[origin_input, input_target], outputs=[origin_output, l2_loss]) model_center_loss.compile( optimizer=keras.optimizers.Adadelta(), loss=[l2_softmax(l2_sm), lambda y_true, y_pred: y_pred], loss_weights=[1, lambda_c], metrics=['accuracy']) return model_center_loss
def full_res_net_model_voxceleb(shape=(32, 1024), num_classes=500, n=1, feature_length=100, l2_sm=15, **kwargs): input_array = keras.Input(shape, name='input') three_d_input = keras.layers.Reshape(target_shape=(*shape, 1))(input_array) resnet_output = resnet_v2(inputs=three_d_input, n=n) resnet_output = BatchNormalization()(resnet_output) mid = keras.layers.Dense(feature_length, activation='sigmoid', name="feature_layer")(resnet_output) mid = BatchNormalization()(mid) output = keras.layers.Dense(num_classes, activation='softmax', name="output_layer")(mid) model = Model(inputs=input_array, outputs=output) model.compile(loss=l2_softmax(l2_sm), optimizer="adam", metrics=['accuracy']) model.summary() return model
def full_res_net_model(shape=(32, 1024), num_classes=500, n=1, feature_length=100, l2_sm=15, **kwargs): input_array = keras.Input(shape, name='input') # three_d_input = keras.layers.Reshape(target_shape=(*shape, 1))(input_array) three_d_input = SpatialDropout2D(0.1)(input_array) resnet_output = resnet_v2(inputs=three_d_input, n=n) resnet_output = BatchNormalization()(resnet_output) resnet_output = Dropout(0.1)(resnet_output) mid = keras.layers.Dense(feature_length, activation=relu_advanced, name="feature_layer")(resnet_output) mid = Dropout(0.05)(mid) mid = BatchNormalization()(mid) output = keras.layers.Dense(num_classes, activation='softmax', name="output_layer")(mid) model = Model(inputs=input_array, outputs=output) model.compile(loss=l2_softmax(l2_sm), optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.summary() return model
def res_plus_attention_model(shape=(32, 1024), num_classes=500, n=1, feature_length=100, l2_sm=15, **kwargs): input_array = keras.Input(shape, name='input') resnet_output = resnet_1d(inputs=input_array, n=n) resnet_output = BatchNormalization()(resnet_output) resnet_output = Dropout(0.3)(resnet_output) mid = keras.layers.Dense(feature_length, activation='sigmoid', name="feature_layer")(resnet_output) mid = Dropout(0.3)(mid) mid = BatchNormalization()(mid) output = keras.layers.Dense(num_classes, activation='softmax', name="output_layer")(mid) model = Model(inputs=input_array, outputs=output) model.compile(loss=l2_softmax(l2_sm), optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.summary() plot_model(model, "attention.png") return model
def rnn(shape=(32, 1024), num_classes=500, l2_sm=15, **kwargs): input_array = keras.Input(shape, name='input') rnn = GRU(50, return_sequences=False)(input_array) full = rnn den = Dense(50, activation="relu", name="feature_layer")(full) output = Dense(num_classes, activation='softmax', name="output_layer")(den) model = Model(inputs=input_array, outputs=output) model.compile(loss=l2_softmax(l2_sm), optimizer=keras.optimizers.Adadelta(), metrics=['accuracy']) model.summary() return model
def res_plus_transformer_model(shape=(32, 1024), num_classes=500): input_array = keras.Input(shape) three_d_input = keras.layers.Reshape(target_shape=(*shape, 1))(input_array) transformer_output = keras.layers.Flatten()(get_transformer( transformer_input=input_array, transformer_depth=3)) resnet_output = resnet_v2(inputs=three_d_input, n=1) mid = keras.layers.concatenate([resnet_output, transformer_output]) output = keras.layers.Dense(num_classes, activation='relu', kernel_initializer='he_normal')(mid) model = Model(inputs=input_array, outputs=output) model.compile(loss=l2_softmax(5), optimizer="sgd", metrics=['accuracy']) model.summary() # plot_model(model, to_file='model.png') return model
def text_cnn(shape=(500, 64), num_classes=2, input_dim=93, feature_length=100, l2_sm=15, **kwargs): input_array = keras.Input(shape, name='input') three_d_input = keras.layers.Reshape(target_shape=(*shape, 1))(input_array) embedding_output = three_d_input _embed = SpatialDropout2D(0.1)(embedding_output) warppers = [] num_filters = 64 kernel_size = [3, 5] conv_action = 'relu' for _kernel_size in kernel_size: for dilated_rate in [1, 3]: conv1d = Conv2D(filters=num_filters, kernel_size=_kernel_size, activation=conv_action, dilation_rate=dilated_rate)(_embed) warppers.append(GlobalMaxPooling2D()(conv1d)) fc = concatenate(warppers) fc = BatchNormalization()(fc) fc = Dropout(0.1)(fc) fc = Dense(feature_length, activation='sigmoid', name="feature_layer")(fc) fc = Dropout(0.1)(fc) preds = Dense(num_classes, activation='softmax', name="output_layer")(fc) model = Model(inputs=input_array, outputs=preds) model.summary() model.compile(loss=l2_softmax(l2_sm), optimizer='adam', metrics=['accuracy']) # plot_model(model, "attention.png") return model
for layer in origin_model.layers: layer.trainable = False input_target = Input(shape=(1, )) centers = Embedding(class_num, feature_length, name='embedding_layer')(input_target) l2_loss = Lambda( lambda x: K.sum(K.square(x[0] - x[1][:, 0]), 1, keepdims=True), name='l2_loss')([origin_feature_output, centers]) model_center_loss = Model(inputs=[origin_input, input_target], outputs=[origin_output, l2_loss]) model_center_loss.compile( optimizer="adam", loss=[l2_softmax(l2_lambda), lambda y_true, y_pred: y_pred], loss_weights=[1, lambda_c], metrics=['accuracy']) model = model_center_loss # create dir checkpoint = ModelCheckpoint( filepath='./models/weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_acc', verbose=1, save_best_only=False) callbacks = [checkpoint] print(int(len(x) / batch_size)) if not load_trained_model:
def load_model(model_path, load_type=0) -> keras.Model: """ 返回训练好的模型 :return: """ def temp(a, b): return b if load_type == 0: return keras.models.load_model(model_path, custom_objects={ 'internal': l2_softmax(10), '<lambda>': temp }) elif load_type == 2: model = keras.models.load_model(model_path, custom_objects={ 'internal': l2_softmax(10), '<lambda>': temp }) output = model.get_layer('feature_layer').output new_model = Model(inputs=model.get_layer('input').input, outputs=output) return new_model elif load_type == 1: model = keras.models.load_model( model_path, custom_objects={'internal': l2_softmax(10)}) output = model.get_layer('feature_layer').output new_model = Model(inputs=model.input, outputs=output) return new_model elif load_type == 3: model = keras.models.load_model(model_path, custom_objects={ 'bpr_triplet_loss': bpr_triplet_loss, 'identity_loss': identity_loss }) print(len(model.layers)) return Model(inputs=model.get_layer('model_1').get_input_at(0), output=model.get_layer('model_1').get_output_at(0)) elif load_type == 4: model = keras.models.load_model(model_path, custom_objects={ 'cos_distance': cos_distance, 'eucl_dist_output_shape': eucl_dist_output_shape, 'contrastive_loss': contrastive_loss, 'accuracy': accuracy, 'l2_normalize': l2_normalize, 'euclidean_distance': euclidean_distance }) print(len(model.layers)) return Model(inputs=model.get_layer('model_1').get_input_at(0), output=model.get_layer('model_1').get_output_at(0)) elif load_type == 5: model = keras.models.load_model(model_path, custom_objects={ 'internal': l2_softmax(10), '<lambda>': temp }) return Model(inputs=model.input, output=model.layers[-2].output)