Skip to content

Commit

Permalink
edits with Chee
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-kz committed Mar 1, 2025
1 parent 4a3ac0d commit ac5fa4f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 88 deletions.
43 changes: 22 additions & 21 deletions frame_2D_alg/agg_recursion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from itertools import zip_longest
from multiprocessing import Pool, Manager
from frame_blobs import frame_blobs_root, intra_blob_root, imread
from vect_edge import L2N, base_comp, sum_G_, comb_H_, sum_H, copy_, comp_node_, comp_link_, sum2graph, get_rim, CG, CLay, vectorize_root, extend_box, Val_, val_
from vect_edge import L2N, base_comp, sum_G_, comb_H_, sum_H, copy_, comp_node_, comp_link_, sum2graph, get_rim, CG, CLay, vect_root, extend_box, Val_, val_
'''
notation:
prefix f: flag
Expand Down Expand Up @@ -34,7 +34,8 @@
(which may include extending eval function with new match-projecting derivatives)
Similar to cross-projection by data-coordinate filters, described in "imagination, planning, action" section of part 3 in Readme.
'''
ave, ave_L, icoef, max_dist = 5, 1, 0.15, 2
w_ = np.ones(4) # higher-scope weights
ave, ave_L, max_dist, icoef = 5, 2, 5, .5

def cross_comp(root, fn, rc): # form agg_Level by breadth-first node_,link_ cross-comp, connect clustering, recursion
# rc: recursion count coef to ave
Expand All @@ -53,8 +54,8 @@ def cross_comp(root, fn, rc): # form agg_Level by breadth-first node_,link_ cro
derH[0] += [comb_H_(lL_, root, fd=1)] # += dlay
plL_ = {l for n in lN_ for l,_ in get_rim(n,fd=1)}
if len(plL_) > ave_L:
cluster_N_(root, plL_, ave*(rc+4), fd=1) # form altGs for cluster_C_, no new links between dist-seg Gs

cluster_N_(root, plL_, ave*(rc+4), fd=1)
# form altGs for cluster_C_, no new links between dist-seg Gs
root.derH += derH # feedback
comb_altG_(root.node_[-1].node_, ave*(rc+4)) # comb node contour: altG_ | neg links sum, cross-comp -> CG altG
cluster_C_(root, rc+5) # -> mfork G,altG exemplars, +altG surround borrow, root.derH + 1|2 lays, agg++
Expand All @@ -64,15 +65,15 @@ def cross_comp(root, fn, rc): # form agg_Level by breadth-first node_,link_ cro

def cluster_N_(root, L_, ave, fd): # top-down segment L_ by >ave ratio of L.dists

L_ = sorted(L_, key=lambda x: x.dist) # short links first
L_ = sorted(L_, key=lambda x: x.L) # short links first
min_dist = 0; Et = root.Et
while True:
# each loop forms G_ of contiguous-distance L_ segment
_L = L_[0]; N_, et = copy(_L.nodet), _L.Et
for n in [n for l in L_ for n in l.nodet]:
n.fin = 0
for i, L in enumerate(L_[1:], start=1):
rel_dist = L.dist/_L.dist # >= 1
rel_dist = L.L/_L.L # >= 1
if rel_dist < 1.2 or len(L_[i:]) < ave_L: # ~= dist Ns or either side of L is weak: continue dist segment
LV = Val_(et, Et, ave) # link val
_G,G = L.nodet # * surround density: extH (_Ete[0]/ave + Ete[0]/ave) / 2, after cross_comp:
Expand All @@ -82,7 +83,7 @@ def cluster_N_(root, L_, ave, fd): # top-down segment L_ by >ave ratio of L.dis
else:
i -= 1; break # terminate contiguous-distance segment
G_ = []
max_dist = _L.dist
max_dist = _L.L
for N in {*N_}: # cluster current distance segment
if N.fin: continue # clustered from prior _N_
_eN_,node_,link_,et, = [N],[],[], np.zeros(4)
Expand All @@ -93,7 +94,7 @@ def cluster_N_(root, L_, ave, fd): # top-down segment L_ by >ave ratio of L.dis
for L,_ in get_rim(eN, fd): # all +ve
if L not in link_:
eN_ += [n for n in L.nodet if not n.fin]
if L.dist < max_dist:
if L.L < max_dist:
link_+=[L]; et+=L.Et
_eN_ = {*eN_}
link_ = list({*link_}); Lay = CLay()
Expand Down Expand Up @@ -295,7 +296,7 @@ def agg_H_par(focus): # draft parallel level-updating pipeline

