Exemple #1
0
def predict_regressions(config: Union[
    str, SoccerSageConfig] = SoccerSageConfig()) -> None:
    if isinstance(config, str):
        config = SoccerSageConfig.from_yaml(config)
    config.pretrained_classifier = True
    files = SoccerSageFiles(config)
    time_steps = SoccerSageConfig.time_steps
    features, matches, thresholds = load_data_for_predictions(
        files.sql_query, time_steps, config.classifier_type)
    features = np.array(features)
    model = reg_mlstm_fcn(config)
    predictions = model.predict(features)

    parameters = []
    sql_code = '''
    INSERT IGNORE INTO matches
    (id, tf_regression)
    VALUES (%s, %s)
    ON DUPLICATE KEY UPDATE
      tf_regression = VALUES(tf_regression)
    '''

    for index, prediction in enumerate(predictions):
        match_id = matches[index]
        params = (match_id, int(prediction))
        print('PARAMS: {}'.format(params), file=sys.stderr)
        parameters.append(params)

    if len(parameters) > 0:
        engine = get_mysql_engine()
        with engine.connect() as conn:
            conn.execute(sql_code, parameters)
Exemple #2
0
def save_dataset(
        config: Union[str, SoccerSageConfig] = SoccerSageConfig()
) -> None:
    '''Store the train and test dataset.

    Args:
        config: A path of a file with config parameters or the default
                config parameters.
    '''
    if isinstance(config, str):
        config = SoccerSageConfig.from_yaml(config)
    files = SoccerSageFiles(config)
    time_steps = SoccerSageConfig.time_steps

    recency_features, pi_ratings = load_raw_data(files.sql_query, time_steps,
                                                config.classifier_type)
    ad_cpt, gh_cpt, ga_cpt, prediction_cpt = get_CPT(recency_features)
    # print('TRAIN_SIZE: {}'.format(len(train_dataset)), file=sys.stderr)
    # print('TEST SIZE: {}'.format(len(test_dataset)), file=sys.stderr)
    # write_holdout(train_dataset[:config.n_train_samples],
    #               files.train_dataset, time_steps)
    # write_holdout(test_dataset[:config.n_test_samples],
    #               files.test_dataset, time_steps)
    # bayesian_network_prediction(recency_features[100:300], ad_cpt, gh_cpt, ga_cpt, prediction_cpt)

    return (ad_cpt, gh_cpt, ga_cpt, prediction_cpt, pi_ratings)
