【語義分割標籤製作的疑問與測試筆記】

     深度學習作爲一項擁有近40年曆史的技術,至到2012年卷積網絡的出現纔有了今天繁榮。但數年的發展或許已將技術紅利消耗殆盡,在人工智能的三大影響因素——算法、數據、算力中,“數據”已經被冠以核心影響因素很長一段時間,那麼算法如何突破呢? 周少華表示:“我們無法估量下一次算法的突破會在什麼時候,不過現有的算法體系的確不能滿足人們想象中的人工智能。下一個的智能在哪裏?這還需要各行各業領域學者的共同努力。”

進入正題,語義分割的標籤與onet-hot轉化一直是個關鍵點,尤其是多分類情況下,這裏做個記錄測試 。

 4D的tensor:

實際標籤中像素按類別分別爲0,1,2,3.......,經過onet-hot表徵陳4維Tensor(NHWC),4維的Tensor每個通道對應一個類別,如下圖所示。https://blog.csdn.net/Pierce_KK/article/details/90640172

 


import numpy as np
from keras.utils.np_utils import to_categorical

 
def one_hot_it(labels,h,w):
    x=np.zeros([h,w,3])
    print(x)
    for i in range(h):
        for j in range(w):
            x[i,j,int(labels[i][j])]=1
    print("x.shape",x.shape)
    print(x)

img=np.random.randint(0,3,size=(2,2))
#3*5矩陣  
print(img)
print("img.shape",img.shape)
# img=np.
one_hot_it(img,img.shape[0],img.shape[1])

print("......")
categorical_labels = to_categorical(img, 3)
print(categorical_labels.shape)
print(categorical_labels)
label=categorical_labels.reshape(1,img.shape[0],img.shape[1],3)
print(label)
print(label.shape)

參考:https://blog.csdn.net/m0_37733057/article/details/78364035

one-hot標籤製作:https://gitee.com/BBuf/ENet-Keras/blob/master/LoadBatches.py

def getSegmentationArr(path, n_classes, width, height):
    seg_labels = np.zeros((height, width, n_classes))
    try:
        img = cv2.imread(path, 1)
        img = cv2.resize(img, (width, height))
        img = img[:, :, 0]
        for c in range(n_classes):
            seg_labels[:, :, c] = (img == c).astype(int)
    except Exception as e:
        print(e)
    seg_labels = np.reshape(seg_labels, (width * height, n_classes))
    return seg_labels
import numpy as np

from keras.utils.np_utils import to_categorical
img1=np.random.randint(0,2,(2,2,3,2))
print(img1)
print(img1.shape)
print("......")
img=np.arange(24).reshape(2,2,3,2)
print(img.shape)
print(img)

print("..............................")
img2=img1[:,:,:,0]
print("img2.shape",img2.shape)
categorical_labels = to_categorical(img2, 2)
print(categorical_labels.shape)
print(categorical_labels)
print("..............................")
label=categorical_labels.reshape(2,2,3,2)
print(label)

3個測試例子

import numpy as np



def one_hot_it(labels,h,w):
    x=np.zeros([h,w,3])
    for i in range(h):
        for j in range(w):
            x[i,j,int(labels[i][j])]=1
    return x

from keras.preprocessing.image import ImageDataGenerator, load_img
from keras import Model, Input, optimizers
from keras.utils.np_utils import to_categorical
img1=np.random.randint(0,3,(2,2,3))
print("img1:::")
print(img1)
print(img1.shape)
# print("......")
# img=np.arange(24).reshape(2,2,3,2)
# print(img.shape)
# print(img)
 
print("..............................")
img=img1[1,:,:]
img_flatten=np.reshape(img,(1,6))
print("img.shape",img.shape)
print("img",img)
print("img_flatten.shape",img_flatten.shape)
print("img_flatten",img_flatten)
print("..............................")
print("one_hot_it.shape",one_hot_it(img,2,3).shape)
print("one_hot_it(img,2.3)",one_hot_it(img,2,3))

print("..............................")
img_one_hot=to_categorical(img_flatten,3)
# label=np.reshape(img_one_hot,(2,2,3,3))
# print("label.shape")
# print(label)
print(img_one_hot.shape)
print(img_one_hot)

print("..............................")
# reshape
img_one_hot=np.reshape(img_one_hot[0,:,:],(2,3,3))
print("reshape",img_one_hot)


4 測試代碼

import numpy as np
# import time
# import eventlet

data1 = np.random.randint(0, 5, [2,3,2,4])
print(data1)
print("...................")
data2=np.random.random([2,2,3,4])
print(data2)
print("...................")
#求第四維的最大值
print(np.max(data2,axis=3))
print("...................")
#求第四維最大值索引 語義分割常用標籤提取
print(np.argmax(data2,axis=3))
print("...................")


