arnaucube
arnaucube/galdricPublic Self-hosted repository
almost implemented k
arnaucode idoctnef@openmailbox.org
Thu, 29 Jun 2017 09:57:25 +0200
commit

c783acc32fed359d6f99fb37922b1f5729cce65d

parent

9d541b2f016c520aa64b8f3b8230561980a4c452

3 files changed, 79 insertions(+), 7 deletions(-)

jump to
M README.md → README.md

@@ -8,3 +8,27 @@ - Each image is resized to the same size, configured in the config.json

- For the input images, calculates the euclidean distances - Gets the nearest neighbour - Show the result, that is the label of the object in the image + + + + + + + + + + + + + +------------- + +send file over ssh: +``` +scp dataset.tar.gz root@51.255.193.106:/root/galdric +``` + +on the server, untar file: +``` +tar -xvzf dataset.tar.gz +```
M knn.go → knn.go

@@ -1,5 +1,10 @@

package main +import ( + "fmt" + "sort" +) + func euclideanDist(img1, img2 [][]float64) float64 { var dist float64 for i := 0; i < len(img1); i++ {

@@ -11,17 +16,55 @@

return dist } +type Neighbour struct { + Dist float64 + Label string +} + +func isNeighbour(neighbours []Neighbour, dist float64, label string) []Neighbour { + var temp []Neighbour + + for i := 0; i < len(neighbours); i++ { + temp = append(temp, neighbours[i]) + } + ntemp := Neighbour{dist, label} + temp = append(temp, ntemp) + + //now, sort the temp array + sort.Slice(temp, func(i, j int) bool { + return temp[i].Dist < temp[j].Dist + }) + + for i := 0; i < len(neighbours); i++ { + neighbours[i] = temp[i] + } + + return neighbours +} + func knn(dataset Dataset, input [][]float64) string { - d := euclideanDist(dataset["leopard"][0], input) - label := "lamp" - for k, v := range dataset { + k := 3 + var neighbours []Neighbour + //d := euclideanDist(dataset["leopard"][0], input) + for i := 0; i < k; i++ { + /*neighbours[i].Dist = euclideanDist(dataset["leopard"][0], input) + neighbours[i].Label = "leopard"*/ + neighbours = append(neighbours, Neighbour{euclideanDist(dataset["leopard"][0], input), "leopard"}) + } + for l, v := range dataset { for i := 0; i < len(v); i++ { dNew := euclideanDist(v[i], input) - if dNew < d { + /*if dNew < d { d = dNew - label = k - } + label = l + }*/ + neighbours = isNeighbour(neighbours, dNew, l) } } - return label + for i := 0; i < len(neighbours); i++ { + fmt.Print(neighbours[i].Label + " - ") + fmt.Println(neighbours[i].Dist) + } + + return neighbours[0].Label }
A tests.sh

@@ -0,0 +1,5 @@

+curl -F file=@./leop2.jpg http://127.0.0.1:3055/image + +curl -F file=@./test2.jpg http://127.0.0.1:3055/image + +curl -F file=@./test.jpg http://127.0.0.1:3055/image