part0
import numpy as np import cv2 def video_volume(image_list): '''Create a video volume from the image list. Input: image_list - a list of frames. Each element of the list contains a numpy array of a colored image. You may assume that each frame has the same shape, (rows, cols, 3). Output: output - a single 4d numpy array. This array should have dimensions (time, rows, cols, 3) and dtype np.uint8. ''' output = None # Insert your code here ------------------------------------------------------ (rows, cols, color) = image_list[0].shape output = np.zeros((len(image_list), rows, cols, color), dtype = np.uint8) for i in range(len(image_list)): output[i,:,:,:] = image_list[i] # ---------------------------------------------------------------------------- return output def ssd(video_volume): '''Compute the sum of squared distances for each pair of frames in video volume. Input: video_volume - as returned by your video_volume function. Output: output - a square 2d numpy array of dtype float. output[i,j] should contain the dsum of square differences between frames i and j. ''' output = None # Insert your code here ------------------------------------------------------ (frames, rows, cols, color) = video_volume.shape output = np.zeros((frames, frames), dtype = np.float) for i in range(frames): image_i = video_volume[i,:,:,:] for j in range(frames): image_j = video_volume[j,:,:,:] total = 0.0 diff_image = image_i - image_j for k1 in range(rows): for k2 in range(cols): for k3 in range(3): total = total + diff_image[k1,k2,k3] * diff_image[k1,k2,k3] output[i, j] = total # ---------------------------------------------------------------------------- return output def test(): '''This script will perform a unit test on your function, and provide useful output. ''' image_list1 = [np.array([[[ 0, 0, 0], [ 0, 0, 0]]], dtype = np.uint8), np.array([[[ 1, 1, 1], [ 1, 1, 1]]], dtype = np.uint8), np.array([[[ 2, 2, 2], [ 2, 2, 2]]], dtype = np.uint8), np.array([[[ 3, 3, 3], [ 3, 3, 3]]], dtype = np.uint8)] video_volume1 = np.array([[[[ 0, 0, 0], [ 0, 0, 0]]], [[[ 1, 1, 1], [ 1, 1, 1]]], [[[ 2, 2, 2], [ 2, 2, 2]]], [[[ 3, 3, 3], [ 3, 3, 3]]]], dtype = np.uint8) image_list2 =[np.array([[[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]], [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]], dtype = np.uint8), np.array([[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]], dtype = np.uint8), np.array([[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]], dtype = np.uint8)] video_volume2 = np.array([[[[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]], [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]], [[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]], dtype = np.uint8) diff1 = np.array([[ 0., 6., 24., 54.], [ 6., 0., 6., 24.], [ 24., 6., 0., 6.], [ 54., 24., 6., 0.]], dtype = np.float) diff2 = np.array([[ 0., 24., 96.], [ 24., 0., 24.], [ 96., 24., 0.]], dtype = np.float) if __name__ == "__main__": print 'Evaluating video_volume.' for img_list, true_out in zip((image_list1, image_list2), (video_volume1, video_volume2)): if __name__ == "__main__": print "input:\n{}\n".format(img_list) usr_out = video_volume(img_list) if not type(usr_out) == type(true_out): if __name__ == "__main__": print "Error- video_volume has type {}. Expected type is {}.".format( type(usr_out), type(true_out)) return False if not usr_out.shape == true_out.shape: if __name__ == "__main__": print "Error- video_volume has shape {}. Expected shape is {}.".format( usr_out.shape, true_out.shape) return False if not usr_out.dtype == true_out.dtype: if __name__ == "__main__": print "Error- video_volume has dtype {}. Expected dtype is {}.".format( usr_out.dtype, true_out.dtype) return False if not np.all(usr_out == true_out): if __name__ == "__main__": print "Error- video_volume has value:\n{}\nExpected value:\n{}".format( usr_out, true_out) return False if __name__ == "__main__": print "video_volume passed." print "evaluating ssd" for vid_volume, true_out in zip((video_volume1, video_volume2), (diff1, diff2)): if __name__ == "__main__": print "input:\n{}\n".format(vid_volume) usr_out = ssd(vid_volume) if not type(usr_out) == type(true_out): if __name__ == "__main__": print "Error- ssd has type {}. Expected type is {}.".format( type(usr_out), type(true_out)) return False if not usr_out.shape == true_out.shape: if __name__ == "__main__": print "Error- ssd has shape {}. Expected shape is {}.".format( usr_out.shape, true_out.shape) return False if not usr_out.dtype == true_out.dtype: if __name__ == "__main__": print "Error- ssd has dtype {}. Expected dtype is {}.".format( usr_out.dtype, true_out.dtype) return False if not np.all(np.abs(usr_out - true_out) < 1.): if __name__ == "__main__": print "Error- ssd has value:\n{}\nExpected value:\n{}".format( usr_out, true_out) return False if __name__ == "__main__": print "All unit tests successful." return True if __name__ == "__main__": print "Performing unit tests.(ssd will be accepted if the output is within 1 of the expected output.)" np.set_printoptions(precision=1) test()
part1
import numpy as np import cv2 import scipy.signal def binomial_filter_5(): ''' Create a binomial filter of length 4. ''' return np.array([ 0.0625, 0.25 , 0.375 , 0.25 , 0.0625], dtype=float) def diff2(diff1): '''Compute the transition costs between frames, taking dynamics into account. Input: diff1 - a difference matrix as produced by your ssd function. Output: output - a new difference matrix that takes preceding and following frames into account. The cost of transitioning from i to j should weight the surrounding frames according to the binomial filter provided to you above. So, the difference between i and j has weight 0.375, the frames immediately following weight 0.25, etc... The output difference matrix should have the same dtype as the input, but be 4 rows and columns smaller, corresponding to only the frames that have valid dynamics. Hint: there is a very efficient way to do this with 2d convolution. Think about the coordinates you are using as you consider the preceding and following frame pairings. ''' output = None # Insert your code here ------------------------------------------------------ w_1d = binomial_filter_5() (w_size) = w_1d.shape (rows, cols) = diff1.shape output = np.zeros((rows - 4, cols - 4), dtype = np.float) for i in range(2, rows - 2, 1): for j in range(2, cols - 2, 1): total = 0.0; for k in range(-2, 3, 1): total = total + diff1[i + k, j + k]*w_1d[k + 2] output[i - 2, j - 2] = total #kernel = np.outer(w_1d, w_1d) #output = scipy.signal.convolve2d(diff1, kernel, 'valid') # ---------------------------------------------------------------------------- return output def test(): '''This script will perform a unit test on your function, and provide useful output. ''' d1 = np.zeros((9,9), dtype = float) d1[4,4] = 1 d2 = np.eye(5, dtype = float) out1 = np.array([[ 0.0625, 0. , 0. , 0. , 0. ], [ 0. , 0.25 , 0. , 0. , 0. ], [ 0. , 0. , 0.375 , 0. , 0. ], [ 0. , 0. , 0. , 0.25 , 0. ], [ 0. , 0. , 0. , 0. , 0.0625]], dtype = float) out2 = np.array([[1.]], dtype = float) if __name__ == "__main__": print 'Evaluating diff2.' for d, true_out in zip((d1, d2), (out1, out2)): if __name__ == "__main__": print "input:\n{}\n".format(d) usr_out = diff2(d) if not type(usr_out) == type(true_out): if __name__ == "__main__": print "Error- diff2 output has type {}. Expected type is {}.".format( type(usr_out), type(true_out)) return False if not usr_out.shape == true_out.shape: if __name__ == "__main__": print "Error- diff2 output has shape {}. Expected shape is {}.".format( usr_out.shape, true_out.shape) return False if not usr_out.dtype == true_out.dtype: if __name__ == "__main__": print "Error- diff2 output has dtype {}. Expected dtype is {}.".format( usr_out.dtype, true_out.dtype) return False if not np.all(np.abs(usr_out - true_out) < 0.05): if __name__ == "__main__": print "Error- diff2 output has value:\n{}\nExpected value:\n{}".format( usr_out, true_out) return False if __name__ == "__main__": print "diff2 passed." if __name__ == "__main__": print "All unit tests successful." return True if __name__ == "__main__": print "Performing unit tests. (Your output will be accepted if it is within .05 of the true output)" np.set_printoptions(precision=1) test()
part2
import numpy as np import cv2 import sys import scipy.signal def find_biggest_loop(diff2, alpha): '''Given the difference matrix, find the longest and smoothest loop that we can. Inputs: diff2 - a square 2d numpy array of dtype float. Each cell contains the cost of transitioning from frame i to frame j in the input video. alpha - a parameter for how heavily you should weigh the size of the loop relative to the transition cost of the loop. Larger alphas favor longer loops. Outputs: s - the beginning frame of the longest loop. f - the final frame of the longest loop. s, f will the indices in the diff2 matrix that give the maximum score according to the following metric: alpha*(f-s) - diff2[f,s] ''' s = None f = None # Insert your code here ------------------------------------------------------ (rows, cols) = diff2.shape maxValue = -10000 for ss in range(rows): for ff in range(cols): val = alpha*(ff-ss) - diff2[ff,ss] if val > maxValue: maxValue = val s = ss f = ff # ---------------------------------------------------------------------------- return s, f def synthesize_loop(video_volume, s, f): '''Pull out the given loop from video. Input: video_volume - a (t, row, col, 3) array, as created by your video_volume function. i - the index of the starting frame. j - the index of the ending frame. Output: output - a list of arrays of size (row, col, 3) and dtype np.uint8, similar to the original input the video_volume function in part0. ''' output = [] # Insert your code here ------------------------------------------------------ for i in range(s, f + 1, 1): output.append(video_volume[i, :, :, :]) # ---------------------------------------------------------------------------- return output def test(): '''This script will perform a unit test on your function, and provide useful output. ''' d1 = np.ones((5,5), dtype = float) a1 = 1 out1 = (0,4) d2 = np.array([[ 0., 1., 1., 5.], [ 1., 0., 3., 4.], [ 1., 3., 0., 5.], [ 5., 4., 5., 0.]]) a2 = 1 out2 = (0,2) d3 = np.array([[ 0., 1., 4.], [ 1., 0., 1.], [ 4., 1., 0.]]) a3 = 2 out3 = (0,1) if __name__ == "__main__": print 'Evaluating find_biggest_loop' for d, a, true_out in zip((d1, d2, d3), (a1, a2, a3), (out1, out2, out3)): if __name__ == "__main__": print "input:\n{}\n".format(d) print "alpha = {}".format(a) usr_out = find_biggest_loop(d, a) if not usr_out == true_out: if __name__ == "__main__": print "Error- find_biggest_loop is {}. Expected output is {}.".format( usr_out, true_out) return False if __name__ == "__main__": print "find_biggest_loop passed." print "testing synthesize_loop." video_volume1 = np.array([[[[ 0, 0, 0], [ 0, 0, 0]]], [[[ 1, 1, 1], [ 1, 1, 1]]], [[[ 2, 2, 2], [ 2, 2, 2]]], [[[ 3, 3, 3], [ 3, 3, 3]]]], dtype = np.uint8) video_volume2 = np.array([[[[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]], [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]], [[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]], [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]], dtype = np.uint8) frames1 = (2,3) frames2 = (1,1) out1 = [np.array([[[ 2, 2, 2], [ 2, 2, 2]]], dtype = np.uint8), np.array([[[ 3, 3, 3], [ 3, 3, 3]]], dtype = np.uint8)] out2 = [np.array([[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]], dtype = np.uint8)] for video_volume, frames, true_out in zip((video_volume1, video_volume2), (frames1, frames2), (out1, out2)): if __name__ == "__main__": print "input:\n{}\n".format(video_volume) print "input frames:\n{}\n".format(frames) usr_out = synthesize_loop(video_volume, frames[0], frames[1]) if not type(usr_out) == type(true_out): if __name__ == "__main__": print "Error- synthesize_loop has type {}. Expected type is {}.".format( type(usr_out), type(true_out)) return False if not len(usr_out) == len(true_out): if __name__ == "__main__": print "Error - synthesize_loop has len {}. Expected len is {}.".format( len(usr_out), len(true_out)) return False for usr_img, true_img in zip(usr_out, true_out): if not type(usr_img) == type(true_img): if __name__ == "__main__": print "Error- synthesize_loop has type {}. Expected type is {}.".format( type(usr_img), type(true_img)) return False if not usr_img.shape == true_img.shape: if __name__ == "__main__": print "Error- synthesize_loop has shape {}. Expected shape is {}.".format( usr_img.shape, true_img.shape) return False if not usr_img.dtype == true_img.dtype: if __name__ == "__main__": print "Error- synthesize_loop has dtype {}. Expected dtype is {}.".format( usr_img.dtype, true_img.dtype) return False if not np.all(usr_img == true_img): if __name__ == "__main__": print "Error- synthesize_loop has value:\n{}\nExpected value:\n{}".format( usr_img, true_img) return False if __name__ == "__main__": print "synthesize_loop passed." if __name__ == "__main__": print "All unit tests successful." return True if __name__ == "__main__": print "Performing unit tests." test()