-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththresh_and_size.py
202 lines (162 loc) · 6.44 KB
/
thresh_and_size.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import cv2
import collections
import time
import tqdm
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import KFold, train_test_split
import torchvision
import torchvision.transforms as transforms
import torch
from torch.utils.data import TensorDataset, DataLoader,Dataset
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.optim.lr_scheduler import StepLR, ReduceLROnPlateau, CosineAnnealingLR
import albumentations as albu
import configparser
import argparse
import wandb
# Catalyst is amazing.
from catalyst.data import Augmentor
from catalyst.dl import utils
from catalyst.data.reader import ImageReader, ScalarReader, ReaderCompose, LambdaReader
from catalyst.dl.runner import SupervisedRunner
# from catalyst.dl.runner import SupervisedWandbRunner as SupervisedRunner
from catalyst.contrib.models.segmentation import Unet
from catalyst.dl.callbacks import DiceCallback, EarlyStoppingCallback, InferCallback, CheckpointCallback
# PyTorch made my work much much easier.
import segmentation_models_pytorch as smp
from dataloader import SegmentationDataset, SegmentationDatasetTest, SegmentationDataset_withid
from augmentations import get_training_augmentation, get_validation_augmentation, get_preprocessing
from utils import *
import pickle
def get_ids(train_ids_file='../train_ids.pkl', valid_ids_file='../valid_ids.pkl'):
with open(train_ids_file, 'rb') as handle:
train_ids = pickle.load(handle)
with open(valid_ids_file, 'rb') as handle:
valid_ids = pickle.load(handle)
return train_ids, valid_ids
train_ids, valid_ids = get_ids()
# valid_ids = list(train_ids)+list(valid_ids)
# FIX LOADERS
# def get_loaders(bs=32, num_workers=4, preprocessing_fn=None):
# train_ids, valid_ids = get_ids()
# train_dataset = SegmentationDataset(ids=train_ids,
# transforms=get_training_augmentation(),
# preprocessing=get_preprocessing(preprocessing_fn),
# img_db="../input/train_images_525/train_images_525",
# mask_db="../input/mask")
# valid_dataset = SegmentationDataset(ids=valid_ids,
# transforms=get_validation_augmentation(),
# preprocessing=get_preprocessing(preprocessing_fn),
# img_db="../input/train_images_525/train_images_525",
# mask_db="../input/mask")
#
# train_loader = DataLoader(train_dataset, batch_size=bs,
# shuffle=True, num_workers=num_workers)
# valid_loader = DataLoader(valid_dataset, batch_size=bs,
# shuffle=False, num_workers=num_workers)
#
# loaders = {
# "train": train_loader,
# "valid": valid_loader
# }
# return loaders
def dice(img1, img2):
img1 = np.asarray(img1).astype(np.bool)
img2 = np.asarray(img2).astype(np.bool)
if img1.sum() + img2.sum() == 0:
print("ok...")
return 1
intersection = np.logical_and(img1, img2)
return 2. * intersection.sum() / (img1.sum() + img2.sum())
def post_process(probability, threshold, min_size):
"""
Post processing of each predicted mask, components with lesser number of pixels
than `min_size` are ignored
"""
# don't remember where I saw it
mask = cv2.threshold(probability, threshold, 1, cv2.THRESH_BINARY)[1]
num_component, component = cv2.connectedComponents(mask.astype(np.uint8))
predictions = np.zeros((350, 525), np.float32)
num = 0
for c in range(1, num_component):
p = (component == c)
if p.sum() > min_size:
predictions[p] = 1
num += 1
return predictions, num
sigmoid = lambda x: 1 / (1 + np.exp(-x))
bs = 8
num_workers = 0
encoder = 'efficientnet-b4'
arch = 'linknet'
model, preprocessing_fn = get_model(encoder, type=arch)
loaders = get_loaders(bs, num_workers, preprocessing_fn)
valid_dataset = SegmentationDataset(ids=valid_ids,
transforms=get_validation_augmentation(),
preprocessing=get_preprocessing(preprocessing_fn),
img_db="../input/train_images_480/",
mask_db="../input/train_masks_480/", npy=True)
valid_loader = DataLoader(valid_dataset, batch_size=bs,
shuffle=False, num_workers=num_workers)
logdir = f"./logs/{arch}_{encoder}"
model_path = f"{logdir}checkpoints/best.pth"
runner = SupervisedRunner()
encoded_pixels = []
loaders = {"infer": valid_loader}
runner.infer(
model=model,
loaders=loaders,
callbacks=[
CheckpointCallback(
resume=model_path),
InferCallback()
],
)
import tqdm
valid_masks = []
probabilities = np.zeros((int(555*4), 350, 525))
for i, (batch, output) in enumerate(tqdm.tqdm(zip(
valid_dataset, runner.callbacks[0].predictions["logits"]))):
image, mask = batch
for m in mask:
if m.shape != (350, 525):
m = cv2.resize(m, dsize=(525, 350), interpolation=cv2.INTER_LINEAR)
valid_masks.append(m)
for j, probability in enumerate(output):
if probability.shape != (350, 525):
probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR)
probabilities[i * 4 + j, :, :] = probability
class_params = {}
for class_id in range(4):
print(class_id)
attempts = []
for t in tqdm.tqdm(range(0, 100, 5)):
t /= 100
for ms in [100, 5000, 10000, 15000, 20000, 22000, 25000]:
masks = []
for i in range(class_id, len(probabilities), 4):
probability = probabilities[i]
predict, num_predict = post_process(sigmoid(probability), t, ms)
masks.append(predict)
d = []
for i, j in zip(masks, valid_masks[class_id::4]):
if (i.sum() == 0) & (j.sum() == 0):
d.append(1)
else:
d.append(dice(i, j))
attempts.append((t, ms, np.mean(d)))
attempts_df = pd.DataFrame(attempts, columns=['threshold', 'size', 'dice'])
attempts_df = attempts_df.sort_values('dice', ascending=False)
print(attempts_df.head())
best_threshold = attempts_df['threshold'].values[0]
best_size = attempts_df['size'].values[0]
class_params[class_id] = (best_threshold, best_size)
with open("something", 'wb') as handle:
pickle.dump(class_params, handle)