Beispiel #1
0
    def build_model(self, config):
        input_shape = config['input_shape']
        # input_shape is (None, rows, cols, timesteps)
        model = self.model

        model.add(Conv3D(32, kernel_size=(3, 2, 4), strides=(1, 1, 2),
                         input_shape=(input_shape[1],
                                      input_shape[2],
                                      input_shape[3], 1)))
        model.add(BatchNormalization(axis=2))
        model.add(MaxPool3D(pool_size=(2, 2, 1)))
        model.add(Conv3D(1, kernel_size=(1, 1, 1)))
        model.add(Reshape(target_shape=(
        model.layers[-1].output_shape[1] * model.layers[-1].output_shape[2],
        model.layers[-1].output_shape[3])))
        model.add(GRU(20))
        model.add(Dense(32))
        model.add(Dropout(0.5))
        model.add(Dense(4, activation='softmax'))
        optimizer = Adam(learning_rate=config['lr'], beta_1=0.9, beta_2=0.999,
                         amsgrad=False)
        model.compile(optimizer=optimizer,
                      loss=CategoricalCrossentropy(from_logits=True),
                      metrics=['accuracy'])
        model.summary()
        print("Model compiled.")
Beispiel #2
0
    def build_model(self, config):

        inputs = Input(shape=(22, 100))

        layer = inputs

        layer = Reshape((22, 100, 1))(layer)

        layer = Conv2D(48, (1, 10), activation='elu',
                       kernel_regularizer=l2(config['l2']))(layer)
        layer = BatchNormalization()(layer)
        # layer = Conv2D(15, (1, 15), activation='elu',kernel_regularizer=regularizers.l2(0.02))(layer)
        layer = Dropout(0.1)(layer)
        layer = Conv2D(40, (22, 1), activation='elu',
                       kernel_regularizer=l2(config['l2']))(layer)
        layer = BatchNormalization()(layer)
        # layer = Conv2D(14, (22, 1), activation='elu')(layer)
        layer = AveragePooling2D((1, 25), strides=(1, 4))(layer)

        layer = Flatten()(layer)

        layer = Dense(4)(layer)
        outputs = Activation('softmax')(layer)
        self.model = Model(inputs=inputs, outputs=outputs)

        optimizer = Adam(learning_rate=config['lr'], beta_1=0.9, beta_2=0.999,
                         amsgrad=False)
        self.model.compile(optimizer=optimizer,
                      loss=CategoricalCrossentropy(from_logits=True),
                      metrics=['accuracy'])
        self.model.summary()
        print("Model compiled.")
Beispiel #3
0
    def _build_model(self):
        # Defines the input for the model: s(t), s(t+1), a(t)
        state1_input = Input(self.state_size)
        state2_input = Input(self.state_size)
        action_input = Input(self.action_size)
        encoded_input = Input(self.feature_size)

        # Build the feature encoding network
        features = Sequential()
        features.add(
            Dense(self.hidden_size,
                  input_dim=self.state_size,
                  activation='relu'))
        features.add(Dense(self.hidden_size, activation='relu'))
        features.add(Dense(self.feature_size, activation='linear'))

        # Get the incodings with shared weights
        encodings_s1 = features(state1_input)
        encodings_s2 = features(state2_input)
        state_encodings = layers.Concatenate(axis=-1)(
            [encodings_s1, encodings_s2])

        # Build the inverse model
        inverse = Sequential()
        inverse.add(
            Dense(self.hidden_size,
                  input_dim=self.feature_size * 2,
                  activation='relu'))
        inverse.add(Dense(self.hidden_size, activation='relu'))

        # Transmit the state encodings to get an action prediction
        action_prediction = Dense(self.action_size, activation='softmax')(
            inverse(state_encodings))  # maybe softmax?
        action_network = Model(inputs=[state1_input, state2_input],
                               outputs=action_prediction)
        action_network.compile(loss=[CategoricalCrossentropy()],
                               optimizer=Adam(lr=self.lr),
                               loss_weights=[1 - self.beta])
        print(action_network.summary())

        # Build the forward model
        forward = Sequential()
        forward.add(
            Dense(self.hidden_size,
                  input_dim=self.feature_size + self.action_size,
                  activation='relu'))
        forward.add(Dense(self.hidden_size, activation='relu'))

        # transmit the concatenated feature encodings of s(t) with the action to get a state prediction
        forward_input = layers.Concatenate(axis=-1)(
            [action_input, encoded_input])
        state_prediction = Dense(self.feature_size)(forward(forward_input))
        state_enc_network = Model(inputs=[action_input, encoded_input],
                                  outputs=state_prediction)
        state_enc_network.compile(loss=['mse'],
                                  optimizer=Adam(lr=self.lr),
                                  loss_weights=[self.beta])
        print(state_enc_network.summary())
        return action_network, state_enc_network, features
Beispiel #4
0
def create_final_model(base_model, img_size, num_classes):
    base_model.trainable = False
    inputs = Input(shape=(img_size, img_size, 3))
    x = base_model(inputs, training=False)
    x = GlobalAveragePooling2D()(x)
    outputs = Dense(num_classes, activation='softmax')(x)
    m = Model(inputs, outputs)
    m.compile(optimizer=Adam(),
              loss=CategoricalCrossentropy(),
              metrics=['acc'])
    return m
Beispiel #5
0
def individual_evaluator(individual: MLPIndividual, trn: Proben1Split,
                         tst: Proben1Split, **kwargs):
    """Evaluate an individual.

    :param individual: current individual to evaluate.
    :param trn: training data and labels.
    :param tst: validation data and labels.
    :param multi_class: ``True`` if the dataset is for multiclass
        classification.
    :returns: the fitness values.

    """
    multi_class = kwargs.get("multi_class", False)
    start_time = time.perf_counter()
    units_size_list = [
        layer.config["units"] for layer in individual.layers[:-1]
    ]
    DGPLOGGER.debug(
        f"    Evaluating individual with neuron number: {units_size_list}")
    # Create the model with the individual configuration
    model = Sequential()

    for layer_index, layer in enumerate(individual.layers):
        model.add(Dense.from_config(layer.config))
        model.layers[layer_index].set_weights([layer.weights, layer.bias])

    model.compile(
        optimizer=SGD(learning_rate=0.01),
        loss=CategoricalCrossentropy()
        if multi_class else BinaryCrossentropy(),
    )

    model.fit(trn.X, trn.y_cat, epochs=100, batch_size=16, verbose=0)

    # Predict the scores
    predicted_y = model.predict_classes(tst.X)
    f2_score = fbeta_score(
        tst.y,
        predicted_y,
        beta=2,
        average="micro" if multi_class else "binary",
    )
    error_perc = (1.0 -
                  accuracy_score(tst.y, predicted_y, normalize=True)) * 100
    neuron_layer_score = sum(units_size_list) * len(units_size_list)
    DGPLOGGER.debug(
        f"        error%={error_perc:.2f}\n"
        f"        neuron/layer-score={neuron_layer_score:.2f}\n"
        f"        f2-score={f2_score:.5f}\n"
        f"        evaluation time={time.perf_counter() - start_time: .2f} sec")

    return (error_perc, neuron_layer_score, f2_score)
Beispiel #6
0
def defineModel(input_shape, num_outputs):

    nn = Sequential()

    nn.add(Dense(1000, input_shape=input_shape, name='dens_1_class'))
    nn.add(Dense(100, activation='sigmoid', name='dens_2_class'))
    nn.add(Dense(num_outputs, activation='softmax', name='dens_3_class'))

    nn.compile(optimizer=Adam(learning_rate=0.001),
               loss=CategoricalCrossentropy(),
               metrics='accuracy')

    return nn
Beispiel #7
0
 def build_model(self, config):
     # replace hardcoded dimensions with config dictionary
     model = self.model
     model.add(GRU(22, input_shape=(config['input_shape'][0], config['input_shape'][1]), return_sequences=True))
     model.add(Conv1D(22, 10))
     model.add(Flatten())
     model.add(Dropout(config['dropout']))
     model.add(Dense(4, activation='softmax'))
     optimizer = Adam(learning_rate=config['lr'], beta_1=0.9, beta_2=0.999,
                      amsgrad=False)
     model.compile(optimizer=optimizer,
                   loss=CategoricalCrossentropy(from_logits=True),
                   metrics=['accuracy'])
     model.summary()
     print("Model compiled.")
Beispiel #8
0
    def __init__(self,
                 input_size: Tuple[int, int],
                 output_size: int,
                 cached_model: bool,
                 cached_model_path: str,
                 seed=None):

        np.random.seed(seed)
        if cached_model:
            # returns an already compiled model
            self._model = load_model(cached_model_path)
        else:
            # returns an already compiled model
            self._model = self.create_model(input_size,
                                            output_size,
                                            loss=CategoricalCrossentropy(),
                                            optimizer="adam",
                                            metrics=['accuracy'],
                                            verbose=False)
Beispiel #9
0
def create_conv_model_with_two_inputs(other_features_dim, vocab_size,
                                      number_of_dimensions, embedding_matrix,
                                      padding_len, output_number):

    loss = CategoricalCrossentropy(label_smoothing=0.2)
    #loss = 'categorical_crossentropy'
    activation = 'softmax'
    filters, kernel_size = 10, 5

    text_input = Input(shape=(padding_len, ))
    vector_input = Input(shape=(other_features_dim, ))

    embedding_layer = Embedding(
        input_dim=vocab_size,
        output_dim=number_of_dimensions,
        weights=[embedding_matrix],
        input_length=padding_len,
        trainable=True,
        embeddings_regularizer=reg.l1(0.0005))(text_input)
    conv_layer = Conv1D(filters,
                        kernel_size,
                        activation='relu',
                        kernel_regularizer=reg.l2(0.005))(embedding_layer)
    conv_layer = Dropout(0.2)(conv_layer)
    conv_layer = MaxPooling1D(10)(conv_layer)
    #conv_layer = GlobalMaxPooling1D()(conv_layer)
    conv_layer = Flatten()(conv_layer)

    concat_layer = Concatenate()([vector_input, conv_layer])
    concat_layer = Dense(10, activation='relu')(concat_layer)
    concat_layer = Dropout(0.1)(concat_layer)

    output = Dense(output_number, activation=activation)(concat_layer)

    model = Model(inputs=[text_input, vector_input], outputs=output)
    model.compile(optimizer=Adam(learning_rate=0.01),
                  loss=loss,
                  metrics=['acc', f1_micro, f1_weighted])

    return model
Beispiel #10
0
 def build_model(self, config):
     input_shape = config['input_shape']
     model = self.model
     model.add(Conv1D(22, 10,
                      input_shape=(input_shape[0], input_shape[1]),
                      kernel_regularizer=l2(config['l2'])))
     model.add(BatchNormalization(axis=1))
     model.add(MaxPooling1D(2))
     if config['LSTM']:
         model.add(LSTM(44, kernel_regularizer=l2(config['l2']), return_sequences=True))
     else:
         model.add(GRU(44, kernel_regularizer=l2(config['l2']), return_sequences=True))
     model.add(Dropout(config['dropout']))
     model.add(Flatten())
     model.add(Dense(64))
     model.add(Dropout(config['dropout']))
     model.add(Dense(4, activation='softmax'))
     optimizer = Adam(learning_rate=config['lr'], beta_1=0.9, beta_2=0.999,
                      amsgrad=False)
     model.compile(optimizer=optimizer,
                   loss=CategoricalCrossentropy(from_logits=True),
                   metrics=['accuracy'])
     model.summary()
     print("Model compiled.")
Beispiel #11
0
trained_model = load_model(
    r'NN_data\model_19_without_steering_with_speed_GRU_cut_in_bi.h5')
trained_model.summary()
# Reshape predicted Y to dim (timesteps*m, feature_dims)
Y_valid_pred = trained_model.predict(X_validation, verbose=2)
Y_valid_pred_temp = Y_valid_pred.reshape(
    (Y_valid_pred.shape[0] * Y_valid_pred.shape[1], 3))
Y_valid_pred_bool = np.argmax(Y_valid_pred_temp, axis=1)

