-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredict_vector_intensity.py
More file actions
172 lines (139 loc) · 5.57 KB
/
predict_vector_intensity.py
File metadata and controls
172 lines (139 loc) · 5.57 KB
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
from efnet import *
import numpy as np
import torch
# import torchvision.transforms as transform
import pandas as pd
import json
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import PIL.Image as Image
import torch.backends.cudnn as cudnn
import torch.optim as optim
from tqdm import tqdm
import torchvision
import tifffile as tif
import matplotlib.pyplot as plt
transform_train = transforms.Compose([
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
class predict():
def __init__(self):
# Its epicenter pixel cordinates in your image.
self.cord=(3600,5760)
return
def setintensity(self,intensity):
self.intensity=tif.imread(intensity)
# plt.imshow(self.intensity),plt.show()
def netload(self,model_path='./checkpoint/ckpt2.pth'):
GPUID=0
os.environ['CUDA_VISIBLE_DEVICES']=str(GPUID)
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(self.device)
EFnet = EVIBuild()
EFnet = EFnet.to(self.device)
EFnet.eval()
if self.device == 'cuda':
EFnet = torch.nn.DataParallel(EFnet)
cudnn.benchmark = True
EFnet.load_state_dict(torch.load(model_path)['net'])
self.net=EFnet
# self.net.eval()
self.loaded=True
def default_loader(self,image):
# print(image.shape)
image = np.expand_dims(image, axis=2)
image = np.concatenate((image, image, image), axis=-1)
# print(image.shape)
image=image.astype(np.float32)
# mutil= mutil.astype(np.float32)
mutil = transform_train(image)
# print(mutil)
# exit(0)
return mutil
def despimage(self,tifpath,_size=32,rate=0.9,random=True):
image=tif.imread(tifpath)
self.maxheight=image.max()
# plt.imshow(image),plt.show()
W=image.shape[0]-_size
H=image.shape[1]-_size
print(image.shape)
result=np.zeros(image.shape,dtype=np.float32)
num=int(W*H/(_size**2*rate))
if not random:
for cx in tqdm(range(0,W,16)):
for cy in range(0,H,16):
small=image[cx:cx+_size,cy:cy+_size]
small=small/self.maxheight
input_=self.default_loader(small)
intensity=self.intensity[cx,cy]
xx=(cx-self.cord[0])
yy=(cy-self.cord[1])
distence=math.sqrt((xx)**2+(yy)**2)/(math.sqrt(2)*H)
vec=(intensity/10,xx/W,yy/W,distence)
# if i==0:
# print(input_,' || ',vec)
vec=torch.Tensor(vec)
vec=vec.float()
# print(input_.shape,' || ',vec.shape)
input_=input_.unsqueeze(0)
vec=vec.unsqueeze(0)
# print(input_.shape,' || ',vec.shape)
input_,vec=input_.to(self.device),vec.to(self.device)
input_assembled=(input_,vec)
# print('cord:,',x,y,image[x,y],self.maxheight,input_)
if self.loaded:
outputs=self.net(input_assembled)
_,predict=outputs.max(1)
# print(predict.cpu().numpy()[0])
# exit(0)
result[cx:cx+_size,cy:cy+_size]+=predict.cpu().numpy()[0]
if random:
for i in tqdm(range(num)):
x=np.random.randint(0,W)
y=np.random.randint(0,H)
small=image[x:x+_size,y:y+_size]
small=small/self.maxheight
# print(small)
# exit(0)
input_=self.default_loader(small)
intensity=self.intensity[x,y]
xx=(x-self.cord[0])
yy=(y-self.cord[1])
distence=math.sqrt((xx)**2+(yy)**2)/(math.sqrt(2)*H)
vec=(intensity/10,xx/W,yy/W,distence)
if i==0:
print(input_,' || ',vec)
vec=torch.Tensor(vec)
vec=vec.float()
# print(input_.shape,' || ',vec.shape)
input_=input_.unsqueeze(0)
vec=vec.unsqueeze(0)
# print(input_.shape,' || ',vec.shape)
input_,vec=input_.to(self.device),vec.to(self.device)
input_assembled=(input_,vec)
# print('cord:,',x,y,image[x,y],self.maxheight,input_)
if self.loaded:
outputs=self.net(input_assembled)
_,predict=outputs.max(1)
# print(predict)
result[x:x+_size,y:y+_size]+=predict.cpu().numpy()[0]
plt.imshow(result),plt.show()
# plt.imsave('result.png',result)
# patch=image
def main():
dempath='/home/winshare/paper/DEMUPDATE/data/LargeUpdate.tif'
intensity='/home/winshare/paper/DEMUPDATE/intensityupdate.tif'
predict_=predict()
predict_.netload(model_path='./checkpoint/ckpt2.pth')
predict_.setintensity(intensity)
predict_.despimage(dempath)
image=torch.randn((1,3,32,32))
vec=torch.randn((1,4))
print(image,'\n\n',vec)
a=(image.to(predict_.device),vec.to(predict_.device))
b=predict_.net(a)
print(b)
if __name__ == '__main__':
main()