-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotImagesOnGrid.py
80 lines (66 loc) · 2.28 KB
/
plotImagesOnGrid.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
import os
import numpy as np
import matplotlib
import matplotlib.cm as cm
import matplotlib.pyplot as plt
# Make all numpy available via shorter 'num' prefix
import numpy as np
# Make all matlib functions accessible at the top level via M.func()
import numpy.matlib as M
# Make some matlib functions accessible directly at the top level via, e.g. rand(3,3)
from numpy.matlib import rand, zeros, ones
def plotPaperPlot(orig, approx, activations, width, height):
h = plt.figure()
numRows = 3
numCols = 10
cnt = 1
def f(_img, _h, _numRows, _numCols, _cnt):
_h.add_subplot(_numRows, _numCols, _cnt)
plt.imshow(_img, cmap='gray')
for i in range(len(orig)):
f(orig[i].reshape(width, height), h, numRows, numCols, cnt)
cnt += 1
for i in range(len(approx)):
f(approx[i].reshape(width, height), h, numRows, numCols, cnt)
cnt += 1
for i in range(len(activations)):
act = activations[i]
h.add_subplot(numRows, numCols, cnt)
plt.hist(act)
cnt += 1
plt.show()
def plotImagesOnGrid(X, numRows, numCols, width, height, samples, fileName):
# PLOTIMAGESONGRID plot a set of images on a grid
# plotImagesGrid(X, numRows, numCols, width, height, samples)
# create a figure
h = plt.figure()
# begin drawing along grid
cnt = 1
img = []
bgc = 1
for i in range(0, numRows):
for j in range(0, numCols):
# determine if there is something to draw and if so prepare it
empty = False
if cnt > len(samples):
img = bgc * ones((height, width))
empty = True
else:
img = X[samples[cnt-1], :]
# normalize image (still as row)
minImg = img.min()
maxImg = img.max()
if minImg == maxImg:
img = zeros(img.shape)
else:
img = (img - minImg)/(maxImg - minImg)
# get the image
img = img.reshape(height, width)
# draw cell content
h.add_subplot(numRows, numCols, cnt)
plt.imshow(img, cmap='gray')
cnt = cnt + 1
if not os.path.exists('./fig'):
os.mkdir('fig')
h.savefig(fileName)
plt.show()