Exemple #3
0
def mlstm_fcn(config: Union[str,
                            SoccerSageConfig] = SoccerSageConfig()) -> Model:
    '''Load and train the MLSTM-FCN model.
    Source: https://github.com/titu1994/MLSTM-FCN/blob/master/eeg_model.py

    Args:
        config: The configuration data.

    Returns:
        Model: A model in TensorFlow.
    '''
    if isinstance(config, str):
        config = SoccerSageConfig.from_yaml(config)

    n_classes = config.num_results
    if config.classifier_type == 'total_goals':
        n_classes = config.num_goals

    ip = Input(shape=(config.time_steps, config.feature_size))

    x = Permute((2, 1))(ip)
    #x = Masking(mask_value=config.masking_value)(x)
    x = LSTM(64)(ip)
    x = Dropout(0.8)(x)

    y = Conv1D(128, 8, padding='same', kernel_initializer='he_uniform')(ip)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = squeeze_excite_block(y)

    y = Conv1D(256, 5, padding='same', kernel_initializer='he_uniform')(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = squeeze_excite_block(y)

    y = Conv1D(128, 3, padding='same', kernel_initializer='he_uniform')(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)

    y = GlobalAveragePooling1D()(y)

    x = concatenate([x, y])

    out = Dense(n_classes, activation='softmax')(x)

    classifier = Model(ip, out)

    classifier.compile(
        loss='categorical_crossentropy',
        optimizer=tf.keras.optimizers.Adam(lr=config.learning_rate),
        metrics=['accuracy', ranked_probability_score])

    if config.pretrained_classifier:
        files = SoccerSageFiles(config)
        classifier.load_weights(files.model_weights)

    return classifier
Exemple #4
0
def predict_results(config: Union[
    str, SoccerSageConfig] = SoccerSageConfig()) -> None:
    if isinstance(config, str):
        config = SoccerSageConfig.from_yaml(config)
    config.pretrained_classifier = True
    files = SoccerSageFiles(config)
    time_steps = SoccerSageConfig.time_steps
    features, matches, thresholds = load_data_for_predictions(
        files.sql_query, time_steps, config.classifier_type)
    features = np.array(features)
    model = mlstm_fcn(config)
    predictions = model.predict(features)

    parameters = []
    sql_code = ''

    if config.classifier_type == 'results':
        prediction_indexes = predictions.argmax(axis=1)
        sql_code = '''
        INSERT IGNORE INTO matches
        (id, tf_prediction_test, tf_confidence_test)
        VALUES (%s, %s, %s)
        ON DUPLICATE KEY UPDATE
          tf_prediction_test = VALUES(tf_prediction_test),
          tf_confidence_test = VALUES(tf_confidence_test)
        '''

        for index, prediction in enumerate(prediction_indexes):
            match_id = matches[index]
            confidence = predictions[index][prediction]
            params = (match_id, int(prediction), float(confidence))
            print('PARAMS: {}'.format(params), file=sys.stderr)
            parameters.append(params)
    else:
        prediction_indexes = predictions.argmax(axis=1)
        sql_code = '''
        INSERT IGNORE INTO matches
        (id, tf_total_goals, tf_tg_confidence)
        VALUES (%s, %s, %s)
        ON DUPLICATE KEY UPDATE
          tf_total_goals = VALUES(tf_total_goals),
          tf_tg_confidence = VALUES(tf_tg_confidence)
        '''
        for index, threshold in enumerate(thresholds):
            if threshold and not math.isnan(threshold):
                match_id = matches[index]
                threshold = int(threshold)
                under = float(
                    np.array(predictions[index])[0:threshold + 1].sum())
                over = float(1.0 - under)
                if over > under:
                    params = (match_id, 1, over)
                else:
                    params = (match_id, 0, under)
                print('PARAMS: {}'.format(params), file=sys.stderr)
                parameters.append(params)
    '''
Exemple #5
0
    def __init__(self,
                 config: Union[str, SoccerSageConfig] = SoccerSageConfig()):
        if isinstance(config, str):
            config = SoccerSageConfig.from_yaml(config)
        self._input_dir = config.input_directory_path
        self._processed_dir = config.processed_directory_path
        self._output_dir = config.output_directory_path

        self._input_dir.mkdir(parents=True, exist_ok=True)
        self._processed_dir.mkdir(parents=True, exist_ok=True)
        self._output_dir.mkdir(parents=True, exist_ok=True)
        self.classifier_type = config.classifier_type
Exemple #6
0
def reg_mlstm_fcn(config: Union[
    str, SoccerSageConfig] = SoccerSageConfig()) -> Model:
    '''Load and train the regression MLSTM-FCN model.
    Source: https://github.com/titu1994/MLSTM-FCN/blob/master/eeg_model.py

    Args:
        config: The configuration data.

    Returns:
        Model: A model in TensorFlow.
    '''
    if isinstance(config, str):
        config = SoccerSageConfig.from_yaml(config)

    ip = Input(shape=(config.time_steps, config.feature_size))

    x = Permute((2, 1))(ip)
    # x = Masking(mask_value=config.masking_value)(x)
    x = LSTM(8)(x)
    x = Dropout(0.8)(x)

    y = Conv1D(128, 8, padding='same', kernel_initializer='he_uniform')(ip)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = squeeze_excite_block(y)

    y = Conv1D(256, 5, padding='same', kernel_initializer='he_uniform')(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = squeeze_excite_block(y)

    y = Conv1D(128, 3, padding='same', kernel_initializer='he_uniform')(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)

    y = GlobalAveragePooling1D()(y)

    x = concatenate([x, y])

    out = Dense(1, activation='linear')(x)

    classifier = Model(ip, out)

    classifier.compile(
        loss='mse',
        optimizer=tf.keras.optimizers.Adam(lr=config.learning_rate),
        metrics=['mse', 'mae'])

    if config.pretrained_classifier:
        files = SoccerSageFiles(config)
        classifier.load_weights(files.regressor_weights)

    return classifier
Exemple #7
0
def train_model(
    config: Union[str, SoccerSageConfig] = SoccerSageConfig()
) -> None:
    """Train the model and save classifier and feature weights."""
    if isinstance(config, str):
        config = SoccerSageConfig.from_yaml(config)
    files = SoccerSageFiles(config)
    # train_en = r'D:\\DeepFootball\\soccer_sage\\soccer_sage\\assets\\processed\\train(36).tfrecord'
    # test_en = r'D:\\DeepFootball\\soccer_sage\\soccer_sage\\assets\\processed\\test(36).tfrecord'
    # train_it = r'D:\\DeepFootball\\soccer_sage\\soccer_sage\\assets\\processed\\train(34).tfrecord'
    # test_it = r'D:\\DeepFootball\\soccer_sage\\soccer_sage\\assets\\processed\\test(34).tfrecord'
    # x_train_en, y_train_en = load_dataset(train_en, config)
    # x_test_en, y_test_en = load_dataset(test_en, config)
    # x_train_it, y_train_it = load_dataset(train_it, config)
    # x_test_it, y_test_it = load_dataset(test_it, config)
    #
    # x_train = tf.concat([x_train_en, x_train_it], axis=0)
    # y_train = tf.concat([y_train_en, y_train_it], axis=0)
    # x_test = tf.concat([x_test_en, x_test_it], axis=0)
    # y_test = tf.concat([y_test_en, y_test_it], axis=0)
    #
    # print(x_train_en.shape)
    # print(y_train_en.shape)
    # print(x_test_en.shape)
    # print(y_test_en.shape)
    #
    # print(x_train_it.shape)
    # print(y_train_it.shape)
    # print(x_test_it.shape)
    # print(y_test_it.shape)
    #
    # print(x_train.shape)
    # print(y_train.shape)
    # print(x_test.shape)
    # print(y_test.shape)

    x_train, y_train = load_dataset(files.train_dataset, config)
    x_test, y_test = load_dataset(files.test_dataset, config)
    model = mlstm_fcn(config)
    class_weight = {0: 1.0, 1: 1.6771, 2: 1.653}
    model.fit(
        x_train, y_train,
        validation_data=(x_test, y_test),
        verbose=config.verbose,
        batch_size=config.batch_size,
        epochs=config.n_epochs,
        steps_per_epoch=config.steps_per_epoch,
        validation_steps=config.validation_steps
    )
    model.save_weights(files.model_weights, overwrite=True)
    return 'OK'
Exemple #8
0
def train_regression_model(
    config: Union[str, SoccerSageConfig] = SoccerSageConfig()
) -> None:
    """Train the model and save classifier and feature weights."""
    if isinstance(config, str):
        config = SoccerSageConfig.from_yaml(config)
    files = SoccerSageFiles(config)
    x_train, y_train = load_dataset(files.train_dataset, config)
    x_test, y_test = load_dataset(files.test_dataset, config)
    y_train = tf.argmax(y_train, axis=1)
    y_test = tf.argmax(y_test, axis=1)
    print('EXAMPLE OUTPUT: {}'.format(y_test[0:10]), file=sys.stderr)
    model = reg_mlstm_fcn(config)
    model.fit(
        x_train, y_train,
        validation_data=(x_test, y_test),
        verbose=config.verbose,
        batch_size=config.batch_size,
        epochs=config.n_epochs,
        steps_per_epoch=config.steps_per_epoch,
        validation_steps=config.validation_steps
    )
    model.save_weights(files.regressor_weights, overwrite=True)
    return 'OK'
Exemple #9
0
def _parse_sequence_example(
        sequence_example: tf.train.SequenceExample,
        config: SoccerSageConfig = SoccerSageConfig()
) -> Tuple[tf.Tensor, tf.Tensor]:
    '''Decode a TFRecord example'''
    sequence_features = {
        'recency_features': tf.io.FixedLenSequenceFeature(
                                [SoccerSageConfig.feature_size], tf.float32)
    }
    context_features = {
        'result': tf.io.FixedLenFeature([], tf.int64)
    }
    context_parsed, sequence_parsed = tf.io.parse_single_sequence_example(
        serialized=sequence_example,
        context_features=context_features,
        sequence_features=sequence_features)
    return (sequence_parsed['recency_features'], context_parsed['result'])