|
|
|
|
|
|
|
|
import random |
|
|
import random |
|
|
|
|
|
|
|
|
import numpy as np |
|
|
import numpy as np |
|
|
|
|
|
import pandas as pd |
|
|
import torch |
|
|
import torch |
|
|
from tqdm import tqdm |
|
|
from tqdm import tqdm |
|
|
|
|
|
|
|
|
from data_loaders import make_dfs, build_loaders |
|
|
from data_loaders import make_dfs, build_loaders |
|
|
from evaluation import metrics, report_per_class, roc_auc_plot, precision_recall_plot, plot_tsne, plot_pca |
|
|
|
|
|
|
|
|
from evaluation import metrics, report_per_class, roc_auc_plot, precision_recall_plot, plot_tsne, plot_pca, \ |
|
|
|
|
|
save_embedding |
|
|
from learner import batch_constructor |
|
|
from learner import batch_constructor |
|
|
from model import FakeNewsModel |
|
|
|
|
|
|
|
|
from model import FakeNewsModel, calculate_loss |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test(config, test_loader, trial_number=None): |
|
|
def test(config, test_loader, trial_number=None): |
|
|
|
|
|
|
|
|
targets = [] |
|
|
targets = [] |
|
|
predictions = [] |
|
|
predictions = [] |
|
|
scores = [] |
|
|
scores = [] |
|
|
|
|
|
ids = [] |
|
|
|
|
|
losses = [] |
|
|
tqdm_object = tqdm(test_loader, total=len(test_loader)) |
|
|
tqdm_object = tqdm(test_loader, total=len(test_loader)) |
|
|
for i, batch in enumerate(tqdm_object): |
|
|
for i, batch in enumerate(tqdm_object): |
|
|
batch = batch_constructor(config, batch) |
|
|
batch = batch_constructor(config, batch) |
|
|
with torch.no_grad(): |
|
|
with torch.no_grad(): |
|
|
output, score = model(batch) |
|
|
output, score = model(batch) |
|
|
|
|
|
|
|
|
prediction = output.detach() |
|
|
|
|
|
predictions.append(prediction) |
|
|
|
|
|
|
|
|
|
|
|
score = score.detach() |
|
|
|
|
|
scores.append(score) |
|
|
|
|
|
|
|
|
|
|
|
target = batch['label'].detach() |
|
|
|
|
|
targets.append(target) |
|
|
|
|
|
|
|
|
|
|
|
image_feature = model.image_embeddings.detach() |
|
|
|
|
|
image_features.append(image_feature) |
|
|
|
|
|
|
|
|
|
|
|
text_feature = model.text_embeddings.detach() |
|
|
|
|
|
text_features.append(text_feature) |
|
|
|
|
|
|
|
|
|
|
|
multimodal_feature = model.multimodal_embeddings.detach() |
|
|
|
|
|
multimodal_features.append(multimodal_feature) |
|
|
|
|
|
|
|
|
|
|
|
concat_feature = model.classifier.embeddings.detach() |
|
|
|
|
|
concat_features.append(concat_feature) |
|
|
|
|
|
|
|
|
|
|
|
# config.writer.add_graph(model, input_to_model=batch, verbose=True) |
|
|
|
|
|
|
|
|
loss, c_loss, s_loss = calculate_loss(model, score, batch['label']) |
|
|
|
|
|
|
|
|
|
|
|
predictions.append(output.detach()) |
|
|
|
|
|
scores.append(score.detach()) |
|
|
|
|
|
targets.append(batch['label'].detach()) |
|
|
|
|
|
ids.append(batch['id'].detach()) |
|
|
|
|
|
image_features.append(model.image_embeddings.detach()) |
|
|
|
|
|
text_features.append(model.text_embeddings.detach()) |
|
|
|
|
|
multimodal_features.append(model.multimodal_embeddings.detach()) |
|
|
|
|
|
concat_features.append(model.classifier.embeddings.detach()) |
|
|
|
|
|
losses.append((loss.detach(), c_loss.detach(), s_loss.detach())) |
|
|
|
|
|
|
|
|
s = '' |
|
|
s = '' |
|
|
s += report_per_class(targets, predictions) + '\n' |
|
|
s += report_per_class(targets, predictions) + '\n' |
|
|
s += metrics(targets, predictions, scores, file_path=str(config.output_path) + '/fpr_tpr.csv') + '\n' |
|
|
|
|
|
with open(config.output_path + '/results.txt', 'w') as f: |
|
|
|
|
|
|
|
|
s += metrics(targets, predictions, scores, file_path=str(config.output_path) + '/new_fpr_tpr.csv') + '\n' |
|
|
|
|
|
with open(config.output_path + '/new_results.txt', 'w') as f: |
|
|
f.write(s) |
|
|
f.write(s) |
|
|
|
|
|
|
|
|
roc_auc_plot(targets, scores, fname=str(config.output_path) + "/roc.png") |
|
|
roc_auc_plot(targets, scores, fname=str(config.output_path) + "/roc.png") |
|
|
precision_recall_plot(targets, scores, fname=str(config.output_path) + "/pr.png") |
|
|
precision_recall_plot(targets, scores, fname=str(config.output_path) + "/pr.png") |
|
|
|
|
|
|
|
|
# saving_in_tensorboard(config, image_features, targets, 'image_features') |
|
|
|
|
|
plot_tsne(config, image_features, targets, fname=str(config.output_path) + '/image_features_tsne.png') |
|
|
|
|
|
plot_pca(config, image_features, targets, fname=str(config.output_path) + '/image_features_pca.png') |
|
|
|
|
|
|
|
|
|
|
|
# saving_in_tensorboard(config, text_features, targets, 'text_features') |
|
|
|
|
|
plot_tsne(config, text_features, targets, fname=str(config.output_path) + '/text_features_tsne.png') |
|
|
|
|
|
plot_pca(config, text_features, targets, fname=str(config.output_path) + '/text_features_pca.png') |
|
|
|
|
|
# |
|
|
|
|
|
# saving_in_tensorboard(config, multimodal_features, targets, 'multimodal_features') |
|
|
|
|
|
plot_tsne(config, multimodal_features, targets, fname=str(config.output_path) + '/multimodal_features_tsne.png') |
|
|
|
|
|
plot_pca(config, multimodal_features, targets, fname=str(config.output_path) + '/multimodal_features_pca.png') |
|
|
|
|
|
|
|
|
|
|
|
# saving_in_tensorboard(config, concat_features, targets, 'concat_features') |
|
|
|
|
|
plot_tsne(config, concat_features, targets, fname=str(config.output_path) + '/concat_features_tsne.png') |
|
|
|
|
|
plot_pca(config, concat_features, targets, fname=str(config.output_path) + '/concat_features_pca.png') |
|
|
|
|
|
|
|
|
save_embedding(config, image_features, fname=str(config.output_path) + '/new_image_features.tsv') |
|
|
|
|
|
save_embedding(config, text_features, fname=str(config.output_path) + '/new_text_features.tsv') |
|
|
|
|
|
save_embedding(config, multimodal_features, fname=str(config.output_path) + '/new_multimodal_features_.tsv') |
|
|
|
|
|
save_embedding(config, concat_features, fname=str(config.output_path) + '/new_concat_features.tsv') |
|
|
|
|
|
|
|
|
config_parameters = str(config) |
|
|
config_parameters = str(config) |
|
|
with open(config.output_path + '/parameters.txt', 'w') as f: |
|
|
|
|
|
|
|
|
with open(config.output_path + '/new_parameters.txt', 'w') as f: |
|
|
f.write(config_parameters) |
|
|
f.write(config_parameters) |
|
|
print(config) |
|
|
print(config) |
|
|
|
|
|
|
|
|
|
|
|
pd.DataFrame({'id': ids, 'predicted_label': predictions, 'real_label': targets, 'losses': losses}).to_csv( |
|
|
|
|
|
str(config.output_path) + '/new_text_label.csv') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_main(config, trial_number=None): |
|
|
def test_main(config, trial_number=None): |
|
|
train_df, test_df, validation_df = make_dfs(config, ) |
|
|
train_df, test_df, validation_df = make_dfs(config, ) |