Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add query script, update usage #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
*cache*
data.csv
.ycm*

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
31 changes: 31 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,38 @@ In my database, there are 25 classes, each class has its own directory,
and the images belong to this class should put into this directory.

### Part2: run the code

#### For image retrieval test

I implement several algorithm, you can run it with python3.
If you need to test with one of the algorithms, please use `query.py <method to test>`, by default the image being queried is the first one in `data.csv`

result looks like

```text
$ .venv/bin/python query.py resnet
Using cache..., config=resnet152-avg, distance=d1, depth=3

[+] query: database/vim/vim-02.jpg

database/vim/vim-01.jpg: 0.4892213046550751, Class vim
database/vim/vim-03.jpg: 0.5926028490066528, Class vim
database/emacs/emacs-02.jpg: 0.7468970417976379, Class emacs
database/emacs/emacs-01.jpg: 0.7663252353668213, Class emacs
database/store/qingfeng-1.jpg: 0.818065881729126, Class store

$ cat data.csv
img,cls
database/vim/vim-02.jpg,vim
database/store/qingfeng-1.jpg,store
database/vim/vim-04.jpg,vim
database/vim/vim-01.jpg,vim
database/vim/vim-03.jpg,vim
database/store/qingfeng-2.jpg,store
database/store/qingfeng.jpg,store
database/emacs/emacs-01.jpg,emacs
database/emacs/emacs-02.jpg,emacs
```

#### For RGB histogram
```python
Expand Down
Binary file added database/emacs/emacs-01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/emacs/emacs-02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/store/qingfeng-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/store/qingfeng-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/store/qingfeng.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/vim/vim-01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/vim/vim-02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/vim/vim-03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/vim/vim-04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# pylint: disable=invalid-name,missing-docstring,exec-used,too-many-arguments,too-few-public-methods,no-self-use
from __future__ import print_function

import sys
from src.color import Color
from src.daisy import Daisy
from src.DB import Database
from src.edge import Edge
from src.evaluate import infer
from src.gabor import Gabor
from src.HOG import HOG
from src.resnet import ResNetFeat
from src.vggnet import VGGNetFeat

depth = 5
d_type = 'd1'
query_idx = 0

if __name__ == '__main__':
db = Database()

# methods to use
methods = {
"color": Color,
"daisy": Daisy,
"edge": Edge,
"hog": HOG,
"gabor": Gabor,
"vgg": VGGNetFeat,
"resnet": ResNetFeat
}

try:
mthd = sys.argv[1].lower()
except IndexError:
print("usage: {} <method>".format(sys.argv[0]))
print("supported methods:\ncolor, daisy, edge, gabor, hog, vgg, resnet")

sys.exit(1)

# call make_samples(db) accordingly
samples = getattr(methods[mthd](), "make_samples")(db)

# query the first img in data.csv
query = samples[query_idx]
print("\n[+] query: {}\n".format(query["img"]))

_, result = infer(query, samples=samples, depth=depth, d_type=d_type)

for match in result:
print("{}:\t{},\tClass {}".format(match["img"],
match["dis"],
match["cls"]))
63 changes: 33 additions & 30 deletions src/DB.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
# -*- coding: utf-8 -*-
# pylint: disable=invalid-name,missing-docstring,exec-used,too-many-arguments,too-few-public-methods,no-self-use

from __future__ import print_function

import pandas as pd
import os

import pandas as pd

DB_dir = 'database'
DB_csv = 'data.csv'


class Database(object):
class Database:

def __init__(self):
self._gen_csv()
self.data = pd.read_csv(DB_csv)
self.classes = set(self.data["cls"])

def _gen_csv(self):
if os.path.exists(DB_csv):
return
with open(DB_csv, 'w', encoding='UTF-8') as f:
f.write("img,cls")

def __init__(self):
self._gen_csv()
self.data = pd.read_csv(DB_csv)
self.classes = set(self.data["cls"])
for root, _, files in os.walk(DB_dir, topdown=False):
cls = root.split('/')[-1]

def _gen_csv(self):
if os.path.exists(DB_csv):
return
with open(DB_csv, 'w', encoding='UTF-8') as f:
f.write("img,cls")
for root, _, files in os.walk(DB_dir, topdown=False):
cls = root.split('/')[-1]
for name in files:
if not name.endswith('.jpg'):
continue
img = os.path.join(root, name)
f.write("\n{},{}".format(img, cls))
for name in files:
if not name.endswith('.jpg'):
continue
img = os.path.join(root, name)
f.write("\n{},{}".format(img, cls))

def __len__(self):
return len(self.data)
def __len__(self):
return len(self.data)

def get_class(self):
return self.classes
def get_class(self):
return self.classes

def get_data(self):
return self.data
def get_data(self):
return self.data


if __name__ == "__main__":
db = Database()
data = db.get_data()
classes = db.get_class()
db = Database()
data = db.get_data()
classes = db.get_class()

print("DB length:", len(db))
print(classes)
print("DB length:", len(db))
print(classes)
Loading