Skip to content

Commit

Permalink
Done HW6
Browse files Browse the repository at this point in the history
  • Loading branch information
VidocqH committed Jan 6, 2021
1 parent 8236bf0 commit 12ece5b
Show file tree
Hide file tree
Showing 3 changed files with 592 additions and 77 deletions.
54 changes: 47 additions & 7 deletions hw6_release/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def hog_feature(image, pixel_per_cell=8):
hog_image: an image representation of hog provided by skimage.
"""
### YOUR CODE HERE
pass
pixels_per_cell = (pixel_per_cell, pixel_per_cell)
hog_feature, hog_image = feature.hog(image, pixels_per_cell=pixels_per_cell, block_norm='L1', visualize=True)
### END YOUR CODE
return (hog_feature, hog_image)

Expand Down Expand Up @@ -72,7 +73,13 @@ def sliding_window(image, template_feature, step_size, window_size, pixel_per_ce
for c in range(0, W, step_size):
score = 0
### YOUR CODE HERE
pass
cur_win = pad_image[ r:r+winH, c:c+winW ]
hog_feature = feature.hog(cur_win, pixels_per_cell=(pixel_per_cell, pixel_per_cell))
score = hog_feature.dot(template_feature)
if score > max_score:
max_score = score
maxr = r
maxc = c
### END YOUR CODE
response_map[(r) // step_size, (c) // step_size] = score

Expand Down Expand Up @@ -112,7 +119,9 @@ def pyramid(image, scale=0.9, min_size=(200, 100)):
while True:
# Use "break" to exit this loop when termination conditions are met.
### YOUR CODE HERE
pass
H, W = image.shape
if H < min_size[0] and H < min_size[1]:
break
### END YOUR CODE

# Compute the new dimensions of the image and resize it
Expand Down Expand Up @@ -150,7 +159,15 @@ def pyramid_score(image, template_feature, shape, step_size=20,

images = pyramid(image, scale)
### YOUR CODE HERE
pass
for each_image in images:
res = sliding_window(each_image[1], template_feature, \
step_size=step_size, window_size=shape, pixel_per_cell=pixel_per_cell)
if res[0] > max_score:
max_score = res[0]
maxr = res[1]
maxc = res[2]
max_response_map = res[3]
max_scale = each_image[0]
### END YOUR CODE
return max_score, maxr, maxc, max_scale, max_response_map

Expand Down Expand Up @@ -182,7 +199,10 @@ def compute_displacement(part_centers, face_shape):
"""
d = np.zeros((part_centers.shape[0], 2))
### YOUR CODE HERE
pass
face_centers = np.array([face_shape[0] / 2, face_shape[1] / 2], dtype=np.int32)
d = part_centers - face_centers
mu = np.abs(np.mean(d, axis=0))
sigma = np.std(d, axis=0)
### END YOUR CODE
return mu, sigma

Expand All @@ -208,7 +228,10 @@ def shift_heatmap(heatmap, mu):
"""
### YOUR CODE HERE
heatmap = np.copy(heatmap)
pass
# Normalize heatmap
heatmap /= np.max(heatmap, axis=None)
# Shift heatmap
new_heatmap = interpolation.shift(heatmap, mu)
### END YOUR CODE
return new_heatmap

Expand Down Expand Up @@ -237,7 +260,24 @@ def gaussian_heatmap(heatmap_face, heatmaps, sigmas):
heatmaps = list(np.copy(heatmaps))
sigmas = list(np.copy(sigmas))
### YOUR CODE HERE
pass
heatmap = heatmaps[0]
maxVal = 0
maxr = 0
maxc = 0
H, W = heatmap.shape
for i in range(len(heatmaps)):
# Apply gaussian filter
heatmaps[i] = gaussian(heatmaps[i], sigma=sigmas[i])
# Add filtered heatmaps with face heatmap
heatmaps[i] += heatmap_face
idx = np.argmax(heatmaps[i], axis=None)
r = int(idx / W)
c = idx - (r * W)
if heatmaps[i][r, c] > maxVal:
maxVal = heatmaps[i][r, c]
maxr = r
maxc = c
heatmap = heatmaps[i]
### END YOUR CODE
return heatmap, maxr, maxc

Expand Down
592 changes: 526 additions & 66 deletions hw6_release/hw6.ipynb

Large diffs are not rendered by default.

23 changes: 19 additions & 4 deletions hw6_release/k_nearest_neighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def compute_distances(X1, X2):
# in particular you should not use functions from scipy.
#
# HINT: Try to formulate the l2 distance using matrix multiplication

pass
X1_square = np.sum(X1 ** 2, axis=1)
X2_square = np.sum(X2 ** 2, axis=1)
# (x-y)^2 == x^2 + y^2 -2xy
dists = np.sqrt(X1_square.reshape(-1, 1) + X2_square - 2 * X1.dot(X2.T))
# END YOUR CODE

assert dists.shape == (M, N), "dists should have shape (M, N), got %s" % dists.shape
Expand Down Expand Up @@ -62,7 +64,13 @@ def predict_labels(dists, y_train, k=1):
# Hint: Look up the functions numpy.argsort and numpy.bincount

# YOUR CODE HERE
pass
for i in range(num_test):
# Find the k nearest neighbors of the ith testing point
knn = np.argsort(dists[i])[0:k]
# Find the labels of neighbors
closest_y = y_train[knn[:]]
idxArr = np.bincount(closest_y)
y_pred[i] = np.argmax(idxArr)
# END YOUR CODE

return y_pred
Expand Down Expand Up @@ -112,7 +120,14 @@ def split_folds(X_train, y_train, num_folds):

# YOUR CODE HERE
# Hint: You can use the numpy array_split function.
pass
X_lst = np.array_split(X_train, num_folds, axis=0)
y_train = y_train.reshape(-1, 1)
y_lst = np.array_split(y_train, num_folds, axis=0)
for i in range(num_folds):
X_vals[i, :, :] = X_lst[i]
X_trains[i, :, :] = np.vstack(X_lst[:i] + X_lst[i+1:])
y_vals[i, :] = y_lst[i][:, 0]
y_trains[i, :] = np.vstack(y_lst[:i] + y_lst[i+1:])[:, 0]
# END YOUR CODE

return X_trains, y_trains, X_vals, y_vals

0 comments on commit 12ece5b

Please sign in to comment.