Caffe:Python
lastmod 2026-11-10created 2016-11-084 edits
pycaffe is the Python interface of the deep learning framework Caffe; with it you can load a model via caffe.Net, run forward and backward passes, and work directly with each layer's blobs and weights as numpy arrays. Input images are read with skimage or similar, converted to float arrays, and preprocessed to match the channel order and mean values before being fed in. This page contains code that reads an image from a byte buffer or a file and turns it into a numpy array for Caffe input.
byte buffer to Numpy image
import numpy as np
import skimage.io
def load_image_from_buffer(buffer, color=True):
img_buffer = np.frombuffer(buffer, dtype=np.uint8)
img_buffer.shape = (300, 300, 3)
img = skimage.img_as_float(img_buffer).astype(np.float32)
if img.ndim == 2:
img = img[:, :, np.newaxis]
if color:
img = np.tile(img, (1, 1, 3))
elif img.shape[2] == 4:
img = img[:, :, :3]
return img
def load_image(filename, color=True):
img = skimage.img_as_float(skimage.io.imread(filename, as_grey=not color)).astype(np.float32)
if img.ndim == 2:
img = img[:, :, np.newaxis]
if color:
img = np.tile(img, (1, 1, 3))
elif img.shape[2] == 4:
img = img[:, :, :3]
return img