Spaces:
Runtime error
Runtime error
Commit
·
3b835fc
0
Parent(s):
Duplicate from soumyaprabhamaiti/image_segmentation_web_app
Browse filesCo-authored-by: soumya_prabha_maiti <[email protected]>
- .gitattributes +35 -0
- .github/workflows/check_file_size.yml +16 -0
- .github/workflows/sync_to_HF_hub.yml +20 -0
- .gitignore +163 -0
- LICENSE +21 -0
- README.md +186 -0
- app.py +93 -0
- example_images/img1.jpg +0 -0
- example_images/img2.jpg +0 -0
- example_images/img3.jpg +0 -0
- example_images/img4.jpg +0 -0
- readme_images/image.png +0 -0
- readme_images/unet.png +0 -0
- requirements.txt +3 -0
- saved_model/fingerprint.pb +3 -0
- saved_model/keras_metadata.pb +3 -0
- saved_model/saved_model.pb +3 -0
- saved_model/variables/variables.data-00000-of-00001 +3 -0
- saved_model/variables/variables.index +3 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.github/workflows/check_file_size.yml
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Check file size
|
2 |
+
on: # or directly `on: [push]` to run the action on every push on any branch
|
3 |
+
pull_request:
|
4 |
+
branches: [main]
|
5 |
+
|
6 |
+
# to run this workflow manually from the Actions tab
|
7 |
+
workflow_dispatch:
|
8 |
+
|
9 |
+
jobs:
|
10 |
+
check-file-size:
|
11 |
+
runs-on: ubuntu-latest
|
12 |
+
steps:
|
13 |
+
- name: Check large files
|
14 |
+
uses: ActionsDesk/[email protected]
|
15 |
+
with:
|
16 |
+
filesizelimit: 10485760 # this is 10MB so we can sync to HF Spaces
|
.github/workflows/sync_to_HF_hub.yml
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Sync to Hugging Face hub
|
2 |
+
on:
|
3 |
+
push:
|
4 |
+
branches: [main]
|
5 |
+
|
6 |
+
# to run this workflow manually from the Actions tab
|
7 |
+
workflow_dispatch:
|
8 |
+
|
9 |
+
jobs:
|
10 |
+
sync-to-hub:
|
11 |
+
runs-on: ubuntu-latest
|
12 |
+
steps:
|
13 |
+
- uses: actions/checkout@v3
|
14 |
+
with:
|
15 |
+
fetch-depth: 0
|
16 |
+
lfs: true
|
17 |
+
- name: Push to hub
|
18 |
+
env:
|
19 |
+
HF: ${{ secrets.HF }}
|
20 |
+
run: git push --force https://soumyaprabhamaiti:[email protected]/spaces/soumyaprabhamaiti/image_segmentation_web_app main
|
.gitignore
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
#.idea/
|
161 |
+
|
162 |
+
# Additional ignores
|
163 |
+
flagged/
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Soumya Prabha Maiti
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
ADDED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Image Segmentation
|
3 |
+
emoji: 🌖
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: gray
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.35.2
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: mit
|
11 |
+
duplicated_from: soumyaprabhamaiti/image_segmentation_web_app
|
12 |
+
---
|
13 |
+
|
14 |
+
# Image Segmentation Web App
|
15 |
+
[](https://huggingface.co/spaces/soumyaprabhamaiti/image_segmentation_web_app)
|
16 |
+

|
17 |
+
|
18 |
+
This is a web app that segments the image of a pet animal into three regions - foreground (pet), background and boundary. It uses a [U-Net](https://arxiv.org/abs/1505.04597) model trained on [Oxford-IIIT Pet Dataset](https://www.robots.ox.ac.uk/~vgg/data/pets/) and is deployed using [Gradio](https://gradio.app/).
|
19 |
+
|
20 |
+
## Demo
|
21 |
+
|
22 |
+
The deployed version of this project can be accessed at [Hugging Face Spaces](https://huggingface.co/spaces/soumyaprabhamaiti/image_segmentation_web_app). Segmentation on a sample image is shown below:
|
23 |
+

|
24 |
+
|
25 |
+
## Installing Locally
|
26 |
+
|
27 |
+
To run this project locally, please follow these steps:
|
28 |
+
|
29 |
+
1. Clone the repository:
|
30 |
+
|
31 |
+
```
|
32 |
+
git clone https://github.com/soumya-prabha-maiti/image-segmentation-web-app
|
33 |
+
```
|
34 |
+
|
35 |
+
2. Navigate to the project folder:
|
36 |
+
|
37 |
+
```
|
38 |
+
cd image-segmentation-web-app
|
39 |
+
```
|
40 |
+
|
41 |
+
3. Install the required libraries:
|
42 |
+
|
43 |
+
```
|
44 |
+
pip install -r requirements.txt
|
45 |
+
```
|
46 |
+
|
47 |
+
4. Run the application:
|
48 |
+
|
49 |
+
```
|
50 |
+
python app.py
|
51 |
+
```
|
52 |
+
|
53 |
+
5. Access the application in your web browser at the specified port.
|
54 |
+
|
55 |
+
## Dataset
|
56 |
+
|
57 |
+
The [Oxford-IIIT Pet Dataset](https://www.robots.ox.ac.uk/~vgg/data/pets/) contains 37 categories of pets with roughly 200 images for each category. The images have a large variation in scale, pose and lighting. All images have an associated ground truth annotation of breed, head ROI, and pixel level trimap segmentation. Here the dataset was obtained using [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/oxford_iiit_pet).
|
58 |
+
|
59 |
+
## Model Architecture
|
60 |
+
|
61 |
+
The segmentation model uses the UNET architecture. The basic architecture of the UNET model is shown below:
|
62 |
+

|
63 |
+
The UNET model consists of an encoder and a decoder. The encoder is a series of convolutional layers that extract features from the input image. The decoder is a series of transposed convolutional layers that upsample the features to the original image size. Skip connections are used to connect the encoder and decoder layers. The skip connections concatenate the feature maps from the encoder to the corresponding feature maps in the decoder. This helps the decoder to recover the spatial information lost during the encoding process.
|
64 |
+
|
65 |
+
The detailed architecture of the UNET model used in this project is shown below:
|
66 |
+
```
|
67 |
+
Model: "model"
|
68 |
+
__________________________________________________________________________________________________
|
69 |
+
Layer (type) Output Shape Param # Connected to
|
70 |
+
==================================================================================================
|
71 |
+
input_1 (InputLayer) [(None, 256, 256, 3 0 []
|
72 |
+
)]
|
73 |
+
|
74 |
+
conv2d (Conv2D) (None, 256, 256, 16 448 ['input_1[0][0]']
|
75 |
+
)
|
76 |
+
|
77 |
+
conv2d_1 (Conv2D) (None, 256, 256, 16 2320 ['conv2d[0][0]']
|
78 |
+
)
|
79 |
+
|
80 |
+
max_pooling2d (MaxPooling2D) (None, 128, 128, 16 0 ['conv2d_1[0][0]']
|
81 |
+
)
|
82 |
+
|
83 |
+
dropout (Dropout) (None, 128, 128, 16 0 ['max_pooling2d[0][0]']
|
84 |
+
)
|
85 |
+
|
86 |
+
conv2d_2 (Conv2D) (None, 128, 128, 32 4640 ['dropout[0][0]']
|
87 |
+
)
|
88 |
+
|
89 |
+
conv2d_3 (Conv2D) (None, 128, 128, 32 9248 ['conv2d_2[0][0]']
|
90 |
+
)
|
91 |
+
|
92 |
+
max_pooling2d_1 (MaxPooling2D) (None, 64, 64, 32) 0 ['conv2d_3[0][0]']
|
93 |
+
|
94 |
+
dropout_1 (Dropout) (None, 64, 64, 32) 0 ['max_pooling2d_1[0][0]']
|
95 |
+
|
96 |
+
conv2d_4 (Conv2D) (None, 64, 64, 64) 18496 ['dropout_1[0][0]']
|
97 |
+
|
98 |
+
conv2d_5 (Conv2D) (None, 64, 64, 64) 36928 ['conv2d_4[0][0]']
|
99 |
+
|
100 |
+
max_pooling2d_2 (MaxPooling2D) (None, 32, 32, 64) 0 ['conv2d_5[0][0]']
|
101 |
+
|
102 |
+
dropout_2 (Dropout) (None, 32, 32, 64) 0 ['max_pooling2d_2[0][0]']
|
103 |
+
|
104 |
+
conv2d_6 (Conv2D) (None, 32, 32, 128) 73856 ['dropout_2[0][0]']
|
105 |
+
|
106 |
+
conv2d_7 (Conv2D) (None, 32, 32, 128) 147584 ['conv2d_6[0][0]']
|
107 |
+
|
108 |
+
max_pooling2d_3 (MaxPooling2D) (None, 16, 16, 128) 0 ['conv2d_7[0][0]']
|
109 |
+
|
110 |
+
dropout_3 (Dropout) (None, 16, 16, 128) 0 ['max_pooling2d_3[0][0]']
|
111 |
+
|
112 |
+
conv2d_8 (Conv2D) (None, 16, 16, 256) 295168 ['dropout_3[0][0]']
|
113 |
+
|
114 |
+
conv2d_9 (Conv2D) (None, 16, 16, 256) 590080 ['conv2d_8[0][0]']
|
115 |
+
|
116 |
+
conv2d_transpose (Conv2DTransp (None, 32, 32, 128) 295040 ['conv2d_9[0][0]']
|
117 |
+
ose)
|
118 |
+
|
119 |
+
concatenate (Concatenate) (None, 32, 32, 256) 0 ['conv2d_transpose[0][0]',
|
120 |
+
'conv2d_7[0][0]']
|
121 |
+
|
122 |
+
dropout_4 (Dropout) (None, 32, 32, 256) 0 ['concatenate[0][0]']
|
123 |
+
|
124 |
+
conv2d_10 (Conv2D) (None, 32, 32, 128) 295040 ['dropout_4[0][0]']
|
125 |
+
|
126 |
+
conv2d_11 (Conv2D) (None, 32, 32, 128) 147584 ['conv2d_10[0][0]']
|
127 |
+
|
128 |
+
conv2d_transpose_1 (Conv2DTran (None, 64, 64, 64) 73792 ['conv2d_11[0][0]']
|
129 |
+
spose)
|
130 |
+
|
131 |
+
concatenate_1 (Concatenate) (None, 64, 64, 128) 0 ['conv2d_transpose_1[0][0]',
|
132 |
+
'conv2d_5[0][0]']
|
133 |
+
|
134 |
+
dropout_5 (Dropout) (None, 64, 64, 128) 0 ['concatenate_1[0][0]']
|
135 |
+
|
136 |
+
conv2d_12 (Conv2D) (None, 64, 64, 64) 73792 ['dropout_5[0][0]']
|
137 |
+
|
138 |
+
conv2d_13 (Conv2D) (None, 64, 64, 64) 36928 ['conv2d_12[0][0]']
|
139 |
+
|
140 |
+
conv2d_transpose_2 (Conv2DTran (None, 128, 128, 32 18464 ['conv2d_13[0][0]']
|
141 |
+
spose) )
|
142 |
+
|
143 |
+
concatenate_2 (Concatenate) (None, 128, 128, 64 0 ['conv2d_transpose_2[0][0]',
|
144 |
+
) 'conv2d_3[0][0]']
|
145 |
+
|
146 |
+
dropout_6 (Dropout) (None, 128, 128, 64 0 ['concatenate_2[0][0]']
|
147 |
+
)
|
148 |
+
|
149 |
+
conv2d_14 (Conv2D) (None, 128, 128, 32 18464 ['dropout_6[0][0]']
|
150 |
+
)
|
151 |
+
|
152 |
+
conv2d_15 (Conv2D) (None, 128, 128, 32 9248 ['conv2d_14[0][0]']
|
153 |
+
)
|
154 |
+
|
155 |
+
conv2d_transpose_3 (Conv2DTran (None, 256, 256, 16 4624 ['conv2d_15[0][0]']
|
156 |
+
spose) )
|
157 |
+
|
158 |
+
concatenate_3 (Concatenate) (None, 256, 256, 32 0 ['conv2d_transpose_3[0][0]',
|
159 |
+
) 'conv2d_1[0][0]']
|
160 |
+
|
161 |
+
dropout_7 (Dropout) (None, 256, 256, 32 0 ['concatenate_3[0][0]']
|
162 |
+
)
|
163 |
+
|
164 |
+
conv2d_16 (Conv2D) (None, 256, 256, 16 4624 ['dropout_7[0][0]']
|
165 |
+
)
|
166 |
+
|
167 |
+
conv2d_17 (Conv2D) (None, 256, 256, 16 2320 ['conv2d_16[0][0]']
|
168 |
+
)
|
169 |
+
|
170 |
+
conv2d_18 (Conv2D) (None, 256, 256, 3) 51 ['conv2d_17[0][0]']
|
171 |
+
|
172 |
+
==================================================================================================
|
173 |
+
Total params: 2,158,739
|
174 |
+
Trainable params: 2,158,739
|
175 |
+
Non-trainable params: 0
|
176 |
+
```
|
177 |
+
## Libraries Used
|
178 |
+
|
179 |
+
The following libraries were used in this project:
|
180 |
+
|
181 |
+
- TensorFlow: To build segmentation model.
|
182 |
+
- Gradio: To create the user interface for the segmentation app.
|
183 |
+
|
184 |
+
## License
|
185 |
+
|
186 |
+
This project is licensed under the [MIT License](LICENSE).
|
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
|
8 |
+
# Path to the pre-trained sentiment analysis model
|
9 |
+
model_path = "saved_model"
|
10 |
+
|
11 |
+
# Load the pre-trained segmentation model
|
12 |
+
segmentation_model = tf.keras.models.load_model(model_path)
|
13 |
+
|
14 |
+
# Target image shape
|
15 |
+
TARGET_SHAPE = (256, 256)
|
16 |
+
|
17 |
+
# Define image segmentation function
|
18 |
+
def segment_image(img:np.ndarray):
|
19 |
+
# Original image shape
|
20 |
+
ORIGINAL_SHAPE = img.shape
|
21 |
+
|
22 |
+
# Check if the image is RGB and convert if not
|
23 |
+
if len(ORIGINAL_SHAPE) == 2:
|
24 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
25 |
+
|
26 |
+
# Resize the image to TARGET_SHAPE
|
27 |
+
img = cv2.resize(img, TARGET_SHAPE)
|
28 |
+
|
29 |
+
# Add a batch dimension
|
30 |
+
img = np.expand_dims(img, axis=0)
|
31 |
+
|
32 |
+
# Predict the segmentation mask
|
33 |
+
mask = segmentation_model.predict(img)
|
34 |
+
|
35 |
+
# Remove the batch dimension
|
36 |
+
mask = np.squeeze(mask, axis=0)
|
37 |
+
|
38 |
+
# Convert to labels
|
39 |
+
mask = np.argmax(mask, axis=-1)
|
40 |
+
|
41 |
+
# Convert to uint8
|
42 |
+
mask = mask.astype(np.uint8)
|
43 |
+
|
44 |
+
# Resize to original image shape
|
45 |
+
mask = cv2.resize(mask, (ORIGINAL_SHAPE[1], ORIGINAL_SHAPE[0]))
|
46 |
+
|
47 |
+
return mask
|
48 |
+
|
49 |
+
def overlay_mask(img, mask, alpha=0.5):
|
50 |
+
# Define color mapping
|
51 |
+
colors = {
|
52 |
+
0: [255, 0, 0], # Class 0 - Red
|
53 |
+
1: [0, 255, 0], # Class 1 - Green
|
54 |
+
2: [0, 0, 255] # Class 2 - Blue
|
55 |
+
# Add more colors for additional classes if needed
|
56 |
+
}
|
57 |
+
|
58 |
+
# Create a blank colored overlay image
|
59 |
+
overlay = np.zeros_like(img)
|
60 |
+
|
61 |
+
# Map each mask value to the corresponding color
|
62 |
+
for class_id, color in colors.items():
|
63 |
+
overlay[mask == class_id] = color
|
64 |
+
|
65 |
+
# Blend the overlay with the original image
|
66 |
+
output = cv2.addWeighted(img, 1 - alpha, overlay, alpha, 0)
|
67 |
+
|
68 |
+
return output
|
69 |
+
|
70 |
+
|
71 |
+
# The main function
|
72 |
+
def transform(img):
|
73 |
+
mask=segment_image(img)
|
74 |
+
blended_img = overlay_mask(img, mask)
|
75 |
+
return blended_img
|
76 |
+
|
77 |
+
|
78 |
+
# Create the Gradio app
|
79 |
+
app = gr.Interface(
|
80 |
+
fn=transform,
|
81 |
+
inputs=gr.Image(label="Input Image"),
|
82 |
+
outputs=gr.Image(label="Image with Segmentation Overlay"),
|
83 |
+
title="Image Segmentation on Pet Images",
|
84 |
+
description="Segment image of a pet animal into three classes: background, pet, and boundary.",
|
85 |
+
examples=[
|
86 |
+
"example_images/img1.jpg",
|
87 |
+
"example_images/img2.jpg",
|
88 |
+
"example_images/img3.jpg"
|
89 |
+
]
|
90 |
+
)
|
91 |
+
|
92 |
+
# Run the app
|
93 |
+
app.launch()
|
example_images/img1.jpg
ADDED
![]() |
example_images/img2.jpg
ADDED
![]() |
example_images/img3.jpg
ADDED
![]() |
example_images/img4.jpg
ADDED
![]() |
readme_images/image.png
ADDED
![]() |
readme_images/unet.png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
gradio
|
3 |
+
opencv-python
|
saved_model/fingerprint.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:706b1eb7b39818e6adb05977067a9eb60de93f0ee08cdba69eab221ccbe0c2a5
|
3 |
+
size 56
|
saved_model/keras_metadata.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:997f7b7999d5f61640e01f3812c19808ab71c88ccf47c7e3d6a90f6921991b7d
|
3 |
+
size 89706
|
saved_model/saved_model.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cb09b7345801bdfc009ceaed06323e63fbfa0992c73c9fc2506c4541d34b7abc
|
3 |
+
size 859084
|
saved_model/variables/variables.data-00000-of-00001
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b6c07ac84dc7c851f433881f68030336f10901f0d4594a3e84baaf6620dba4cf
|
3 |
+
size 25947878
|
saved_model/variables/variables.index
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:719056f4d8e75d7f561e76fd756fc3e8ba406025dd7c234583fa9c4c152cfd74
|
3 |
+
size 9077
|