-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
163 lines (120 loc) · 6.04 KB
/
dataset.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
152
153
154
155
156
157
158
159
160
161
162
163
import os
import numpy as np
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader
import pytorch_lightning as pl
# NLP ------------------------------------------------------------------------------------------------------------------
class Intent_Classification_Dataset(Dataset):
def __init__(self, df, tokenizer, intent_label_vocab, max_seq_len):
self.tokenizer = tokenizer
self.intent_label_vocab = intent_label_vocab
self.max_seq_len = max_seq_len
# for debugging -- to smallset
# N = 70
# df = df[:N]
# transform all data
from tqdm.auto import tqdm
df_iterator = tqdm(df.iterrows(), desc="Iteration")
self.texts = []
self.intents = []
for row_idx, (index, row) in enumerate(df_iterator):
text_objs = self.proc_text(str(row['text']), self.tokenizer)
intent_id = self.proc_intent_text(row['intent_text'])
self.texts.append(text_objs)
self.intents.append(intent_id)
def proc_text(self, text, tokenizer):
obj = tokenizer(text, padding='max_length', max_length=self.max_seq_len, truncation=True)
if 'token_type_ids' not in obj:
obj['token_type_ids'] = [0] * len(obj['input_ids'])
return obj
def proc_intent_text(self, intent_text):
intent_id = self.intent_label_vocab[intent_text]
return intent_id
def __getitem__(self, i):
input_ids = np.array(self.texts[i]['input_ids'])
token_type_ids = np.array(self.texts[i]['token_type_ids'])
attention_mask = np.array(self.texts[i]['attention_mask'])
intent_ids = np.array(self.intents[i])
item = [input_ids, token_type_ids, attention_mask, intent_ids]
return item
def __len__(self):
return (len(self.texts))
class Intent_Classification_Data_Module(pl.LightningDataModule):
def __init__(self, domain, text_reader, max_seq_length, batch_size):
super().__init__()
# prepare tokenizer
from utils import get_tokenizer
self.tokenizer = get_tokenizer(domain, text_reader)
# data preparing params
self.data_dir = os.path.join("./data", domain, "run")
self.max_seq_length = max_seq_length
self.batch_size = batch_size
# number of intents for determining model's last dimension
self.num_intents = None
def prepare_data(self):
# vocab
intent_label_vocab = self._load_vocab(os.path.join(self.data_dir, "intent.vocab"))
self.num_intents = len(intent_label_vocab)
# read data
train_df = pd.read_csv(os.path.join(self.data_dir, "train.nlu.tsv"), sep='\t')
valid_df = pd.read_csv(os.path.join(self.data_dir, "dev.nlu.tsv"), sep='\t')
test_df = pd.read_csv(os.path.join(self.data_dir, "test.nlu.tsv"), sep='\t')
# building dataset
self.train_dataset = Intent_Classification_Dataset(train_df, self.tokenizer, intent_label_vocab, self.max_seq_length)
self.valid_dataset = Intent_Classification_Dataset(valid_df, self.tokenizer, intent_label_vocab, self.max_seq_length)
self.test_dataset = Intent_Classification_Dataset(test_df, self.tokenizer, intent_label_vocab, self.max_seq_length)
def _load_vocab(self, fn):
print("Vocab loading from {}".format(fn))
vocab = {}
with open(fn, 'r', encoding='utf-8') as f:
for line in f:
line = line.rstrip()
symbol, _id = line.split('\t')
vocab[symbol] = int(_id)
return vocab
def train_dataloader(self):
return DataLoader(self.train_dataset, batch_size=self.batch_size, shuffle=True)
def val_dataloader(self):
return DataLoader(self.valid_dataset, batch_size=self.batch_size)
def test_dataloader(self):
return DataLoader(self.test_dataset, batch_size=self.batch_size)
def train_dataloader_for_dump(self):
return DataLoader(self.train_dataset, batch_size=1) # fixed batch_size=1
def test_dataloader_for_dump(self):
return DataLoader(self.test_dataset, batch_size=1) # fixed batch_size=1
# ----------------------------------------------------------------------------------------------------------------------
# Vision ---------------------------------------------------------------------------------------------------------------
import torchvision.transforms as transforms
from torchvision.datasets import ImageFolder
class Image_Intent_Classification_Data_Module(pl.LightningDataModule):
def __init__(self, data_dir, batch_size):
super().__init__()
# data preparing params
self.data_dir = data_dir
self.batch_size = batch_size
# number of intents for determining model's last dimension
self.num_intents = None
# transform -- to tensor
self.transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor(),
])
def prepare_data(self):
train_data_folder = os.path.join(self.data_dir, "train")
train_dataset = ImageFolder(train_data_folder, transform=self.transform)
valid_data_folder = os.path.join(self.data_dir, "test") # using test-set at validation
valid_dataset = ImageFolder(valid_data_folder, transform=self.transform)
test_data_folder = os.path.join(self.data_dir, "test")
test_dataset = ImageFolder(test_data_folder, transform=self.transform)
self.train_dataset = train_dataset
self.valid_dataset = valid_dataset
self.test_dataset = test_dataset
self.num_intents = len(train_dataset.classes)
def train_dataloader(self):
return DataLoader(self.train_dataset, batch_size=self.batch_size, shuffle=True)
def val_dataloader(self):
return DataLoader(self.valid_dataset, batch_size=self.batch_size)
def test_dataloader(self):
return DataLoader(self.test_dataset, batch_size=self.batch_size)
# ----------------------------------------------------------------------------------------------------------------------