# Reshape real Y to dim
Y_validation_temp = Y_validation.reshape(
    (Y_validation.shape[0] * Y_validation.shape[1], 3))
Y_valid_bool = np.argmax(Y_validation_temp, axis=1)

# Calculate benchmark of error
cee = CategoricalCrossentropy()
categorical_err_benchmark = cee(Y_valid_pred, Y_validation).numpy()
# print("categorical_crossentropy_benchmark: {:.2f}".format(categorical_err_benchmark))
err_dict_benchmark = classification_report(Y_valid_bool,
                                           Y_valid_pred_bool,
                                           output_dict=True)
# print(error_dict)
# print(err_dict_benchmark)
f1_benchmark = (float(err_dict_benchmark['0']['f1-score']),
                float(err_dict_benchmark['1']['f1-score']),
                float(err_dict_benchmark['2']['f1-score']),
                float(err_dict_benchmark['weighted avg']['f1-score']))
print(
    "f1_score_benchmark: (free driving-{:.3f})  (left cut in-{:.3f})  (right cut in-{:.3f}) (weighted avg-{:.3f})"
    .format(f1_benchmark[0], f1_benchmark[1], f1_benchmark[2],
            f1_benchmark[3]) + '\n')
Beispiel #12
0
def perform_run(model_params, cluster_job, model_checkpoints, config=None):
    # Load data
    dataset_type = get_dataset_type_by_name(
        model_params.get_parameter("dataset_name"))
    (x_train, y_train), (x_test, y_test), num_classes = get_dataset_by_type(
        dataset_type, model_params.get_parameter("seed"))
    x_val, y_val = None, None
    x_scaling, y_scaling = None, None

    if model_params.get_parameter("temp_scaling", False):
        x_train, x_scaling, y_train, y_scaling = train_test_split(
            x_train,
            y_train,
            test_size=0.15,
            random_state=model_params.get_parameter("seed"))

    if not model_params.get_parameter("test_run"):
        x_train, x_val, y_train, y_val = train_test_split(
            x_train, y_train, random_state=model_params.get_parameter("seed"))

    x_train, y_train, pixel_mean = preprocess_data(
        x_train, y_train, num_classes, None,
        model_params.get_parameter("subtract_pixel_mean"))
    if not model_params.get_parameter("test_run"):
        x_val, y_val, _ = preprocess_data(x_val, y_val, num_classes,
                                          pixel_mean)
    else:
        x_test, y_test, _ = preprocess_data(x_test, y_test, num_classes,
                                            pixel_mean)

    if model_params.get_parameter("temp_scaling", False):
        x_scaling, y_scaling, _ = preprocess_data(x_scaling, y_scaling,
                                                  num_classes, pixel_mean)

    input_shape = x_train.shape[1:]

    # Instantiate model
    model_type = model_params.get_parameter("model_type")
    model_fn = get_backbone_model_fn_by_type(model_type)

    if model_params.get_parameter("temp_scaling", False):
        final_activation = None
    else:
        final_activation = "softmax"

    model = model_fn(classes=num_classes,
                     weights=None,
                     input_shape=input_shape,
                     final_activation=final_activation)

    if model_params.get_parameter("loss_type") == LossType.LR:
        loss_fn = LabelRelaxationLoss(
            alpha=model_params.get_parameter("alpha"), n_classes=num_classes)
    elif model_params.get_parameter("loss_type") == LossType.FOCAL:
        loss_fn = FocalLoss(alpha=model_params.get_parameter("alpha"))
    elif model_params.get_parameter(
            "loss_type") == LossType.CONFIDENCE_PENALTY:
        loss_fn = ConfidencePenaltyLoss(
            alpha=model_params.get_parameter("alpha"))
    else:
        loss_fn = CategoricalCrossentropy(
            label_smoothing=model_params.get_parameter("alpha"),
            from_logits=model_params.get_parameter("temp_scaling", False))

    lr_sched_prov = LearningRateScheduleProvider(
        init_lr=model_params.get_parameter("initial_lr"),
        steps=model_params.get_parameter("steps", [80, 120, 160, 180]),
        multiplier=model_params.get_parameter("lr_sched_multipler", 0.1))

    opt_params = {}
    if model_params.get_parameter("gradient_clipping") > 0.0:
        opt_params["clipvalue"] = model_params.get_parameter(
            "gradient_clipping")
    opt_params["decay"] = model_params.get_parameter("decay")

    optimizer = SGD(learning_rate=lr_sched_prov.get_lr_schedule(0),
                    momentum=0.9,
                    nesterov=True,
                    **opt_params)

    model.compile(loss=loss_fn, optimizer=optimizer, metrics=['accuracy'])

    # Callbacks
    callbacks = []
    if model_params.get_parameter("reduce_on_plateau"):
        callbacks.append(
            ReduceLROnPlateau(factor=np.sqrt(0.1),
                              cooldown=0,
                              patience=10,
                              min_lr=0.5e-6))
    callbacks.append(LearningRateScheduler(lr_sched_prov.get_lr_schedule))

    if model_checkpoints and not model_params.get_parameter("test_run"):
        save_dir = get_model_checkpoint_path(config)
        model_name = '{}_{}_model.h5'.format(dataset_type.value, model_type)
        if not os.path.isdir(save_dir):
            os.makedirs(save_dir)
        filepath = os.path.join(save_dir, model_name)

        callbacks.append(
            ModelCheckpoint(filepath=filepath,
                            monitor='val_accuracy',
                            verbose=1,
                            save_best_only=True))

    if not cluster_job:
        callbacks.append(TensorBoard(log_dir=get_tensorboard_path(config)))

    callbacks.append(TerminateOnNaN())

    verbosity = 1
    if cluster_job:
        verbosity = 0

    val_data = None
    if not model_params.get_parameter("test_run"):
        val_data = (x_val, y_val)

    # Training
    if not model_params.get_parameter("data_augmentation"):
        logging.info('Training without data augmentation...')
        conduct_simple_model_training(model,
                                      x_train,
                                      y_train,
                                      validation_data=val_data,
                                      model_parameters=model_params,
                                      callbacks=callbacks,
                                      shuffle=True,
                                      verbose=verbosity)
    else:
        logging.info('Training without data augmentation.')
        datagen = ImageDataGenerator(featurewise_center=False,
                                     samplewise_center=False,
                                     featurewise_std_normalization=False,
                                     samplewise_std_normalization=False,
                                     zca_whitening=False,
                                     zca_epsilon=1e-06,
                                     rotation_range=0,
                                     width_shift_range=0.1,
                                     height_shift_range=0.1,
                                     shear_range=0.,
                                     zoom_range=0.,
                                     channel_shift_range=0.,
                                     fill_mode='nearest',
                                     cval=0.,
                                     horizontal_flip=True,
                                     vertical_flip=False,
                                     rescale=None,
                                     preprocessing_function=None,
                                     data_format=None,
                                     validation_split=0.0)
        datagen.fit(x_train)

        conduct_gen_model_training(
            model,
            datagen.flow(x_train,
                         y_train,
                         batch_size=model_params.get_parameter("batch_size")),
            val_data,
            model_parameters=model_params,
            callbacks=callbacks,
            verbose=verbosity)
    # Score trained model
    if model_params.get_parameter("test_run"):
        scores = model.evaluate(x_test, y_test, verbose=verbosity)
        logging.info('Test accuracy: {}'.format(scores[1]))
        mlflow.log_metric("test_accuracy", scores[1])

        # ECE evaluation
        preds_test = model.predict(x_test)
        if not model_params.get_parameter("temp_scaling", False):
            ece, _ = evaluate_ece(preds_test,
                                  y_test,
                                  n_bins=15,
                                  temp_scaling=False)
        else:
            scaling_preds = model.predict(x_scaling)

            ece, opt_t = evaluate_ece(preds_test,
                                      y_test,
                                      n_bins=15,
                                      temp_scaling=True,
                                      preds_val=scaling_preds,
                                      y_val=y_scaling)
            mlflow.log_param("T", opt_t)

        mlflow.log_metric("ece", ece)

        return scores[1], ece
    else:
        # Return validation error
        scores = model.evaluate(x_val, y_val, verbose=verbosity)

        # ECE evaluation
        preds_val = model.predict(x_val)
        if not model_params.get_parameter("temp_scaling", False):
            ece, _ = evaluate_ece(preds_val,
                                  y_val,
                                  n_bins=15,
                                  temp_scaling=False)
        else:
            scaling_preds = model.predict(x_scaling)

            ece, opt_t = evaluate_ece(preds_val,
                                      y_val,
                                      n_bins=15,
                                      temp_scaling=True,
                                      preds_val=scaling_preds,
                                      y_val=y_scaling)
            mlflow.log_param("T", opt_t)

        mlflow.log_metric("ece", ece)

        return scores[1], ece
