簡體   English   中英

如何為連體網絡創建支持集、正負對

[英]How do I create support set, positive and negative pairs for siamese network

我有一個需要一對輸入的連體。 我想創建正面、負面和支持集來訓練我在這里提到的網絡。 正對:來自相同視圖(或可能不同視圖)和相同類別的圖像。 負對:不同的類別和不同的觀點。 我有以下文件夾結構。

+---aluminium_foil
|   +---view1
|   |       15a-scale_2_im_1_col.png
|   |       15a-scale_3_im_1_col.png
|   |       15a-scale_4_im_1_col.png
|   |       
|   +---view2
|   |       15a-scale_2_im_2_col.png
|   |       15a-scale_3_im_2_col.png
|   |       
|   \---view3
|           15a-scale_2_im_3_col.png
|           15a-scale_3_im_3_col.png
|           15a-scale_4_im_3_col.png
|           15a-scale_5_im_3_col.png
|           15a-scale_6_im_3_col.png
|           
+---brown_bread
|   +---view1
|   |       48a-scale_2_im_1_col.png
|   |       48a-scale_3_im_1_col.png
|   |       48a-scale_4_im_1_col.png
|   |       48a-scale_5_im_1_col.png
|   |       
|   +---view2
|   |       48a-scale_2_im_3_col.png
|   |       48a-scale_3_im_3_col.png
|   |       48a-scale_5_im_3_col.png
|   |       48a-scale_6_im_3_col.png
|   |       48a-scale_7_im_3_col.png
|   |       
|   \---view3
|           48a-scale_2_im_5_col.png
|           48a-scale_2_im_6_col.png
|           48a-scale_3_im_5_col.png
|           48a-scale_3_im_6_col.png
|           
+---corduroy
|   +---view1
|   |       42a-scale_2_im_1_col.png
|   |       42a-scale_2_im_2_col.png
|   |       42a-scale_3_im_1_col.png
|   |       42a-scale_3_im_2_col.png
|   |       42a-scale_4_im_1_col.png
|   |       42a-scale_4_im_2_col.png
|   |       
|   +---view2
|   |       42a-scale_4_im_4_col.png
|   |       42a-scale_4_im_5_col.png
|   |       42a-scale_4_im_6_col.png
|   |       42a-scale_4_im_7_col.png
|   |       42a-scale_5_im_4_col.png
|   |       42a-scale_5_im_5_col.png
|   |       42a-scale_5_im_6_col.png
|   |       42a-scale_5_im_7_col.png
|   |       
|   \---view3
|           42a-scale_2_im_10_col.png
|           42a-scale_2_im_11_col.png
|           42a-scale_2_im_12_col.png
|           42a-scale_3_im_10_col.png
|           42a-scale_3_im_11_col.png
|           42a-scale_3_im_12_col.png
|           
+---cork
|   +---view1
|   |       16a-scale_2_im_1_col.png
|   |       16a-scale_2_im_2_col.png
|   |       16a-scale_2_im_3_col.png
|   |       16a-scale_2_im_4_col.png
|   |       16a-scale_2_im_5_col.png
|   |       
|   +---view2
|   |       16a-scale_3_im_3_col.png
|   |       16a-scale_3_im_4_col.png
|   |       16a-scale_3_im_5_col.png
|   |       16a-scale_3_im_6_col.png
|   |       16a-scale_4_im_3_col.png
|   |       16a-scale_4_im_4_col.png
|   |       16a-scale_4_im_5_col.png
|   |       16a-scale_4_im_6_col.png
|   |       
|   \---view3
|           16a-scale_8_im_10_col.png
|           16a-scale_8_im_11_col.png
|           
\---cotton
    +---view1
    |       46a-scale_3_im_1_col.png
    |       46a-scale_3_im_2_col.png
    |       
    +---view2
    |       46a-scale_2_im_10_col.png
    |       46a-scale_2_im_11_col.png
    |       46a-scale_2_im_12_col.png
    |       46a-scale_3_im_10_col.png
    |       46a-scale_3_im_11_col.png
    |       46a-scale_3_im_12_col.png
    |       
    \---view3
            46a-scale_5_im_10_col.png
            46a-scale_5_im_11_col.png
            46a-scale_5_im_12_col.png
.
.
.        

我編寫了代碼來為以下文件夾結構配對。

+---aluminium_foil
|       v3_s1_patch_1.JPG
|       v3_s1_patch_10.JPG
|       v3_s1_patch_11.JPG
|       
+---brown_bread
|       v3_s1_patch_11.JPG
|       v3_s1_patch_12.JPG
|       v3_s1_patch_13.JPG
|       v3_s1_patch_14.JPG
|       v3_s1_patch_15.JPG
|       
+---corduroy
|       v3_s1_patch_1.JPG
|       v3_s1_patch_10.JPG
|       v3_s1_patch_11.JPG
|       v3_s1_patch_12.JPG
|       v3_s1_patch_13.JPG
|       v3_s1_patch_17.JPG
|       v3_s1_patch_18.JPG
|       
+---cork
|       v3_s1_patch_10.JPG
|       v3_s1_patch_11.JPG
|       v3_s1_patch_12.JPG
|       v3_s1_patch_13.JPG
|       
+---cotton
|       v3_s1_patch_1.JPG
|       v3_s1_patch_10.JPG
|       v3_s1_patch_11.JPG
|       v3_s1_patch_12.JPG
|       v3_s1_patch_13.JPG

+---lettuce_leaf
|       v3_s1_patch_1.JPG
|       v3_s1_patch_10.JPG
|       v3_s1_patch_11.JPG
|       v3_s1_patch_12.JPG
|       v3_s1_patch_16.JPG
|       v3_s1_patch_17.JPG   

這是我為上述文件夾結構制作正負對的代碼。

def load_data_set(data_path, image_size = (224,224)):
    """
    Load images in numpy array. Images are loaded in RGB format.
    Input: Main data directory and image size in 2D.
    Output: X and Y as numpy array.
    """
    directory_list = os.listdir(path=data_path)
    directory_list.sort()
    x_data = list()
    y_data = list()

    for directory in directory_list:
        class_path = os.path.join(data_path, directory)
        image_names = os.listdir(path=class_path)
        image_names.sort()
        for image_name in image_names:
            image_array = cv2.imread(
                os.path.join(class_path, image_name), cv2.IMREAD_COLOR
            )
            image_array = cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB)
            image_array = cv2.resize(image_array, image_size)
            x_data.append(image_array)
            y_data.append(directory)

    x_data = np.array(x_data)
    y_data = np.array(y_data)

    return x_data, y_data

def positive_pair(x_value, y_value, augmentation= False):
    """
    Create a random positive duplet from the data.
    If augmentation is True, then image augmentation is applied
    for both images of negative duplet.
    """
    random_index = random.randint(0, x_value.shape[0] - 1)

    if augmentation is True:
        x_anchor = image_augmentation(x_value[random_index])
    else:
        x_anchor = x_value[random_index]

    y_anchor = y_value[random_index]
    pos_index = np.squeeze(np.where(y_value == y_anchor))

    if augmentation is True:
        x_positive = image_augmentation(
            x_value[pos_index[random.randint(0, len(pos_index) - 1)]]
        )
    else:
        x_positive = x_value[pos_index[random.randint(0, len(pos_index) - 1)]]

    return [x_anchor, x_positive], 1.0


def negative_pair(x_value, y_value, augmentation = False):
    """
    Create a random negative duplet from the data.
    If augmentation is True, then image augmentation is applied
    for both images of negative duplet.
    """
    random_index = random.randint(0, x_value.shape[0] - 1)

    if augmentation is True:
        x_anchor = image_augmentation(x_value[random_index])
    else:
        x_anchor = x_value[random_index]

    y_anchor = y_value[random_index]
    neg_index = np.squeeze(np.where(y_value != y_anchor))

    if augmentation is True:
        x_negative = image_augmentation(
            x_value[neg_index[random.randint(0, len(neg_index) - 1)]]
        )
    else:
        x_negative = x_value[neg_index[random.randint(0, len(neg_index) - 1)]]

    return [x_anchor, x_negative], 0.0

我也想創建支持集這個 . 如何采用我現有的代碼來創建正負對以及上述文件夾結構的支持集。 任何幫助,將不勝感激。

對於任何有同樣問題的人,我想出了如何為這樣的文件夾結構創建正負對。

從目錄中讀取圖像

def get_dataset(path):
    
    x_data = list()
    y_data = list()
    v_data = list()

    image_size = (224,224)

    for filename in glob.iglob(path + '**/*.JPG', recursive=True):
        view_folder = str(filename.split('/')[-3])
        class_folder = str(filename.split('/')[-2])

        image_array = cv2.imread(filename,cv2.IMREAD_COLOR)
        image_array = cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB)
        image_array = cv2.resize(image_array, image_size)

        x_data.append(image_array)
        y_data.append(class_folder)
        v_data.append(view_folder)
    
    x_data = np.array(x_data)
    y_data = np.array(y_data)
    v_data = np.array(v_data)
    
    return x_data/255.0, y_data, v_data

從數據中創建正負對

def positive_duplet(x_value, y_value, v_value):
    """
    Create a random positive duplet from data.
    for the both images of negative duplet.
    """
    random_index = random.randint(0, x_value.shape[0] - 1)

    x_anchor = x_value[random_index]
    v_anchor = v_value[random_index]

    y_anchor = y_value[random_index]
    pos_index = np.squeeze(np.where( (y_value == y_anchor) & (v_value == v_anchor ) ) )
    
    
    x_positive = x_value[pos_index[random.randint(0, len(pos_index) - 1)]]

    return [x_anchor, x_positive], 1.0


def negative_duplet(x_value, y_value, v_value):
    """
    Create a random negative duplet from data.
    for the both images of negative duplet.
    """
    random_index = random.randint(0, x_value.shape[0] - 1)

    x_anchor = x_value[random_index]
    v_anchor = v_value[random_index]

    y_anchor = y_value[random_index]
    neg_index = np.squeeze(np.where( (y_value != y_anchor) & (v_value != v_anchor) ) )
    #print(neg_index)

    x_negative = x_value[neg_index[random.randint(0, len(neg_index) - 1)]]

    return [x_anchor, x_negative], 0.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM