Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. def read_binary_blob(filename):
  2. #
  3. # Read binary blob file from C3D
  4. # INPUT
  5. # filename : input filename.
  6. #
  7. # OUTPUT
  8. # s : a 1x5 matrix indicates the size of the blob
  9. # which is [num channel length height width].
  10. # blob : a 5-D tensor size num x channel x length x height x width
  11. # containing the blob data.
  12. # read_status : a scalar value = 1 if sucessfully read, 0 otherwise.
  13.  
  14.  
  15. # precision is set to 'single', used by C3D
  16.  
  17. # open file and read size and data buffer
  18. # [s, c] = fread(f, [1 5], 'int32');
  19. read_status = 1
  20. blob = collections.namedtuple('Blob', ['size', 'data'])
  21.  
  22. f = open(filename, 'rb')
  23. s = array.array("i") # int32
  24. s.fromfile(f, 5)
  25.  
  26. if len(s) == 5 :
  27. m = s[0]*s[1]*s[2]*s[3]*s[4]
  28.  
  29. # [data, c] = fread(f, [1 m], precision)
  30. data_aux = array.array("f")
  31. data_aux.fromfile(f, m)
  32. data = np.array(data_aux.tolist())
  33.  
  34. if len(data) != m:
  35. read_status = 0;
  36.  
  37. else:
  38. read_status = 0;
  39.  
  40. # If failed to read, set empty output and return
  41. if not read_status:
  42. s = []
  43. blob_data = []
  44. b = blob(s, blob_data)
  45. return s, b, read_status
  46.  
  47. # reshape the data buffer to blob
  48. # note that MATLAB use column order, while C3D uses row-order
  49. # blob = zeros(s(1), s(2), s(3), s(4), s(5), Float);
  50. blob_data = np.zeros((s[0], s[1], s[2], s[3], s[4]), np.float32)
  51. off = 0
  52. image_size = s[3]*s[4]
  53. for n in range(0, s[0]):
  54. for c in range(0, s[1]):
  55. for l in range(0, s[2]):
  56. # print n, c, l, off, off+image_size
  57. tmp = data[np.array(range(off, off+image_size))];
  58. blob_data[n][c][l][:][:] = tmp.reshape(s[3], -1);
  59. off = off+image_size;
  60.  
  61.  
  62. b = blob(s, blob_data)
  63. f.close()
  64. return s, b, read_status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement