Exemplo n.º 1
0
 def __init__(self,
              maxlen=128,
              batch_size=32,
              w_embed_size=200,
              padding="post",
              h_embed_size=200,
              dropout=0.1,
              patience=1,
              plot=True,
              max_epochs=100):
     self.maxlen = maxlen
     self.METRICS = [
         BinaryAccuracy(name='accuracy'),
         Precision(name='precision'),
         Recall(name='recall'),
         AUC(name='auc')
     ]
     self.w_embed_size = w_embed_size
     self.h_embed_size = h_embed_size
     self.dropout = dropout
     self.vocab_size = -1
     self.padding = padding
     self.patience = patience
     self.model = None
     self.w2i = {}
     self.epochs = max_epochs
     self.i2w = {}
     self.vocab = []
     self.batch_size = batch_size
     self.show_the_model = plot
     self.threshold = 0.2
     self.toxic_label = 2
     self.not_toxic_label = 1
     self.unk_token = "[unk]"
     self.pad_token = "[pad]"
Exemplo n.º 2
0
def eval_model(model, gens):
    '''
    Evaluate model comparing performance against different generators

    param:
    model - Keras neural network
    gens - list of Keras ImageDataGenerator

    return:
    None
    '''
    loss_list = []
    auc_list = []
    gen_names = []

    acc = BinaryAccuracy()
    bce = BinaryCrossentropy()

    for gen in gens:
        filename = gen.filenames[0]
        first_index = filename.index('.')
        try:
            second_index = filename.index('.', first_index + 1)
            gen_name = filename[first_index + 1:second_index]

            if (gen_name == 'steg'):
                gen_name = 'Basic LSB'
        except Exception as e:
            gen_name = 'None'

        gen_names.append(gen_name)

        print(f"Evaluating: {gen_name}")

        predictions = model.predict(gen, verbose=1)
        acc.update_state(gen.labels, predictions)
        loss_list.append((bce(gen.labels,
                              predictions).numpy(), acc.result().numpy()))
        acc.reset_states()

    plt.figure('Model Performance vs LSB Type')

    plt.subplot(1, 2, 1)
    plt.bar(gen_names, [loss[0] for loss in loss_list])
    plt.xlabel('LSB Generator Type')
    plt.ylabel('Binary Crossentropy Loss')

    plt.subplot(1, 2, 2)
    plt.bar(gen_names, [loss[1] for loss in loss_list])
    plt.xlabel('LSB Generator Type')
    plt.ylabel('Accuracy')

    plt.show()
def modelBuilder(hp):
    """
    Assign input and output tensors, build neural net and compile model
    :param hp: hyperparameters, argument needed to call this function from evaluateBestParams function
               (see also https://keras.io/api/keras_tuner/hyperparameters/)
    :return: model, compiled neural net model
    """
    ## keras model
    u = Input(shape=(1, ))
    s = Input(shape=(1, ))
    u_embedding = Embedding(N, K)(u)  ## (N, 1, K)
    s_embedding = Embedding(M, K)(s)  ## (N, 1, K)
    u_embedding = Flatten()(u_embedding)  ## (N, K)
    s_embedding = Flatten()(s_embedding)  ## (N, K)
    x = Concatenate()([u_embedding, s_embedding])  ## (N, 2K)

    ## Tune the number of units in the first Dense layer
    ## Choose an optimal value between 32-512
    hp_units = hp.Int('units', min_value=32, max_value=512, step=32)
    x = Dense(units=hp_units)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(100)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dense(1, activation="sigmoid")(x)

    ## define model and compile. Use BinaryCrossEntropy for binary classification approach
    model = Model(inputs=[u, s], outputs=x)
    model.compile(
        loss=BinaryCrossentropy(from_logits=True),
        optimizer=SGD(lr=0.08, momentum=0.9),
        metrics=[
            AUC(thresholds=[0.0, 0.5, 1.0]),
            BinaryAccuracy(threshold=0.5),
            Precision(),
            Recall()
        ],
    )

    ## print model summary
    # model.summary()

    return model
