src/utils.py
def conv2mat(im_shape,kernel,format="channels_first",dtype=_dtype):
	"""
	2D convolution (correlation) matrix with zero boundary conditions
	"""
	if len(im_shape)==2:
		im_size_y, im_size_x = im_shape
		num_channels = 1
	else:
		if format=="channels_last":
			im_size_y, im_size_x, num_channels = im_shape
		else:
			num_channels, im_size_y, im_size_x = im_shape

	ker_size_y, ker_size_x = kernel.shape
	ker_size = kernel.size
	kernel = kernel.ravel()

	mat_size_x = im_size_x * im_size_y
	mat = sp.dia_matrix( (mat_size_x,mat_size_x), dtype=dtype )

	# diagonal blocks corresponding to the rows of the kernel
	for j in range(-(ker_size_y//2),ker_size_y//2+1):
		# diagonal of the block corresponding to the given row of the kernel
		# correlation
		diag = sp.eye(im_size_y,im_size_y,-j,dtype=dtype)
		# # convolution
		# diag = sp.eye(im_size_y,im_size_y,-j,dtype=dtype)

		# contribution from convolution matrix corresponding to the given row of the kernel
		mat += sp.kron( diag, convmat(im_size_x,kernel[ker_size//2-ker_size_x//2-j*ker_size_x:ker_size//2-ker_size_x//2-(j-1)*ker_size_x],dtype=dtype) ) #.astype(dtype=dtype)

	if num_channels>1:
		if format=="channels_last":
			diag_ch = sp.eye(num_channels,num_channels,dtype=dtype)
			return sp.kron(mat,diag_ch) #.tocsr()
		else:
			diag_ch = sp.eye(num_channels,num_channels,dtype=dtype)
			return sp.kron(diag_ch,mat) #.tocsr()

	return mat

	2D convolution (correlation) matrix with zero boundary conditions
	
2D convolution (correlation) matrix with zero boundary conditions
2D convolution ( correlation ) matrix with zero boundary conditions
def conv2mat(im_shape,kernel,format="channels_first",dtype=_dtype):
	if len(im_shape)==2:
		im_size_y, im_size_x = im_shape
		num_channels = 1
	else:
		if format=="channels_last":
			im_size_y, im_size_x, num_channels = im_shape
		else:
			num_channels, im_size_y, im_size_x = im_shape
	ker_size_y, ker_size_x = kernel.shape
	ker_size = kernel.size
	kernel = kernel.ravel()
	mat_size_x = im_size_x * im_size_y
	mat = sp.dia_matrix( (mat_size_x,mat_size_x), dtype=dtype )
	for j in range(-(ker_size_y//2),ker_size_y//2+1):
		diag = sp.eye(im_size_y,im_size_y,-j,dtype=dtype)
		mat += sp.kron( diag, convmat(im_size_x,kernel[ker_size//2-ker_size_x//2-j*ker_size_x:ker_size//2-ker_size_x//2-(j-1)*ker_size_x],dtype=dtype) ) 
	if num_channels>1:
		if format=="channels_last":
			diag_ch = sp.eye(num_channels,num_channels,dtype=dtype)
			return sp.kron(mat,diag_ch) 
		else:
			diag_ch = sp.eye(num_channels,num_channels,dtype=dtype)
			return sp.kron(diag_ch,mat) 
	return mat
def conv2mat ( im_shape , kernel , format = "channels_first" , dtype = _dtype ) : if len ( im_shape ) == 2 : im_size_y , im_size_x = im_shape num_channels = 1 else : if format == "channels_last" : im_size_y , im_size_x , num_channels = im_shape else : num_channels , im_size_y , im_size_x = im_shape ker_size_y , ker_size_x = kernel . shape ker_size = kernel . size kernel = kernel . ravel ( ) mat_size_x = im_size_x * im_size_y mat = sp . dia_matrix ( ( mat_size_x , mat_size_x ) , dtype = dtype ) for j in range ( - ( ker_size_y // 2 ) , ker_size_y // 2 + 1 ) : diag = sp . eye ( im_size_y , im_size_y , - j , dtype = dtype ) mat += sp . kron ( diag , convmat ( im_size_x , kernel [ ker_size // 2 - ker_size_x // 2 - j * ker_size_x : ker_size // 2 - ker_size_x // 2 - ( j - 1 ) * ker_size_x ] , dtype = dtype ) ) if num_channels > 1 : if format == "channels_last" : diag_ch = sp . eye ( num_channels , num_channels , dtype = dtype ) return sp . kron ( mat , diag_ch ) else : diag_ch = sp . eye ( num_channels , num_channels , dtype = dtype ) return sp . kron ( diag_ch , mat ) return mat
2D convolution (correlation) matrix with zero boundary conditions
2D convolution ( correlation ) matrix with zero boundary conditions
"""
	2D convolution (correlation) matrix with zero boundary conditions
	""" # diagonal blocks corresponding to the rows of the kernel # diagonal of the block corresponding to the given row of the kernel # correlation # # convolution # diag = sp.eye(im_size_y,im_size_y,-j,dtype=dtype) # contribution from convolution matrix corresponding to the given row of the kernel #.astype(dtype=dtype) #.tocsr() #.tocsr()
im_shape
kernel
format
dtype