Example #1
0
    def __getitem__(self, index):
        # loading and preprocessing. TODO move them to transform classes
        # buffer = self.loadvideo(self.fname[index])
        try:
            buffer = produce_frames(self.fname[index])
        except:
            index = 0
            buffer = produce_frames(self.fname[index])
        if self.mode == 'train':
            buffer = self.randomflip(buffer)
        # print("---------------")
        # print(buffer.shape)
        # size =(224,224)
        # if size要改,crop函数里面的随机裁剪也要跟着改
        # temp = []
        # for i in buffer:
        #     # cv2.imshow("1",i)
        #     # cv2.waitKey(10)
        #     temp.append(i)
        if self.mode == 'train':
            a = 0
            for i in buffer:
                a = a + 1
                cv2.imwrite(','.join(self.fname[index][0].split('/')) + '.jpg',
                            i)
                break
                print(self.fname[index][0])

        buffer = np.concatenate(buffer, axis=2)

        # print(buffer.shape)
        # ________________________________________debug
        # b = video_transform(buffer)
        #
        # b= b.numpy().transpose((1,2,3,0))*255
        # a =0
        # for i in b:
        #     a =a+1
        #     cv2.imwrite(str(a)+str(index)+'.jpg',i)
        # a = 0

        if self.mode == 'test':
            return test_transform(buffer), torch.from_numpy(
                np.array(self.labels[index])).long()
        else:
            # print(video_transform(buffer).shape,"123")
            return video_transform(buffer), torch.from_numpy(
                np.array(self.labels[index])).long(), self.fname[index]
Example #2
0
    def __getitem__(self, index):
        # loading and preprocessing. TODO move them to transform classes
        frame = cv2.imread(self.fname[index])
        # frame = cv2.imread(self.fname[index])
        # a = re.findall(r'Camera(\d+)',self.fname[index])[0]
        # if a=='1':
        #     frame = cv2.imread(self.fname[index])
        #     frame = frame[172:480,59:488,:]
        #
        # if a=='2':
        #     frame = cv2.imread(self.fname[index])
        #     frame = frame[73:459,208:394,:]

        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        if self.mode == 'train':
            if np.random.random() < 0.5:
                frame = cv2.flip(frame, flipCode=1)

        # size =(112,112)
        # if size要改,crop函数里面的随机裁剪也要跟着改

        # print(buffer.shape)
        # ________________________________________debug
        # b = video_transform(frame)
        # b= b.numpy().transpose((1,2,3,0))
        #
        # for i in b:
        #     cv2.imshow(str(self.labels[index]),i)
        #     cv2.waitKey(1000)

        if self.mode == 'test':
            return test_transform(frame), torch.from_numpy(
                np.array(self.labels[index])).long()
        else:
            # print(video_transform(buffer).shape,"123")
            return video_transform(frame), torch.from_numpy(
                np.array(self.labels[index])).long()
Example #3
0
def submit_video_2():
    dic = {}
    dic[0] = 7
    dic[1] = 11
    dic[2] = 6
    dic[3] = 8
    dic[4] = 10
    model = TSN(5, 5, modality='RGB', partial_bn=True, is_shift=True)

    pretrained_dict = torch.load(
        '/data/xudw/temporal-shift-module-master/Fall_down_5_frame_5.13.pth.tar',
        map_location='cpu')
    try:
        model_dict = model.module.state_dict()
    except AttributeError:
        model_dict = model.state_dict()

    pretrained_dict = {
        k: v
        for k, v in pretrained_dict.items() if k in model_dict
    }
    print("load pretrain model")

    model_dict.update(pretrained_dict)
    model.load_state_dict(model_dict)

    model = model.cuda()
    model.eval()
    path = '/data/xudw/labels_submit_video_camera2/'  #come from the singe frame label
    import os
    import csv
    import re
    import cv2
    import pandas as pd
    import numpy as np
    with torch.no_grad():
        for Subject in os.listdir(path):
            c1 = re.findall('(\d+)', Subject)
            Subject_id = c1[0]
            for Activity in os.listdir(path + Subject):
                c2 = re.findall('(\d+)', Activity)
                Activity_id = c2[0]

                for csv_1 in os.listdir(path + Subject + '/' + Activity):
                    csv_data = pd.read_csv(path + Subject + '/' + Activity +
                                           '/' + csv_1)
                    c3 = re.findall('(\d+)', csv_1)
                    Trial_id = c3[-1]
                    loc = np.array(csv_data["Timestamp"])
                    loc = list(loc)

                    label = np.array(csv_data['Tag'])
                    label = list(label)
                    print(path + Subject + '/' + Activity + '/' + csv_1)

                    # csv_data.close()
                    f = open(path + Subject + '/' + Activity + '/' + csv_1,
                             'w',
                             encoding='utf-8',
                             newline='')
                    # 2. 基于文件对象构建 csv写入对象
                    csv_writer = csv.writer(f)
                    # 3. 构建列表头
                    csv_writer.writerow(["Timestamp", "Tag"])
                    frames = sorted(
                        os.listdir('/data/xudw/Fall_Down_data/' + 'Subject' +
                                   Subject_id + 'Activity' + Activity_id +
                                   'Trial' + Trial_id + 'Camera2'))
                    for i in range(len(loc)):
                        if 0 <= i <= 3:
                            print(loc[i], label[i])
                            csv_writer.writerow([loc[i], label[i]])
                        else:
                            buffer = []
                            frame_1 = cv2.imread('/data/xudw/Fall_Down_data/' +
                                                 'Subject' + Subject_id +
                                                 'Activity' + Activity_id +
                                                 'Trial' + Trial_id +
                                                 'Camera2/' + frames[i - 4])
                            frame_1 = frame_1[73:459, 208:394, :]

                            # if a == '2':
                            #     frame = cv2.imread(i)
                            #     frame = frame[73:459, 208:394, :]

                            frame_1 = cv2.cvtColor(frame_1, cv2.COLOR_BGR2RGB)
                            buffer.append(frame_1)

                            frame_2 = cv2.imread('/data/xudw/Fall_Down_data/' +
                                                 'Subject' + Subject_id +
                                                 'Activity' + Activity_id +
                                                 'Trial' + Trial_id +
                                                 'Camera2/' + frames[i - 3])
                            frame_2 = frame_2[73:459, 208:394, :]
                            frame_2 = cv2.cvtColor(frame_2, cv2.COLOR_BGR2RGB)
                            buffer.append(frame_2)
                            frame_3 = cv2.imread('/data/xudw/Fall_Down_data/' +
                                                 'Subject' + Subject_id +
                                                 'Activity' + Activity_id +
                                                 'Trial' + Trial_id +
                                                 'Camera2/' + frames[i - 2])
                            frame_3 = frame_3[73:459, 208:394, :]
                            frame_3 = cv2.cvtColor(frame_3, cv2.COLOR_BGR2RGB)
                            buffer.append(frame_3)
                            frame_4 = cv2.imread('/data/xudw/Fall_Down_data/' +
                                                 'Subject' + Subject_id +
                                                 'Activity' + Activity_id +
                                                 'Trial' + Trial_id +
                                                 'Camera2/' + frames[i - 1])
                            frame_4 = frame_4[73:459, 208:394, :]
                            frame_4 = cv2.cvtColor(frame_4, cv2.COLOR_BGR2RGB)
                            buffer.append(frame_4)
                            frame_5 = cv2.imread('/data/xudw/Fall_Down_data/' +
                                                 'Subject' + Subject_id +
                                                 'Activity' + Activity_id +
                                                 'Trial' + Trial_id +
                                                 'Camera2/' + frames[i])
                            frame_5 = frame_5[73:459, 208:394, :]
                            frame_5 = cv2.cvtColor(frame_5, cv2.COLOR_BGR2RGB)
                            buffer.append(frame_5)

                            temp = []
                            for j in buffer:
                                temp.append(j)
                            buffer = np.concatenate(temp, axis=2)
                            labels = model(
                                test_transform(buffer).unsqueeze(dim=0).cuda())
                            # print(label)
                            _, predicted = torch.max(labels.data, 1)
                            print(loc[i])
                            print(label[i])
                            print("----" * 10)
                            print(loc[i], dic[int(predicted)])
                            csv_writer.writerow(
                                [loc[i], str(dic[int(predicted)])])