Exemplo n.º 4
0
def create_model(depth=5, breadth=20, learning_rate=0.001):
    """
    A model for predicting whether the current player in a game of hex will win.
    Applies a a sequence of convolutional layers to the input. Each convolutional unit is average-pooled,
    then a dense layer connects these pools to the output.
    The network is provided four representations of the current state of the board,
    by applying 180 degree rotational symmetry and diagonal reflection + player swapping symmetry.
    The second symmetry is not quite a true symmetry since swapping players also changes who the current player is.
    For this reason, the final dense layer has different weights for the player-swapped inputs, so that this difference
    can be taken into account. The convolutional layers use the same weights in all four cases.
    Output of the network should be interpreted as a logit
    representing the probability that the current player will win.
    Functions for constructing input data for these models can be found in the model_input module.

    depth: number of convolutional layers applied to each of the four representations of the board state.
    breadth: number of units in each convolutional layer.
    learning_rate: learning rate for Adam optimizer.
    """
    input_tensors = [Input(shape=(board_size + 1, board_size + 1, 2), name=input_names[k])
                     for k in itertools.product((0, 1), (False, True))]
    out_components = []
    tensors = input_tensors
    pool = GlobalAveragePooling2D()

    for i in range(depth):
        conv_layer = Conv2D(breadth, 3, padding="same", activation="relu")
        tensors = list(map(conv_layer, tensors))
        dense_layer = Dense(1, kernel_initializer="zeros")
        out_components += [dense_layer(pool(t)) for t in tensors[:2]]
        dense_layer = Dense(1, kernel_initializer="zeros")
        out_components += [dense_layer(pool(t)) for t in tensors[2:]]

    output_tensor = Add(name="winners")(out_components)

    model = Model(input_tensors, [output_tensor])

    optimizer = Adam(lr=learning_rate)

    model.compile(
        loss=BinaryCrossentropy(from_logits=True),
        optimizer=optimizer,
        metrics=[BinaryAccuracy(threshold=0.0)]
    )
    return model
Exemplo n.º 5
0
def assignModel(N, M, K):
    """
    Assign input and output tensors, build neural net and compile model
    :param N: integer, number of users
    :param M: integer, number of songs
    :param K: integer, latent dimensionality
    :return: model, compiled neural net model
    """
    ## keras model
    u = Input(shape=(1,))
    s = Input(shape=(1,))
    u_embedding = Embedding(N, K)(u)   ## (N, 1, K)
    s_embedding = Embedding(M, K)(s)   ## (N, 1, K)
    u_embedding = Flatten()(u_embedding)  ## (N, K)
    s_embedding = Flatten()(s_embedding)  ## (N, K)
    x = Concatenate()([u_embedding, s_embedding])  ## (N, 2K)

    ## the neural network (use sigmoid activation function in output layer for binary classification)
    x = Dense(400)(x)
    # x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(100)(x)
    x = BatchNormalization()(x)
    # x = Activation('sigmoid')(x)
    x = Dense(1, activation="sigmoid")(x)

    ## define model and compile. Use BinaryCrossEntropy for binary classification approach
    model = Model(inputs=[u, s], outputs=x)
    model.compile(
      loss=BinaryCrossentropy(from_logits=True),
      optimizer=SGD(lr=0.08, momentum=0.9),
      metrics=[AUC(thresholds=[0.0, 0.5, 1.0]),
               BinaryAccuracy(threshold=0.5),
               Precision(),
               Recall()],
    )

    return model
Exemplo n.º 6
0
# Stratified K-fold here because 10-fold cv is generally recommended and Stratified will maintain the
#  target classification categorical balances for each fold
for train_idx, test_idx in StratifiedKFold(n_splits=total_folds,
                                           shuffle=True,
                                           random_state=1).split(
                                               data_x, data_y):
    print('Fold {}/{}'.format(fold_num, total_folds))
    fold_num += 1

    # Set up the training and testing sets
    X_train, X_test = data_x[train_idx], data_x[test_idx]
    y_train, y_test = data_y[train_idx], data_y[test_idx]

    # Set up the metrics we want to collect
    accuracy = BinaryAccuracy(
    )  # Will change this to Categorical if the target classification is categorical
    tp = TruePositives(
    )  # These could be collected with a confusion matrix, however translating back
    tn = TrueNegatives(
    )  #  and forth from an image may be frustrating (it was last time I did it)
    fp = FalsePositives()
    fn = FalseNegatives()
    metrics = [accuracy, tp, tn, fp, fn]

    # The model must be reinitialized otherwise the model will have trained on all of the data (that wouldn't be true 10-fold cv)
    model = Sequential()
    model.add(Dense(128, input_shape=(
        9, )))  # Input layer, needs same shape as input data (9 values 1D)
    model.add(Dense(64, activation='relu'))  # Hidden layer of nodes
    model.add(Dense(32, activation='relu'))  # Hidden layer of nodes
    model.add(Dense(
Exemplo n.º 7
0
def model_accuracy(y_true, y_pred):
    return BinaryAccuracy()(y_true[..., 0], y_pred[..., 0])
Exemplo n.º 8
0
lung_path = os.path.join(PATH, 'CXR_png')
mask_path = os.path.join(PATH, 'masks')
test_path = os.path.join(PATH, 'test')
weight_path = "{}_weights.best.hdf5".format('cxr_reg')

# %% [code]
train_lung_path, train_mask_path, test_lung_path = get_path_images(lung_path,
                                                                   mask_path,
                                                                   test_path)

# %% [code]
metrics = [TruePositives(name='tp'),  # Valores realmente positivos
           TrueNegatives(name='tn'),  # Valores realmente negativos
           FalsePositives(name='fp'),  # Valores erroneamente positivos
           FalseNegatives(name='fn'),  # Valores erroneamente negativos
           BinaryAccuracy(name='accuracy')]

# %% [code]
filtros = 32
depth = 5
act = 'elu'
# Criação e compilação do modelo 1 proposto
model = model_unet((DIM, DIM, 1), filter_root=filtros,
                   depth=depth, activation=act)
model.compile(optimizer=Adam(lr=1e-3),
              loss=dice_coef_loss,
              metrics=metrics)
model.summary()

# %% [code]
plot_model(model, "my_first_model.png", show_shapes=True)
# - FOLD------------------------------------------------------------------------------------------------------------
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)

brojac = 1
for train, test in kfold.split(X_fit,y):
    model = keras.Sequential()
    model.add(keras.layers.Flatten(input_shape=(brojInputa,), name='PrviSloj'))
    model.add(keras.layers.Dense(16, activation=tf.nn.relu, name='DrugiSloj'))
    model.add(keras.layers.Dense(8, activation=tf.nn.relu, name='TreciSloj'))
    model.add(keras.layers.Dense(4, activation=tf.nn.relu, name='CetvrtiSloj'))
    model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid, name='Izlaz'))

    #opt_adam = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=10e-08, decay=0.0)
    model.compile(optimizer='adam',
                    loss='binary_crossentropy',
                    metrics=[Recall(), Precision(), BinaryAccuracy(threshold=0.5), SpecificityAtSensitivity(0.5)])

    # zaustavi treniranje modela ako n epoha nema poboljšanja u metrici
    callback_early_stopping = EarlyStopping(monitor='val_precision',
                                        patience=20, verbose=1)

    # upisuj u log tijekom treniranja
    callback_tensorboard = TensorBoard(log_dir='./Logovi/',
                                   histogram_freq=0,
                                   write_graph=False)
    # zabilježi svaki checkpoint
    path_checkpoint = 'Checkpoint.keras'
    callback_checkpoint = ModelCheckpoint(filepath=path_checkpoint,
                                          monitor='val_precision',
                                          verbose=1,
                                          save_weights_only=True,
Exemplo n.º 10
0
)
# Metrica para a parada do treino
early = EarlyStopping(monitor="val_loss",
                      mode="min",
                      restore_best_weights=True,
                      patience=40)
