-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04_test_detector.py
More file actions
75 lines (63 loc) · 2.92 KB
/
Copy path04_test_detector.py
File metadata and controls
75 lines (63 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
DATAPATH = "./multitude.csv" #path of the dataset for training
MODELPATH = "./finetuned_models/" #path where fine-tuned model will be saved
CACHE = "./cache/" #change to your huggingface cache folder (where the pretrained models will be downloaded)
import sys
PRE_TRAINED_MODEL_NAME = sys.argv[1]
model_name = PRE_TRAINED_MODEL_NAME.split('/')[-1]
dataset = sys.argv[2] #'en', 'es', 'ru', 'all', 'en3'
generative_model = sys.argv[3] #'text-davinci-003', 'gpt-3.5-turbo', 'gpt-4', 'llama-65b', 'opt-66b', 'opt-iml-max-1.3b', 'all'
output_model = f'{MODELPATH}{model_name}-finetuned-{dataset}-{generative_model}'
balance = False
if balance:
output_model = f'{MODELPATH}{model_name}-finetuned-{dataset}-{generative_model}-balanced'
import os
os.environ['HF_HOME'] = CACHE
import pandas as pd
from sklearn.metrics import classification_report
from transformers import pipeline
import numpy as np
import torch
import gc
import nvidia_smi, psutil, shutil
import time
from tqdm import tqdm
RANDOM_SEED = 42
np.random.seed(RANDOM_SEED)
torch.manual_seed(RANDOM_SEED)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
torch.cuda.empty_cache()
def report_gpu():
nvidia_smi.nvmlInit()
handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)
info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
print("GPU [GB]:", f'{info.used/1024/1024/1024:.2f}', "/", f'{info.total/1024/1024/1024:.1f}')
nvidia_smi.nvmlShutdown()
print('RAM [GB]:', f'{psutil.virtual_memory()[3]/1024/1024/1024:.2f}', "/", f'{psutil.virtual_memory()[0]/1024/1024/1024:.1f}')
start = time.time()
classifier = pipeline("text-classification", model=output_model, device=device, torch_dtype=torch.float16)
end = time.time()
print(f"{output_model.split('/')[-1]} loading took {(end - start)/60} min")
print(f"{output_model.split('/')[-1]} memory footprint {classifier.model.get_memory_footprint()/1024/1024/1024} GB")
report_gpu()
def predict(df):
preds = ['unknown'] * len(df)
scores = [0] * len(df)
for index, row in tqdm(df.iterrows(), total=len(df)):
tokenizer_kwargs = {'truncation':True,'max_length':512}
pred = classifier(row['text'], **tokenizer_kwargs)
preds[index] = pred[0]['label']
scores[index] = pred[0]['score']
return preds, scores
test = pd.read_csv(DATAPATH)
test = test[test.split == "test"].reset_index(drop=True)
test['label'] = ["human" if "human" in x else "machine" for x in test.multi_label]
start = time.time()
preds = predict(test)
test['predictions'] = preds[0]
test['prediction_probs'] = preds[1]
end = time.time()
print(f"{output_model.split('/')[-1]} testing took {(end - start)/60} min")
print(f"{output_model.split('/')[-1]} memory footprint {classifier.model.get_memory_footprint()/1024/1024/1024} GB")
report_gpu()
test.to_csv(f"{DATAPATH.replace('multitude.csv','results/finetuned/')}{output_model.split('/')[-1]}.csv.gz", compression='gzip', index=False)
print(classification_report(test['label'], test['predictions'], digits=4))