plt.plot(cout0[:, 0], cout0[:, 1], 'r')
plt.subplot(1, 2, 2)
plt.plot(k)

#%%

#generate shape from segmentable image

reload(wg)
import analysis.experiment as exp
img = exp.load_img(wid=80, t=524273 - 1)

plt.figure(1)
plt.clf()

m = wm.WormModel(npoints=31)
m.from_image(img, verbose=True)

l, r = m.shape()

plt.subplot(2, 3, 5)
plt.plot(l[:10, 0], l[:10, 1], 'w')

#%%

img2 = exp.load_img(wid=80, t=524273)

plt.figure(2)
plt.clf()
cont = wg.contours_from_image(img2, verbose=True)
Exemplo n.º 2
0
import analysis.experiment as exp
import scipy.ndimage.filters as filters
from interpolation.curve import Curve
from interpolation.resampling import resample as resample_curve

import worm.costs as wc
reload(wc)

### Gradient descent

t0 = 500000

img = exp.load_img(wid=80, t=t0)
#imgs = filters.gaussian_filter(np.asarray(img, float), 1.0);

w = wm.WormModel(npoints=10)
w.from_image(img, verbose=False)

w.move_forward(0.1)
w.rotate(0.1)

contours = wgeo.contours_from_image(img,
                                    sigma=1,
                                    absolute_threshold=None,
                                    threshold_factor=0.9,
                                    verbose=False,
                                    save=None)
contour = Curve(resample_curve(contours[0], 100), nparameter=50)

head_tail_xy = wgeo.head_tail_from_contour_discrete(contour,
                                                    ncontour=all,
Exemplo n.º 3
0
def test():
  import numpy as np
  import tensorflow as tf
  import matplotlib.pyplot as plt;
  import worm.model as wm;
  import worm.machine_vision_d as wmv
  import worm.geometry as wgeo
  
  reload(wgeo)
  reload(wmv)  
  
  ### Prepare optimization task
  
  # work shape
  w = wm.WormModel(length = 80);
  ig = wmv.ImageGenerator();
    
  ig.t_counter = 500000 + 25620 - 5;
  ig.t_counter = 500000 + 2510;
  ig.wid_counter = 0;

  img, cntr, nrml = ig.get_batch(nimages = 1);
  w.from_image(img)  
  w.move_forward(0.05);

  plt.figure(20); plt.clf();
  #wgeo.skeleton_from_image_discrete(img, verbose = True, with_head_tail=True)
  w.plot(image = img);
  plt.scatter(cntr[:,0], cntr[:,1])
  
  # target
  ig.t_counter = 500000 + 25620 - 1;
  ig.t_counter = 500000 + 2510;
  ig.wid_counter = 0;
  #imgs, skels, valids, hts, htvs = ig.get_batch(nimages = 1);
  imgt, cntrt, nrmlt = ig.get_batch(nimages = 1);
  plt.figure(21); plt.clf();
  w.plot(image = imgt);
  plt.scatter(cntrt[:,0], cntrt[:,1])
  
  w2 = w.copy();
  npts = w.npoints;  
  
  ### bending profiles
  bend_exponent = 4;
  n2 = npts//2;
  bp = np.zeros((2, npts));
  bp[0,:n2] = np.exp(-bend_exponent * np.linspace(-0.2,1,n2));
  bp[1,-n2:] = np.exp(-bend_exponent * np.linspace(-0.2,1,n2))[::-1];
  #can add other bending profiles here
  plt.figure(22); plt.clf();
  plt.plot(bp.T)  
  
  
  ### Cost functions 
  wb = 0.0; ws = 0.0; gamma = 1.0; kappa = 10.0

  xy = wmv.create_variable([2], value = 0.0);
  persitaltic = wmv.create_variable([1], value = 0.0);
  bend = wmv.create_variable([bp.shape[0]], value = 0.0);
  
  #center = wmv.create_variable([npts, 2]);
  center = tf.constant(w.center, "float32");
  
  wh = w.width; wh[wh < 1.0] = 1.0;
  width = tf.constant(wh / 2.0, "float32");
  length = w.length + 20;
  
  bend_profiles = tf.constant(bp, "float32");
  
  contour = tf.constant(cntrt, "float32");
  contour_normals = tf.constant(nrmlt, "float32");
  
  cost, cost_left, cost_right, cost_spacing, cost_bending, center_new, left, right, normals = wmv.create_cost(
                    center, xy, width, persitaltic, bend, bend_profiles, contour, contour_normals, length,
                    weight_bending = wb, weight_spacing = ws, gamma = gamma, kappa = kappa);
  
  costs       = [ cost, cost_left, cost_right, cost_spacing, cost_bending];
  costs_names = ['cost', 'cost_left', 'cost_right', 'cost_spacing', 'cost_bending'];
  
  grad = tf.gradients(cost, [persitaltic, bend, xy]);
  
  ### Tensoroflow  - Session 
  sess = None;
  sess = tf.InteractiveSession()
  init = tf.initialize_all_variables();
  sess.run(init)      
  
  assign_p = persitaltic.assign([0]);
  assign_b = bend.assign([0,0]);
  sess.run(assign_p); sess.run(assign_b); 
  g = sess.run(grad);
  print g

  ### Compare costs
  for c,n in zip(costs, costs_names):
    print '%20s: %f' % (n, sess.run(c));
  
  ### Manual Gradient descent
  
  c1 = w.center;
  
  p1 = np.array([0]); b1 = np.array([0,0]);
  xy1 = np.array([0,0]);
  w2 = w.copy();
  sg = .15;
  nsteps = 100;
  for i in range(nsteps): 
    sess.run(persitaltic.assign(p1)); sess.run(bend.assign(b1)); sess.run(xy.assign(xy1));
    g = sess.run(grad);
    p1 = p1 - sg * g[0]/ np.sqrt(np.sum(g[0]*g[0]));
    b1 = b1 - sg * g[1] / np.sqrt(np.sum(g[1]*g[1]));
    xy1 = xy1 - sg * g[2] / np.sqrt(np.sum(g[2]*g[2]));

    w2.center = sess.run(center_new);
    plt.figure(10); plt.clf();
    w2.plot(image = imgt);
    plt.scatter(cntrt[0,0], cntrt[0,1], c = 'k', s = 150)
    plt.scatter(cntrt[1,0], cntrt[1,1], c = 'r', s = 150)
    plt.scatter(w2.center[0,0], w2.center[0,1], c = 'r', s = 150)
    plt.scatter(cntrt[:,0], cntrt[:,1])
    for cc,nn in zip(cntrt, nrmlt):
      cn = cc + nn;
      plt.plot([cc[0], cn[0]], [cc[1], cn[1]], c = 'k')
    
    l,r = left.eval(), right.eval();
    nrmls = normals.eval();
    for cc,nn in zip(l, nrmls):
      cn = cc + nn;
      plt.plot([cc[0], cn[0]], [cc[1], cn[1]], c = 'k')

    for cc,nn in zip(r, -nrmls):
      cn = cc + nn;
      plt.plot([cc[0], cn[0]], [cc[1], cn[1]], c = 'k')


    plt.title('cost %f' % sess.run(cost));
    #plt.xlim(40,120); plt.ylim(40, 120)
    plt.draw();
    plt.pause(0.05);
  
  
  
  
  # test routines
  t  = wmv.create_tangent(center);
  tn = wmv.create_normalize_tangent(t);
  nl = wmv.create_normal(tn);  
  l,r = wmv.create_left_right(center, width, nl);

  d = wmv.create_pair_wise_distances(l, contour);
  a = wmv.create_pair_wise_dots(nl, contour_normals);
  a = tf.scalar_mul(-0.5, tf.add(a, -1.0)); # [0,1] 0 = aligned
  da= wmv.create_aligned_distance(d,a,gamma = gamma, k = kappa);
  
  plt.figure(6); plt.clf();
  plt.subplot(2,2,1);
  plt.imshow(d.eval(), interpolation = 'none');
  plt.subplot(2,2,2);
  plt.imshow(a.eval(), interpolation = 'none');
  plt.subplot(2,2,3);
  plt.imshow(d.eval() + a.eval() * np.max(d.eval()), interpolation = 'none');
  plt.subplot(2,2,4);
  plt.plot(da.eval());
  
    #Test center to left,right
  wh = w.width; wh[wh < 1.0] = 1.0; 
  l1,r1 = w.shape();
  length = w.length;
  
  width = tf.constant(wh /2.0, "float32");
  center = tf.constant(w.center, "float32");
  
  left,right = wmv.create_left_right(center, width)
  
  sess = None;
  sess = tf.InteractiveSession()
  init = tf.initialize_all_variables();
  sess.run(init)   
  l2= sess.run(left);
  r2= sess.run(right);
  
  plt.figure(1); plt.clf();
  plt.scatter(l1[:,0], l1[:,1], c='g');
  plt.scatter(r1[:,0], r1[:,1], c='r');
  plt.scatter(l2[:,0], l2[:,1], c='k');
  plt.scatter(r2[:,0], r2[:,1], c='m'); 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  c = left; s = contour;
  c_shape = c.get_shape().as_list();        
  s_shape = s.get_shape().as_list();
  
  #expand matrices
  cc = tf.reshape(c, [c_shape[0], c_shape[1], 1]);    
  ss = tf.reshape(s, [s_shape[0], s_shape[1], 1]);
  ss = tf.transpose(ss, perm = [2,1,0]);
  cc = tf.tile(cc, [1, 1, s_shape[0]]);
  ss = tf.tile(ss, [c_shape[0], 1, 1]);
  #cc = tf.transpose(cc, perm = [2,1,0]);
  #cc = tf.tile(cc, [s_shape[0], 1, 1]);
  #ss = tf.tile(ss, [1, 1, c_shape[0]]);  
  
  #pairwise distances
  dist2 = tf.sqrt(tf.reduce_sum(tf.squared_difference(cc,ss), reduction_indices = 1));
    
  #softmin
  k = 100000.0
  softmin = tf.mul(tf.nn.softmax(tf.scalar_mul(tf.constant(-k,"float32"), dist2)), dist2);
  softmin2 = tf.reduce_sum(tf.mul(tf.nn.softmax(tf.scalar_mul(tf.constant(-k,"float32"), dist2)), dist2),reduction_indices = 1);  
    
  plt.figure(11); plt.clf();
  plt.subplot(2,2,1);
  plt.imshow(dist2.eval(), interpolation = 'none'); plt.colorbar()
  plt.subplot(2,2,2);
  plt.imshow(softmin.eval(), interpolation = 'none'); plt.colorbar()
  plt.subplot(2,2,3);
  plt.plot(softmin2.eval())
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  ### Tensorflow optimization
  #trainer = tf.train.AdadeltaOptimizer().minimize(cost, var_list=[par]);
  trainer = tf.train.GradientDescentOptimizer(learning_rate=2.0).minimize(cost);
  init = tf.initialize_all_variables();
  sess.run(init)   
  
  sess.run(assign_op)
  
  nsteps = 1000;
  for i in range(nsteps):
    trainer.run(session = sess, feed_dict={});
    p1 = par.eval(session = sess);
    
    if i%10 == 0:
      plt.figure(10); plt.clf();
      w.center = p1[0];
      w.plot(image= imgt);
      plt.scatter(skels[0,:,0], skels[0,:,1], c= 'm')
      plt.xlim(0,151); plt.ylim(0,151)
      plt.draw();
      plt.pause(0.1);
Exemplo n.º 4
0
t = t+1;
print(t);
img = exp.load_img(wid = 80, t= 524700+914+t);
imgs = filters.gaussian_filter(np.asarray(img, float), 1.0);
#imgs = img;
th = 90;
imgs[imgs  > th] = th;
#imgs =- th -imgs;


plt.figure(1); plt.clf();
plt.imshow(imgs);

# find worm model

ws = aw.WormModel();
ws.from_image(imgs, verbose = False, sigma = None, threshold_factor = 0.95);

plt.figure(1); plt.clf();
ws.plot(image = imgs)


# represent worm by spline coefficients for angle of center line + fixed width profile







Exemplo n.º 5
0
    import numpy as np
    import matplotlib.pyplot as plt
    import scipy.ndimage.filters as filters

    from interpolation.curve import Curve
    from interpolation.resampling import resample as resample_curve
    import analysis.experiment as exp

    reload(wgeo)
    reload(wmod)

    ### Initialize Worm from Image
    npoints = 11
    nobs = npoints * 2 - 2
    #d differences from the sides / head tail only once
    worm = wmod.WormModel(npoints=npoints)
    nparameter = worm.nparameter
    #full number of parameter

    t0 = 500000
    threshold_factor = 0.9
    absolute_threshold = None

    img = exp.load_img(wid=80, t=t0)
    imgs = filters.gaussian_filter(np.asarray(img, float), 1.0)
    worm.from_image(img,
                    absolute_threshold=absolute_threshold,
                    threshold_factor=threshold_factor,
                    verbose=True)
    worm0 = worm.copy()
#%%

f_id = 80

import worm.geometry as wgeo
reload(wgeo);

plt.clf()
res = wgeo.shape_from_image(blur[f_id], threshold_factor = 0.975, sigma =  None, verbose = True, smooth = 15, smooth_center = 2.5, npoints = 45, ncontour = 80, center_offset = 3)


#%%

import worm.model as wmod

wm = wmod.WormModel(npoints = 21)

f_id = 5;
plt.figure(1); plt.clf();
wm.from_image(blur[f_id], sigma = None, verbose = True, smooth = 10)

plt.figure(19); plt.clf();
plt.subplot(1,2,1);
wm.plot(blur[f_id])
plt.subplot(1,2,2);
plt.plot(wm.width);
plt.draw();


#%%
import worm.geometry as wgeo
Detect worm shapes / postures
"""
__license__ = 'MIT License <http://www.opensource.org/licenses/mit-license.php>'
__author__ = 'Christoph Kirst <*****@*****.**>'

import os
import numpy as np
import matplotlib.pyplot as plt

import analysis.experiment as exp
import analysis.plot as aplt
import worm.model as wmodel

npts = 22
w = wmodel.WormModel(npoints=npts)

fn = exp.filename(strain='n2', wid=80, dtype='img')
imgdata = np.load(fn, mmap_mode='r')

ntimes = imgdata.shape[0]

theta, orientation, xy, length = w.theta()
ntheta = theta.shape[0]

data_path = '/home/ckirst/Desktop'
data_names = ['theta', 'orientation', 'xy', 'length', 'center', 'width']
data_sizes = [(ntimes, ntheta), ntimes, (ntimes, 2), ntimes, (ntimes, npts, 2),
              (ntimes, npts)]

data_files = [
reload(wgeo)
reload(wm)

import analysis.experiment as exp
import scipy.ndimage.filters as filters
from interpolation.curve import Curve
from interpolation.resampling import resample as resample_curve

# load image

t0 = 500000

img = exp.load_img(wid=80, t=t0)
imgs = filters.gaussian_filter(np.asarray(img, float), 1.0)

w = wm.WormModel(npoints=20)
w.from_image(img, verbose=True)

w.move_forward(0.1)
w.rotate(0.1)

plt.figure(1)
plt.clf()
plt.subplot(1, 2, 1)
w.plot(image=imgs)
plt.subplot(1, 2, 2)
cntrs = wgeo.contour_from_image(imgs,
                                sigma=1,
                                absolute_threshold=None,
                                threshold_factor=0.9,
                                verbose=True,
Exemplo n.º 9
0
# create worms
fig = plt.figure(9);

writer.setup(fig, "turning_resul_2t.mp4", dpi = 300)

plt.clf(); plt.subplot(1,1,1);
ax = plt.gca();
ax.set_xlim(0, 151)
ax.set_ylim(0, 151)
for i,c in enumerate(centers_2):
  print i
  #if i == 54-fid:
  #  continue;
  #if i == 65 - fid:
  #  break;
  wm = wmod.WormModel(center = c, width = width_prev);
  plt.clf();
  wm.plot(blur[i+fid], ax = plt.gca(), cmap = 'gray');
  plt.xlim(0,151); plt.ylim(0,151);
  plt.draw(); plt.pause(0.05);
  writer.grab_frame();
  

writer.cleanup();



#%%

reload(dst);
Exemplo n.º 10
0
def test():
    import numpy as np
    import tensorflow as tf
    import matplotlib.pyplot as plt
    import worm.model as wm
    import worm.machine_vision_b as wmv
    import worm.geometry as wgeo

    reload(wgeo)
    reload(wmv)

    ### Prepare optimization task

    # work shape
    w = wm.WormModel(length=80)
    ig = wmv.ImageGenerator()

    ig.t_counter = 500000 + 25620 - 5
    ig.wid_counter = 0

    imgs, cntrs = ig.get_batch(nimages=1)
    img = imgs[0, :, :, 0]
    cntr = cntrs[0]
    w.from_image(img)

    plt.figure(20)
    plt.clf()
    #wgeo.skeleton_from_image_discrete(img, verbose = True, with_head_tail=True)
    w.plot(image=img)
    plt.scatter(cntr[:, 0], cntr[:, 1])

    # target
    ig.t_counter = 500000 + 25620 - 1
    ig.wid_counter = 0
    #imgs, skels, valids, hts, htvs = ig.get_batch(nimages = 1);
    imgs, cntrs = ig.get_batch(nimages=1)
    imgt = imgs[0, :, :, 0]
    cntrt = cntrs[0]
    plt.figure(21)
    plt.clf()
    w.plot(image=imgt)
    plt.scatter(cntrt[:, 0], cntrt[:, 1])

    #Test center to left,right
    wh = w.width
    wh[wh < 1.0] = 1.0
    l1, r1 = w.shape()
    length = w.length

    width = tf.constant(wh / 2.0, "float32")
    center = tf.constant(w.center, "float32")

    left, right = wmv.create_left_right(center, width)

    sess = None
    sess = tf.InteractiveSession()
    init = tf.initialize_all_variables()
    sess.run(init)
    l2 = sess.run(left)
    r2 = sess.run(right)

    plt.figure(1)
    plt.clf()
    plt.scatter(l1[:, 0], l1[:, 1], c='g')
    plt.scatter(r1[:, 0], r1[:, 1], c='r')
    plt.scatter(l2[:, 0], l2[:, 1], c='k')
    plt.scatter(r2[:, 0], r2[:, 1], c='m')

    ### Cost functions
    wb = 0
    ws = 0.1
    wd = 0.1

    npts = w.npoints
    left = wmv.create_variable([npts, 2])
    right = wmv.create_variable([npts, 2])

    wh = w.width
    wh[wh < 1.0] = 1.0
    width = tf.constant(wh, "float32")
    length = w.length
    contour = tf.constant(cntrt, "float32")

    cost = wmv.create_cost(left,
                           right,
                           contour,
                           width,
                           length,
                           weight_bending=wb,
                           weight_spacing=ws,
                           weight_distance=wd)

    cost_left = wmv.create_cost_side(left,
                                     contour,
                                     length,
                                     weight_spacing=ws,
                                     weight_bending=wb)
    cost_right = wmv.create_cost_side(right,
                                      contour,
                                      length,
                                      weight_spacing=ws,
                                      weight_bending=wb)
    cost_left_bend = wmv.create_cost_bending(left)
    cost_right_bend = wmv.create_cost_bending(right)
    cost_left_spacing = wmv.create_cost_spacing(left, length)
    cost_right_spacing = wmv.create_cost_spacing(right, length)

    cost_dist = wmv.create_cost_distance(left, right, width)
    cost_left_contour = wmv.create_cost_soft_min_distance(left, contour)
    cost_right_contour = wmv.create_cost_soft_min_distance(right, contour)

    costs = [
        cost, cost_left, cost_right, cost_left_bend, cost_right_bend,
        cost_left_spacing, cost_right_spacing, cost_dist, cost_left_contour,
        cost_right_contour
    ]
    costs_names = [
        'cost', 'cost_left', 'cost_right', 'cost_left_bend', 'cost_right_bend',
        'cost_left_spacing', 'cost_right_spacing', 'cost_dist',
        'cost_left_contour', 'cost_right_contour'
    ]
    costs_weights = [1, 1, 1, wb, wb, ws, ws, wd, 1, 1]

    grad = tf.gradients(cost, [left, right])

    ### Tensoroflow  - Session
    sess = None
    sess = tf.InteractiveSession()
    init = tf.initialize_all_variables()
    sess.run(init)

    l, r = w.shape()
    assign_left = left.assign(l)
    assign_right = right.assign(r)
    sess.run(assign_left)
    sess.run(assign_right)
    g = sess.run(grad)
    print g

    ### Compare costs
    for c, n, ww in zip(costs, costs_names, costs_weights):
        print '%20s: %f' % (n, ww * sess.run(c))

    ### Manual Gradient descent

    l1, r1 = w.shape()
    sg = .75
    nsteps = 100
    for i in range(nsteps):
        sess.run(left.assign(l1))
        sess.run(right.assign(r1))
        g = sess.run(grad)
        l1 = l1 - sg * g[0] / np.sqrt(np.sum(g[0] * g[0]))
        r1 = r1 - sg * g[1] / np.sqrt(np.sum(g[1] * g[1]))

        plt.figure(10)
        plt.clf()
        plt.imshow(imgt, cmap='jet')
        plt.scatter(l1[:, 0], l1[:, 1], s=60, c='r')
        plt.scatter(r1[:, 0], r1[:, 1], s=60, c='g')
        plt.plot(l1[:, 0], l1[:, 1], c='r')
        plt.plot(r1[:, 0], r1[:, 1], c='g')
        plt.scatter(l1[0, 0], l1[0, 1], c='k', s=150)
        plt.scatter(cntrt[0, 0], cntrt[0, 1], c='k', s=150)
        plt.scatter(cntrt[:, 0], cntrt[:, 1])

        plt.title('cost %f' % sess.run(cost))
        plt.draw()
        plt.pause(0.05)

    c = left
    s = contour
    c_shape = c.get_shape().as_list()
    s_shape = s.get_shape().as_list()

    #expand matrices
    cc = tf.reshape(c, [c_shape[0], c_shape[1], 1])
    ss = tf.reshape(s, [s_shape[0], s_shape[1], 1])
    ss = tf.transpose(ss, perm=[2, 1, 0])
    cc = tf.tile(cc, [1, 1, s_shape[0]])
    ss = tf.tile(ss, [c_shape[0], 1, 1])
    #cc = tf.transpose(cc, perm = [2,1,0]);
    #cc = tf.tile(cc, [s_shape[0], 1, 1]);
    #ss = tf.tile(ss, [1, 1, c_shape[0]]);

    #pairwise distances
    dist2 = tf.sqrt(
        tf.reduce_sum(tf.squared_difference(cc, ss), reduction_indices=1))

    #softmin
    k = 100000.0
    softmin = tf.mul(
        tf.nn.softmax(tf.scalar_mul(tf.constant(-k, "float32"), dist2)), dist2)
    softmin2 = tf.reduce_sum(tf.mul(
        tf.nn.softmax(tf.scalar_mul(tf.constant(-k, "float32"), dist2)),
        dist2),
                             reduction_indices=1)

    plt.figure(11)
    plt.clf()
    plt.subplot(2, 2, 1)
    plt.imshow(dist2.eval(), interpolation='none')
    plt.colorbar()
    plt.subplot(2, 2, 2)
    plt.imshow(softmin.eval(), interpolation='none')
    plt.colorbar()
    plt.subplot(2, 2, 3)
    plt.plot(softmin2.eval())

    ### Tensorflow optimization
    #trainer = tf.train.AdadeltaOptimizer().minimize(cost, var_list=[par]);
    trainer = tf.train.GradientDescentOptimizer(
        learning_rate=2.0).minimize(cost)
    init = tf.initialize_all_variables()
    sess.run(init)

    sess.run(assign_op)

    nsteps = 1000
    for i in range(nsteps):
        trainer.run(session=sess, feed_dict={})
        p1 = par.eval(session=sess)

        if i % 10 == 0:
            plt.figure(10)
            plt.clf()
            w.center = p1[0]
            w.plot(image=imgt)
            plt.scatter(skels[0, :, 0], skels[0, :, 1], c='m')
            plt.xlim(0, 151)
            plt.ylim(0, 151)
            plt.draw()
            plt.pause(0.1)
Exemplo n.º 11
0
def test():
    import numpy as np
    import tensorflow as tf
    import matplotlib.pyplot as plt
    import worm.model as wm
    import worm.machine_vision_c as wmv
    import worm.geometry as wgeo

    reload(wgeo)
    reload(wmv)

    ### Prepare optimization task

    # work shape
    w = wm.WormModel(length=80)
    ig = wmv.ImageGenerator()

    ig.t_counter = 500000 + 25620 - 5
    ig.wid_counter = 0

    img, cntr, nrml = ig.get_batch(nimages=1)
    w.from_image(img)

    plt.figure(20)
    plt.clf()
    #wgeo.skeleton_from_image_discrete(img, verbose = True, with_head_tail=True)
    w.plot(image=img)
    plt.scatter(cntr[:, 0], cntr[:, 1])

    # target
    ig.t_counter = 500000 + 25620 - 1
    ig.wid_counter = 0
    #imgs, skels, valids, hts, htvs = ig.get_batch(nimages = 1);
    imgt, cntrt, nrmlt = ig.get_batch(nimages=1)
    plt.figure(21)
    plt.clf()
    w.plot(image=imgt)
    plt.scatter(cntrt[:, 0], cntrt[:, 1])

    w2 = w.copy()

    ### Cost functions
    wb = 1.0
    ws = 0.5
    gamma = 1.0
    kappa = 10.0

    npts = w.npoints
    center = wmv.create_variable([npts, 2])

    wh = w.width
    wh[wh < 1.0] = 1.0
    width = tf.constant(wh / 2.0, "float32")
    length = w.length + 20
    contour = tf.constant(cntrt, "float32")
    contour_normals = tf.constant(nrmlt, "float32")

    cost, cost_left, cost_right, cost_spacing, cost_bending, normals, left, right = wmv.create_cost(
        center,
        width,
        contour,
        contour_normals,
        length,
        weight_bending=wb,
        weight_spacing=ws,
        gamma=gamma,
        kappa=kappa)

    costs = [cost, cost_left, cost_right, cost_spacing, cost_bending]
    costs_names = [
        'cost', 'cost_left', 'cost_right', 'cost_spacing', 'cost_bending'
    ]

    grad = tf.gradients(cost, [center])

    ### Tensoroflow  - Session
    sess = None
    sess = tf.InteractiveSession()
    init = tf.initialize_all_variables()
    sess.run(init)

    assign = center.assign(w.center)
    sess.run(assign)
    g = sess.run(grad)
    print g

    ### Compare costs
    for c, n in zip(costs, costs_names):
        print '%20s: %f' % (n, sess.run(c))

    ### Manual Gradient descent

    c1 = w.center
    w2 = w.copy()
    sg = .75
    nsteps = 100
    for i in range(nsteps):
        sess.run(center.assign(c1))
        g = sess.run(grad)
        c1 = c1 - sg * g[0] / np.sqrt(np.sum(g[0] * g[0]))

        w2.center = c1
        plt.figure(10)
        plt.clf()
        w2.plot(image=imgt)
        plt.scatter(cntrt[0, 0], cntrt[0, 1], c='k', s=150)
        plt.scatter(cntrt[1, 0], cntrt[1, 1], c='r', s=150)
        plt.scatter(w2.center[0, 0], w2.center[0, 1], c='r', s=150)
        plt.scatter(cntrt[:, 0], cntrt[:, 1])
        for cc, nn in zip(cntrt, nrmlt):
            cn = cc + nn
            plt.plot([cc[0], cn[0]], [cc[1], cn[1]], c='k')

        l, r = left.eval(), right.eval()
        nrmls = normals.eval()
        for cc, nn in zip(l, nrmls):
            cn = cc + nn
            plt.plot([cc[0], cn[0]], [cc[1], cn[1]], c='k')

        for cc, nn in zip(r, -nrmls):
            cn = cc + nn
            plt.plot([cc[0], cn[0]], [cc[1], cn[1]], c='k')

        plt.title('cost %f' % sess.run(cost))
        plt.xlim(40, 120)
        plt.ylim(40, 120)
        plt.draw()
        plt.pause(0.05)

    # test routines
    t = wmv.create_tangent(center)
    tn = wmv.create_normalize_tangent(t)
    nl = wmv.create_normal(tn)
    l, r = wmv.create_left_right(center, width, nl)

    d = wmv.create_pair_wise_distances(l, contour)
    a = wmv.create_pair_wise_dots(nl, contour_normals)
    a = tf.scalar_mul(-0.5, tf.add(a, -1.0))
    # [0,1] 0 = aligned
    da = wmv.create_aligned_distance(d, a, gamma=gamma, k=kappa)

    plt.figure(6)
    plt.clf()
    plt.subplot(2, 2, 1)
    plt.imshow(d.eval(), interpolation='none')
    plt.subplot(2, 2, 2)
    plt.imshow(a.eval(), interpolation='none')
    plt.subplot(2, 2, 3)
    plt.imshow(d.eval() + a.eval() * np.max(d.eval()), interpolation='none')
    plt.subplot(2, 2, 4)
    plt.plot(da.eval())

    #Test center to left,right
    wh = w.width
    wh[wh < 1.0] = 1.0
    l1, r1 = w.shape()
    length = w.length

    width = tf.constant(wh / 2.0, "float32")
    center = tf.constant(w.center, "float32")

    left, right = wmv.create_left_right(center, width)

    sess = None
    sess = tf.InteractiveSession()
    init = tf.initialize_all_variables()
    sess.run(init)
    l2 = sess.run(left)
    r2 = sess.run(right)

    plt.figure(1)
    plt.clf()
    plt.scatter(l1[:, 0], l1[:, 1], c='g')
    plt.scatter(r1[:, 0], r1[:, 1], c='r')
    plt.scatter(l2[:, 0], l2[:, 1], c='k')
    plt.scatter(r2[:, 0], r2[:, 1], c='m')

    c = left
    s = contour
    c_shape = c.get_shape().as_list()
    s_shape = s.get_shape().as_list()

    #expand matrices
    cc = tf.reshape(c, [c_shape[0], c_shape[1], 1])
    ss = tf.reshape(s, [s_shape[0], s_shape[1], 1])
    ss = tf.transpose(ss, perm=[2, 1, 0])
    cc = tf.tile(cc, [1, 1, s_shape[0]])
    ss = tf.tile(ss, [c_shape[0], 1, 1])
    #cc = tf.transpose(cc, perm = [2,1,0]);
    #cc = tf.tile(cc, [s_shape[0], 1, 1]);
    #ss = tf.tile(ss, [1, 1, c_shape[0]]);

    #pairwise distances
    dist2 = tf.sqrt(
        tf.reduce_sum(tf.squared_difference(cc, ss), reduction_indices=1))

    #softmin
    k = 100000.0
    softmin = tf.mul(
        tf.nn.softmax(tf.scalar_mul(tf.constant(-k, "float32"), dist2)), dist2)
    softmin2 = tf.reduce_sum(tf.mul(
        tf.nn.softmax(tf.scalar_mul(tf.constant(-k, "float32"), dist2)),
        dist2),
                             reduction_indices=1)

    plt.figure(11)
    plt.clf()
    plt.subplot(2, 2, 1)
    plt.imshow(dist2.eval(), interpolation='none')
    plt.colorbar()
    plt.subplot(2, 2, 2)
    plt.imshow(softmin.eval(), interpolation='none')
    plt.colorbar()
    plt.subplot(2, 2, 3)
    plt.plot(softmin2.eval())

    ### Tensorflow optimization
    #trainer = tf.train.AdadeltaOptimizer().minimize(cost, var_list=[par]);
    trainer = tf.train.GradientDescentOptimizer(
        learning_rate=2.0).minimize(cost)
    init = tf.initialize_all_variables()
    sess.run(init)

    sess.run(assign_op)

    nsteps = 1000
    for i in range(nsteps):
        trainer.run(session=sess, feed_dict={})
        p1 = par.eval(session=sess)

        if i % 10 == 0:
            plt.figure(10)
            plt.clf()
            w.center = p1[0]
            w.plot(image=imgt)
            plt.scatter(skels[0, :, 0], skels[0, :, 1], c='m')
            plt.xlim(0, 151)
            plt.ylim(0, 151)
            plt.draw()
            plt.pause(0.1)
Exemplo n.º 12
0
def test():
    import numpy as np
    import tensorflow as tf
    import matplotlib.pyplot as plt
    import worm.model as wm
    import worm.machine_vision_2 as wmv
    import worm.geometry as wgeo

    reload(wgeo)
    reload(wmv)

    ### Prepare optimization task

    # work shape
    w = wm.WormModel(length=80)
    ig = wmv.ImageGenerator()
    net = wmv.WormVision(model=w, images=ig)

    ig.t_counter = 500000 + 25620 - 5
    ig.wid_counter = 0
    imgs, cntrs = ig.get_batch(nimages=1)
    img = imgs[0, :, :, 0]
    w.from_image(img)

    plt.figure(20)
    plt.clf()
    wgeo.skeleton_from_image_discrete(img, verbose=True, with_head_tail=True)
    w.plot()

    # target
    ig.t_counter = 500000 + 25620 - 1
    ig.wid_counter = 0
    #imgs, skels, valids, hts, htvs = ig.get_batch(nimages = 1);
    imgs, skels, hts = ig.get_batch(nimages=1)
    imgt = imgs[0, :, :, 0]
    plt.figure(21)
    plt.clf()
    wgeo.skeleton_from_image_discrete(imgt,
                                      verbose=True,
                                      with_head_tail=True,
                                      absolute_threshold=75)
    w.plot()

    ### Cost functions
    wb = 2
    ws = 1.0

    par = net.create_variable(net.output.get_shape())
    skel = tf.constant(skels, "float32")

    cost = net.create_cost(par, skel, weight_bending=wb, weight_spacing=ws)
    cost_bend = net.create_cost_bending(par)
    cost_spacing = net.create_cost_spacing(par)
    cost_dist = net.create_cost_soft_min_distance(par, skel)

    grad = tf.gradients(cost, [par])

    ### Tensoroflow
    ### Session
    sess = None
    sess = tf.InteractiveSession()
    init = tf.initialize_all_variables()
    sess.run(init)

    assign_op = par.assign(w.center[None, :, :])
    sess.run(assign_op)

    ### Compare costs

    cb = wb * sess.run(cost_bend)
    cs = ws * sess.run(cost_spacing)
    cd = sess.run(cost_dist)
    c = sess.run(cost)
    print 'Costs: full: %f;  dist: %f;  bend: %f;  spacing :%f' % (c, cd, cb,
                                                                   cs)

    ### Manual Gradient descent

    p1 = w.center
    sg = .75
    nsteps = 100
    for i in range(nsteps):
        sess.run(par.assign(p1[None, :, :]))
        g = sess.run(grad)[0][0]
        p1 = p1 - sg * g / np.sqrt(np.sum(g * g))

        plt.figure(10)
        plt.clf()
        #plt.subplot(1,2,1)
        #w.plot(image = img);

        w.center = p1
        #plt.subplot(1,2,2);
        w.plot(image=imgt)

        plt.title('cost %f' % sess.run(cost))

        plt.draw()
        plt.pause(0.05)

    ### Tensorflow optimization
    #trainer = tf.train.AdadeltaOptimizer().minimize(cost, var_list=[par]);
    trainer = tf.train.GradientDescentOptimizer(
        learning_rate=2.0).minimize(cost)
    init = tf.initialize_all_variables()
    sess.run(init)

    sess.run(assign_op)

    nsteps = 1000
    for i in range(nsteps):
        trainer.run(session=sess, feed_dict={})
        p1 = par.eval(session=sess)

        if i % 10 == 0:
            plt.figure(10)
            plt.clf()
            w.center = p1[0]
            w.plot(image=imgt)
            plt.scatter(skels[0, :, 0], skels[0, :, 1], c='m')
            plt.xlim(0, 151)
            plt.ylim(0, 151)
            plt.draw()
            plt.pause(0.1)

    import shapely.geometry as geom
    cntrs = wgeo.contours_from_image(imgt)
    poly = geom.Polygon(cntrs[0], [cntrs[i] for i in range(1, len(cntrs))])
    poly2 = poly.buffer(-3)
    bdr = poly.boundary

    from descartes.patch import PolygonPatch
    patch = PolygonPatch(poly,
                         facecolor='b',
                         edgecolor='k',
                         alpha=0.5,
                         zorder=2)
    patch2 = PolygonPatch(poly2,
                          facecolor='r',
                          edgecolor='k',
                          alpha=0.5,
                          zorder=2)

    fig = plt.figure(28)
    ax = fig.add_subplot(111)
    ax.add_patch(patch)
    ax.add_patch(patch2)
    plt.xlim(0, 151)
    plt.ylim(1, 151)
    plt.show()
Exemplo n.º 13
0
fig = plt.figure(8); plt.clf();
plt.imshow(img, cmap = 'gray');
plt.xlim(0,151); plt.ylim(0,151)
plt.axis('off')
fig.savefig('pipeline_fail.png', facecolor = 'white')


t0 =  500000 + 25620 -5;
img = exp.load_img(t=t0, sigma = 1.0)
fig = plt.figure(8); plt.clf();
plt.imshow(img, cmap = 'gray');
plt.xlim(0,151); plt.ylim(0,151)
plt.axis('off')


w = wmod.WormModel(npoints = 22)
w.from_image(img, verbose = True)

fig = plt.figure(9); plt.clf();
w.plot(image = img, cmap = 'gray', ccolor='b');

plt.xlim(0,151); plt.ylim(0,151)
plt.axis('off')
fig.savefig('pipeline_fail_0.png', facecolor = 'white')


plt.clf();
t1 = t0 + 5
img = exp.load_img(t=t1, sigma = 1.0)
w1 = w.copy();
w1.bend(9)
Exemplo n.º 14
0
"""

import numpy as np
import matplotlib.pyplot as plt

import copy
import time

import worm.model as aw

#%%

### Test simple shape properties
reload(aw)
#ws = aw.WormModel(theta = 0.1, width = 10 * (1 - np.exp(-0.1 * (21 - np.abs(2* (np.arange(20)+1) - 21)))));
ws = aw.WormModel(width=None, length=80, orientation=2.1)
#ws.widen(0.5);

plt.figure(1)
plt.clf()
plt.subplot(1, 2, 1)
ws.plot()
plt.axis('equal')
plt.xlim(0, 151)
plt.ylim(0, 151)
plt.subplot(1, 2, 2)
ws.plot(npoints=2 * ws.npoints + 1)
plt.xlim(0, 151)
plt.ylim(0, 151)
plt.axis('equal')
plt.draw()
Exemplo n.º 15
0
def test():
  import numpy as np
  import tensorflow as tf
  import matplotlib.pyplot as plt;
  import worm.model as wm;
  import worm.machine_vision_3 as wmv
  import worm.geometry as wgeo
  
  reload(wgeo)
  reload(wmv)  
  
  ### Prepare optimization task
  
  # work shape
  w = wm.WormModel(length = 80);
  ig = wmv.ImageGenerator();
  net = wmv.WormVision(model = w, images = ig);  
  
  ig.t_counter = 500000 + 25620 - 5;
  ig.wid_counter = 0;

  imgs, cntrs = ig.get_batch(nimages = 1);
  img = imgs[0,:,:,0]; cntr = cntrs[0];
  w.from_image(img)  

  plt.figure(20); plt.clf();
  #wgeo.skeleton_from_image_discrete(img, verbose = True, with_head_tail=True)
  w.plot(image = img);
  plt.scatter(cntr[:,0], cntr[:,1])
  
  # target
  ig.t_counter = 500000 + 25620 - 1;
  ig.wid_counter = 0;
  #imgs, skels, valids, hts, htvs = ig.get_batch(nimages = 1);
  imgs, cntrs = ig.get_batch(nimages = 1);
  imgt = imgs[0,:,:,0]; cntrt = cntrs[0];
  plt.figure(21); plt.clf();
  w.plot(image = imgt);
  plt.scatter(cntrt[:,0], cntrt[:,1])
  
  
  ### Cost functions 
  wb = 2; ws = 1.0; wd = 2.0
  
  npts = w.npoints;
  left = net.create_variable([1, npts]);
  right = net.create_variable([1, npts]);
  
  width = tf.constant(w.width, "float32");
  length = tf.constant(w.length, "float32");
  contour = tf.constant(cntrs, "float32");
  
  cost = net.create_cost(left, right, contour, width,  weight_bending = wb, weight_spacing = ws);
  cost_bend = net.create_cost_bending(par);
  cost_spacing = net.create_cost_spacing(par);
  cost_dist = net.create_cost_soft_min_distance(par, skel);

  grad = tf.gradients(cost, [par]);
  
  ### Tensoroflow 
  ### Session 
  sess = None;
  sess = tf.InteractiveSession()
  init = tf.initialize_all_variables();
  sess.run(init)      
  
  assign_op = par.assign(w.center[None,:,:]);
  sess.run(assign_op)

  ### Compare costs

  cb = wb * sess.run(cost_bend);
  cs = ws * sess.run(cost_spacing);
  cd = sess.run(cost_dist);
  c  = sess.run(cost);
  print 'Costs: full: %f;  dist: %f;  bend: %f;  spacing :%f' % (c, cd, cb, cs);
  
  
  ### Manual Gradient descent
  
  p1 = w.center;
  sg = .75;
  nsteps = 100;
  for i in range(nsteps): 
    sess.run(par.assign(p1[None,:,:]));
    g = sess.run(grad)[0][0];
    p1 = p1 - sg * g / np.sqrt(np.sum(g*g));
    
    
    plt.figure(10); plt.clf();
    #plt.subplot(1,2,1)
    #w.plot(image = img);

    w.center = p1;
    #plt.subplot(1,2,2);
    w.plot(image= imgt);

    plt.title('cost %f' % sess.run(cost));
    
    plt.draw();
    plt.pause(0.05);
      
  
  ### Tensorflow optimization
  #trainer = tf.train.AdadeltaOptimizer().minimize(cost, var_list=[par]);
  trainer = tf.train.GradientDescentOptimizer(learning_rate=2.0).minimize(cost);
  init = tf.initialize_all_variables();
  sess.run(init)   
  
  sess.run(assign_op)
  
  nsteps = 1000;
  for i in range(nsteps):
    trainer.run(session = sess, feed_dict={});
    p1 = par.eval(session = sess);
    
    if i%10 == 0:
      plt.figure(10); plt.clf();
      w.center = p1[0];
      w.plot(image= imgt);
      plt.scatter(skels[0,:,0], skels[0,:,1], c= 'm')
      plt.xlim(0,151); plt.ylim(0,151)
      plt.draw();
      plt.pause(0.1);


  
  import shapely.geometry as geom
  cntrs = wgeo.contours_from_image(imgt)
  poly = geom.Polygon(cntrs[0], [cntrs[i] for i in range(1,len(cntrs))]);
  poly2 = poly.buffer(-3)
  bdr = poly.boundary;
  
  from descartes.patch import PolygonPatch
  patch = PolygonPatch(poly, facecolor = 'b', edgecolor='k', alpha=0.5, zorder=2)
  patch2= PolygonPatch(poly2, facecolor = 'r', edgecolor='k', alpha=0.5, zorder=2)
  
  fig = plt.figure(28);
  ax = fig.add_subplot(111)
  ax.add_patch(patch)
  ax.add_patch(patch2)
  plt.xlim(0,151); plt.ylim(1,151)
  plt.show()
  
  
  
  ### make boundary pts in tensorflow
  
  npoints = 21;
  width = tf.placeholder("float32", shape = [npoints,2]);