callbacks_list = [checkpoint, early, reduceLROnPlat]

model = model_unet((DIM, DIM, 1), filter_root=16, depth=4, activation="relu")

metrics = [
    TruePositives(name="tp"),  # Valores realmente positivos
    TrueNegatives(name="tn"),  # Valores realmente negativos
    FalsePositives(name="fp"),  # Valores erroneamente positivos
    FalseNegatives(name="fn"),  # Valores erroneamente negativos
    BinaryAccuracy(name="accuracy"),
]

weight_path = "./.model/weight_val_acc_96.34.h5"
model = model_unet((DIM, DIM, 1), filter_root=32, depth=5, activation="relu")
model.compile(optimizer=Adam(lr=1e-3), loss=dice_coef_loss, metrics=metrics)
model.summary()
model.load_weights(weight_path)

new_data = "./data"
old_data = os.listdir("./old_data")

# %%
for _type in old_data:
    type_path = os.path.join("./old_data", _type)
    id = 0
Exemplo n.º 11
0
            tb_callback = TensorBoard(log_dir="tensorboard/model_{}{}".format(vowel, subset_id), histogram_freq=0, write_graph=False)
            scaler = StandardScaler()
            train_data_x = scaler.fit_transform(x[train_id, step:step+26+39].astype("float32"))      # Fit z-score normalizer only on training data and apply both on train and test set
            test_data_x = scaler.transform(x[test_id,step:step+26+39].astype("float32"))
            train_data_x = train_data_x[:,feature_mask[vowel]].astype("float32")   # Feature selection with previously defined mask
            test_data_x = test_data_x[:,feature_mask[vowel]].astype("float32")

            img_train_path = x[train_id, step+26+39]
            img_test_path = x[test_id, step+26+39]
            img_train_data = import_images(img_train_path)
            img_test_data = import_images(img_test_path)

            step += 27+39

            model = neural_nets.create_mixed_model(dim=train_data_x.shape[1])
            model.compile(loss="binary_crossentropy", optimizer=Adam(lr=1e-4), metrics=[BinaryAccuracy()])
            print('Vowel "{}"{} model training started'.format(vowel, subset_id))
            model.fit(
                x=[train_data_x, img_train_data], y=train_data_y,
                #validation_data=([test_data_x, img_test_data], test_data_y), callbacks=[tb_callback],
                epochs=600, batch_size=16, shuffle=True#,verbose=0
            )

            predictions = model.predict_on_batch([test_data_x, img_test_data])
            decisions = np.add(decisions, np.squeeze(predictions).round(0))
            K.clear_session()
            tf.compat.v1.reset_default_graph()

        ### All models were trained and predicted
        for i in range(len(decisions)):
            decisions[i] = 1 if decisions[i] > 2.5 else 0