# eventlet.monkey_patch()#必須加這條代碼
# with eventlet.Timeout(2,False):#設置超時時間爲2秒
#    time.sleep(4)
#    print('沒有跳過這條輸出')
# print('跳過了輸出')

5. numpy實現語義分割中多分類focalloss損失  參考鏈接:https://www.cnblogs.com/leebxo/p/10547091.html

import numpy as np
import keras.backend as k


def one_hot_it(labels,h,w):
    x=np.zeros([h,w,3])
    for i in range(h):
        for j in range(w):
            x[i,j,int(labels[i][j])]=1
    return x

def Focal_Loss(y_true, y_pred, alpha=0.25, gamma=2):
    """
    focal loss for multi-class classification
    fl(pt) = -alpha*(1-pt)^(gamma)*log(pt)
    :param y_true: ground truth one-hot vector shape of [batch_size, nb_class]
    :param y_pred: prediction after softmax shape of [batch_size, nb_class]
    :param alpha:
    :param gamma:
    :return:
    """
    # # parameters
    # alpha = 0.25
    # gamma = 2

    # To avoid divided by zero
    y_pred += 0.000001

    # Cross entropy
    ce = -y_true * np.log(y_pred)

    # Not necessary to multiply y_true(cause it will multiply with CE which has set unconcerned index to zero ),
    # but refer to the definition of p_t, we do it
    weight = np.power(1 - y_pred, gamma) * y_true

    # Now fl has a shape of [batch_size, nb_class]
    # alpha should be a step function as paper mentioned, but it doesn't matter like reason mentioned above
    # (CE has set unconcerned index to zero)
    #
    # alpha_step = tf.where(y_true, alpha*np.ones_like(y_true), 1-alpha*np.ones_like(y_true))
    fl = ce * weight * alpha
    print("!")
    print(fl)
    print("!")
    # Both reduce_sum and reduce_max are ok
    reduce_fl = np.max(fl, axis=-1)
    #reduce_fl = k.max(fl, axis=-1)

    return reduce_fl


print("..............................")

print(np.random.standard_normal(2))#array([-2.04606393, -1.05720303])
#size爲整數序列
print(np.random.standard_normal((2,3)))
print(np.random.standard_normal([2,3]).shape)#(2, 3)
list1=np.random.randint(0,2,(2,3))#生成一個2x3整數數組,取值範圍:[2,6)隨機整數
print(list1)
list2=np.random.rand(2,3)#生成2x3的二維數組
print(list2)
print(".................")
list3=[[0 ,1 ,0],
 [1, 0, 0]]
list4=[[0.1,0.7,0.2],
       [0.6,0.2,0.2]]
num1=np.array(list3)
num2=np.array(list4)
print(num1)
print(num2)
# print(Focal_Loss(num1, num2, 0.25, 2).shape)
print(Focal_Loss(num1, num2, 0.25, 2))

 6 分享各個框架關於dice損失函數的實現方法(很經典)

    https://blog.csdn.net/JMU_Ma/article/details/97533768

    https://www.aiuai.cn/aifarm1159.html

7 . 深入理解深度學習中tensor:https://blog.csdn.net/holmes_mx/article/details/82813865

8. np.sum使用方法


def dice_coef(y_true, y_pred, smooth, thresh):
    #y_pred =K.cast((K.greater(y_pred,thresh)), dtype='float32')#轉換爲float型
    #y_pred = y_pred[y_pred > thresh]=1.0
    y_true_f =y_true# K.flatten(y_true)
    y_pred_f =y_pred# K.flatten(y_pred)
    print("y_true_f",y_true_f.shape)
    print("y_pred_f",y_pred_f.shape)
    intersection = K.sum(y_true_f * y_pred_f,axis=(0,1,2))
    denom =K.sum(y_true_f,axis=(0,1,2)) + K.sum(y_pred_f,axis=(0,1,2))
    return K.mean((2. * intersection + smooth) /(denom + smooth))


c = np.array([[[0, 1, 2, 3], 
               [4, 5, 6, 7]],
               [[1, 2, 3, 4],
                [5, 6, 7, 8]]])

c1 = np.array([[[0, 1, 1, 1], 
               [1, 1, 1, 1]],
               [[1, 1, 1, 1],
                [1, 1, 1, 1]]])



print(c.shape) 
print("......")           
print(c.sum(axis=0))
print("......")    
print(c.sum(axis=1)) 
print("......")    
print(c.sum(axis=2)) 
print(np.sum(c,axis=2)) 
print(np.sum(c1,axis=(0,1,2)))

print("single chaanles")
s=np.random.randint(0,2,(2,2,4,1))
print(s)
print(np.sum(s,axis=(0,1,2)))
a=np.random.randint(0,2,(2,2,4,3))
print(a)
print("................")

print("mutilple chaanles")
b=np.random.random((2,2,4,3))
print(b)
print(np.sum(a,axis=(0,1,2)))
print(np.sum(a,axis=(0,1,2,3)))

