forked from OUPL/MLCert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoise.v
More file actions
65 lines (47 loc) · 2.01 KB
/
noise.v
File metadata and controls
65 lines (47 loc) · 2.01 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
Set Implicit Arguments.
Unset Strict Implicit.
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp Require Import all_ssreflect.
Require Import List. Import ListNotations.
Require Import QArith Reals Rpower Ranalysis Fourier.
Require Import OUVerT.chernoff OUVerT.learning OUVerT.bigops OUVerT.dist OUVerT.numerics OUVerT.dyadic.
Require Import MLCert.monads MLCert.samplers.
Section image_noise.
Variables X Y : Type.
Variables (T:Type) (T_min T_max : T).
Variable example_mapM : forall M:Type->Type, (T -> M T) -> X -> M X.
Context {training_set} `{Foldable training_set (X*Y)}.
Context {sampler_state} `{BasicSamplers sampler_state}.
Variable r:Type.
Definition NoiseM T := StateT (M:=@Cont r) sampler_state T.
Section noise.
Variable noise_index : T -> NoiseM T.
Definition noise_example (xy:X*Y) : NoiseM (X*Y) :=
let: (x,y) := xy in
x' <-- example_mapM noise_index x;
ret (ret (x',y)).
Definition noise (t:training_set) : NoiseM training_set :=
foldable_mapM noise_example t.
End noise.
(*A "salt-and-pepper" image noise model, with probability p corrupting index i to
either x_min or x_max (each with probability p*1/2)*)
Definition salt_and_pepper_index (p:D) (t:T) : NoiseM T :=
corrupt <-- bernoulli_sampler p;
if corrupt then
flip <-- bernoulli_sampler (Dmake 1 1);
ret (if flip then T_min else T_max)
else ret t.
Definition salt_and_pepper (p:D) (t:training_set) : NoiseM training_set :=
noise (salt_and_pepper_index p) t.
(*A random-valued impulse noise model, with probability p corrupting index i to
a number in the range [0,255], chosen uniformly.*)
Variable T_of_nat : nat -> T.
Definition rv_impulse_index (p:D) (t:T) : NoiseM T :=
corrupt <-- bernoulli_sampler p;
if corrupt then
i <-- uniform_sampler (m:=256);
ret (T_of_nat i)
else ret t.
Definition rv_impulse (p:D) (t:training_set) : NoiseM training_set :=
noise (rv_impulse_index p) t.
End image_noise.