#NEWLINE# def visualize_grid( Xs , ubound = 255.0 , padding = 1 , grid_size = None ) : #NEWLINE# #INDENT# '\n    Reshape a 4D tensor of image data to a grid for easy visualization.\n\n    Inputs:\n    - Xs: Data of shape (N, H, W, C)\n    - ubound: Output grid will have values scaled to the range [0, ubound]\n    - padding: The number of blank pixels between elements of the grid\n    ' #NEWLINE# ( N , H , W , C ) = Xs . shape #NEWLINE# if ( grid_size is None ) : #NEWLINE# #INDENT# grid_size_y = grid_size_x = int ( ceil ( sqrt ( N ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type ( grid_size ) == tuple ) : #NEWLINE# #INDENT# grid_size_x = grid_size [ 0 ] #NEWLINE# grid_size_y = grid_size [ 1 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# grid_size_y = grid_size_x = grid_size #NEWLINE# #UNINDENT# #UNINDENT# grid_height = ( ( H * grid_size_y ) + ( padding * ( grid_size_y - 1 ) ) ) #NEWLINE# grid_width = ( ( W * grid_size_x ) + ( padding * ( Xs - 1 ) ) ) #NEWLINE# grid = np . zeros ( ( grid_height , grid_width , C ) ) #NEWLINE# next_idx = 0 #NEWLINE# ( y0 , y1 ) = ( 0 , H ) #NEWLINE# for y in xrange ( grid_size_y ) : #NEWLINE# #INDENT# ( x0 , x1 ) = ( 0 , W ) #NEWLINE# for x in xrange ( grid_size_x ) : #NEWLINE# #INDENT# if ( next_idx < N ) : #NEWLINE# #INDENT# img = Xs [ next_idx ] #NEWLINE# ( low , high ) = ( np . min ( img ) , np . max ( img ) ) #NEWLINE# grid [ y0 : y1 , x0 : x1 ] = ( ( ubound * ( img - low ) ) / ( high - low ) ) #NEWLINE# next_idx += 1 #NEWLINE# #UNINDENT# x0 += ( W + padding ) #NEWLINE# x1 += ( W + padding ) #NEWLINE# #UNINDENT# y0 += ( H + padding ) #NEWLINE# y1 += ( H + padding ) #NEWLINE# #UNINDENT# return grid