Commit e88b7760 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add model zoo

parent 8bbad053
Loading
Loading
Loading
Loading
+89 −0
Original line number Diff line number Diff line
name: Onnx Model Export

on:
  push:
  workflow_dispatch:

jobs:
  onnx_export:
    name: Onnx Export
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - 'ubuntu-20.04'
        python-version:
          - '3.8'
        model-name:
          - 'lpips'

    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 20
      - name: Set up python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Set up python dependences
        run: |
          pip install --upgrade pip
          pip install --upgrade flake8 setuptools wheel twine
          pip install -r requirements-build.txt
          pip install -r requirements.txt
          pip install -r requirements-zoo.txt
      - name: Transform models to onnx format
        env:
          DIST_DIR: dist
          CKPT_NAME: ${{ matrix.ckpt-name }}
        run: |
          python -m zoo.${{ matrix.model-name }} export -O ./${{ matrix.model-name }} 
          ls -al ./${{ matrix.model-name }}
      - name: Upload the character databases
        uses: actions/upload-artifact@v3
        with:
          name: onnx-models
          path: ${{ matrix.model-name }}/*

  data_upload:
    name: Data Upload
    runs-on: ${{ matrix.os }}
    needs:
      - onnx_export
    strategy:
      fail-fast: false
      matrix:
        os:
          - 'ubuntu-latest'
        python-version:
          - '3.8'

    steps:
      - name: Prepare the repository
        shell: bash
        run: |
          sudo apt-get install -y tree
          git lfs install
          git clone https://huggingface.co/deepghs/imgutils-models
          ls -al imgutils-models
      - name: Download from artifact
        uses: actions/download-artifact@v3
        with:
          name: onnx-models
          path: imgutils-models
      - name: See what is in this path
        shell: bash
        run: |
          tree .
      - name: Push models to hugging face repostory
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        run: |
          cd imgutils-models
          git config user.name 'narugo1992'
          git config user.email 'narugo@126.com'
          git add -A
          git diff-index --quiet HEAD || git commit -a -m "dev(narugo): model publish $(date -R)"
          git push https://narugo:$HF_TOKEN@huggingface.co/deepghs/imgutils-models main
+2 −1
Original line number Diff line number Diff line
@@ -1214,3 +1214,4 @@ fabric.properties
/resource/yolort/*
!/resource/yolort/Makefile
!/test/testfile/**/*
!/zoo/testfile/**/*
 No newline at end of file
+0 −0

Empty file added.

+62 −0
Original line number Diff line number Diff line
from functools import lru_cache
from typing import Tuple, Sequence

import cv2
import numpy as np
from PIL import Image
from sklearn.cluster import DBSCAN
from tqdm.auto import tqdm

from ..data import load_image, ImageTyping


def _image_resize(image: Image.Image, size=640):
    return image.resize((size, size), resample=Image.BILINEAR)


def _fft_encode(image: Image.Image, size=640, scale=(0.25, 0.75)):
    image = _image_resize(image, size)
    data = np.asarray(image)
    height, width, channels = data.shape
    ls, us = scale

    fshift = np.fft.fftshift(np.fft.fft2(data))
    fft_freq = np.log(np.abs(fshift) + 1e-6)
    cropped_freq = fft_freq[int(height * ls): int(height * us), int(width * ls): int(width * us)]
    blur_freq = cv2.GaussianBlur(cropped_freq, (5, 5), 0)
    return blur_freq


def _diff(d1: np.ndarray, d2: np.ndarray) -> float:
    return float(((d1 - d2) ** 2).mean())


def fft_difference(img1: ImageTyping, img2: ImageTyping, size: int = 640, scale: Tuple[float, float] = (0.25, 0.75)):
    image1 = load_image(img1, mode='RGB')
    image2 = load_image(img2, mode='RGB')
    return _diff(
        _fft_encode(image1, size=size, scale=scale),
        _fft_encode(image2, size=size, scale=scale),
    )


def fft_clustering(imgs: Sequence[ImageTyping], size: int = 640, scale: Tuple[float, float] = (0.15, 0.75),
                   threshold: float = 0.3, min_samples: int = 2):
    images = [load_image(item, mode='RGB') for item in imgs]
    n = len(images)
    encoded = [_fft_encode(item, size, scale) for item in tqdm(images)]
    stacked = np.stack(encoded)  # BHWC
    print(stacked)
    print(stacked.shape)

    @lru_cache(maxsize=min(n * (n - 1) // 2, 10000))
    def _get_diff(x, y):
        return _diff(stacked[x], stacked[y])

    def _metric(x, y):
        x, y = int(min(x, y)), int(max(x, y))
        return _get_diff(x, y)

    samples = np.array(range(n)).reshape(-1, 1)
    cls = DBSCAN(eps=threshold, min_samples=min_samples, metric=_metric).fit(samples)
    return cls.labels_

requirements-zoo.txt

0 → 100644
+10 −0
Original line number Diff line number Diff line
torch
lpips
matplotlib
torchvision
tqdm
onnx
onnxoptimizer
onnxsim
onnxruntime
click
Loading