前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Caffe2 - (十八) 图片数据处理函数

Caffe2 - (十八) 图片数据处理函数

作者头像
AIHGF
发布2019-02-18 09:44:24
3910
发布2019-02-18 09:44:24
举报
文章被收录于专栏:AIUAIAIUAI

Caffe2 - 图片数据处理函数

Caffe2 提供了对图片进行加载、裁剪、缩放、去均值、batch 等处理的函数 - helper.py.

代码语言:javascript
复制
## @package helpers
# Module caffe2.python.tutorials.helpers
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import numpy as np
import skimage.io
import skimage.transform


def crop_center(img, cropx, cropy):
    y, x, c = img.shape
    startx = x // 2 - (cropx // 2)
    starty = y // 2 - (cropy // 2)
    return img[starty:starty + cropy, startx:startx + cropx]


def rescale(img, input_height, input_width):
    # print("Original image shape:" + str(img.shape) + " --> it should be in H, W, C!")
    # print("Model's input shape is %dx%d") % (input_height, input_width)
    aspect = img.shape[1] / float(img.shape[0])
    # print("Orginal aspect ratio: " + str(aspect))
    if(aspect > 1):
        # landscape orientation - wide image
        res = int(aspect * input_height)
        imgScaled = skimage.transform.resize(
            img,
            (input_width, res),
            preserve_range=False)
    if(aspect < 1):
        # portrait orientation - tall image
        res = int(input_width / aspect)
        imgScaled = skimage.transform.resize(
            img,
            (res, input_height),
            preserve_range=False)
    if(aspect == 1):
        imgScaled = skimage.transform.resize(
            img,
            (input_width, input_height),
            preserve_range=False)
    return imgScaled


def load(img):
    # load and transform image
    img = skimage.img_as_float(skimage.io.imread(img)).astype(np.float32)
    return img


def chw(img):
    # switch to CHW
    img = img.swapaxes(1, 2).swapaxes(0, 1)
    return img


def bgr(img):
    # switch to BGR
    img = img[(2, 1, 0), :, :]
    return img


def removeMean(img, mean):
    # remove mean for better results
    img = img * 255 - mean
    return img


def batch(img):
    # add batch size
    img = img[np.newaxis, :, :, :].astype(np.float32)
    return img


def loadToNCHW(img, mean, input_size):
    img = load(img)
    img = rescale(img, input_size, input_size)
    img = crop_center(img, input_size, input_size)
    img = chw(img)
    img = bgr(img)
    img = removeMean(img, mean)
    img = batch(img)
    return img


def parseResults(results):
    results = np.asarray(results)
    results = np.delete(results, 1)
    index = 0
    highest = 0
    arr = np.empty((0, 2), dtype=object)
    arr[:, 0] = int(10)
    arr[:, 1:] = float(10)
    for i, r in enumerate(results):
        # imagenet index begins with 1!
        i = i + 1
        arr = np.append(arr, np.array([[i, r]]), axis=0)
        if (r > highest):
            highest = r
            index = i

    # top 3 results
    print("Raw top 3 results:", sorted(arr, key=lambda x: x[1], reverse=True)[:3])

    # now we can grab the code list
    with open('inference_codes.txt', 'r') as f:
        for line in f:
            code, result = line.partition(":")[::2]
            if (code.strip() == str(index)):
                answer = "The image contains a %s with a %s percent probability." \
                    % (result.strip()[1:-2], highest * 100)
    f.closed
    return answer
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年01月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Caffe2 - 图片数据处理函数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档