# vq **Repository Path**: williamzjc/vq ## Basic Information - **Project Name**: vq - **Description**: vector quantization - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-04-20 - **Last Updated**: 2024-08-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # vq #### Introduction vector quantization for images VQ as an encoder/transformer of images ### Installation `git clone https://gitee.com/williamzjc/vq/` ### Usage #### Import `import vqtransform [as vq]` or `from vqtransform import vq` #### Easy example ```python im = Image.open('cloth.jpg') t = VQTransformer(n_clusters=2) t.fit(im) im = t.reconstruct() im.save('cloth.kmeans2.jpg') ``` #### Other examples ```python im = Image.open('lenna.jpg') block_size = 4, 4 n_clusters = 3 def demo(model, name='Encoder'): im0 = model.reconstruct() n_clusters = len(model.codebook_) import matplotlib.pyplot as plt fig, ax = plt.subplots(1,2) ax[0].imshow(im) ax[0].set_title('Original Image') ax[1].imshow(im0) ax[1].set_title(name) ax[0].axis('off') ax[1].axis('off') # fig.suptitle(f'{name} Demo (block-size: {block_size[0]} X {block_size[1]}, #clusters: {n_clusters})') plt.show() def demo_meanshift(): ms = MeanShiftTransformer(block_size=block_size, max_iter=100) ms.fit(im) im0 = ms.reconstruct() ms.fit(im, with_pos=True) im1 = ms.reconstruct() import matplotlib.pyplot as plt fig, ax = plt.subplots(1,3) ax[0].imshow(im) ax[0].set_title('Original Image') ax[1].imshow(im0) ax[1].set_title('MeanShift') ax[2].imshow(im1) ax[2].set_title('MeanShift (with position)') for a in ax: a.axis('off') # fig.suptitle(f'Demo of MeanShift (block-size: {block_size[0]} X {block_size[1]})') plt.show() demo_meanshift() def _mask(im, block_size=(1,1), shape=None): """Transform an image to 2d array where block_size = (w, h) Arguments: im {Image} -- an image Keyword Arguments: block_size {tuple} -- a rectangle domain of an image (default: (1, 1)) shape {tuple} -- size of small domain (default: {None}) calculated based on the size of the image by default Returns: tuple of a block matrix and its shape=(r, c) """ w, h = block_size if shape is None: W, H = im.size c, cc = divmod(W, w) r, rr = divmod(H, h) shape = r, c im = im.resize((w*c, h*r)) else: r, c = shape im = im.resize((0, 0, w*c, h*r)) I = np.asarray(im, dtype=np.float64) shape = (r, c) # blocks <- concatenate the columns of the block matrix def _t(a): def _f(x): if np.all(x == (255,0,0)): return 0 elif np.all(x == (0,255,0)): return 1 elif np.all(x == (0,0,255)): return 2 else: return -1 return np.max(np.apply_along_axis(_f, 2, a)) return np.array([[_t(I[i*h:(i+1)*h, j*w:(j+1)*w]) for j in range(c)] for i in range(r)]) def semivq(): svq = SemiVQTransformer(block_size=block_size, n_clusters=n_clusters) masked = Image.open('lenna-masked.jpg') mask = _mask(masked, block_size=block_size) svq.fit(im, mask=mask) im1 = svq.reconstruct() import matplotlib.pyplot as plt fig, ax = plt.subplots(2, 2) ax[0,0].imshow(im) ax[0,0].set_title('Original Image') ax[0,1].imshow(masked) ax[0,1].set_title('Masked Image') ax[1,0].imshow(im0) ax[1,0].set_title('VQ') ax[1,1].imshow(im1) ax[1,1].set_title('Semisupervised VQ') ax[0,0].axis('off') ax[0,1].axis('off') ax[1,0].axis('off') ax[1,1].axis('off') plt.show() ``` #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)