Beispiel #13
0
    def build_model(self, config):
        if config['MLP']:
            input_shape = config['input_shape']
            model = self.model
            #model.add(Flatten())
            model.add(Reshape(input_shape=(input_shape[1], input_shape[2], input_shape[3]),
                              target_shape=((input_shape[1]*input_shape[2]*input_shape[3],))))
            #model.add(Dense(126, activation='relu'))
            model.add(Dense(100, activation='relu'))
            model.add(Dropout(0.5))
            model.add(Dense(100, activation='relu'))
            model.add(Dropout(0.5))
            model.add(Dense(4, activation='softmax'))
            optimizer = Adam(learning_rate=config['lr'], beta_1=0.9, beta_2=0.999,
                             amsgrad=False)
            model.compile(optimizer=optimizer,
                          loss=CategoricalCrossentropy(from_logits=True),
                          metrics=['accuracy'])
            model.summary()

        else:
            input_shape = config['input_shape']
            # input_shape is (batch, rows, cols, channel)
            model = self.model
            model.add(Conv2D(128, kernel_size=(2, 2), input_shape=(
            input_shape[1], input_shape[2], input_shape[3])))
            print(model.layers[-1].output_shape)
            # kernel_regularizer=l2(config['l2'])))
            original_shape = model.layers[-1].output_shape
            model.add(Reshape(
                (original_shape[1] * original_shape[2], original_shape[3])))
            model.add(BatchNormalization(axis=2))
            model.add(Reshape((original_shape[1],
                               original_shape[2],
                               original_shape[3])))
            model.add(Conv2D(64, kernel_size=(
            2, 2)))  # , kernel_regularizer=l2(config['l2'])))
            # model.add(Conv2D(64, kernel_size=(2, 2)))
            model.add(MaxPooling2D())
            model.add(Flatten())
            model.add(Dropout(0.2))
            model.add(Dense(32, activation='relu'))
            model.add(Dropout(0.2))
            model.add(Dense(4, activation='softmax'))

            # input_shape = config['input_shape']
            # # input_shape is (batch, rows, cols, channel)
            # model = self.model
            # model.add(Conv2D(32, kernel_size=(2, 2), input_shape=(
            # input_shape[1], input_shape[2], input_shape[3])))
            # model.add(Conv2D(32, kernel_size=(2, 2)))
            # model.add(MaxPooling2D())
            # model.add(Flatten())
            # model.add(Dropout(0.5))
            # model.add(Dense(128))
            # model.add(Dropout(0.5))
            # model.add(Dense(4, activation='softmax'))
            # model.summary()




            optimizer = Adam(learning_rate=config['lr'],
                                                 beta_1=0.9, beta_2=0.999,
                                                 amsgrad=False)
            model.compile(optimizer=optimizer,
                          loss=CategoricalCrossentropy(
                              from_logits=True),
                          metrics=['accuracy'])
            model.summary()
Beispiel #14
0
def distributions_over_time(dir_path, filepath, plot=False):
    class_names = [
        'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog',
        'horse', 'ship', 'truck'
    ]
    with open(dir_path + filepath, 'r') as f:
        data = json.load(f)
        f.close()
    distributions = data['class distr']
    true_lab = data['true lab']
    predicted = data['predicted']
    cat_cross = CategoricalCrossentropy()
    entropies = {}
    # Non classified - Lost chance test
    """diff = []
    diff_not = []"""
    # Class occurrencies test
    labels = np.zeros((10, ))
    labels_f = np.zeros((10, ))
    if not plot:
        keys = list(distributions.keys())
    else:
        # Cherry-picked examples for plotting
        keys = ['1', '22', '24', '28']
    for key in keys:
        if key not in entropies.keys():
            entropies[key] = []
        for el in distributions[key]:
            margin = [x / sum(el[:10]) for x in el[:10]]
            one_hot_label = [
                1.0 if x == true_lab[key] else 0.0 for x in range(len(margin))
            ]
            entropies[key].append(cat_cross(margin, one_hot_label).numpy())
        """print(
            'Number of steps: {sn} - Starting entropy: {se} - Ending entropy: {ee} - Predicted: {p} - Ground truth: {gt}'.format(
                sn=len(entropies[key]),
                se=entropies[key][0],
                ee=entropies[key][len(entropies[key]) - 1],
                p=predicted[key] if key in predicted.keys() else 'Non predicted',
                gt=true_lab[key]))"""
        # Non classified - Lost chance test
        """if key in predicted.keys():
            diff.append(abs(entropies[key][len(entropies[key]) - 1] - entropies[key][len(entropies[key]) - 2]))
        else:
            diff_not.append(abs(entropies[key][len(entropies[key]) - 1] - entropies[key][len(entropies[key]) - 2]))
    print(np.average(diff), np.average(diff_not))"""
        # Class occurrencies test
        if key not in predicted.keys() and entropies[key][len(entropies[key]) -
                                                          1] < 3:
            labels[true_lab[key]] += 1
        if key not in predicted.keys() and entropies[key][len(entropies[key]) -
                                                          1] > 3:
            labels_f[true_lab[key]] += 1
    fig, ax = plt.subplots(1, 1, figsize=(7, 7))
    ax.set_ylabel('Occurrences')
    plt.setp(ax.get_xticklabels(),
             rotation=45,
             ha="right",
             rotation_mode="anchor")
    ax.plot(class_names, list(labels), label='Correct non classified')
    ax.plot(class_names, list(labels_f), label='Wrong not classified')
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
               loc=9,
               ncol=2,
               mode="expand",
               borderaxespad=0.)
    fig.savefig(dir_path + 'non_class_occurrences.png')
    plt.show()
    print(labels, labels_f)

    if plot:
        fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 10))
        axs[0, 0].plot(range(len(entropies[keys[0]])), entropies[keys[0]])
        axs[0, 0].set_title('Right classification')
        axs[0, 0].set_xlabel('Timesteps')
        axs[0, 0].set_ylabel('Entropy')
        axs[0, 1].plot(range(len(entropies[keys[1]])), entropies[keys[1]])
        axs[0, 1].set_title('Non classified episode')
        axs[0, 1].set_xlabel('Timesteps')
        axs[0, 1].set_ylabel('Entropy')
        axs[1, 0].plot(range(len(entropies[keys[2]])), entropies[keys[2]])
        axs[1, 0].set_title('Missed opportunity')
        axs[1, 0].set_xlabel('Timesteps')
        axs[1, 0].set_ylabel('Entropy')
        axs[1, 1].plot(range(len(entropies[keys[3]])), entropies[keys[3]])
        axs[1, 1].set_title('Wrong classification')
        axs[1, 1].set_xlabel('Timesteps')
        axs[1, 1].set_ylabel('Entropy')
        plt.show()
        fig.savefig(dir_path + 'entropies.png')
Beispiel #15
0
    ggru_mp = MaxPooling1D()(ggru_do2)
    ggru_ap = AveragePooling1D()(ggru_do2)
    ggru_out = concatenate([ggru_mp, ggru_ap])

    # GloveBiLSTM
    glstm_r1 = Bidirectional(
        LSTM(32,
             activation='sigmoid',
             recurrent_dropout=0.2,
             recurrent_activation='sigmoid',
             return_sequences=True))(e)
    glstm_do1 = Dropout(0.5)(glstm_r1)
    glstm_r2 = Bidirectional(
        LSTM(32,
             activation='sigmoid',
             recurrent_dropout=0.2,
             recurrent_activation='sigmoid',
             return_sequences=True))(glstm_do1)
    glstm_do2 = Dropout(0.5)(glstm_r2)
    glstm_mp = MaxPooling1D()(glstm_do2)
    glstm_ap = AveragePooling1D()(glstm_do2)
    glstm_out = concatenate([glstm_mp, glstm_ap])

    output_1 = concatenate([gcnn_out, grnn_out, ggru_out, glstm_out])
    outputs = Dense(4)(output_1)

    model = keras.Model(inputs=inputs, outputs=outputs, name='fusion_model')
    model.summary()

    model.compile(loss=CategoricalCrossentropy(), optimizer=Adam(lr=10**-4))
Beispiel #16
0
from keras.metrics import CategoricalAccuracy
from keras.optimizers import RMSprop, Adam

model = Sequential([
    Dense(units=32, input_shape=(784, ), activation='relu'),
    Dense(units=64, activation='relu'),
    Dense(units=10, activation='softmax')
])

#%% Model summary
model.summary()

#%% Compiling the model

model.compile(optimizer=Adam(),
              loss=CategoricalCrossentropy(),
              metrics=[CategoricalAccuracy()])

#%% Training the model

model.fit(x=X_train, y=Y_train, batch_size=16, epochs=20)

#%% Evaluating the model

model.evaluate(x=X_dev, y=Y_dev)

#%% Predicting on test set

predicted_one_hot_encoder = model.predict(test)
predicted_original_values = encoder.inverse_transform(
    predicted_one_hot_encoder)
Beispiel #17
0
    
file_names = np.asarray(file_names)
labels = np.asarray(labels)
images_data = np.asarray(images_data)

# split data
sss = StratifiedShuffleSplit(test_size=0.2)
train_index, test_index = list(sss.split(images_data, labels))[0]
train_images = images_data[train_index]
test_images = images_data[test_index]
train_labels = labels[train_index]
test_labels = labels[test_index]

model = Sequential([
    layers.Dense(256, activation=activations.relu),
    layers.Dense(128, activation=activations.relu),
    layers.Dense(10, activation=activations.softmax)
])

model.compile(optimizer=Adam(learning_rate=0.01), loss=CategoricalCrossentropy())

history = model.fit(train_images, to_categorical(train_labels), epochs=10, validation_split=0.2)
plot_history(history)
predictions = model.predict_classes(test_images)

print(accuracy_score(test_labels, predictions))

# dummy = DummyClassifier(strategy='uniform')
# dummy.fit(train_images, train_labels)
# predictions = dummy.predict(test_images)
# print(accuracy_score(test_labels, predictions))
Beispiel #18
0
def create_alex_model(train_images, train_labels, test_images, test_labels):

    resized_train_imgs = process_images(train_images)
    train_data = resized_train_imgs.numpy()

    resized_test_imgs = process_images(test_images)
    test_data = resized_test_imgs.numpy()

    model = keras.models.Sequential([
        keras.layers.Conv2D(filters=32,
                            kernel_size=(9, 9),
                            strides=(4, 4),
                            activation='relu',
                            input_shape=(227, 227, 3)),
        keras.layers.MaxPool2D(pool_size=(3, 3), strides=(2, 2)),
        keras.layers.BatchNormalization(),
        keras.layers.Conv2D(filters=64,
                            kernel_size=(5, 5),
                            strides=(1, 1),
                            activation='relu',
                            padding="same"),
        keras.layers.MaxPool2D(pool_size=(3, 3), strides=(2, 2)),
        keras.layers.BatchNormalization(),
        keras.layers.Conv2D(filters=128,
                            kernel_size=(3, 3),
                            strides=(1, 1),
                            activation='relu',
                            padding="same"),
        keras.layers.Conv2D(filters=128,
                            kernel_size=(3, 3),
                            strides=(1, 1),
                            activation='relu',
                            padding="same"),
        keras.layers.Conv2D(filters=64,
                            kernel_size=(3, 3),
                            strides=(1, 1),
                            activation='relu',
                            padding="same"),
        keras.layers.MaxPool2D(pool_size=(3, 3), strides=(2, 2)),
        keras.layers.Flatten(),
        keras.layers.Dense(512, activation='relu'),
        keras.layers.Dropout(0.5),
        keras.layers.Dense(256, activation='relu'),
        keras.layers.Dropout(0.5),
        keras.layers.Dense(7, activation='softmax')
    ])

    opt = keras.optimizers.SGD(learning_rate=0.005)

    model.compile(loss=CategoricalCrossentropy(from_logits=False),
                  optimizer=opt,
                  metrics=['accuracy'])
    model.summary()

    history = model.fit(train_data,
                        train_labels,
                        epochs=20,
                        batch_size=2,
                        validation_data=(test_data, test_labels),
                        shuffle=True)

    _, train_acc = model.evaluate(train_data, train_labels, verbose=0)
    _, test_acc = model.evaluate(test_data, test_labels, verbose=0)
    print('Train: %.3f, Test: %.3f' % (train_acc, test_acc))

    acc = history.history['accuracy']
    val_acc = history.history['val_accuracy']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    epochs_range = range(20)

    plt.figure(figsize=(20, 20))
    plt.subplot(2, 2, 1)
    plt.plot(epochs_range, acc, label='Training Accuracy')
    plt.plot(epochs_range, val_acc, label='Validation Accuracy')
    plt.legend(loc='lower right')
    plt.title('Training and Validation Accuracy')

    plt.subplot(2, 2, 2)
    plt.plot(epochs_range, loss, label='Training Loss')
    plt.plot(epochs_range, val_loss, label='Validation Loss')
    plt.legend(loc='upper right')
    plt.title('Training and Validation Loss')
    plt.show()

    # y = np.concatenate([y for x, y in test_data], axis=0)
    predictions = model.predict(test_data)
    y_pred = np.argmax(predictions, axis=1)

    rounded_labels = np.argmax(test_labels, axis=1)
    report = classification_report(rounded_labels, y_pred)
    print(report)

    emotionNames = [0, 1, 2, 3, 4, 5, 6]
    matrix = confusion_matrix(rounded_labels, y_pred)
    print(matrix)
Beispiel #19
0
class GoftNet:
    _OPTIMIZERS = {'adam': Adam, 'SGD': SGD, 'RMSprop': RMSprop}

    _LOSS_FUNCTIONS = {

        # TODO Why categorical crossentropy gives my a constant zero loss??
        'categorical_crossentropy': CategoricalCrossentropy(),
        'binary_crossentropy': BinaryCrossentropy(),
        'mse': MSE,
    }

    def __init__(self, config):

        self._num_classes = config['data']['num_classes']
        self._class_labels = config['data']['labels']
        self._class_labels_dict = {
            i: self._class_labels[i]
            for i in range(len(self._class_labels))
        }
        self._input_dim = config['model']['input_dim']

        # Feature Extractor Blocks
        # Assume the user didnt mess around with these arguments...
        self._num_features = config['model']['num_features']
        self._kernel_shapes = config['model']['kernel_shapes']
        self._num_conv_layers = config['model']['num_conv_layers']

        # Classifier Layers
        self._units = config['model']['units']
        self._last_layer_activation = config['model'].get(
            'last_later_activation', None)

        # Training parameters.
        self._optimizer = config['train']['optimizer']
        self._loss_function = config['train']['loss_function']
        self._epochs = config['train']['epochs']

        # General
        self._output_dir = config['general'][
            'output_dir']  # Here Tensorboard logs will be written.
        self._summary = True

        # Eval
        self._model_path = config['eval']['model_path']

        # define pathes
        timestamp = str(int(time()))
        self.model_dir_path = os.path.join(self._output_dir, timestamp)
        self.model_path = os.path.join(self.model_dir_path, 'model.h5')
        self.log_dir_path = os.path.join(self._output_dir, timestamp, 'logs')

        os.makedirs(self.model_dir_path, exist_ok=True)
        os.makedirs(self.log_dir_path, exist_ok=True)

        self._create_model()

    def _create_block(self,
                      num_features,
                      kernel_shape,
                      number_conv_layers,
                      first_layer,
                      last_layer,
                      padding='same'):

        for _ in range(number_conv_layers):

            if first_layer:
                self._model.add(
                    Conv2D(num_features,
                           kernel_shape,
                           padding=padding,
                           input_shape=self._input_dim))
            else:
                self._model.add(
                    Conv2D(num_features, kernel_shape, padding=padding))

            self._model.add(BatchNormalization())
            # self._model.add(Dropout(0.3))
            self._model.add(Activation('relu'))

        if not last_layer:
            self._model.add(MaxPooling2D(pool_size=(2, 2)))

    def _get_optimizer(self):
        name = self._optimizer['name']
        opt_params = self._optimizer['params']

        return self._OPTIMIZERS[name](**opt_params)

    def _compile(self):

        optimizer = self._get_optimizer()
        loss_function = self._LOSS_FUNCTIONS[self._loss_function]

        self._model.compile(loss=loss_function,
                            optimizer=optimizer,
                            metrics=['accuracy'])

    def _create_model(self, print_color='yellow'):

        print(colored('###################################', print_color))
        print(colored('######### CREATING MODEL #########', print_color))
        print(colored('###################################', print_color))

        # build the CNN architecture with Keras Sequential API
        self._model = Sequential()

        # ---------------------------------------- #
        # --------- DEFINE F.E BLOCKS ------------ #
        # ---------------------------------------- #
        for i, (num_features, kernel_shape, num_conv_layers) in enumerate(
                zip(self._num_features, self._kernel_shapes,
                    self._num_conv_layers)):
            if i == 0:  # First Layer. Need to define input layer
                first_layer, last_layer = True, False

            elif i == len(
                    self._num_features) - 1:  # Last Layer. No max pooling.
                first_layer, last_layer = False, True

            else:
                first_layer, last_layer = False, False

            self._create_block(num_features,
                               kernel_shape,
                               num_conv_layers,
                               first_layer=first_layer,
                               last_layer=last_layer)

        # ---------------------------------------- #
        # ----- DEFINE CLASSIFIER BLOCKS --------- #
        # ---------------------------------------- #
        self._model.add(Flatten())

        for units in self._units:
            self._model.add(Dense(units))
            self._model.add(Activation('relu'))

        self._model.add(Dense(self._num_classes))
        if self._last_layer_activation is not None:
            # If last layer activation is None, loss wil be calculated directly on logits.
            self._model.add(Activation(self._last_layer_activation))

        # Compile the model with chosen optimizer.
        self._compile()

        # Summary
        if self._summary:
            self._model.summary()

    def train(self, train_data, val_data):

        callbacks = [
            ModelCheckpoint(filepath=self.model_path,
                            monitor='val_accuracy',
                            mode='max',
                            save_best_only=True),
            TensorBoard(log_dir=self.log_dir_path)
        ]

        train_log = self._model.fit_generator(generator=train_data,
                                              validation_data=val_data,
                                              epochs=self._epochs,
                                              callbacks=callbacks,
                                              verbose=1)

        self.plot_log(train_log=train_log, model_dir_path=self.model_dir_path)

    def load_model(self, ):
        self._model = load_model(self._model_path)
        self._compile()

    def inference_on_data(self, test_data):
        results = self._model.predict(test_data, verbose=1)
        results = [
            np.eye(self._num_classes)[np.argmax(res)] for res in results
        ]  # Turn results to one hot.
        return results

    def print_metrics(self, y_pred, y_test):

        y_pred_labels = [
            self._class_labels_dict[class_num]
            for class_num in np.argmax(y_pred, axis=1)
        ]
        y_test_labels = [
            self._class_labels_dict[class_num]
            for class_num in np.argmax(y_test, axis=1)
        ]

        cm = confusion_matrix(y_pred_labels,
                              y_test_labels,
                              labels=np.unique(y_test_labels))
        cm = pd.DataFrame(cm,
                          index=np.unique(y_test_labels),
                          columns=np.unique(y_test_labels))

        report = classification_report(y_test, y_pred)

        print(
            colored("\n===================================================",
                    'yellow'))
        print(
            colored("============== CLASSIFICATION REPORT ==============",
                    'yellow'))
        print(
            colored("===================================================",
                    'yellow'))
        print(colored(report + '\n', 'yellow'))
        print(colored(cm, 'yellow'))

    @staticmethod
    def plot_log(train_log, model_dir_path):

        # Plot training & validation accuracy values
        f = plt.figure(1)
        plt.plot(train_log.history['accuracy'])
        plt.plot(train_log.history['val_accuracy'])
        plt.title('Model accuracy')
        plt.ylabel('Accuracy')
        plt.xlabel('Epoch')
        plt.legend(['Train', 'Val'], loc='upper left')
        f.savefig(os.path.join(model_dir_path, 'acc.png'))

        # Plot training & validation loss values
        g = plt.figure(2)
        plt.plot(train_log.history['loss'])
        plt.plot(train_log.history['val_loss'])
        plt.title('Model loss')
        plt.ylabel('Loss')
        plt.xlabel('Epoch')
        plt.legend(['Train', 'Val'], loc='upper left')
        g.savefig(os.path.join(model_dir_path, 'loss.png'))
Beispiel #20
0
def custom_loss(y_true, y_pred):
    y = tf.argmax(y_true, axis=-1)
    mask = tf.greater(y, 0)
    return CategoricalCrossentropy((tf.boolean_mask(y_true, mask)),
                                   (tf.boolean_mask(y_pred, mask)))
