Skip to content
Snippets Groups Projects
Commit 85dedb8f authored by Angeline Aguinaldo's avatar Angeline Aguinaldo
Browse files

included random mean initialization for gmm training

parent d6a9aa10
Branches
No related tags found
No related merge requests found
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
<file leaf-file-name="PixelRep_Demo.py" pinned="false" current-in-tab="false"> <file leaf-file-name="PixelRep_Demo.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/PixelRep_Demo.py"> <entry file="file://$PROJECT_DIR$/PixelRep_Demo.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="162"> <state relative-caret-position="145">
<caret line="30" column="0" selection-start-line="30" selection-start-column="0" selection-end-line="60" selection-end-column="51" /> <caret line="35" column="32" selection-start-line="35" selection-start-column="32" selection-end-line="35" selection-end-column="32" />
<folding> <folding>
<element signature="e#1584#1618#0" expanded="true" /> <element signature="e#1584#1618#0" expanded="true" />
</folding> </folding>
...@@ -36,8 +36,8 @@ ...@@ -36,8 +36,8 @@
<file leaf-file-name="PIXEL_REPLICATION_LIB.py" pinned="false" current-in-tab="true"> <file leaf-file-name="PIXEL_REPLICATION_LIB.py" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py"> <entry file="file://$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="522"> <state relative-caret-position="58">
<caret line="86" column="0" selection-start-line="86" selection-start-column="0" selection-end-line="89" selection-end-column="23" /> <caret line="71" column="27" selection-start-line="71" selection-start-column="27" selection-end-line="71" selection-end-column="27" />
<folding /> <folding />
</state> </state>
</provider> </provider>
...@@ -46,8 +46,8 @@ ...@@ -46,8 +46,8 @@
<file leaf-file-name="GENERATE_ELLIPSE_LIB.py" pinned="false" current-in-tab="false"> <file leaf-file-name="GENERATE_ELLIPSE_LIB.py" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py"> <entry file="file://$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-84"> <state relative-caret-position="902">
<caret line="122" column="42" selection-start-line="122" selection-start-column="42" selection-end-line="122" selection-end-column="42" /> <caret line="117" column="45" selection-start-line="117" selection-start-column="45" selection-end-line="117" selection-end-column="45" />
<folding /> <folding />
</state> </state>
</provider> </provider>
...@@ -66,11 +66,11 @@ ...@@ -66,11 +66,11 @@
<option name="CHANGED_PATHS"> <option name="CHANGED_PATHS">
<list> <list>
<option value="$PROJECT_DIR$/PixelReplication.py" /> <option value="$PROJECT_DIR$/PixelReplication.py" />
<option value="$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py" />
<option value="$PROJECT_DIR$/TestScript.py" /> <option value="$PROJECT_DIR$/TestScript.py" />
<option value="$PROJECT_DIR$/PIXELREP_GUI_WRAP.py" /> <option value="$PROJECT_DIR$/PIXELREP_GUI_WRAP.py" />
<option value="$PROJECT_DIR$/PixelRep_Demo.py" />
<option value="$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py" /> <option value="$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py" />
<option value="$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py" />
<option value="$PROJECT_DIR$/PixelRep_Demo.py" />
</list> </list>
</option> </option>
</component> </component>
...@@ -381,13 +381,13 @@ ...@@ -381,13 +381,13 @@
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.0" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" /> <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.0" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" /> <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32921812" sideWeight="0.50208336" order="7" side_tool="true" content_ui="tabs" /> <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.32921812" sideWeight="0.50208336" order="7" side_tool="true" content_ui="tabs" />
<window_info id="Python Console" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.6515625" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" /> <window_info id="Python Console" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.6067708" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" /> <window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.23868313" sideWeight="0.49791667" order="2" side_tool="false" content_ui="tabs" /> <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.2489712" sideWeight="0.49791667" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" /> <window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" /> <window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" /> <window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.2654321" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" /> <window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.31995884" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" /> <window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" /> <window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" /> <window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
...@@ -410,7 +410,7 @@ ...@@ -410,7 +410,7 @@
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>
<option name="time" value="16" /> <option name="time" value="19" />
</breakpoint-manager> </breakpoint-manager>
<watches-manager /> <watches-manager />
</component> </component>
...@@ -543,16 +543,16 @@ ...@@ -543,16 +543,16 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py"> <entry file="file://$PROJECT_DIR$/GENERATE_ELLIPSE_LIB.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-84"> <state relative-caret-position="902">
<caret line="122" column="42" selection-start-line="122" selection-start-column="42" selection-end-line="122" selection-end-column="42" /> <caret line="117" column="45" selection-start-line="117" selection-start-column="45" selection-end-line="117" selection-end-column="45" />
<folding /> <folding />
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/PixelRep_Demo.py"> <entry file="file://$PROJECT_DIR$/PixelRep_Demo.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="162"> <state relative-caret-position="145">
<caret line="30" column="0" selection-start-line="30" selection-start-column="0" selection-end-line="60" selection-end-column="51" /> <caret line="35" column="32" selection-start-line="35" selection-start-column="32" selection-end-line="35" selection-end-column="32" />
<folding> <folding>
<element signature="e#1584#1618#0" expanded="true" /> <element signature="e#1584#1618#0" expanded="true" />
</folding> </folding>
...@@ -561,8 +561,8 @@ ...@@ -561,8 +561,8 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py"> <entry file="file://$PROJECT_DIR$/PIXEL_REPLICATION_LIB.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="522"> <state relative-caret-position="58">
<caret line="86" column="0" selection-start-line="86" selection-start-column="0" selection-end-line="89" selection-end-column="23" /> <caret line="71" column="27" selection-start-line="71" selection-start-column="27" selection-end-line="71" selection-end-column="27" />
<folding /> <folding />
</state> </state>
</provider> </provider>
......
...@@ -67,29 +67,35 @@ def PixelReplicate(bwim): ...@@ -67,29 +67,35 @@ def PixelReplicate(bwim):
ptsRep[int(nInsert):int(nInsert + nrep), 0:] = rp ptsRep[int(nInsert):int(nInsert + nrep), 0:] = rp
nInsert = nInsert + nrep nInsert = nInsert + nrep
return ptsRep return ptsRep, pts
def fitGMM(ptsRep, k): def fitGMM(ptsRep, k, pts):
from sklearn.mixture import GMM
import numpy as np import numpy as np
import cv2 import cv2
print("Fitting GMM...") print("Fitting GMM...")
bestGMM = []
bestLL = float("-inf")
uniqPts = pts
for i in np.arange(0, 10, 1):
rndIdx = np.random.randint(low=0, high=uniqPts.shape[0], size=(1, 1, k), dtype=int)
rndMeans = np.matrix(uniqPts[np.array(rndIdx), 0:][0][0])
gmm0 = cv2.ml.EM_create() gmm0 = cv2.ml.EM_create()
gmm0.setCovarianceMatrixType(2) gmm0.setCovarianceMatrixType(2)
gmm0.setClustersNumber(k) gmm0.setClustersNumber(k)
termCrit = (20, 400, 1e-15) termCrit = (1000, 1000, 1e-25)
gmm0.setTermCriteria(termCrit) gmm0.setTermCriteria(termCrit)
bestGMM = []
bestLL = float("-inf")
for i in np.arange(0, 10, 1):
print('Replicate: ', str(i+1)) print('Replicate: ', str(i+1))
newGMM = gmm0.trainEM(ptsRep) newGMM = gmm0.trainE(samples=ptsRep, means0=rndMeans)
LL = sum(newGMM[1]) LL = sum(newGMM[1])
print('Likelihood Logarithm of GMM fit = ', LL)
if LL > bestLL: if LL > bestLL:
bestGMM = gmm0 bestGMM = gmm0
bestLL = LL bestLL = LL
# gmm0 = GMM(n_components=k, covariance_type='full', n_iter=400, n_init=20, verbose=0, tol=1e-4, init_params='m')
# gmmfit = gmm0.fit(ptsRep)
return bestGMM return bestGMM
def drawClusters(gmm0, bwim, ptsEdge): def drawClusters(gmm0, bwim, ptsEdge):
......
...@@ -33,15 +33,14 @@ import GENERATE_ELLIPSE_LIB as GenEllipse ...@@ -33,15 +33,14 @@ import GENERATE_ELLIPSE_LIB as GenEllipse
# Generate random ellipse image # Generate random ellipse image
print("Generating Ellipse...") print("Generating Ellipse...")
K = 4 # number of ellipse to fit K = 3 # number of ellipse to fit
# bwim, trueBounds = GenEllipse.GetRandomEllipseImage(K) bwim, trueBounds = GenEllipse.GetRandomEllipseImage(K)
# # You may import an image for demo by uncommenting the following code: # # You may import an image for demo by uncommenting the following code:
import cv2 # import cv2
im = cv2.imread('U:\Angeline\PixelRep\Ellipse_k4.tif', cv2.CV_8UC1)
# im = cv2.imread('INSERT FILENAME', cv2.CV_8UC1) # im = cv2.imread('INSERT FILENAME', cv2.CV_8UC1)
th, bwim = cv2.threshold(im, 0, 1, cv2.THRESH_BINARY) # th, bwim = cv2.threshold(im, 0, 1, cv2.THRESH_BINARY)
trueBounds = [0, 0] # trueBounds = [0, 0]
# # (Remember to modify K number of ellipses) # # (Remember to modify K number of ellipses)
# # --------------------------------------------------------------------- # # ---------------------------------------------------------------------
...@@ -49,12 +48,12 @@ trueBounds = [0, 0] ...@@ -49,12 +48,12 @@ trueBounds = [0, 0]
# bwim only has 1 connected component. if bwim has multiple connected # bwim only has 1 connected component. if bwim has multiple connected
# components, process each one separately # components, process each one separately
ptsRep = PR.PixelReplicate(bwim) ptsRep, pts = PR.PixelReplicate(bwim)
# Fit GMM to PR points # Fit GMM to PR points
# NOTE - more replicates will be more accurate, but will take longer. # NOTE - more replicates will be more accurate, but will take longer.
gmmfit = PR.fitGMM(ptsRep, K) gmmfit = PR.fitGMM(ptsRep, K, pts)
# Draw clusters # Draw clusters
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment