Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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