-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
128 lines (101 loc) · 3.61 KB
/
misc.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
import numpy as np
import cv2
import heapq
import pandas as pd
from utils.coco.pycocoevalcap.classify import Classify
from config import Config
from data_generation import summary
class ImageLoader(object):
def __init__(self):
self.config = Config()
self.bgr = True
self.scale_shape = np.array([224, 224], np.int32)
self.crop_shape = np.array([224, 224], np.int32)
#self.mean = np.load(mean_file).mean(1).mean(1)
def load_image(self, image_file):
""" Load and preprocess an image. """
image = cv2.imread(self.config.img_dir+image_file[1:])
if self.bgr:
temp = image.swapaxes(0, 2)
temp = temp[::-1]
image = temp.swapaxes(0, 2)
image = cv2.resize(image, (self.scale_shape[0], self.scale_shape[1]))
return image
def load_images(self, image_files):
""" Load and preprocess a list of images. """
images = []
for image_file in image_files:
images.append(self.load_image(image_file))
images = np.array(images, np.float32)
return images
class CaptionData(object):
def __init__(self, sentence, memory, output, score):
self.sentence = sentence
self.memory = memory
self.output = output
self.score = score
def __cmp__(self, other):
assert isinstance(other, CaptionData)
if self.score == other.score:
return 0
elif self.score < other.score:
return -1
else:
return 1
def __lt__(self, other):
assert isinstance(other, CaptionData)
return self.score < other.score
def __eq__(self, other):
assert isinstance(other, CaptionData)
return self.score == other.score
class TopN(object):
def __init__(self, n):
self._n = n
self._data = []
def size(self):
assert self._data is not None
return len(self._data)
def push(self, x):
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
def extract(self, sort=False):
assert self._data is not None
data = self._data
self._data = None
if sort:
data.sort(reverse=True)
return data
def reset(self):
self._data = []
class EmotionLoader(object):
def __init__(self):
self.config = Config()
self.classify = Classify(self.config.emotion_file, self.config.action_unit_file, self.config.intensity_file)
def load_emotions(self, emotion_files):
onehot_emo = self.classify.one_hot(self.classify.emotion)
emotions = []
for emotion_file in emotion_files:
emotion = open(self.config.img_dir+emotion_file[1:], "r")
emotion = emotion.read()
emotion = np.float(emotion[:-5])
emotion = np.int(emotion)
emotion = onehot_emo[emotion-1]
emotion = emotion.tolist()
emotions.append(emotion)
return emotions
def load_data(self, data_files):
onehot_emo = self.classify.one_hot(self.classify.emotion)
emotions = []
for data_file in data_files:
if self.config.data == 'CK+' or not self.config.server:
data = pd.read_csv(data_file[:])
else:
server_dir = self.config.img_dir
data = pd.read_csv(server_dir+data_file[1:])
emotion = (data['Emotion'].tolist())[0]
emotion = onehot_emo[emotion-1]
emotions.append(emotion)
return emotions