For creating a one-hot tensor, I have implemented five functions…

  
      github.com
  
  
    lartpang/PyLoss/blob/main/losspy/utils/convert_onehot.py 2
# -*- coding: utf-8 -*-
# @Time    : 2020/12/13
# @Author  : Lart Pang
# @GitHub  : https://github.com/lartpang

import time

import torch
from torch.nn.functional import one_hot


def bhw_to_onehot_by_for(bhw_tensor: torch.Tensor, num_classes: int):
    """
    Args:
        bhw_tensor: b,h,w
        num_classes:

    Returns: b,h,w,num_classes
    """
    assert bhw_tensor.ndim == 3, bhw_tensor.shape


  This file has been truncated. show original

  
  
    
    
  
  


Hope it works for you.