zhengye1995 / Zero-shot-Instance-Segmentation

code for CVPR paper Zero-shot Instance Segmentation
Apache License 2.0
93 stars 14 forks source link

How is the file word_w2v_withbg_65_15.txt generated in the source code? #24

Closed xkhnhms closed 2 years ago

xkhnhms commented 3 years ago

How is the file word_w2v_withbg_65_15.txt generated in the source code?

zhengye1995 commented 2 years ago
  1. Get "word_w2v.txt" from https://github.com/salman-h-khan/PL-ZSD_Release
  2. Calculate the average of the word vectors of all classes as the word vector of the background class.
  3. Merge the word vector of background class with word_w2v.txt through placing it in the first of all word vectors to obtain word_w2v_withbg.txt
  4. Use this code to obtain word_w2v_withbg_48_17.txt or word_w2v_withbg_65_15.txt :
    
    import numpy as np

source_w2v = 'data/coco/word_w2v_withbg.txt' taget_w2v = 'data/coco/word_w2v_withbg_48_17.txt'

with open(source_w2v, 'r') as f: vec_81 = np.loadtxt(source_w2v, dtype='float32', delimiter=',')

print(vec_81.shape)

coco_catname_order = {'person': 0, 'bicycle': 1, 'car': 2, 'motorcycle': 3, 'airplane': 4, 'bus': 5, 'train': 6, 'truck': 7, 'boat': 8, 'traffic_light': 9, 'fire_hydrant': 10, 'stop_sign': 11, 'parking_meter': 12, 'bench': 13, 'bird': 14, 'cat': 15, 'dog': 16, 'horse': 17, 'sheep': 18, 'cow': 19, 'elephant': 20, 'bear': 21, 'zebra': 22, 'giraffe': 23, 'backpack': 24, 'umbrella': 25, 'handbag': 26, 'tie': 27, 'suitcase': 28, 'frisbee': 29, 'skis': 30, 'snowboard': 31, 'sports_ball': 32, 'kite': 33, 'baseball_bat': 34, 'baseball_glove': 35, 'skateboard': 36, 'surfboard': 37, 'tennis_racket': 38, 'bottle': 39, 'wine_glass': 40, 'cup': 41, 'fork': 42, 'knife': 43, 'spoon': 44, 'bowl': 45, 'banana': 46, 'apple': 47, 'sandwich': 48, 'orange': 49, 'broccoli': 50, 'carrot': 51, 'hot_dog': 52, 'pizza': 53, 'donut': 54, 'cake': 55, 'chair': 56, 'couch': 57, 'potted_plant': 58, 'bed': 59, 'dining_table': 60, 'toilet': 61, 'tv': 62, 'laptop': 63, 'mouse': 64, 'remote': 65, 'keyboard': 66, 'cell_phone': 67, 'microwave': 68, 'oven': 69, 'toaster': 70, 'sink': 71, 'refrigerator': 72, 'book': 73, 'clock': 74, 'vase': 75, 'scissors': 76, 'teddy_bear': 77, 'hair_drier': 78, 'toothbrush': 79}

coco_classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic_light', 'fire_hydrant', 'stop_sign', 'parking_meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports_ball', 'kite', 'baseball_bat', 'baseball_glove', 'skateboard', 'surfboard', 'tennis_racket', 'bottle', 'wine_glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot_dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted_plant', 'bed', 'dining_table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell_phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy_bear', 'hair_drier', 'toothbrush']

seen_65_classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'truck', 'boat', 'traffic_light', 'fire_hydrant', 'stop_sign', 'bench', 'bird', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'skis', 'sports_ball', 'kite', 'baseball_bat', 'baseball_glove', 'skateboard', 'surfboard', 'tennis_racket', 'bottle', 'wine_glass', 'cup', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'orange', 'broccoli', 'carrot', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted_plant', 'bed', 'dining_table', 'tv', 'laptop', 'remote', 'keyboard', 'cell_phone', 'microwave', 'oven', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy_bear', 'toothbrush']

unseen_15_classes_names = ['airplan', 'train', 'parking meter', 'cat', 'bear', 'suitcase', 'frisbee', 'snowboard', 'fork', 'sandwich', 'hot dog', 'toilet', 'mouse', 'toaster', 'hair drier']

coco_65_15classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'truck', 'boat', 'traffic_light', 'fire_hydrant', 'stop_sign', 'bench', 'bird', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'skis', 'sports_ball', 'kite', 'baseball_bat', 'baseball_glove', 'skateboard', 'surfboard', 'tennis_racket', 'bottle', 'wine_glass', 'cup', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'orange', 'broccoli', 'carrot', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted_plant', 'bed', 'dining_table', 'tv', 'laptop', 'remote', 'keyboard', 'cell_phone', 'microwave', 'oven', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy_bear', 'toothbrush', 'airplane', 'train', 'parking meter', 'cat', 'bear', 'suitcase', 'frisbee', 'snowboard', 'fork', 'sandwich', 'hot dog', 'toilet', 'mouse', 'toaster', 'hair drier']

seen_48_classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'truck', 'boat', 'bench', 'bird', 'horse', 'sheep', 'zebra', 'giraffe', 'backpack', 'handbag', 'skis', 'kite', 'surfboard', 'bottle', 'spoon', 'bowl', 'banana', 'apple', 'orange', 'broccoli', 'carrot', 'pizza', 'donut', 'chair', 'bed', 'tv', 'laptop', 'remote', 'microwave', 'oven', 'refrigerator', 'book', 'clock', 'vase', 'toothbrush', 'train', 'bear', 'suitcase', 'frisbee', 'fork', 'sandwich', 'toilet', 'mouse', 'toaster']

unseen_17_classes_names = ['bus', 'dog', 'cow', 'elephant', 'umbrella', 'tie', 'skateboard', 'cup', 'knife', 'cake', 'couch', 'keyboard', 'sink', 'scissors', 'airplane', 'cat', 'snowboard']

coco65_15_order = {} for i in range(len(coco_65_15classes_names)): coco65_15_order[coco_65_15classes_names[i]] = i+1

vec_48_17 = np.zeros([300, (48+17+1)]) assert len(seen_48_classes_names) == 48 assert len(unseen_17_classes_names) == 17 for i in range(len(seen_48_classes_names)): id = coco65_15_order[seen_48_classes_names[i]] vec_48_17[:, i+1] = vec_81[:, id] for i in range(len(unseen_17_classes_names)): id = coco65_15_order[unseen_17_classes_names[i]] vec_48_17[:, i+1+48] = vec_81[:, id]

vec_48_17[:, 0] = vec_81[:, 0]

np.savetxt(taget_w2v, vec_48_17, delimiter=',')

xkhnhms commented 2 years ago
  1. Get "word_w2v.txt" from https://github.com/salman-h-khan/PL-ZSD_Release
  2. Calculate the average of the word vectors of all classes as the word vector of the background class.
  3. Merge the word vector of background class with word_w2v.txt through placing it in the first of all word vectors to obtain word_w2v_withbg.txt
  4. Use this code to obtain word_w2v_withbg_48_17.txt or word_w2v_withbg_65_15.txt :
import numpy as np

source_w2v = 'data/coco/word_w2v_withbg.txt'
taget_w2v = 'data/coco/word_w2v_withbg_48_17.txt'

with open(source_w2v, 'r') as f:
    vec_81 = np.loadtxt(source_w2v, dtype='float32', delimiter=',')

print(vec_81.shape)

coco_catname_order = {'person': 0, 'bicycle': 1, 'car': 2, 'motorcycle': 3, 'airplane': 4, 'bus': 5, 'train': 6,
                      'truck': 7, 'boat': 8, 'traffic_light': 9, 'fire_hydrant': 10, 'stop_sign': 11,
                      'parking_meter': 12, 'bench': 13, 'bird': 14, 'cat': 15, 'dog': 16, 'horse': 17,
                      'sheep': 18, 'cow': 19, 'elephant': 20, 'bear': 21, 'zebra': 22, 'giraffe': 23,
                      'backpack': 24, 'umbrella': 25, 'handbag': 26, 'tie': 27, 'suitcase': 28, 'frisbee': 29,
                      'skis': 30, 'snowboard': 31, 'sports_ball': 32, 'kite': 33, 'baseball_bat': 34,
                      'baseball_glove': 35, 'skateboard': 36, 'surfboard': 37, 'tennis_racket': 38, 'bottle': 39,
                      'wine_glass': 40, 'cup': 41, 'fork': 42, 'knife': 43, 'spoon': 44, 'bowl': 45, 'banana': 46,
                      'apple': 47, 'sandwich': 48, 'orange': 49, 'broccoli': 50, 'carrot': 51, 'hot_dog': 52,
                      'pizza': 53, 'donut': 54, 'cake': 55, 'chair': 56, 'couch': 57, 'potted_plant': 58, 'bed': 59,
                      'dining_table': 60, 'toilet': 61, 'tv': 62, 'laptop': 63, 'mouse': 64, 'remote': 65,
                      'keyboard': 66, 'cell_phone': 67, 'microwave': 68, 'oven': 69, 'toaster': 70, 'sink': 71,
                      'refrigerator': 72, 'book': 73, 'clock': 74, 'vase': 75, 'scissors': 76, 'teddy_bear': 77,
                      'hair_drier': 78, 'toothbrush': 79}

