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
use tch::{Kind, Tensor, index::*};
const GLCM_BINCOUNT_SIZE: i64 = 0x00FF_FFFF;
pub fn glcm(image: &Tensor, offset: (i64, i64), num_shades: u8, mask: Option<&Tensor>, symmetric: bool) -> Tensor {
if image.device().is_cuda(){
glcm_gpu(image, offset, num_shades, mask, symmetric)
} else {
glcm_cpu(image, offset, num_shades, mask, symmetric)
}
}
pub fn glcm_gpu(image: &Tensor, offset: (i64, i64), num_shades: u8, mask: Option<&Tensor>, symmetric: bool) -> Tensor{
let (offset_y, offset_x) = offset;
let (offset_y, offset_x) = (offset_y as i64, offset_x as i64);
let (batch_size, _, height, width) = image.size4().unwrap();
let mut image = image * (num_shades as f64 - 1e-6);
if let Some(mask) = mask {
let mask = (mask - 1.0) * -(num_shades as f64);
image += mask;
}
let image = image.floor().clamp(0.0, 255.0).to_kind(Kind::Uint8);
let rslice = (
..,
..,
(-offset_y).max(0)..(height-offset_y).min(height),
(-offset_x).max(0)..(width-offset_x).min(width),
);
let nslice = (
..,
..,
offset_y.max(0)..(height+offset_y).min(height),
offset_x.max(0)..(width+offset_x).min(width),
);
let ref_img = image.i(rslice.clone()).to_kind(Kind::Int64);
let neigh_img = image.i(nslice.clone()).to_kind(Kind::Int64);
let num_shades = (num_shades + 1) as i64; let group_size = ((GLCM_BINCOUNT_SIZE-1) / num_shades.pow(2)).min(batch_size);
println!("group_size: {}", group_size);
let group_count = batch_size / group_size + (batch_size % group_size != 0) as i64;
println!("group_count: {}", group_count);
let batch_idx = Tensor::arange(batch_size as i64, (Kind::Int64, image.device())).remainder(group_size);
let batch_idx = batch_idx.view([-1, 1, 1, 1]);
let pairs = batch_idx * num_shades.pow(2) + ref_img * num_shades as i64 + neigh_img;
let glcms = {
let pairs = pairs.tensor_split(group_count, 0);
let bincount_size = num_shades.pow(2) * group_size;
pairs.iter()
.map(|t| t.view([-1]))
.map(|t| t.bincount::<&Tensor>(None, bincount_size))
.map(|t| t.view([-1, num_shades as i64, num_shades as i64]))
.collect::<Vec<_>>()
};
let mut glcm = Tensor::cat(&glcms[..], 0).i((.., ..num_shades-1, ..num_shades-1));
if symmetric {
glcm+=glcm.copy().transpose(-1, -2);
}
let len = glcm.sum_dim_intlist(Some(&[1, 2][..]), false, Kind::Float);
let len = len.view([-1, 1, 1]);
&glcm / len
}
pub fn glcm_cpu(image: &Tensor, offset: (i64, i64), num_shades: u8, mask: Option<&Tensor>, symmetric: bool) -> Tensor{
let (offset_y, offset_x) = offset;
let (batch_size, _, height, width) = image.size4().unwrap();
let (batch_size, height, width) = (batch_size as usize, height as usize, width as usize);
let device = image.device();
let mut image = image * num_shades as f64;
if let Some(mask) = mask {
let mask = (mask - 1.0) * -255.0;
image += mask;
}
let image = image.clamp(0.0, 255.0).to_kind(Kind::Uint8);
let batch_span = height * width;
let image = Vec::<u8>::from(&image);
let it =((-offset_y).max(0)..(height as i64-offset_y).min(height as i64))
.flat_map(|y| ((-offset_x).max(0)..(width as i64-offset_x).min(width as i64))
.map(move|x| (y as usize, x as usize))
);
let glcm = (0..batch_size)
.map(|batch|{
let mut glcm = vec![0; num_shades as usize * num_shades as usize];
for (y, x) in it.clone(){
let reference_shade = image[batch * batch_span + y * width + x] as usize;
let neighbor_shade = image[batch * batch_span + (y as i64 + offset_y) as usize * width + (x as i64 + offset_x) as usize] as usize;
if reference_shade >= num_shades as usize || neighbor_shade >= num_shades as usize{
continue;
}
glcm[reference_shade * num_shades as usize + neighbor_shade] += 1;
}
Tensor::of_slice(&glcm).view([num_shades as i64, num_shades as i64])
});
let mut glcm = Tensor::stack(&glcm.collect::<Vec<_>>()[..], 0).to_device(device);
if symmetric {
glcm+=glcm.copy().transpose(-1, -2);
}
let len = glcm.sum_dim_intlist(Some(&[1, 2][..]), false, Kind::Float);
let len = len.view([-1, 1, 1]);
glcm / len
}
#[cfg(test)]
mod test {
use tch::{Tensor, Kind, index::*};
use crate::utils::assert_eq_tensor;
#[test]
fn bidouillage(){
}
#[test]
fn test_glcm_no_mask(){
let input = Tensor::of_slice(&[
1.0, 1.0, 2.0, 1.0,
2.0, 1.0, 1.0, 1.0,
3.0, 2.0, 3.0, 1.0,
3.0, 2.0, 1.0, 2.0,
1.0, 1.0, 2.0, 1.0,
2.0, 1.0, 1.0, 1.0,
3.0, 2.0, 3.0, 1.0,
3.0, 2.0, 1.0, 2.0
]);
let expected = Tensor::of_slice(&[
0.25, 0.17, 0.0,
0.25, 0.0, 0.08,
0.08, 0.17, 0.0,
0.25, 0.17, 0.0,
0.25, 0.0, 0.08,
0.08, 0.17, 0.0,
]).view((2, 3, 3));
let input = (input.view((2, 1, 4, 4))-1.0) / 3.0;
let rand = Tensor::rand(&[2, 1, 4, 4], (Kind::Float, tch::Device::Cpu));
let input = Tensor::cat(&[input, rand], 0);
let glcm = super::glcm(&input, (1, 0), 3, None, false);
let glcm_gpu = super::glcm_gpu(&input, (1, 0), 3, None, false);
let glcm_cpu = super::glcm_cpu(&input, (1, 0), 3, None, false);
let glcm = glcm.i(..2);
let glcm_gpu = glcm_gpu.i(..2);
let glcm_cpu = glcm_cpu.i(..2);
assert_eq_tensor(&glcm, &expected);
assert_eq_tensor(&glcm_gpu, &expected);
assert_eq_tensor(&glcm_cpu, &expected);
}
#[test]
fn test_glcm_mask(){
let input = Tensor::of_slice(&[
1.0, 1.0, 2.0, 1.0,
2.0, 1.0, 1.0, 1.0,
3.0, 2.0, 3.0, 1.0,
3.0, 2.0, 1.0, 2.0
]);
let input = (input.view((1, 1, 4, 4))-1.0) / 3.0;
let mask = Tensor::of_slice(&[
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 0.0
]).view((1, 1, 4, 4));
let expected = Tensor::of_slice(&[
3.0, 1.0, 0.0,
3.0, 0.0, 1.0,
1.0, 2.0, 0.0,
]).view((1, 3, 3)) / 11.0;
let glcm = super::glcm(&input, (1, 0), 3, Some(&mask), false);
assert_eq_tensor(&glcm, &expected);
}
}