How to improve the precessing speed of tensorflow sess.run() - session

I'm trying to implement Recurrent Lest-Squares filter (RLS) implemented by tensorflow. Use about 300,000 data to train.It costs about 3min. is there any way to reduce the time consumption.
def rls_filter_graphic(x, d, length, Pint, Wint): # Projection matrix
P = tf.Variable(Pint, name='P')
# Filter weight
w = tf.Variable(Wint, name='W')
# Get output of filter
y = tf.matmul(x, w)
# y mast round,if(y>0) y=int(y) else y=int(y+0.5)
y = tf.round(y)
# get expect error which means expect value substract out put value
e = tf.subtract(d, y)
# get gain it is equal as k=P(n-1)x(n)/[lamda+x(n)P(n-1)x(n-1)]
tx = tf.transpose(x)
Px = tf.matmul(P, tx)
xPx = tf.matmul(x, Px)
xPx = tf.add(xPx, LAMDA)
k = tf.div(Px, xPx)
# update w(n) with k(n) as w(n)=w(n-1)+k(n)*err
wn = tf.add(w, tf.matmul(k, e))
# update P(n)=[P(n-1)-K(n)x(n)P(n-1)]/lamda
xP = tf.matmul(x, P)
kxP = tf.matmul(k, xP)
Pn = tf.subtract(P, kxP)
Pn = tf.divide(Pn, LAMDA)
update_P = tf.assign(P, Pn)
update_W = tf.assign(w, wn)
return y, e, w, k, P, update_P, update_W
And this will called in this func
def inter_band_predict(dataset, length, width, height):
img = np.zeros(width * height)
# use iterator get the vectors
itr = dataset.make_one_shot_iterator()
(x, d) = itr.get_next()
# build the predict graphics
ini_P, ini_W, = rls_filter_init(0.001, length)
y_p, err, weight, kn, Pn, update_P, update_W = rls_filter_graphic(x, d, length, ini_P, ini_W)
# err=tf.matmul(x,ini_W)
# init value
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for i in range(height*width):
[e, trainP, trainW] = sess.run([err, update_P, update_W])
img[i] = e
return img
I found that call sess.run() per every loop is so costly.Is there any way to avoid this.
by the way,the dataset is the form as this
(x, d)
(
[0.1,1.0],[1.0]
[0.0,2.0],[2.0]
[0.2,3.0],[1.0]
……
)
    

Related

MVGC F value and P value calculation

I am currently working to find Multivariate Granger Causality F value and p value via this code.
def demean(x, axis=0):
"Return x minus its mean along the specified axis"
x = np.asarray(x)
if axis == 0 or axis is None or x.ndim <= 1:
return x - x.mean(axis)
ind = [slice(None)] * x.ndim
ind[axis] = np.newaxis
return x - x.mean(axis)[ind]
#------------------------------
def tsdata_to_autocov(X, q):
import numpy as np
from matplotlib import pylab
if len(X.shape) == 2:
X = np.expand_dims(X, axis=2)
[n, m, N] = np.shape(X)
else:
[n, m, N] = np.shape(X)
X = demean(X, axis=1)
G = np.zeros((n, n, (q+1)))
for k in range(q+1):
M = N * (m-k)
G[:,:,k] = np.dot(np.reshape(X[:,k:m,:], (n, M)), np.reshape(X[:,0:m-k,:], (n, M)).conj().T) / M-1
return G
#-------------------------
def autocov_to_mvgc(G, x, y):
import numpy as np
from mvgc import autocov_to_var
n = G.shape[0]
z = np.arange(n)
z = np.delete(z,[np.array(np.hstack((x, y)))])
# indices of other variables (to condition out)
xz = np.array(np.hstack((x, z)))
xzy = np.array(np.hstack((xz, y)))
F = 0
# full regression
ixgrid1 = np.ix_(xzy,xzy)
[AF,SIG] = autocov_to_var(G[ixgrid1])
# reduced regression
ixgrid2 = np.ix_(xz,xz)
[AF,SIGR] = autocov_to_var(G[ixgrid2])
ixgrid3 = np.ix_(x,x)
F = np.log(np.linalg.det(SIGR[ixgrid3]))-np.log(np.linalg.det(SIG[ixgrid3]))
return F
Can anyone show me an example for how they got to solving for F and p?
It would also help a lot to see what your timeseries data looks like.

