def build_liae_autoencoder(self, inputs): """ Build the LIAE Autoencoder """ for side in ("a", "b"): encoder = self.networks["encoder"].network(inputs[0]) if side == "a": intermediate = Concatenate()([self.networks["intermediate"].network(encoder), self.networks["intermediate"].network(encoder)]) else: intermediate = Concatenate()([self.networks["intermediate_b"].network(encoder), self.networks["intermediate"].network(encoder)]) output = self.networks["decoder"].network(intermediate) autoencoder = KerasModel(inputs, output) self.add_predictor(side, autoencoder)
def encoder(self): """ DFL H128 Encoder """ input_ = Input(shape=self.input_shape) var_x = input_ var_x = self.blocks.conv(var_x, 128) var_x = self.blocks.conv(var_x, 256) var_x = self.blocks.conv(var_x, 512) var_x = self.blocks.conv(var_x, 1024) var_x = Dense(self.encoder_dim)(Flatten()(var_x)) var_x = Dense(8 * 8 * self.encoder_dim)(var_x) var_x = Reshape((8, 8, self.encoder_dim))(var_x) var_x = self.blocks.upscale(var_x, self.encoder_dim) return KerasModel(input_, var_x)
def decoder_b(self): """ Decoder for side B """ kwargs = dict(kernel_size=5, kernel_initializer=self.kernel_initializer) dense_dim = 384 if self.lowmem else self.config["complexity_decoder_b"] decoder_complexity = 384 if self.lowmem else 512 decoder_shape = self.input_shape[0] // 16 input_ = Input(shape=(decoder_shape, decoder_shape, dense_dim)) var_x = input_ if self.lowmem: var_x = self.blocks.upscale(var_x, decoder_complexity, **kwargs) var_x = self.blocks.upscale(var_x, decoder_complexity // 2, **kwargs) var_x = self.blocks.upscale(var_x, decoder_complexity // 4, **kwargs) var_x = self.blocks.upscale(var_x, decoder_complexity // 8, **kwargs) else: var_x = self.blocks.upscale(var_x, decoder_complexity, res_block_follows=True, **kwargs) var_x = self.blocks.res_block(var_x, decoder_complexity, kernel_initializer=self.kernel_initializer) var_x = self.blocks.upscale(var_x, decoder_complexity, res_block_follows=True, **kwargs) var_x = self.blocks.res_block(var_x, decoder_complexity, kernel_initializer=self.kernel_initializer) var_x = self.blocks.upscale(var_x, decoder_complexity // 2, res_block_follows=True, **kwargs) var_x = self.blocks.res_block(var_x, decoder_complexity // 2, kernel_initializer=self.kernel_initializer) var_x = self.blocks.upscale(var_x, decoder_complexity // 4, **kwargs) var_x = self.blocks.conv2d(var_x, 3, kernel_size=5, padding="same", activation="sigmoid", name="face_out") outputs = [var_x] if self.config.get("mask_type", None): var_y = input_ var_y = self.blocks.upscale(var_y, decoder_complexity) if not self.lowmem: var_y = self.blocks.upscale(var_y, decoder_complexity) var_y = self.blocks.upscale(var_y, decoder_complexity // 2) var_y = self.blocks.upscale(var_y, decoder_complexity // 4) if self.lowmem: var_y = self.blocks.upscale(var_y, decoder_complexity // 8) var_y = self.blocks.conv2d(var_y, 1, kernel_size=5, padding="same", activation="sigmoid", name="mask_out") outputs.append(var_y) return KerasModel(input_, outputs=outputs)
def __build_keras_model(self): model_day = Input(shape=(1, )) output_day = Embedding(31, 16, name='day_embedding')(model_day) output_day = Reshape(target_shape=(16, ))(output_day) model_dow = Input(shape=(1, )) output_dow = Embedding(7, 5, name='dow_embedding')(model_dow) output_dow = Reshape(target_shape=(5, ))(output_dow) input_month = Input(shape=(1, )) output_month = Embedding(12, 6, name='month_embedding')(input_month) output_month = Reshape(target_shape=(6, ))(output_month) model_woy = Input(shape=(1, )) output_woy = Embedding(52, 26, name='week_embedding')(model_woy) output_woy = Reshape(target_shape=(26, ))(output_woy) model_item = Input(shape=(1, )) output_item = Embedding(50, 26, name='item_embedding')(model_item) output_item = Reshape(target_shape=(26, ))(output_item) model_store = Input(shape=(1, )) output_store = Embedding(10, 6, name='store_embedding')(model_store) output_store = Reshape(target_shape=(6, ))(output_store) model_year = Input(shape=(1, )) output_year = Embedding(5, 3, name='year_embedding')(model_year) output_year = Reshape(target_shape=(3, ))(output_year) input_model = [ model_day, model_dow, input_month, model_woy, model_item, model_store, model_year ] output_embeddings = [ output_day, output_dow, output_month, output_woy, output_item, output_store, output_year ] output_model = Concatenate()(output_embeddings) output_model = Dense(128, kernel_initializer="glorot_uniform")(output_model) output_model = Activation('relu')(output_model) output_model = Dense(32, kernel_initializer="glorot_uniform")(output_model) output_model = Activation('relu')(output_model) output_model = Dense(1)(output_model) self.model = KerasModel(inputs=input_model, outputs=output_model) self.model.compile(loss=custom_smape, optimizer='sgd')
def __init__(self): self.normalizer = None depth = self.base_filter_count input_layer = Input(shape=(self.time_points, 6)) y = input_layer y = Conv1D(depth, 4)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = Dropout(0.3)(y) y = Conv1D(depth, 3)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = hybrid_pool_layer(y) y = Dropout(0.4)(y) depth = 2 * depth y = Conv1D(depth, 3)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = Dropout(0.4)(y) y = Conv1D(depth, 3)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = hybrid_pool_layer(y) y = Dropout(0.5)(y) y = Conv1D(self.fc_nodes, 10, activation=None)(y) # 1 - 2 - 6 - 12 - 17 + 4 * (n - 1) # y = Flatten()(y) # y = Dense(self.fc_nodes)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) # y = Dense(self.fc_nodes)(y) y = Conv1D(self.fc_nodes, 1, activation=None)(y) y = ELU()(y) y = Dropout(0.5)(y) y = Conv1D(1, 1, activation=None)(y) y = Activation('sigmoid')(y) output_layer = y model = KerasModel(inputs=input_layer, outputs=output_layer) opt = optimizers.Nadam(lr=0.0005, schedule_decay=0.01) #opt = optimizers.Adam(lr=0.01, decay=0.5) model.compile(optimizer=opt, loss='binary_crossentropy', metrics=["accuracy"], sample_weight_mode="temporal") self.model = model
def decoder_a(self): """ RealFace Decoder (A) Network """ input_filters = self.config['complexity_encoder'] * 2**( self.downscalers_no - 1) input_width = self.config['input_size'] // self._downscale_ratio input_ = Input(shape=(input_width, input_width, input_filters)) var_xy = input_ dense_nodes = int(self.config['dense_nodes'] / 1.5) dense_filters = int(self.dense_filters / 1.5) var_xy = Dense(dense_nodes)(Flatten()(var_xy)) var_xy = Dense(self.dense_width * self.dense_width * dense_filters)(var_xy) var_xy = Reshape( (self.dense_width, self.dense_width, dense_filters))(var_xy) var_xy = self.blocks.upscale(var_xy, dense_filters) var_x = var_xy var_x = self.blocks.res_block(var_x, dense_filters, use_bias=False) decoder_a_complexity = int(self.config['complexity_decoder'] / 1.5) for idx in range(self.upscalers_no - 2): var_x = self.blocks.upscale(var_x, decoder_a_complexity // 2**idx) var_x = self.blocks.upscale(var_x, decoder_a_complexity // 2**(idx + 1)) var_x = Conv2D(3, kernel_size=5, padding="same", activation="sigmoid")(var_x) outputs = [var_x] if self.config.get("mask_type", None) is not None: var_y = var_xy mask_a_complexity = 384 for idx in range(self.upscalers_no - 2): var_y = self.blocks.upscale(var_y, mask_a_complexity // 2**idx) var_y = self.blocks.upscale(var_y, mask_a_complexity // 2**(idx + 1)) var_y = Conv2D(1, kernel_size=5, padding='same', activation='sigmoid')(var_y) outputs += [var_y] return KerasModel(input_, outputs=outputs)
def __build_keras_model(self): input_store = Input(shape=(1, )) output_store = Embedding(1115, 10, name='store_embedding')(input_store) output_store = Reshape(target_shape=(10, ))(output_store) input_dow = Input(shape=(1, )) output_dow = Embedding(7, 6, name='dow_embedding')(input_dow) output_dow = Reshape(target_shape=(6, ))(output_dow) input_promo = Input(shape=(1, )) output_promo = Dense(1)(input_promo) input_year = Input(shape=(1, )) output_year = Embedding(3, 2, name='year_embedding')(input_year) output_year = Reshape(target_shape=(2, ))(output_year) input_month = Input(shape=(1, )) output_month = Embedding(12, 6, name='month_embedding')(input_month) output_month = Reshape(target_shape=(6, ))(output_month) input_day = Input(shape=(1, )) output_day = Embedding(31, 10, name='day_embedding')(input_day) output_day = Reshape(target_shape=(10, ))(output_day) input_germanstate = Input(shape=(1, )) output_germanstate = Embedding( 12, 6, name='state_embedding')(input_germanstate) output_germanstate = Reshape(target_shape=(6, ))(output_germanstate) input_model = [ input_store, input_dow, input_promo, input_year, input_month, input_day, input_germanstate ] output_embeddings = [ output_store, output_dow, output_promo, output_year, output_month, output_day, output_germanstate ] output_model = Concatenate()(output_embeddings) output_model = Dense(1000, kernel_initializer="uniform")(output_model) output_model = Activation('relu')(output_model) output_model = Dense(500, kernel_initializer="uniform")(output_model) output_model = Activation('relu')(output_model) output_model = Dense(1)(output_model) output_model = Activation('sigmoid')(output_model) self.model = KerasModel(inputs=input_model, outputs=output_model) self.model.compile(loss='mean_absolute_error', optimizer='adam')
def create_model_mtl_only_exchange_rate(horizon=1, nb_train_samples=512, batch_size=32, feature_count=6, time_lag=6): x = Input(shape=(time_lag, feature_count), name="input_layer") conv = Conv1D(filters=5, kernel_size=1, activation='relu')(x) conv2 = Conv1D(filters=5, kernel_size=3, padding='causal', strides=1, activation='relu', dilation_rate=2)(conv) conv3 = Conv1D(filters=5, kernel_size=3, padding='causal', strides=1, activation='relu', dilation_rate=4)(conv2) mp = MaxPooling1D(pool_size=1)(conv3) # conv2 = Conv1D(filters=5, kernel_size=3, activation='relu')(mp) # mp = MaxPooling1D(pool_size=2)(conv2) lstm1 = GRU(16, return_sequences=True)(mp) lstm2 = GRU(32, return_sequences=True)(lstm1) shared_dense = Dense(64, name="shared_layer")(lstm2) ## sub1 is main task; units = reshape dimension multiplication sub1 = GRU(units=48, name="task1")(shared_dense) sub2 = GRU(units=16, name="task2")(shared_dense) sub3 = GRU(units=16, name="task3")(shared_dense) sub4 = GRU(units=16, name="task4")(shared_dense) sub5 = GRU(units=16, name="task5")(shared_dense) out1 = Dense(8, name="spec_out1")(sub1) out1 = Dense(1, name="out1")(out1) out2 = Dense(8, name="spec_out2")(sub2) out2 = Dense(1, name="out2")(out2) out3 = Dense(1, name="spec_out3")(sub3) out3 = Dense(1, name="out3")(out3) out4 = Dense(1, name="spec_out4")(sub4) out4 = Dense(1, name="out4")(out4) out5 = Dense(1, name="spec_out5")(sub5) out5 = Dense(1, name="out5")(out5) outputs = [out1, out2, out3, out4, out5] model = KerasModel(inputs=x, outputs=outputs) model.compile(optimizer='adam', loss='mse', metrics=['mae', 'mape', 'mse'], loss_weights=[0.5, 0.25, 0.25, 0.25, 0.25]) # Callbacks # callbacks = [EarlyStopping(monitor='val_mse', patience=10)] model.summary() return model
def build_autoencoders(self): """ Initialize Dfaker model """ logger.debug("Initializing model") inputs = [Input(shape=self.input_shape, name="face")] if self.config.get("mask_type", None): mask_shape = (self.input_shape[0] * 2, self.input_shape[1] * 2, 1) inputs.append(Input(shape=mask_shape, name="mask")) for side in ("a", "b"): decoder = self.networks["decoder_{}".format(side)].network output = decoder(self.networks["encoder"].network(inputs[0])) autoencoder = KerasModel(inputs, output) self.add_predictor(side, autoencoder) logger.debug("Initialized model")
def encoder(self): """ Encoder Network """ input_ = Input(shape=self.input_shape) var_x = input_ var_x = self.blocks.conv(var_x, 128) var_x = self.blocks.conv(var_x, 256) var_x = self.blocks.conv(var_x, 512) if not self.config.get("lowmem", False): var_x = self.blocks.conv(var_x, 1024) var_x = Dense(self.encoder_dim)(Flatten()(var_x)) var_x = Dense(4 * 4 * 1024)(var_x) var_x = Reshape((4, 4, 1024))(var_x) var_x = self.blocks.upscale(var_x, 512) return KerasModel(input_, var_x)
def Encoder_v3(self): """Lighter on resources encoder with bigger first conv layer""" retina = Input(shape=self.IMAGE_SHAPE) x = self.conv_sep_v3(192)(retina) x = self.conv(256)(x) x = self.conv(384)(x) x = self.conv_sep_v3(512)(x) x = self.conv(768)(x) x = self.conv_sep_v3(1024)(x) x = Dense(self.ENCODER_DIM)(Flatten()(x)) x = Dense(4 * 4 * 1024)(x) x = Reshape((4, 4, 1024))(x) out = self.upscale(512)(x) return KerasModel(retina, out)
def __make_model(self) -> KerasModel: """ This method is used to generate our KerasModel containing the Embedding layers alongside with the output layers :return: a compiled KerasModel object """ inputs, outputs = self._make_embedding_layers() output_model = self.config.model_assembler.make_hidden_layers(outputs) output_model = self.config.model_assembler.make_final_layer( output_model) model = KerasModel(inputs=inputs, outputs=output_model) model = self.config.model_assembler.compile_model(model) return model
def create_model_mtl_mtv_electricity(horizon=1, nb_train_samples=512, batch_size=32, feature_count=11, lag_time=6, auxiliary_feature_count=12): x = Input(shape=(lag_time, feature_count), name="input_layer") # conv = Conv1D(filters=5, kernel_size=3, activation='relu')(x) conv = Conv1D(filters=1, kernel_size=1, activation='relu')(x) conv2 = Conv1D(filters=5, kernel_size=3, padding='causal', strides=1, activation='relu', dilation_rate=2)(conv) conv3 = Conv1D(filters=5, kernel_size=3, padding='causal', strides=1, activation='relu', dilation_rate=4)(conv2) # mp = MaxPooling1D(pool_size=2)(conv3) mp = MaxPooling1D(pool_size=1)(conv3) lstm1 = GRU(16, return_sequences=True)(mp) lstm2 = GRU(32, return_sequences=True)(lstm1) shared_dense = Dense(64, name="shared_layer")(lstm2) ## sub1 is main task; units = reshape dimension multiplication sub1 = GRU(units=(lag_time*auxiliary_feature_count), name="task1")(shared_dense) sub2 = GRU(units=16, name="task2")(shared_dense) sub3 = GRU(units=16, name="task3")(shared_dense) sub1 = Reshape((lag_time, auxiliary_feature_count))(sub1) auxiliary_input = Input(shape=(lag_time, auxiliary_feature_count), name='aux_input') concate = Concatenate(axis=-1)([sub1, auxiliary_input]) # out1_gp = Dense(1, name="out1_gp")(sub1) out1 = Dense(8, name="spec_out1")(concate) out1 = Flatten()(out1) out1 = Dense(1, name="out1")(out1) out2 = Dense(8, name="spec_out2")(sub2) out2 = Dense(1, name="out2")(out2) out3 = Dense(1, name="spec_out3")(sub3) out3 = Dense(1, name="out3")(out3) outputs = [out1, out2, out3] # outputs = [out1, out2] model = KerasModel(inputs=[x, auxiliary_input], outputs=outputs) model.compile(optimizer='adam', loss='mse', metrics=['mae', 'mape', 'mse'], loss_weights=[0.5, 0.05, 0.05]) # Callbacks # callbacks = [EarlyStopping(monitor='val_mse', patience=10)] model.summary() return model
def create_model_mtv_electricity(horizon=1, nb_train_samples=512, batch_size=32, feature_count=11, time_lag=6): x = Input(shape=(time_lag, feature_count), name="input_layer") conv = Conv1D(kernel_size=1, filters=5, activation='relu')(x) conv2 = Conv1D(5, kernel_size=3, padding='causal', strides=1, activation='relu', dilation_rate=2)(conv) conv3 = Conv1D(5, kernel_size=3, padding='causal', strides=1, activation='relu', dilation_rate=4)(conv2) mp = MaxPooling1D(pool_size=1)(conv3) lstm1 = GRU(16, return_sequences=True)(mp) lstm2 = GRU(32, return_sequences=True)(lstm1) shared_dense = Dense(64, name="shared_layer")(lstm2) ## sub1 is main task; units = reshape dimension multiplication sub1 = GRU(units=72, name="task1")(shared_dense) out1 = Dense(8, name="spec_out1")(sub1) out1 = Dense(1, name="out1")(out1) outputs = out1 model = KerasModel(inputs=x, outputs=outputs) model.compile(optimizer='adam', loss='mse', metrics=['mae', 'mape', 'mse']) # Callbacks # callbacks = [EarlyStopping(monitor='val_mse', patience=10)] model.summary() # x = Input(shape=(time_lag, 12), name='aux_input') # # out1 = Dense(8, name="spec_out1")(x) # out1 = Flatten()(out1) # out1 = Dense(1, name="out1")(out1) # # outputs = out1 # # model = KerasModel(inputs=x, outputs=outputs) # # # model.compile(optimizer='adam', loss='mse', metrics=['mae', 'mape', 'mse']) # # Callbacks # # callbacks = [EarlyStopping(monitor='val_mse', patience=10)] # # model.summary() # return model
def _mail_model_two(self): output_size = 2 in_mail = Input(shape=(None, self.line_embedding_size), dtype='float32') # batches of arbitrary number of vectors with size self.line_embedding_size mask = Masking()(in_mail) hidden = Bidirectional(GRU(32 // 2, return_sequences=True, implementation=0))(mask) crf = CRF(output_size, sparse_target=False) # , test_mode='marginal', learn_mode='marginal') output = crf(hidden) model = KerasModel(inputs=in_mail, outputs=output) # model.compile(loss=crf.loss_function, optimizer='adam', metrics=[crf.accuracy]) return model
def encoder_df(self): """ DFL SAE DF Encoder Network""" input_ = Input(shape=self.input_shape) dims = self.input_shape[-1] * self.config["encoder_dims"] lowest_dense_res = self.input_shape[0] // 16 var_x = input_ var_x = self.blocks.conv(var_x, dims) var_x = self.blocks.conv(var_x, dims * 2) var_x = self.blocks.conv(var_x, dims * 4) var_x = self.blocks.conv(var_x, dims * 8) var_x = Dense(self.ae_dims)(Flatten()(var_x)) var_x = Dense(lowest_dense_res * lowest_dense_res * self.ae_dims)(var_x) var_x = Reshape((lowest_dense_res, lowest_dense_res, self.ae_dims))(var_x) var_x = self.blocks.upscale(var_x, self.ae_dims) return KerasModel(input_, var_x)
def build_gans(self): inputs = [Input(shape = self.input_shape , name = "face")] outputs_list = [] for side in range(self.num_of_sides): encoded_output = self.networks["encoder"].network(inputs[0]) for side1 in range(self.num_of_sides): if (side1 == side): reconstructed_output = self.networks["decoder_{}".format(side)].network(encoded_output) outputs_list.insert(0,reconstructed_output) else: self.networks[f"discriminator_{side1}"].trainable = False swaped_output = self.networks[f"decoder_{side1}"].network(encoded_output) discriminator_output = self.networks[f"discriminator_{side1}"].networks(swaped_output) outputs_list.append(discriminator_output) self.add_gan(str(side) , KerasModel(inputs , outputs_list))
def fit(self, X, Y, W=None): # TODO retraining would modify the original model, should we do a copy there? pretrained = self.original_model.load() for layer in pretrained.layers: layer.trainable = False y_onehot = keras.utils.np_utils.to_categorical(Y) last = Dense(y_onehot.shape[1], name='classify', activation='softmax')\ (pretrained.layers[-2].output) model = KerasModel(inputs=pretrained.input, outputs=last) model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) model.fit(X, y_onehot, epochs=50, **self.fit_params) return NNTransferModel(model)
def Encoder_v2(self): """Old algorithm; pretty good but slow""" retina = Input(shape=self.IMAGE_SHAPE) x = self.conv(128)(retina) x = self.conv(144)(x) x = self.conv_sep(256)(x) x = self.conv(448)(x) x = self.conv_sep(512)(x) x = self.conv(768)(x) x = self.conv_sep(1024)(x) x = Dense(self.ENCODER_DIM)(Flatten()(x)) x = Dense(4 * 4 * 1024)(x) x = Reshape((4, 4, 1024))(x) out = self.upscale(512)(x) return KerasModel(retina, out)
def build_autoencoders(self): """ Initialize original model """ logger.debug("Initializing model") inputs = [Input(shape=self.input_shape, name="face")] if self.config.get("mask_type", None): mask_shape = (self.input_shape[:2] + (1, )) inputs.append(Input(shape=mask_shape, name="mask")) for side in range(self.num_of_sides): logger.debug("Adding Autoencoder. Side: %s", str(side)) decoder = self.networks["decoder_{}".format(side)].network output = decoder(self.networks["encoder"].network(inputs[0])) autoencoder = KerasModel(inputs, output) self.add_predictor(str(side), autoencoder) logger.debug("Initialized model")
def Encoder(self): input_ = Input(shape=IMAGE_SHAPE) x = input_ x = self.conv(128)(x) x = self.conv(256)(x) x = self.conv(512)(x) x = self.conv(1024)(x) x = ConvLSTM2D(1024, kernel_size=5, strides=2, padding='same')(x) x = Dense(ENCODER_DIM)(Flatten()(x)) x = Dense(4 * 4 * 1024)(x) x = Reshape((4, 4, 1024))(x) x = self.upscale(512)(x) return KerasModel(input_, x)
def encoder(self): """ RealFace Encoder Network """ input_ = Input(shape=self.input_shape) var_x = input_ encoder_complexity = self.config["complexity_encoder"] for idx in range(self.downscalers_no - 1): var_x = self.blocks.conv(var_x, encoder_complexity * 2**idx) var_x = self.blocks.res_block(var_x, encoder_complexity * 2**idx, use_bias=True) var_x = self.blocks.res_block(var_x, encoder_complexity * 2**idx, use_bias=True) var_x = self.blocks.conv(var_x, encoder_complexity * 2**(idx + 1)) return KerasModel(input_, var_x)
def Encoder(input_shape): impt = Input(shape=input_shape) x = cls.conv(cls.ENCODER_COMPLEXITY)(impt) x = cls.conv_sep(cls.ENCODER_COMPLEXITY * 2)(x) x = cls.conv(cls.ENCODER_COMPLEXITY * 4)(x) x = cls.conv_sep(cls.ENCODER_COMPLEXITY * 8)(x) dense_shape = cls.IMAGE_SHAPE[0] // 16 x = Dense(cls.ENCODER_DIM, kernel_initializer=_kern_init)(Flatten()(x)) x = Dense(dense_shape * dense_shape * 512, kernel_initializer=_kern_init)(x) x = Reshape((dense_shape, dense_shape, 512))(x) x = cls.upscale(512)(x) return KerasModel(impt, x)
def train(self, train_mentions, val_mentions=None, epochs=None, batch_size=None, maxlen=None, verbose=1): self.parameters['maxlen'] = maxlen or self.parameters['maxlen'] self.parameters['epochs'] = epochs or self.parameters['epochs'] self.parameters[ 'batch_size'] = batch_size or self.parameters['batch_size'] train_mentions = [ m for m in train_mentions if m.sentiment == 'POSITIVE' ] + train_mentions train_x, train_y = self._mentions_to_xs( train_mentions), self._mentions_to_ys(train_mentions) val_x, val_y = None, None if val_mentions: val_x, val_y = self._mentions_to_xs( val_mentions), self._mentions_to_ys(val_mentions) # 1. Define model input_layer = Input(shape=(None, 300)) masking_layer = Masking( mask_value=self.word2vector.zero_vector)(input_layer) output_layer = Activation('softmax')(Dense(3)(Concatenate(axis=1)([ LSTM(128, recurrent_dropout=0.5, dropout=0.5)(masking_layer), LSTM(128, recurrent_dropout=0.5, dropout=0.5, go_backwards=True)(masking_layer) ]))) self.model = KerasModel(input_layer, output_layer) # 2. train model self.model.summary() self.model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) val_xy = (val_x, val_y) if val_x is not None else None self.model.fit(train_x, train_y, epochs=self.parameters['epochs'], batch_size=self.parameters['batch_size'], shuffle=True, validation_data=val_xy, verbose=verbose)
def init_model(self, dl_rate): x = Input(shape=(IMGWIDTH, IMGWIDTH, 3)) x1 = Conv2D(16, (3, 3), dilation_rate=dl_rate, strides=1, padding='same', activation='relu')(x) x1 = Conv2D(4, (1, 1), padding='same', activation='relu')(x1) x1 = BatchNormalization()(x1) x1 = MaxPooling2D(pool_size=(8, 8), padding='same')(x1) y = Flatten()(x1) y = Dropout(0.5)(y) y = Dense(1, activation='sigmoid')(y) return KerasModel(inputs=x, outputs=y)
def decoder(self): """ DFL H128 Decoder """ input_ = Input(shape=(16, 16, self.encoder_dim)) var = input_ var = self.blocks.upscale(var, self.encoder_dim) var = self.blocks.upscale(var, self.encoder_dim // 2) var = self.blocks.upscale(var, self.encoder_dim // 4) # Face var_x = Conv2D(3, kernel_size=5, padding="same", activation="sigmoid")(var) outputs = [var_x] # Mask if self.config.get("mask_type", None): var_y = Conv2D(1, kernel_size=5, padding="same", activation="sigmoid")(var) outputs.append(var_y) return KerasModel(input_, outputs=outputs)
def build_autoencoders(self, inputs): """ Initialize IAE model """ logger.debug("Initializing model") decoder = self.networks["decoder"].network encoder = self.networks["encoder"].network inter_both = self.networks["inter"].network for side in ("a", "b"): inter_side = self.networks["intermediate_{}".format(side)].network output = decoder(Concatenate()([ inter_side(encoder(inputs[0])), inter_both(encoder(inputs[0])) ])) autoencoder = KerasModel(inputs, output) self.add_predictor(side, autoencoder) logger.debug("Initialized model")
def decoder_b(self): """ RealFace Decoder Network """ input_filters = self.config["complexity_encoder"] * 2**(self.downscalers_no-1) input_width = self.config["input_size"] // self._downscale_ratio input_ = Input(shape=(input_width, input_width, input_filters)) var_xy = input_ var_xy = Dense(self.config["dense_nodes"])(Flatten()(var_xy)) var_xy = Dense(self.dense_width * self.dense_width * self.dense_filters)(var_xy) var_xy = Reshape((self.dense_width, self.dense_width, self.dense_filters))(var_xy) var_xy = self.blocks.upscale(var_xy, self.dense_filters) var_x = var_xy var_x = self.blocks.res_block(var_x, self.dense_filters, use_bias=False) decoder_b_complexity = self.config["complexity_decoder"] for idx in range(self.upscalers_no - 2): var_x = self.blocks.upscale(var_x, decoder_b_complexity // 2**idx) var_x = self.blocks.res_block(var_x, decoder_b_complexity // 2**idx, use_bias=False) var_x = self.blocks.res_block(var_x, decoder_b_complexity // 2**idx, use_bias=True) var_x = self.blocks.upscale(var_x, decoder_b_complexity // 2**(idx + 1)) var_x = self.blocks.conv2d(var_x, 3, kernel_size=5, padding="same", activation="sigmoid", name="face_out") outputs = [var_x] if self.config.get("mask_type", None) is not None: var_y = var_xy mask_b_complexity = 384 for idx in range(self.upscalers_no-2): var_y = self.blocks.upscale(var_y, mask_b_complexity // 2**idx) var_y = self.blocks.upscale(var_y, mask_b_complexity // 2**(idx + 1)) var_y = self.blocks.conv2d(var_y, 1, kernel_size=5, padding="same", activation="sigmoid", name="mask_out") outputs += [var_y] return KerasModel(input_, outputs=outputs)
def evaluate(self, dataset, batch_size: int = 16) -> float: iters_test = int( np.ceil(dataset['x_test'].shape[0] / float(batch_size))) test_gen = self.test_generator(dataset, batch_size) # We can use the `ctc_decoded` layer that is part of our model here. decoding_model = KerasModel( inputs=self.network.input, outputs=self.network.get_layer('ctc_decoded').output) preds = decoding_model.predict_generator(test_gen, steps=iters_test, verbose=2) trues = np.argmax(dataset['y_test'], -1) pred_strings = [ ''.join(self.data.mapping.get(label, '') for label in pred).strip(' |_') for pred in preds ] true_strings = [ ''.join(self.data.mapping.get(label, '') for label in true).strip(' |_') for true in trues ] char_accuracies = [ 1 - editdistance.eval(true_string, pred_string) / len(true_string) for pred_string, true_string in zip(pred_strings, true_strings) ] mean_accuracy = np.mean(char_accuracies) sorted_ind = np.argsort(char_accuracies) print("\nLeast accurate predictions:") for ind in sorted_ind[:5]: print(f'True: {true_strings[ind]}') print(f'Pred: {pred_strings[ind]}') print("\nMost accurate predictions:") for ind in sorted_ind[-5:]: print(f'True: {true_strings[ind]}') print(f'Pred: {pred_strings[ind]}') print("\nRandom predictions:") random_ind = np.random.randint(0, len(char_accuracies), 5) for ind in random_ind: print(f'True: {true_strings[ind]}') print(f'Pred: {pred_strings[ind]}') return mean_accuracy
def __init__(self): self.normalizer = None depth = self.base_filter_count input_layer = Input(shape=(self.time_points, 9)) y = input_layer y = Conv1D(depth, 5)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = Conv1D(depth, 5)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = hybrid_pool_layer_2(y) depth = 3 * depth // 2 y = Conv1D(depth, 5)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = Conv1D(depth, 5)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = hybrid_pool_layer_2(y) depth = 3 * depth // 2 y = Conv1D(depth, 5)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = Conv1D(depth, 5)(y) y = ELU()(y) y = keras.layers.BatchNormalization(scale=False, center=False)(y) y = hybrid_pool_layer_2(y) y = Flatten()(y) y = Dense(self.fc_nodes)(y) y = ELU()(y) y = Dense(1)(y) y = Activation('sigmoid')(y) output_layer = y model = KerasModel(inputs=input_layer, outputs=output_layer) opt = optimizers.Nadam(lr=0.0005, schedule_decay=0.01) #opt = optimizers.Adam(lr=0.01, decay=0.5) model.compile(optimizer=opt, loss='binary_crossentropy', metrics=["accuracy"]) self.model = model