coco_classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
                       'train', 'truck', 'boat', 'traffic_light', 'fire_hydrant',
                       'stop_sign', 'parking_meter', 'bench', 'bird', 'cat', 'dog',
                       'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',
                       'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
                       'skis', 'snowboard', 'sports_ball', 'kite', 'baseball_bat',
                       'baseball_glove', 'skateboard', 'surfboard', 'tennis_racket',
                       'bottle', 'wine_glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
                       'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot',
                       'hot_dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
                       'potted_plant', 'bed', 'dining_table', 'toilet', 'tv', 'laptop',
                       'mouse', 'remote', 'keyboard', 'cell_phone', 'microwave',
                       'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock',
                       'vase', 'scissors', 'teddy_bear', 'hair_drier', 'toothbrush']

seen_65_classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'truck', 'boat',
                           'traffic_light', 'fire_hydrant',
                           'stop_sign', 'bench', 'bird', 'dog',
                           'horse', 'sheep', 'cow', 'elephant', 'zebra', 'giraffe',
                           'backpack', 'umbrella', 'handbag', 'tie',
                           'skis', 'sports_ball', 'kite', 'baseball_bat',
                           'baseball_glove', 'skateboard', 'surfboard', 'tennis_racket',
                           'bottle', 'wine_glass', 'cup', 'knife', 'spoon', 'bowl',
                           'banana', 'apple', 'orange', 'broccoli', 'carrot',
                           'pizza', 'donut', 'cake', 'chair', 'couch',
                           'potted_plant', 'bed', 'dining_table', 'tv', 'laptop',
                           'remote', 'keyboard', 'cell_phone', 'microwave',
                           'oven', 'sink', 'refrigerator', 'book', 'clock',
                           'vase', 'scissors', 'teddy_bear', 'toothbrush']

unseen_15_classes_names = ['airplan', 'train', 'parking meter', 'cat', 'bear', 'suitcase', 'frisbee', 'snowboard', 'fork',
                            'sandwich', 'hot dog', 'toilet', 'mouse', 'toaster', 'hair drier']

coco_65_15classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'truck', 'boat',
                           'traffic_light', 'fire_hydrant',
                           'stop_sign', 'bench', 'bird', 'dog',
                           'horse', 'sheep', 'cow', 'elephant', 'zebra', 'giraffe',
                           'backpack', 'umbrella', 'handbag', 'tie',
                           'skis', 'sports_ball', 'kite', 'baseball_bat',
                           'baseball_glove', 'skateboard', 'surfboard', 'tennis_racket',
                           'bottle', 'wine_glass', 'cup', 'knife', 'spoon', 'bowl',
                           'banana', 'apple', 'orange', 'broccoli', 'carrot',
                           'pizza', 'donut', 'cake', 'chair', 'couch',
                           'potted_plant', 'bed', 'dining_table', 'tv', 'laptop',
                           'remote', 'keyboard', 'cell_phone', 'microwave',
                           'oven', 'sink', 'refrigerator', 'book', 'clock',
                           'vase', 'scissors', 'teddy_bear', 'toothbrush',
                           'airplane', 'train', 'parking meter', 'cat', 'bear',
                           'suitcase', 'frisbee', 'snowboard', 'fork',
                            'sandwich', 'hot dog', 'toilet', 'mouse', 'toaster', 'hair drier']

seen_48_classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'truck', 'boat', 'bench', 'bird', 'horse', 'sheep',
                          'zebra', 'giraffe', 'backpack', 'handbag', 'skis', 'kite', 'surfboard', 'bottle', 'spoon',
                          'bowl', 'banana', 'apple', 'orange', 'broccoli', 'carrot', 'pizza', 'donut', 'chair', 'bed',
                          'tv', 'laptop', 'remote', 'microwave', 'oven', 'refrigerator', 'book', 'clock', 'vase',
                          'toothbrush', 'train', 'bear', 'suitcase', 'frisbee', 'fork', 'sandwich', 'toilet', 'mouse',
                          'toaster']

unseen_17_classes_names = ['bus', 'dog', 'cow', 'elephant', 'umbrella', 'tie', 'skateboard', 'cup', 'knife', 'cake',
                            'couch', 'keyboard', 'sink', 'scissors', 'airplane', 'cat', 'snowboard']

coco65_15_order = {}
for i in range(len(coco_65_15classes_names)):
    coco65_15_order[coco_65_15classes_names[i]] = i+1

vec_48_17 = np.zeros([300, (48+17+1)])
assert len(seen_48_classes_names) == 48
assert len(unseen_17_classes_names) == 17
for i in range(len(seen_48_classes_names)):
    id = coco65_15_order[seen_48_classes_names[i]]
    vec_48_17[:, i+1] = vec_81[:, id]
for i in range(len(unseen_17_classes_names)):
    id = coco65_15_order[unseen_17_classes_names[i]]
    vec_48_17[:, i+1+48] = vec_81[:, id]

vec_48_17[:, 0] = vec_81[:, 0]

np.savetxt(taget_w2v, vec_48_17, delimiter=',')

I have received your reply, and thank you very much for helping me answer the questions I have encountered while learning your source code.