-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use faiss scalar quantizer to reduce dimensionality
- Loading branch information
Showing
8 changed files
with
186 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
downsample = 4 | ||
# C x H x W | ||
screen_size = (4, 360, 640) | ||
screen_size = (4, int(360/downsample), int(640/downsample)) | ||
# screen_size = (128, 128) | ||
# TODO: The screen size is too large to fit in GPU memory and is probably overkill | ||
# Let's downscale the resolution of the input images and represent the four channels as one | ||
|
||
screen_input_size = 4 * 360 * 640 | ||
voxel_grid_size = (64*64,) | ||
dataset_size = 1 | ||
# screen_input_size = 4 * 360 * 640 | ||
voxel_grid_size = (16*16,) | ||
dataset_size = 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import faiss | ||
import numpy as np | ||
|
||
# random training data | ||
mt = np.random.rand(1, 4).astype('float32') | ||
mat = faiss.PCAMatrix (4, 2) | ||
mat.train(mt) | ||
assert mat.is_trained | ||
tr = mat.apply(mt) | ||
# print this to show that the magnitude of tr's columns is decreasing | ||
print(tr.shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import faiss | ||
import numpy as np | ||
from skimage import io, transform | ||
import os | ||
from einops import rearrange | ||
import sys | ||
|
||
d = 4 # data dimension | ||
dataset_size = 32 | ||
|
||
# train set | ||
img = io.imread(os.path.join("data", "voxels_0.png")) | ||
xt = rearrange(img, 'h w c -> (h w) c').astype('float32') | ||
print(sys.getsizeof(xt)) | ||
|
||
# QT_8bit allocates 8 bits per dimension (QT_4bit also works) | ||
sq = faiss.ScalarQuantizer(d, faiss.ScalarQuantizer.QT_4bit) | ||
sq.train(xt) | ||
|
||
# encode | ||
codes = sq.compute_codes(xt) | ||
print(sys.getsizeof(codes)) | ||
print(type(codes[0][1])) | ||
|
||
# decode | ||
x2 = sq.decode(codes) | ||
|
||
comp = rearrange(x2, '(h w) c -> h w c', h=90, w=160) | ||
io.imsave("test.png", comp) | ||
|
||
# compute reconstruction error | ||
# avg_relative_error = ((xt - x2)**2).sum() / (xt ** 2).sum() | ||
# print(codes) | ||
# print(avg_relative_error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters