Skip to content

Instantly share code, notes, and snippets.

@burrussmp
Last active May 5, 2020 18:44
Show Gist options
  • Save burrussmp/dd4fe17d8c157dc13f4b5f2ea5f6f1ca to your computer and use it in GitHub Desktop.
Save burrussmp/dd4fe17d8c157dc13f4b5f2ea5f6f1ca to your computer and use it in GitHub Desktop.
Takes in a matrix HxWx1 where each pixel is a label in the set {0,..,k} and returns a matrix of size HxWx(k+1)
"""
One-hot-encodes segmentation map
@params
y: np.ndarray
HxWx1 seegmentation map where each element is in set {0,...,num_labels}
num_labels: int
Defines the set {0,...,num_labels} which is used to one-hot-encode the segmentation map
Ex. For annotation segmentation, we have 26 letters, so we say 26. It is assumed that a 0 means no class of interest.
@return
target: np.ndarray
HxWx(num_labels+1) segmentation matrix
"""
def convert_to_segmentation_ground_truth(y,num_labels=26):
target = np.zeros((num_labels+1,128,128))
target[0,:,:] = np.ones((128,128))
for i in range(1,num_labels+1):
seg_one = y == i
target[i,:,:] = seg_one
target[0,:,:] = target[0,:,:] - target[i,:,:]
target = target.astype(np.float32)
return target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment