Skip to content

Commit 63fa7b2

Browse files
committed
Add: -j / --jobs flag - Allow parallelization, default is 1 parallel job.
1 parent d76675b commit 63fa7b2

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ For more informations, refeer to the helper:
4545
4646
[-u|--url] is required
4747
usage: deepsort [-h|--help] -u|--url "<value>" -i|--input "<value>"
48-
[-d|--dry-run] [-R|--recursive]
48+
[-R|--recursive] [-j|--jobs <integer>] [-d|--dry-run]
4949
5050
AI powered image tagger backed by DeepDetect
5151
@@ -54,8 +54,9 @@ Arguments:
5454
-h --help Print help information
5555
-u --url URL of your DeepDetect instance (i.e: http://localhost:8080)
5656
-i --input Your input folder.
57-
-d --dry-run Just classify images and return results, do not apply.
5857
-R --recursive Process files recursively.
58+
-j --jobs Number of parallel jobs. Default: 1
59+
-d --dry-run Just classify images and return results, do not apply.
5960
```
6061

6162
# (Really really quick) Benchmark

cmd/deepsort/arguments.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ func argumentParsing(args []string, argument *Arguments) {
1414
// Create flags
1515
URL := parser.String("u", "url", &argparse.Options{Required: true, Help: "URL of your DeepDetect instance (i.e: http://localhost:8080)"})
1616
input := parser.String("i", "input", &argparse.Options{Required: true, Help: "Your input folder."})
17-
dryRun := parser.Flag("d", "dry-run", &argparse.Options{Required: false, Help: "Just classify images and return results, do not apply."})
1817
recursive := parser.Flag("R", "recursive", &argparse.Options{Required: false, Help: "Process files recursively."})
18+
jobs := parser.Int("j", "jobs", &argparse.Options{Required: false, Help: "Number of parallel jobs", Default: 1})
19+
dryRun := parser.Flag("d", "dry-run", &argparse.Options{Required: false, Help: "Just classify images and return results, do not apply."})
1920
// Parse input
2021
err := parser.Parse(os.Args)
2122
if err != nil {
@@ -27,6 +28,7 @@ func argumentParsing(args []string, argument *Arguments) {
2728
// Handle the input flag
2829
inputFolder, _ := filepath.Abs(*input)
2930
// Finally save the collected flags
31+
argument.Jobs = *jobs
3032
argument.DryRun = *dryRun
3133
argument.Recursive = *recursive
3234
argument.Input = inputFolder

cmd/deepsort/classify.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"net/http"
77
"os"
88
"path/filepath"
9+
"sync"
910

1011
"github.com/CorentinB/DeepSort/pkg/logging"
1112
"github.com/labstack/gommon/color"
1213
)
1314

14-
func googleNetClassification(path string, arguments *Arguments) {
15+
func googleNetClassification(path string, arguments *Arguments, wg *sync.WaitGroup) {
16+
defer wg.Done()
1517
url := arguments.URL + "/predict"
1618
path, _ = filepath.Abs(path)
1719
var jsonStr = []byte(`{"service":"deepsort-googlenet","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + path + `"]}`)

cmd/deepsort/main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,28 @@ type Arguments struct {
1313
URL string
1414
DryRun bool
1515
Recursive bool
16+
Jobs int
1617
}
1718

1819
func main() {
1920
start := time.Now()
2021
arguments := new(Arguments)
22+
arguments.Jobs = 1
2123
argumentParsing(os.Args, arguments)
2224
startGoogleNet(arguments)
2325
if arguments.Recursive == true {
24-
logging.Success("Starting image classification recursively..", "[GoogleNet]")
26+
if arguments.Jobs == 1 {
27+
logging.Success("Starting image classification recursively..", "[GoogleNet]")
28+
} else {
29+
logging.Success("Starting image classification recursively with "+string(arguments.Jobs)+" parallel jobs..", "[GoogleNet]")
30+
}
2531
runRecursively(arguments)
2632
} else {
27-
logging.Success("Starting image classification..", "[GoogleNet]")
33+
if arguments.Jobs == 1 {
34+
logging.Success("Starting image classification..", "[GoogleNet]")
35+
} else {
36+
logging.Success("Starting image classification with "+string(arguments.Jobs)+" parallel jobs..", "[GoogleNet]")
37+
}
2838
run(arguments)
2939
}
3040
color.Println(color.Cyan("Done in ") + color.Yellow(time.Since(start)) + color.Cyan("!"))

cmd/deepsort/process.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import (
55
"log"
66
"os"
77
"path/filepath"
8+
"sync"
89

910
"github.com/CorentinB/DeepSort/pkg/logging"
1011
filetype "gopkg.in/h2non/filetype.v1"
1112
)
1213

1314
func run(arguments *Arguments) {
15+
// Handle parallelization
16+
count := 0
17+
var wg sync.WaitGroup
18+
// Open input folder
1419
f, err := os.Open(arguments.Input)
1520
if err != nil {
1621
log.Fatal(err)
@@ -22,16 +27,27 @@ func run(arguments *Arguments) {
2227
os.Exit(1)
2328
}
2429

30+
// Process files in the input folder
2531
for _, file := range files {
2632
path := arguments.Input + "/" + file.Name()
2733
buf, _ := ioutil.ReadFile(path)
2834
if filetype.IsImage(buf) {
29-
googleNetClassification(path, arguments)
35+
count++
36+
wg.Add(1)
37+
go googleNetClassification(path, arguments, &wg)
38+
if count == arguments.Jobs {
39+
wg.Wait()
40+
count = 0
41+
}
3042
}
3143
}
3244
}
3345

3446
func runRecursively(arguments *Arguments) ([]string, error) {
47+
// Handle parallelization
48+
count := 0
49+
var wg sync.WaitGroup
50+
// Open input folder
3551
fileList := make([]string, 0)
3652
e := filepath.Walk(arguments.Input, func(path string, f os.FileInfo, err error) error {
3753
fileList = append(fileList, path)
@@ -46,7 +62,13 @@ func runRecursively(arguments *Arguments) ([]string, error) {
4662
for _, file := range fileList {
4763
buf, _ := ioutil.ReadFile(file)
4864
if filetype.IsImage(buf) {
49-
googleNetClassification(file, arguments)
65+
count++
66+
wg.Add(1)
67+
go googleNetClassification(file, arguments, &wg)
68+
}
69+
if count == arguments.Jobs {
70+
wg.Wait()
71+
count = 0
5072
}
5173
}
5274

0 commit comments

Comments
 (0)