Emotion detection using facial landmarks

I plan on using scikit svm for class prediction.
I have been trying this :
Get images from a webcam
Detect Facial Landmarks
Train a machine learning algorithm (we will use a linear SVM)
Predict emotions
I have a problem in this line : clf.fit(npar_train, training_labels)
also I have a problem in site-packages\sklearn\svm_base.py and in site-packages\sklearn\utils\validation.py
How can I remove this error?
thank you in advance
python script
emotions = ['neutral', 'sad', 'happy', 'anger']
data={}
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
clf = SVC(kernel='linear', probability=True, tol=1e-3)
def get_files(emotion):
files = glob.glob('img\\datasets\\%s\\*' %emotion)
random.shuffle(files)
training = files[:int(len(files)*0.8)]
prediction = files[-int(len(files)*0.2)]
return training, prediction
def get_landmarks(image):
detections = detector(image, 1)
for k, d in enumerate(detections): # For all detected face instances individually
shape = predictor(image, d) # Draw Facial Landmarks with the predictor class
xlist = []
ylist = []
for i in range(1, 68): # Store X and Y coordinates in two lists
xlist.append(float(shape.part(i).x))
ylist.append(float(shape.part(i).y))
xmean = np.mean(xlist)
ymean = np.mean(ylist)
xcentral = [(x - xmean) for x in xlist]
ycentral = [(y - ymean) for y in ylist]
landmarks_vectorised = []
for x, y, w, z in zip(xcentral, ycentral, xlist, ylist):
landmarks_vectorised.append(w)
landmarks_vectorised.append(z)
meannp = np.asarray((ymean, xmean))
coornp = np.asarray((z, w))
dist = np.linalg.norm(coornp - meannp)
landmarks_vectorised.append(dist)
landmarks_vectorised.append((math.atan2(y, x) * 360) / (2 * math.pi))
data['landmarks_vectorised'] = landmarks_vectorised
if len(detections) < 1:
data['landmarks_vestorised'] = "error"
def make_sets():
training_data = []
training_labels = []
prediction_data = []
prediction_labels = []
for emotion in emotions:
print("Working on %s emotion" %emotion)
training, prediction = get_files(emotion)
for item in training:
image = cv2.imread(item)
try:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
except:
print()
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
clahe_image = clahe.apply(image)
get_landmarks(clahe_image)
if data['landmarks_vectorised'] == "error":
print("no face detected on this one")
else:
training_data.append(data['landmarks_vectorised']) # append image array to training data list
training_labels.append(emotions.index(emotion))
for item in prediction:
image = cv2.imread(item)
try:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
except:
print()
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
clahe_image = clahe.apply(image)
get_landmarks(clahe_image)
if data['landmarks_vectorised'] == "error":
print("no face detected on this one")
else:
prediction_data.append(data['landmarks_vectorised'])
prediction_labels.append(emotions.index(emotion))
return training_data, training_labels, prediction_data, prediction_labels
accur_lin = []
for i in range(0,10):
print("Making sets %s" % i) # Make sets by random sampling 80/20%
training_data, training_labels, prediction_data, prediction_labels = make_sets()
npar_train = np.array(training_data)
npar_trainlabs = np.array(training_labels)
print("training SVM linear %s" % i) # train SVM
clf.fit(npar_train, training_labels)
print("getting accuracies %s" % i)
npar_pred = np.array(prediction_data)
pred_lin = clf.score(npar_pred, prediction_labels)
print("Mean value lin svm: %s" % np.mean(accur_lin))

How do I get HSV values of an average pixel of an image?

In this code
im = Vips::Image.new_from_file "some.jpg"
r = (im * [1,0,0]).avg
g = (im * [0,1,0]).avg
b = (im * [0,0,1]).avg
p [r,g,b] # => [57.1024, 53.818933333333334, 51.9258]
p Vips::Image.sRGB2HSV [r,g,b]
the last line throws
/ruby-vips-1.0.3/lib/vips/argument.rb:154:in `set_property': invalid argument Array (expect #<Class:0x007fbd7c923600>) (ArgumentError)`
P.S.: temporary took and refactored the ChunkyPNG implementation:
def to_hsv r, g, b
r, g, b = [r, g, b].map{ |component| component.fdiv 255 }
min, max = [r, g, b].minmax
chroma = max - min
[
60.0 * ( chroma.zero? ? 0 : case max
when r ; (g - b) / chroma
when g ; (b - r) / chroma + 2
when b ; (r - g) / chroma + 4
else 0
end % 6 ),
chroma / max,
max,
]
end
Pixel averaging should really be in a linear colorspace. XYZ is an easy one, but scRGB would work well too. Once you have a 1x1 pixel image, convert to HSV and read out the value.
#!/usr/bin/ruby
require 'vips'
im = Vips::Image.new_from_file ARGV[0]
# xyz colourspace is linear, ie. the value is each channel is proportional to
# the number of photons of that frequency
im = im.colourspace "xyz"
# 'shrink' is a fast box filter, so each output pixel is the simple average of
# the corresponding input pixels ... this will shrink the whole image to a
# single pixel
im = im.shrink im.width, im.height
# now convert the one pixel image to hsv and read out the values
im = im.colourspace "hsv"
h, s, v = im.getpoint 0, 0
puts "h = #{h}"
puts "s = #{s}"
puts "v = #{v}"
I wouldn't use HSV myself, LCh is generally much better.
https://en.wikipedia.org/wiki/Lab_color_space#Cylindrical_representation:_CIELCh_or_CIEHLC
For LCh, just change the end to:
im = im.colourspace "lch"
l, c, h = im.getpoint 0, 0
I realised, that it is obviously wrong to calculate average Hue as arithmetic average, so I solved it by adding vectors of length equal to Saturation. But I didn't find how to iterate over pixels in vips so I used a crutch of chunky_png:
require "vips"
require "chunky_png"
def get_average_hsv_by_filename filename
im = Vips::Image.new filename
im.write_to_file "temp.png"
y, x = 0, 0
ChunkyPNG::Canvas.from_file("temp.png").to_rgba_stream.unpack("N*").each do |rgba|
h, s, v = ChunkyPNG::Color.to_hsv(rgba)
a = h * Math::PI / 180
y += Math::sin(a) * s
x += Math::cos(a) * s
end
h = Math::atan2(y, x) / Math::PI * 180
_, s, v = im.colourspace("hsv").bandsplit.map(&:avg)
[h, s, v]
end
For large images I used .resize that seems to inflict only up to ~2% error when resizing down to 10000 square pixels area with default kernel.

MatLab code speed and optimization. How to improve?

