def calculate_FG_2020_F1_and_accuracy_scores_with_extended_predictions(self, instances, path_to_video, path_to_real_labels, original_sample_rate, delete_value=-1):
        dict_filename_to_predictions = transform_probabilities_to_original_sample_rate(database_instances=instances,
                                                                                       path_to_video=path_to_video,
                                                                                       original_sample_rate=original_sample_rate,
                                                                                       need_save=False)
        real_filenames = os.listdir(path_to_real_labels)
        total_predictions = pd.DataFrame()
        total_labels = pd.DataFrame()
        for real_labels_filename in real_filenames:
            predictions_filename = real_labels_filename.split('.')[0] + '.csv'
            predictions = dict_filename_to_predictions[predictions_filename]
            if total_predictions.shape[0] == 0:
                total_predictions = predictions
            else:
                total_predictions = total_predictions.append(predictions)

            real_labels = pd.read_csv(path_to_real_labels + real_labels_filename, header=None)
            if total_labels.shape[0] == 0:
                total_labels = real_labels
            else:
                total_labels = total_labels.append(real_labels)

        total_predictions = np.argmax(total_predictions.values, axis=-1).reshape((-1, 1))
        mask = total_labels != delete_value
        total_labels = total_labels.values[mask]
        total_predictions = total_predictions[mask]

        return 0.67 * f1_score(total_labels, total_predictions, average='macro') + 0.33 * accuracy_score(total_labels,total_predictions), \
               f1_score(total_labels, total_predictions, average='macro'), \
               accuracy_score(total_labels, total_predictions)
    def calculate_FG_2020_CCC_score_with_extended_predictions(instances, path_to_video, path_to_real_labels, original_sample_rate, delete_value=-5):

        dict_filename_to_predictions = transform_probabilities_to_original_sample_rate(database_instances=instances,
                                                                                       path_to_video=path_to_video,
                                                                                       original_sample_rate=original_sample_rate,
                                                                                       need_save=False,
                                                                                       labels_type='regression')
        real_filenames = os.listdir(path_to_real_labels)
        total_predictions = pd.DataFrame()
        total_labels = pd.DataFrame()
        for real_labels_filename in real_filenames:
            predictions_filename = real_labels_filename.split('.')[0] + '.csv'
            predictions = dict_filename_to_predictions[predictions_filename]
            if total_predictions.shape[0] == 0:
                total_predictions = predictions
            else:
                total_predictions = total_predictions.append(predictions)

            real_labels = pd.read_csv(path_to_real_labels + real_labels_filename, header=None)
            if total_labels.shape[0] == 0:
                total_labels = real_labels
            else:
                total_labels = total_labels.append(real_labels)

        mask = (total_labels.values != delete_value).all(axis=-1)
        total_labels = total_labels.values[mask]
        total_predictions = total_predictions.values[mask]

        result=np.zeros(shape=(total_labels.shape[-1],))
        for i in range(total_labels.shape[-1]):
            result[i]=CCC_2_sequences_numpy(total_labels[...,i], total_predictions[...,i])
        return result
Exemple #3
0
def generate_test_predictions(path_to_data, labels_filename, model, model_output_sample_rate, path_to_video, window_size, window_step, prediction_mode):

    instance = Database_instance()
    instance.loading_data_function = load_data_wav
    instance.load_data(path_to_data.split('_left')[0].split('_right')[0].split('_vocals')[0].split('.')[0] +'_vocals.'+path_to_data.split('.')[-1])
    instance.label_filename = labels_filename
    instance.labels, instance.labels_frame_rate = np.array([[0],[0]]), model_output_sample_rate
    instance.align_number_of_labels_and_data()
    instance.generate_timesteps_for_labels()
    instance.cut_data_and_labels_on_windows(window_size, window_step)
    predict_data_with_the_model(model, [instance], prediction_mode=prediction_mode)
    dict_filename_to_predictions = transform_probabilities_to_original_sample_rate(
        database_instances=[instance],
        path_to_video=path_to_video,
        original_sample_rate=model_output_sample_rate,
        need_save=False)
    return dict_filename_to_predictions
    if prediction_mode == 'sequence_to_sequence':
        model.compile(optimizer=optimizer,
                      loss=loss,
                      sample_weight_mode="temporal")
    else:
        model.compile(optimizer=optimizer, loss=loss)

    generate_predictions(validation_database, model, need_save=False)
    if not os.path.exists(path_to_save_predictions):
        os.mkdir(path_to_save_predictions)
    path_to_aligned_extended_predictions = path_to_save_predictions + 'prediction_probabilities_extended_aligned\\'
    path_to_aligned_labels = 'D:\\Databases\\AffWild2\\Annotations\\EXPR_Set\\train\\Aligned_labels\\'

    dict_filename_to_predictions = transform_probabilities_to_original_sample_rate(
        database_instances=validation_database.data_instances,
        path_to_video=path_to_video,
        original_sample_rate=original_sample_rate,
        need_save=True,
        path_to_output=path_to_aligned_extended_predictions)
    '''# calculate score for extended predictions
    real_filenames=os.listdir(path_to_aligned_labels)
    total_predictions=pd.DataFrame()
    total_labels=pd.DataFrame()
    for real_labels_filename in real_filenames:
        predictions_filename=real_labels_filename.split('.')[0]+'.csv'
        predictions=dict_filename_to_predictions[predictions_filename]
        if total_predictions.shape[0]==0:
            total_predictions=predictions
        else:
            total_predictions=total_predictions.append(predictions)

        real_labels=pd.read_csv(path_to_aligned_labels+real_labels_filename, header=None)