Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
Image Classification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Massardier Matthieu
Image Classification
Commits
6e2e532b
Unverified
Commit
6e2e532b
authored
Nov 9, 2023
by
Jangberry (Nomad-Debian)
Browse files
Options
Downloads
Patches
Plain Diff
Pep8
parent
cab6108d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.vscode/settings.json
+4
-0
4 additions, 0 deletions
.vscode/settings.json
README.md
+13
-1
13 additions, 1 deletion
README.md
knn.py
+25
-16
25 additions, 16 deletions
knn.py
requirements.txt
+2
-1
2 additions, 1 deletion
requirements.txt
with
44 additions
and
18 deletions
.vscode/settings.json
0 → 100644
+
4
−
0
View file @
6e2e532b
{
"python.analysis.autoImportCompletions"
:
true
,
"python.analysis.typeCheckingMode"
:
"off"
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
README.md
+
13
−
1
View file @
6e2e532b
...
...
@@ -3,10 +3,22 @@
## Setup
1.
Download the
[
CIFAR dataset
](
https://www.cs.toronto.edu/~kriz/cifar.html
)
`cifar-10-batches-py`
in the
[
data
](
./data/
)
folder.
1.
You might want to create a venv with
`python -m venv .venv`
and activate it with
`source .venv/bin/activate`
.
1.
(
*optionnal*
)
You might want to create a venv with
`python -m venv .venv`
and activate it with
`source .venv/bin/activate`
.
1.
Install the requirements using
`pip install -r requirements.txt`
.
## Usage
To test knn, simply run
[
knn.py
](
./knn.py
)
using
`python knn.py`
.
Otherwise here is a test result: !
[
knn test result
](
./results/knn.png
)
## Some proofs
### *1. Prove that $`\sigma' = \sigma \times (1-\sigma)`$*
To prove that, let's firt derive the sigmoid function:
$
\s
igma(x) =
\f
rac{1}{1+e^{-x}}$
so $
\s
igma'(x)=
\f
rac{e^{-x}}{(1+e^{-x})^2}$
$
\s
igma'(x)=
\f
rac{1}{1+e^{-x}}
\t
imes
\f
rac{e^{-x}}{1+e^{-x}}$
Here we can identify $
\f
rac{1}{1+e^{-x}} =
\s
igma(x)$
And $
\f
rac{e^{-x}}{1+e^{-x}} = 1 -
\f
rac{1}{1+e^{x}}$
So $
\s
igma'(x) =
\s
igma(x)
\t
imes (1 -
\s
igma(x))$
\ No newline at end of file
This diff is collapsed.
Click to expand it.
knn.py
+
25
−
16
View file @
6e2e532b
import
read_cifar
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
numpy
as
np
import
read_cifar
def
distance_matrix
(
X
:
np
.
array
,
Y
:
np
.
array
):
"""
Compute the L2 distance between two matricies
...
...
@@ -12,9 +14,12 @@ def distance_matrix(X: np.array, Y: np.array):
dist -- distance matrix (shape: (X.shape[0], Y.shape[0])) => dist[i, j] = L2(X[i], Y[j])
"""
return
np
.
sqrt
(
np
.
sum
(
np
.
square
(
X
),
axis
=
1
).
reshape
((
-
1
,
1
))
@np.ones
((
1
,
Y
.
shape
[
0
]))
+
# X²
np
.
ones
((
X
.
shape
[
0
],
1
))
@np.sum
(
np
.
square
(
Y
),
axis
=
1
).
reshape
((
1
,
-
1
))
# Y²
-
2
*
X
@Y.T
)
# -2XY
# X² + Y² - 2XY
np
.
sum
(
np
.
square
(
X
),
axis
=
1
).
reshape
((
-
1
,
1
))
@np.ones
((
1
,
Y
.
shape
[
0
]))
+
np
.
ones
((
X
.
shape
[
0
],
1
))
@np.sum
(
np
.
square
(
Y
),
axis
=
1
).
reshape
((
1
,
-
1
))
-
2
*
X
@Y.T
)
def
knn_predict
(
dist
:
np
.
array
,
labels_train
:
np
.
array
,
k
:
int
):
"""
Predict the labels of the test set using the k-nearest neighbors algorithm
...
...
@@ -31,10 +36,12 @@ def knn_predict(dist: np.array, labels_train: np.array, k: int):
# Get the labels of the k nearest neighbors
labels
=
labels_train
[
indices
]
# Get the most frequent label
labels
=
np
.
apply_along_axis
(
lambda
x
:
np
.
bincount
(
x
).
argmax
(),
axis
=
1
,
arr
=
labels
)
labels
=
np
.
apply_along_axis
(
lambda
x
:
np
.
bincount
(
x
).
argmax
(),
axis
=
1
,
arr
=
labels
)
return
labels
def
evaluate_knn
(
data_train
:
np
.
array
,
labels_train
:
np
.
array
,
data_test
:
np
.
array
,
labels_test
:
np
.
array
,
k
:
int
,
dist
=
None
):
"""
Evaluate the k-nearest neighbors algorithm
...
...
@@ -62,7 +69,8 @@ if __name__ == "__main__":
# Split the data into training and testing sets
print
(
"
Splitting sets
"
)
images_train
,
labels_train
,
images_test
,
labels_test
=
read_cifar
.
split_dataset
(
images
,
labels
,
split_factor
)
images_train
,
labels_train
,
images_test
,
labels_test
=
read_cifar
.
split_dataset
(
images
,
labels
,
split_factor
)
# Compute the distance matrix
print
(
"
Computing the distance matrix...
"
)
...
...
@@ -72,7 +80,8 @@ if __name__ == "__main__":
accuracies
=
[]
# List of the accuracy
ks
=
[]
# List to make sure the plot starts at one and not 0
for
k
in
range
(
1
,
21
):
accuracy
=
evaluate_knn
(
images_train
,
labels_train
,
images_test
,
labels_test
,
k
)
accuracy
=
evaluate_knn
(
images_train
,
labels_train
,
images_test
,
labels_test
,
k
)
print
(
f
"
Accuracy for k =
{
k
}
:
{
accuracy
}
"
)
accuracies
.
append
(
accuracy
)
ks
.
append
(
k
)
...
...
This diff is collapsed.
Click to expand it.
requirements.txt
+
2
−
1
View file @
6e2e532b
numpy
matplotlib
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment