Skip to content

Commit

Permalink
edits in comp_blob_recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
boris-kz committed May 1, 2021
1 parent c66e0ca commit 8248829
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
74 changes: 38 additions & 36 deletions frame_2D_alg/comp_blob_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class CderBlob(ClusterStructure):

blob = object # not needed, common for all derBlobs in derBlob_?
_blob = object
mB = int
dB = int
Expand All @@ -20,16 +19,13 @@ class CderBlob(ClusterStructure):
mG = int
dM = int
mM = int
# DerBlob only, do not accumulate:
neg_mB = int
distance = int

class CBblob(ClusterStructure):

DerBlob = object
blob_ = list

ave_mB = -20000
ave_mB = 20000 # ave can't be negative
ave_rM = .7 # average relative match at rL=1: rate of ave_mB decay with relative distance, due to correlation between proximity and similarity


Expand All @@ -40,8 +36,9 @@ def cross_comp_blobs(frame):
blob_ = frame.blob_

for blob in blob_: # each blob forms derBlob per compared adj_blob and accumulates adj_blobs'derBlobs:
comp_blob_recursive(blob, blob.adj_blobs[0], derBlob_=[], derBlob_id_=[])

This comment has been minimized.

Copy link
@kwcckw

kwcckw May 1, 2021

why we apply comp_blob_recursive first before initialize the blob.DerBlob? Wouldn't this wipe out the accumulated blob.DerBlob from the comp_blob_recursive if we initialize it afterwards?

This comment has been minimized.

Copy link
@boris-kz

boris-kz May 1, 2021

Author Owner

Right, sorry.

# derBlob_ and derBlob_id_ are local and frame-wide
blob.DerBlob = CderBlob()
comp_blob_recursive(blob, blob.adj_blobs[0], comped_id_=[blob.id])

bblob_ = form_bblob_(blob_) # form blobs of blobs, connected by mutual match

Expand All @@ -50,29 +47,38 @@ def cross_comp_blobs(frame):
return bblob_


def comp_blob_recursive(blob, adj_blob_, comped_id_):
def comp_blob_recursive(blob, adj_blob_, derBlob_, derBlob_id_):
'''
called by cross_comp_blob to recursively compare blob to adj_blobs in incremental layers of adjacency
'''
for adj_blob in adj_blob_:
if adj_blob.id in comped_id_: # this is ids of blobs compared to immediate adjacents?
# pseudo for assign checked id' derBlob as current derBlob:
derBlob = @ adj_blob.id
accum_derBlob(blob, derBlob)
else:
derBlob = comp_blob(blob, adj_blob) # compare blob and adjacent blob
accum_derBlob(blob, derBlob) # from all compared blobs, regardless of mB sign
comped_id_.append(adj_blob.id)

if derBlob.mB > 0: # replace blob with adj_blob for continuing adjacency search:
adj_blob.DerBlob = CderBlob()
comp_blob_recursive(adj_blob, adj_blob.adj_blobs[0], comped_id_)
else:
blob.DerBlob.neg_mB += derBlob.mB # mB accumulated over comparison scope
blob.DerBlob.distance += np.sqrt(adj_blob.A)

if blob.Dert.M + blob.DerBlob.neg_mB > ave_mB: # negative mB, extend blob comparison to adjacents of adjacent, depth-first
comp_blob_recursive(blob, adj_blob.adj_blobs[0], comped_id_)
derBlob_id = adj_blob.id * blob.id # unique comparand_pair identifier, frame-wide, same for derBlob_?

This comment has been minimized.

Copy link
@kwcckw

kwcckw May 1, 2021

Looks like a good idea to use product in representing the unique blob pair ids, we should do this to derBlob_ too.

This comment has been minimized.

Copy link
@kwcckw

kwcckw May 1, 2021

i checked and found out it may not be a good idea to use their product, since multiple numbers able to yield the same product, for example:

4 * 10 = 40
8 * 5 = 40

I just pushed the edits to use pairing function in unique number generation.

This comment has been minimized.

Copy link
@boris-kz

boris-kz May 1, 2021

Author Owner

Thanks! I realized that too, later last night.

fnew = 1 # new comparand pair

for i, id in enumerate(derBlob_id_): # is there a shortcut for finding index of a known value, this could be very slow?

This comment has been minimized.

Copy link
@kwcckw

kwcckw May 1, 2021

Yea, but with some extra steps:

if id in derBlob_id_:
    index = derBlob_id_.index(id)
if id==derBlob_id:
derBlob = derBlob_[i]
accum_derBlob(blob, derBlob) # also adj_blob.rdn += 1: redundancy coeff?

This comment has been minimized.

Copy link
@kwcckw

kwcckw May 1, 2021

blob.rdn is used in intra_blob to adjust the aves, so we will use the rdn in comp_blob too?

This comment has been minimized.

Copy link
@boris-kz

boris-kz May 1, 2021

Author Owner

I don't have any uses for it yet.

This comment has been minimized.

Copy link
@kwcckw

kwcckw May 1, 2021

okay, then it would be not necessary to accumulate the rdn for now.

fnew=0
break
if fnew:
if adj_blob is not blob:
derBlob = comp_blob(blob, adj_blob) # compare blob and adjacent blob
accum_derBlob(blob, derBlob) # from all compared blobs, regardless of mB sign
derBlob_id_.append(adj_blob.id * blob.id) # unique comparand_pair identifier
derBlob_.append(derBlob) # also frame-wide