Beispiel #21
0
def train():
    config_defaults = {
        'epochs': 20,
        'batch_size': 64,
        'weight_decay': 1e-3,
        'channel_1': 64,
        'channel_2': 128,
        'channel_3': 512,
        'dense_1': 256,  #256
        'dense_2': 128,  #128
        'dense_3': 4,
        'learning_rate': 3e-4,
        'kernel_size': 5,
        'dropout': 0.8,
        'test_split': 0.3,
        'random_state': 88,
        'opt': "Adam"
    }
    wandb.init(config=config_defaults)  #, project="age-classifier")
    config = wandb.config

    print("==============Splitting Data===========")
    X_train, X_test, Y_train, Y_test = train_test_split(
        merged_data,
        merged_labels,
        test_size=config.test_split,
        random_state=config.random_state)

    model = Sequential()
    model.add(
        Conv2D(config.channel_1,
               kernel_size=config.kernel_size,
               padding='same',
               activation='relu',
               input_shape=(img_size, img_size, 3),
               kernel_regularizer=l2(config.weight_decay)))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(
        Conv2D(config.channel_2,
               kernel_size=config.kernel_size,
               padding='same',
               activation='relu',
               kernel_regularizer=l2(config.weight_decay)))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(
        Conv2D(config.channel_3,
               kernel_size=config.kernel_size,
               padding='same',
               activation='relu',
               kernel_regularizer=l2(config.weight_decay)))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())

    model.add(Dense(config.dense_1,
                    kernel_regularizer=l2(config.weight_decay)))
    model.add(BatchNormalization())
    model.add(Activation("relu"))
    model.add(Dropout(config.dropout))

    model.add(Dense(config.dense_2,
                    kernel_regularizer=l2(config.weight_decay)))
    model.add(BatchNormalization())
    model.add(Activation("relu"))
    model.add(Dropout(config.dropout))

    model.add(
        Dense(config.dense_3,
              activation="softmax",
              kernel_regularizer=l2(config.weight_decay)))

    if config.opt == "Adam":
        optimizer = Adam(lr=config.learning_rate)
    elif config.opt == "RMSprop":
        optimizer = RMSprop(lr=config.learning_rate)

    model.compile(optimizer=optimizer,
                  loss=CategoricalCrossentropy(),
                  metrics=['accuracy'])
    model.fit(X_train,
              Y_train,
              validation_data=(X_test, Y_test),
              epochs=config.epochs,
              batch_size=config.batch_size,
              verbose=1,
              callbacks=[checkpoint, WandbCallback()])
base_model.trainable = False

inputs = keras.Input(shape=(image_shape, image_shape, 3), dtype=tf.float32)
x = base_model(inputs)
x = GlobalAveragePooling2D()(x)
outputs = Dense(7, activation='softmax')(x)
# outputs = Dense(7)(x)
model = Model(inputs, outputs)

lr_schedule = ExponentialDecay(initial_learning_rate=1e-3,
                               decay_steps=1000,
                               decay_rate=0.9)
optimizer = Adam(learning_rate=lr_schedule)

model.compile(optimizer=optimizer,
              loss=CategoricalCrossentropy(from_logits=True),
              metrics=[CategoricalAccuracy()])

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
    'data/train',
    target_size=(image_shape, image_shape),
    batch_size=32,
    class_mode='categorical',
Beispiel #23
0
    def train(self):
        '''
        Train the model over training data, evaluate accuracy
        and store trained model
        '''

        files = os.listdir(self.DATA_FOLDER)

        if not self.CSV_DATASET in files:
            cprint('[FOLDER without files]', 'blue', end=' ')
            print('The dataset directory', end=' ')
            cprint(f'{self.DATA_FOLDER}', 'green', end=' ')
            print(
                "doesn't contain required csv files of dataset and labels dictionary"
            )
            exit(0)

        #Load the dataset
        dataset = loadtxt(self.DATA_FOLDER + self.CSV_DATASET, delimiter=',')
        #Split into input (X) and input/label (y) variables
        self.X_train = dataset[0:len(dataset):2, :-1]
        self.Y_train = dataset[0:len(dataset):2, -1]
        self.X_validation = dataset[1:len(dataset):4, :-1]
        self.Y_validation = dataset[1:len(dataset):4, -1]
        self.X_test = dataset[3:len(dataset):4, :-1]
        self.Y_test = dataset[3:len(dataset):4, -1]
        print(len(self.X_train))

        cprint(f'\n\n{len(dataset)}', 'red', end='\n\n')

        #Define the keras model
        model = Sequential()

        #Map labels into integer values
        self.Y_train = to_categorical(self.Y_train, len(self.labels))
        self.Y_validation = to_categorical(self.Y_validation, len(self.labels))
        self.Y_test = to_categorical(self.Y_test, len(self.labels))

        if self.OPTION == 'spectrum':
            #Create the network with spectrogram feature as input
            model.add(
                Dense(1024, input_dim=len(self.X_train[0]), activation='relu'))
            model.add(Dense(len(self.labels), activation='sigmoid'))
            model.compile(loss=CategoricalCrossentropy(),
                          optimizer='adam',
                          metrics=['accuracy'])

            history = model.fit(self.X_train,
                                self.Y_train,
                                epochs=30,
                                shuffle=True)
        else:
            #Create the network with touch/touch_hit feature as input
            model.add(
                Dense(100, input_dim=len(self.X_train[0]), activation='relu'))
            model.add(Dense(len(self.labels), activation='sigmoid'))
            model.compile(loss=CategoricalCrossentropy(),
                          optimizer='adam',
                          metrics=['accuracy'])

            #end = int(len(self.X)/2)
            epochs = 0

            #Train the model (epochs=4 less accuracy)
            if self.OPTION == 'touch_hit':
                epochs = 20
            else:
                epochs = 30

            history = model.fit(self.X_train,
                                self.Y_train,
                                epochs=epochs,
                                shuffle=True)

        # Evaluate the model
        scores = model.evaluate(self.X_train, self.Y_train, verbose=0)
        cprint(f'Training accuracy:', 'green', end='  ')
        print(f'{scores[1]*100} %')

        # Evaluate the model
        scores = model.evaluate(self.X_validation,
                                self.Y_validation,
                                verbose=0)
        cprint(f'Validation accuracy:', 'green', end='  ')
        print(f'{scores[1]*100} %')

        # Evaluate the model
        scores = model.evaluate(self.X_test, self.Y_test, verbose=0)
        cprint(f'Test accuracy:', 'green', end='  ')
        print(f'{scores[1]*100} %')

        #Save the model on the File System
        model.save(self.DATA_FOLDER + self.MODEL_NAME)