def ModelShare(): tweet_a = Input(shape=(280, 256)) tweet_b = Input(shape=(280, 256)) # This layer can take as input a matrix # and will return a vector of size 64 shared_lstm = LSTM(64, return_sequences=True, name='lstm') # When we reuse the same layer instance # multiple times, the weights of the layer # are also being reused # (it is effectively *the same* layer) encoded_a = shared_lstm(tweet_a) encoded_b = shared_lstm(tweet_b) # We can then concatenate the two vectors: merged_vector = concatenate([encoded_a, encoded_b], axis=-1) # And add a logistic regression on top predictions = Dense(1, activation='sigmoid')(merged_vector) # We define a trainable model linking the # tweet inputs to the predictions model = Model(inputs=[tweet_a, tweet_b], outputs=predictions) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) return model
def build(self): inputs = Input(shape=self.input_shape, name='encoder_input') x = Dense(self.intermediate_dim, activation=self.activation_fct)(inputs) z_mean = Dense(self.latent_dim, name='z_mean')(x) z_log_var = Dense(self.latent_dim, name='z_log_var')(x) # use reparameterization trick to push the sampling out as input # note that "output_shape" isn't necessary with the TensorFlow backend z = Lambda(sampling, output_shape=(self.latent_dim, ), name='z')([z_mean, z_log_var]) # instantiate encoder model encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') # build decoder model latent_inputs = Input(shape=(self.latent_dim, ), name='z_sampling') x = Dense(self.intermediate_dim, activation=self.activation_fct)(latent_inputs) outputs = Dense(self.original_dim, activation='sigmoid')(x) # instantiate decoder model decoder = Model(latent_inputs, outputs, name='decoder') # instantiate VAE model outputs = decoder(encoder(inputs)[2]) vae = Model(inputs, outputs, name='vae_mlp') # VAE Loss = mse_loss or xent_loss + kl_loss reconstruction_loss = mse(inputs, outputs) reconstruction_loss *= self.original_dim kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) kl_loss = K.sum(kl_loss, axis=-1) kl_loss *= -0.5 vae_loss = K.mean(reconstruction_loss + kl_loss) vae.add_loss(vae_loss) vae.compile(optimizer=self.optimizer, loss=self.loss, metrics=['accuracy']) x_train_split, x_valid_split = train_test_split( self.x_train, test_size=self.train_test_split, random_state=self.seed) vae.fit(x_train_split, x_train_split, batch_size=self.batch_size, epochs=self.epochs, verbose=self.verbosity, shuffle=True, validation_data=(x_valid_split, x_valid_split)) x_train_pred = vae.predict(self.x_train) train_mse = np.mean(np.power(self.x_train - x_train_pred, 2), axis=1) self.threshold = np.quantile(train_mse, 0.9) self.vae = vae
def modelMasking(self, code_layer_type, input_dim, code_dim): self.code_layer_type = code_layer_type assert len(code_dim) > 0 if self.code_layer_type == 'lstm': assert len(input_dim) == 2 input_data = Input(shape=(input_dim[0], input_dim[1])) mask = Masking(mask_value=0.)(input_data) if len(code_dim) == 1: encoded = LSTM(code_dim[0])(mask) decoded = RepeatVector(input_dim[0])(encoded) elif len(code_dim) > 1: encoded = mask for i, units in enumerate(code_dim): if i == len(code_dim) - 1: encoded = LSTM(units)(encoded) continue encoded = LSTM(units, return_sequences=True)(encoded) for i, units in enumerate(reversed(code_dim)): if i == 1: decoded = LSTM(units, return_sequences=True)( RepeatVector(input_dim[0])(encoded)) elif i > 1: decoded = LSTM(units, return_sequences=True)(decoded) else: raise ValueError("The codDim must be over 0.") decoded = LSTM(input_dim[-1], return_sequences=True)(decoded) self.model = Model(input_data, decoded) elif self.code_layer_type == 'cov': pass elif self.code_layer_type == 'dense': assert len(input_dim) == 1 input_data = Input(shape=(input_dim[0], )) # encoded = input_data # for i, units in enumerate(codeDim): # encoded = Dense(units, activation='relu')(encoded) # decoded = Dense(inputDim[-1], activation='sigmoid')(encoded) # self.model = Model(input_data, decoded) encoder = Dense( code_dim[0], activation="tanh", activity_regularizer=regularizers.l1(10e-5))(input_data) encoder = Dense(int(code_dim[0] / 2), activation="relu")(encoder) decoder = Dense(int(code_dim[0] / 2), activation='tanh')(encoder) decoder = Dense(input_dim[0], activation='relu')(decoder) self.model = Model(input_data, decoder)
def __init__(self, latent_dim=49): config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config) # ENCODER inp = Input((896, 896, 1)) e = Conv2D(32, (10, 10), activation='relu')(inp) e = MaxPooling2D((10, 10))(e) e = Conv2D(64, (6, 6), activation='relu')(e) e = MaxPooling2D((10, 10))(e) e = Conv2D(64, (3, 3), activation='relu')(e) l = Flatten()(e) l = Dense(49, activation='softmax')(l) # DECODER d = Reshape((7, 7, 1))(l) d = Conv2DTranspose(64, (3, 3), strides=8, activation='relu', padding='same')(d) d = BatchNormalization()(d) d = Conv2DTranspose(64, (3, 3), strides=8, activation='relu', padding='same')(d) d = BatchNormalization()(d) d = Conv2DTranspose(64, (3, 3), strides=2, activation='relu', padding='same')(d) d = BatchNormalization()(d) d = Conv2DTranspose(32, (3, 3), activation='relu', padding='same')(d) decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(d) self.CAD = tf.keras.Model(inp, decoded) opt = tf.keras.optimizers.RMSprop(lr=0.0001, decay=1e-6) self.CAD.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) self.Flow = tf.keras.Sequential([ tf.keras.layers.LSTM(32, input_shape=(3, 2), return_sequences=True), tf.keras.layers.Dropout(0.4), tf.keras.layers.Bidirectional( tf.keras.layers.LSTM(32, return_sequences=True)), tf.keras.layers.Dropout(0.4), tf.keras.layers.TimeDistributed( tf.keras.layers.Dense(10, activation='relu')), tf.keras.layers.Flatten(), tf.keras.layers.Dense(2, activation='relu') ]) opt = tf.keras.optimizers.RMSprop(lr=0.0001, decay=1e-6) self.Flow.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]) print(self.Flow.summary()) print(self.CAD.summary())
def modelDemoStandardConvLSTMInception(input_shape, parameter=None): # define LSTM input = Input(shape=input_shape, name='main_input') I_1 = TimeDistributed(Conv2D(16, (1, 1), activation='relu', padding='same', name='C_1'), name='I_11')(input) I_1 = TimeDistributed(Conv2D(16, (5, 5), activation='relu', padding='same', name='C_2'), name='I_12')(I_1) I_2 = TimeDistributed(MaxPooling2D((3, 3), strides=(1, 1), padding='same', name='C_3'), name='I_21')(input) I_2 = TimeDistributed(Conv2D(16, (1, 1), activation='relu', padding='same', name='C_4'), name='I_22')(I_2) concatenate_output = concatenate([I_1, I_2], axis=-1) # x = TimeDistributed(Flatten())(x) x = ConvLSTM2D(filters=32, kernel_size=(3, 3), padding='same', return_sequences=False)(concatenate_output) #x = MaxPooling2D((3, 3), strides=(1, 1), padding='same', name='M_1')(x) x = (Flatten())(x) x = RepeatVector(8)(x) x = LSTM(50, return_sequences=True)(x) output = TimeDistributed(Dense(8, activation='softmax'), name='main_output')(x) #with tensorflow.device('/cpu'): model = Model(inputs=[input], outputs=[output]) # compile the model with gpu #parallel_model = multi_gpu_model(model, gpus=2) #parallel_model.compile(loss={'main_output': 'categorical_crossentropy'}, # loss_weights={'main_output': 1.}, optimizer='adam', metrics=['accuracy']) #model = multi_gpu(model, gpus=[1, 2]) model.compile(loss={'main_output': 'categorical_crossentropy'}, loss_weights={'main_output': 1.}, optimizer='adam', metrics=['accuracy']) return model
def ModelVisualQuestionAnswering(): # First, let's define a vision model using a Sequential model. # This model will encode an image into a vector. vision_model = Sequential() vision_model.add( Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3))) vision_model.add(Conv2D(64, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) vision_model.add(Conv2D(128, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) vision_model.add(Conv2D(256, (3, 3), activation='relu')) vision_model.add(Conv2D(256, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Flatten()) # Now let's get a tensor with the output of our vision model: image_input = Input(shape=(224, 224, 3)) encoded_image = vision_model(image_input) # Next, let's define a language model to encode the question into a vector. # Each question will be at most 100 words long, # and we will index words as integers from 1 to 9999. question_input = Input(shape=(100, ), dtype='int32') embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)(question_input) encoded_question = LSTM(256)(embedded_question) # Let's concatenate the question vector and the image vector: merged = concatenate([encoded_question, encoded_image]) # And let's train a logistic regression over 1000 words on top: output = Dense(1000, activation='softmax')(merged) # This is our final model: vqa_model = Model(inputs=[image_input, question_input], outputs=output) return vqa_model
def model(self, code_layer_type, input_dim, code_dim): self.code_layer_type = code_layer_type assert len(code_dim) > 0 if self.code_layer_type == 'lstm': assert len(input_dim) == 2 input_data = Input(shape=(input_dim[0], input_dim[1])) if len(code_dim) == 1: encoded = LSTM(code_dim[0])(input_data) decoded = RepeatVector(input_dim[0])(encoded) elif len(code_dim) > 1: encoded = input_data for i, units in enumerate(code_dim): if i == len(code_dim) - 1: encoded = LSTM(units)(encoded) continue encoded = LSTM(units, return_sequences=True)(encoded) for i, units in enumerate(reversed(code_dim)): if i == 1: decoded = LSTM(units, return_sequences=True)( RepeatVector(input_dim[0])(encoded)) elif i > 1: decoded = LSTM(units, return_sequences=True)(decoded) else: raise ValueError("The codDim must be over 0.") decoded = LSTM(input_dim[-1], return_sequences=True)(decoded) self.model = Model(input_data, decoded) elif self.code_layer_type == 'dense': assert len(input_dim) == 1 input_data = Input(shape=(input_dim[0], )) encoded = input_data for i, units in enumerate(code_dim): encoded = Dense(units, activation='relu')(encoded) decoded = Dense(input_dim[-1], activation='sigmoid')(encoded) self.model = Model(input_data, decoded) elif self.code_layer_type == 'cov': pass
def modelB(row, col, parameter=None): # define LSTM input = Input(shape=(None, row, col, 1), name='main_input') ''' x = TimeDistributed(Conv2D(16, (2, 2)))(input) x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(0.25)(x) ''' # tower_1 = TimeDistributed(Conv2D(16, (1, 1), padding='same', activation='relu'))(input) # tower_1 = TimeDistributed(Conv2D(16, (3, 3), padding='same', activation='relu'))(tower_1) tower_2 = TimeDistributed(Conv2D(16, (1, 1), padding='same'))(input) x = BatchNormalization()(tower_2) x = Activation('relu')(x) x = Dropout(0.25)(x) tower_2 = TimeDistributed(Conv2D(16, (5, 5), padding='same'))(x) x = BatchNormalization()(tower_2) x = Activation('relu')(x) tower_2 = Dropout(0.25)(x) tower_3 = TimeDistributed( MaxPooling2D((3, 3), strides=(1, 1), padding='same'))(input) tower_3 = TimeDistributed(Conv2D(16, (1, 1), padding='same'))(tower_3) x = BatchNormalization()(tower_3) x = Activation('relu')(x) tower_3 = Dropout(0.25)(x) concatenate_output = concatenate([tower_2, tower_3], axis=-1) x = TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))(concatenate_output) x = Dropout(0.25)(x) x = TimeDistributed(Flatten())(x) # convLstm = ConvLSTM2D(filters=40, kernel_size=(3, 3),padding='same', return_sequences=False)(x) lstm_output = LSTM(75)(x) lstm_output = BatchNormalization()(lstm_output) # lstm_output = BatchNormalization()(convLstm) # auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_output) # auxiliary_input = Input(shape=(4,), name='aux_input') # x = concatenate([lstm_output, auxiliary_input]) x = RepeatVector(4)(lstm_output) x = LSTM(50, return_sequences=True)(x) # model.add(Dropout(0.25)) x = BatchNormalization()(x) output = TimeDistributed(Dense(4, activation='softmax'), name='main_output')(x) model = Model(inputs=[input], outputs=[output]) model.compile(loss={'main_output': 'categorical_crossentropy'}, loss_weights={'main_output': 1.}, optimizer='adam', metrics=['accuracy']) return model
def SingleOutputCNN( input_shape, output_shape, cnns_per_maxpool=1, maxpool_layers=1, dense_layers=1, dense_units=64, dropout=0.25, regularization=False, global_maxpool=False, name='', ) -> Model: function_name = cast(types.FrameType, inspect.currentframe()).f_code.co_name model_name = f"{function_name}-{name}" if name else function_name # model_name = seq([ function_name, name ]).filter(lambda x: x).make_string("-") # remove dependency on pyfunctional - not in Kaggle repo without internet inputs = Input(shape=input_shape) x = inputs for cnn1 in range(0, maxpool_layers): for cnn2 in range(1, cnns_per_maxpool + 1): x = Conv2D(32 * cnn2, kernel_size=(3, 3), padding='same', activation='relu')(x) x = MaxPooling2D(pool_size=(2, 2))(x) x = BatchNormalization()(x) x = Dropout(dropout)(x) if global_maxpool: x = GlobalMaxPooling2D()(x) x = Flatten()(x) for nn1 in range(0, dense_layers): if regularization: x = Dense(dense_units, activation='relu', kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(x) else: x = Dense(dense_units, activation='relu')(x) x = BatchNormalization()(x) x = Dropout(dropout)(x) x = Dense(output_shape, activation='softmax')(x) model = Model(inputs, x, name=model_name) # plot_model(model, to_file=os.path.join(os.path.dirname(__file__), f"{name}.png")) return model
def ModelResidual(): # input tensor for a 3-channel 256x256 image x = Input(shape=(256, 256, 3)) # 3x3 conv with 3 output channels (same as input channels) y = Conv2D(3, (3, 3), padding='same')(x) # this returns x + y. z = add([x, y]) model = Model(inputs=[x], outputs=z) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) return model
def ModelSharedVision(): # First, define the vision modules digit_input = Input(shape=(27, 27, 1)) x = Conv2D(64, (3, 3))(digit_input) x = Conv2D(64, (3, 3))(x) x = MaxPooling2D((2, 2))(x) out = Flatten()(x) vision_model = Model(digit_input, out) # Then define the tell-digits-apart model digit_a = Input(shape=(27, 27, 1)) digit_b = Input(shape=(27, 27, 1)) # The vision model will be shared, weights and all out_a = vision_model(digit_a) out_b = vision_model(digit_b) concatenated = concatenate([out_a, out_b]) out = Dense(1, activation='sigmoid')(concatenated) classification_model = Model([digit_a, digit_b], out) return classification_model
def modelA(row, col): # define LSTM input = Input(shape=(None, row, col, 1), name='main_input') x = TimeDistributed(Conv2D(16, (2, 2), activation='relu'))(input) x = Dropout(0.25)(x) x = BatchNormalization()(x) x = TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))(x) x = Dropout(0.25)(x) x = TimeDistributed(Flatten())(x) lstm_output = LSTM(75)(x) lstm_output = BatchNormalization()(lstm_output) auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_output) auxiliary_input = Input(shape=(4, ), name='aux_input') x = concatenate([lstm_output, auxiliary_input]) x = RepeatVector(8)(x) x = LSTM(50, return_sequences=True)(x) # model.add(Dropout(0.25)) x = BatchNormalization()(x) output = TimeDistributed(Dense(5, activation='softmax'), name='main_output')(x) model = Model(inputs=[input, auxiliary_input], outputs=[output, auxiliary_output]) model.compile(loss={ 'main_output': 'categorical_crossentropy', 'aux_output': 'binary_crossentropy' }, loss_weights={ 'main_output': 1., 'aux_output': 0.2 }, optimizer='adam', metrics=['accuracy']) return model
def get_autoencoder_model128(img_width=128, img_height=128): # Built for 128x128 autoencoder = Sequential() autoencoder.add(Input(shape=(img_width, img_height, 1))) autoencoder.add(Conv2D(64, (5, 5), activation='relu', padding='same')) autoencoder.add(MaxPooling2D((2, 2), padding='same')) autoencoder.add(Conv2D(128, (3, 3), activation='relu', padding='same')) autoencoder.add(MaxPooling2D((2, 2), padding='same')) # Decoder autoencoder.add(Conv2D(128, (3, 3), activation='relu', padding='same')) autoencoder.add(UpSampling2D((2, 2))) autoencoder.add(Conv2D(64, (3, 3), activation='relu', padding='same')) autoencoder.add(UpSampling2D((2, 2))) autoencoder.add(Conv2D(1, (5, 5), activation='sigmoid', padding='same')) autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) autoencoder.summary() return autoencoder
def create_model(step: Tensorflow2ModelStep) -> tf.keras.Model: """ Create a TensorFlow v2 sequence to sequence (seq2seq) encoder-decoder model. :param step: The base Neuraxle step for TensorFlow v2 (Tensorflow2ModelStep) :return: TensorFlow v2 Keras model """ # shape: (batch_size, seq_length, input_dim) encoder_inputs = Input(shape=(None, step.hyperparams['input_dim']), batch_size=None, dtype=tf.dtypes.float32, name='encoder_inputs') last_encoder_outputs, last_encoders_states = _create_encoder( step, encoder_inputs) decoder_outputs = _create_decoder(step, last_encoder_outputs, last_encoders_states) return Model(encoder_inputs, decoder_outputs)
def modelDemoStandard(row, col): # define LSTM input = Input(shape=(None, row, col, 1), name='main_input') x = TimeDistributed(Conv2D(16, (2, 2), activation='relu'))(input) x = TimeDistributed(Flatten())(x) lstm_output = LSTM(75)(x) x = RepeatVector(4)(lstm_output) x = LSTM(50, return_sequences=True)(x) output = TimeDistributed(Dense(4, activation='softmax'), name='main_output')(x) model = Model(inputs=[input], outputs=[output]) model.compile(loss={'main_output': 'categorical_crossentropy'}, loss_weights={'main_output': 1.}, optimizer='adam', metrics=['accuracy']) return model
def ModelInception(): input_img = Input(shape=(256, 256, 3)) tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img) tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1) tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img) tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2) tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img) tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3) output = concatenate([tower_1, tower_2, tower_3], axis=1) model = Model(inputs=[input_img], outputs=output) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) return model
def get_model(X, N_class, total_words=86627, EMBEDDING_DIM=100, maxlen=53): embeddings_index = {} f = open('glove.6B/glove.6B.100d.txt') for line in f: values = line.split() word = values[0] coefs = np.asarray(values[1:], dtype='float32') embeddings_index[word] = coefs f.close() embedding_matrix = np.zeros((total_words, EMBEDDING_DIM)) for word, i in X.items(): embedding_vector = embeddings_index.get(word) if embedding_vector is not None: embedding_matrix[i] = embedding_vector inp = Input(shape=(maxlen, ), dtype='int32') embedding = Embedding(total_words, EMBEDDING_DIM, embeddings_initializer=Constant(embedding_matrix), input_length=maxlen, trainable=False)(inp) x = LSTM(300, dropout=0.25, recurrent_dropout=0.25, return_sequences=True)(embedding) x = Dropout(0.25)(x) merged = Attention_COSTUM(maxlen)(x) merged = Dense(256, activation='relu')(merged) merged = Dropout(0.25)(merged) merged = BatchNormalization()(merged) outp = Dense(N_class, activation='softmax')(merged) AttentionLSTM = Model(inputs=inp, outputs=outp) AttentionLSTM.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) return AttentionLSTM
def modelDemoStandardConvLSTM(row, col): # define LSTM input = Input(shape=(None, row, col, 1), name='main_input') # x = TimeDistributed(Flatten())(x) x = ConvLSTM2D(filters=75, kernel_size=(3, 3), padding='same', return_sequences=False)(input) x = (Flatten())(x) x = RepeatVector(4)(x) x = LSTM(50, return_sequences=True)(x) output = TimeDistributed(Dense(4, activation='softmax'), name='main_output')(x) model = Model(inputs=[input], outputs=[output]) model.compile(loss={'main_output': 'categorical_crossentropy'}, loss_weights={'main_output': 1.}, optimizer='adam', metrics=['accuracy']) return model
def train_model(x_data, y_data, rain_data, k): k_fold = KFold(n_splits=k, shuffle=True, random_state=0) #stratified_k_fold = StratifiedKFold(n_splits=k, shuffle=True, random_state=0) model_number = 0 #for train_idx, val_idx in tqdm(stratified_k_fold.split(x_data, rain_data)): for train_idx, val_idx in tqdm(k_fold.split(x_data)): x_train, y_train = x_data[train_idx], y_data[train_idx] x_val, y_val = x_data[val_idx], y_data[val_idx] input_layer = Input(x_train.shape[1:]) output_layer = train2_unet2_model(input_layer, 32) model = Model(input_layer, output_layer) callbacks_list = [ # 스케쥴러? #tf.keras.callbacks.ReduceLROnPlateau( # monitor='val_loss', # patience=3, # factor=0.8 #), tf.keras.callbacks.ModelCheckpoint( filepath='./models/model' + str(model_number) + '.h5', monitor='score', save_best_only=True, #save_weights_only=True, verbose=1 ) ] model.compile(loss='mae', optimizer=RAdamOptimizer(learning_rate=1e-3) , metrics=[score, maeOverFscore_keras, fscore_keras]) # stratified_k_fold 사용시 batch_size는 최소 128에서 256이 되어야한다. model.fit(x_train, y_train, epochs=50, batch_size=128, shuffle=True, validation_data=(x_val, y_val), callbacks=callbacks_list) model_number += 1
def build(self): input_img = Input(shape=(28, 28, 1)) cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) cnn = MaxPooling2D((2, 2), padding='same')(cnn) cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(cnn) cnn = MaxPooling2D((2, 2), padding='same')(cnn) cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(cnn) encoded = MaxPooling2D((2, 2), padding='same')(cnn) cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded) cnn = UpSampling2D((2, 2))(cnn) cnn = Conv2D(32, (3, 3), activation='relu', padding='same')(cnn) cnn = UpSampling2D((2, 2))(cnn) cnn = Conv2D(32, (3, 3), activation='relu')(cnn) cnn = UpSampling2D((2, 2))(cnn) decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(cnn) cnn_autoencoder = Model(input_img, decoded) cnn_autoencoder.compile(optimizer='adam', loss='binary_crossentropy') x_train = self.x_train.reshape(-1, 28, 28, 1) x_train_split, x_valid_split = train_test_split(x_train, test_size=self.train_test_split, random_state=self.seed) cnn_autoencoder.fit(x_train_split, x_train_split, epochs=self.epochs, batch_size=self.batch_size, validation_data=(x_valid_split, x_valid_split), verbose=self.verbosity) x_train_pred = cnn_autoencoder.predict(x_train) mse = np.mean(np.power(x_train - x_train_pred, 2), axis=1) # Semi-supervised due to given threshold self.threshold = np.quantile(mse, 0.9) self.cnn_autoencoder = cnn_autoencoder
def _build_model(self, hl1_dims, hl2_dims, hl3_dims, input_layer_size, output_layer_size, optimizer, loss): input_v = Input(shape=(1, input_layer_size)) branch_v = Flatten()(input_v) branch_v = Dense(hl1_dims, activation='relu')(branch_v) branch_v = BatchNormalization()(branch_v) branch_v = Dense(hl2_dims, activation='relu')(branch_v) branch_v = BatchNormalization()(branch_v) out_v = Dense(hl3_dims, activation='relu')(branch_v) out_v = BatchNormalization()(out_v) out_v = Dense(output_layer_size, activation='linear')(out_v) model = Model(inputs=input_v, outputs=out_v) #model = Model(inputs=m_v.inputs, outputs=out_v) # print(model.summary()) model.compile( optimizer=optimizer, loss=loss ) # Use Huber Loss Function for DQN based on TensorBoard analysis return model
def modelStandardB(row, col): # define LSTM input_img = Input(shape=(None, row, col, 1), name='input') x = TimeDistributed(Conv2D(16, (2, 2), activation='relu'))(input_img) x = Dropout(0.25)(x) x = BatchNormalization()(x) x = TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))(x) x = Dropout(0.25)(x) x = TimeDistributed(Flatten())(x) x = LSTM(75)(x) # model.add(Dropout(0.25)) x = BatchNormalization()(x) x = RepeatVector(4)(x) x = LSTM(50, return_sequences=True)(x) # model.add(Dropout(0.25)) x = BatchNormalization()(x) output = TimeDistributed(Dense(4, activation='softmax'))(x) model = Model(input_img, output) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
# 가중치의 체크 포인트 이름 저장 checkpoint_path = ".saved_weight/cp.ckpt" checkpoint_dir = os.path.dirname(checkpoint_path) # 체크포인트 콜백 만들기 cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, save_weights_only=True, verbose=1) # 데이터 파이프라인 만들기 train_dataset = tf.data.Dataset.from_generator(trainGenerator, (tf.float32, tf.float32), (tf.TensorShape([40, 40, my_col]), tf.TensorShape([40, 40, 1]))) # batch크기가 512로 너무 큰거 같다는 생각이 듬 논문을 참고하면 32에서 64가 이미지 분석에는 적당하다고 봄 train_dataset = train_dataset.batch(32).prefetch(1) input_layer = Input((40, 40, my_col)) # 처음 시작 뉴론 32개 말고 다른것으로 변경해 볼 것 #output_layer = resnet_model(input_layer) output_layer = unet_model(input_layer, 32) model = Model(input_layer, output_layer) # 저장된 가중치 불러오기 model.load_weights(checkpoint_path) # ################## compile 메서드로 모형 완성 ########################### adam = tf.keras.optimizers.Adam(learning_rate=0.0005, beta_1=0.9, beta_2=0.999, epsilon=1e-07,amsgrad=True, name='Adam') model.compile(loss="mae", optimizer=adam, metrics=[maeOverFscore_keras, fscore_keras, "accuracy"]) # ################## fit 메서드로 트레이닝 #################################
def ModelVideoQuestionAnswering(): # First, let's define a vision model using a Sequential model. # This model will encode an image into a vector. vision_model = Sequential() vision_model.add( Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3))) vision_model.add(Conv2D(64, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) vision_model.add(Conv2D(128, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) vision_model.add(Conv2D(256, (3, 3), activation='relu')) vision_model.add(Conv2D(256, (3, 3), activation='relu')) vision_model.add(MaxPooling2D((2, 2))) vision_model.add(Flatten()) # Now let's get a tensor with the output of our vision model: image_input = Input(shape=(224, 224, 3)) encoded_image = vision_model(image_input) # Next, let's define a language model to encode the question into a vector. # Each question will be at most 100 words long, # and we will index words as integers from 1 to 9999. question_input = Input(shape=(100, ), dtype='int32') embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)(question_input) encoded_question = LSTM(256)(embedded_question) # Let's concatenate the question vector and the image vector: merged = concatenate([encoded_question, encoded_image]) # And let's train a logistic regression over 1000 words on top: output = Dense(1000, activation='softmax')(merged) # This is our final model: # vqa_model = Model(inputs=[image_input, question_input], outputs=output) video_input = Input(shape=(100, 224, 224, 3)) # This is our video encoded via the previously trained vision_model (weights are reused) encoded_frame_sequence = TimeDistributed(vision_model)( video_input) # the output will be a sequence of vectors encoded_video = LSTM(256)( encoded_frame_sequence) # the output will be a vector # This is a model-level representation of the question encoder, reusing the same weights as before: question_encoder = Model(inputs=question_input, outputs=encoded_question) # Let's use it to encode the question: video_question_input = Input(shape=(100, ), dtype='int32') encoded_video_question = question_encoder(video_question_input) # And this is our video question answering model: merged = concatenate([encoded_video, encoded_video_question]) output = Dense(1000, activation='softmax')(merged) video_qa_model = Model(inputs=[video_input, video_question_input], outputs=output) return video_qa_model
def init_model(self, mode="resnet50"): """ 初始化模型 :param mode: 模型类型 :return: 模型名称和基础模型 """ if mode == "resnet50v2": from tensorflow.keras.applications.resnet_v2 import ResNet50V2 model_name = 'rotnet_v3_resnet50v2_{epoch:02d}_{val_acc:.4f}.hdf5' base_model = ResNet50V2(weights='imagenet', include_top=False, input_shape=self.input_shape) elif mode == "resnet50": from tensorflow.keras.applications.resnet import ResNet50 model_name = 'rotnet_v3_resnet50_{epoch:02d}_{val_acc:.4f}.hdf5' base_model = ResNet50(weights='imagenet', include_top=False, input_shape=self.input_shape) elif mode == "mobilenetv2": from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 model_name = 'rotnet_v3_mobilenetv2_{epoch:02d}_{val_acc:.4f}.hdf5' base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=self.input_shape) elif mode == "densenet121": from tensorflow.python.keras.applications.densenet import DenseNet121 model_name = 'rotnet_v3_densenet121_{epoch:02d}_{val_acc:.4f}.hdf5' base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=self.input_shape) else: raise Exception("[Exception] mode {} 不支持!!".format(mode)) # freeze for layer in base_model.layers: layer.trainable = True x = base_model.output # if mode == "mobilenetv2": # x = Dense(128, activation="relu")(x) x = Flatten()(x) if self.is_hw_ratio: # 是否使用宽高比 x1 = base_model.output x1 = Flatten()(x1) input_ratio = Input(shape=(1, ), name='ratio') x2 = Dense(1, activation='relu')(input_ratio) x = concatenate([x1, x2]) final_output = Dense(self.nb_classes, activation='softmax', name='fc360')(x) model = Model(inputs=base_model.input, outputs=final_output) # model.summary() # 优化器 if self.nb_classes == 360: metrics = ["acc", angle_error] else: metrics = ["acc"] model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.0004, momentum=0.9), metrics=metrics) if self.model_path: model.load_weights(self.model_path) print('[Info] 加载模型的路径: {}'.format(self.model_path)) return model_name, model
from tensorflow.keras.optimizers import SGD from tensorflow.keras.callbacks import TensorBoard from tensorflow.keras.models import load_model import datetime from sklearn.model_selection import train_test_split import numpy as np input_data = np.load('dog_breed_og_flp_trans_data_test.npy') print(input_data.shape) input_labels = np.load('dog_breed_og_flp_trans_labels_test.npy') # X_train, X_val, y_train, y_val = train_test_split(data_in, labels_in, test_size=0.2, random_state=42) # print("done splitting") new_input = Input(shape=(224,224,3)) # should be 224,224 # graph changes over epochs # logdir = "logs/scalars/" + datetime.time().strftime("%Y%m%d-%H%M%S") # tensorboard_callback = TensorBoard(log_dir=logdir) model = Sequential() model.add(ResNet50(weights='imagenet', include_top=False, input_tensor=new_input, pooling='avg')) model.trainable = False model.add(Flatten()) model.add(Dense(120, activation='softmax')) print(model.summary()) model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.001, momentum=0.0, decay=0.0, nesterov=False),
def construct_keras_api_model(embedding_weights): # input_no_time_no_repeat = Input(shape=max_len, dtype='int32') # embedded_no_time_no_repeat = Embedding( # creative_id_window,embedding_size,weights=[embedding_weights],trainable=False # )(input_no_time_no_repeat) # ================================================================================== Input_fix_creative_id = Input(shape=(math.ceil(time_id_max / period_days) * period_length), dtype='int32', name='input_fix_creative_id') Embedded_fix_creative_id = Embedding( creative_id_window, embedding_size, weights=[embedding_weights], trainable=False)(Input_fix_creative_id) # ================================================================================== # input_no_time_with_repeat = Input(shape=max_len, dtype='int32') # embedded_no_time_with_repeat = Embedding(creative_id_window,embedding_size,weights=[embedding_weights],trainable=False)(input_no_time_with_repeat) # ---------------------------------------------------------------------- GM_x = keras.layers.GlobalMaxPooling1D()(Embedded_fix_creative_id) GM_x = Dropout(0.5)(GM_x) GM_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(GM_x) GM_x = BatchNormalization()(GM_x) GM_x = Activation('relu')(GM_x) GM_x = Dropout(0.5)(GM_x) GM_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(GM_x) GM_x = BatchNormalization()(GM_x) GM_x = Activation('relu')(GM_x) GM_x = Dense(1, 'sigmoid')(GM_x) # ---------------------------------------------------------------------- GA_x = GlobalAveragePooling1D()(Embedded_fix_creative_id) GA_x = Dropout(0.5)(GA_x) GA_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(GA_x) GA_x = BatchNormalization()(GA_x) GA_x = Activation('relu')(GA_x) GA_x = Dropout(0.5)(GA_x) GA_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(GA_x) GA_x = BatchNormalization()(GA_x) GA_x = Activation('relu')(GA_x) GA_x = Dense(1, 'sigmoid')(GA_x) # ================================================================================== Conv_creative_id = Conv1D(embedding_size, 15, 5, activation='relu')(Embedded_fix_creative_id) # ---------------------------------------------------------------------- Conv_GM_x = MaxPooling1D(7)(Conv_creative_id) Conv_GM_x = Conv1D(embedding_size, 2, 1, activation='relu')(Conv_GM_x) Conv_GM_x = GlobalMaxPooling1D()(Conv_GM_x) Conv_GM_x = Dropout(0.5)(Conv_GM_x) Conv_GM_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(Conv_GM_x) Conv_GM_x = BatchNormalization()(Conv_GM_x) Conv_GM_x = Activation('relu')(Conv_GM_x) Conv_GM_x = Dropout(0.5)(Conv_GM_x) Conv_GM_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(Conv_GM_x) Conv_GM_x = BatchNormalization()(Conv_GM_x) Conv_GM_x = Activation('relu')(Conv_GM_x) Conv_GM_x = Dense(1, 'sigmoid')(Conv_GM_x) # ---------------------------------------------------------------------- Conv_GA_x = AveragePooling1D(7)(Conv_creative_id) Conv_GA_x = Conv1D(embedding_size, 2, 1, activation='relu')(Conv_GA_x) Conv_GA_x = GlobalAveragePooling1D()(Conv_GA_x) Conv_GA_x = Dropout(0.5)(Conv_GA_x) Conv_GA_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(Conv_GA_x) Conv_GA_x = BatchNormalization()(Conv_GA_x) Conv_GA_x = Activation('relu')(Conv_GA_x) Conv_GA_x = Dropout(0.5)(Conv_GA_x) Conv_GA_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(Conv_GA_x) Conv_GA_x = BatchNormalization()(Conv_GA_x) Conv_GA_x = Activation('relu')(Conv_GA_x) Conv_GA_x = Dense(1, 'sigmoid')(Conv_GA_x) # ---------------------------------------------------------------------- LSTM_x = Conv1D(embedding_size, 14, 7, activation='relu')(Conv_creative_id) LSTM_x = LSTM(embedding_size, return_sequences=True)(LSTM_x) LSTM_x = LSTM(embedding_size, return_sequences=True)(LSTM_x) LSTM_x = LSTM(embedding_size)(LSTM_x) LSTM_x = Dropout(0.5)(LSTM_x) LSTM_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(LSTM_x) LSTM_x = BatchNormalization()(LSTM_x) LSTM_x = Activation('relu')(LSTM_x) LSTM_x = Dropout(0.5)(LSTM_x) LSTM_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(LSTM_x) LSTM_x = BatchNormalization()(LSTM_x) LSTM_x = Activation('relu')(LSTM_x) LSTM_x = Dense(1, 'sigmoid')(LSTM_x) # ---------------------------------------------------------------------- concatenated = concatenate([ GM_x, GA_x, Conv_GM_x, Conv_GA_x, LSTM_x, ], axis=-1) output_tensor = Dense(1, 'sigmoid')(concatenated) keras_api_model = Model( [ # input_no_time_no_repeat, Input_fix_creative_id, # input_no_time_with_repeat, ], output_tensor) keras_api_model.summary() plot_model(keras_api_model, to_file='model/keras_api_word2vec_model.png') print('-' * 5 + ' ' * 3 + "编译模型" + ' ' * 3 + '-' * 5) keras_api_model.compile(optimizer=optimizers.RMSprop(lr=RMSProp_lr), loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy]) return keras_api_model
def build_model(self, model_name, query_dim, terms_dim, output_dim, word_embedding): self.model_name = model_name query_input = Input(shape=(query_dim, ), name='query_input') terms_input = Input(shape=(terms_dim, ), name='terms_input') if model_name == 'lstm': embedding_feature_block = Sequential(layers=[ Embedding(word_embedding.vocabulary_size, word_embedding.dimensions, weights=[word_embedding.embedding_matrix], trainable=True, mask_zero=False), BatchNormalization(), LSTM(64, return_sequences=True) ]) elif model_name == 'bilstm': embedding_feature_block = Sequential(layers=[ Embedding(word_embedding.vocabulary_size, word_embedding.dimensions, weights=[word_embedding.embedding_matrix], trainable=True, mask_zero=False), BatchNormalization(), Bidirectional(LSTM(64, return_sequences=True)) ]) else: # default cnn embedding_feature_block = Sequential(layers=[ Embedding(word_embedding.vocabulary_size, word_embedding.dimensions, weights=[word_embedding.embedding_matrix], trainable=True, mask_zero=False), BatchNormalization(), Conv1D(filters=64, kernel_size=3, strides=1), MaxPooling1D(pool_size=3) ]) # Features query_feature = embedding_feature_block(query_input) terms_feature = embedding_feature_block(terms_input) # Query-Terms alignment attention = Dot(axes=-1)([query_feature, terms_feature]) softmax_attention = Lambda(lambda x: softmax(x, axis=1), output_shape=unchanged_shape)(attention) terms_aligned = Dot(axes=1)([softmax_attention, terms_feature]) # Aligned features if model_name == 'lstm': flatten_layer = LSTM(128, return_sequences=False)(terms_aligned) elif model_name == 'bilstm': flatten_layer = Bidirectional(LSTM( 128, return_sequences=False))(terms_aligned) else: # default cnn merged_cnn = Conv1D(filters=128, kernel_size=3, strides=1)(terms_aligned) merged_cnn = MaxPooling1D(pool_size=3)(merged_cnn) flatten_layer = Flatten()(merged_cnn) # Output dense = BatchNormalization()(flatten_layer) dense = Dense(64, activation='sigmoid')(dense) out = Dense(output_dim, activation='linear')(dense) self.model = Model(inputs=[query_input, terms_input], outputs=out) self.model.compile(optimizer='adam', loss=losses.mean_squared_error) self.model.summary()
df = StandardScaler().fit_transform(X=df) kfold = KFold(n_splits=5, shuffle=True) rmseList = [] rrseList = [] def rmse(y_true, y_pred): return K.sqrt(K.mean(K.square(y_pred - y_true))) opt = SGD(lr=0.001, momentum=0.6, nesterov=False) print("begin training") for i, (train, test) in enumerate(kfold.split(X)): model_in = Input(shape=(len(unique_user, ))) x = Dense(10, activation='relu')(model_in) model_out1 = Dense(10, activation='relu')(model_in) model_out2 = Dense(10, activation='relu')(model_in) model_out = Dense(len(unique_movies), activation='sigmoid')(model_out2) model = Model(inputs=model_in, outputs=model_out) model.compile(loss='mae', optimizer=opt, metrics=['mae', 'acc']) history = model.fit(X[train], Y[train], validation_data=(X[test], Y[test]), epochs=400, batch_size=10, verbose=0) scores = model.evaluate(X[test], Y[test]) rmseList.append(scores[1])