diff --git a/.gitignore b/.gitignore index 69cfdca..7226740 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,7 @@ __pycache__/ mmsegmentation/work_dirs mmsegmentation/out mmsegmentation/wandb -*.jpg \ No newline at end of file +*.jpg +mmsegmentation/output +mmsegmentation/nohup.out + diff --git a/mmsegmentation/configs/_ljh_/_base_/default_runtime.py b/mmsegmentation/configs/_ljh_/_base_/default_runtime.py index 15dc2c6..deeafdd 100644 --- a/mmsegmentation/configs/_ljh_/_base_/default_runtime.py +++ b/mmsegmentation/configs/_ljh_/_base_/default_runtime.py @@ -12,5 +12,5 @@ log_level = 'INFO' load_from = None resume_from = None -workflow = [('train', 1)] +workflow = [('train', 1),('val',1)] cudnn_benchmark = True diff --git a/mmsegmentation/configs/_ljh_/_base_/models/psanet_r50-d8.py b/mmsegmentation/configs/_ljh_/_base_/models/psanet_r50-d8.py new file mode 100644 index 0000000..689513f --- /dev/null +++ b/mmsegmentation/configs/_ljh_/_base_/models/psanet_r50-d8.py @@ -0,0 +1,49 @@ +# model settings +norm_cfg = dict(type='SyncBN', requires_grad=True) +model = dict( + type='EncoderDecoder', + pretrained='open-mmlab://resnet50_v1c', + backbone=dict( + type='ResNetV1c', + depth=50, + num_stages=4, + out_indices=(0, 1, 2, 3), + dilations=(1, 1, 2, 4), + strides=(1, 2, 1, 1), + norm_cfg=norm_cfg, + norm_eval=False, + style='pytorch', + contract_dilation=True), + decode_head=dict( + type='PSAHead', + in_channels=2048, + in_index=3, + channels=512, + mask_size=(97, 97), + psa_type='bi-direction', + compact=False, + shrink_factor=2, + normalization_factor=1.0, + psa_softmax=True, + dropout_ratio=0.1, + num_classes=19, + norm_cfg=norm_cfg, + align_corners=False, + loss_decode=dict( + type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)), + auxiliary_head=dict( + type='FCNHead', + in_channels=1024, + in_index=2, + channels=256, + num_convs=1, + concat_input=False, + dropout_ratio=0.1, + num_classes=19, + norm_cfg=norm_cfg, + align_corners=False, + loss_decode=dict( + type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.4)), + # model training and testing settings + train_cfg=dict(), + test_cfg=dict(mode='whole')) diff --git a/mmsegmentation/configs/_ljh_/_base_/schedules/schedule_160k.py b/mmsegmentation/configs/_ljh_/_base_/schedules/schedule_160k.py index 39630f2..001ecfe 100644 --- a/mmsegmentation/configs/_ljh_/_base_/schedules/schedule_160k.py +++ b/mmsegmentation/configs/_ljh_/_base_/schedules/schedule_160k.py @@ -1,8 +1,11 @@ # optimizer -optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0005) +optimizer = dict(type='SGD', lr=0.001, momentum=0.9, weight_decay=0.0005) +# optimizer = dict(type='AdamW', lr=0.001, betas=(0.9, 0.999), weight_decay=0.0005) optimizer_config = dict() + # learning policy -lr_config = dict(policy='poly', power=0.9, min_lr=1e-4, by_epoch=False) +lr_config = dict(policy='poly', power=0.9, min_lr=1e-5, by_epoch=False) + # runtime settings runner = dict(type='IterBasedRunner', max_iters=160000) checkpoint_config = dict(by_epoch=False, interval=16000) diff --git a/mmsegmentation/configs/_ljh_/psanet_r101-d8_160k_pascal.py b/mmsegmentation/configs/_ljh_/psanet_r101-d8_160k_pascal.py new file mode 100644 index 0000000..da7ded6 --- /dev/null +++ b/mmsegmentation/configs/_ljh_/psanet_r101-d8_160k_pascal.py @@ -0,0 +1,2 @@ +_base_ = './psanet_r50-d8_160k_pascal.py' +model = dict(pretrained='open-mmlab://resnet101_v1c', backbone=dict(depth=101)) diff --git a/mmsegmentation/configs/_ljh_/psanet_r50-d8_160k_pascal.py b/mmsegmentation/configs/_ljh_/psanet_r50-d8_160k_pascal.py new file mode 100644 index 0000000..91dd543 --- /dev/null +++ b/mmsegmentation/configs/_ljh_/psanet_r50-d8_160k_pascal.py @@ -0,0 +1,9 @@ +_base_ = [ + './_base_/models/psanet_r50-d8.py', + './_base_/datasets/pascal_context.py', './_base_/default_runtime.py', + './_base_/schedules/schedule_160k.py' +] +model = dict( + decode_head=dict(align_corners=True), + auxiliary_head=dict(align_corners=True), + test_cfg=dict(mode='slide', crop_size=(769, 769), stride=(513, 513))) diff --git a/mmsegmentation/configs/_owj_/_base_/datasets/pascal_context.py b/mmsegmentation/configs/_owj_/_base_/datasets/pascal_context.py new file mode 100644 index 0000000..246530b --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/datasets/pascal_context.py @@ -0,0 +1,60 @@ +# dataset settings +dataset_type = 'PascalContextDataset' +data_root = '../data/mmseg' +img_norm_cfg = dict( + mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) + +img_scale = (520, 520) +crop_size = (480, 480) + +train_pipeline = [ + dict(type='LoadImageFromFile'), + dict(type='LoadAnnotations'), + dict(type='Resize', img_scale=img_scale, ratio_range=(0.5, 2.0)), + dict(type='RandomCrop', crop_size=crop_size, cat_max_ratio=0.75), + dict(type='RandomFlip', prob=0.5), + dict(type='PhotoMetricDistortion'), + dict(type='Normalize', **img_norm_cfg), + dict(type='Pad', size=crop_size, pad_val=0, seg_pad_val=255), + dict(type='DefaultFormatBundle'), + dict(type='Collect', keys=['img', 'gt_semantic_seg']), +] +test_pipeline = [ + dict(type='LoadImageFromFile'), + dict( + type='MultiScaleFlipAug', + img_scale=img_scale, + # img_ratios=[0.5, 0.75, 1.0, 1.25, 1.5, 1.75], + flip=False, + transforms=[ + dict(type='Resize', keep_ratio=True), + dict(type='RandomFlip'), + dict(type='Normalize', **img_norm_cfg), + dict(type='ImageToTensor', keys=['img']), + dict(type='Collect', keys=['img']), + ]) +] +data = dict( + samples_per_gpu=4, + workers_per_gpu=4, + train=dict( + type=dataset_type, + data_root=data_root, + img_dir='images', + ann_dir='labels', + split='splits/train.txt', + pipeline=train_pipeline), + val=dict( + type=dataset_type, + data_root=data_root, + img_dir='images', + ann_dir='labels', + split='splits/val.txt', + pipeline=test_pipeline), + test=dict( + type=dataset_type, + data_root=data_root, + img_dir='images', + ann_dir='labels', + split='splits/test.txt', + pipeline=test_pipeline)) diff --git a/mmsegmentation/configs/_owj_/_base_/default_runtime.py b/mmsegmentation/configs/_owj_/_base_/default_runtime.py new file mode 100644 index 0000000..699b7d4 --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/default_runtime.py @@ -0,0 +1,16 @@ +# yapf:disable +log_config = dict( + interval=50, + hooks=[ + dict(type="TextLoggerHook", by_epoch=False), + dict(type="WandbLoggerHook", init_kwargs=dict(entity='cv04',project="Semantic Segmentation", name="ccnet_test")), + ], +) + +# yapf:enable +dist_params = dict(backend='nccl') +log_level = 'INFO' +load_from = None +resume_from = None +workflow = [('train', 1)] +cudnn_benchmark = True diff --git a/mmsegmentation/configs/_owj_/_base_/models/ccnet_r50-d8.py b/mmsegmentation/configs/_owj_/_base_/models/ccnet_r50-d8.py new file mode 100644 index 0000000..794148f --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/models/ccnet_r50-d8.py @@ -0,0 +1,44 @@ +# model settings +norm_cfg = dict(type='SyncBN', requires_grad=True) +model = dict( + type='EncoderDecoder', + pretrained='open-mmlab://resnet50_v1c', + backbone=dict( + type='ResNetV1c', + depth=50, + num_stages=4, + out_indices=(0, 1, 2, 3), + dilations=(1, 1, 2, 4), + strides=(1, 2, 1, 1), + norm_cfg=norm_cfg, + norm_eval=False, + style='pytorch', + contract_dilation=True), + decode_head=dict( + type='CCHead', + in_channels=2048, + in_index=3, + channels=512, + recurrence=2, + dropout_ratio=0.1, + num_classes=19, + norm_cfg=norm_cfg, + align_corners=False, + loss_decode=dict( + type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)), + auxiliary_head=dict( + type='FCNHead', + in_channels=1024, + in_index=2, + channels=256, + num_convs=1, + concat_input=False, + dropout_ratio=0.1, + num_classes=19, + norm_cfg=norm_cfg, + align_corners=False, + loss_decode=dict( + type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.4)), + # model training and testing settings + train_cfg=dict(), + test_cfg=dict(mode='whole')) diff --git a/mmsegmentation/configs/_owj_/_base_/models/ocrnet_hr18.py b/mmsegmentation/configs/_owj_/_base_/models/ocrnet_hr18.py new file mode 100644 index 0000000..c60f62a --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/models/ocrnet_hr18.py @@ -0,0 +1,68 @@ +# model settings +norm_cfg = dict(type='SyncBN', requires_grad=True) +model = dict( + type='CascadeEncoderDecoder', + num_stages=2, + pretrained='open-mmlab://msra/hrnetv2_w18', + backbone=dict( + type='HRNet', + norm_cfg=norm_cfg, + norm_eval=False, + extra=dict( + stage1=dict( + num_modules=1, + num_branches=1, + block='BOTTLENECK', + num_blocks=(4, ), + num_channels=(64, )), + stage2=dict( + num_modules=1, + num_branches=2, + block='BASIC', + num_blocks=(4, 4), + num_channels=(18, 36)), + stage3=dict( + num_modules=4, + num_branches=3, + block='BASIC', + num_blocks=(4, 4, 4), + num_channels=(18, 36, 72)), + stage4=dict( + num_modules=3, + num_branches=4, + block='BASIC', + num_blocks=(4, 4, 4, 4), + num_channels=(18, 36, 72, 144)))), + decode_head=[ + dict( + type='FCNHead', + in_channels=[18, 36, 72, 144], + channels=sum([18, 36, 72, 144]), + in_index=(0, 1, 2, 3), + input_transform='resize_concat', + kernel_size=1, + num_convs=1, + concat_input=False, + dropout_ratio=-1, + num_classes=19, + norm_cfg=norm_cfg, + align_corners=False, + loss_decode=dict( + type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.4)), + dict( + type='OCRHead', + in_channels=[18, 36, 72, 144], + in_index=(0, 1, 2, 3), + input_transform='resize_concat', + channels=512, + ocr_channels=256, + dropout_ratio=-1, + num_classes=19, + norm_cfg=norm_cfg, + align_corners=False, + loss_decode=dict( + type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)), + ], + # model training and testing settings + train_cfg=dict(), + test_cfg=dict(mode='whole')) diff --git a/mmsegmentation/configs/_owj_/_base_/schedules/custom_schedule.py b/mmsegmentation/configs/_owj_/_base_/schedules/custom_schedule.py new file mode 100644 index 0000000..69dbead --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/schedules/custom_schedule.py @@ -0,0 +1,18 @@ +# optimizer +optimizer = dict(type='AdamW', lr=0.00006, betas=(0.9, 0.999), weight_decay=0.01, + # paramwise_cfg=dict(custom_keys={'absolute_pos_embed': dict(decay_mult=0.), + # 'relative_position_bias_table': dict(decay_mult=0.), + # 'norm': dict(decay_mult=0.)}) + ) +optimizer_config = dict() + +lr_config = dict(policy='poly', + warmup='linear', + warmup_iters=1500, + warmup_ratio=1e-6, + power=1.0, min_lr=0.0, by_epoch=False) + +# runtime settings +runner = dict(type='IterBasedRunner', max_iters=320000) +checkpoint_config = dict(by_epoch=False, interval=32000) +evaluation = dict(interval=32000, metric='mIoU') diff --git a/mmsegmentation/configs/_owj_/_base_/schedules/schedule_160k.py b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_160k.py new file mode 100644 index 0000000..39630f2 --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_160k.py @@ -0,0 +1,9 @@ +# optimizer +optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0005) +optimizer_config = dict() +# learning policy +lr_config = dict(policy='poly', power=0.9, min_lr=1e-4, by_epoch=False) +# runtime settings +runner = dict(type='IterBasedRunner', max_iters=160000) +checkpoint_config = dict(by_epoch=False, interval=16000) +evaluation = dict(interval=16000, metric='mIoU', pre_eval=True) diff --git a/mmsegmentation/configs/_owj_/_base_/schedules/schedule_20k.py b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_20k.py new file mode 100644 index 0000000..73c7021 --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_20k.py @@ -0,0 +1,9 @@ +# optimizer +optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0005) +optimizer_config = dict() +# learning policy +lr_config = dict(policy='poly', power=0.9, min_lr=1e-4, by_epoch=False) +# runtime settings +runner = dict(type='IterBasedRunner', max_iters=20000) +checkpoint_config = dict(by_epoch=False, interval=2000) +evaluation = dict(interval=2000, metric='mIoU', pre_eval=True) diff --git a/mmsegmentation/configs/_owj_/_base_/schedules/schedule_320k.py b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_320k.py new file mode 100644 index 0000000..a0b2306 --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_320k.py @@ -0,0 +1,9 @@ +# optimizer +optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0005) +optimizer_config = dict() +# learning policy +lr_config = dict(policy='poly', power=0.9, min_lr=1e-4, by_epoch=False) +# runtime settings +runner = dict(type='IterBasedRunner', max_iters=320000) +checkpoint_config = dict(by_epoch=False, interval=32000) +evaluation = dict(interval=32000, metric='mIoU') diff --git a/mmsegmentation/configs/_owj_/_base_/schedules/schedule_40k.py b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_40k.py new file mode 100644 index 0000000..2a74d7b --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_40k.py @@ -0,0 +1,9 @@ +# optimizer +optimizer = dict(type='AdamW', lr=0.001, momentum=0.9, weight_decay=0.0005) +optimizer_config = dict() +# learning policy +lr_config = dict(policy='poly', power=0.9, min_lr=1e-4, by_epoch=False) +# runtime settings +runner = dict(type='IterBasedRunner', max_iters=40000) +checkpoint_config = dict(by_epoch=False, interval=4000) +evaluation = dict(interval=4000, metric='mIoU', pre_eval=True) diff --git a/mmsegmentation/configs/_owj_/_base_/schedules/schedule_80k.py b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_80k.py new file mode 100644 index 0000000..8365a87 --- /dev/null +++ b/mmsegmentation/configs/_owj_/_base_/schedules/schedule_80k.py @@ -0,0 +1,9 @@ +# optimizer +optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0005) +optimizer_config = dict() +# learning policy +lr_config = dict(policy='poly', power=0.9, min_lr=1e-4, by_epoch=False) +# runtime settings +runner = dict(type='IterBasedRunner', max_iters=80000) +checkpoint_config = dict(by_epoch=False, interval=8000) +evaluation = dict(interval=8000, metric='mIoU', pre_eval=True) diff --git a/mmsegmentation/configs/_owj_/ccnet_r101-d8_512x1024_40k_cityscapes.py b/mmsegmentation/configs/_owj_/ccnet_r101-d8_512x1024_40k_cityscapes.py new file mode 100644 index 0000000..d2bac38 --- /dev/null +++ b/mmsegmentation/configs/_owj_/ccnet_r101-d8_512x1024_40k_cityscapes.py @@ -0,0 +1,2 @@ +_base_ = './ccnet_r50-d8_512x1024_40k_cityscapes.py' +model = dict(pretrained='open-mmlab://resnet101_v1c', backbone=dict(depth=101)) diff --git a/mmsegmentation/configs/_owj_/ccnet_r50-d8_512x1024_40k_cityscapes.py b/mmsegmentation/configs/_owj_/ccnet_r50-d8_512x1024_40k_cityscapes.py new file mode 100644 index 0000000..c3b9b56 --- /dev/null +++ b/mmsegmentation/configs/_owj_/ccnet_r50-d8_512x1024_40k_cityscapes.py @@ -0,0 +1,4 @@ +_base_ = [ + './_base_/models/ccnet_r50-d8.py', './_base_/datasets/pascal_context.py', + './_base_/default_runtime.py', './_base_/schedules/custom_schedule.py' +] diff --git a/mmsegmentation/configs/_owj_/ocrnet_hr18_512x1024_40k_cityscapes.py b/mmsegmentation/configs/_owj_/ocrnet_hr18_512x1024_40k_cityscapes.py new file mode 100644 index 0000000..722e317 --- /dev/null +++ b/mmsegmentation/configs/_owj_/ocrnet_hr18_512x1024_40k_cityscapes.py @@ -0,0 +1,4 @@ +_base_ = [ + './_base_/models/ocrnet_hr18.py', './_base_/datasets/pascal_context.py', + './_base_/default_runtime.py', './_base_/schedules/custom_schedule.py' +] diff --git a/mmsegmentation/configs/_owj_/ocrnet_hr48_512x1024_40k_cityscapes.py b/mmsegmentation/configs/_owj_/ocrnet_hr48_512x1024_40k_cityscapes.py new file mode 100644 index 0000000..0aada9d --- /dev/null +++ b/mmsegmentation/configs/_owj_/ocrnet_hr48_512x1024_40k_cityscapes.py @@ -0,0 +1,39 @@ +_base_ = './ocrnet_hr18_512x1024_40k_cityscapes.py' +norm_cfg = dict(type='SyncBN', requires_grad=True) +model = dict( + pretrained='open-mmlab://msra/hrnetv2_w48', + backbone=dict( + extra=dict( + stage2=dict(num_channels=(48, 96)), + stage3=dict(num_channels=(48, 96, 192)), + stage4=dict(num_channels=(48, 96, 192, 384)))), + decode_head=[ + dict( + type='FCNHead', + in_channels=[48, 96, 192, 384], + channels=sum([48, 96, 192, 384]), + input_transform='resize_concat', + in_index=(0, 1, 2, 3), + kernel_size=1, + num_convs=1, + norm_cfg=norm_cfg, + concat_input=False, + dropout_ratio=-1, + num_classes=19, + align_corners=False, + loss_decode=dict( + type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.4)), + dict( + type='OCRHead', + in_channels=[48, 96, 192, 384], + channels=512, + ocr_channels=256, + input_transform='resize_concat', + in_index=(0, 1, 2, 3), + norm_cfg=norm_cfg, + dropout_ratio=-1, + num_classes=19, + align_corners=False, + loss_decode=dict( + type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)) + ]) diff --git a/mmsegmentation/soft_voting.ipynb b/mmsegmentation/soft_voting.ipynb new file mode 100644 index 0000000..95f3b97 --- /dev/null +++ b/mmsegmentation/soft_voting.ipynb @@ -0,0 +1,132 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "import glob\n", + "import os\n", + "import pandas as pd\n", + "from tqdm import tqdm" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "624it [00:52, 11.78it/s]\n" + ] + } + ], + "source": [ + "data_root = 'out'\n", + "path_list = ['test','psa_80k']\n", + "\n", + "pt_lists = []\n", + "for p in path_list:\n", + " pt_list = sorted(glob.glob(f'{data_root}/{p}/*.pt'))\n", + " pt_lists.append(pt_list)\n", + "\n", + "os.makedirs(f'{data_root}/essamble', exist_ok=True)\n", + "\n", + "for p in tqdm(zip(*pt_lists),total=len(pt_list)):\n", + " logits = list(map(lambda x: torch.load(x),p))\n", + " logit_sum = sum(logits)\n", + " result = torch.argmax(logit_sum,dim=1)\n", + " file_name = p[0].split('/')[-1].replace('.pt','.txt')\n", + " with open(f'{data_root}/essamble/{file_name}', 'w') as f:\n", + " for r in result[0]:\n", + " r_list = r.tolist()\n", + " f.write(' '.join(map(str,r_list)) + '\\n')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + " 3%|▎ | 17/624 [00:00<00:03, 162.46it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "making submission ...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100%|██████████| 624/624 [00:04<00:00, 146.46it/s]\n" + ] + } + ], + "source": [ + "print('\\nmaking submission ...')\n", + "dic = {\"image_id\":[],\"PredictionString\":[]}\n", + "for path in tqdm(sorted(glob.glob(f'{data_root}/essamble/*.txt'))):\n", + " image_id = path.replace(f'{data_root}/essamble/','').replace('_03_','_03/').replace('vt_','vt/').replace('.txt','.jpg')\n", + " with open(path, 'r') as f:\n", + " value_list = f.readlines()\n", + " new_value_list = []\n", + " for i in range(0,len(value_list),2):\n", + " value = value_list[i].split(' ')\n", + " value = ' '.join(value[0::2])\n", + " new_value_list.append(value)\n", + "\n", + " dic[\"image_id\"].append(image_id)\n", + " dic[\"PredictionString\"].append(' '.join(new_value_list))\n", + "\n", + "df = pd.DataFrame(dic)\n", + "df.to_csv(f'{data_root}/essamble/_submission.csv',index=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "d4d1e4263499bec80672ea0156c357c1ee493ec2b1c70f0acce89fc37c4a6abe" + }, + "kernelspec": { + "display_name": "Python 3.8.5 ('base')", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/mmsegmentation/tools/test.py b/mmsegmentation/tools/test.py index 2df3f4f..141bbef 100644 --- a/mmsegmentation/tools/test.py +++ b/mmsegmentation/tools/test.py @@ -6,9 +6,11 @@ import shutil import time import warnings +from matplotlib.pyplot import axis import mmcv import torch +import torchvision from mmcv.cnn.utils import revert_sync_batchnorm from mmcv.parallel import MMDataParallel, MMDistributedDataParallel from mmcv.runner import (get_dist_info, init_dist, load_checkpoint, @@ -237,87 +239,114 @@ def main(): print('"PALETTE" not found in meta, use dataset.PALETTE instead') model.PALETTE = dataset.PALETTE - # clean gpu memory when starting a new evaluation. - torch.cuda.empty_cache() - eval_kwargs = {} if args.eval_options is None else args.eval_options - - # Deprecated - efficient_test = eval_kwargs.get('efficient_test', False) - if efficient_test: - warnings.warn( - '``efficient_test=True`` does not have effect in tools/test.py, ' - 'the evaluation and format results are CPU memory efficient by ' - 'default') - - eval_on_format_results = ( - args.eval is not None and 'cityscapes' in args.eval) - if eval_on_format_results: - assert len(args.eval) == 1, 'eval on format results is not ' \ - 'applicable for metrics other than ' \ - 'cityscapes' - if args.format_only or eval_on_format_results: - if 'imgfile_prefix' in eval_kwargs: - tmpdir = eval_kwargs['imgfile_prefix'] - else: - tmpdir = '.format_cityscapes' - eval_kwargs.setdefault('imgfile_prefix', tmpdir) - mmcv.mkdir_or_exist(tmpdir) - else: - tmpdir = None - - if not distributed: - warnings.warn( - 'SyncBN is only supported with DDP. To be compatible with DP, ' - 'we convert SyncBN to BN. Please use dist_train.sh which can ' - 'avoid this error.') - if not torch.cuda.is_available(): - assert digit_version(mmcv.__version__) >= digit_version('1.4.4'), \ - 'Please use MMCV >= 1.4.4 for CPU training!' - model = revert_sync_batchnorm(model) - model = MMDataParallel(model, device_ids=cfg.gpu_ids) - results = single_gpu_test( - model, - data_loader, - args.show, - args.show_dir, - False, - args.opacity, - pre_eval=args.eval is not None and not eval_on_format_results, - format_only=args.format_only or eval_on_format_results, - format_args=eval_kwargs) - else: - model = MMDistributedDataParallel( - model.cuda(), - device_ids=[torch.cuda.current_device()], - broadcast_buffers=False) - results = multi_gpu_test( - model, - data_loader, - args.tmpdir, - args.gpu_collect, - False, - pre_eval=args.eval is not None and not eval_on_format_results, - format_only=args.format_only or eval_on_format_results, - format_args=eval_kwargs) + device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') - rank, _ = get_dist_info() - if rank == 0: - if args.out: - warnings.warn( - 'The behavior of ``args.out`` has been changed since MMSeg ' - 'v0.16, the pickled outputs could be seg map as type of ' - 'np.array, pre-eval results or file paths for ' - '``dataset.format_results()``.') - print(f'\nwriting results to {args.out}') - mmcv.dump(results, args.out) - if args.eval: - eval_kwargs.update(metric=args.eval) - metric = dataset.evaluate(results, **eval_kwargs) - metric_dict = dict(config=args.config, metric=metric) - mmcv.dump(metric_dict, json_file, indent=4) - if tmpdir is not None and eval_on_format_results: - # remove tmp dir when cityscapes evaluation - shutil.rmtree(tmpdir) + model.eval() + model.to(device) + # Get logit + os.makedirs(args.show_dir,exist_ok=True) + for i in tqdm(range(len(dataset))): + sample = dataset[i] + img = sample['img'][0].data.unsqueeze(0).to(device) + img = torchvision.transforms.Resize((512,512))(img) + logit = model.whole_inference(img=img, img_meta=sample['img_metas'][0].data, rescale=False) + logit = torch.nn.Softmax(dim=None)(logit) + result = torch.argmax(logit,dim=1) + + file_name = sample['img_metas'][0].data['filename'].split('/')[-1] + # file_name_ann = file_name.replace('.jpg','_ann.jpg') + file_name_pt = file_name.replace('.jpg','.pt') + file_name_txt = file_name.replace('.jpg','.txt') + + # logit 저장 + torch.save(logit,f'{args.show_dir}/{file_name_pt}') + # result 저장 + with open(f'{args.show_dir}/{file_name_txt}','w') as f: + for r in result[0]: + f.write(' '.join(map(str,r.tolist())) + '\n') + + + # # clean gpu memory when starting a new evaluation. + # torch.cuda.empty_cache() + # eval_kwargs = {} if args.eval_options is None else args.eval_options + + # # Deprecated + # efficient_test = eval_kwargs.get('efficient_test', False) + # if efficient_test: + # warnings.warn( + # '``efficient_test=True`` does not have effect in tools/test.py, ' + # 'the evaluation and format results are CPU memory efficient by ' + # 'default') + + # eval_on_format_results = ( + # args.eval is not None and 'cityscapes' in args.eval) + # if eval_on_format_results: + # assert len(args.eval) == 1, 'eval on format results is not ' \ + # 'applicable for metrics other than ' \ + # 'cityscapes' + # if args.format_only or eval_on_format_results: + # if 'imgfile_prefix' in eval_kwargs: + # tmpdir = eval_kwargs['imgfile_prefix'] + # else: + # tmpdir = '.format_cityscapes' + # eval_kwargs.setdefault('imgfile_prefix', tmpdir) + # mmcv.mkdir_or_exist(tmpdir) + # else: + # tmpdir = None + + # if not distributed: + # warnings.warn( + # 'SyncBN is only supported with DDP. To be compatible with DP, ' + # 'we convert SyncBN to BN. Please use dist_train.sh which can ' + # 'avoid this error.') + # if not torch.cuda.is_available(): + # assert digit_version(mmcv.__version__) >= digit_version('1.4.4'), \ + # 'Please use MMCV >= 1.4.4 for CPU training!' + # model = revert_sync_batchnorm(model) + # model = MMDataParallel(model, device_ids=cfg.gpu_ids) + # results = single_gpu_test( + # model, + # data_loader, + # args.show, + # args.show_dir, + # False, + # args.opacity, + # pre_eval=args.eval is not None and not eval_on_format_results, + # format_only=args.format_only or eval_on_format_results, + # format_args=eval_kwargs) + # else: + # model = MMDistributedDataParallel( + # model.cuda(), + # device_ids=[torch.cuda.current_device()], + # broadcast_buffers=False) + # results = multi_gpu_test( + # model, + # data_loader, + # args.tmpdir, + # args.gpu_collect, + # False, + # pre_eval=args.eval is not None and not eval_on_format_results, + # format_only=args.format_only or eval_on_format_results, + # format_args=eval_kwargs) + + # rank, _ = get_dist_info() + # if rank == 0: + # if args.out: + # warnings.warn( + # 'The behavior of ``args.out`` has been changed since MMSeg ' + # 'v0.16, the pickled outputs could be seg map as type of ' + # 'np.array, pre-eval results or file paths for ' + # '``dataset.format_results()``.') + # print(f'\nwriting results to {args.out}') + # mmcv.dump(results, args.out) + # if args.eval: + # eval_kwargs.update(metric=args.eval) + # metric = dataset.evaluate(results, **eval_kwargs) + # metric_dict = dict(config=args.config, metric=metric) + # mmcv.dump(metric_dict, json_file, indent=4) + # if tmpdir is not None and eval_on_format_results: + # # remove tmp dir when cityscapes evaluation + # shutil.rmtree(tmpdir) print('\nmaking submission ...') dic = {"image_id":[],"PredictionString":[]}