def test_hwc_to_chw(): x = np.array([[[ 1, 2, 3], [ 4, 5, 6] ], [[ 7, 8, 9], [10, 11, 12] ], [[13, 14, 15], [16, 17, 18], ], [[19, 20, 21], [22, 23, 24] ] ]) assert_equals(x.shape[0], 4) # h assert_equals(x.shape[1], 2) # w assert_equals(x.shape[2], 3) # c y = mu.hwc_to_chw(x) assert_equals(y.shape[0], 3) assert_equals(y.shape[1], 4) assert_equals(y.shape[2], 2) assert_equals(x[3][1][2], y[2][3][1]) for i in range(4): for j in range(2): for k in range(3): assert_equals(x[i][j][k], y[k][i][j])
def read_img_caf(fpath, mean=None): ''' load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe ''' img_dat = caffe.io.load_image(fpath) # pixel value range per channel: [0, 1] img_dat *= 255. # RGB to BGR img_dat = img_dat[:, :, ::-1] # per-channel mean subtraction if mean is not None: img_dat -= mean # reorder dimensions img_dat = mu.hwc_to_chw(img_dat) return img_dat
def read_img_PIL(fpath, mean=None): ''' load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe ''' img = Image.open(fpath) # pixel value range per channel: [0, 255] img_dat = np.array(img, dtype=np.float32) # RGB to BGR img_dat = img_dat[:, :, ::-1] # per-channel mean subtraction if mean is not None: img_dat -= mean # reorder dimensions img_dat = mu.hwc_to_chw(img_dat) return img_dat
def read_img_cv2(fpath, mean=None): ''' load image in BGR, subtract mean, and make dims C x H x W for Caffe ''' img_dat = cv2.imread(fpath) # pixel value range per channel: [0, 255] # channels already in BGR order # per-channel mean subtraction if mean is not None: img_dat = img_dat.astype(np.float32) img_dat -= mean img_dat = img_dat.astype(np.float) # reorder dimensions img_dat = mu.hwc_to_chw(img_dat) # casting to np.float enables plugging into protobuf return img_dat
def read_img_caf(fpath, mean=None): ''' load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe ''' img_dat = caffe.io.load_image( fpath) # pixel value range per channel: [0, 1] img_dat *= 255. # RGB to BGR img_dat = img_dat[:, :, ::-1] # per-channel mean subtraction if mean is not None: img_dat -= mean # reorder dimensions img_dat = mu.hwc_to_chw(img_dat) return img_dat