File size: 2,352 Bytes
d77a28a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import pandas as pd
import pickle
import numpy as np
from torch.utils.data import Dataset

CLASSES = ["Bag", "Bed", "Bowl","Clock", "Dishwasher", "Display", "Door", "Earphone", "Faucet",
    "Hat", "StorageFurniture", "Keyboard", "Knife", "Laptop", "Microwave", "Mug",
    "Refrigerator", "Chair", "Scissors", "Table", "TrashCan", "Vase", "Bottle"]

AFFORD_CL = ['lay','sit','support','grasp','lift','contain','open','wrap_grasp','pour', 
                'move','display','push','pull','listen','wear','press','cut','stab']

def pc_normalize(pc):
    centroid = np.mean(pc, axis=0)
    pc = pc - centroid
    m = np.max(np.sqrt(np.sum(pc**2, axis=1)))
    pc = pc / m
    return pc, centroid, m

class Corrupt(Dataset):

    def __init__(self,
                 corrupt_type='scale',
                 level=0
                 ):
        
        #replace with the path to the LASO-C/PIAD-C dataset
        data_root='LASO-C'

        file_name = f'{corrupt_type}_{level}.pkl'

        self.corrupt_type = corrupt_type
        self.level = level

        self.cls2idx = {cls.lower():np.array(i).astype(np.int64) for i, cls in enumerate(CLASSES)}
        self.aff2idx = {cls:np.array(i).astype(np.int64) for i, cls in enumerate(AFFORD_CL)}

        with open(os.path.join(data_root, 'point', file_name), 'rb') as f:
            self.anno = pickle.load(f)

        self.question_df = pd.read_csv(os.path.join(data_root, 'text', 'Affordance-Question.csv'))
       
    def find_rephrase(self, df, object_name, affordance):

        qid = 'Question0'
        result = df.loc[(df['Object'] == object_name) & (df['Affordance'] == affordance), [qid]]
        if not result.empty:
            return result.iloc[0][qid]
        else:
            raise NotImplementedError
            
    def __getitem__(self, index):

        data = self.anno[index]        
        cls = data['class']
        affordance = data['affordance']
        gt_mask = data['mask']
        point_set = data['point']
        point_set,_,_ = pc_normalize(point_set)    

        question = self.find_rephrase(self.question_df, cls, affordance)
        
        affordance = self.aff2idx[affordance]

        point_input = point_set.transpose()

        return point_input, self.cls2idx[cls], gt_mask, question, affordance

    def __len__(self):

        return len(self.anno)