AI经典代码

Mask RCNN

def generate_anchors(scales, ratios, shape, feature_stride, anchor_stride):
    """全图生成多种候选框 https://github.com/thethrone/Mask_RCNN/blob/master/mrcnn/utils.py
    scales: 1D array of anchor sizes in pixels. Example: [32, 64, 128]
    ratios: 1D array of anchor ratios of width/height. Example: [0.5, 1, 2]
    shape: [height, width] spatial shape of the feature map over which
            to generate anchors.
    feature_stride: Stride of the feature map relative to the image in pixels.
    anchor_stride: Stride of anchors on the feature map. For example, if the
        value is 2 then generate anchors for every other feature map pixel.
    """
    # Get all combinations of scales and ratios
    scales, ratios = np.meshgrid(np.array(scales), np.array(ratios))
    scales = scales.flatten()
    ratios = ratios.flatten()

    # Enumerate heights and widths from scales and ratios
    heights = scales / np.sqrt(ratios)
    widths = scales * np.sqrt(ratios)

    # Enumerate shifts in feature space
    shifts_y = np.arange(0, shape[0], anchor_stride) * feature_stride
    shifts_x = np.arange(0, shape[1], anchor_stride) * feature_stride
    shifts_x, shifts_y = np.meshgrid(shifts_x, shifts_y)

    # Enumerate combinations of shifts, widths, and heights
    box_widths, box_centers_x = np.meshgrid(widths, shifts_x)
    box_heights, box_centers_y = np.meshgrid(heights, shifts_y)

    # Reshape to get a list of (y, x) and a list of (h, w)
    box_centers = np.stack(
        [box_centers_y, box_centers_x], axis=2).reshape([-1, 2])
    box_sizes = np.stack([box_heights, box_widths], axis=2).reshape([-1, 2])

    # Convert to corner coordinates (y1, x1, y2, x2)
    boxes = np.concatenate([box_centers - 0.5 * box_sizes,
                            box_centers + 0.5 * box_sizes], axis=1)
    return boxes

pytorch处理边长输入的问题  RNN对于变长序列的处理方法, 为什么RNN需要mask_anshiquanshu的专栏-优快云博客_rnn变长序列

自带三个函数

  1. torch.nn.utils.rnn.PackedSequence()
  2. torch.nn.utils.rnn.pack_padded_sequence()
  3. torch.nn.utils.rnn.pad_packed_sequence()
import torch
import torch.nn as nn
from torch.nn import utils as nn_utils
 
batch_size = 2
max_length = 3
hidden_size = 2
n_layers = 1
 
print("1.--------------")
tensor_in = torch.FloatTensor([[1, 0, 0], [1, 2, 3]]).resize_(2, 3, 1)
seq_lengths = [1, 3]  # list of integers holding information about the batch size at each sequence step
print("seq_lengths:\n", seq_lengths)
print("tensor_in:\n", tensor_in)
 
print("2.--------------")
seq_lens = torch.IntTensor([1, 3])
_, idx_sort = torch.sort(seq_lens, dim=0, descending=True)
_, idx_unsort = torch.sort(idx_sort, dim=0)
order_seq_lengths = torch.index_select(seq_lens, dim=0, index=idx_sort)
order_tensor_in = torch.index_select(tensor_in, dim=0, index=idx_sort)
print("order_seq_lengths:\n", order_seq_lengths)
print("order_tensor_in:\n", order_tensor_in)
 
print("3.--------------")
x_packed = nn_utils.rnn.pack_padded_sequence(order_tensor_in, order_seq_lengths, batch_first=True)
rnn = nn.RNN(1, hidden_size, n_layers, batch_first=True)
h0 = torch.randn(n_layers, batch_size, hidden_size)
y_packed, h_n = rnn(x_packed, h0)
print('x_packed:\n', x_packed)
print('y_packed:\n', y_packed)
 
print("4.--------------")
y_sort, length = nn_utils.rnn.pad_packed_sequence(y_packed, batch_first=True)
print("sort unpacked output:\n", y_sort)
print(y_sort.shape)
 
print("5.--------------")
# unsort output to original order
y = torch.index_select(y_sort, dim=0, index=idx_unsort)
print("org unpacked output:\n", y)
print(y.shape)
 
print("6.--------------")
# unsort output to original order
last_h = torch.index_select(h_n[-1], dim=0, index=idx_unsort)
print("last hidden state:\n", last_h)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值