frame = frame_blobs_root(focus)
intra_blob_root(frame)
vectorize_root(frame)
vect_root(frame)
if frame.node_: # converted edges
G_ = []
for edge in frame.node_:
Expand All @@ -311,22 +312,22 @@ def agg_H_par(focus): # draft parallel level-updating pipeline

frame.aggH = list(H) # convert back to list

def agg_H_seq(focus, image, _nestt=(1,0), _rM=0, _rV_t=[]): # recursive level-forming pipeline, called from cluster_C_

global ave, ave_L, icoef, max_dist, ave_w, ave_L_w, icoef_w, max_dist_w # add weight per global
if _rM:
ave *= ave_w*_rM; ave_L *= ave_L_w*_rM; icoef *= icoef_w*_rM; max_dist *= max_dist_w*_rM
# fb _rM only
frame = frame_blobs_root(focus, _rM)
def agg_H_seq(focus, image, _nestt=(1,0), _rM=1, _rv_t=[]): # recursive level-forming pipeline, called from cluster_C_

global ave, ave_L, icoef, max_dist # adjust cost params
ave, ave_L, icoef, max_dist = np.array([ave, ave_L, icoef, max_dist]) * (w_ * _rM)

frame = frame_blobs_root(focus, _rM) # no _rv_t
intra_blob_root(frame, _rM) # not sure
vectorize_root(frame, _rM, _rV_t)
vect_root(frame, _rM, _rv_t)
if not frame.nnest:
return frame
comb_altG_(frame.node_[-1].node_, ave*2) # PP graphs in frame.node_[2]
# forward agg+:
cluster_C_(frame, rc=1) # ave *= recursion count
rM,rD = 1,1 # sum derTT coefs: m_,d_ [M,D,n,o I,G,A,L] / Et, baseT, dimension
rV_t = np.ones((2,8)) # d value is borrowed from corresponding ms in proportion to d mag, both scaled by fb
rM,rD = 1,1 # sum derTT coefs: m_,d_ [M,D,n,o, I,G,A,L] / Et, baseT, dimension
rv_t = np.ones((2,8)) # d value is borrowed from corresponding ms in proportion to d mag, both scaled by fb
# feedback to scale m,d aves:
for fd, nest,_nest,Q in zip((0,1), (frame.nnest,frame.lnest), _nestt, (frame.node_[2:],frame.link_[1:])): # skip blob_,PP_,link_PP_
if nest==_nest: continue # no new nesting
Expand All @@ -335,7 +336,7 @@ def agg_H_seq(focus, image, _nestt=(1,0), _rM=0, _rV_t=[]): # recursive level-f
_m,_d,_n,_ = hG.Et; m,d,n,_ = lev_G.Et
rM += (_m/_n) / (m/n) # no o eval?
rD += (_d/_n) / (d/n)
rV_t += np.abs((hG.derTT/_n) / (lev_G.derTT/n))
rv_t += np.abs((hG.derTT/_n) / (lev_G.derTT/n))
hG = lev_G
if rM > ave: # base-level
base = frame.node_[2]; Et,box,baseT = base.Et, base.box, base.baseT
Expand All @@ -346,8 +347,8 @@ def agg_H_seq(focus, image, _nestt=(1,0), _rM=0, _rV_t=[]): # recursive level-f
y,x,Y,X = box # current focus?
y = y+dy; x = x+dx; Y = Y+dy; X = X+dx # alter focus shape, also focus size: +/m-, res decay?
if y > 0 and x > 0 and Y < image.shape[0] and X < image.shape[1]: # focus is inside the image
# rerun agg+ with new bottom-level focus, aves:
agg_H_seq(image[y:Y,x:X], image, (frame.nnest,frame.lnest), rM, rV_t)
# rerun agg+ with new focus and aves:
agg_H_seq(image[y:Y,x:X], image, (frame.nnest,frame.lnest), rM, rv_t)