Could someone please run this for me and tell me how long it takes for you? It took my laptop 60s. I can't tell if it's my laptop that's crappy or my code. Probably both.
I just started learning MatLab, so I'm not yet familiar with which functions are better than others for specific tasks. If you have any suggestions on how I could improve this code, it would be greatly appreciated.
function gbp
clear; clc;
zi = 0; % initial position
zf = 100; % final position
Ei = 1; % initial electric field
c = 3*10^8; % speed of light
epsilon = 8.86*10^-12; % permittivity of free space
lambda = 1064*10^-9; % wavelength
k = 2*pi/lambda; % wave number
wi = 1.78*10^-3; % initial waist width (minimum spot size)
zr = (pi*wi^2)/lambda; % Rayleigh range
Ri = zi + zr^2/zi; % initial radius of curvature
qi = 1/(1/Ri-1i*lambda/(pi*wi^2)); % initial complex beam parameter
Psii = atan(real(qi)/imag(qi)); % Gouy phase
mat = [1 zf; 0 1]; % transformation matrix
A = mat(1,1); B = mat(1,2); C = mat(2,1); D = mat(2,2);
qf = (A*qi + B)/(C*qi + D); % final complex beam parameter
wf = sqrt(-lambda/pi*(1/imag(1/qf))); % final spot size
Rf = 1/real(1/qf); % final radius of curvature
Psif = atan(real(qf)/imag(qf)); % final Gouy phase
% Hermite - Gaussian modes function
u = #(z, x, n, w, R, Psi) (2/pi)^(1/4)*sqrt(exp(1i*(2*n+1)*Psi)/(2^n*factorial(n)*w))*...
hermiteH(n,sqrt(2)*x/w).*exp(-x.^2*(1/w^2+1i*k/(2*R))-1i*k*z);
% Complex amplitude coefficients function
a = #(n) exp(1i*k*zi)*integral(#(x) Ei.*conj(u(zi, x, n, wi, Ri, Psii)),-2*wi,2*wi);
%----------------------------------------------------------------------------
xlisti = -0.1:1/10000:0.1; % initial x-axis range
xlistf = -0.1:1/10000:0.1; % final x-axis range
nlist = 0:2:20; % modes range
function Eiplot
Efieldi = zeros(size(xlisti));
for nr = nlist
Efieldi = Efieldi + a(nr).*u(zi, xlisti, nr, wi, Ri, Psii)*exp(-1i*k*zi);
end
Ii = 1/2*c*epsilon*arrayfun(#(x)x.*conj(x),Efieldi);
end
function Efplot
Efieldf = zeros(size(xlistf));
for nr = nlist
Efieldf = Efieldf + a(nr).*u(zf, xlistf, nr, wf, Rf, Psif)*exp(-1i*k*zf);
end
If = 1/2*c*epsilon*arrayfun(#(x)x.*conj(x),Efieldf);
end
Eiplot
Efplot
plot(xlisti,real(Ii),xlistf,real(If))
xlabel('x(m)') % x-axis label
ylabel('I(W/m^2)') % y-axis label
end
The cost is coming from the calls to hermiteH -- for every call, this creates a new function using symbolic variables, then evaluates the function at your input. The key to speeding this up is to pre-compute the hermite polynomial functions then evaluate those rather than create them from scratch each time (speedup from ~26 seconds to around 0.75 secs on my computer).
With the changes:
function gbp
x = sym('x');
zi = 0; % initial position
zf = 100; % final position
Ei = 1; % initial electric field
c = 3*10^8; % speed of light
epsilon = 8.86*10^-12; % permittivity of free space
lambda = 1064*10^-9; % wavelength
k = 2*pi/lambda; % wave number
wi = 1.78*10^-3; % initial waist width (minimum spot size)
zr = (pi*wi^2)/lambda; % Rayleigh range
Ri = zi + zr^2/zi; % initial radius of curvature
qi = 1/(1/Ri-1i*lambda/(pi*wi^2)); % initial complex beam parameter
Psii = atan(real(qi)/imag(qi)); % Gouy phase
mat = [1 zf; 0 1]; % transformation matrix
A = mat(1,1); B = mat(1,2); C = mat(2,1); D = mat(2,2);
qf = (A*qi + B)/(C*qi + D); % final complex beam parameter
wf = sqrt(-lambda/pi*(1/imag(1/qf))); % final spot size
Rf = 1/real(1/qf); % final radius of curvature
Psif = atan(real(qf)/imag(qf)); % final Gouy phase
% Hermite - Gaussian modes function
nlist = 0:2:20; % modes range
% precompute hermite polynomials for nlist
hermites = {};
for n = nlist
if n == 0
hermites{n + 1} = #(x)1.0;
else
hermites{n + 1} = matlabFunction(hermiteH(n, x));
end
end
u = #(z, x, n, w, R, Psi) (2/pi)^(1/4)*sqrt(exp(1i*(2*n+1)*Psi)/(2^n*factorial(n)*w))*...
hermites{n + 1}(sqrt(2)*x/w).*exp(-x.^2*(1/w^2+1i*k/(2*R))-1i*k*z);
% Complex amplitude coefficients function
a = #(n) exp(1i*k*zi)*integral(#(x) Ei.*conj(u(zi, x, n, wi, Ri, Psii)),-2*wi,2*wi);
%----------------------------------------------------------------------------
xlisti = -0.1:1/10000:0.1; % initial x-axis range
xlistf = -0.1:1/10000:0.1; % final x-axis range
function Eiplot
Efieldi = zeros(size(xlisti));
for nr = nlist
Efieldi = Efieldi + a(nr).*u(zi, xlisti, nr, wi, Ri, Psii)*exp(-1i*k*zi);
end
Ii = 1/2*c*epsilon*arrayfun(#(x)x.*conj(x),Efieldi);
end
function Efplot
Efieldf = zeros(size(xlistf));
for nr = nlist
Efieldf = Efieldf + a(nr).*u(zf, xlistf, nr, wf, Rf, Psif)*exp(-1i*k*zf);
end
If = 1/2*c*epsilon*arrayfun(#(x)x.*conj(x),Efieldf);
end
Eiplot
Efplot
plot(xlisti,real(Ii),xlistf,real(If))
xlabel('x(m)') % x-axis label
ylabel('I(W/m^2)') % y-axis label
end

How to modify the color of images to remove vibrancy?

How I can change the colors from this:
into this:
I generated the output image using Gimp with the input image as layer one, and the background color from the image as layer two, in the Layers panel I selected the mode "colors"
I want preserve the background color, but want the colors be shades of brown.
Any ideas to do this with ChunkyPNG? Or should I use ImageMagick with color lookup tables?
Thanks for your ideas.
I found the one from Linuxios the most helpful Gimp layer modes
require "json"
require "httpclient"
require "chunky_png"
module ChunkyPNG::Color
def h(value)
r,g,b = r(value).to_f / MAX, g(value).to_f / MAX, b(value).to_f / MAX
min, max = [r,g,b].minmax
return 0 if max == min
result = case max
when r then (g - b) / (max - min) + (g < b ? 6 : 0)
when g then (b - r) / (max - min) + 2
when b then (r - g) / (max - min) + 4
end
result * 60
end
def s(value)
min, max = [r(value), g(value), b(value)].minmax.map { |value| value.to_f / MAX }
max == 0 ? 0 : (max - min) / max
end
def v(value)
[r(value), g(value), b(value)].max.to_f / MAX
end
def hsv(h, s, v)
h = h.to_f / 360
i = (h * 6).floor
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
case i % 6
when 0 then r, g, b = v, t, p
when 1 then r, g, b = q, v, p
when 2 then r, g, b = p, v, t
when 3 then r, g, b = p, q, v
when 4 then r, g, b = t, p, v
when 5 then r, g, b = v, p, q
end
rgb *[r,g,b].map {|value| (value * 255).round }
end
end
module CoderWall
module_function
def badges(username)
url = URI.escape("https://coderwall.com/#{username}.json")
response = HTTPClient.get(url)
json = JSON.parse(response.body)
urls = json['badges'].map {|b| b['badge']}
brown = ChunkyPNG::Color.from_hex("#bf8a30")
hue = ChunkyPNG::Color.h(brown)
saturation = ChunkyPNG::Color.s(brown)
value = ChunkyPNG::Color.v(brown)
urls.each do |url|
matches = url.match /(\w+)\-/
response = HTTPClient.get(url)
filename = "./tmp/coderwall/"+matches[1]+".png"
File.open(filename,"w") {|f| f.write(response.body)}
image = ChunkyPNG::Image.from_file(filename)
image.pixels.map! do |pixel|
v = ChunkyPNG::Color.v(pixel)
unless ChunkyPNG::Color.a(pixel) > 0
v = value
end
ChunkyPNG::Color.hsv(hue,saturation, v)
end
image.save(filename.gsub(/\.png/,"_brown.png"), :fast_rgba)
end
end
end
badges = CoderWall.badges("astropanic")
That above code consists from some snippets found around the web.
Here is the result: Stop doing new year resolutions. Make the change now !

Resources