Example #4
0
def submit_2():
    dic = {}
    dic[0] = 7
    dic[1] = 11
    dic[2] = 6
    dic[3] = 8
    dic[4] = 10
    model = TSN(5,
                1,
                base_model='resnet34',
                modality='RGB',
                partial_bn=True,
                is_shift=False)
    pretrained_dict = torch.load(
        '/data/xudw/temporal-shift-module-master/Fall_down_Single_test111_5.13.pth.tar',
        map_location='cpu')
    try:
        model_dict = model.module.state_dict()
    except AttributeError:
        model_dict = model.state_dict()

    pretrained_dict = {
        k: v
        for k, v in pretrained_dict.items() if k in model_dict
    }
    print("load pretrain model")

    model_dict.update(pretrained_dict)
    model.load_state_dict(model_dict)

    model = model.cuda()
    model.eval()
    path = '/data/xudw/labels_submit_2/'
    import os
    import csv
    import re
    import cv2
    import pandas as pd
    import numpy as np
    with torch.no_grad():
        for Subject in os.listdir(path):
            c1 = re.findall('(\d+)', Subject)
            Subject_id = c1[0]
            for Activity in os.listdir(path + Subject):
                c2 = re.findall('(\d+)', Activity)
                Activity_id = c2[0]
                for csv_1 in os.listdir(path + Subject + '/' + Activity):
                    csv_data = pd.read_csv(path + Subject + '/' + Activity +
                                           '/' + csv_1)
                    c3 = re.findall('(\d+)', csv_1)
                    Trial_id = c3[-1]
                    loc = np.array(csv_data["Timestamp"])
                    loc = list(loc)
                    label = np.array(csv_data['Tag'])
                    label = list(label)
                    # csv_data.close()
                    f = open(path + Subject + '/' + Activity + '/' + csv_1,
                             'w',
                             encoding='utf-8',
                             newline='')
                    # 2. 基于文件对象构建 csv写入对象

                    csv_writer = csv.writer(f)
                    # 3. 构建列表头
                    csv_writer.writerow(["Timestamp", "Tag"])
                    frames = sorted(
                        os.listdir('/data/xudw/Fall_Down_data/' + 'Subject' +
                                   Subject_id + 'Activity' + Activity_id +
                                   'Trial' + Trial_id + 'Camera2'))
                    print(path + Subject + '/' + Activity + '/' + csv_1)
                    for i in range(len(loc)):
                        frame = cv2.imread('/data/xudw/Fall_Down_data/' +
                                           'Subject' + Subject_id +
                                           'Activity' + Activity_id + 'Trial' +
                                           Trial_id + 'Camera2/' + frames[i])
                        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                        labels = model(
                            test_transform(frame).unsqueeze(dim=0).cuda())
                        # print(label)

                        _, predicted = torch.max(labels.data, 1)
                        print(label[i])
                        print(dic[int(predicted)])
                        csv_writer.writerow([loc[i], str(dic[int(predicted)])])