print("......")
b1=b[0,...]
a1=a[0,...]
# print(b1)
# print(a1)

print("......")
a2=np.argmax(a1,axis=-1)
b2=np.argmax(b1,axis=-1)

print(a2)
print(b2)

8 .numpy驗證二分類與多分類dice損失函數

# -*- coding: utf-8 -*-
import numpy as np 
# import keras.backend as K
#多分類dice

def dice_coef(y_true, y_pred):
    #求得每個sample的每個類的dice
    mooth=0.001
    intersection = np.sum(y_true * y_pred, axis=(1,2,3))
    union = np.sum(y_true, axis=(1,2,3)) + np.sum(y_pred, axis=(1,2,3))
    sample_dices=(2. * intersection + smooth) / (union + smooth) #一維數組 爲各個類別的dice
    #求得每個類的dice
    dices=np.mean(sample_dices,axis=0)
    return np.mean(dices) #所有類別dice求平均的dice

 
 
# 二分類損失
def dice(y_true, y_pred, smooth):
    intersection = np.sum(y_true_f * y_pred_f,axis=(0,1,2))
    denom =np.sum(y_true_f,axis=(0,1,2)) + np.sum(y_pred_f,axis=(0,1,2))
    return np.mean((2. * intersection + smooth) /(denom + smooth))
 
 
a=np.random.randint(0,2,(2,2,3,1))
b=np.random.randint(0,2,(2,2,3,1))
 
# print("a...........")
# print(a)
# print("b...........")
# print(b)
# print("a*b......")
# print(a*b)
# print("intersection.........")
# intersection = np.sum(a * b,axis=(0,1,2,3)) #5
# print(intersection)
# print(a*b)
# print(np.sum(a * b,axis=(0,1,2))) # [5]
# print(np.mean(np.sum(a * b,axis=(0,1,2))),axis=0)
 
# 二分類
print("(1,2,3))")
intersection = np.sum(a * b,axis=(1,2,3))
denom =np.sum(a,axis=(1,2,3)) + np.sum(b,axis=(1,2,3))
print(intersection)
print(denom)
smooth=0.0001
print((2. * intersection + smooth) /(denom + smooth))
print(np.mean((2. * intersection + smooth) /(denom + smooth)))

print("(0,1,2,3))")
intersection = np.sum(a * b,axis=(0,1,2,3))
denom =np.sum(a,axis=(0,1,2,3)) + np.sum(b,axis=(0,1,2,3))
print(intersection)
print(denom)
smooth=0.0001
print((2. * intersection + smooth) /(denom + smooth))
print(np.mean((2. * intersection + smooth) /(denom + smooth)))

print("(0,1,2))")
intersection = np.sum(a * b,axis=(0,1,2))
denom =np.sum(a,axis=(0,1,2)) + np.sum(b,axis=(0,1,2))
print(intersection)
print(denom)
smooth=0.0001
print((2. * intersection + smooth) /(denom + smooth))
print(np.mean((2. * intersection + smooth) /(denom + smooth)))
 


#  ************************************************************************
# 多分類測試
print("多分類")
a=np.random.randint(0,2,(2,2,3,3))
b=np.random.randint(0,2,(2,2,3,3))
 
# print("a...........")
# print(a)
# print("b...........")
# print(b)
# print("a*b......")
# print(a*b)
# print("intersection.........")
# intersection = np.sum(a * b,axis=(0,1,2,3))
# print(intersection)
# print(np.sum(a * b,axis=(0,1,2)))
 
# 多分類
print("axis=(0,1,2,3)")
intersection = np.sum(a * b,axis=(0,1,2,3))
denom =np.sum(a,axis=(0,1,2,3)) + np.sum(b,axis=(0,1,2,3))
print(intersection)
print(denom)
smooth=0.0001
print((2. * intersection + smooth) /(denom + smooth))
# print(np.mean((2. * intersection + smooth) /(denom + smooth)))
 
print("axis=(1,2,3)")
intersection = np.sum(a * b,axis=(1,2,3))
denom =np.sum(a,axis=(1,2,3)) + np.sum(b,axis=(1,2,3))
print(intersection)
print(denom)
smooth=0.0001
print((2. * intersection + smooth) /(denom + smooth))
print(np.mean((2. * intersection + smooth) /(denom + smooth)))

print("axis=(0,1,2)")
intersection = np.sum(a * b,axis=(0,1,2))
denom =np.sum(a,axis=(0,1,2)) + np.sum(b,axis=(0,1,2))
print(intersection)
print(denom)
smooth=0.0001
print((2. * intersection + smooth) /(denom + smooth))
print(np.mean((2. * intersection + smooth) /(denom + smooth)))

上面代碼是自己的筆記,對於np.sum(a,axis=0,1,2) 還是np.sum(a,axis=0,1,2,3) 測試結論是後者。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章