-
Notifications
You must be signed in to change notification settings - Fork 3
/
ising.py
182 lines (140 loc) · 4.55 KB
/
ising.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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 23 14:14:32 2016
@author: hughsalimbeni
Based on code written by Matthew Lee: https://github.com/mauinz
"""
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from skimage.filters import threshold_adaptive
IMAGE = 'penguin.jpg'
block_size = 40
image = np.array(Image.open(IMAGE).convert('L'))
image = threshold_adaptive(image, block_size, offset=10)
image_scaled = (image * 2) - 1
#plt.gray()
#plt.imshow(image_scaled)
#plt.colorbar()
#plt.show()
noise = 1*np.random.randn(image.shape[0]*image.shape[1]).reshape(image.shape)
noise = 2*np.random.binomial(1, 0.7, image.shape[0]*image.shape[1]).reshape(image.shape)-1.
noisey_image = (image_scaled*noise)
#noisey_image = (image_scaled + noise)
#plt.gray()
#plt.imshow(noisey_image)
#plt.colorbar()
#plt.show()
#plt.bar
# Helper functions for the updates
def fast_convolve(matrix, weights):
h, w = matrix.shape
conv = np.zeros((h, w))
matrix = np.pad(matrix,((1, 1), (1, 1)),mode='constant')
for i in range(h):
for j in range(w):
conv[i, j] = (matrix[i:i + 3,j:j + 3] * weights).sum()
return conv
def fast_convolve_double(matrix, weights):
h, w = matrix.shape
conv = np.zeros((h, w))
weights2 = weights**2
matrix = np.pad(matrix,((1, 1), (1, 1)),mode='constant')
for i in range(h):
for j in range(w):
conv[i, j] = (matrix[i:i + 3,j:j + 3] * weights2).sum()
return conv
def prob(img, mu, sigma):
# # this is an optimisation
# res = np.exp(-((img - mu)**2 / (2 * sigma**2)))
# res = res / (sigma * np.sqrt(2 * np.pi))
# return res
return sigma*img*mu
def logprob(img, sigma):
log_plus = prob(img, 1, sigma)
log_minus = prob(img, -1, sigma)
return log_plus - log_minus
#def LB(img, mu, sigma):
# L_plus = prob(img, 1, sigma)
# L_minus = prob(img, -1, sigma)
# a = fast_convolve(mu, weights)
# b = fast_convolve_double(mu, weights)
# lb = np.sum(b) + np.sum(a) + np.sum(0.5*(L_plus - L_minus))
# return np.log(lb)
def update(mu, weights, L):
weighted_mu = fast_convolve(mu, weights)
new_mu = np.tanh(weighted_mu + 0.5 * L)
return new_mu
# Set model paramters and run updates
steps = np.arange(10)
mu = noisey_image
weights = np.ones((3, 3))
weights[1,1] = 0
decay = 1
L_1 = logprob(noisey_image, 0.0001)
L_2 = logprob(noisey_image, 0.1)
L_3 = logprob(noisey_image, 1.0)
L_4 = logprob(noisey_image, 3.)
#fig = plt.figure(figsize=(12, 8), facecolor='white')
fig, ((ax_1, ax_2, ax_3), (ax_4, ax_5, ax_6)) = plt.subplots(2, 3)
#plt.show(block=False)
def draw(mu_1, mu_2, mu_3, mu_4):
for ax in (ax_1, ax_2, ax_3, ax_4, ax_5, ax_6):
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
plt.gray()
ax_1.set_title('Original')
ax_1.imshow(image_scaled)
ax_4.set_title('Corrupted')
ax_4.imshow(noisey_image)
ax_2.set_title('Sigma = 0.0001')
ax_2.imshow(mu_1)
ax_3.set_title('Sigma = 0.1')
ax_3.imshow(mu_2)
ax_5.set_title('Sigma = 1.0')
ax_5.imshow(mu_3)
ax_6.set_title('Sigma = 3.0')
ax_6.imshow(mu_4)
mu_1 = np.zeros_like(noisey_image.copy())
mu_2 = np.zeros_like(noisey_image.copy())
mu_3 = np.zeros_like(noisey_image.copy())
mu_4 = np.zeros_like(noisey_image.copy())
for i in steps:
print i
mu_1 = (1 - decay)*mu_1 + (decay * update(mu_1, weights, L_1))
mu_2 = (1 - decay)*mu_2 + (decay * update(mu_2, weights, L_2))
mu_3 = (1 - decay)*mu_3 + (decay * update(mu_3, weights, L_3))
mu_4 = (1 - decay)*mu_4 + (decay * update(mu_4, weights, L_4))
draw(mu_1, mu_2, mu_3, mu_4)
plt.savefig('ising.pdf')
plt.show()
#def draw(mu, objective):
# for ax in (ax_1, ax_2, ax_3):
# ax.axes.get_xaxis().set_visible(False)
# ax.axes.get_yaxis().set_visible(False)
# plt.gray()
# ax_1.set_title('Original')
# ax_1.imshow(image_scaled)
# ax_2.set_title('Corrupted')
# ax_2.imshow(noisey_image)
# ax_3.set_title('Posterior')
#
# ax_3.imshow(mu)
#
# ax_4.set_title('L')
# ax_4.scatter(steps[:len(objective)], objective, color='b')
# ax_4.set_xlim([0, len(steps)])
# ax_4.set_xlabel('Number iterations')
# ax_4.set_ylabel('Lower bound log ML')
# plt.draw()
# plt.pause(0.1)
#objective = []
#for i in steps:
# mu = (1 - decay)*mu + (decay * update(mu, weights, L))
# lb = LB(noisey_image, mu, sigma)
# objective.append(lb)
# draw(mu, objective)
# plt.gray()
# plt.imshow(image_scaled)
# plt.colorbar()
# plt.show()