Skip to content

Commit

Permalink
Merge pull request #2 from rentainhe/rebuild_project
Browse files Browse the repository at this point in the history
rebuild project structure
  • Loading branch information
rentainhe authored Sep 30, 2021
2 parents dae674c + 3381428 commit 3e40f3f
Show file tree
Hide file tree
Showing 30 changed files with 354 additions and 256 deletions.
38 changes: 6 additions & 32 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
ckpts/
results/cache/*.json
results/cache/*.txt
results/result_test/*.json
results/result_test/*.txt
results/pred/*.pkl
results/log/*.txt
data/clevr/raw/images/
data/clevr/raw/questions/
data/clevr/raw/scenes/
data/clevr/feats/train/
data/clevr/feats/val/
data/clevr/feats/test/
data/gqa/raw/eval/
data/gqa/raw/questions1.2/
data/gqa/raw/sceneGraphs/
data/gqa/feats/gqa-frcn/
data/gqa/feats/gqa-grid/
data/vqa/raw/*.json
data/vqa/feats/train2014/
data/vqa/feats/val2014/
data/vqa/feats/test2015/
.idea/



# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -73,6 +48,7 @@ htmlcov/
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

Expand Down Expand Up @@ -116,8 +92,12 @@ ipython_config.py
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py
Expand Down Expand Up @@ -148,9 +128,3 @@ dmypy.json

# Pyre type checker
.pyre/

# Microsoft
.vscode
.vscode/
.vs
.vs/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Francesco Saverio Zuppichini

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include visualize/test_data/*.jpg
119 changes: 86 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,106 @@
# visualization
a collection of visualization operation
a collection of visualization operation for easier usage, check [usage](#usage) for a quick start.

## New Features
**2021/09/29**
- Add pip installation
- Build a cleaner repo

## Contents
- [__Attention Map Visualization__](https://github.com/rentainhe/visualization/tree/master/visualize_attention_map)
- [__Image Reading Difference__](https://github.com/rentainhe/visualization/tree/master/read_img)
- [__Region Attention Visualization__](https://github.com/rentainhe/visualization/tree/master/visualize_region_attention)
- [__Draw Line Chart__]()
### Visualization Function
- [__Grid Attention Visualization__](/visualize/grid_attention_visualization/)
- [__Region Attention Visualization__](/visualize/region_attention_visualization/)
- [__Draw Line Chart__](/visualize/draw_line_chart/draw.py)

### Learning Notes Sharing
- [__Image Reading Difference__](/notes)


## Installation
```bash
pip install visualize==0.5.0
```

## Usage
### 1. Grid Attention Visualization
<details>
<summary> <b> Run Example </b> </summary>

You can try [example.py](/example.py) by cloning this repo for a quick start.
```bash
git clone https://github.com/rentainhe/visualization.git
python example.py
```
results will be saved to `./test_grid_attention` and `./test_region_attention`
</details>

<details>
<summary> <b> Region Attention Visualization </b> </summary>

**download the [example.jpg](/visualize/test_data/example.jpg) to any folder you like**
```bash
$ wget
```
**build the following python script for a quick start**
```python
import numpy as np
from visualize_attention_map.visualize_attention_map_V2 import visulize_attention_ratio

img_path = 'test_data/test_image.jpg'
save_path = 'test_data/'
random_attention = np.random.randn(14, 14)
from visualize import visualize_region_attention

visulize_attention_ratio(img_path=img_path, save_path=save_path, attention_mask=random_attention, save_image=True,
save_original_image=True)
img_path="path/to/example.jpg"
save_path="example"
attention_retio=1.0
boxes = np.array([[14, 25, 100, 200], [56, 75, 245, 300]], dtype='int')
boxes_attention = [0.36, 0.64]
visualize_region_attention(img_path,
save_path=save_path,
boxes=boxes,
box_attentions=boxes_attention,
attention_ratio=attention_retio,
save_image=True,
save_origin_image=True,
quality=100)
```
- __img_path: where the image you want to put an attention mask on.__
- __save_path: where to save the image.__
- __attention_mask: the attention mask with format `numpy.ndarray`, its shape is (H, W)__
- __save_image=True: save the image with attention map or not, default: True.__
- __save_original_image=True: save the original image, default: True__
- `img_path`: where to load the original image
- `boxes`: a list of coordinates for the bounding boxes
- `box_attentions`: a list of attention scores for each bounding box
- `attention_ratio`: a special param, if you set the attention_ratio larger, it will make the attention map look more shallow. Just try!
- `save_image=True`: save the image with attention map or not, e.g., default: True.
- `save_original_image=True`: save the original image at the same time, e.g., default: True

**Note that you can check [Region Attention Visualization](/visualize/region_attention_visualization/) here for more details**

__Just run this example to see the result: [grid_attention_example.py](https://github.com/rentainhe/visualization/blob/master/grid_attention_example.py)__
</details>

__Or you can check [Attention Map Visualization](https://github.com/rentainhe/visualization/tree/master/visualize_attention_map) here for more details__
<details>
<summary> <b> Grid Attention Visualization</b> </summary>

### 2. Region Attention Visualization
**download the [example.jpg](/visualize/test_data/example.jpg) to any folder you like**
```bash
$ wget
```

**build the following python script for a quick start**
```python
from visualize_region_attention.region_attention_visualization import region_attention_visualization
from visualize import visualize_grid_attention_v2
import numpy as np

img_path = "test_data/test_image.jpg"
boxes = np.array([[14, 25, 100, 200], [56, 75, 245, 300]], dtype='int')
region_attention_visualization(img_path, boxes, box_attentions=[0.36, 0.64], attention_ratio=1.0)
img_path="./example.jpg"
save_path="test"
attention_mask = np.random.randn(14, 14)
visualize_grid_attention_v2(img_path,
save_path=save_path,
attention_mask=attention_mask,
save_image=True,
save_original_image=True,
quality=100)
```
- __img_path: the path of the original image__
- __boxes: bounding box__
- __box_attentions: the attention score of each bounding box__
- __attention_ratio: a special param, if you set the attention_ratio larger, it will make the attention map look more shallow. Just try!__
- `img_path`: where the image you want to put an attention mask on.
- `save_path`: where to save the image.
- `attention_mask`: the attention mask with format `numpy.ndarray`, its shape is (H, W)
- `save_image=True`: save the image with attention map or not, e.g., default: True.
- `save_original_image=True`: save the original image at the same time, e.g., default: True


__Just run this example to see the result: [region_attention_example.py](https://github.com/rentainhe/visualization/blob/master/region_attention_example.py)__
**Note that you can check [Attention Map Visualization](https://github.com/rentainhe/visualization/tree/master/visualize_attention_map) here for more details**

__Or you can check [Region Attention Visualization](https://github.com/rentainhe/visualization/tree/master/visualize_region_attention) here for more details__
</details>

### 3. Draw Line Chart
- [__example.py__](https://github.com/rentainhe/visualization/blob/master/draw_line_chart/draw.py)
37 changes: 0 additions & 37 deletions debug.py

This file was deleted.

42 changes: 42 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from visualize import visualize_region_attention, visualize_grid_attention, visualize_grid_attention_v2
import numpy as np

def run_region_attention_example():
# test region attention
img_path="visualize/test_data/example.jpg"
save_path="test_region_attention/"
attention_retio=1.0
boxes = np.array([[14, 25, 100, 200], [56, 75, 245, 300]], dtype='int')
boxes_attention = [0.36, 0.64]
visualize_region_attention(img_path,
save_path=save_path,
boxes=boxes,
box_attentions=boxes_attention,
attention_ratio=attention_retio,
save_image=True,
save_origin_image=True,
quality=100)


def run_grid_attention_example(img_path="visualize/test_data/example.jpg", save_path="test_grid_attention/", attention_mask=None, version=2, quality=100):
if not attention_mask:
attention_mask = np.random.randn(14, 14)
assert version in [1, 2], "We only support two version of attention visualization example"
if version == 1:
visualize_grid_attention(img_path=img_path,
save_path=save_path,
attention_mask=attention_mask,
save_image=True,
save_original_image=True,
quality=quality)
elif version == 2:
visualize_grid_attention_v2(img_path=img_path,
save_path=save_path,
attention_mask=attention_mask,
save_image=True,
save_original_image=True,
quality=quality)

if __name__ == "__main__":
run_grid_attention_example(version=2) # version in [1, 2]
run_region_attention_example()
11 changes: 0 additions & 11 deletions grid_attention_example.py

This file was deleted.

File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions region_attention_example.py

This file was deleted.

46 changes: 46 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from setuptools import find_packages


try:
from setuptools import setup
except ImportError:
from distutils.core import setup
"""
这里这么写的目的是防止setup导入出错,安装出现异常。但一般不会出错
"""

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name='visualize',
version='0.1.43', # 版本号
author='rentianhe',
author_email='[email protected]',
description='useful visualization function',
license='MIT',
packages=find_packages(),#需要安装的代码包,也可以用find_packages函数
install_requires=['numpy',
'opencv-python',
'matplotlib',
'Pillow'
],
classifiers = [
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Intended Audience :: Developers',
'Operating System :: OS Independent',
],
keywords='computer vision',
url='https://github.com/rentainhe/visualization',
# zip_safe=True, # 设为True,以zip的方式进行传输
# include_package_data=True,
platforms='any',
long_description=long_description,
long_description_content_type="text/markdown",
)
Binary file removed test_data/test_image_original.jpg
Binary file not shown.
Binary file removed test_data/test_image_with_attention.jpg
Binary file not shown.
2 changes: 2 additions & 0 deletions visualize/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .grid_attention_visualization import visualize_grid_attention, visualize_grid_attention_v2
from .region_attention_visualization import visualize_region_attention
Empty file.
File renamed without changes.
2 changes: 2 additions & 0 deletions visualize/example/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .grid_attention_example import run_grid_attention_example
from .region_attention_example import run_region_attention_example
Loading

0 comments on commit 3e40f3f

Please sign in to comment.