diff --git a/README.md b/README.md index 32b3423..b63d02e 100644 --- a/README.md +++ b/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 +``` \ No newline at end of file diff --git a/knn.go b/knn.go index 7f888cb..d923892 100644 --- a/knn.go +++ b/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 } diff --git a/tests.sh b/tests.sh new file mode 100644 index 0000000..c224aed --- /dev/null +++ b/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