if derBlob.mB > 0:
# replace blob with adj_blob for continuing adjacency search:
if not isinstance(adj_blob.DerBlob, CderBlob): # do we really need this?

This comment has been minimized.

Copy link
@kwcckw

kwcckw May 1, 2021

this is to prevent never ending loop. So if the blob is initialized with DerBlob, they should have performed the search before (maybe in the previous call of function) , so do we really need to search again from here?

This comment has been minimized.

Copy link
@boris-kz

boris-kz May 1, 2021

Author Owner

Yes, because search is asymmetric here, depending on blob value. So some blobs will search deeper than others.

This comment has been minimized.

Copy link
@kwcckw

kwcckw May 1, 2021

if that is the case, probably we shouldn't reinit the adj_blob.DerBlob if there is existing adj_blob.DerBlob?

This comment has been minimized.

Copy link
@boris-kz

boris-kz May 1, 2021

Author Owner

Right, we will just keep accumulating it.

adj_blob.DerBlob = CderBlob()
comp_blob_recursive(adj_blob, adj_blob.adj_blobs[0], derBlob_, derBlob_id_)

elif blob.Dert.M + blob.neg_mB+ derBlob.mB > ave_mB: # neg mB but positive comb M,
# extend blob comparison to adjacents of adjacent, depth-first
blob.neg_mB += derBlob.mB # mB and distance are accumulated over comparison scope
blob.distance += np.sqrt(adj_blob.A)
comp_blob_recursive(blob, adj_blob.adj_blobs[0], derBlob_, derBlob_id_)


def comp_blob(blob, _blob):
Expand All @@ -91,11 +97,11 @@ def comp_blob(blob, _blob):
dM = _M - M
mM = min(_M, M)

mB = mI + mA + mG + mM - ave_mB * (ave_rM ** ((1+blob.DerBlob.distance) / np.sqrt(A)))
mB = mI + mA + mG + mM - ave_mB * (ave_rM ** ((1+blob.distance) / np.sqrt(A)))
# deviation from average blob match at current distance
dB = dI + dA + dG + dM

derBlob = CderBlob(blob=blob, _blob=_blob, mB=mB, dB=dB) # blob is core node, _blob is adjacent blob
derBlob = CderBlob(_blob=_blob, mB=mB, dB=dB) # blob is core node, _blob is adjacent blob

if _blob.fsliced and blob.fsliced:
pass
Expand Down Expand Up @@ -127,17 +133,13 @@ def form_bblob_recursive(bblob_, bblob, checked_ids):
for blob in bblob.blob_: # search blob' derBlob's blobs to get potential border clustering blob
if (blob.DerBlob.mB > 0) and (blob.id not in checked_ids): # positive mB
for derBlob in blob.derBlob_:
# if derBlob.blob is in bblob.blob_, but derBlob._blob is not in bblob_blob_
# if blob is in bblob.blob_, but derBlob._blob is not in bblob_blob_
# so if sum of derBlob._blob's mB with blob's mB > 0 , pack the derBlob._blob into bblob.blob_
if (derBlob.blob in bblob.blob_) and (derBlob._blob.DerBlob.mB + blob.DerBlob.mB >0) and (derBlob._blob not in bblob.blob_):
merge_blob = derBlob._blob
fsearch = 1
if (derBlob._blob in bblob.blob_) and (derBlob._blob.DerBlob.mB + blob.DerBlob.mB >0) and (derBlob.blob not in bblob.blob_):
merge_blob = derBlob.blob
fsearch = 1
if fsearch:
accum_bblob(bblob, merge_blob)
if (derBlob._blob not in bblob.blob_) and (derBlob._blob.DerBlob.mB + blob.DerBlob.mB >0):
accum_bblob(bblob, derBlob._blob)
checked_ids.append(blob.id)
fsearch = 1

if fsearch:
form_bblob_recursive(bblob_, bblob, checked_ids)

Expand Down
11 changes: 9 additions & 2 deletions frame_2D_alg/frame_blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ class CBlob(ClusterStructure):
derPd__ = list
Pd__ = list

# comp blobs
DerBlob = object
derBlob_ = list
distance = int # common per derBlob_
neg_mB = int # common per derBlob_


# draft
def comp_pixel_hybrid(image): # 3x3 kernel M and 2x2 quadrant G, see comp_pixel_versions file for other versions and more explanation
'''
Expand Down Expand Up @@ -185,7 +192,7 @@ def derts2blobs(dert__, verbose=False, render=False, use_c=False):
frame, idmap, adj_pairs = wrapped_flood_fill(dert__)
else:
blob_, idmap, adj_pairs = flood_fill(dert__, sign__=dert__[3] > 0, verbose=verbose)
I = Dy = Dx = G = M = 0
I, Dy, Dx, G, M = 0, 0, 0, 0, 0
for blob in blob_:
I += blob.Dert.I
Dy += blob.Dert.Dy
Expand Down Expand Up @@ -422,7 +429,7 @@ def check_deep_blob(deep_layer,i):
print_deep_blob_forking(deep_layers)
print("\rFinished intra_blob")

cross_comp_blobs(frame.blob_)
bblob_ = cross_comp_blobs(frame)

end_time = time() - start_time

Expand Down

0 comments on commit 8248829

Please sign in to comment.