Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
TakanoTaiga committed Nov 4, 2024
1 parent 57a9208 commit d860796
Show file tree
Hide file tree
Showing 11 changed files with 1,055 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 1000
exclude = .git,__pycache__,docs
38 changes: 38 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Docker Build and Push to GHCR
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build-and-push:
runs-on: ubuntu-22.04

steps:
- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@main
with:
tool-cache: false

- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to GitHub Container Registry
if: github.event_name == 'push'
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build Docker image
run: |
docker build -t ghcr.io/${{ github.repository }}:latest .
- name: Push Docker image
if: github.event_name == 'push'
run: |
docker push ghcr.io/${{ github.repository }}:latest
26 changes: 26 additions & 0 deletions .github/workflows/python-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Lint Python Code

on:
pull_request:
branches:
- main

jobs:
flake8-lint:
runs-on: ubuntu-22.04
steps:
- name: Check out the repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
- name: Run flake8
run: flake8 .
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM nvidia/cuda:12.6.2-cudnn-devel-ubuntu22.04

RUN apt-get update && apt-get install -y git curl python3-pip
RUN git config --global --add safe.directory /app
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install poetry \
&& poetry config virtualenvs.create false

WORKDIR /app

COPY pyproject.toml poetry.lock ./
RUN poetry install --no-root

COPY download_weight.py ./
RUN python3 download_weight.py
6 changes: 6 additions & 0 deletions download_weight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import clip
import torch

device = "cuda" if torch.cuda.is_available() else "cpu"
print("Detect device: ", device)
model, preprocess = clip.load("ViT-B/32", device=device)
7 changes: 3 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import glob

device = "cuda" if torch.cuda.is_available() else "cpu"
print("Detect device: ", device)
model, preprocess = clip.load("ViT-B/32", device=device)


def get_image_features(image_path):
image = preprocess(Image.open(image_path)).unsqueeze(0).to(device)
with torch.no_grad():
Expand All @@ -26,8 +28,5 @@ def get_image_features(image_path):
continue
similarities.append((feature @ feature_.T).item())

similarity_average = sum(similarities) / len(similarities)
similarity_average = sum(similarities) / len(similarities)
print(similarity_average)

# 0.8536337896439236
# 0.9495137247899262
903 changes: 903 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions poetylock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

cd /app
poetry lock --no-update
17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tool.poetry]
name = "lab_tool"
version = "0.1.0"
description = "research"
authors = ["Taiga Takano <[email protected]>"]

[tool.poetry.dependencies]
python = ">=3.10,<3.12"
flake8 = "7.1.1"
torch = "2.5.1"
torchvision = "0.20.1"
torchaudio = "2.5.1"
clip = { git = "https://github.com/openai/CLIP.git", rev = "dcba3cb" }

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
38 changes: 38 additions & 0 deletions run_dev_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# https://qiita.com/k_ikasumipowder/items/5e71208b7c7ae3e4fe7c

ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

# Prevent running as root.
if [[ $(id -u) -eq 0 ]]; then
echo "This script cannot be executed with root privileges."
echo "Please re-run without sudo and follow instructions to configure docker for non-root user if needed."
exit 1
fi

# Check if user can run docker without root.
RE="\<docker\>"
if [[ ! $(groups $USER) =~ $RE ]]; then
echo "User |$USER| is not a member of the 'docker' group and cannot run docker commands without sudo."
echo "Run 'sudo usermod -aG docker \$USER && newgrp docker' to add user to 'docker' group, then re-run this script."
echo "See: https://docs.docker.com/engine/install/linux-postinstall/"
exit 1
fi

# Check if able to run docker commands.
if [[ -z "$(docker ps)" ]] ; then
echo "Unable to run docker commands. If you have recently added |$USER| to 'docker' group, you may need to log out and log back in for it to take effect."
echo "Otherwise, please check your Docker installation."
exit 1
fi

PLATFORM="$(uname -m)"

if [ $PLATFORM = "x86_64" ]; then
echo "x86"
docker pull ghcr.io/moriyalab/lab_tool:latest
docker run -it --rm --gpus all --runtime nvidia --shm-size=32G -v $ROOT:/app -w /app --network host ghcr.io/moriyalab/clip_image2vec:latest /bin/bash
else
echo "Not Support Platform. Only support x86."
fi
2 changes: 2 additions & 0 deletions viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)


def get_image_features(image_path):
image = preprocess(Image.open(image_path)).unsqueeze(0).to(device)
with torch.no_grad():
image_features = model.encode_image(image)
return image_features / image_features.norm(dim=-1, keepdim=True) # 正規化


# 特徴量の抽出
image_features = []
image_paths = glob.glob("./images/*")
Expand Down

0 comments on commit d860796

Please sign in to comment.