-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
211 lines (169 loc) · 5.88 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from collections import defaultdict, Counter
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import keras.backend as K
import os
import re
import numpy as np
import datetime
import pickle
import cv2
import urllib.request
import requests
import json
from const import BASE_DIR
try:
from google.colab.patches import cv2_imshow
except:
from cv2 import imshow as cv2_imshow
from PIL import Image
DS_SAVE_DIR = BASE_DIR + '/dataset/save'
MEAN_PIXCELS = np.array([103.939, 116.779, 123.68])
decomposers = {
'pca': PCA(),
'tsne': TSNE()
}
def save_image_array(img_array, fname=None, show=None):
# convert 1 channel to 3 channels
channels = img_array.shape[-1]
resolution = img_array.shape[2]
img_rows = img_array.shape[0]
img_cols = img_array.shape[1]
img = np.full([resolution * img_rows, resolution * img_cols, channels], 0.0)
for r in range(img_rows):
for c in range(img_cols):
img[
(resolution * r): (resolution * (r + 1)),
(resolution * (c % 10)): (resolution * ((c % 10) + 1)),
:] = img_array[r, c]
img = denormalize(img).astype(np.uint8)
if show:
try:
cv2_imshow(img)
except Exception as e:
fname = BASE_DIR + '/result/model_{}/img_{}.png'.format(
resolution,
datetime.datetime.now().strftime("%m/%d/%Y-%H%M%S")
)
print('[show fail] ', str(e))
if fname:
try:
Image.fromarray(img).save(fname)
except Exception as e:
print('Save image failed', str(e))
def show_samples(img_array):
shape = img_array.shape
img_samples = img_array.reshape(
(-1, shape[-4], shape[-3], shape[-2], shape[-1])
)
save_image_array(img_samples, None, True)
def triple_channels(image):
# axis = 2 for single image, 3 for many images
if image.shape[-1] == 3:
return image
return np.repeat(image, 3, axis = -1)
def pickle_save(object, path):
try:
print('save data to {} successfully'.format(path))
with open(path, "wb") as f:
return pickle.dump(object, f)
except:
print('save data to {} failed'.format(path))
def http_get_img(url, rst=64, gray=False):
req = urllib.request.urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1)
img = cv2.resize(img, (rst, rst))
if gray:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return normalize(img.reshape((1, rst, rst, -1)))
def pickle_load(path):
try:
print("Loading data from {}".format(path))
with open(path, "rb") as f:
data = pickle.load(f)
print('load data successfully'.format(path))
return data
except Exception as e:
print(str(e))
return None
def visualize_scatter_with_images(X_2d_data, images, figsize=(10,10), image_zoom=0.5):
fig, ax = plt.subplots(figsize=figsize)
artists = []
for xy, i in zip(X_2d_data, images):
x0, y0 = xy
img = OffsetImage(i, zoom=image_zoom)
ab = AnnotationBbox(img, (x0, y0), xycoords='data', frameon=False)
artists.append(ax.add_artist(ab))
ax.update_datalim(X_2d_data)
ax.autoscale()
plt.show()
def visualize_scatter(data_2d, label_ids, figsize=(8,8), legend=True,title="None"):
plt.figure(figsize=figsize)
plt.grid()
nb_classes = len(np.unique(label_ids))
colors = cm.rainbow(np.linspace(0, 1, nb_classes))
for i,label_id in enumerate(np.unique(label_ids)):
plt.scatter(data_2d[np.where(label_ids == label_id), 0],
data_2d[np.where(label_ids == label_id), 1],
marker='o',
color=colors[i],
linewidth='1',
alpha=0.8,
label=label_id)
if legend:
plt.legend(loc='best')
else:
# plt.title(title)
plt.axis('off')
plt.show()
def scatter_plot(x, y, encoder, name='chart', opt='pca', plot_img=None,
legend=True, title="None"):
step = 1
if encoder.input_shape[-1] != x.shape[-1]:
x = triple_channels(x)
x_embeddings = encoder.predict(x)
if len(x_embeddings.shape) > 2:
x_embeddings = x_embeddings.reshape(x_embeddings.shape[0], -1)
decomposed_embeddings = decomposers[opt].fit_transform(x_embeddings)
if plot_img:
return visualize_scatter_with_images(decomposed_embeddings,x)
visualize_scatter(decomposed_embeddings, y, legend=legend,title=title)
def prune(x, y, prune_classes):
"""
prune data by give classes
"""
for class_to_prune in range(len(prune_classes)):
remove_size = prune_classes[class_to_prune]
if remove_size <= 0:
continue
print(class_to_prune)
all_ids = list(np.arange(len(x)))
mask = [lc == class_to_prune for lc in y]
all_ids_c = np.array(all_ids)[mask]
np.random.shuffle(all_ids_c)
to_delete = all_ids_c[:remove_size]
x = np.delete(x, to_delete, axis=0)
y = np.delete(y, to_delete, axis=0)
print('Remove {} items in class {}'.format(remove_size, class_to_prune))
return x, y
def preprocess(imgs):
"""
BGR -> RBG then subtract the mean
"""
return imgs - MEAN_PIXCELS
def deprocess(imgs):
return imgs + MEAN_PIXCELS
def normalize(imgs):
return ((imgs) - 127.5) / 127.5
def denormalize(imgs):
return (imgs * 127.5 + 127.5)
def load_chestxray14_data(rst=128):
return pickle_load(os.path.join(
BASE_DIR,
'dataset/multi_chest/imgs_labels_{}.pkl'.format(rst)
))