Caffe Prediction

本文详细介绍了在使用Caffe进行图像预测时如何正确处理图像颜色通道的问题,特别是从RGB到BGR的转换过程,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**If you find content helpful and want to quote this blog, please provide the link [ayst123] ( http://blog.youkuaiyun.com/ayst123/article/details/44223321 )


correct me if I am wrong

BGR or RGB?

Caffe uses opencv to convert image to datum. The channels will be disordered to BGR rather than RGB by OpenCV. It means all blobs in caffe are in the BGR format.
When doing test, we have to make image be in the BGR format.

The prediction code

import caffe
###################################set mode
caffe.set_mode_cpu()
net = caffe.Net(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',caffe_root +'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',caffe.TEST)

# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1)) # mean pixel
transformer.set_raw_scale('data', 255)  # the reference model operates on images in [0,255] range instead of [0,1]
transformer.set_channel_swap('data', (2,1,0))  # the reference model has channels in BGR order instead of RGB

###########################################predict
net.blobs['data'].reshape(1,3,227,227)
net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(caffe_root + 'examples/images/cat.jpg'))
out = net.forward()

Explanation

caffe.io.load_image converts image to numpy, which range is [0,1].

def load_image(filename, color=True):
    """
    Load an image converting from grayscale or alpha as needed.
    Take
    filename: string
    color: flag for color format. True (default) loads as RGB while False
        loads as intensity (if image is already grayscale).
    Give
    image: an image with type np.float32 in range [0, 1]
        of size (H x W x 3) in RGB or
        of size (H x W x 1) in grayscale.
    """
    img = skimage.img_as_float(skimage.io.imread(filename)).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

transformer. preprocess

net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(caffe_root + 'examples/images/cat.jpg'))

do
- convert to single
- resize to input dimensions (preserving number of channels)
- transpose dimensions to K x H x W
- reorder channels (for instance color to BGR)
- scale raw input (e.g. from [0, 1] to [0, 255] for ImageNet models)
- subtract mean
- scale feature

If loading image directly, the python will make its range be [0,1]. That is why we need to set scale raw

forward

out = net.forward()

It will output all output blob. If we want to add additional blob as output, it is better to use

out = net.forward( [‘blob_name’] )



**If you find content helpful and want to quote this blog, please provide the link [ayst123] ( http://blog.youkuaiyun.com/ayst123/article/details/44223321 )

以下是使用Python OpenCV库进行人脸性别年龄预测的代码示例: ```python import cv2 import math # 加载人脸检测器和性别年龄分类器 face_detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") gender_age_detector = cv2.dnn.readNetFromCaffe("deploy_gender.prototxt", "gender_net.caffemodel") # 定义性别和年龄标签 gender_labels = ["Male", "Female"] age_labels = ["0-2", "4-6", "8-12", "15-20", "25-32", "38-43", "48-53", "60-"] # 加载输入图像 img = cv2.imread("image.jpg") # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = face_detector.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30)) # 对每个检测到的人脸进行处理 for (x,y,w,h) in faces: # 提取人脸区域 face_img = img[y:y+h, x:x+w].copy() # 将人脸区域转换为blob格式 blob = cv2.dnn.blobFromImage(face_img, 1, (227, 227), (78.4263377603, 87.7689143744, 114.895847746), swapRB=False) # 输入blob到性别年龄分类器 gender_age_detector.setInput(blob) predictions = gender_age_detector.forward() # 解析预测结果 gender_prediction = gender_labels[predictions[0].argmax()] age_prediction = age_labels[int(math.floor(predictions[1].argmax() / 3.0))] # 打印预测结果 print("Gender prediction: {}".format(gender_prediction)) print("Age prediction: {}".format(age_prediction)) ``` 注意,在运行此代码之前,你需要下载并提供以下文件: - haarcascade_frontalface_default.xml:OpenCV的人脸检测器 - deploy_gender.prototxt:性别年龄分类器的网络架构文件 - gender_net.caffemodel:性别年龄分类器的预训练权重文件
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值