-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.py
135 lines (84 loc) · 3.45 KB
/
infer.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
import ttach as tta
import os
from skimage import io
import glob
import torch
import segmentation_models_pytorch as smp
import torch
import torch.nn as nn
import numpy as np
from tqdm import tqdm
from PIL import Image, ImageFilter, ImageOps
from SEUNet import Unet
model = Unet(12,2).cuda()
def ds1(arr):
arr1 = np.zeros((16,16))
for i in range(16):
for j in range(16):
patch = arr[i*50:(i+1)*50,j*50:(j+1)*50]
if np.mean(patch)>0:
arr1[i,j] = 1
return arr1
def test(val_path,result_path):
os.makedirs(result_path,exist_ok=True)
tmp_list = []
tiles = []
for parent,tile,_ in os.walk(val_path):
tiles = [os.path.join(val_path,p) for p in tile]
break
for tile in tqdm(tiles):
sar_path = [os.path.join(tile,p) for p in glob.glob(os.path.join(tile,"S1A*.tif"))]
s2_path = [os.path.join(tile,p) for p in glob.glob(os.path.join(tile,"L2A*.tif"))]
lc8_path = [os.path.join(tile,p) for p in glob.glob(os.path.join(tile,"LC08*.tif"))]
viirs_path = [os.path.join(tile,p) for p in glob.glob(os.path.join(tile,"DNB*.tif"))]
tmp_list.append(s2_path + lc8_path+sar_path+viirs_path)
checkpoint = torch.load("seunet12-checkpoint-best.pth")
model.load_state_dict(checkpoint['state_dict'])
model.eval()
tta_model = tta.SegmentationTTAWrapper(model, tta.aliases.d4_transform(), merge_mode='mean')
for maskpath in tmp_list:
image = [io.imread(i) for i in maskpath]
image = np.array(image,'float')
image[0:80] = image[0:80]/10000.0
image[89:98] = image[89:98]/10.0
image=image.transpose(1, 2, 0)
viirs = image[:,:,94]
image = np.concatenate((image[:,:,12:24],viirs[:,:,np.newaxis]),axis = 2)
image_padding = np.zeros((1152,1152,13))
for i in range(13):
band = image[:,:,i]
band = np.pad(band,((0,224),(0,224)),'edge')
band = np.pad(band,((64,64),(64,64)),'edge')
image_padding[:,:,i] = band
image = image_padding.transpose(2, 0, 1)
labels = np.zeros((64,256,256))
label = np.zeros((1024,1024))
label_final = np.zeros((800,800))
# label =
for i in range(8):
for j in range(8):
x_patch = image[:,i*128:(i+2)*128,j*128:(j+2)*128]
inputs = torch.from_numpy(x_patch).unsqueeze(0).float()
inputs = inputs.cuda()
v = inputs[:,12,:,:]
s2 = inputs[:,0:12,:,:]
# pred = model(v[:,np.newaxis,:,:],s2)
with torch.no_grad():
output = tta_model(v[:,np.newaxis,:,:],s2)
pred = output.squeeze().cpu().data.numpy()
pred = np.argmax(pred,axis=0)
pred_map = pred.astype("uint8")
labels[i*8+j,:,:] = pred_map
for i in range(8):
for j in range(8):
label[i*128:(i+1)*128,j*128:(j+1)*128] = labels[i*8+j,64:192,64:192]
label_final = label[0:800,0:800]
label_final1 = ds1(label_final)
im = Image.fromarray(label_final1)
path1,_ = os.path.split(maskpath[0])
_,path2 = os.path.split(path1)
save_file_name = path2[4:]+".tif"
save_path = os.path.join(result_path,save_file_name)
im.save(save_path)
print(save_path)
test('/Test/','/result1')