Skip to content

Commit 51991d0

Browse files
Merge pull request #23 from tomas-gajarsky/feature/expand_box_ratio
Feature/expand box ratio
2 parents be88115 + 093c1e4 commit 51991d0

File tree

12 files changed

+669
-611
lines changed

12 files changed

+669
-611
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Change Log
22

3+
4+
## 0.1.2
5+
6+
Released on November 10, 2022.
7+
8+
### Added
9+
* links to Demo App and User Guide in README.md
10+
11+
### Changed
12+
* Box expansion method changed from static absolute value to dynamic relative value
13+
314
## 0.1.1
415

516
Released on August 31, 2022.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
[![PyPI - License](https://img.shields.io/pypi/l/facetorch)](https://raw.githubusercontent.com/tomas-gajarsky/facetorch/main/LICENSE)
77
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
88

9+
[Demo App on Hugging Face Spaces 🤗 ](https://huggingface.co/spaces/tomas-gajarsky/facetorch-app)
910

10-
[Documentation](https://tomas-gajarsky.github.io/facetorch/facetorch/index.html), [Docker Hub](https://hub.docker.com/repository/docker/tomasgajarsky/facetorch) [(GPU)](https://hub.docker.com/repository/docker/tomasgajarsky/facetorch-gpu)
11+
[User Guide](https://medium.com/@gajarsky.tomas/facetorch-user-guide-a0e9fd2a5552), [Documentation](https://tomas-gajarsky.github.io/facetorch/facetorch/index.html)
12+
13+
[Docker Hub](https://hub.docker.com/repository/docker/tomasgajarsky/facetorch) [(GPU)](https://hub.docker.com/repository/docker/tomasgajarsky/facetorch-gpu)
1114

1215
Facetorch is a Python library that can detect faces and analyze facial features using deep neural networks. The goal is to gather open sourced face analysis models from the community, optimize them for performance using TorchScript and combine them to create a face analysis tool that one can:
1316

conf/analyzer/detector/retinaface.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ postprocessor:
4545
clip: False
4646
variance: [0.1, 0.2]
4747
reverse_colors: ${analyzer.detector.reverse_colors} # True # bool
48-
expand_pixels: 4 # int
48+
expand_box_ratio: 0.15 # float

docs/facetorch/analyzer/detector/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4-
<meta name="google-site-verification" content="YIhgkma200wIq_XsAUQ9zFq2isMAdB5CUSrzqys38y4" />
54
<meta charset="utf-8">
65
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
76
<meta name="generator" content="pdoc 0.10.0" />
@@ -252,7 +251,6 @@ <h4><code><a title="facetorch.analyzer.detector.FaceDetector" href="#facetorch.a
252251
</nav>
253252
</main>
254253
<footer id="footer">
255-
<p><a href="https://github.com/tomas-gajarsky/facetorch" title="facetorch: detect faces and analyze facial features using neural networks"><cite>facetorch GitHub repository</cite></a></p>
256254
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p>
257255
</footer>
258256
</body>

docs/facetorch/analyzer/detector/post.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ <h1 class="title">Module <code>facetorch.analyzer.detector.post</code></h1>
145145
prior_box: PriorBox,
146146
variance: List[float],
147147
reverse_colors: bool = False,
148-
expand_pixels: int = 0,
148+
expand_box_ratio: float = 0.0,
149149
):
150150
&#34;&#34;&#34;Initialize the detector postprocessor. Modified from https://github.com/biubug6/Pytorch_Retinaface.
151151

@@ -161,7 +161,7 @@ <h1 class="title">Module <code>facetorch.analyzer.detector.post</code></h1>
161161
prior_box (PriorBox): PriorBox object.
162162
variance (List[float]): Prior box variance.
163163
reverse_colors (bool): Whether to reverse the colors of the image tensor from RGB to BGR or vice versa. If False, the colors remain unchanged. Default: False.
164-
expand_pixels (int): Number of pixels to expand the face location and tensor by. Default: 0.
164+
expand_box_ratio (float): Expand the box by this ratio. Default: 0.0.
165165
&#34;&#34;&#34;
166166
super().__init__(transform, device, optimize_transform)
167167
self.confidence_threshold = confidence_threshold
@@ -172,7 +172,7 @@ <h1 class="title">Module <code>facetorch.analyzer.detector.post</code></h1>
172172
self.prior_box = prior_box
173173
self.variance = list(variance)
174174
self.reverse_colors = reverse_colors
175-
self.expand_pixels = expand_pixels
175+
self.expand_box_ratio = expand_box_ratio
176176

177177
@Timer(&#34;PostRetFace.run&#34;, &#34;{name}: {milliseconds:.2f} ms&#34;, logger.debug)
178178
def run(
@@ -313,7 +313,7 @@ <h1 class="title">Module <code>facetorch.analyzer.detector.post</code></h1>
313313
y2=int(_det[3]),
314314
)
315315

316-
loc.expand(amount=self.expand_pixels)
316+
loc.expand(amount=self.expand_box_ratio)
317317
loc.form_square()
318318

319319
return loc
@@ -587,7 +587,7 @@ <h2 id="returns">Returns</h2>
587587
</dd>
588588
<dt id="facetorch.analyzer.detector.post.PostRetFace"><code class="flex name class">
589589
<span>class <span class="ident">PostRetFace</span></span>
590-
<span>(</span><span>transform: torchvision.transforms.transforms.Compose, device: torch.device, optimize_transform: bool, confidence_threshold: float, top_k: int, nms_threshold: float, keep_top_k: int, score_threshold: float, prior_box: <a title="facetorch.analyzer.detector.post.PriorBox" href="#facetorch.analyzer.detector.post.PriorBox">PriorBox</a>, variance: List[float], reverse_colors: bool = False, expand_pixels: int = 0)</span>
590+
<span>(</span><span>transform: torchvision.transforms.transforms.Compose, device: torch.device, optimize_transform: bool, confidence_threshold: float, top_k: int, nms_threshold: float, keep_top_k: int, score_threshold: float, prior_box: <a title="facetorch.analyzer.detector.post.PriorBox" href="#facetorch.analyzer.detector.post.PriorBox">PriorBox</a>, variance: List[float], reverse_colors: bool = False, expand_box_ratio: float = 0.0)</span>
591591
</code></dt>
592592
<dd>
593593
<div class="desc"><p>Initialize the detector postprocessor. Modified from <a href="https://github.com/biubug6/Pytorch_Retinaface.">https://github.com/biubug6/Pytorch_Retinaface.</a></p>
@@ -615,8 +615,8 @@ <h2 id="args">Args</h2>
615615
<dd>Prior box variance.</dd>
616616
<dt><strong><code>reverse_colors</code></strong> :&ensp;<code>bool</code></dt>
617617
<dd>Whether to reverse the colors of the image tensor from RGB to BGR or vice versa. If False, the colors remain unchanged. Default: False.</dd>
618-
<dt><strong><code>expand_pixels</code></strong> :&ensp;<code>int</code></dt>
619-
<dd>Number of pixels to expand the face location and tensor by. Default: 0.</dd>
618+
<dt><strong><code>expand_box_ratio</code></strong> :&ensp;<code>float</code></dt>
619+
<dd>Expand the box by this ratio. Default: 0.0.</dd>
620620
</dl></div>
621621
<details class="source">
622622
<summary>
@@ -637,7 +637,7 @@ <h2 id="args">Args</h2>
637637
prior_box: PriorBox,
638638
variance: List[float],
639639
reverse_colors: bool = False,
640-
expand_pixels: int = 0,
640+
expand_box_ratio: float = 0.0,
641641
):
642642
&#34;&#34;&#34;Initialize the detector postprocessor. Modified from https://github.com/biubug6/Pytorch_Retinaface.
643643

@@ -653,7 +653,7 @@ <h2 id="args">Args</h2>
653653
prior_box (PriorBox): PriorBox object.
654654
variance (List[float]): Prior box variance.
655655
reverse_colors (bool): Whether to reverse the colors of the image tensor from RGB to BGR or vice versa. If False, the colors remain unchanged. Default: False.
656-
expand_pixels (int): Number of pixels to expand the face location and tensor by. Default: 0.
656+
expand_box_ratio (float): Expand the box by this ratio. Default: 0.0.
657657
&#34;&#34;&#34;
658658
super().__init__(transform, device, optimize_transform)
659659
self.confidence_threshold = confidence_threshold
@@ -664,7 +664,7 @@ <h2 id="args">Args</h2>
664664
self.prior_box = prior_box
665665
self.variance = list(variance)
666666
self.reverse_colors = reverse_colors
667-
self.expand_pixels = expand_pixels
667+
self.expand_box_ratio = expand_box_ratio
668668

669669
@Timer(&#34;PostRetFace.run&#34;, &#34;{name}: {milliseconds:.2f} ms&#34;, logger.debug)
670670
def run(
@@ -805,7 +805,7 @@ <h2 id="args">Args</h2>
805805
y2=int(_det[3]),
806806
)
807807

808-
loc.expand(amount=self.expand_pixels)
808+
loc.expand(amount=self.expand_box_ratio)
809809
loc.form_square()
810810

811811
return loc

docs/facetorch/datastruct.html

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,27 @@ <h1 class="title">Module <code>facetorch.datastruct</code></h1>
8686
else:
8787
pass
8888

89-
def expand(self, amount: int) -&gt; None:
89+
def expand(self, amount: float) -&gt; None:
9090
&#34;&#34;&#34;Expand the location while keeping the center.
9191

9292
Args:
93-
amount (int): Amount of pixels to expand the location by.
93+
amount (float): Amount to expand the location by in multiples of the original size.
94+
9495

9596
Returns:
9697
None
9798
&#34;&#34;&#34;
98-
if amount != 0:
99-
self.x1 = self.x1 - amount
100-
self.y1 = self.y1 - amount
101-
self.x2 = self.x2 + amount
102-
self.y2 = self.y2 + amount
99+
assert amount &gt;= 0, &#34;Amount must be greater than or equal to 0.&#34;
100+
# if amount != 0:
101+
# self.x1 = self.x1 - amount
102+
# self.y1 = self.y1 - amount
103+
# self.x2 = self.x2 + amount
104+
# self.y2 = self.y2 + amount
105+
if amount != 0.0:
106+
self.x1 = self.x1 - int((self.x2 - self.x1) / 2 * amount)
107+
self.y1 = self.y1 - int((self.y2 - self.y1) / 2 * amount)
108+
self.x2 = self.x2 + int((self.x2 - self.x1) / 2 * amount)
109+
self.y2 = self.y2 + int((self.y2 - self.y1) / 2 * amount)
103110

104111

105112
@dataclass
@@ -302,8 +309,7 @@ <h2 id="attributes">Attributes</h2>
302309
<summary>
303310
<span>Expand source code</span>
304311
</summary>
305-
<pre><code class="python">@dataclass
306-
class Dimensions:
312+
<pre><code class="python">class Dimensions:
307313
&#34;&#34;&#34;Data class for image dimensions.
308314

309315
Attributes:
@@ -347,8 +353,7 @@ <h2 id="attributes">Attributes</h2>
347353
<summary>
348354
<span>Expand source code</span>
349355
</summary>
350-
<pre><code class="python">@dataclass
351-
class Location:
356+
<pre><code class="python">class Location:
352357
&#34;&#34;&#34;Data class for face location.
353358

354359
Attributes:
@@ -383,20 +388,27 @@ <h2 id="attributes">Attributes</h2>
383388
else:
384389
pass
385390

386-
def expand(self, amount: int) -&gt; None:
391+
def expand(self, amount: float) -&gt; None:
387392
&#34;&#34;&#34;Expand the location while keeping the center.
388393

389394
Args:
390-
amount (int): Amount of pixels to expand the location by.
395+
amount (float): Amount to expand the location by in multiples of the original size.
396+
391397

392398
Returns:
393399
None
394400
&#34;&#34;&#34;
395-
if amount != 0:
396-
self.x1 = self.x1 - amount
397-
self.y1 = self.y1 - amount
398-
self.x2 = self.x2 + amount
399-
self.y2 = self.y2 + amount</code></pre>
401+
assert amount &gt;= 0, &#34;Amount must be greater than or equal to 0.&#34;
402+
# if amount != 0:
403+
# self.x1 = self.x1 - amount
404+
# self.y1 = self.y1 - amount
405+
# self.x2 = self.x2 + amount
406+
# self.y2 = self.y2 + amount
407+
if amount != 0.0:
408+
self.x1 = self.x1 - int((self.x2 - self.x1) / 2 * amount)
409+
self.y1 = self.y1 - int((self.y2 - self.y1) / 2 * amount)
410+
self.x2 = self.x2 + int((self.x2 - self.x1) / 2 * amount)
411+
self.y2 = self.y2 + int((self.y2 - self.y1) / 2 * amount)</code></pre>
400412
</details>
401413
<h3>Class variables</h3>
402414
<dl>
@@ -452,42 +464,49 @@ <h2 id="returns">Returns</h2>
452464
</details>
453465
</dd>
454466
<dt id="facetorch.datastruct.Location.expand"><code class="name flex">
455-
<span>def <span class="ident">expand</span></span>(<span>self, amount: int) ‑> None</span>
467+
<span>def <span class="ident">expand</span></span>(<span>self, amount: float) ‑> None</span>
456468
</code></dt>
457469
<dd>
458470
<div class="desc"><p>Expand the location while keeping the center.</p>
459471
<h2 id="args">Args</h2>
460472
<dl>
461-
<dt><strong><code>amount</code></strong> :&ensp;<code>int</code></dt>
462-
<dd>Amount of pixels to expand the location by.</dd>
473+
<dt><strong><code>amount</code></strong> :&ensp;<code>float</code></dt>
474+
<dd>Amount to expand the location by in multiples of the original size.</dd>
463475
</dl>
464476
<h2 id="returns">Returns</h2>
465477
<p>None</p></div>
466478
<details class="source">
467479
<summary>
468480
<span>Expand source code</span>
469481
</summary>
470-
<pre><code class="python">def expand(self, amount: int) -&gt; None:
482+
<pre><code class="python">def expand(self, amount: float) -&gt; None:
471483
&#34;&#34;&#34;Expand the location while keeping the center.
472484

473485
Args:
474-
amount (int): Amount of pixels to expand the location by.
486+
amount (float): Amount to expand the location by in multiples of the original size.
487+
475488

476489
Returns:
477490
None
478491
&#34;&#34;&#34;
479-
if amount != 0:
480-
self.x1 = self.x1 - amount
481-
self.y1 = self.y1 - amount
482-
self.x2 = self.x2 + amount
483-
self.y2 = self.y2 + amount</code></pre>
492+
assert amount &gt;= 0, &#34;Amount must be greater than or equal to 0.&#34;
493+
# if amount != 0:
494+
# self.x1 = self.x1 - amount
495+
# self.y1 = self.y1 - amount
496+
# self.x2 = self.x2 + amount
497+
# self.y2 = self.y2 + amount
498+
if amount != 0.0:
499+
self.x1 = self.x1 - int((self.x2 - self.x1) / 2 * amount)
500+
self.y1 = self.y1 - int((self.y2 - self.y1) / 2 * amount)
501+
self.x2 = self.x2 + int((self.x2 - self.x1) / 2 * amount)
502+
self.y2 = self.y2 + int((self.y2 - self.y1) / 2 * amount)</code></pre>
484503
</details>
485504
</dd>
486505
</dl>
487506
</dd>
488507
<dt id="facetorch.datastruct.Prediction"><code class="flex name class">
489508
<span>class <span class="ident">Prediction</span></span>
490-
<span>(</span><span>label: str = &lt;factory&gt;, logits: torch.Tensor = &lt;factory&gt;, other: Dict = &lt;factory&gt;)</span>
509+
<span>(</span><span>label: str = &lt;factory&gt;, logits: torch.Tensor = &lt;factory&gt;, other: Dict[~KT, ~VT] = &lt;factory&gt;)</span>
491510
</code></dt>
492511
<dd>
493512
<div class="desc"><p>Data class for face prediction results and derivatives.</p>
@@ -504,8 +523,7 @@ <h2 id="attributes">Attributes</h2>
504523
<summary>
505524
<span>Expand source code</span>
506525
</summary>
507-
<pre><code class="python">@dataclass
508-
class Prediction:
526+
<pre><code class="python">class Prediction:
509527
&#34;&#34;&#34;Data class for face prediction results and derivatives.
510528

511529
Attributes:
@@ -528,7 +546,7 @@ <h3>Class variables</h3>
528546
<dd>
529547
<div class="desc"></div>
530548
</dd>
531-
<dt id="facetorch.datastruct.Prediction.other"><code class="name">var <span class="ident">other</span> : Dict</code></dt>
549+
<dt id="facetorch.datastruct.Prediction.other"><code class="name">var <span class="ident">other</span> : Dict[~KT, ~VT]</code></dt>
532550
<dd>
533551
<div class="desc"></div>
534552
</dd>
@@ -557,8 +575,7 @@ <h2 id="attributes">Attributes</h2>
557575
<summary>
558576
<span>Expand source code</span>
559577
</summary>
560-
<pre><code class="python">@dataclass
561-
class Detection:
578+
<pre><code class="python">class Detection:
562579
&#34;&#34;&#34;Data class for detector output.
563580

564581
Attributes:
@@ -625,8 +642,7 @@ <h2 id="attributes">Attributes</h2>
625642
<summary>
626643
<span>Expand source code</span>
627644
</summary>
628-
<pre><code class="python">@dataclass
629-
class Face:
645+
<pre><code class="python">class Face:
630646
&#34;&#34;&#34;Data class for face attributes.
631647

632648
Attributes:
@@ -702,8 +718,7 @@ <h2 id="attributes">Attributes</h2>
702718
<summary>
703719
<span>Expand source code</span>
704720
</summary>
705-
<pre><code class="python">@dataclass
706-
class ImageData:
721+
<pre><code class="python">class ImageData:
707722
&#34;&#34;&#34;The main data class used for passing data between the different facetorch modules.
708723

709724
Attributes:
@@ -1045,8 +1060,7 @@ <h2 id="attributes">Attributes</h2>
10451060
<summary>
10461061
<span>Expand source code</span>
10471062
</summary>
1048-
<pre><code class="python">@dataclass
1049-
class Response:
1063+
<pre><code class="python">class Response:
10501064
&#34;&#34;&#34;Data class for response data, which is a subset of ImageData.
10511065

10521066
Attributes:

docs/facetorch/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4+
<meta name="google-site-verification" content="YIhgkma200wIq_XsAUQ9zFq2isMAdB5CUSrzqys38y4" />
45
<meta charset="utf-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
67
<meta name="generator" content="pdoc 0.10.0" />
@@ -453,6 +454,7 @@ <h4><code><a title="facetorch.FaceAnalyzer" href="#facetorch.FaceAnalyzer">FaceA
453454
</nav>
454455
</main>
455456
<footer id="footer">
457+
<p><a href="https://github.com/tomas-gajarsky/facetorch" title="facetorch: detect faces and analyze facial features using neural networks"><cite>facetorch GitHub repository</cite></a></p>
456458
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p>
457459
</footer>
458460
</body>

docs/facetorch/transforms.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ <h3>Ancestors</h3>
197197
<ul class="hlist">
198198
<li>torch.nn.modules.module.Module</li>
199199
</ul>
200+
<h3>Class variables</h3>
201+
<dl>
202+
<dt id="facetorch.transforms.SquarePad.dump_patches"><code class="name">var <span class="ident">dump_patches</span> : bool</code></dt>
203+
<dd>
204+
<div class="desc"></div>
205+
</dd>
206+
<dt id="facetorch.transforms.SquarePad.training"><code class="name">var <span class="ident">training</span> : bool</code></dt>
207+
<dd>
208+
<div class="desc"></div>
209+
</dd>
210+
</dl>
200211
<h3>Methods</h3>
201212
<dl>
202213
<dt id="facetorch.transforms.SquarePad.forward"><code class="name flex">
@@ -304,6 +315,8 @@ <h1>Index</h1>
304315
<h4><code><a title="facetorch.transforms.SquarePad" href="#facetorch.transforms.SquarePad">SquarePad</a></code></h4>
305316
<ul class="">
306317
<li><code><a title="facetorch.transforms.SquarePad.forward" href="#facetorch.transforms.SquarePad.forward">forward</a></code></li>
318+
<li><code><a title="facetorch.transforms.SquarePad.dump_patches" href="#facetorch.transforms.SquarePad.dump_patches">dump_patches</a></code></li>
319+
<li><code><a title="facetorch.transforms.SquarePad.training" href="#facetorch.transforms.SquarePad.training">training</a></code></li>
307320
</ul>
308321
</li>
309322
</ul>

0 commit comments

Comments
 (0)