├── .github ├── ISSUE_TEMPLATE.md └── ISSUE_TEMPLATE │ ├── BUG_REPORT.md │ ├── FEATURE_REQUEST.md │ └── QUESTION.md ├── 10_idet5_food_baseline ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py └── setup.py ├── 11_idet5_car_baseline ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py └── setup.py ├── 12_idet_food_baseline ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py └── setup.py ├── 13_idet_car_baseline ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py └── setup.py ├── 14_ig5_inpaint ├── 14_ig5_inpaint │ ├── data_loader.py │ └── evaluation.py ├── README.md ├── data_local_loader.py ├── example │ ├── x_GT.png │ ├── x_input.png │ └── x_mask.png ├── main.py ├── nsml_run.sh ├── nsml_submit.sh ├── setup.py └── utils.py ├── 15_ig_super ├── README.md ├── data_description.png ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── logger.py ├── main.py ├── model │ └── edsr.py └── setup.py ├── 16_tcls_movie ├── README.md ├── data_loader.py ├── dataset.py ├── evaluation.py ├── kor_char_parser.py ├── main.py ├── models.py ├── nsmlignore └── setup.py ├── 17_tcls_intent ├── README.md ├── data_loader.py ├── data_utils.py ├── evaluation.py ├── main.py ├── model.py ├── setup.py ├── trainer.py └── vocab.pickle ├── 18_tcls_query ├── 18_tcls_query │ ├── data_loader.py │ └── evaluation.py ├── README.md ├── args.py ├── data_local_loader.py ├── data_utils.py ├── main.py ├── model │ ├── __init__.py │ ├── bilinear_scorer.py │ └── char_cnn.py └── setup.py ├── 19_tcls_qa ├── .gitignore ├── .nsmlignore ├── README.md ├── base_config │ ├── baseline.json │ └── test.json ├── data_loader.py ├── evaluation.py ├── main.py ├── requirements.txt ├── setup.py └── utils.py ├── 1_reg_dust ├── README.md ├── data_loader.py ├── evaluation.py ├── main.py ├── main_kr.py ├── main_tf.py ├── requirements.txt └── setup.py ├── 20_scls_stt ├── .gitignore ├── README.md ├── edit_distance.py ├── evaluation │ ├── data_loader.py │ └── evaluation.py ├── label_loader.py ├── loader.py ├── log.py ├── main.py ├── models │ ├── DecoderRNN.py │ ├── EncoderRNN.py │ ├── LICENSE │ ├── TopKDecoder.py │ ├── __init__.py │ ├── attention.py │ ├── baseRNN.py │ └── seq2seq.py ├── run_nsml.sh ├── script.labels ├── setup.py └── wavio.py ├── 2_cls_crane1 ├── README.md ├── data_loader.py ├── evaluation.py ├── main.py └── setup.py ├── 3_cls_crane2 ├── README.md ├── data_loader.py ├── evaluation.py ├── main.py └── setup.py ├── 4_cls_food ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py └── setup.py ├── 5_vcls_emotion ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py ├── png │ ├── 1.png │ └── emotion_model.png ├── setup.py └── utils.py ├── 6_vcls_age ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py ├── png │ ├── 1.png │ └── age_model.png ├── setup.py └── utils.py ├── 7_icls_face ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py └── setup.py ├── 8_iret_food ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── example_images │ ├── Img_069_0070.JPG │ ├── Img_082_0042.jpg │ └── Img_082_0047.jpg ├── generate_test_label.py ├── main.py └── setup.py ├── 9_iret_car ├── README.md ├── data_loader.py ├── data_local_loader.py ├── evaluation.py ├── main.py ├── resources │ ├── input.JPG │ ├── sample1.JPG │ └── sample2.JPG └── setup.py ├── README.md └── sample_data ├── 2_cls_crane_a_samples ├── normal_C0004HZBR_37.npy ├── normal_C0004HZMT_29.npy ├── normal_C0004HZMT_43.npy ├── normal_C0004HZMT_7.npy ├── normal_C0004VTBR_10.npy ├── normal_C0004VTBR_32.npy ├── normal_C0004VTBR_38.npy ├── normal_C0004VTBR_46.npy ├── normal_C0004VTMT_15.npy ├── normal_C0004VTMT_36.npy ├── normal_C0004VTMT_48.npy ├── normal_C0004VTMT_5.npy ├── normal_C0005HZBR_13.npy ├── normal_C0005HZBR_23.npy ├── normal_C0005HZBR_31.npy ├── normal_C0005HZBR_39.npy ├── normal_C0005HZBR_47.npy ├── normal_C0005HZBR_5.npy ├── normal_C0005HZMT_17.npy ├── normal_C0005HZMT_28.npy ├── normal_C0005HZMT_37.npy ├── normal_C0005HZMT_46.npy ├── normal_C0005HZMT_7.npy ├── normal_C0005VTBR_16.npy ├── normal_C0005VTBR_29.npy ├── normal_C0005VTBR_43.npy ├── normal_C0005VTBR_5.npy ├── normal_C0005VTMT_14.npy ├── normal_C0005VTMT_37.npy └── normal_C0005VTMT_49.npy └── 3_cls_crane_b_samples ├── B_2.npy ├── C_2.npy ├── D_3.npy ├── normal_10.npy ├── normal_17.npy ├── normal_24.npy ├── normal_33.npy ├── normal_34.npy └── normal_5.npy /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 3 | 4 | ### Any other comments 5 | 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/BUG_REPORT.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🐛 Bug Report 3 | about: Create a report to help us improve 🤔. 4 | --- 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | **Informations** 15 | #### CLI 16 | - **OS (Mac, Windows, Linux, etc) and version**: 17 | - **client version(please show nsml --version)**: 18 | - **Full command line**: 19 | 20 | 21 | 22 | 23 | #### WEB 24 | - **Brower (chrome, firfox, etc)**: 25 | - **URL**: 26 | 27 | 28 | **What is your NSML login ID?** 29 | 30 | 31 | **What is name of session in problem? (bug message or screenshot)** 32 | 33 | 34 | **Steps to reproduce the problem** 35 | 36 | 37 | **What is the expected behavior?** 38 | 39 | 40 | **Suggested solutions** 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🚀 Feature Request 3 | about: Suggest an idea for this project 💡! 4 | --- 5 | 6 | 8 | 9 | **What is the expected behavior?** 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/QUESTION.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Question 3 | about: Question about NSML 4 | --- 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | **Informations** 15 | #### CLI 16 | - **OS (Mac, Windows, Linux, etc) and version**: 17 | - **client version(please show nsml --version)**: 18 | 19 | 20 | 21 | 22 | #### WEB 23 | - **Brower (chrome, firfox, etc)**: 24 | - **URL**: 25 | 26 | 27 | **What is your NSML login ID?** 28 | 29 | 30 | **Question** 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /10_idet5_food_baseline/README.md: -------------------------------------------------------------------------------- 1 | ## 파라미터 5MB 이내 사진내의 음식 영역 추출하기 Baseline 2 | 3 | Task 4 | ``` 5 | 파라미터 5MB 이내 사진내의 음식 영역 추출하기 6 | ``` 7 | 8 | Dataset Description: 9 | ``` 10 | \_train 11 | \_ train_data (folder) 12 | \_ *.jpg (음식 사진) 13 | \_ *.txt (bounding box ([x,y,w,h])) 14 | 15 | # of training images: 40,612 16 | # of test images: 1,791 17 | ``` 18 | 19 | Evaluation Metric: 20 | ``` 21 | mean of intersection-over-union (mIoU) 22 | (1/N)*sum_{k=1}^{N}(IoU(pred_k, gt_k)) 23 | 24 | - N: number of test images 25 | - pred_k: bouding box prediction ([x,y,w,h]) of k-th image 26 | - gt_k: bouding box ground truth ([x,y,w,h]) of k-th image 27 | ``` 28 | 29 | 30 | How to run: 31 | 32 | ```bash 33 | nsml run -d 10_idet5_food -g 1 --cpus 2 -e main.py 34 | ``` 35 | 36 | How to list checkpoints saved: 37 | 38 | ```bash 39 | nsml model ls {USER_ID}/10_idet5_food/{SESSION_NUMBER} 40 | ``` 41 | 42 | How to submit: 43 | 44 | ```bash 45 | nsml submit {USER_ID}/10_idet5_food/{SESSION_NUMBER} {CHEKCOPOINT_NUMBER} 46 | ``` 47 | -------------------------------------------------------------------------------- /10_idet5_food_baseline/data_loader.py: -------------------------------------------------------------------------------- 1 | """ data_loader.py 2 | Replicated in the NSML leaderboard dataset, KoreanFoodLocalize. 3 | """ 4 | 5 | from nsml import IS_ON_NSML, DATASET_PATH 6 | 7 | import os 8 | 9 | def feed_infer(output_file, infer_func): 10 | """" 11 | infer_func(function): inference 할 유저의 함수 12 | output_file(str): inference 후 결과값을 저장할 파일의 위치 패스 13 | (이위치에 결과를 저장해야 evaluation.py 에 올바른 인자로 들어옵니다.) 14 | """ 15 | if IS_ON_NSML: 16 | root_path = os.path.join(DATASET_PATH, 'test') 17 | else: 18 | root_path = '/home/data/NIPAKoreanFoodLocalizeSmall/test' 19 | results = infer_func(root_path) 20 | results_str = [','.join(str(v) for v in l) for l in list(results)] 21 | print('write output') 22 | with open(output_file, 'w') as file_writer: 23 | file_writer.write("\n".join(results_str)) 24 | 25 | if os.stat(output_file).st_size == 0: 26 | raise AssertionError('output result of inference is nothing') 27 | 28 | 29 | def test_data_loader(root_path): 30 | return root_path 31 | -------------------------------------------------------------------------------- /10_idet5_food_baseline/data_local_loader.py: -------------------------------------------------------------------------------- 1 | from torch.utils import data 2 | from torchvision import datasets, transforms 3 | from torchvision.datasets.folder import default_loader 4 | import torch 5 | import os 6 | 7 | def get_transform(): 8 | normalize = transforms.Normalize( 9 | mean=[x / 255.0 for x in [125.3, 123.0, 113.9]], 10 | std=[x / 255.0 for x in [63.0, 62.1, 66.7]]) 11 | transform = [] 12 | transform.append(transforms.Resize((224,224))) 13 | transform.append(transforms.ToTensor()) 14 | transform.append(normalize) 15 | return transforms.Compose(transform) 16 | 17 | def target_transform(target, sizes): 18 | width,height = sizes 19 | 20 | target[0] = float(target[0]) / float(width) 21 | target[1] = float(target[1]) / float(height) 22 | target[2] = float(target[2]) / float(width) 23 | target[3] = float(target[3]) / float(height) 24 | 25 | return target 26 | 27 | 28 | class CustomDataset(data.Dataset): 29 | def __init__(self, root, transform, target_transform, loader=default_loader): 30 | self.root = root 31 | self.transform = transform 32 | self.target_transform = target_transform 33 | self.loader = loader 34 | 35 | self.images = [] 36 | self.boxes = [] 37 | dir_list = sorted(os.listdir(self.root)) 38 | 39 | for file_path in dir_list: 40 | if file_path.endswith('.jpg'): 41 | self.images.append(os.path.join(self.root, file_path)) 42 | 43 | box_file_path = file_path[:-4] + '.txt' 44 | box_str = open(os.path.join(self.root, box_file_path), 'r', encoding='utf-8').read().split('\n')[0] 45 | box = [int(float(bb)) for bb in box_str.split(',')] 46 | self.boxes.append(box) 47 | 48 | def __len__(self): 49 | return len(self.images) 50 | 51 | def __getitem__(self, index): 52 | images = self.loader(self.images[index]) 53 | targets = torch.Tensor(self.boxes[index]) 54 | 55 | inputs = self.transform(images) 56 | width, height = images.size 57 | targets = self.target_transform(targets, (width, height)) 58 | 59 | return inputs, targets 60 | 61 | 62 | def data_loader(root, phase='train', batch_size=256): 63 | if phase == 'train': 64 | is_train = True 65 | elif phase == 'test': 66 | is_train = False 67 | else: 68 | raise KeyError 69 | input_transform = get_transform() 70 | dataset = CustomDataset(root, input_transform, target_transform) 71 | return data.DataLoader(dataset=dataset, 72 | batch_size=batch_size, 73 | shuffle=is_train) 74 | 75 | 76 | def data_loader_with_split(root, train_split=0.9, batch_size=256, val_label_file='./val_label'): 77 | input_transform = get_transform() 78 | dataset = CustomDataset(root, input_transform, target_transform) 79 | split_size = int(len(dataset) * train_split) 80 | train_set, valid_set = data.random_split(dataset, [split_size, len(dataset) - split_size]) 81 | tr_loader = data.DataLoader(dataset=train_set, 82 | batch_size=batch_size, 83 | shuffle=True) 84 | val_loader = data.DataLoader(dataset=valid_set, 85 | batch_size=batch_size, 86 | shuffle=False) 87 | 88 | 89 | gt_labels = [valid_set[idx][1] for idx in range(len(valid_set))] 90 | gt_labels_string = [','.join([str(s.numpy()) for s in l]) 91 | for l in list(gt_labels)] 92 | with open(val_label_file, 'w') as file_writer: 93 | file_writer.write("\n".join(gt_labels_string)) 94 | 95 | return tr_loader, val_loader, val_label_file 96 | -------------------------------------------------------------------------------- /10_idet5_food_baseline/evaluation.py: -------------------------------------------------------------------------------- 1 | """ evaluation.py 2 | Replicated in the NSML leaderboard dataset, KoreanFood. 3 | """ 4 | 5 | import argparse 6 | import torch 7 | 8 | def compute_iou(box_a, box_b): 9 | # make [x1,y1,x2,y2] from [x,y,w,h] 10 | box_a[:, 2:] += box_a[:, :2] 11 | box_b[:, 2:] += box_b[:, :2] 12 | 13 | max_xy = torch.min(box_a[:, 2:], box_b[:, 2:]) 14 | min_xy = torch.max(box_a[:, :2], box_b[:, :2]) 15 | inter = torch.clamp((max_xy - min_xy), min=0) 16 | intersection = inter[:, 0] * inter[:, 1] 17 | 18 | area_a = (box_a[:, 2]-box_a[:, 0]) * (box_a[:, 3]-box_a[:, 1]) 19 | area_b = (box_b[:, 2]-box_b[:, 0]) * (box_b[:, 3]-box_b[:, 1]) 20 | union = area_a + area_b - intersection 21 | 22 | return intersection / union 23 | 24 | 25 | def read_prediction_gt(file_name): 26 | with open(file_name) as f: 27 | lines = f.readlines() 28 | boxes = [l.replace('\n', '').split(',') for l in lines] 29 | boxes_t = [] 30 | for box in boxes: 31 | boxes_t.append(torch.Tensor([float(b) for b in box]).unsqueeze(0)) 32 | boxes_t = torch.cat(boxes_t,0) 33 | return boxes_t 34 | 35 | def evaluation_metrics(prediction_file, testset_path): 36 | predictions = read_prediction_gt(prediction_file) 37 | gt_labels = read_prediction_gt(testset_path) 38 | total_iou = compute_iou(predictions, gt_labels) 39 | mean_iou = total_iou.mean().item() 40 | return mean_iou 41 | 42 | if __name__ == '__main__': 43 | args = argparse.ArgumentParser() 44 | args.add_argument('--prediction', type=str, default='pred.txt') 45 | config = args.parse_args() 46 | testset_path = '/data/10_idet5_food/test/test_label' 47 | 48 | try: 49 | print(evaluation_metrics(config.prediction, testset_path)) 50 | except: 51 | print('0') 52 | -------------------------------------------------------------------------------- /10_idet5_food_baseline/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/default_ml:cuda9_torch1.0 2 | 3 | from distutils.core import setup 4 | 5 | setup( 6 | name='nsml test example', 7 | version='1.0', 8 | install_requires=[ 9 | ] 10 | ) -------------------------------------------------------------------------------- /11_idet5_car_baseline/README.md: -------------------------------------------------------------------------------- 1 | ## 파라미터 5MB 이내 사진 내의 자동차 영역 추출하기 Baseline 2 | 3 | Task 4 | ``` 5 | 파라미터 5MB 이내 사진내의 자동차 영역 추출하기 6 | ``` 7 | 8 | Dataset Description: 9 | ``` 10 | \_train 11 | \_ train_data (folder) 12 | \_ *.jpg (자동차 사진) 13 | \_ *.txt (bounding box ([x,y,w,h])) 14 | 15 | # of training images: 1,749 16 | # of test images: 230 17 | ``` 18 | 19 | Evaluation Metric: 20 | ``` 21 | mean of intersection-over-union (mIoU) 22 | (1/N)*sum_{k=1}^{N}(IoU(pred_k, gt_k)) 23 | 24 | - N: number of test images 25 | - pred_k: bouding box prediction ([x,y,w,h]) of k-th image 26 | - gt_k: bouding box ground truth ([x,y,w,h]) of k-th image 27 | ``` 28 | 29 | How to run: 30 | 31 | ```bash 32 | nsml run -d 11_idet5_car -g 1 --cpus 2 -e main.py 33 | ``` 34 | 35 | How to list checkpoints saved: 36 | 37 | ```bash 38 | nsml model ls {USER_ID}/11_idet5_car/{SESSION_NUMBER} 39 | ``` 40 | 41 | How to submit: 42 | 43 | ```bash 44 | nsml submit {USER_ID}/11_idet5_car/{SESSION_NUMBER} {CHEKCOPOINT_NUMBER} 45 | ``` 46 | 47 | -------------------------------------------------------------------------------- /11_idet5_car_baseline/data_loader.py: -------------------------------------------------------------------------------- 1 | """ data_loader.py 2 | Replicated in the NSML leaderboard dataset, KoreanCarLocalize. 3 | """ 4 | 5 | from nsml import IS_ON_NSML, DATASET_PATH 6 | 7 | import os 8 | 9 | 10 | def feed_infer(output_file, infer_func): 11 | """" 12 | infer_func(function): inference 할 유저의 함수 13 | output_file(str): inference 후 결과값을 저장할 파일의 위치 패스 14 | (이위치에 결과를 저장해야 evaluation.py 에 올바른 인자로 들어옵니다.) 15 | """ 16 | if IS_ON_NSML: 17 | root_path = os.path.join(DATASET_PATH, 'test') 18 | else: 19 | root_path = '/home/data/NIPAKoreanCarLocalize/test' 20 | results = infer_func(root_path) 21 | results_str = [','.join(str(v) for v in l) for l in list(results)] 22 | print('write output') 23 | with open(output_file, 'w') as file_writer: 24 | file_writer.write("\n".join(results_str)) 25 | 26 | if os.stat(output_file).st_size == 0: 27 | raise AssertionError('output result of inference is nothing') 28 | 29 | 30 | def test_data_loader(root_path): 31 | return root_path 32 | -------------------------------------------------------------------------------- /11_idet5_car_baseline/data_local_loader.py: -------------------------------------------------------------------------------- 1 | from torch.utils import data 2 | from torchvision import datasets, transforms 3 | from torchvision.datasets.folder import default_loader 4 | import torch 5 | import os 6 | 7 | def get_transform(): 8 | normalize = transforms.Normalize( 9 | mean=[x / 255.0 for x in [125.3, 123.0, 113.9]], 10 | std=[x / 255.0 for x in [63.0, 62.1, 66.7]]) 11 | transform = [] 12 | transform.append(transforms.Resize((224,224))) 13 | transform.append(transforms.ToTensor()) 14 | transform.append(normalize) 15 | return transforms.Compose(transform) 16 | 17 | def target_transform(target, sizes): 18 | width,height = sizes 19 | 20 | target[0] = float(target[0]) / float(width) 21 | target[1] = float(target[1]) / float(height) 22 | target[2] = float(target[2]) / float(width) 23 | target[3] = float(target[3]) / float(height) 24 | 25 | return target 26 | 27 | 28 | class CustomDataset(data.Dataset): 29 | def __init__(self, root, transform, target_transform, loader=default_loader): 30 | self.root = root 31 | self.transform = transform 32 | self.target_transform = target_transform 33 | self.loader = loader 34 | 35 | self.images = [] 36 | self.boxes = [] 37 | dir_list = sorted(os.listdir(self.root)) 38 | 39 | for file_path in dir_list: 40 | if file_path.endswith('.jpg'): 41 | self.images.append(os.path.join(self.root, file_path)) 42 | 43 | box_file_path = file_path[:-4] + '.box' 44 | box_str = open(os.path.join(self.root, box_file_path), 'r', encoding='utf-8').read().split('\n')[0] 45 | box = [int(float(bb)) for bb in box_str.split(',')] 46 | self.boxes.append(box) 47 | 48 | def __len__(self): 49 | return len(self.images) 50 | 51 | def __getitem__(self, index): 52 | images = self.loader(self.images[index]) 53 | targets = torch.Tensor(self.boxes[index]) 54 | 55 | inputs = self.transform(images) 56 | width, height = images.size 57 | targets = self.target_transform(targets, (width, height)) 58 | 59 | return inputs, targets 60 | 61 | 62 | def data_loader(root, phase='train', batch_size=256): 63 | if phase == 'train': 64 | is_train = True 65 | elif phase == 'test': 66 | is_train = False 67 | else: 68 | raise KeyError 69 | input_transform = get_transform() 70 | dataset = CustomDataset(root, input_transform, target_transform) 71 | return data.DataLoader(dataset=dataset, 72 | batch_size=batch_size, 73 | shuffle=is_train) 74 | 75 | 76 | def data_loader_with_split(root, train_split=0.9, batch_size=256, val_label_file='./val_label'): 77 | input_transform = get_transform() 78 | dataset = CustomDataset(root, input_transform, target_transform) 79 | split_size = int(len(dataset) * train_split) 80 | train_set, valid_set = data.random_split(dataset, [split_size, len(dataset) - split_size]) 81 | tr_loader = data.DataLoader(dataset=train_set, 82 | batch_size=batch_size, 83 | shuffle=True) 84 | val_loader = data.DataLoader(dataset=valid_set, 85 | batch_size=batch_size, 86 | shuffle=False) 87 | 88 | 89 | gt_labels = [valid_set[idx][1] for idx in range(len(valid_set))] 90 | gt_labels_string = [','.join([str(s.numpy()) for s in l]) 91 | for l in list(gt_labels)] 92 | with open(val_label_file, 'w') as file_writer: 93 | file_writer.write("\n".join(gt_labels_string)) 94 | 95 | return tr_loader, val_loader, val_label_file 96 | -------------------------------------------------------------------------------- /11_idet5_car_baseline/evaluation.py: -------------------------------------------------------------------------------- 1 | """ evaluation.py 2 | Replicated in the NSML leaderboard dataset, KoreanCar. 3 | """ 4 | 5 | import argparse 6 | import torch 7 | 8 | def compute_iou(box_a, box_b): 9 | # make [x1,y1,x2,y2] from [x,y,w,h] 10 | box_a[:, 2:] += box_a[:, :2] 11 | box_b[:, 2:] += box_b[:, :2] 12 | 13 | max_xy = torch.min(box_a[:, 2:], box_b[:, 2:]) 14 | min_xy = torch.max(box_a[:, :2], box_b[:, :2]) 15 | inter = torch.clamp((max_xy - min_xy), min=0) 16 | intersection = inter[:, 0] * inter[:, 1] 17 | 18 | area_a = (box_a[:, 2]-box_a[:, 0]) * (box_a[:, 3]-box_a[:, 1]) 19 | area_b = (box_b[:, 2]-box_b[:, 0]) * (box_b[:, 3]-box_b[:, 1]) 20 | union = area_a + area_b - intersection 21 | 22 | return intersection / union 23 | 24 | def read_prediction_gt(file_name): 25 | with open(file_name) as f: 26 | lines = f.readlines() 27 | boxes = [l.replace('\n', '').split(',') for l in lines] 28 | boxes_t = [] 29 | for box in boxes: 30 | boxes_t.append(torch.Tensor([float(b) for b in box]).unsqueeze(0)) 31 | boxes_t = torch.cat(boxes_t,0) 32 | return boxes_t 33 | 34 | def evaluation_metrics(prediction_file, testset_path): 35 | predictions = read_prediction_gt(prediction_file) 36 | gt_labels = read_prediction_gt(testset_path) 37 | total_iou = compute_iou(predictions, gt_labels) 38 | mean_iou = total_iou.mean().item() 39 | return mean_iou 40 | 41 | if __name__ == '__main__': 42 | args = argparse.ArgumentParser() 43 | args.add_argument('--prediction', type=str, default='pred.txt') 44 | config = args.parse_args() 45 | testset_path = '/data/11_idet5_car/test/test_label' 46 | 47 | try: 48 | print(evaluation_metrics(config.prediction, testset_path)) 49 | except: 50 | print('0') 51 | -------------------------------------------------------------------------------- /11_idet5_car_baseline/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/default_ml:cuda9_torch1.0 2 | 3 | from distutils.core import setup 4 | 5 | setup( 6 | name='nsml test example', 7 | version='1.0', 8 | install_requires=[ 9 | ] 10 | ) -------------------------------------------------------------------------------- /12_idet_food_baseline/README.md: -------------------------------------------------------------------------------- 1 | ## 사진 내의 음식 영역 추출하기 Baseline 2 | 3 | Task 4 | ``` 5 | 사진 내의 음식 영역 추출하기 6 | ``` 7 | 8 | Dataset Description: 9 | ``` 10 | \_train 11 | \_ train_data (folder) 12 | \_ *.jpg (음식 사진) 13 | \_ *.txt (bounding box ([x,y,w,h])) 14 | 15 | # of training images: 40,612 16 | # of test images: 1,791 17 | ``` 18 | 19 | Evaluation Metric: 20 | ``` 21 | mean of intersection-over-union (mIoU) 22 | (1/N)*sum_{k=1}^{N}(IoU(pred_k, gt_k)) 23 | 24 | - N: number of test images 25 | - pred_k: bouding box prediction ([x,y,w,h]) of k-th image 26 | - gt_k: bouding box ground truth ([x,y,w,h]) of k-th image 27 | ``` 28 | 29 | 30 | How to run: 31 | 32 | ```bash 33 | nsml run -d 12_idet_food -g 1 --cpus 2 -e main.py 34 | ``` 35 | 36 | How to list checkpoints saved: 37 | 38 | ```bash 39 | nsml model ls {USER_ID}/12_idet_food/{SESSION_NUMBER} 40 | ``` 41 | 42 | How to submit: 43 | 44 | ```bash 45 | nsml submit {USER_ID}/12_idet_food/{SESSION_NUMBER} {CHEKCOPOINT_NUMBER} 46 | ``` 47 | 48 | -------------------------------------------------------------------------------- /12_idet_food_baseline/data_loader.py: -------------------------------------------------------------------------------- 1 | """ data_loader.py 2 | Replicated in the NSML leaderboard dataset, KoreanFoodLocalize. 3 | """ 4 | 5 | from nsml import IS_ON_NSML, DATASET_PATH 6 | 7 | import os 8 | 9 | def feed_infer(output_file, infer_func): 10 | """" 11 | infer_func(function): inference 할 유저의 함수 12 | output_file(str): inference 후 결과값을 저장할 파일의 위치 패스 13 | (이위치에 결과를 저장해야 evaluation.py 에 올바른 인자로 들어옵니다.) 14 | """ 15 | if IS_ON_NSML: 16 | root_path = os.path.join(DATASET_PATH, 'test') 17 | else: 18 | root_path = '/home/data/NIPAKoreanFoodLocalize/test' 19 | results = infer_func(root_path) 20 | results_str = [','.join(str(v) for v in l) for l in list(results)] 21 | print('write output') 22 | with open(output_file, 'w') as file_writer: 23 | file_writer.write("\n".join(results_str)) 24 | 25 | if os.stat(output_file).st_size == 0: 26 | raise AssertionError('output result of inference is nothing') 27 | 28 | 29 | def test_data_loader(root_path): 30 | return root_path 31 | -------------------------------------------------------------------------------- /12_idet_food_baseline/data_local_loader.py: -------------------------------------------------------------------------------- 1 | from torch.utils import data 2 | from torchvision import datasets, transforms 3 | from torchvision.datasets.folder import default_loader 4 | import torch 5 | import os 6 | 7 | def get_transform(): 8 | normalize = transforms.Normalize( 9 | mean=[x / 255.0 for x in [125.3, 123.0, 113.9]], 10 | std=[x / 255.0 for x in [63.0, 62.1, 66.7]]) 11 | transform = [] 12 | transform.append(transforms.Resize((224,224))) 13 | transform.append(transforms.ToTensor()) 14 | transform.append(normalize) 15 | return transforms.Compose(transform) 16 | 17 | def target_transform(target, sizes): 18 | width,height = sizes 19 | 20 | target[0] = float(target[0]) / float(width) 21 | target[1] = float(target[1]) / float(height) 22 | target[2] = float(target[2]) / float(width) 23 | target[3] = float(target[3]) / float(height) 24 | 25 | return target 26 | 27 | 28 | class CustomDataset(data.Dataset): 29 | def __init__(self, root, transform, target_transform, loader=default_loader): 30 | self.root = root 31 | self.transform = transform 32 | self.target_transform = target_transform 33 | self.loader = loader 34 | 35 | self.images = [] 36 | self.boxes = [] 37 | dir_list = sorted(os.listdir(self.root)) 38 | 39 | for file_path in dir_list: 40 | if file_path.endswith('.jpg'): 41 | self.images.append(os.path.join(self.root, file_path)) 42 | 43 | box_file_path = file_path[:-4] + '.txt' 44 | box_str = open(os.path.join(self.root, box_file_path), 'r', encoding='utf-8').read().split('\n')[0] 45 | box = [int(float(bb)) for bb in box_str.split(',')] 46 | self.boxes.append(box) 47 | 48 | def __len__(self): 49 | return len(self.images) 50 | 51 | def __getitem__(self, index): 52 | images = self.loader(self.images[index]) 53 | targets = torch.Tensor(self.boxes[index]) 54 | 55 | inputs = self.transform(images) 56 | width, height = images.size 57 | targets = self.target_transform(targets, (width, height)) 58 | 59 | return inputs, targets 60 | 61 | 62 | def data_loader(root, phase='train', batch_size=256): 63 | if phase == 'train': 64 | is_train = True 65 | elif phase == 'test': 66 | is_train = False 67 | else: 68 | raise KeyError 69 | input_transform = get_transform() 70 | dataset = CustomDataset(root, input_transform, target_transform) 71 | return data.DataLoader(dataset=dataset, 72 | batch_size=batch_size, 73 | shuffle=is_train) 74 | 75 | 76 | def data_loader_with_split(root, train_split=0.9, batch_size=256, val_label_file='./val_label'): 77 | input_transform = get_transform() 78 | dataset = CustomDataset(root, input_transform, target_transform) 79 | split_size = int(len(dataset) * train_split) 80 | train_set, valid_set = data.random_split(dataset, [split_size, len(dataset) - split_size]) 81 | tr_loader = data.DataLoader(dataset=train_set, 82 | batch_size=batch_size, 83 | shuffle=True) 84 | val_loader = data.DataLoader(dataset=valid_set, 85 | batch_size=batch_size, 86 | shuffle=False) 87 | 88 | 89 | gt_labels = [valid_set[idx][1] for idx in range(len(valid_set))] 90 | gt_labels_string = [','.join([str(s.numpy()) for s in l]) 91 | for l in list(gt_labels)] 92 | with open(val_label_file, 'w') as file_writer: 93 | file_writer.write("\n".join(gt_labels_string)) 94 | 95 | return tr_loader, val_loader, val_label_file 96 | -------------------------------------------------------------------------------- /12_idet_food_baseline/evaluation.py: -------------------------------------------------------------------------------- 1 | """ evaluation.py 2 | Replicated in the NSML leaderboard dataset, KoreanFood. 3 | """ 4 | 5 | import argparse 6 | import torch 7 | 8 | def compute_iou(box_a, box_b): 9 | # make [x1,y1,x2,y2] from [x,y,w,h] 10 | box_a[:, 2:] += box_a[:, :2] 11 | box_b[:, 2:] += box_b[:, :2] 12 | 13 | max_xy = torch.min(box_a[:, 2:], box_b[:, 2:]) 14 | min_xy = torch.max(box_a[:, :2], box_b[:, :2]) 15 | inter = torch.clamp((max_xy - min_xy), min=0) 16 | intersection = inter[:, 0] * inter[:, 1] 17 | 18 | area_a = (box_a[:, 2]-box_a[:, 0]) * (box_a[:, 3]-box_a[:, 1]) 19 | area_b = (box_b[:, 2]-box_b[:, 0]) * (box_b[:, 3]-box_b[:, 1]) 20 | union = area_a + area_b - intersection 21 | 22 | return intersection / union 23 | 24 | 25 | def read_prediction_gt(file_name): 26 | with open(file_name) as f: 27 | lines = f.readlines() 28 | boxes = [l.replace('\n', '').split(',') for l in lines] 29 | boxes_t = [] 30 | for box in boxes: 31 | boxes_t.append(torch.Tensor([float(b) for b in box]).unsqueeze(0)) 32 | boxes_t = torch.cat(boxes_t,0) 33 | return boxes_t 34 | 35 | def evaluation_metrics(prediction_file, testset_path): 36 | predictions = read_prediction_gt(prediction_file) 37 | gt_labels = read_prediction_gt(testset_path) 38 | total_iou = compute_iou(predictions, gt_labels) 39 | mean_iou = total_iou.mean().item() 40 | return mean_iou 41 | 42 | if __name__ == '__main__': 43 | args = argparse.ArgumentParser() 44 | args.add_argument('--prediction', type=str, default='pred.txt') 45 | config = args.parse_args() 46 | testset_path = '/data/12_idet_food/test/test_label' 47 | 48 | try: 49 | print(evaluation_metrics(config.prediction, testset_path)) 50 | except: 51 | print('0') 52 | -------------------------------------------------------------------------------- /12_idet_food_baseline/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/default_ml:cuda9_torch1.0 2 | 3 | from distutils.core import setup 4 | 5 | setup( 6 | name='nsml test example', 7 | version='1.0', 8 | install_requires=[ 9 | ] 10 | ) -------------------------------------------------------------------------------- /13_idet_car_baseline/README.md: -------------------------------------------------------------------------------- 1 | ## 사진 내의 자동차 영역 추출하기 Baseline 2 | 3 | Task 4 | ``` 5 | 사진 내의 자동차 영역 추출하기 6 | ``` 7 | 8 | Dataset Description: 9 | ``` 10 | \_train 11 | \_ train_data (folder) 12 | \_ *.jpg (자동차 사진) 13 | \_ *.txt (bounding box ([x,y,w,h])) 14 | 15 | # of training images: 1,749 16 | # of test images: 230 17 | ``` 18 | 19 | Evaluation Metric: 20 | ``` 21 | mean of intersection-over-union (mIoU) 22 | (1/N)*sum_{k=1}^{N}(IoU(pred_k, gt_k)) 23 | 24 | - N: number of test images 25 | - pred_k: bouding box prediction ([x,y,w,h]) of k-th image 26 | - gt_k: bouding box ground truth ([x,y,w,h]) of k-th image 27 | ``` 28 | 29 | How to run: 30 | 31 | ```bash 32 | nsml run -d 13_idet_car -g 1 --cpus 2 -e main.py 33 | ``` 34 | 35 | How to list checkpoints saved: 36 | 37 | ```bash 38 | nsml model ls {USER_ID}/13_idet_car/{SESSION_NUMBER} 39 | ``` 40 | 41 | How to submit: 42 | 43 | ```bash 44 | nsml submit {USER_ID}/13_idet_car/{SESSION_NUMBER} {CHEKCOPOINT_NUMBER} 45 | ``` 46 | 47 | -------------------------------------------------------------------------------- /13_idet_car_baseline/data_loader.py: -------------------------------------------------------------------------------- 1 | """ data_loader.py 2 | Replicated in the NSML leaderboard dataset, KoreanCarLocalize. 3 | """ 4 | 5 | from nsml import IS_ON_NSML, DATASET_PATH 6 | 7 | import os 8 | 9 | 10 | def feed_infer(output_file, infer_func): 11 | """" 12 | infer_func(function): inference 할 유저의 함수 13 | output_file(str): inference 후 결과값을 저장할 파일의 위치 패스 14 | (이위치에 결과를 저장해야 evaluation.py 에 올바른 인자로 들어옵니다.) 15 | """ 16 | if IS_ON_NSML: 17 | root_path = os.path.join(DATASET_PATH, 'test') 18 | else: 19 | root_path = '/home/data/NIPAKoreanCarLocalize/test' 20 | results = infer_func(root_path) 21 | results_str = [','.join(str(v) for v in l) for l in list(results)] 22 | print('write output') 23 | with open(output_file, 'w') as file_writer: 24 | file_writer.write("\n".join(results_str)) 25 | 26 | if os.stat(output_file).st_size == 0: 27 | raise AssertionError('output result of inference is nothing') 28 | 29 | 30 | def test_data_loader(root_path): 31 | return root_path 32 | -------------------------------------------------------------------------------- /13_idet_car_baseline/data_local_loader.py: -------------------------------------------------------------------------------- 1 | from torch.utils import data 2 | from torchvision import datasets, transforms 3 | from torchvision.datasets.folder import default_loader 4 | import torch 5 | import os 6 | 7 | def get_transform(): 8 | normalize = transforms.Normalize( 9 | mean=[x / 255.0 for x in [125.3, 123.0, 113.9]], 10 | std=[x / 255.0 for x in [63.0, 62.1, 66.7]]) 11 | transform = [] 12 | transform.append(transforms.Resize((224,224))) 13 | transform.append(transforms.ToTensor()) 14 | transform.append(normalize) 15 | return transforms.Compose(transform) 16 | 17 | def target_transform(target, sizes): 18 | width,height = sizes 19 | 20 | target[0] = float(target[0]) / float(width) 21 | target[1] = float(target[1]) / float(height) 22 | target[2] = float(target[2]) / float(width) 23 | target[3] = float(target[3]) / float(height) 24 | 25 | return target 26 | 27 | 28 | class CustomDataset(data.Dataset): 29 | def __init__(self, root, transform, target_transform, loader=default_loader): 30 | self.root = root 31 | self.transform = transform 32 | self.target_transform = target_transform 33 | self.loader = loader 34 | 35 | self.images = [] 36 | self.boxes = [] 37 | dir_list = sorted(os.listdir(self.root)) 38 | 39 | for file_path in dir_list: 40 | if file_path.endswith('.jpg'): 41 | self.images.append(os.path.join(self.root, file_path)) 42 | 43 | box_file_path = file_path[:-4] + '.box' 44 | box_str = open(os.path.join(self.root, box_file_path), 'r', encoding='utf-8').read().split('\n')[0] 45 | box = [int(float(bb)) for bb in box_str.split(',')] 46 | self.boxes.append(box) 47 | 48 | def __len__(self): 49 | return len(self.images) 50 | 51 | def __getitem__(self, index): 52 | images = self.loader(self.images[index]) 53 | targets = torch.Tensor(self.boxes[index]) 54 | 55 | inputs = self.transform(images) 56 | width, height = images.size 57 | targets = self.target_transform(targets, (width, height)) 58 | 59 | return inputs, targets 60 | 61 | 62 | def data_loader(root, phase='train', batch_size=256): 63 | if phase == 'train': 64 | is_train = True 65 | elif phase == 'test': 66 | is_train = False 67 | else: 68 | raise KeyError 69 | input_transform = get_transform() 70 | dataset = CustomDataset(root, input_transform, target_transform) 71 | return data.DataLoader(dataset=dataset, 72 | batch_size=batch_size, 73 | shuffle=is_train) 74 | 75 | 76 | def data_loader_with_split(root, train_split=0.9, batch_size=256, val_label_file='./val_label'): 77 | input_transform = get_transform() 78 | dataset = CustomDataset(root, input_transform, target_transform) 79 | split_size = int(len(dataset) * train_split) 80 | train_set, valid_set = data.random_split(dataset, [split_size, len(dataset) - split_size]) 81 | tr_loader = data.DataLoader(dataset=train_set, 82 | batch_size=batch_size, 83 | shuffle=True) 84 | val_loader = data.DataLoader(dataset=valid_set, 85 | batch_size=batch_size, 86 | shuffle=False) 87 | 88 | 89 | gt_labels = [valid_set[idx][1] for idx in range(len(valid_set))] 90 | gt_labels_string = [','.join([str(s.numpy()) for s in l]) 91 | for l in list(gt_labels)] 92 | with open(val_label_file, 'w') as file_writer: 93 | file_writer.write("\n".join(gt_labels_string)) 94 | 95 | return tr_loader, val_loader, val_label_file 96 | -------------------------------------------------------------------------------- /13_idet_car_baseline/evaluation.py: -------------------------------------------------------------------------------- 1 | """ evaluation.py 2 | Replicated in the NSML leaderboard dataset, KoreanCar. 3 | """ 4 | 5 | import argparse 6 | import torch 7 | 8 | def compute_iou(box_a, box_b): 9 | # make [x1,y1,x2,y2] from [x,y,w,h] 10 | box_a[:, 2:] += box_a[:, :2] 11 | box_b[:, 2:] += box_b[:, :2] 12 | 13 | max_xy = torch.min(box_a[:, 2:], box_b[:, 2:]) 14 | min_xy = torch.max(box_a[:, :2], box_b[:, :2]) 15 | inter = torch.clamp((max_xy - min_xy), min=0) 16 | intersection = inter[:, 0] * inter[:, 1] 17 | 18 | area_a = (box_a[:, 2]-box_a[:, 0]) * (box_a[:, 3]-box_a[:, 1]) 19 | area_b = (box_b[:, 2]-box_b[:, 0]) * (box_b[:, 3]-box_b[:, 1]) 20 | union = area_a + area_b - intersection 21 | 22 | return intersection / union 23 | 24 | def read_prediction_gt(file_name): 25 | with open(file_name) as f: 26 | lines = f.readlines() 27 | boxes = [l.replace('\n', '').split(',') for l in lines] 28 | boxes_t = [] 29 | for box in boxes: 30 | boxes_t.append(torch.Tensor([float(b) for b in box]).unsqueeze(0)) 31 | boxes_t = torch.cat(boxes_t,0) 32 | return boxes_t 33 | 34 | def evaluation_metrics(prediction_file, testset_path): 35 | predictions = read_prediction_gt(prediction_file) 36 | gt_labels = read_prediction_gt(testset_path) 37 | total_iou = compute_iou(predictions, gt_labels) 38 | mean_iou = total_iou.mean().item() 39 | return mean_iou 40 | 41 | if __name__ == '__main__': 42 | args = argparse.ArgumentParser() 43 | args.add_argument('--prediction', type=str, default='pred.txt') 44 | config = args.parse_args() 45 | testset_path = '/data/13_idet_car/test/test_label' 46 | 47 | try: 48 | print(evaluation_metrics(config.prediction, testset_path)) 49 | except: 50 | print('0') 51 | -------------------------------------------------------------------------------- /13_idet_car_baseline/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/default_ml:cuda9_torch1.0 2 | 3 | from distutils.core import setup 4 | 5 | setup( 6 | name='nsml test example', 7 | version='1.0', 8 | install_requires=[ 9 | ] 10 | ) -------------------------------------------------------------------------------- /14_ig5_inpaint/14_ig5_inpaint/data_loader.py: -------------------------------------------------------------------------------- 1 | """ data_loader.py 2 | Replicated in the NSML leaderboard dataset, KoreanFood. 3 | """ 4 | 5 | try: 6 | import nsml 7 | dir_data_root = nsml.DATASET_PATH 8 | use_nsml = True 9 | except ImportError: 10 | dir_data_root = '/home/data/nipa_korean_faces' 11 | print('dir_data_root:', dir_data_root) 12 | use_nsml = False 13 | 14 | import os 15 | import shutil 16 | import numpy as np 17 | 18 | 19 | def feed_infer(output_file, infer_func): 20 | """" 21 | infer_func(function): inference 할 유저의 함수 22 | output_file(str): inference 후 결과값을 저장할 파일의 위치 패스 23 | (이위치에 결과를 저장해야 evaluation.py 에 올바른 인자로 들어옵니다.) 24 | """ 25 | if use_nsml: 26 | root_path = os.path.join(dir_data_root, 'test') 27 | else: 28 | root_path = '/home/data/14_ig5_inpaint/test' 29 | fnames, x_hats = infer_func(root_path) 30 | np.savez_compressed(output_file, fnames=fnames, x_hats=x_hats) 31 | shutil.move(output_file + '.npz', output_file) 32 | 33 | 34 | def test_data_loader(root_path): 35 | return root_path 36 | -------------------------------------------------------------------------------- /14_ig5_inpaint/14_ig5_inpaint/evaluation.py: -------------------------------------------------------------------------------- 1 | """ evaluation.py 2 | Replicated in the NSML leaderboard dataset, KoreanFood. 3 | """ 4 | 5 | import os 6 | import argparse 7 | 8 | import numpy as np 9 | from PIL import Image 10 | import torch 11 | from torchvision import transforms 12 | 13 | 14 | def l1_loss(x_hat, x_GT): 15 | loss = torch.abs(x_hat - x_GT).sum(dim=[1, 2, 3]).mean() 16 | return loss 17 | 18 | 19 | def load_image(fname, image_size=128): 20 | transform = transforms.Compose([transforms.Resize((image_size, image_size)), 21 | transforms.ToTensor()]) 22 | image = Image.open(fname).convert('RGB') 23 | image = transform(image) 24 | return image 25 | 26 | 27 | def read_prediction_gt(dname, fnames): 28 | images = [] 29 | for fname in fnames: 30 | fname = os.path.join(dname, fname) 31 | image = load_image(fname) 32 | images.append(image) 33 | return torch.stack(images, dim=0) 34 | 35 | 36 | def evaluation_metrics(path_pred, path_GT): 37 | """ 38 | Args: 39 | path_pred: str 40 | path_GT: str 41 | Returns: 42 | loss: float L1 loss 43 | """ 44 | fin = np.load(path_pred) 45 | fnames = fin['fnames'] 46 | x_hats = fin['x_hats'] 47 | x_hats = torch.Tensor(x_hats) 48 | fin.close() 49 | if os.path.isdir(path_GT): 50 | x_GTs = read_prediction_gt(path_GT, fnames) 51 | else: 52 | fin = np.load(path_GT) 53 | x_GTs = fin['x_GTs'] 54 | x_GTs = torch.Tensor(x_GTs) 55 | fin.close() 56 | loss = float(l1_loss(x_hats, x_GTs)) 57 | return loss 58 | 59 | 60 | if __name__ == '__main__': 61 | args = argparse.ArgumentParser() 62 | args.add_argument('--prediction', type=str, default='pred.txt') 63 | config = args.parse_args() 64 | test_label_path = '/data/14_ig5_inpaint/test/test_label' 65 | 66 | try: 67 | print(evaluation_metrics(config.prediction, test_label_path)) 68 | except Exception: 69 | print('999999') 70 | -------------------------------------------------------------------------------- /14_ig5_inpaint/README.md: -------------------------------------------------------------------------------- 1 | # 14_ig5_inpaint 2 | 3 | ## Task description 4 | For given input images and their corresponding masks, synthesize GT images. 5 | 6 | ## Evaluation metric 7 | L1 distance in RGB space between synthesized image and GT 8 | 9 | ## example data 10 | input, mask, GT 11 | 12 | 13 | 14 | ## stats 15 | 16 | | Name of the dataset | 14_ig5_inpaint | 17 | | - | - | 18 | | Number of train data | 48000 | 19 | | Number of test data | 10000 | 20 | 21 | 22 | ## How to run: 23 | 24 | ```bash 25 | nsml run -d 14_ig5_inpaint -e main.py 26 | ``` 27 | 28 | ## How to list checkpoints saved: 29 | 30 | ```bash 31 | # nsml model ls YOUR_ID/14_ig5_inpaint/SESSION_NUM 32 | nsml model ls nipachallenger/14_ig5_inpaint/1 33 | 34 | ``` 35 | 36 | ## How to submit: 37 | 38 | ```bash 39 | # nsml submit YOUR_ID/14_ig5_inpaint/SESSION_NUM CKPT_EPOCH 40 | nsml submit nipachallenger/14_ig5_inpaint/1 9 41 | ``` 42 | -------------------------------------------------------------------------------- /14_ig5_inpaint/data_local_loader.py: -------------------------------------------------------------------------------- 1 | import os 2 | from PIL import Image 3 | from torch.utils import data 4 | from torchvision import datasets, transforms 5 | 6 | from utils import cutout 7 | 8 | 9 | class CustomDataset(datasets.ImageFolder): 10 | def __getitem__(self, index): 11 | """ 12 | Args: 13 | index (int): Index 14 | Returns: 15 | tuple: (fname, masked, mask, GT) 16 | """ 17 | path, target = self.samples[index] 18 | GT = self.loader(path) 19 | if self.transform is not None: 20 | GT = self.transform(GT) 21 | 22 | masked, mask = cutout(GT) 23 | fname = os.path.basename(path) 24 | 25 | return fname, masked, mask, GT 26 | 27 | 28 | class TestDataset(data.Dataset): 29 | def __init__(self, root_dir, transform=None): 30 | self.root_dir = root_dir 31 | self.transform = transform 32 | self.dname_masked = os.path.join(root_dir, 'masked') 33 | self.dname_mask = os.path.join(root_dir, 'mask') 34 | self.samples = sorted(os.listdir(self.dname_masked)) 35 | 36 | def __getitem__(self, index): 37 | """ 38 | Args: 39 | index (int): Index 40 | Returns: 41 | tuple: (masked, mask) 42 | """ 43 | fname = self.samples[index] 44 | fname_masked = os.path.join(self.dname_masked, fname) 45 | fname_mask = os.path.join(self.dname_mask, fname) 46 | masked = Image.open(fname_masked).convert('RGB') 47 | mask = Image.open(fname_mask).convert('RGB') 48 | if self.transform is not None: 49 | masked = self.transform(masked) 50 | mask = self.transform(mask) 51 | mask = (mask > 0.5).float() 52 | 53 | return fname, masked, mask[:1] 54 | 55 | def __len__(self): 56 | return len(self.samples) 57 | 58 | 59 | def data_loader(root, phase='train', batch_size=32, num_workers=2): 60 | if phase == 'train': 61 | is_train = True 62 | Dset = CustomDataset 63 | elif phase == 'test': 64 | is_train = False 65 | Dset = TestDataset 66 | else: 67 | raise KeyError 68 | transform = transforms.Compose([transforms.Resize((128, 128)), 69 | transforms.ToTensor(), 70 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 71 | dataset = Dset(root, transform=transform) 72 | dataloader = data.DataLoader(dataset, batch_size=batch_size, shuffle=is_train, 73 | num_workers=num_workers) 74 | return dataloader 75 | 76 | 77 | def data_loader_with_split(root, train_split=0.9, batch_size=32, num_workers=2): 78 | transform = transforms.Compose([transforms.Resize((128, 128)), 79 | transforms.ToTensor(), 80 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 81 | dataset = CustomDataset(root=root, transform=transform) 82 | train_split_size = int(len(dataset) * train_split) 83 | val_split_size = len(dataset) - train_split_size 84 | # val_split_size = 64 85 | # train_split_size = len(dataset) - val_split_size 86 | if train_split_size > 0: 87 | train_set, valid_set = data.random_split(dataset, [train_split_size, val_split_size]) 88 | tr_loader = data.DataLoader(dataset=train_set, 89 | batch_size=batch_size, 90 | shuffle=True, 91 | num_workers=num_workers) 92 | else: 93 | tr_loader = None 94 | valid_set = dataset 95 | val_loader = data.DataLoader(dataset=valid_set, 96 | batch_size=batch_size, 97 | shuffle=False, 98 | num_workers=num_workers) 99 | return tr_loader, val_loader 100 | -------------------------------------------------------------------------------- /14_ig5_inpaint/example/x_GT.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ai-starthon/AI_Starthon2019/eba6db7ff75b6f69085705cb423ec16f85b946e5/14_ig5_inpaint/example/x_GT.png -------------------------------------------------------------------------------- /14_ig5_inpaint/example/x_input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ai-starthon/AI_Starthon2019/eba6db7ff75b6f69085705cb423ec16f85b946e5/14_ig5_inpaint/example/x_input.png -------------------------------------------------------------------------------- /14_ig5_inpaint/example/x_mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ai-starthon/AI_Starthon2019/eba6db7ff75b6f69085705cb423ec16f85b946e5/14_ig5_inpaint/example/x_mask.png -------------------------------------------------------------------------------- /14_ig5_inpaint/nsml_run.sh: -------------------------------------------------------------------------------- 1 | set -x 2 | nsml run -d 14_ig5_inpaint 3 | -------------------------------------------------------------------------------- /14_ig5_inpaint/nsml_submit.sh: -------------------------------------------------------------------------------- 1 | session_num=$1 2 | iter=$2 3 | set -x 4 | nsml submit YOUR_ID/nipa_inpaint/$session_num $iter 5 | -------------------------------------------------------------------------------- /14_ig5_inpaint/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/ml:cuda9.0-cudnn7-tf-1.11torch1.0keras2.2 2 | from distutils.core import setup 3 | setup( 4 | name='inpaint', 5 | version='1.0', 6 | install_requires=[ 7 | ] 8 | ) 9 | -------------------------------------------------------------------------------- /14_ig5_inpaint/utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from PIL import Image 3 | 4 | import torch 5 | from torchvision import utils as tutils 6 | from torchvision import transforms 7 | 8 | 9 | def compose(x_input, x_hat, mask): 10 | x_composed = x_input * mask + x_hat * (1 - mask) 11 | return x_composed 12 | 13 | 14 | def l1_loss(x_composed, x_GT): 15 | loss = torch.abs(x_composed - x_GT).sum(dim=[1, 2, 3]).mean() 16 | return loss 17 | 18 | 19 | def cutout(images): 20 | # for ease of use 21 | _, height, width = images.shape 22 | 23 | alpha = 1 24 | lam = np.random.beta(alpha, alpha) 25 | 26 | # cutout images 27 | mask = torch.ones_like(images) 28 | from_x, from_y, to_x, to_y = random_bbox(width, height, lam) 29 | mask[:, from_y:to_y, from_x:to_x] = 0 30 | images = images * mask 31 | return images, mask[:1, :, :] 32 | 33 | 34 | def random_bbox(width, height, lam): 35 | cut_ratio = np.sqrt(1. - lam) 36 | cut_width = (width * cut_ratio).astype(np.int) 37 | cut_height = (height * cut_ratio).astype(np.int) 38 | # uniform 39 | from_x = np.random.randint(width - cut_width) 40 | from_y = np.random.randint(height - cut_height) 41 | to_x = from_x + cut_width 42 | to_y = from_y + cut_height 43 | return from_x, from_y, to_x, to_y 44 | 45 | 46 | def normalize(x): 47 | out = x * 2 - 1 48 | return out 49 | 50 | 51 | def denormalize(x): 52 | out = (x + 1) / 2 53 | return out.clamp_(0, 1) 54 | 55 | 56 | def save_image(x, fname): 57 | x = denormalize(x) 58 | tutils.save_image(x, fname, nrow=1, padding=0) 59 | 60 | 61 | def load_image(fname, image_size=128): 62 | transform = transforms.Compose([transforms.Resize((image_size, image_size)), 63 | transforms.ToTensor()]) 64 | image = Image.open(fname).convert('RGB') 65 | image = transform(image) 66 | return image 67 | -------------------------------------------------------------------------------- /15_ig_super/README.md: -------------------------------------------------------------------------------- 1 | # NIPAFacesSR 2 | Baseline code for Face Super Resolution (x4) 3 | ### Dataset description: 15_ig_super 4 | 5 | 6 | ``` 7 | 15_ig_super 8 | \_train 9 | \_ train_data (folder) 10 | \_ hr (folder) 11 | \_ lr (folder) 12 | ``` 13 | Please refer to http://www.aihub.or.kr/content/140 for entire face dataset description. Among 200 people dataset, we chose randomly 58 people and again reduced the number of data by randomly selecting 100 images per person. Originally, the dataset had 32,400 per person, which is too much for challenge purpose. Therefore, our images would include random lightings, accessories, views and emotions. 14 | 15 | The lr folder has 56 x 56 RGB images (total 48,000 images, 100 images per person: 48 people), which are inputs and the hr folder includes corresponding 224 x 224 resolution RGB images, which are labels. If you want to train your model with RGB input format, please change the data_local_loader.py accordingly. Currently, the exemplar code is only using the _"Y"_ channel (YCbCr) of the image, which means that the input of the model has a single channel. 16 | 17 | 18 | ### Evaluation description 19 | Please note that **PSNR is calculated using the _"Y"_ channel (YCbCr) only**. Therefore, your model **should be able to give the best performance on Y channel** and the output of the model should be converted to a single channel accordingly. For example, you can still make a model that use the RGB channel images as input and output, but **you should make sure that the infer function returns a "Y"-channel to the system.** Please refer to evaluation.py file for more details. The test dataset has 56 x 56 RGB images (total 10,000 images, 100 images per person: 10 people), which has no overlap to the training data id (different people). 20 | 21 | ### How to run 22 | ``` 23 | nsml run -d 15_ig_super -g 1 --memory 12G --cpus 1 -e main.py 24 | ``` 25 | : You are running "main.py" as main on the dataset "15_ig_super" 26 | ### How to check your session logs 27 | ``` 28 | nsml logs -f [Session Name Here] 29 | e.g., nsml logs -f KR18838/15_ig_super/4 30 | ``` 31 | : -f is given to follow the logs. Instead of this, you can simply watch the web interface to track the logs. 32 | ### How to submit 33 | ``` 34 | nsml submit [Session Name Here] [Model # Here] 35 | e.g., nsml submit KR18838/15_ig_super/4 1 # ID/Dataset/Session# Model# 36 | ``` 37 | : You are submitting your learned 1st model (which gives the best score, usually it would be last epoch model) of session KR18838/15_ig_super/4 38 | ### How to check the leaderboard 39 | ``` 40 | nsml dataset board 15_ig_super 41 | ``` 42 | -------------------------------------------------------------------------------- /15_ig_super/data_description.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ai-starthon/AI_Starthon2019/eba6db7ff75b6f69085705cb423ec16f85b946e5/15_ig_super/data_description.png -------------------------------------------------------------------------------- /15_ig_super/data_loader.py: -------------------------------------------------------------------------------- 1 | from nsml import IS_ON_NSML, DATASET_PATH 2 | 3 | import os 4 | import shutil 5 | import numpy as np 6 | 7 | def feed_infer(output_file, infer_func): 8 | """" 9 | infer_func(function): inference 할 유저의 함수 (defined in nsml.bind function) 10 | output_file(str): inference 후 결과값을 저장할 파일의 위치 패스 11 | (이위치에 결과를 저장해야 evaluation.py 에 올바른 인자로 들어옵니다.) 12 | """ 13 | if IS_ON_NSML: 14 | root_path = os.path.join(DATASET_PATH, 'test') 15 | else: 16 | root_path = '/home/data/nipa_faces_sr_tmp2/test' # local datapath 17 | 18 | # preds: numpy array for entire test data 19 | preds = infer_func(root_path) 20 | print('write output') 21 | # save model output to output_file (filepath) 22 | 23 | np.savez_compressed(output_file, output=preds) 24 | shutil.move(output_file+'.npz',output_file) 25 | 26 | -------------------------------------------------------------------------------- /15_ig_super/data_local_loader.py: -------------------------------------------------------------------------------- 1 | import os 2 | from PIL import Image 3 | from torch.utils import data 4 | from torchvision import datasets, transforms 5 | 6 | class TrainDataset(data.Dataset): 7 | def __init__(self, root_dir, transform=None): 8 | self.root_dir = root_dir 9 | self.transform = transform 10 | self.dname_lr = os.path.join(root_dir, 'lr') 11 | self.dname_hr = os.path.join(root_dir, 'hr') 12 | self.samples = sorted(os.listdir(self.dname_hr)) 13 | 14 | def __getitem__(self, index): 15 | """ 16 | Args: 17 | index (int): Index 18 | Returns: 19 | tuple: (lr, hr) 20 | """ 21 | fname = self.samples[index] 22 | fname_lr = os.path.join(self.dname_lr, fname) 23 | fname_hr = os.path.join(self.dname_hr, fname) 24 | lr = Image.open(fname_lr).convert('YCbCr') 25 | lr_y, _, _ = lr.split() 26 | hr = Image.open(fname_hr).convert('YCbCr') 27 | hr_y, _, _ = hr.split() 28 | if self.transform is not None: 29 | lr_y = self.transform(lr_y) 30 | hr_y = self.transform(hr_y) 31 | 32 | return lr_y, hr_y 33 | 34 | def __len__(self): 35 | return len(self.samples) 36 | 37 | 38 | class TestDataset(data.Dataset): 39 | def __init__(self, root_dir, transform=None): 40 | self.root_dir = root_dir # '/home/data/nipa_faces_sr_tmp/test' if local 41 | self.transform = transform 42 | self.dname_lr = os.path.join(root_dir, 'test_data') 43 | self.samples = sorted(os.listdir(self.dname_lr)) 44 | 45 | def __getitem__(self, index): 46 | """ 47 | Args: 48 | index (int): Index 49 | Returns: 50 | tuple: (lr_test) 51 | """ 52 | fname = self.samples[index] 53 | fname_lr = os.path.join(self.dname_lr, fname) 54 | lr_test = Image.open(fname_lr).convert('YCbCr') 55 | lr_test_y, _, _ = lr_test.split() 56 | if self.transform is not None: 57 | lr_test_y = self.transform(lr_test_y) 58 | 59 | return lr_test_y 60 | 61 | def __len__(self): 62 | return len(self.samples) 63 | 64 | 65 | def test_loader(root, batch_size=32, num_workers=1): 66 | transform = transforms.Compose([transforms.ToTensor()]) 67 | dataset = TestDataset(root, transform=transform) 68 | dataloader = data.DataLoader(dataset, batch_size=batch_size, shuffle=False, 69 | num_workers=num_workers) 70 | return dataloader 71 | 72 | def data_loader_with_split(root, train_split=0.9, batch_size=32, num_workers=1): 73 | transform = transforms.Compose([transforms.ToTensor()]) 74 | dataset = TrainDataset(root, transform=transform) 75 | train_split_size = int(len(dataset) * train_split) 76 | val_split_size = len(dataset) - train_split_size 77 | 78 | if train_split_size > 0: 79 | train_set, valid_set = data.random_split(dataset, [train_split_size, val_split_size]) 80 | tr_loader = data.DataLoader(dataset=train_set, 81 | batch_size=batch_size, 82 | shuffle=True, 83 | num_workers=num_workers) 84 | else: 85 | tr_loader = None 86 | valid_set = dataset 87 | val_loader = data.DataLoader(dataset=valid_set, 88 | batch_size=batch_size, 89 | shuffle=False, 90 | num_workers=num_workers) 91 | return tr_loader, val_loader 92 | -------------------------------------------------------------------------------- /15_ig_super/evaluation.py: -------------------------------------------------------------------------------- 1 | """ evaluation.py 2 | Replicated in the NSML leaderboard dataset, KoreanFood. 3 | """ 4 | 5 | import argparse 6 | import os 7 | import numpy as np 8 | import time, math, glob 9 | 10 | def PSNR(pred, gt, shave_border=0): 11 | height, width = pred.shape[1:] 12 | pred = pred[shave_border:height - shave_border, shave_border:width - shave_border] 13 | gt = gt[shave_border:height - shave_border, shave_border:width - shave_border] 14 | imdff = pred - gt 15 | rmse = math.sqrt(np.mean(imdff ** 2)) 16 | if rmse == 0: 17 | return 100 18 | return 20 * math.log10(255.0 / rmse) 19 | 20 | 21 | def evaluate(preds, gt_labels): 22 | """ 23 | Args: 24 | preds: numpy array (float) 25 | gt_labels: numpy array (float) 26 | Returns: 27 | avr_psnr_predicted: float average peak signal-to-noise ratio of the test data. 28 | """ 29 | avg_psnr_predicted = 0.0 30 | for query in range(len(gt_labels)): 31 | gt_label = gt_labels[query] 32 | pred = preds[query] 33 | psnr_predicted = PSNR(gt_label, pred) 34 | avg_psnr_predicted += psnr_predicted 35 | 36 | avg_psnr_predicted = avg_psnr_predicted / float(len(gt_labels)) 37 | return avg_psnr_predicted 38 | 39 | 40 | def read_prediction_gt(file_path): 41 | """ 42 | Args: 43 | file_path: str 44 | Returns: 45 | image_list: list(str) 46 | """ 47 | loaded = np.load(file_path) # npz object 48 | loaded = loaded[loaded.files[0]] # now numpy array 49 | return loaded 50 | 51 | 52 | def evaluation_metrics(prediction_file_path, testset_path): 53 | """ 54 | Args: 55 | prediction_file_path: str 56 | testset_path: str 57 | Returns: 58 | avr_psnr_predicted: float average peak signal-to-noise ratio of the test data. 59 | """ 60 | preds = read_prediction_gt(prediction_file_path) 61 | gt_labels = read_prediction_gt(testset_path) 62 | return evaluate(preds, gt_labels) 63 | 64 | 65 | if __name__ == '__main__': 66 | args = argparse.ArgumentParser() 67 | args.add_argument('--prediction', type=str, default='preds.npz') # output_file that is returned from 'feed_infer' of data_loader.py 68 | config = args.parse_args() 69 | test_label_path = '/data/15_ig_super/test/test_label' # do not change 70 | 71 | try: 72 | print(evaluation_metrics(config.prediction, test_label_path)) 73 | except: 74 | print(0.0) -------------------------------------------------------------------------------- /15_ig_super/logger.py: -------------------------------------------------------------------------------- 1 | import nsml 2 | 3 | class Logger: 4 | def __init__(self): 5 | self.last = None 6 | 7 | def scalar_summary(self, tag, value, step): 8 | if self.last and self.last['step'] != step: 9 | nsml.report(**self.last, scope=None) 10 | self.last = None 11 | if self.last is None: 12 | self.last = {'step':step, 'iter':step, 'epoch':1} 13 | self.last[tag] = value 14 | 15 | -------------------------------------------------------------------------------- /15_ig_super/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/ml:cuda9.0-cudnn7-tf-1.11torch1.0keras2.2 2 | from distutils.core import setup 3 | setup( 4 | name='sr', 5 | version='1.0', 6 | install_requires=[ 7 | ] 8 | ) -------------------------------------------------------------------------------- /16_tcls_movie/README.md: -------------------------------------------------------------------------------- 1 | # 영화 리뷰를 이용한 평점 예측 2 | ## 문제설명 3 | 네이버 영화 리뷰와 평점 기반으로 학습하여 새로운 리뷰의 평점을 분류(예측)하는 문제입니다. 4 | 5 | ## dataset 6 | Dataset: `16_tcls_movie` 7 | * 데이터 소스 : 네이버 영화 사용자 리뷰 데이터 8 | * 데이터 특성 : 영화 54,183편에 대한 사용자 리뷰 및 평점 데이터 9 | * 영화 평점(label) : 0 ~ 10점 10 | * \# of data 11 | * train : 약 10.5M 12 | * test : 약 1.3M 13 | 14 | ### data example 15 | | Text | Label | 16 | |:---:|:---:| 17 | | 사람들 평점이 좋아 기대됨 | 7 | 18 | | 이런 대작을 결코 놓쳐서는 안된다. 음악도 너무너무 좋다~ | 10 | 19 | | 이런 배우들로 이런 퀄리티를 내놓다니. 이건 죄악아닌가? | 1 | 20 | | 어느 누가 과연 마지막 장면에 미소 짓지 않을 수 있으리오...... | 9 | 21 | 22 | 23 | ## how to run 24 | 베이스라인 모델 학습 시작 25 | ```bash 26 | nsml run -d 16_tcls_movie -e main.py 27 | ``` 28 | 29 | -------------------------------------------------------------------------------- /16_tcls_movie/data_loader.py: -------------------------------------------------------------------------------- 1 | import os 2 | import csv 3 | 4 | 5 | def test_data_loader(dataset_path, train=False, percentage_to_load=0.1, batch_size=200, ratio_of_validation=0.1, shuffle=True): 6 | if train: 7 | return get_dataloaders(dataset_path=dataset_path, type_of_data='train', 8 | percentage_to_load=percentage_to_load, batch_size=batch_size, 9 | ratio_of_validation=ratio_of_validation, shuffle=shuffle) 10 | else: 11 | # data_dict = {'data': read_test_file(dataset_path=dataset_path)} 12 | data_dict = read_test_file(dataset_path=dataset_path) 13 | return data_dict 14 | 15 | 16 | def read_test_file(dataset_path): 17 | data_path = os.path.join(dataset_path, 'test', 'test_data') # Test data only 18 | loaded_data = [] 19 | with open(data_path, 'rt', encoding='utf-8') as f: # maximum buffer size == 50 MB 20 | lines = f.readlines() 21 | return lines 22 | -------------------------------------------------------------------------------- /16_tcls_movie/evaluation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import argparse 3 | from sklearn.metrics import f1_score 4 | 5 | import warnings 6 | warnings.filterwarnings('ignore') 7 | 8 | # evaluation.py 9 | def read_prediction(prediction_file): 10 | # NEED TO IMPLEMENT #1 11 | # function that loads prediction 12 | pred_array = np.loadtxt(prediction_file, dtype=np.int16) 13 | return pred_array 14 | 15 | 16 | def read_ground_truth(ground_truth_file): 17 | # NEED TO IMPLEMENT #2 18 | # function that loads test_data 19 | gt_array = np.loadtxt(ground_truth_file, dtype=np.int16) 20 | return gt_array 21 | 22 | 23 | # f1 24 | def evaluate(prediction, ground_truth): 25 | # NEET TO IMPLEMENT #3 26 | # Function that actually evaluates 27 | return f1_score(ground_truth, prediction, average='macro') 28 | 29 | # user-defined function for evaluation metrics 30 | def evaluation_metrics(prediction_file: str, ground_truth_file: str): 31 | # read prediction and ground truth from file 32 | prediction = read_prediction(prediction_file) # NOTE: prediction is text 33 | ground_truth = read_ground_truth(ground_truth_file) 34 | return evaluate(prediction, ground_truth) 35 | 36 | 37 | if __name__ == '__main__': 38 | args = argparse.ArgumentParser() 39 | # --prediction is set by file's name that contains the result of inference. (nsml internally sets) 40 | # prediction file requires type casting because '\n' character can be contained. 41 | args.add_argument('--prediction', type=str, default='pred.txt') 42 | config = args.parse_args() 43 | # When push dataset, if you push with leaderboard option, automatically test_label is existed on test/test_label path, set to proper path. 44 | # You should type dataset's name that wants to upload on [dataset] section. 45 | test_label_path = '/data/16_tcls_movie/test/test_label' 46 | # print the evaluation result 47 | # evaluation prints only int or float value. 48 | 49 | try: 50 | print(evaluation_metrics(config.prediction, test_label_path)) 51 | except: 52 | print(0.0) 53 | -------------------------------------------------------------------------------- /16_tcls_movie/models.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import numpy as np 4 | import torch 5 | from torch import nn 6 | from torch.autograd import Variable 7 | 8 | from nsml import GPU_NUM 9 | 10 | class Regression(nn.Module): 11 | """ 12 | 영화리뷰 예측을 위한 Regression 모델입니다. 13 | """ 14 | def __init__(self, embedding_dim: int, max_length: int): 15 | """ 16 | initializer 17 | 18 | :param embedding_dim: 데이터 임베딩의 크기입니다 19 | :param max_length: 인풋 벡터의 최대 길이입니다 (첫 번째 레이어의 노드 수에 연관) 20 | """ 21 | super(Regression, self).__init__() 22 | self.embedding_dim = embedding_dim 23 | self.character_size = 251 24 | self.output_dim = 1 # Regression 25 | self.max_length = max_length 26 | 27 | # 임베딩 28 | self.embeddings = nn.Embedding(self.character_size, self.embedding_dim) 29 | 30 | # 레이어 31 | self.fc1 = nn.Linear(self.max_length * self.embedding_dim, 200) 32 | self.fc2 = nn.Linear(200, 100) 33 | self.fc3 = nn.Linear(100, 1) 34 | 35 | def forward(self, data: list): 36 | """ 37 | 38 | :param data: 실제 입력값 39 | :return: 40 | """ 41 | # 임베딩의 차원 변환을 위해 배치 사이즈를 구합니다. 42 | batch_size = len(data) 43 | # list로 받은 데이터를 torch Variable로 변환합니다. 44 | data_in_torch = Variable(torch.from_numpy(np.array(data)).long()) 45 | # 만약 gpu를 사용중이라면, 데이터를 gpu 메모리로 보냅니다. 46 | if GPU_NUM: 47 | data_in_torch = data_in_torch.cuda() 48 | # 뉴럴네트워크를 지나 결과를 출력합니다. 49 | embeds = self.embeddings(data_in_torch) 50 | hidden = self.fc1(embeds.view(batch_size, -1)) 51 | hidden = self.fc2(hidden) 52 | hidden = torch.relu(hidden) 53 | output = self.fc3(hidden) 54 | return output 55 | 56 | class Classification(nn.Module): 57 | """ 58 | 영화리뷰 예측을 위한 Classification 모델입니다. 59 | """ 60 | def __init__(self, embedding_dim: int, max_length: int): 61 | """ 62 | initializer 63 | 64 | :param embedding_dim: 데이터 임베딩의 크기입니다 65 | :param max_length: 인풋 벡터의 최대 길이입니다 (첫 번째 레이어의 노드 수에 연관) 66 | """ 67 | super(Classification, self).__init__() 68 | self.embedding_dim = embedding_dim 69 | self.character_size = 251 70 | self.output_dim = 11 # Classification(0~10 범위의 lable) 71 | self.max_length = max_length 72 | 73 | # 임베딩 74 | self.embeddings = nn.Embedding(self.character_size, self.embedding_dim) 75 | 76 | # 레이어 77 | self.fc1 = nn.Linear(self.max_length * self.embedding_dim, 200) 78 | self.fc2 = nn.Linear(200, 100) 79 | self.fc3 = nn.Linear(100, self.output_dim) 80 | 81 | def forward(self, data: list): 82 | """ 83 | 84 | :param data: 실제 입력값 85 | :return: 86 | """ 87 | # 임베딩의 차원 변환을 위해 배치 사이즈를 구합니다. 88 | batch_size = len(data) 89 | # list로 받은 데이터를 torch Variable로 변환합니다. 90 | data_in_torch = Variable(torch.from_numpy(np.array(data)).long()) 91 | # 만약 gpu를 사용중이라면, 데이터를 gpu 메모리로 보냅니다. 92 | if GPU_NUM: 93 | data_in_torch = data_in_torch.cuda() 94 | # 뉴럴네트워크를 지나 결과를 출력합니다. 95 | embeds = self.embeddings(data_in_torch) 96 | hidden = self.fc1(embeds.view(batch_size, -1)) 97 | hidden = self.fc2(hidden) 98 | hidden = torch.relu(hidden) 99 | output = self.fc3(hidden) 100 | return output 101 | -------------------------------------------------------------------------------- /16_tcls_movie/nsmlignore: -------------------------------------------------------------------------------- 1 | .pyc -------------------------------------------------------------------------------- /16_tcls_movie/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/ml:cuda9.0-cudnn7-tf-1.11torch1.0keras2.2 2 | 3 | """ 4 | Copyright 2018 NAVER Corp. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 7 | associated documentation files (the "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to 10 | the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all copies or substantial 13 | portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 17 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 18 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 19 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 20 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | """ 22 | 23 | from distutils.core import setup 24 | setup( 25 | name='nsml movie review', 26 | version='1.0', 27 | description='', 28 | install_requires=[ 29 | ] 30 | ) 31 | -------------------------------------------------------------------------------- /17_tcls_intent/README.md: -------------------------------------------------------------------------------- 1 | # NIPA Intent Classification Baseline 2 | NIPA AI STARTHON 2019 Intent Classification Baseline 3 | 4 | ## Overview 5 | **Data** 6 | - Name: ```17_tcls_intent``` 7 | - Number of train data: 40,373 8 | - Number of validation data: 4,057 9 | - Number of test data: 4,057 10 | - Number of intention class: 2,253 11 | - Example: 12 | ``` 13 | {"intent": "메뉴문의", "utterance": "성분이 홍차인지, 아니면 뭐예요?", "intent_label": 1340} 14 | ``` 15 | 16 | ## Usage 17 | **train** 18 | 19 | ```bash 20 | nsml run -e main.py -d 17_tcls_intent -g 1 -a "--embed_dim 256 --hidden_dim 512 --batch_size 256" 21 | ``` 22 | 23 | **list saved checkpoint** 24 | 25 | ```bash 26 | nsml model ls SESSION 27 | ``` 28 | 29 | **submit** 30 | 31 | ```bash 32 | nsml submit SESSION CHECKPOINT 33 | ``` 34 | -------------------------------------------------------------------------------- /17_tcls_intent/data_loader.py: -------------------------------------------------------------------------------- 1 | from nsml import DATASET_PATH 2 | import os 3 | 4 | 5 | def test_data_loader(path): 6 | return path 7 | 8 | 9 | def feed_infer(output_file, infer_func): 10 | data_path = os.path.join(DATASET_PATH, 'test') 11 | result = infer_func(data_path) 12 | result_output = [str(pred[1]) for pred in result] 13 | 14 | print('write output') 15 | with open(output_file, 'w') as output_f: 16 | output_f.write("\n".join(result_output)) -------------------------------------------------------------------------------- /17_tcls_intent/data_utils.py: -------------------------------------------------------------------------------- 1 | import json 2 | import torch 3 | import random 4 | import pickle 5 | 6 | OOV_TOKEN = '' 7 | PAD_TOKEN = '' 8 | 9 | 10 | class InputInstances: 11 | def __init__(self, input_ids, input_length, intent, label): 12 | self.input_ids = input_ids 13 | self.input_length = input_length 14 | self.intent = intent 15 | self.label = label 16 | 17 | def to_tensors(self): 18 | input_ids = torch.LongTensor(self.input_ids) 19 | input_length = torch.LongTensor(self.input_length) 20 | label = torch.LongTensor(self.label) 21 | 22 | return input_ids, input_length, label 23 | 24 | 25 | class Dataset: 26 | def __init__(self, vocab): 27 | self.instances = [] 28 | self.vocab = vocab 29 | 30 | def __len__(self): 31 | return len(self.instances) 32 | 33 | def __getitem__(self, idx): 34 | return self.instances[idx].to_tensors() 35 | 36 | def load_dataset(self, file_path): 37 | with open(file_path) as f: 38 | data = json.load(f) 39 | return data 40 | 41 | def create_instances(self, file_path, max_seq_length, type='train'): 42 | examples = self.load_dataset(file_path) 43 | if type == 'train': 44 | random.shuffle(examples) 45 | 46 | for example in examples: 47 | text = example['utterance'] 48 | 49 | input_ids = [self.vocab.token2idx[char] if char in self.vocab.token2idx 50 | else self.vocab.token2idx[OOV_TOKEN] for char in text] 51 | 52 | if len(input_ids) > max_seq_length: 53 | input_ids = input_ids[-max_seq_length:] 54 | 55 | input_length = [len(input_ids)] 56 | intent = example['intent'] 57 | label = [int(example['intent_label'])] 58 | 59 | while len(input_ids) < max_seq_length: 60 | input_ids.append(self.vocab.token2idx[PAD_TOKEN]) 61 | 62 | assert len(input_ids) == max_seq_length 63 | self.instances.append( 64 | InputInstances(input_ids=input_ids, 65 | input_length=input_length, 66 | intent=intent, 67 | label=label) 68 | ) 69 | 70 | def create_vocab(self, file_path, type='train'): 71 | examples = self.load_dataset(file_path) 72 | 73 | for example in examples: 74 | text = example['utterance'] 75 | 76 | if type == 'train' or type == 'val': 77 | self.vocab.update_vocab(text) 78 | 79 | with open('vocab.pickle', 'wb') as f: 80 | pickle.dump(self.vocab.token2idx, f, protocol=pickle.HIGHEST_PROTOCOL) 81 | 82 | 83 | class Vocabulary: 84 | def __init__(self, vocab_path=None): 85 | self.oov_token = OOV_TOKEN 86 | self.padding_token = PAD_TOKEN 87 | 88 | if vocab_path: 89 | with open('vocab.pickle', 'rb') as f: 90 | self.token2idx = pickle.load(f) 91 | self.word_idx = len(self.token2idx) 92 | else: 93 | self.token2idx = {self.padding_token: 0, self.oov_token: 1} 94 | self.word_idx = 2 95 | 96 | def update_vocab(self, text): 97 | char_text = [char for char in text] 98 | 99 | for char in char_text: 100 | if char not in self.token2idx: 101 | self.token2idx[char] = self.word_idx 102 | self.word_idx += 1 103 | 104 | def index2token(self): 105 | return {v:k for k,v in self.token2idx.items()} 106 | 107 | def vocab_size(self): 108 | return len(self.token2idx) -------------------------------------------------------------------------------- /17_tcls_intent/evaluation.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import numpy as np 3 | from sklearn.metrics import f1_score, accuracy_score 4 | from warnings import filterwarnings 5 | filterwarnings('ignore') 6 | 7 | 8 | def ic_metric(targets, predicts): 9 | targets = np.asarray(targets) 10 | predicts = np.asarray(predicts) 11 | 12 | macro_f1_value = f1_score(targets, predicts, average='macro') 13 | acc_value = accuracy_score(targets, predicts) 14 | return macro_f1_value, acc_value 15 | 16 | 17 | def evaluate(pred_file, gt_file): 18 | preds = read_label_file(pred_file) 19 | gts = read_label_file(gt_file) 20 | 21 | preds = [int(label) for label in preds] 22 | gts = [int(label) for label in gts] 23 | 24 | f1, accuracy = ic_metric(gts, preds) 25 | return f1 26 | 27 | 28 | def read_label_file(file_name): 29 | with open(file_name, 'r') as f: 30 | label = f.read().split('\n') 31 | 32 | return label 33 | 34 | 35 | if __name__ == '__main__': 36 | args = argparse.ArgumentParser() 37 | args.add_argument('--prediction', type=str, default='prediction.txt') 38 | config = args.parse_args() 39 | label_path = '/data/17_tcls_intent/test/test_label' 40 | 41 | try: 42 | print(evaluate(config.prediction, label_path)) 43 | except: 44 | # 에러로인한 0점 45 | print("0") -------------------------------------------------------------------------------- /17_tcls_intent/model.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | from torch.autograd import Variable 5 | 6 | 7 | def expand(tensor, target): 8 | return tensor.expand_as(target) 9 | 10 | 11 | def masking(tensor, mask): 12 | return torch.mul(tensor, expand(mask.unsqueeze(-1), tensor).float()) 13 | 14 | 15 | class Encoder(nn.Module): 16 | def __init__(self, input_dim, hidden_dim, n_layers, dropout=.0): 17 | super(Encoder, self).__init__() 18 | self.n_layers = n_layers 19 | self.hidden_dim = hidden_dim 20 | 21 | self.gru = nn.GRU(input_dim, 22 | hidden_dim, 23 | batch_first=True, 24 | num_layers=n_layers, 25 | bidirectional=True, 26 | dropout=dropout) 27 | 28 | def forward(self, embedded_inputs, input_length, hidden_state=None): 29 | batch_size = embedded_inputs.size(0) 30 | total_length = embedded_inputs.size(1) 31 | 32 | if hidden_state is None: 33 | h_size = (self.n_layers * 2, batch_size, self.hidden_dim) 34 | enc_h_0 = Variable(embedded_inputs.data.new(*h_size).zero_(), requires_grad=False) 35 | 36 | packed_input = nn.utils.rnn.pack_padded_sequence(embedded_inputs, input_length.tolist(), batch_first=True) 37 | packed_output, enc_h_t = self.gru(packed_input, enc_h_0) 38 | _, _ = nn.utils.rnn.pad_packed_sequence(packed_output, batch_first=True, total_length=total_length) 39 | return enc_h_t[-1] 40 | 41 | 42 | class LSTMClassifier(nn.Module): 43 | def __init__(self, config, vocab_size, n_label): 44 | super(LSTMClassifier, self).__init__() 45 | 46 | self.embed = nn.Embedding(vocab_size, config.embed_dim) 47 | 48 | self.encoder = Encoder(config.embed_dim, 49 | config.hidden_dim, 50 | config.n_layer, 51 | dropout=config.dropout) 52 | 53 | self.classifier = nn.Linear(config.hidden_dim, n_label) 54 | self.dropout = nn.Dropout(config.dropout) 55 | 56 | def forward(self, input_ids, input_length): 57 | embedded_layer = self.embed(input_ids) 58 | embedded_layer = self.dropout(embedded_layer) 59 | encoded_layer = self.encoder(embedded_layer, input_length) 60 | logits = self.classifier(encoded_layer) 61 | 62 | outputs = { 63 | "logits": logits, 64 | "predicted_intents": torch.topk(logits, 1)[1], 65 | } 66 | return outputs -------------------------------------------------------------------------------- /17_tcls_intent/setup.py: -------------------------------------------------------------------------------- 1 | #nsml:mj1642/nipa:0.1 2 | 3 | from distutils.core import setup 4 | 5 | setup( 6 | name="nsml: intent_classification", 7 | version="1.0", 8 | description="nsml", 9 | install_requires=[], 10 | ) -------------------------------------------------------------------------------- /17_tcls_intent/vocab.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ai-starthon/AI_Starthon2019/eba6db7ff75b6f69085705cb423ec16f85b946e5/17_tcls_intent/vocab.pickle -------------------------------------------------------------------------------- /18_tcls_query/18_tcls_query/data_loader.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | import numpy as np 4 | from nsml import DATASET_PATH 5 | 6 | 7 | def test_data_loader(): 8 | return None 9 | 10 | 11 | def feed_infer(output_file, infer_func): 12 | result = infer_func(os.path.join(DATASET_PATH, "test")) 13 | np.savetxt(output_file, result) 14 | 15 | if os.stat(output_file).st_size == 0: 16 | raise AssertionError("Output result of inference is nothing") 17 | -------------------------------------------------------------------------------- /18_tcls_query/18_tcls_query/evaluation.py: -------------------------------------------------------------------------------- 1 | 2 | import argparse 3 | import numpy as np 4 | 5 | from sklearn.metrics import roc_auc_score 6 | 7 | 8 | def evaluate(pred_path, target_path): 9 | preds = np.loadtxt(pred_path) 10 | targets = np.loadtxt(target_path) 11 | 12 | return roc_auc_score(targets, preds) 13 | 14 | 15 | if __name__ == "__main__": 16 | parser = argparse.ArgumentParser() 17 | parser.add_argument("--prediction", type=str, default="prediction.txt") 18 | config = parser.parse_args() 19 | 20 | test_label_file_path = "/data/18_tcls_query/test/test_label" 21 | 22 | try: 23 | print(evaluate(config.prediction, test_label_file_path)) 24 | except: 25 | print("0") 26 | -------------------------------------------------------------------------------- /18_tcls_query/README.md: -------------------------------------------------------------------------------- 1 | # NIPA Query Similarity Baseline 2 | NIPA AI STARTHON 2019 Query Similarity Baseline 3 | 4 | ## Data 5 | - Name: ```18_tcls_query``` 6 | - Label: binary 7 | - 1 for similar query pairs 8 | - 0 for nonsimilar query pairs 9 | - Train data: 115,198 query pairs 10 | - train_data/train_data 11 | - train_data/train_label 12 | - Validation data: 20,000 query pairs 13 | - train_data/valid_data 14 | - train_data/valid_label 15 | - Test data: 20,000 query pairs 16 | 17 | ### Example 18 | 19 | #### train_data 20 | Tab separated file, with format `[SOURCE]\t[QUERY_A]\t[QUERY_B]`. Some examples have unknown source, and have `_` as the source. 21 | ``` 22 | 2068803_388327 광주에서 부산까지 고속도로 톨게이트 요금 14일 전국 고속도로 요금 23 | _ 급해요 !!진짜로 !! 급해요 진짜로요.. 24 | ``` 25 | #### train_label 26 | Binary similarity score for each example in the data file. 27 | ``` 28 | 0 29 | 1 30 | ``` 31 | 32 | ## Requirements 33 | 34 | `numpy` must be installed on your local environment. 35 | 36 | ## Usage 37 | ### Train 38 | 39 | ```bash 40 | nsml run -d 18_tcls_query -e main.py 41 | ``` 42 | 43 | ### List saved checkpoints 44 | 45 | ```bash 46 | nsml model ls SESSION 47 | ``` 48 | 49 | ### Submit 50 | 51 | ```bash 52 | nsml submit SESSION CHECKPOINT 53 | ``` 54 | 55 | ## Metric 56 | ROC-AUC Score 57 | -------------------------------------------------------------------------------- /18_tcls_query/args.py: -------------------------------------------------------------------------------- 1 | 2 | import argparse 3 | 4 | 5 | parser = argparse.ArgumentParser() 6 | 7 | parser.add_argument( 8 | "--train_file_name", 9 | type=str, default="train_data", 10 | help=""" Train dataset file name """, 11 | ) 12 | 13 | parser.add_argument( 14 | "--train_label_file_name", 15 | type=str, default="train_label", 16 | help=""" Train dataset label file name """, 17 | ) 18 | 19 | parser.add_argument( 20 | "--valid_file_name", 21 | type=str, default="valid_data", 22 | help=""" Validation dataset file name """, 23 | ) 24 | 25 | parser.add_argument( 26 | "--valid_label_file_name", 27 | type=str, default="valid_label", 28 | help=""" Validation dataset label file name """, 29 | ) 30 | 31 | parser.add_argument( 32 | "--test_file_name", 33 | type=str, default="test_data", 34 | help=""" Validation dataset file name """, 35 | ) 36 | 37 | parser.add_argument( 38 | "--test_label_file_name", 39 | type=str, default="test_label", 40 | help=""" Validation dataset label file name """, 41 | ) 42 | 43 | parser.add_argument( 44 | "--batch_size", 45 | type=int, default=512, 46 | help=""" Maximum batch size for trainer """, 47 | ) 48 | 49 | parser.add_argument( 50 | "--max_sequence_len", 51 | type=int, default=128, 52 | help=""" Maximum sequence length """ 53 | ) 54 | 55 | parser.add_argument( 56 | "--char_embed_size", 57 | type=int, default=200, 58 | help=""" Word embed size of char-word CNN """ 59 | ) 60 | 61 | parser.add_argument( 62 | "--filter_sizes", 63 | type=list, default=[1, 2, 3, 4, 5], 64 | help=""" Sizes of filters of char-word CNN """ 65 | ) 66 | 67 | parser.add_argument( 68 | "--sentence_embed_size", 69 | type=int, default=1000, 70 | help=""" Sentence embed size of char-word CNN """ 71 | ) 72 | 73 | parser.add_argument( 74 | "--dropout", 75 | type=float, default=0.2, 76 | help=""" Dropout value of char-word CNN """ 77 | ) 78 | 79 | parser.add_argument( 80 | "--activation", 81 | type=str, default="relu", 82 | help=""" Activation function of char-word CNN """ 83 | ) 84 | 85 | parser.add_argument( 86 | "--num_epochs", 87 | type=int, default=50, 88 | help=""" Number of epochs for training """ 89 | ) 90 | 91 | parser.add_argument( 92 | "--early_stop_threshold", 93 | type=int, default=10, 94 | help=""" Threshold for early stopping """ 95 | ) 96 | 97 | parser.add_argument( 98 | "--log_steps", 99 | type=int, default=100, 100 | help=""" Number of steps to log """ 101 | ) 102 | 103 | parser.add_argument( 104 | "--learning_rate", 105 | type=float, default=0.003, 106 | help=""" Learning rate """, 107 | ) 108 | 109 | parser.add_argument( 110 | "--seed", 111 | type=int, default=42, 112 | help=""" Random seed """, 113 | ) 114 | 115 | 116 | # reserved for nsml 117 | parser.add_argument("--mode", type=str, default="train") 118 | parser.add_argument("--iteration", type=str, default="0") 119 | parser.add_argument("--pause", type=int, default=0) 120 | 121 | 122 | def get_config(): 123 | return parser.parse_args() 124 | -------------------------------------------------------------------------------- /18_tcls_query/data_utils.py: -------------------------------------------------------------------------------- 1 | import re 2 | import html 3 | import string 4 | import numpy as np 5 | 6 | PAD = "[PAD]" 7 | PAD_IND = 0 8 | 9 | 10 | def get_all_chars(): 11 | koreans = [chr(i) for i in range(44032, 55204)] # 가-힣 12 | korean_chars = [chr(i) for i in range(ord("ㄱ"), ord("ㅣ") + 1)] # ㄱ-ㅎ, ㅏ-ㅣ 13 | alphabets = list(string.ascii_letters) 14 | digits = list(string.digits) 15 | return [PAD, " "] + koreans + korean_chars + alphabets + digits 16 | 17 | 18 | # build char vocabulary 19 | vocabs = get_all_chars() 20 | ind2vocab = {ind: char for ind, char in enumerate(vocabs)} 21 | vocab2ind = {char: ind for ind, char in enumerate(vocabs)} 22 | 23 | _vocabs = "[^" + "".join(vocabs[1:]) + "]" 24 | 25 | 26 | def prepro_text(text): 27 | text = html.unescape(text) 28 | # text = "".join([char if char in vocabs else " " for char in text]) 29 | text = re.sub(_vocabs, " ", text) 30 | return re.sub("\s+", " ", text).strip() 31 | 32 | 33 | def text2ind(text, max_len, raw_text=False): 34 | if raw_text: 35 | text = prepro_text(text) 36 | return np.asarray(list(map(lambda char: vocab2ind[char], text))[:max_len] + \ 37 | [vocab2ind[PAD] for _ in range(max((max_len - len(text)), 0))]) 38 | 39 | 40 | def ind2text(inds): 41 | return "".join(map(lambda ind: ind2vocab[ind] if ind >= 0 else "", inds)) -------------------------------------------------------------------------------- /18_tcls_query/model/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | import torch.nn as nn 3 | 4 | from model.char_cnn import CharCNN 5 | from model.bilinear_scorer import BilinearScorer 6 | 7 | 8 | class CharCNNScorer(nn.Module): 9 | def __init__( 10 | self, 11 | vocab_size, 12 | char_embed_size, 13 | filter_sizes, 14 | sentence_embed_size, 15 | dropout, 16 | activation, 17 | pad_ind, 18 | ): 19 | super().__init__() 20 | 21 | self.sentence_embedder = CharCNN( 22 | vocab_size=vocab_size, 23 | embed_size=char_embed_size, 24 | filter_sizes=filter_sizes, 25 | out_dim=sentence_embed_size, 26 | dropout=dropout, 27 | activation=activation, 28 | pad_ind=pad_ind, 29 | ) 30 | self.scorer = BilinearScorer( 31 | sentence_embed_size, 32 | ) 33 | 34 | def forward( 35 | self, 36 | a_seqs, 37 | b_seqs, 38 | len_a_seqs=None, 39 | len_b_seqs=None, 40 | ): 41 | a_embeds = self.sentence_embedder(a_seqs, len_a_seqs) 42 | b_embeds = self.sentence_embedder(b_seqs, len_b_seqs) 43 | 44 | logits = self.scorer(a_embeds, b_embeds).squeeze(-1) 45 | 46 | return logits 47 | -------------------------------------------------------------------------------- /18_tcls_query/model/bilinear_scorer.py: -------------------------------------------------------------------------------- 1 | 2 | import torch 3 | import torch.nn as nn 4 | 5 | 6 | class BilinearScorer(nn.Module): 7 | def __init__( 8 | self, 9 | input_dim, 10 | ): 11 | super().__init__() 12 | 13 | self.input_dim = input_dim 14 | self.W = nn.Linear(input_dim, input_dim) 15 | 16 | def forward(self, a_embeds, b_embeds): 17 | return torch.sum(a_embeds * self.W(b_embeds), dim=-1) 18 | -------------------------------------------------------------------------------- /18_tcls_query/model/char_cnn.py: -------------------------------------------------------------------------------- 1 | 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | 7 | class CharCNN(nn.Module): 8 | def __init__( 9 | self, 10 | vocab_size, 11 | embed_size, 12 | filter_sizes, 13 | out_dim, 14 | dropout, 15 | activation, 16 | pad_ind=0 17 | ): 18 | super().__init__() 19 | 20 | self.vocab_size = vocab_size 21 | self.embed_size = embed_size 22 | self.out_dim = out_dim 23 | self.filter_sizes = sorted(filter_sizes) 24 | self.activation = { 25 | "relu": F.relu, 26 | "tanh": torch.tanh, 27 | "elu": nn.functional.elu, 28 | "none": lambda x: x, 29 | }[activation] 30 | self.dropout = nn.Dropout(p=dropout) 31 | 32 | assert self.out_dim % len(self.filter_sizes) == 0 33 | 34 | self.embedding = nn.Embedding(self.vocab_size, self.embed_size, 35 | padding_idx=pad_ind) 36 | 37 | self.convs = nn.ModuleList([nn.Conv1d(self.embed_size, self.out_dim // len(self.filter_sizes), width) \ 38 | for width in self.filter_sizes]) 39 | self.pool = nn.AdaptiveMaxPool1d(1) 40 | 41 | def forward(self, x, seq_len=None, word_lens=None): 42 | embeds = self.embedding(x) 43 | embeds = self.dropout(embeds) # [B, L, D] 44 | 45 | if embeds.size(1) < max(self.filter_sizes): 46 | embeds = torch.cat( 47 | [ 48 | embeds, 49 | torch.zeros(embeds.size(0), max(self.filter_sizes) - embeds.size(1), embeds.size(2)).to(embeds.device) 50 | ], dim=1) # pad 51 | 52 | if seq_len is not None: 53 | L = max(seq_len.max().int(), max(self.filter_sizes)) 54 | embeds = embeds[:, :L, :] # [B, L, D] with L cut 55 | 56 | embeds = embeds.transpose(1, 2) # [B, D, L] 57 | conv_outputs = [] 58 | for conv in self.convs: 59 | x = self.pool(self.activation(conv(embeds))).squeeze(-1) 60 | conv_outputs.append(x) 61 | ret = torch.cat(conv_outputs, dim=-1) # [B, num_all_filters] 62 | return ret 63 | -------------------------------------------------------------------------------- /18_tcls_query/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/ml:cuda9.0-cudnn7-tf-1.11torch1.0keras2.2 2 | from distutils.core import setup 3 | setup( 4 | name='18_tcls_query_baseline', 5 | version='1.0', 6 | description='18_tcls_query_baseline', 7 | install_requires=[ 8 | ] 9 | ) 10 | -------------------------------------------------------------------------------- /19_tcls_qa/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/macos,python,jupyternotebooks 2 | # Edit at https://www.gitignore.io/?templates=macos,python,jupyternotebooks 3 | 4 | ### JupyterNotebooks ### 5 | # gitignore template for Jupyter Notebooks 6 | # website: http://jupyter.org/ 7 | 8 | .ipynb_checkpoints 9 | */.ipynb_checkpoints/* 10 | 11 | # Remove previous ipynb_checkpoints 12 | # git rm -r .ipynb_checkpoints/ 13 | # 14 | 15 | ### macOS ### 16 | # General 17 | .DS_Store 18 | .AppleDouble 19 | .LSOverride 20 | 21 | # Icon must end with two \r 22 | Icon 23 | 24 | # Thumbnails 25 | ._* 26 | 27 | # Files that might appear in the root of a volume 28 | .DocumentRevisions-V100 29 | .fseventsd 30 | .Spotlight-V100 31 | .TemporaryItems 32 | .Trashes 33 | .VolumeIcon.icns 34 | .com.apple.timemachine.donotpresent 35 | 36 | # Directories potentially created on remote AFP share 37 | .AppleDB 38 | .AppleDesktop 39 | Network Trash Folder 40 | Temporary Items 41 | .apdisk 42 | 43 | ### Python ### 44 | # Byte-compiled / optimized / DLL files 45 | __pycache__/ 46 | *.py[cod] 47 | *$py.class 48 | 49 | # C extensions 50 | *.so 51 | 52 | # Distribution / packaging 53 | .Python 54 | build/ 55 | develop-eggs/ 56 | dist/ 57 | downloads/ 58 | eggs/ 59 | .eggs/ 60 | lib/ 61 | lib64/ 62 | parts/ 63 | sdist/ 64 | var/ 65 | wheels/ 66 | pip-wheel-metadata/ 67 | share/python-wheels/ 68 | *.egg-info/ 69 | .installed.cfg 70 | *.egg 71 | MANIFEST 72 | 73 | # PyInstaller 74 | # Usually these files are written by a python script from a template 75 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 76 | *.manifest 77 | *.spec 78 | 79 | # Installer logs 80 | pip-log.txt 81 | pip-delete-this-directory.txt 82 | 83 | # Unit test / coverage reports 84 | htmlcov/ 85 | .tox/ 86 | .nox/ 87 | .coverage 88 | .coverage.* 89 | .cache 90 | nosetests.xml 91 | coverage.xml 92 | *.cover 93 | .hypothesis/ 94 | .pytest_cache/ 95 | 96 | # Translations 97 | *.mo 98 | *.pot 99 | 100 | # Django stuff: 101 | *.log 102 | local_settings.py 103 | db.sqlite3 104 | db.sqlite3-journal 105 | 106 | # Flask stuff: 107 | instance/ 108 | .webassets-cache 109 | 110 | # Scrapy stuff: 111 | .scrapy 112 | 113 | # Sphinx documentation 114 | docs/_build/ 115 | 116 | # PyBuilder 117 | target/ 118 | 119 | # Jupyter Notebook 120 | 121 | # IPython 122 | profile_default/ 123 | ipython_config.py 124 | 125 | # pyenv 126 | .python-version 127 | 128 | # pipenv 129 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 130 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 131 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 132 | # install all needed dependencies. 133 | #Pipfile.lock 134 | 135 | # celery beat schedule file 136 | celerybeat-schedule 137 | 138 | # SageMath parsed files 139 | *.sage.py 140 | 141 | # Environments 142 | .env 143 | .venv 144 | env/ 145 | venv/ 146 | ENV/ 147 | env.bak/ 148 | venv.bak/ 149 | 150 | # Spyder project settings 151 | .spyderproject 152 | .spyproject 153 | 154 | # Rope project settings 155 | .ropeproject 156 | 157 | # mkdocs documentation 158 | /site 159 | 160 | # mypy 161 | .mypy_cache/ 162 | .dmypy.json 163 | dmypy.json 164 | 165 | # Pyre type checker 166 | .pyre/ 167 | 168 | # End of https://www.gitignore.io/api/macos,python,jupyternotebooks 169 | -------------------------------------------------------------------------------- /19_tcls_qa/.nsmlignore: -------------------------------------------------------------------------------- 1 | /data 2 | /nipa_mrc 3 | /__pycache__ 4 | /logs 5 | /notebook 6 | -------------------------------------------------------------------------------- /19_tcls_qa/README.md: -------------------------------------------------------------------------------- 1 | # nipa-mrc-baseline 2 | NIPA AI STARTHON 2019 MRC Baseline with CLaF 3 | 4 | ## Overview 5 | 6 | - NSML Dataset Name: `19_tcls_qa` 7 | - Train set - 226894개 8 | - Dev set - 18116개 9 | - Test set - 12680개 10 | - Challenge: 기계독해 11 | - * 기계독해(Machine Reading Comprehension, MRC) : 제시된 본문 안에서 질의에 대한 정답을 추론하는 딥러닝 기반 기술 12 | - Example: 13 | 14 | ``` 15 | { 16 | "data": [{ 17 | "source": 6, 18 | "paragraphs": [{ 19 | "qas": [{ 20 | "question": "쎈 마이웨이 관련 기자간담회 누가 했어", 21 | "id": "m4_278529-1", 22 | "answers": [{ 23 | "answer_start": 0, 24 | "text": "박영선" 25 | }] 26 | }], 27 | "context": "박영선 PD는 18일 오후 서울 양천구 목동 SBS에서 모비딕의 토크 콘텐츠 쎈 마이웨이 관련 기자간담회를 열고 출연진에 신뢰를 드러냈다." 28 | }], 29 | "title": “1 "} 30 | ``` 31 | 32 | - 본문 카테고리(source)의 기입형태 및 설명 33 | - 1 : 정치 34 | - 2 : 경제 35 | - 3 : 사회 36 | - 4 : 생활 37 | - 5 : IT/과학 38 | - 6 : 연예 39 | - 7 : 스포츠 40 | - 8 : 문화 41 | - 9 : 미용/건강 42 | 43 | - 육하원칙(classtype)의 기입 형태 및 설명 44 | - work_where : 어디서 45 | - work_who : 누가 46 | - work_what : 무엇을 47 | - work_how : 어떻게 48 | - work_why : 왜 49 | - work_when : 언제 50 | 51 | 52 | ## Usage 53 | 54 | - Requirements 55 | - [claf](https://github.com/naver/claf)>=0.1.6 56 | - Docker Images: [claf/claf:latest](https://hub.docker.com/r/claf/claf/tags) 57 | 58 | 59 | 60 | - Train and Evaluate 61 | 62 | ```bash 63 | # local 64 | python main.py --base_config base_config/baseline.json 65 | 66 | # nsml 67 | nsml run -d 19_tcls_qa -g 1 --memory "50GB" -e main.py -a "--base_config base_config/baseline.json" 68 | ``` 69 | - Submit 70 | - Submit 시에는 Vocab을 로딩하는 과정이 필요합니다. 그래서 `def test()` 함수 안에 있는 `NSML_CHECKPOINT`, `NSML_SESSION` 이 두 변수에 제출에 사용하는 세션과 체크포인트를 입력한 후 아래 커맨드를 입력합니다. 71 | 72 | ```bash 73 | nsml submit -e main.py SESSION_NAME CHECKPOINT 74 | ``` 75 | -------------------------------------------------------------------------------- /19_tcls_qa/base_config/baseline.json: -------------------------------------------------------------------------------- 1 | { 2 | "data_reader": { 3 | "dataset": "squad", 4 | "train_file_path": "train.json", 5 | "valid_file_path": "valid.json", 6 | "squad": { 7 | "lang_code": "ko", 8 | "context_max_length": 1000 9 | } 10 | }, 11 | "iterator": { 12 | "batch_size": 32 13 | }, 14 | "token": { 15 | "names": ["char", "word"], 16 | "types": ["char", "word"], 17 | "tokenizer": { 18 | "char": { 19 | "name": "jamo_ko" 20 | }, 21 | "word": { 22 | "name": "mecab_ko", 23 | "split_with_regex": true 24 | } 25 | }, 26 | "char": { 27 | "vocab": { 28 | "start_token": "", 29 | "end_token": "", 30 | "max_vocab_size": 70 31 | }, 32 | "indexer": { 33 | "insert_char_start": true, 34 | "insert_char_end": true 35 | }, 36 | "embedding": { 37 | "embed_dim": 20, 38 | "kernel_sizes": [5], 39 | "num_filter": 100, 40 | "activation": "relu", 41 | "dropout": 0.2 42 | } 43 | }, 44 | "word": { 45 | "embedding": { 46 | "embed_dim": 300, 47 | "trainable": true, 48 | "dropout": 0.2 49 | } 50 | } 51 | }, 52 | "model": { 53 | "name": "docqa", 54 | "docqa": { 55 | "answer_maxlen": 17, 56 | "rnn_dim": 100, 57 | "linear_dim": 200, 58 | "preprocess_rnn_num_layer": 1, 59 | "modeling_rnn_num_layer": 1, 60 | "predict_rnn_num_layer": 1, 61 | "dropout": 0.2, 62 | "weight_init": true 63 | } 64 | }, 65 | "trainer": { 66 | "log_dir": "logs/baseline", 67 | "num_epochs": 20, 68 | "early_stopping_threshold": 5, 69 | "metric_key": "f1", 70 | "verbose_step_count": 100, 71 | "eval_and_save_step_count": 1000 72 | }, 73 | "optimizer": { 74 | "op_type": "adamax", 75 | "learning_rate": 0.001, 76 | "lr_scheduler_type": "reduce_on_plateau", 77 | "reduce_on_plateau": { 78 | "factor": 0.5, 79 | "mode": "max", 80 | "patience": 2 81 | } 82 | }, 83 | "seed_num": 2 84 | } 85 | -------------------------------------------------------------------------------- /19_tcls_qa/base_config/test.json: -------------------------------------------------------------------------------- 1 | { 2 | "data_reader": { 3 | "dataset": "squad", 4 | "train_file_path": "valid.json", 5 | "valid_file_path": "valid.json", 6 | "squad": { 7 | "lang_code": "ko", 8 | "context_max_length": 1000 9 | } 10 | }, 11 | "iterator": { 12 | "batch_size": 32 13 | }, 14 | "token": { 15 | "names": ["char", "word"], 16 | "types": ["char", "word"], 17 | "tokenizer": { 18 | "char": { 19 | "name": "jamo_ko" 20 | }, 21 | "word": { 22 | "name": "mecab_ko", 23 | "split_with_regex": true 24 | } 25 | }, 26 | "char": { 27 | "vocab": { 28 | "start_token": "", 29 | "end_token": "", 30 | "max_vocab_size": 70 31 | }, 32 | "indexer": { 33 | "insert_char_start": true, 34 | "insert_char_end": true 35 | }, 36 | "embedding": { 37 | "embed_dim": 20, 38 | "kernel_sizes": [5], 39 | "num_filter": 100, 40 | "activation": "relu", 41 | "dropout": 0.2 42 | } 43 | }, 44 | "word": { 45 | "embedding": { 46 | "embed_dim": 300, 47 | "trainable": true, 48 | "dropout": 0.2 49 | } 50 | } 51 | }, 52 | "model": { 53 | "name": "docqa", 54 | "docqa": { 55 | "answer_maxlen": 17, 56 | "rnn_dim": 100, 57 | "linear_dim": 200, 58 | "preprocess_rnn_num_layer": 1, 59 | "modeling_rnn_num_layer": 1, 60 | "predict_rnn_num_layer": 1, 61 | "dropout": 0.2, 62 | "weight_init": true 63 | } 64 | }, 65 | "trainer": { 66 | "log_dir": "logs/baseline", 67 | "num_epochs": 20, 68 | "early_stopping_threshold": 5, 69 | "metric_key": "f1", 70 | "verbose_step_count": 100, 71 | "eval_and_save_step_count": 50 72 | }, 73 | "optimizer": { 74 | "op_type": "adamax", 75 | "learning_rate": 0.001, 76 | "lr_scheduler_type": "reduce_on_plateau", 77 | "reduce_on_plateau": { 78 | "factor": 0.5, 79 | "mode": "max", 80 | "patience": 2 81 | } 82 | }, 83 | "seed_num": 2 84 | } 85 | -------------------------------------------------------------------------------- /19_tcls_qa/data_loader.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | 4 | from nsml.constants import DATASET_PATH 5 | 6 | 7 | def test_data_loader(root_path): 8 | """ 9 | Data loader for test data 10 | :param root_path: root path of test set. 11 | 12 | :return: data type to use in user's infer() function 13 | """ 14 | test_path = os.path.join(root_path, 'test', 'test_data') 15 | with open(test_path, "r", encoding="utf-8") as in_file: 16 | test_data = in_file.read().split("\n") 17 | return test_data 18 | 19 | 20 | def feed_infer(output_file, infer_func): 21 | """ 22 | This is a function that implements a way to write the user's inference result to the output file. 23 | :param output_file(str): File path to write output (Be sure to write in this location.) 24 | infer_func(function): The user's infer function bound to 'nsml.bind()' 25 | """ 26 | result = infer_func(os.path.join(DATASET_PATH, 'test')) 27 | result = [str(pred[1]) for pred in result] 28 | print('write output') 29 | with open(output_file, 'w') as file_writer: 30 | file_writer.write("\n".join(result)) 31 | 32 | if os.stat(output_file).st_size == 0: 33 | raise AssertionError('output result of inference is nothing') 34 | -------------------------------------------------------------------------------- /19_tcls_qa/evaluation.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | from collections import Counter 3 | import string 4 | import re 5 | import argparse 6 | 7 | '''본 스크립트는 KorQuAD v1.0 평가 스크립트를 바탕으로 작성됨.''' 8 | 9 | def normalize_answer(s): 10 | def remove_(text): 11 | ''' 불필요한 기호 제거 ''' 12 | text = re.sub("'", " ", text) 13 | text = re.sub('"', " ", text) 14 | text = re.sub('《', " ", text) 15 | text = re.sub('》', " ", text) 16 | text = re.sub('<', " ", text) 17 | text = re.sub('>', " ", text) 18 | text = re.sub('〈', " ", text) 19 | text = re.sub('〉', " ", text) 20 | text = re.sub("\(", " ", text) 21 | text = re.sub("\)", " ", text) 22 | text = re.sub("‘", " ", text) 23 | text = re.sub("’", " ", text) 24 | return text 25 | 26 | def white_space_fix(text): 27 | return ' '.join(text.split()) 28 | 29 | def remove_punc(text): 30 | exclude = set(string.punctuation) 31 | return ''.join(ch for ch in text if ch not in exclude) 32 | 33 | def lower(text): 34 | return text.lower() 35 | 36 | return white_space_fix(remove_punc(lower(remove_(s)))) 37 | 38 | 39 | def f1_score(prediction, ground_truth): 40 | prediction_tokens = normalize_answer(prediction).split() 41 | ground_truth_tokens = normalize_answer(ground_truth).split() 42 | 43 | #F1 by character 44 | prediction_Char = [] 45 | for tok in prediction_tokens: 46 | now = [a for a in tok] 47 | prediction_Char.extend(now) 48 | 49 | ground_truth_Char = [] 50 | for tok in ground_truth_tokens: 51 | now = [a for a in tok] 52 | ground_truth_Char.extend(now) 53 | 54 | common = Counter(prediction_Char) & Counter(ground_truth_Char) 55 | num_same = sum(common.values()) 56 | if num_same == 0: 57 | return 0 58 | 59 | precision = 1.0 * num_same / len(prediction_Char) 60 | recall = 1.0 * num_same / len(ground_truth_Char) 61 | f1 = (2 * precision * recall) / (precision + recall) 62 | 63 | return f1 64 | 65 | 66 | def exact_match_score(prediction, ground_truth): 67 | return (normalize_answer(prediction) == normalize_answer(ground_truth)) 68 | 69 | 70 | def evaluate(prediction_path, label_path): 71 | 72 | with open(prediction_path, "r", encoding="utf-8") as in_file: 73 | predictions = in_file.read().split("\n") 74 | 75 | with open(label_path, "r", encoding="utf-8") as in_file: 76 | labels = in_file.read().split("\n") 77 | 78 | f1 = exact_match = total = 0 79 | for prediction, label in zip(predictions, labels): 80 | exact_match += exact_match_score(prediction, label) 81 | f1 += f1_score(prediction, label) 82 | 83 | total += 1 84 | 85 | exact_match = 100.0 * exact_match / total 86 | f1 = 100.0 * f1 / total 87 | return f1 88 | 89 | 90 | if __name__ == '__main__': 91 | parser = argparse.ArgumentParser() 92 | parser.add_argument('--prediction', type=str, default='prediction.txt') # File 93 | 94 | config = parser.parse_args() 95 | label_path = "/data/19_tcls_qa/test/test_label" 96 | 97 | print(evaluate(config.prediction, label_path)) 98 | -------------------------------------------------------------------------------- /19_tcls_qa/requirements.txt: -------------------------------------------------------------------------------- 1 | claf>=0.1.6 2 | -------------------------------------------------------------------------------- /19_tcls_qa/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: claf/claf:latest 2 | 3 | from distutils.core import setup 4 | 5 | # from claf import __version__ as VERSION 6 | 7 | 8 | setup( 9 | name="nsml: claf", 10 | version="1.0", 11 | description="ns-ml", 12 | install_requires=[], 13 | ) 14 | -------------------------------------------------------------------------------- /1_reg_dust/README.md: -------------------------------------------------------------------------------- 1 | ## 미세먼지 및 초미세먼지 시계열 예측 Baseline 2 | 3 | Dataset Description: 4 | ``` 5 | \_train 6 | \_ train_data (npy file) 7 | \_ train_label (npy file) 8 | 9 | # of training samples: 109,136 10 | # of test samples: 5448 11 | ``` 12 | 13 | Feature composition 14 | * 지역: 1개 (값종류: 10 (0 ~ 9)) 15 | * 날짜: 3개 (년, 월, 일) 16 | * 이전 5시간동안 미세먼지, 초미세먼지: 5 * 2 = 10개 17 | 18 | Metric: 0.3*미세먼지MSE + 0.7*초미세먼지MSE 19 | 20 | Output: 다음시간의 (미세먼지, 초미세먼지) 21 | 22 | How to run: 23 | 24 | ```bash 25 | nsml run -d 1_reg_dust -g 1 --memory 12G --shm-size 16G --cpus 4 -e main.py 26 | ``` 27 | 28 | How to list checkpoints saved: 29 | 30 | ```bash 31 | nsml model ls [NSML ID]/1_reg_dust/[Session Number] (e.g.) nsml model ls KR77777/1_reg_dust/2) 32 | ``` 33 | 34 | How to submit: 35 | 36 | ```bash 37 | nsml submit [NSML ID]/1_reg_dust/[Session Number] [check idx] (e.g) nsml submit KR77777/1_reg_dust/2 100) 38 | ``` 39 | -------------------------------------------------------------------------------- /1_reg_dust/data_loader.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | from nsml import DATASET_PATH 4 | 5 | def test_data_loader(root_path): 6 | """ 7 | Data loader for test data 8 | 9 | :param root_path: path to the test dataset. root_path would be enough. 10 | :return: data type used in infer() function 11 | 12 | location of the test data is root_path/test/* 13 | """ 14 | return np.load(os.path.join(root_path, 'test', 'test_data'), dtype=np.float32) 15 | 16 | def feed_infer(output_file, infer_func): 17 | """ 18 | This is a function that implements a way to write the user's inference result to the output file. 19 | :param output_file(str): File path to write output (Be sure to write in this location.) 20 | infer_func(function): The user's infer function bound to 'nsml.bind()' 21 | """ 22 | result = infer_func(os.path.join(DATASET_PATH, 'test')) 23 | result = [str(pred[1]) for pred in result] 24 | print('write output') 25 | with open(output_file, 'w') as file_writer: 26 | file_writer.write("\n".join(result)) 27 | 28 | if os.stat(output_file).st_size == 0: 29 | raise AssertionError('output result of inference is nothing') -------------------------------------------------------------------------------- /1_reg_dust/evaluation.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import numpy as np 3 | from sklearn.metrics import f1_score, accuracy_score, mean_squared_error 4 | from warnings import filterwarnings 5 | filterwarnings('ignore') 6 | 7 | def weighted_mse(targets, predicts): 8 | #sum_val = 0.0 9 | pred = np.asarray(predicts) 10 | gt = np.asarray(targets) 11 | w_mse = 0.3*mean_squared_error(gt[:,0], pred[:,0]) + 0.7*mean_squared_error(gt[:,1], pred[:,1]) 12 | #for idx, v in enumerate(targets): 13 | # #try: 14 | # sum_val += (0.3*(v[0]-predicts[idx][0])*(v[0]-predicts[idx][0])+0.7*(v[1]-predicts[idx][1])*(v[1]-predicts[idx][1])) 15 | # #except IndexError: 16 | # # print(v, predicts[idx]) 17 | #w_mse = sum_val/len(targets) 18 | return w_mse 19 | 20 | def ic_metric(targets, predicts): 21 | #targets = np.asarray(targets) 22 | #targets = [val.] 23 | predicts = np.asarray(predicts) 24 | 25 | #macro_f1_value = f1_score(targets, predicts, average='macro') 26 | #acc_value = accuracy_score(targets, predicts) 27 | #mse_score = mean_squared_error(targets, predicts) 28 | mse_score = weighted_mse(targets, predicts) 29 | return mse_score 30 | #return macro_f1_value, acc_value 31 | 32 | 33 | def evaluate(pred_file, gt_file): 34 | preds = read_label_file(pred_file) 35 | #gts = read_label_file(gt_file) 36 | gts = np.load(gt_file) 37 | ppreds = [] 38 | for label in preds: 39 | #try: 40 | labels = label.strip().replace("[","").replace("]","").split(',') 41 | if len(labels) > 2: 42 | labels = [l for l in labels if l != " "] 43 | ppreds.append((np.float32(labels[0].strip()), np.float32(labels[-1].strip()))) 44 | 45 | #except ValueError: 46 | # print (labels) 47 | # exit() 48 | # preds = [(np.float32(label[0]), np.float32(label[1])) for label in preds] 49 | #gts = [np.float32(label) for label in gts] 50 | 51 | mse_score = ic_metric(gts, ppreds) 52 | #f1, accuracy = ic_metric(gts, preds) 53 | return mse_score 54 | 55 | 56 | def read_label_file(file_name): 57 | #label = np.load(file_name) 58 | with open(file_name, 'r') as f: 59 | label = f.read().split('\n') 60 | 61 | return label 62 | 63 | 64 | if __name__ == '__main__': 65 | args = argparse.ArgumentParser() 66 | args.add_argument('--prediction', type=str, default='prediction.txt') 67 | config = args.parse_args() 68 | label_path = '/data/1_reg_dust/test/test_label' 69 | 70 | print(evaluate(config.prediction, label_path)) -------------------------------------------------------------------------------- /1_reg_dust/main_kr.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.autograd import Variable 3 | import numpy as np 4 | 5 | from nsml import DATASET_PATH 6 | import nsml 7 | import h5py 8 | 9 | from keras.models import Sequential 10 | from keras.layers import Dense, Activation 11 | from keras.optimizers import RMSprop 12 | import keras.models 13 | from sklearn.metrics import f1_score, accuracy_score, mean_squared_error 14 | 15 | #import glob 16 | import os 17 | import argparse 18 | 19 | FEATURE_DIM = 14 #지역(0~9), 연(2016~2019), 월, 일, t-5 ~ t-1의 미세 & 초미세 20 | OUTPUT_DIM = 2 # t-time의 (미세, 초미세) 21 | 22 | def bind_model(model): 23 | def save(path, *args, **kwargs): 24 | # save the model with 'checkpoint' dictionary. 25 | 26 | model.model.save(os.path.join(path, 'model')) 27 | 28 | def load(path, *args, **kwargs): 29 | model = keras.models.load_model(os.path.join(path, 'model')) 30 | return model 31 | 32 | def infer(path, **kwargs): 33 | return inference(path, model, config) 34 | 35 | nsml.bind(save, load, infer) 36 | 37 | 38 | def inference(path, model, config, **kwargs): 39 | 40 | #model.eval() 41 | 42 | test_path = DATASET_PATH+'/test/test_data' 43 | data = convData(np.load(test_path)) 44 | mean10_val = np.mean(data[:,4::2]) 45 | pred_val = model.model.predict(data/mean10_val).tolist() 46 | pred_results = [[step, val] for step, val in enumerate(pred_val)] 47 | return pred_results 48 | 49 | def convData(data_arr): 50 | v = np.zeros(FEATURE_DIM, dtype=np.float32) 51 | v[1] = 2016 52 | new_d = np.asarray([d - v for d in data_arr]) 53 | return new_d 54 | 55 | class Model(): 56 | def __init__(self, lr): 57 | self.model = Sequential([Dense(32, input_shape=(FEATURE_DIM,)), Activation('relu'), Dense(OUTPUT_DIM), Activation('relu')]) 58 | rms_pr = RMSprop(lr=lr) 59 | self.model.compile(optimizer=rms_pr, loss='mse') 60 | return 61 | 62 | 63 | 64 | if __name__ == '__main__': 65 | 66 | args = argparse.ArgumentParser() 67 | 68 | # DONOTCHANGE: They are reserved for nsml 69 | args.add_argument('--mode', type=str, default='train', help='submit일때 해당값이 test로 설정됩니다.') 70 | args.add_argument('--iteration', type=str, default='0', 71 | help='fork 명령어를 입력할때의 체크포인트로 설정됩니다. 체크포인트 옵션을 안주면 마지막 wall time 의 model 을 가져옵니다.') 72 | args.add_argument('--pause', type=int, default=0, help='model 을 load 할때 1로 설정됩니다.') 73 | 74 | config = args.parse_args() 75 | 76 | EP, LR = 10, 0.001 77 | # Bind model 78 | model = Model(LR) 79 | bind_model(model) 80 | 81 | 82 | # DONOTCHANGE: They are reserved for nsml 83 | # Warning: Do not load data before the following code! 84 | if config.pause: 85 | nsml.paused(scope=locals()) 86 | 87 | if config.mode == "train": 88 | 89 | train_dataset_path = DATASET_PATH + '/train/train_data' 90 | #train_data_files = sorted(glob.glob(train_dataset_path + '/*.npy')) 91 | tr_X = convData(np.load(train_dataset_path)) 92 | mean10_val = np.mean(tr_X[:,4::2]) 93 | train_label_file = DATASET_PATH + '/train/train_label' 94 | tr_Y = np.load(train_label_file) 95 | 96 | model.model.fit(tr_X/mean10_val, tr_Y, epochs=EP, batch_size=1024) 97 | 98 | 99 | #tr_X = np.load(DATASET_PATH + '/train/train_data.npy') 100 | #tr_Y = np.load(DATASET_PATH + '/train/train_label.npy') # numpy array of labels. They are all zeros!! 101 | 102 | 103 | # Train 104 | 105 | nsml.save(EP) 106 | 107 | # Save 108 | #epoch = 1 109 | #nsml.save(epoch) # If you are using neural networks, you may want to use epoch as checkpoints 110 | 111 | # Load test (Check if load method works well) 112 | #nsml.load(epoch) 113 | 114 | # Infer test 115 | #for file in train_data_files[:10]: 116 | # data = np.load(file) 117 | # print(model.forward(data)) -------------------------------------------------------------------------------- /1_reg_dust/requirements.txt: -------------------------------------------------------------------------------- 1 | h5py 2 | -------------------------------------------------------------------------------- /1_reg_dust/setup.py: -------------------------------------------------------------------------------- 1 | #nsml: nsml/ml:cuda9.0-cudnn7-tf-1.11torch1.0keras2.2 2 | 3 | from distutils.core import setup 4 | 5 | setup( 6 | name='CraneA_NSML_Baseline', 7 | version='1', 8 | description='CraneA_NSML_Baseline', 9 | install_requires=[ 10 | ] 11 | ) -------------------------------------------------------------------------------- /20_scls_stt/.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | -------------------------------------------------------------------------------- /20_scls_stt/README.md: -------------------------------------------------------------------------------- 1 | # nipa-speech-2019 2 | 3 | NIPA Speech Recognition Baseline 4 | 5 | ### 실행 6 | * ./run.sh [ENTER] 7 | * run.sh 8 | ``` 9 | #!/bin/sh 10 | 11 | BATCH_SIZE=64 12 | WORKER_SIZE=4 13 | GPU_SIZE=2 14 | CPU_SIZE=4 15 | 16 | nsml run -d 20_scls_stt -g $GPU_SIZE -c $CPU_SIZE -a "--batch_size $BATCH_SIZE --workers $WORKER_SIZE --use_attention" 17 | ``` 18 | 19 | ### files 20 | * script.labels 21 | * 정답 낱글자들에 대한 레이블 매핑 테이블 22 | * "_" : padding 23 | * "\" : begin of sentence 24 | * "\" : end of sentence 25 | * train/train_label 26 | * 모드 training wav파일들의 정답 레이블 리스트 저장 27 | * 정답 레이블의 실제 문자열은 sript.labels와 매칭해서 확이 가능 28 | ``` 29 | # ,