Tensorflow版Faster RCNN源码解析(TFFRCNN) (07) utils/blob.py

本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记

---------------个人学习笔记---------------

----------------本文作者疆--------------

------点击此处链接至博客园原文------

 

1.im_list_to_blob(ims)函数

将多张图像构成的列表ims构造为blob作为网络的输入,blob的维度为(图像数量,max_shape[0],max_shape[1],3), 需要注意的是传入图像列表中图像需经减图像均值、BGR转换等处理

def im_list_to_blob(ims):
    # 将图像构成的列表构成网络的blob输入
    """Convert a list of images into a network input.
    Assumes images are already prepared (means subtracted, BGR order, ...).
    """
    max_shape = np.array([im.shape for im in ims]).max(axis=0)  # 取出最大图像shape max_shape[0]、max_shape[1]
    num_images = len(ims)
    # 构造blob输入,维度为(图像数量,max_shape[0],max_shape[1],3)
    blob = np.zeros((num_images, max_shape[0], max_shape[1], 3),
                    dtype=np.float32)
    for i in xrange(num_images):
        im = ims[i]
        blob[i, 0:im.shape[0], 0:im.shape[1], :] = im
    return blob
# -*- coding:utf-8 -*-
# Author: WUJiang
# 测试功能

import numpy as np

max_shape = np.array([(1, 2, 3), (2, 1, 3)]).max(axis=0)
# [2, 2, 3]
print(max_shape)
View Code

2.prep_im_for_blob(im,pixel_means,target_size,max_size)

对逐张图像进行减均值、缩放处理,以便构造图像数据blob作为网络输入

def prep_im_for_blob(im, pixel_means, target_size, max_size):
    # 为构造blob网络输入对图像预处理,包括减去均值、缩放
    """Mean subtract and scale an image for use in a blob."""
    im = im.astype(np.float32, copy=False)
    im -= pixel_means
    im_shape = im.shape
    im_size_min = np.min(im_shape[0:2])
    im_size_max = np.max(im_shape[0:2])
    im_scale = float(target_size) / float(im_size_min)
    # Prevent the biggest axis from being more than MAX_SIZE
    if np.round(im_scale * im_size_max) > max_size:
        im_scale = float(max_size) / float(im_size_max)
    # 随机下采样
    # 默认TRAIN.RANDOM_DOWNSAMPLE = False,训练阶段不使用随机下采样
    if cfg.TRAIN.RANDOM_DOWNSAMPLE:
        r = 0.6 + np.random.rand() * 0.4
        im_scale *= r
    im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,
                    interpolation=cv2.INTER_LINEAR)
    return im, im_scale
posted @ 2019-08-06 12:16  JiangJ~  阅读(306)  评论(0编辑  收藏  举报