-
Notifications
You must be signed in to change notification settings - Fork 0
/
DEM_features.py
96 lines (82 loc) · 3.08 KB
/
DEM_features.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
import torch.nn as nn
import torch
from torch.nn import functional as F
import numpy as np
import math
from DeviceSetting import device
class Slope(nn.Module):
def __init__(self):
super(Slope, self).__init__()
weight1 = np.zeros(shape=(3, 3), dtype=np.float32)
weight2 = np.zeros(shape=(3, 3), dtype=np.float32)
weight1[0][0] = -1
weight1[0][1] = 0
weight1[0][2] = 1
weight1[1][0] = -2
weight1[1][1] = 0
weight1[1][2] = 2
weight1[2][0] = -1
weight1[2][1] = 0
weight1[2][2] = 1
weight2[0][0] = -1
weight2[0][1] = -2
weight2[0][2] = -1
weight2[1][0] = 0
weight2[1][1] = 0
weight2[1][2] = 0
weight2[2][0] = 1
weight2[2][1] = 2
weight2[2][2] = 1
weight1 = np.reshape(weight1, (1, 1, 3, 3))
weight2 = np.reshape(weight2, (1, 1, 3, 3))
weight1 = weight1 / (8 * 30)
weight2 = weight2 / (8 * 30)
self.weight1 = nn.Parameter(torch.tensor(weight1)) # 自定义的权值
self.weight2 = nn.Parameter(torch.tensor(weight2))
self.bias = nn.Parameter(torch.zeros(1)) # 自定义的偏置
def forward(self, x):
dx = F.conv2d(x, self.weight1, self.bias, stride=1, padding=1)
dy = F.conv2d(x, self.weight2, self.bias, stride=1, padding=1)
ij_slope = torch.sqrt(torch.pow(dx, 2) + torch.pow(dy, 2))
ij_slope = torch.arctan(ij_slope) * 180 / math.pi
return ij_slope
class Aspect(nn.Module):
def __init__(self):
super(Aspect, self).__init__()
weight1 = np.zeros(shape=(3, 3), dtype=np.float32)
weight2 = np.zeros(shape=(3, 3), dtype=np.float32)
weight1[0][0] = -1
weight1[0][1] = 0
weight1[0][2] = 1
weight1[1][0] = -2
weight1[1][1] = 0
weight1[1][2] = 2
weight1[2][0] = -1
weight1[2][1] = 0
weight1[2][2] = 1
weight2[0][0] = -1
weight2[0][1] = -2
weight2[0][2] = -1
weight2[1][0] = 0
weight2[1][1] = 0
weight2[1][2] = 0
weight2[2][0] = 1
weight2[2][1] = 2
weight2[2][2] = 1
weight1 = np.reshape(weight1, (1, 1, 3, 3))
weight2 = np.reshape(weight2, (1, 1, 3, 3))
weight1 = weight1 / (8)
weight2 = weight2 / (8)
self.weight1 = nn.Parameter(torch.tensor(weight1)) # 自定义的权值
self.weight2 = nn.Parameter(torch.tensor(weight2))
self.bias = nn.Parameter(torch.zeros(1)) # 自定义的偏置
def forward(self, x):
dx = F.conv2d(x, self.weight1, self.bias, stride=1, padding=1)
dy = F.conv2d(x, self.weight2, self.bias, stride=1, padding=1)
batchsize, channel, row, col = x.shape
aspect = 57.29578 * torch.atan2(dy, -dx)
# a = np.array(aspect[1][0].cpu())
aspect = torch.where(aspect > 90, 360 - aspect + 90, 90 - aspect)
return aspect
Slope_net = Slope().to(device)
Aspect_net = Aspect().to(device)