def create_ans2label(occurence, name, cache_root): """Note that this will also create label2ans.pkl at the same time occurence: dict {answer -> whatever} name: prefix of the output file features_path: str """ ans2label = {} label2ans = [] ix_to_answer = {} label = 0 for answer in occurence: label2ans.append(answer) ans2label[answer] = label ix_to_answer[label] = answer label += 1 utils.create_dir(cache_root) cache_file = os.path.join(cache_root, name + '_ans2label.pkl') cPickle.dump(ans2label, open(cache_file, 'wb')) cache_file = os.path.join(cache_root, name + '_label2ans.pkl') cPickle.dump(label2ans, open(cache_file, 'wb')) answer_ix_map = {'answer_to_ix': ans2label, 'ix_to_answer': ix_to_answer} with open(os.path.join(cache_root, 'answer_ix_map.json'), 'w') as f: json.dump(answer_ix_map, f) return ans2label
def compute_target(answers_dset, ans2label, name, cache_root, fixed_score=None): """Augment answers_dset with soft score as label ***answers_dset should be preprocessed*** Write result into a cache file """ target = [] for ans_entry in answers_dset: answers = ans_entry['answers'] answer_count = {} for answer in answers: answer_ = answer['answer'] answer_count[answer_] = answer_count.get(answer_, 0) + 1 labels = [] scores = [] for answer in answer_count: if answer not in ans2label: continue labels.append(ans2label[answer]) if fixed_score is None: score = get_score(answer_count[answer]) else: score = fixed_score scores.append(score) target.append({ 'question_id': ans_entry['question_id'], 'image_id': ans_entry['image_id'], 'labels': labels, 'scores': scores }) utils.create_dir(cache_root) cache_file = os.path.join(cache_root, name + '_target.pkl') cPickle.dump(target, open(cache_file, 'wb')) with open(os.path.join(cache_root, name + '_target.json'), 'w') as f: json.dump(target, f) return target
# -*- coding: utf-8 -*- """ Created on Wed Dec 9 14:07:23 2020 @author: XEON """ import os from preprocess import utils if __name__ == "__main__": ROOT_DIR = os.getcwd() createdir = ["Train", "Valid", "Test"] utils.create_dir(ROOT_DIR, createdir) Path_file = utils.read_file(ROOT_DIR) # 0 = 骨质疏松 = osteoporosis = OP = # 1 = 骨量减少 = Osteopenia = OST = low bone mass low bone mass # 2 = 骨量正常 = NBM = normal bone mass OP = [name for name in Path_file if name.split("\\")[-2] == str(0)] OST = [name for name in Path_file if name.split("\\")[-2] == str(1)] NBM = [name for name in Path_file if name.split("\\")[-2] == str(2)] maxImagesNum = min([len(OP), len(OST), len(NBM)]) randomList = utils.getRandomList(OP, maxImagesNum) OP = [OP[i] for i in randomList] randomList = utils.getRandomList(OST, maxImagesNum) OST = [OST[i] for i in randomList] randomList = utils.getRandomList(NBM, maxImagesNum)
# -*- coding: utf-8 -*- """ Created on Sat Nov 7 16:56:13 2020 @author: XEON """ import os from preprocess import utils if __name__ == "__main__": ROOT_DIR = os.getcwd() utils.create_dir(ROOT_DIR, [r"./Label/Report"]) for reportPath in utils.loadData("Report_imagePath.list"): os.chdir(reportPath.replace(reportPath.split("\\")[-1], '')) utils.OutputReport( reportPath.split("\\")[-1].strip(), r"J:\Medical\preprocess\Label\Report", reportPath) print("finished")