def k_fold(folder_k): folder_k_fold = SAVE_SETS + "/K-fold" if not os.path.exists(folder_k_fold): os.makedirs(folder_k_fold) def folder_k_lines(dataset): random.shuffle(dataset) n = int(len(dataset) / folder_k) r = len(dataset) % folder_k lines = [] a = 1 start = 0 for i in range(folder_k - 1): if r == 0: a = 0 else: r = r - 1 lines = lines + ['folder{}:\n'.format(i + 1) ] + dataset[start:start + n + a] start = start + n + a lines = lines + ['folder{}:\n'.format(i + 2) ] + dataset[start:len(dataset)] return lines dtset_hp.write_lines(folder_k_fold + "/SAMM_k_fold.txt", folder_k_lines(SAMM_all)) dtset_hp.write_lines(folder_k_fold + "/micro_k_fold.txt", folder_k_lines(micro))
def LOVO(): folder_LOVO = SAVE_SETS + "/LOVO" if not os.path.exists(folder_LOVO): os.makedirs(folder_LOVO) random.shuffle(SAMM_all) random.shuffle(micro) dtset_hp.write_lines(folder_LOVO + "/SAMM_LOVO.txt", SAMM_all) dtset_hp.write_lines(folder_LOVO + "/micro_LOVO.txt", micro)
def hold_out(ratio): folder_hold_out = SAVE_SETS + "/Hold-Out" if not os.path.exists(folder_hold_out): os.makedirs(folder_hold_out) micro_train, micro_test = train_test_split(micro, test_size=1 - ratio, random_state=0) SAMM_train, SAMM_test = train_test_split(SAMM_all, test_size=1 - ratio, random_state=0) micro_hold_out = ['trainval:\n'] + micro_train + ['test:\n' ] + micro_test SAMM_hold_out = ['trainval:\n'] + SAMM_train + ['test:\n' ] + SAMM_test dtset_hp.write_lines(folder_hold_out + "/SAMM_hold_out.txt", SAMM_hold_out) dtset_hp.write_lines(folder_hold_out + "/micro_hold_out.txt", micro_hold_out)
def hold_out(ratio): folder_hold_out = SAVE_SETS + "/Hold-Out" if not os.path.exists(folder_hold_out): os.makedirs(folder_hold_out) micro_train, micro_test = train_test_split(micro, test_size=1 - ratio, random_state=0) macro_train, macro_test = train_test_split(macro, test_size=1 - ratio, random_state=0) micro_hold_out = ['trainval:\n'] + micro_train + ['test:\n' ] + micro_test macro_hold_out = ['trainval:\n'] + macro_train + ['test:\n' ] + macro_test ME2_train = micro_train + macro_train ME2_test = micro_test + macro_test random.shuffle(ME2_train) random.shuffle(ME2_test) ME2_hold_out = ['trainval:\n'] + ME2_train + ['test:\n'] + ME2_test dtset_hp.write_lines(folder_hold_out + "/ME2_hold_out.txt", ME2_hold_out) dtset_hp.write_lines(folder_hold_out + "/micro_hold_out.txt", micro_hold_out) dtset_hp.write_lines(folder_hold_out + "/macro_hold_out.txt", macro_hold_out)
def LOSO(): folder_LOSO = SAVE_SETS + "/LOSO" if not os.path.exists(folder_LOSO): os.makedirs(folder_LOSO) def loso_lines(dataset): random.shuffle(dataset) map_subject = {} for line in dataset: subject = line.split()[0] if subject in map_subject: map_subject[subject].append(line) else: map_subject[subject] = [line] lines = [] for i, value in enumerate(map_subject.values()): lines = lines + ['subject{}:\n'.format(i + 1)] + value return lines dtset_hp.write_lines(folder_LOSO + "/SAMM_LOSO.txt", loso_lines(SAMM_all)) dtset_hp.write_lines(folder_LOSO + "/micro_LOSO.txt", loso_lines(micro))
def generate_dataset(self): wb = xlrd.open_workbook(SOURCE_DATA + "/SAMM_Micro_FACS_Codes.xlsx") sheet_micro = wb.sheet_by_name('MICRO_ONLY') nrows = sheet_micro.nrows allInfo = [] micro_all = [] SAMM_all = [] for row_id in range(nrows): if row_id <= 10: continue folder = sheet_micro.cell_value(row_id, 0) video = str(sheet_micro.cell_value(row_id, 1)).split('_')[1] cls = "micro-expression" onset_frame = sheet_micro.cell_value(row_id, 3) apex_frame = sheet_micro.cell_value(row_id, 4) offset_frame = sheet_micro.cell_value(row_id, 5) info_line = "{} {} {} {} {} {}\n".format(folder, video, cls, onset_frame, apex_frame, offset_frame) allInfo.append(info_line) video_line = "{} {}\n".format(folder, video) micro_all.append(video_line) root, folders, _ = dtset_hp.return_oswalk(SOURCE_DATA) for folder in folders: videos_path = os.path.join(root, folder) _, videos, _ = dtset_hp.return_oswalk(videos_path) for video in videos: video_line = "{} {}\n".format(folder, video) SAMM_all.append(video_line) micro_all = list(set(micro_all)) dtset_hp.write_lines(SAVE_SETS + "/all_Info.txt", allInfo) dtset_hp.write_lines(SAVE_SETS + "/SAMM_all.txt", SAMM_all) dtset_hp.write_lines(SAVE_SETS + "/micro_all.txt", micro_all)
def generate_dataset(self): wb = xlrd.open_workbook(SOURCE_DATA + "/CAS(ME)^2code_final.xlsx") sheet_CASFEcode = wb.sheet_by_name('CASFEcode_final') sheet_namingRule1 = wb.sheet_by_name('naming rule1') sheet_namingRule2 = wb.sheet_by_name('naming rule2') video_map = {} # { anger1:0401, ...} name to code for i in range(sheet_namingRule2.nrows): code = sheet_namingRule2.cell_value(i, 0) name = sheet_namingRule2.cell_value(i, 1) video_map[name] = code nrows = sheet_CASFEcode.nrows allInfo = [] ME2_all = [] micro_all = [] macro_all = [] for row_id in range(nrows): folder = sheet_namingRule1.cell_value(row_id, 1) video_name = sheet_CASFEcode.cell_value(row_id, 1).split('_')[0] video = video_map[video_name] cls = sheet_CASFEcode.cell_value(row_id, 7) onset_frame = sheet_CASFEcode.cell_value(row_id, 2) apex_frame = sheet_CASFEcode.cell_value(row_id, 3) offset_frame = sheet_CASFEcode.cell_value(row_id, 4) if offset_frame == 0: offset_frame = apex_frame if apex_frame == 0: apex_frame = (onset_frame + offset_frame) / 2 info_line = "{} {} {} {} {} {}\n".format(folder, video, cls, onset_frame, apex_frame, offset_frame) allInfo.append(info_line) video_line = "{} {}\n".format(folder, video) if cls == "micro-expression": micro_all.append(video_line) if cls == "macro-expression": macro_all.append(video_line) root, folders, _ = dtset_hp.return_oswalk(SOURCE_DATA + '/rawpic') for folder in folders: videos_path = os.path.join(root, folder) _, videos, _ = dtset_hp.return_oswalk(videos_path) for video in videos: video = video.split('_')[1][0:4] video_line = "{} {}\n".format(folder, video) ME2_all.append(video_line) ME2_all = list(set(ME2_all)) micro_all = list(set(micro_all)) macro_all = list(set(macro_all)) macro_all = [x for x in macro_all if x not in micro_all] dtset_hp.write_lines(SAVE_SETS + "/all_Info.txt", allInfo) dtset_hp.write_lines(SAVE_SETS + "/ME2_all.txt", ME2_all) dtset_hp.write_lines(SAVE_SETS + "/micro_all.txt", micro_all) dtset_hp.write_lines(SAVE_SETS + "/macro_all.txt", macro_all)