DaSE-Computer-Vision-2021
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

73 linhas
2.1 KiB

  1. from builtins import range
  2. from past.builtins import xrange
  3. from math import sqrt, ceil
  4. import numpy as np
  5. def visualize_grid(Xs, ubound=255.0, padding=1):
  6. """
  7. Reshape a 4D tensor of image data to a grid for easy visualization.
  8. Inputs:
  9. - Xs: Data of shape (N, H, W, C)
  10. - ubound: Output grid will have values scaled to the range [0, ubound]
  11. - padding: The number of blank pixels between elements of the grid
  12. """
  13. (N, H, W, C) = Xs.shape
  14. grid_size = int(ceil(sqrt(N)))
  15. grid_height = H * grid_size + padding * (grid_size - 1)
  16. grid_width = W * grid_size + padding * (grid_size - 1)
  17. grid = np.zeros((grid_height, grid_width, C))
  18. next_idx = 0
  19. y0, y1 = 0, H
  20. for y in range(grid_size):
  21. x0, x1 = 0, W
  22. for x in range(grid_size):
  23. if next_idx < N:
  24. img = Xs[next_idx]
  25. low, high = np.min(img), np.max(img)
  26. grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low)
  27. # grid[y0:y1, x0:x1] = Xs[next_idx]
  28. next_idx += 1
  29. x0 += W + padding
  30. x1 += W + padding
  31. y0 += H + padding
  32. y1 += H + padding
  33. # grid_max = np.max(grid)
  34. # grid_min = np.min(grid)
  35. # grid = ubound * (grid - grid_min) / (grid_max - grid_min)
  36. return grid
  37. def vis_grid(Xs):
  38. """ visualize a grid of images """
  39. (N, H, W, C) = Xs.shape
  40. A = int(ceil(sqrt(N)))
  41. G = np.ones((A*H+A, A*W+A, C), Xs.dtype)
  42. G *= np.min(Xs)
  43. n = 0
  44. for y in range(A):
  45. for x in range(A):
  46. if n < N:
  47. G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = Xs[n,:,:,:]
  48. n += 1
  49. # normalize to [0,1]
  50. maxg = G.max()
  51. ming = G.min()
  52. G = (G - ming)/(maxg-ming)
  53. return G
  54. def vis_nn(rows):
  55. """ visualize array of arrays of images """
  56. N = len(rows)
  57. D = len(rows[0])
  58. H,W,C = rows[0][0].shape
  59. Xs = rows[0][0]
  60. G = np.ones((N*H+N, D*W+D, C), Xs.dtype)
  61. for y in range(N):
  62. for x in range(D):
  63. G[y*H+y:(y+1)*H+y, x*W+x:(x+1)*W+x, :] = rows[y][x]
  64. # normalize to [0,1]
  65. maxg = G.max()
  66. ming = G.min()
  67. G = (G - ming)/(maxg-ming)
  68. return G