-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPreprocessing.py
More file actions
122 lines (91 loc) · 3.83 KB
/
Copy pathPreprocessing.py
File metadata and controls
122 lines (91 loc) · 3.83 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
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
#import tensorflow.keras.datasets as datasets
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
def save_expanded():
#Load directly from cifar10
training_set, testing_set = datasets.cifar10.load_data()
x_train, _ = training_set
x_test, _ = testing_set
x_train_new = []
x_test_new = []
#Resize from 32x32 to 150x150, interpolation for enhanced resolution
for img in (x_train):
newImage = cv2.resize(img, dsize = (50, 50), interpolation = cv2.INTER_CUBIC)
newImage = (newImage-np.min(newImage))/(np.max(newImage) - np.min(newImage)) #Normalize
x_train_new.append(newImage)
x_train_new = np.array(x_train_new)
for img in (x_test):
newImage = cv2.resize(img, dsize = (50, 50), interpolation = cv2.INTER_CUBIC)
newImage = (newImage-np.min(newImage))/(np.max(newImage) - np.min(newImage))
x_test_new.append(newImage)
x_test_new = np.array(x_test_new)
#Demo plot of resized image
plt.imshow(x_train_new[0])
#Save new image arrays
np.save("expandedTrain.npy", x_train_new)
np.save("expandedTest.npy", x_test_new)
def convert_format(path, label):
#Reads all the images in a given file directory, and preprocesses them into use for the network
files = os.listdir(path)
imgList = []
labelList = []
print("{} and {}".format(path, files[0]))
for i,file in enumerate(files):
img = cv2.imread(os.path.join(path, file), 1)
img = cv2.resize(img, dsize = (50, 50), interpolation = cv2.INTER_CUBIC)
imgList.append(img)
labelList.append(label)
return imgList, labelList
def encode_oneHot(labels, idx):
encoded_labels = []
for label in labels:
zero_vec = np.zeros((idx))
zero_vec[label] = 1
encoded_labels.append(zero_vec)
return np.array(encoded_labels)
def load_embeddings(path, wanted_embs, zero_shot_classes, save_embed = False):
"""
path: Path of word embeddings
wanted_embs: String list of training classes of zero shot model
zero_shot_classes: String list of all classes in zero shot model
"""
total_classes = wanted_embs+zero_shot_classes
embed_file = open(path, 'r')
text = embed_file.read()
word_sep = text.split('\n')
#Last element is blank
word_sep.pop()
word_dict = {}
for word_part in word_sep:
temp_array = word_part.split()
if temp_array[0] in total_classes:
embeddingList = list(map(float, temp_array[1:]))
word_dict[temp_array[0]] = embeddingList
if save_embed:
embed_matrix = np.array([], dtype = np.float32).reshape(0, 100)
for word in wanted_embs:
embed_matrix = np.vstack([embed_matrix, np.array(word_dict[word])])
np.save("embedding_matrix.npy", embed_matrix)
return word_dict
def closest_vectors(path, train_classes, embed_dict):
embed_file = open(path, 'r')
text = embed_file.read()
word_sep = text.split('\n')
#Last element is blank
word_sep.pop()
distance_array = []
for tclass in train_classes:
for word_part in word_sep:
temp_array = word_part.split()
distance_array.append(np.linalg.norm(np.array(embed_dict[tclass]) - np.array(temp_array[1:]).astype(np.float64)))
closest_vectors = []
for _ in range(20):
smallest_idx = np.argmin(np.array(distance_array))
closest_vectors.append(smallest_idx)
distance_array[smallest_idx] = float('inf')
closest_vectors = ' '.join([word_sep[i].split()[0] for i in closest_vectors])
print("{}'s closest 20 vectors are:{}".format(tclass, closest_vectors))
closest_vectors = []
distance_array = []