return frame

Expand Down
32 changes: 12 additions & 20 deletions frame_2D_alg/comp_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
len prior root_ sorted by G is root.olp, to eval for inclusion in PP or start new P by ave*olp
'''

# module-specific:
ave, ave_d, ave_G, ave_PPm, ave_PPd, ave_L, ave_dI, mw_, dw_ =5, 10, 100, 50, 50, 4, 20, np.ones(6), np.ones(6)
w_ = np.ones(12) # higher-scope ave weights, dw_ = w_/ 2?
ave_md = [ave,ave_d]

class CdP(CBase): # produced by comp_P, comp_slice version of Clink
Expand All @@ -50,30 +50,22 @@ def __init__(l, nodet, span, angle, yx, Et, vertuple, latuple=None, root=None):
l.prim = []
def __bool__(l): return l.nodet

def vectorize_root(frame, _fb_={}, fb_={}):

if _fb_: # update ave based on feedback coefficients
global ave, ave_d, ave_G, ave_PPm, ave_PPd, ave_L, ave_dI, mw_, dw_, ave_md
ave *= _fb_['vect_edge'] # get feedback coefficient from comp_slice
ave_d *= _fb_['vect_edge']
ave_G *= _fb_['vect_edge']
ave_PPm *= _fb_['vect_edge']
ave_PPd *= _fb_['vect_edge']
ave_L *= _fb_['vect_edge']
ave_dI *= _fb_['vect_edge']
mw_ *= _fb_['vect_edge']
dw_ *= _fb_['vect_edge']
fb_['slice_edge'] = (ave+ave_d+ave_G+ave_PPm+ave_PPd+ave_L+ave_dI+sum(mw_)+sum(dw_))/23 # feedback coefficient to the next slice_edge
def vectorize_root(frame, W=1, ww_t_=[]):

blob_ = unpack_blob_(frame)
for blob in blob_:
if not blob.sign and blob.G > ave_G * blob.root.olp:
edge = slice_edge(blob)
if edge.G * (len(edge.P_) - 1) > ave_PPm: # eval PP, olp=1
comp_slice(edge)
comp_slice(edge, W, ww_t_)

def comp_slice(edge): # root function
def comp_slice(edge, W, ww_t): # root function

global ave, avd, aI, aG, aA, aL, ave_L, ave_PPm, ave_PPd
ave_L, ave_PPm, ave_PPd = np.array([ave_L, ave_PPm, ave_PPd]) * (w_ * W)
if ww_t:
ave, avd, aI, aG, aA, aL = np.array([ave, avd, aI, aG, aA, aL]) * (ww_t[0][:2]+ww_t[0][4:8]) # match eval only?
# ders
edge.Et, edge.vertuple = np.zeros(4), np.array([np.zeros(6), np.zeros(6)]) # (M, D, n, o), (m_,d_)
for P in edge.P_: # add higher links
P.vertuple = np.array([np.zeros(6), np.zeros(6)])
Expand Down Expand Up @@ -193,9 +185,9 @@ def comp_latuple(_latuple, latuple, _n,n): # 0der params, add dir?
I*=rn; dI = _I - I; mI = ave_dI -dI / max(_I,I, 1e-7) # vI = mI - ave
G*=rn; dG = _G - G; mG = min(_G, G) / max(_G,G, 1e-7) # vG = mG - ave_mG
M*=rn; dM = _M - M; mM = min(_M, M) / max(_M,M, 1e-7) # vM = mM - ave_mM
D*=rn; dD = _D - D; mD = min(_D, D) / max(_D,D, 1e-7) # vD = mD - ave_mD
L*=rn; dL = _L - L; mL = min(_L, L) / max(_L,L) # vL = mL - ave_mL
mA, dA = comp_angle((_Dy,_Dx),(Dy,Dx)) # vA = mA - ave_mA, normalized
D*=rn; dD = _D - D; mD = min(_D, D) / max(_D,D) if _D or D else 1e-7 # may be negative
L*=rn; dL = _L - L; mL = min(_L, L) / max(_L,L)
mA, dA = comp_angle((_Dy,_Dx),(Dy,Dx)) # normalized

d_ = np.array([dI, dG, dA, dM, dD, dL]) # derTT[:3], Et
m_ = np.array([mI, mG, mA, mM, mD, mL])
Expand Down
16 changes: 6 additions & 10 deletions frame_2D_alg/frame_blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,10 @@ def G(blob): return blob.latuple[-1]
@property
def yx_(blob): return list(blob.dert_.keys())

def frame_blobs_root(image, _fb_={}):
if _fb_: # update ave based on feedback coefficients
global ave
ave *= _fb_['frame_blobs'] # get feedback coefficient from intra_blob

def frame_blobs_root(image, W=1):
global ave
ave *= W
dert__ = comp_pixel(image)
frame = CFrame(image)
flood_fill(frame, dert__) # flood-fill 1 pixel at a time
Expand Down Expand Up @@ -185,13 +184,10 @@ def __init__(rnode_, blob):
rnode_.olp= blob.root.olp + 1.5
rnode_.rng = blob.root.rng + 1

def intra_blob_root(frame, _fb_={}, fb_={}):

if _fb_: # update ave based on feedback coefficients
global aveR
aveR *= _fb_['intra_blob'] # get feedback coefficient from slice_edge
fb_['frame_blobs'] = aveR # feedback coefficient to the next frame_blobs
def intra_blob_root(frame, W=1):

global aveR
aveR *= W
frame.olp = frame.rng = 1
for blob in frame.blob_:
rblob(blob)
Expand Down
18 changes: 7 additions & 11 deletions frame_2D_alg/slice_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,17 @@ def __init__(P, yx, axis):
P.dert_ = []
P.latuple = None # I,G, M,D, L, [Dy, Dx]

def vectorize_root(frame):
def vectorize_root(frame, W=1, w_=[]):

blob_ = unpack_blob_(frame)
for blob in blob_:
if not blob.sign and blob.G > frame.ave.G:
slice_edge(blob, frame.ave)
if not blob.sign and blob.G > ave_G * blob.n * W:
slice_edge(blob, w_)

def slice_edge(edge, _fb_={}, fb_={}):
def slice_edge(edge, w_=[]):

if _fb_: # update ave based on feedback coefficients
global ave_I, ave_G, ave_dangle
ave_I *= _fb_['slice_edge'] # get feedback coefficient from comp_slice
ave_G *= _fb_['slice_edge']
ave_dangle *= _fb_['slice_edge']
fb_['intra_blob'] = (ave_I+ave_G+ave_dangle)/3 # feedback coefficient to the next intra_blob (sum them? or return an array? But we only need 1)
global ave_I, ave_G, ave_dangle # +w / ave_L, etc?
ave_I, ave_G, ave_dangle = np.array([ave_I, ave_G, ave_dangle]) * w_

axisd = select_max(edge)
yx_ = sorted(axisd.keys(), key=lambda yx: edge.dert_[yx][-1]) # sort by g
Expand Down Expand Up @@ -95,7 +91,7 @@ def form_P(P, edge):
if mangle < ave_dangle: break # terminate P if angle miss
m = min(_i,i) + min(_g,g) + mangle
d = abs(-i-i) + abs(_g-g) + dangle
if m < ave_I + ave_G + ave_dangle: break # need separate weights? terminate P if total miss, blob should be more permissive than P
if m < ave_I + ave_G + ave_dangle: break # terminate P if total miss, blob should be more permissive than P
# update P:
edge.rootd[ky, kx] = P
I+=i; Dy+=dy; Dx+=dx; G+=g; M+=m; D+=d; L+=1
Expand Down
Loading

0 comments on commit ac5fa4f

Please sign in to comment.