-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
280 lines (215 loc) · 9.33 KB
/
functions.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# coding: utf-8
import time
import cv2 #bgr order
import math
from itertools import compress,product
from typing import Tuple
from skimage.util import view_as_windows
# get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
plt.switch_backend('agg')
import numpy as np
import scipy.signal
import pandas as pd
import glob
import matplotlib.image as mpimg
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as dsets
from torchsummary import summary
from torch.autograd import Variable
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import random
import os
from os import listdir
from os.path import isfile, join
from os import walk
# from albumentations import (
# HorizontalFlip, VerticalFlip, CLAHE,
# ShiftScaleRotate, OpticalDistortion, GridDistortion, ElasticTransform, HueSaturationValue,
# RandomBrightnessContrast, IAAPiecewiseAffine,
# IAASharpen, IAAEmboss, Flip, OneOf, Compose, IAASuperpixels
# )
import warnings
warnings.filterwarnings("ignore")
class logMAEloss(nn.Module):
# https://www.wolframalpha.com/input/?i=-log%281-x%29
# -log(1-x)
def __init__(self):
super(logMAEloss,self).__init__()
def forward(self,inputs,targets, reduction='mean'):
# ‑log((‑x)+1)
mae = torch.abs(inputs - targets)
loss = -torch.log((-mae)+1.0)
if reduction=='mean':
loss = torch.mean(loss)
elif reduction == 'sum':
loss = torch.sum(loss)
return loss
def cross_entropy_loss_RCF(prediction, label):
label = label.long()
mask = label.float()
num_positive = torch.sum((mask==1).float()).float()
num_negative = torch.sum((mask==0).float()).float()
mask[mask == 1] = 1.0 * num_negative / (num_positive + num_negative)
mask[mask == 0] = 1.1 * num_positive / (num_positive + num_negative)
# mask[mask == 2] = 0
cost = torch.nn.functional.binary_cross_entropy(
prediction.float(),label.float(), weight=mask, reduce=False)
return torch.sum(cost)#torch.mean(cost)
def split_Image(bigImage,isMask,top_pad,bottom_pad,left_pad,right_pad,splitsize,stepsize,vertical_splits_number,horizontal_splits_number):
# print(bigImage.shape)
if isMask==True:
arr = np.pad(bigImage,((top_pad,bottom_pad),(left_pad,right_pad)),"reflect")
splits = view_as_windows(arr, (splitsize,splitsize),step=stepsize)
splits = splits.reshape((vertical_splits_number*horizontal_splits_number,splitsize,splitsize))
else:
arr = np.pad(bigImage,((top_pad,bottom_pad),(left_pad,right_pad),(0,0)),"reflect")
splits = view_as_windows(arr, (splitsize,splitsize,3),step=stepsize)
splits = splits.reshape((vertical_splits_number*horizontal_splits_number,splitsize,splitsize,3))
return splits # return list of arrays.
#idea from https://github.com/dovahcrow/patchify.py
def recover_Image(patches: np.ndarray, imsize: Tuple[int, int, int], left_pad,right_pad,top_pad,bottom_pad, overlapsize):
# patches = np.squeeze(patches)
assert len(patches.shape) == 5
i_h, i_w, i_chan = imsize
image = np.zeros((i_h+top_pad+bottom_pad, i_w+left_pad+right_pad, i_chan), dtype=patches.dtype)
divisor = np.zeros((i_h+top_pad+bottom_pad, i_w+left_pad+right_pad, i_chan), dtype=patches.dtype)
# print("i_h, i_w, i_chan",i_h, i_w, i_chan)
n_h, n_w, p_h, p_w,_= patches.shape
o_w = overlapsize
o_h = overlapsize
s_w = p_w - o_w
s_h = p_h - o_h
for i, j in product(range(n_h), range(n_w)):
patch = patches[i,j]
image[(i * s_h):(i * s_h) + p_h, (j * s_w):(j * s_w) + p_w] += patch
divisor[(i * s_h):(i * s_h) + p_h, (j * s_w):(j * s_w) + p_w] += 1
recover = image / divisor
return recover[top_pad:top_pad+i_h, left_pad:left_pad+i_w]
def recover_Image2(patches: np.ndarray, imsize: Tuple[int, int, int], left_pad,right_pad,top_pad,bottom_pad, overlapsize):
# patches = np.squeeze(patches)
assert len(patches.shape) == 5
i_h, i_w, i_chan = imsize
image = np.zeros((i_h+top_pad+bottom_pad, i_w+left_pad+right_pad, i_chan), dtype=patches.dtype)
divisor = np.zeros((i_h+top_pad+bottom_pad, i_w+left_pad+right_pad, i_chan), dtype=patches.dtype)
# print("i_h, i_w, i_chan",i_h, i_w, i_chan)
n_h, n_w, p_h, p_w,_= patches.shape
o_w = overlapsize
o_h = overlapsize
s_w = p_w - o_w
s_h = p_h - o_h
for i, j in product(range(n_h), range(n_w)):
patch = patches[i,j]
image[(i * s_h):(i * s_h) + p_h, (j * s_w):(j * s_w) + p_w] += patch
# divisor[(i * s_h):(i * s_h) + p_h, (j * s_w):(j * s_w) + p_w] += 1
recover = image / 4
return recover[top_pad:top_pad+i_h, left_pad:left_pad+i_w]
#https://github.com/Vooban/Smoothly-Blend-Image-Patches
def spline_window(window_size, power=2):
"""
Squared spline (power=2) window function:
https://www.wolframalpha.com/input/?i=y%3Dx**2,+y%3D-(x-2)**2+%2B2,+y%3D(x-4)**2,+from+y+%3D+0+to+2
"""
intersection = int(window_size/4)
wind_outer = (abs(2*(scipy.signal.triang(window_size))) ** power)/2
wind_outer[intersection:-intersection] = 0
wind_inner = 1 - (abs(2*(scipy.signal.triang(window_size) - 1)) ** power)/2
wind_inner[:intersection] = 0
wind_inner[-intersection:] = 0
wind = wind_inner + wind_outer
wind = wind / np.average(wind)
return wind
cached_2d_windows = dict()
def window_2D(window_size, power=2):
"""
Make a 1D window function, then infer and return a 2D window function.
Done with an augmentation, and self multiplication with its transpose.
Could be generalized to more dimensions.
"""
# Memoization
global cached_2d_windows
key = "{}_{}".format(window_size, power)
if key in cached_2d_windows:
wind = cached_2d_windows[key]
else:
wind = spline_window(window_size, power)
wind = np.expand_dims(np.expand_dims(wind, -1), -1)
wind = wind * wind.transpose(1, 0, 2)
cached_2d_windows[key] = wind
return wind
def crop(variable, th, tw): # this is for crop model layer or model outputs
h, w = variable.shape[2], variable.shape[3]
x1 = int(round((w - tw) / 2.))
y1 = int(round((h - th) / 2.))
return variable[:, :, y1 : y1 + th, x1 : x1 + tw]
def crop2(variable, th, tw): # this is for crop center when outputs are 96*96
h, w = variable.shape[-2], variable.shape[-1]
x1 = int(round((w - tw) / 2.))
y1 = int(round((h - th) / 2.))
return variable[:, :, y1 : y1 + th, x1 : x1 + tw]
# def strong_aug(p=1):
# return OneOf([
# ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=1),
# IAASharpen(p=1),
# IAAEmboss(p=1),
# RandomBrightnessContrast(p=1),
# HorizontalFlip(p=1),
# VerticalFlip(p=1),
# Compose([VerticalFlip(p=1), HorizontalFlip(p=1)]),
# ElasticTransform(p=1, alpha=400, sigma=400 * 0.05, alpha_affine=400 * 0.03),
# GridDistortion(p=1),
# OpticalDistortion(p=1)
# ], p=p)
#https://www.kaggle.com/iezepov/fast-iou-scoring-metric-in-pytorch-and-numpy
SMOOTH = 1e-6
def iou_pytorch(outputs: torch.Tensor, labels: torch.Tensor):
# You can comment out this line if you are passing tensors of equal shape
# But if you are passing output from UNet or something it will most probably
# be with the BATCH x 1 x H x W shape
outputs = outputs.squeeze(1) # BATCH x 1 x H x W => BATCH x H x W
intersection = (outputs & labels).float().sum((1, 2)) # Will be zero if Truth=0 or Prediction=0
union = (outputs | labels).float().sum((1, 2)) # Will be zzero if both are 0
# print("=============outputs.sum((1,2))labels.sum=============")
# print((outputs>0).sum((1,2)),(labels>0).sum((1,2)))
# print("==========================")
# print("intersection: ",intersection, "union: ",union)
iou = (intersection + SMOOTH) / (union + SMOOTH) # We smooth our devision to avoid 0/0
# print(iou)
# thresholded = torch.clamp(20 * (iou - 0.5), 0, 10).ceil() / 10 # This is equal to comparing with thresolds
# return thresholded # Or thresholded.mean() if you are interested in average across the batch
return iou
def iou_numpy(outputs: np.array, labels: np.array):
# outputs = outputs.squeeze(1)
intersection = (outputs & labels).sum((0,1))
union = (outputs | labels).sum((0,1))
iou = (intersection + SMOOTH) / (union + SMOOTH)
# thresholded = np.ceil(np.clip(20 * (iou - 0.5), 0, 10)) / 10
return iou # Or thresholded.mean()
# In[34]:
def acc_metrics(outputs, labels):
TP=0
TN=0
FP=0
FN=0
# TP predict 和 label 同时为1
TP += ((outputs == 1) & (labels == 1)).sum()
# TN predict 和 label 同时为0
TN += ((outputs == 0) & (labels == 0)).sum()
# FN predict 0 label 1
FN += ((outputs == 0) & (labels == 1)).sum()
# FP predict 1 label 0
FP += ((outputs == 1) & (labels == 0)).sum()
p = TP / (TP + FP)
r = TP / (TP + FN)
F1 = 2 * r * p / (r + p)
acc = (TP + TN) / (TP + TN + FP + FN)
return p,r,F1,acc
# ---------------------
# 作者:Link2Link
# 来源:CSDN
# 原文:https://blog.csdn.net/qq_15602569/article/details/79565402
# 版权声明:本文为博主原创文章,转载请附上博文链接!