-
Notifications
You must be signed in to change notification settings - Fork 3
/
svm.py
66 lines (48 loc) · 1.39 KB
/
svm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
import matplotlib.pyplot as plt
from utils import load_image, whiten
def X(im, i, j):
""" Return 3x3 image features. Assume that 0 < i < H, 0 < j < W"""
return im[i - 1:i + 2, j - 1:j + 2].reshape(-1, 3)
def loss_and_grad(w, y, x, lamb=0.0001):
# Type casting
x = x.reshape(-1)
wdotx = w.dot(x)
grad = lamb * w
if 1 > y * wdotx:
grad -= (y * x)
loss = max(0, 1 - y * wdotx) + lamb * np.sum(w ** 2)
return loss, grad
def predict(wim, w):
H, W = wim.shape[:2]
pred = np.zeros((H, W))
for i in range(1, H - 1):
for j in range(1, W - 1):
pred[i, j] = w.dot(X(wim, i, j).reshape(-1))
return pred
im = load_image('cat1.jpg')
im2 = load_image('cat2.jpg')
y = load_image('cat1_label.png').astype('int')
y = y * 2 - 1 # {-1, 1}^{H \times W}
H, W = im.shape[:2]
MAX_ITER = 2000
# Whiten the image
whitened_im = whiten(im)
whitened_im2 = whiten(im2)
# Define weight
w = np.random.rand(27) / 27
for curr_iter in range(MAX_ITER + 1):
# Get random image patch
i = np.random.randint(1, H - 1)
j = np.random.randint(1, W - 1)
# Get loss and gradient
loss, grad = loss_and_grad(w, y[i, j], X(whitened_im, i, j))
w -= 0.001 * grad
if curr_iter % 50 == 0:
print("Iter: {}\tLoss: {:.4}".format(curr_iter, loss))
if curr_iter % 1000 == 0:
pred = predict(whitened_im2, w)
plt.imshow(pred)
plt.clim(-1, 1)
plt.axis('off')
plt.show()