-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_datautils.py
155 lines (126 loc) · 5.53 KB
/
my_datautils.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os.path
from collections import defaultdict
import tqdm, random
from torch.utils.data import Dataset, Subset
import csv, clip, torch, cn_clip
import numpy as np
import pandas as pd
from PIL import Image
from cn_clip.clip import load_from_name
## 1 fake, 0 real
class FakeNews_Dataset(Dataset):
def __init__(self, model, preprocess, data_path, img_path, dataset_name):
self.img_path = img_path
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.model = model
self.preprocess = preprocess
self.dataset_name = dataset_name
with open(data_path, 'r') as inf:
self.data = pd.read_csv(inf, header=None)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
img = self.data.iloc[idx][1] + ".jpg"
label = self.data.iloc[idx][2]
txt = self.data.iloc[idx][0]
if self.dataset_name == "weibo":
txt = cn_clip.clip.tokenize(txt).squeeze().to(self.device)
img = self.preprocess(Image.open(self.img_path + img)).to(self.device)
else:
txt = clip.tokenize(txt, truncate=True).squeeze().to(self.device)
img = self.preprocess(Image.open(self.img_path + img)).to(self.device)
label = torch.as_tensor(int(label)).to(self.device, torch.long)
return txt, img, label
class FewShotSampler_weibo:
def __init__(self, dataset, few_shot_per_class, seed):
self.dataset = dataset
self.few_shot_per_class = few_shot_per_class
self.seed = seed
def get_train_dataset(self):
indices_per_class = defaultdict(list)
for idx in range(len(self.dataset)):
_, _, label = self.dataset[idx]
indices_per_class[label.item()].append(idx)
train_indices = []
for label, indices in indices_per_class.items():
random.Random(self.seed).shuffle(indices)
train_indices.extend(indices[:self.few_shot_per_class])
train_dataset = Subset(self.dataset, train_indices)
return train_dataset
class FewShotSampler_fakenewsnet:
def __init__(self, dataset, few_shot_per_class, seed):
self.dataset = dataset
self.few_shot_per_class = few_shot_per_class
self.seed = seed
def get_train_val_datasets(self):
indices_per_class = defaultdict(list)
for idx in range(len(self.dataset)):
_, _, label = self.dataset[idx]
indices_per_class[label.item()].append(idx)
train_indices = []
val_indices = []
for label, indices in indices_per_class.items():
random.Random(self.seed).shuffle(indices)
train_indices.extend(indices[:self.few_shot_per_class])
val_indices.extend(indices[self.few_shot_per_class:])
train_dataset = Subset(self.dataset, train_indices)
val_dataset = Subset(self.dataset, val_indices)
return train_dataset, val_dataset
def sim_cal(txt_path, img_path):
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = load_from_name("ViT-B-16", device=device,
download_root='~/PycharmProjects/local_models/clip-chinese')
model.eval()
cos = torch.nn.CosineSimilarity()
all_txt, all_img, all_label = [], [], []
with open(txt_path, 'r') as inf:
content = inf.readlines()
for i in tqdm.tqdm(range(0, len(content), 3)):
three_lines = content[i:i + 3]
if len(three_lines[2].strip()) != 0: # remove empty text
urls = three_lines[1].split("|")
txt = cn_clip.clip.tokenize(three_lines[2].strip()).to(device)
txt_emb = model.encode_text(txt)
img_final = None
tmp_sim = 0
for item in urls:
if item != 'null\n':
url = item.split("/")[-1]
if os.path.exists(img_path + url):
img = preprocess(Image.open(img_path + url)).unsqueeze(0).to(device)
img_emb = model.encode_image(img)
sim = cos(img_emb, txt_emb).cpu().detach().numpy()
if sim >= tmp_sim:
tmp_sim = sim
img_final = url
if img_final is not None:
img_final = img_final.split(".")[0]
all_img.append(img_final)
all_txt.append(three_lines[2].strip())
if img_path.split("/")[-2] == "rumor_images":
all_label.append("1")
else:
all_label.append("0")
return all_txt, all_img, all_label
def weibo_2_csv(fake_txt, fake_img, real_txt, real_img, out_path):
ftxt, fimg, flabel = sim_cal(fake_txt, fake_img)
rtxt, rimg, rlabel = sim_cal(real_txt, real_img)
txt = ftxt + rtxt
img = fimg + rimg
label = flabel + rlabel
with open(out_path, 'w', newline='') as outf:
csv_writer = csv.writer(outf)
for l in range(len(label)):
csv_writer.writerow([txt[l], img[l], label[l]])
def load_from_csv(csv_path, img_path):
all_txt, all_img, all_label = [], [], []
with open(csv_path, 'r') as inf:
data = csv.reader(inf)
for line in data:
img = line[2] + ".jpg"
label = line[3]
txt = line[1]
all_img.append(img_path + img)
all_txt.append(txt)
all_label.append(label)
return all_txt, all_img, all_label