forked from hrlee113/gameplaylist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
88 lines (72 loc) · 1.98 KB
/
utils.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
from PIL import Image
import pickle
import numpy as np
from sklearn.metrics import roc_auc_score, f1_score
import torch
from torch.utils.data import Dataset
'''
Metric
'''
def roc_auc_compute_fn(y_pred, y_true):
return roc_auc_score(y_true, y_pred)
def f1_score_compute_fn(y_pred, y_true):
return f1_score(y_true, y_pred)
'''
Pickle
'''
def load_pickle(filename):
with open(filename, 'rb') as f:
res = pickle.load(f)
return res
def save_pickle(data, filename):
with open(filename, 'wb') as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
'''
Embedding
'''
def get_labels_images(game):
# 이미지 데이터 로드
image_dict = {}
for i in range(5):
image = load_pickle('image_data_dict_v2_{}.pickle'.format(i+1))
image_dict.update(image)
# 게임 고유번호 - 이미지 매칭
labels=[]; images=[]
for i in range(len(game)):
game_id = game.loc[i, 'game_id']
image = image_dict[game_id]
image = Image.fromarray(image)
image = image.resize((28, 28)).convert('RGB')
image = np.array(image)
image = image.reshape(1, 28, 28, 3)
image = np.swapaxes(image, 1, 3)
labels.append(game_id)
images.append(image)
labels = np.array(labels); images = np.concatenate(images)
return labels, images
class CustomDataset(Dataset):
# 데이터 정의
def __init__(self, x_data, y_data = None):
self.x_data = x_data
self.y_data = y_data.reshape(-1,1)
# 총 데이터 개수
def __len__(self):
return len(self.x_data)
# 인덱싱
def __getitem__(self, idx):
if self.y_data is None:
x = torch.FloatTensor(self.x_data[idx])
return x
else:
x = torch.FloatTensor(self.x_data[idx])
y = torch.LongTensor(self.y_data[idx])[0]
return x, y
'''
Modeling
'''
def torch_device():
if torch.cuda.is_available():
DEVICE = torch.device('cuda')
else:
DEVICE = torch.device('cpu')
return DEVICE