Browse Source

almost implemented k

master
arnaucode 6 years ago
parent
commit
c783acc32f
3 changed files with 79 additions and 7 deletions
  1. +24
    -0
      README.md
  2. +50
    -7
      knn.go
  3. +5
    -0
      tests.sh

+ 24
- 0
README.md

@ -8,3 +8,27 @@ machine learning server, for image classification
- 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
```

+ 50
- 7
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 @@ func euclideanDist(img1, img2 [][]float64) float64 {
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
}

+ 5
- 0
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

Loading…
Cancel
Save