-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscatter_car196.py
160 lines (112 loc) · 3.84 KB
/
scatter_car196.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
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from matplotlib.cbook import get_sample_data
from sklearn.manifold import TSNE
# FOR CUB200-2011
print("Loading data")
data = np.load('car196_fea_label_path.npy')
data = data.item()
feature_set = data.get('feature')
label_set = data.get('label')
path_set = data.get('path')
"""
print("processing TSNE")
model = TSNE(random_state=22222)
fea_reduce = model.fit_transform(feature_set)
np.save('car196_tsne_fea_cub.npy', fea_reduce)
"""
fea_reduce = np.load('car196_tsne_fea_cub.npy')
print("FINISH TSNE")
x_min = np.min(fea_reduce[:,0])
x_max = np.max(fea_reduce[:,0])
y_min = np.min(fea_reduce[:,1])
y_max = np.max(fea_reduce[:,1])
print(x_min, x_max, y_min, y_max)
# (-86.09199, 59.1048, -70.07758, 74.11734)
box1 = [60, 73, 41, 49]
box2 = [-66, -56, 31, 39]
box3 = [49, 61, -62, -51]
box4 = [-83, -72, -40, -28]
box_set = [box1, box2, box3, box4]
def plot_rect(ax, x_min, x_max, y_min, y_max, step=0.02):
x_axis = np.arange(x_min, x_max, step)
y_axis = np.arange(y_min, y_max, step)
ax.plot(x_axis, np.full_like(x_axis, y_min), 'r-')
ax.plot(x_axis, np.full_like(x_axis, y_max), 'r-')
ax.plot(np.full_like(y_axis, x_min), y_axis, 'r-')
ax.plot(np.full_like(y_axis, x_max), y_axis, 'r-')
# return ax
def get_box_feature(fea_reduce, x_min, x_max, y_min, y_max):
index_x = np.where(np.logical_and(fea_reduce[:,0] < x_max, fea_reduce[:, 0] > x_min))
index_x = index_x[0]
# print(index_x.shape)
fea_sub_set = fea_reduce[index_x, 1]
index_y = np.where(np.logical_and(fea_sub_set < y_max, fea_sub_set > y_min))
index_y = index_y[0]
index_xy = index_x[index_y]
return index_xy
def get_sub_set(fea_reduce, path_set, index):
index = index.astype(int)
sub_fea = fea_reduce[index, :]
sub_path = [path_set[idx] for idx in index]
return sub_fea, sub_path
print(fea_reduce.shape)
print(path_set[0])
print(os.path.isfile(path_set[0]))
path_set = [path.replace('/data/Guoxian_Dai/car196/car_ims', './car196_thumbnail').replace('.jpg', '.png') for path in path_set]
print(path_set[0])
print(os.path.isfile(path_set[0]))
# This is for subfigure
box_id = 3
box_set = [box_set[box_id]]
index_xy = get_box_feature(fea_reduce, box_set[0][0], box_set[0][1], box_set[0][2], box_set[0][3])
fea_reduce, path_set = get_sub_set(fea_reduce, path_set, index_xy)
print(fea_reduce.shape)
print(len(path_set))
def main():
x = fea_reduce[:,0]
y = fea_reduce[:,1]
# image_path = get_sample_data('ada.png')
image_path = path_set
fig, ax = plt.subplots()
# plot_rect(ax, -50, 50, -50, 50)
# plot_rect(ax, -87, -77, -12, -6)
for box in box_set:
plot_rect(ax, box[0], box[1], box[2], box[3])
# zoom = 0.06 For the whole image
# imscatter(x, y, image_path, zoom=0.06, ax=ax)
# zoom = 0.1 For the first boxes
imscatter(x, y, image_path, zoom=0.2, ax=ax)
ax.plot(x, y, 'o')
ax.axis('off')
plt.show()
def imscatter(x, y, image_set, ax=None, zoom=1):
def read_img(ax, image):
if ax is None:
ax = plt.gca()
try:
image = plt.imread(image)
except TypeError:
# Likely already an array...
pass
return image
# im = OffsetImage(image, zoom=zoom)
x, y = np.atleast_1d(x, y)
artists = []
counter = 0
for x0, y0, image in zip(x, y, image_set):
if counter % 100 == 0:
print(counter, image)
image = read_img(ax, image)
im = OffsetImage(image, zoom=zoom)
ab = AnnotationBbox(im, (x0, y0), xycoords='data', frameon=False)
artists.append(ax.add_artist(ab))
counter += 1
# plot_rect(ax, -87, -77, -12, -6)
ax.update_datalim(np.column_stack([x, y]))
ax.autoscale()
return artists
main()