+
    Qók°"  ã                   óH  € R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	t	^ RI
t^ RIt^ RIHu Ht ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ]! ]4      P9                  4       P:                  ^,          t]R,          t]! R	4      t R
 t!R t"]#R8X  d
   ]"! 4        R# R# )uI   Train a point-prompted decoder on consumed GÃ¼ndÃ¼z component candidates.N)ÚPath)Ú	Predictor)ÚLOGGER)ÚGunduzExpansionAssets)ÚGunduzSpeciesAssets)Úsha256zresults/r117z/tmp/mobile_sam.ptc                óX  € \         P                  ! V 4      pVP                  w  r4VP                  W438X  d   VP                  4       '       g   Q h\        P
                  ! VP                  R4      \        P                  ^4      p\         P                  ! \        \         P                  ! V4      4      VP                  4      w  rgWV3,          '       g   Q h\        RR\        WC4      ,          4      p\        ^\        WH,          4      4      \        ^\        W8,          4      4      r©RV	,
          ^,          RV
,
          ^,          rËWš3WC38X  d   TM'\        P                  ! W)V
3\        P                  R7      p\        P                  ! VP                  R4      Wš3\        P                   R7      p\         P"                  ! R
\        \         P$                  ! V4      4      \         P&                  4      p\         P(                  ! R
\         P&                  4      pWßWÌV
,           1W»V	,           13&   VVWÌV
,           1W»V	,           13&   V\+        V4      V	,          V,          ,           V\+        V4      V
,          V,          ,           .pVVVR\        V4      \        V4      .RWC.RWš.RW¼.R	V/3# )zEReference-derived interior click and median-padded 512px source crop.Úuint8g      ð?é   )ÚinterpolationÚsource_point_xyÚsource_size_xyÚresized_size_xyÚ
padding_xyÚinference_point_xy)r
   r
   )ÚnpÚasarrayÚshapeÚanyÚcv2ÚdistanceTransformÚastypeÚDIST_L2Úunravel_indexÚintÚargmaxÚminÚmaxÚroundÚresizeÚ
INTER_AREAÚINTER_NEARESTÚfullÚmedianr	   ÚzerosÚfloat)ÚimageÚmaskÚgrayÚhÚwÚdistanceÚyÚxÚscaleÚiwÚihÚoxÚoyÚsourceÚlabelÚcanvasÚ	full_maskÚpoints   &&                ÚN/home/mchadwick/src/ansp/diatom-research/research/r117_source_decoder_train.pyÚpreparer9      s
  € ä�:Š:�eÓ€DØ�:‰:�D€AØ�:‰:˜!˜Ô D§H¡H§J¢JÐ.Ð.Ü×$Ò$ T§[¡[°Ó%9¼3¿;¹;ÈÓJ€HÜ×ÒœC¤§	¢	¨(Ó 3Ó4°h·n±nÓE�D€AØ�1��:Œ:Ðˆ:Ü��Cœ#˜a›)•OÓ$€EÜ�”E˜!�)Ó$Ó%¤s¨1¬e°AµIÓ.>Ó'?ˆØ�B�h˜1�_˜s R�x¨A�oˆØ�X ! Ô'‰T¬S¯ZªZ¸À2¸hÔVY×VdÑVdÔ-e€FÜ�JŠJ�t—{‘{ 7Ó+¨b¨XÄS×EVÑEVÔW€EÜ�WŠW�Z¤¤R§Y¢Y¨t£_Ó!5´r·x±xÓ@€FÜ—’˜¤R§X¡XÓ.€IØ!'ˆ2��eˆ8�R˜2��XÐÑØ$)€Iˆb�B•ˆh˜˜b�5˜Ð Ñ!Ø”%˜“(˜R•- !Õ#Õ# R¬%°«(°R­-¸!Õ*;Õ%;Ð<€EØ�9˜eÐ&7¼#¸a»&Ä#ÀaÃ&Ð9IØ%5¸°vÐ?PÐSUÐRZØ%1°B°8Ð=QÐSXð&Zð Zð Zó    c                  óŠ  € \         P                  ! 4       p \        P                  ! R RR7      P	                  ^\
        P                  4      ^ 8X  g   Q hV \         P                  ! 4       8w  d   \        R4      h\        P                  ! ^4       \        P                  ! ^4       \        P                  ! R4       \        P                  ! R4      p\        P                   ! R4       \"        P%                  RR7       \"        R,          P'                  4       '       d   Q h\)        R4      p\+        R4      p\-        V4      ^8X  d   \-        V4      ^8X  g   Q hVP.                   Uu0 uF  qDR	,          kK  	  ppVP.                   Uu0 uF  qDR	,          kK  	  ppWV,          '       d   Q h. RNOpR
RRV Uu/ uF  qˆ\1        \2        V,          4      bK  	  upR\1        \4        4      RRRVP.                   Uu. uF  qDR,          NK  	  upRVP.                   Uu. uF  qDR,          NK  	  upRRRRRRRRRR/p	\"        R,          P7                  \8        P:                  ! V	^R7      R,           4       \<        P>                  ! 4       p
\A        R\C        \4        4      RR R!R"R#R$R%R&R'RR(R)R*R)R+R,/	R-7      p. . rÜ\E        VP.                  4       EF«  w  rïW.,          p\G        VR.,          VR/,          4      w  ppppVPI                  \        PJ                  ! V\        PL                  4      4       VPN                  PQ                  4       PS                  4       p\        PT                  ! VPW                  R04      4      RO,          p\X        PZ                  ! VRPR1R27      pVP]                  4       '       d   V^ 8H  P]                  4       '       g   Q hVP_                  R3VR4VR5\        P`                  ! V..\        Pb                  R67      R7\        P`                  ! ^..\        Pd                  R67      /4       VP_                  RVR,          R	VR	,          R8VR9,          R:\g        VPi                  4       4      /VC4       EK®  	  VPj                  pVPm                  4        F  pVPo                  R)4       K  	  VPp                  Pm                  4        F  pVPo                  R4       K  	  VPs                  4        VPp                  Pu                  4        \        Pv                  Py                  VPp                  Pm                  4       R;R,R<7      p. p\{        ^4       EFM  p\}        \{        \-        V4      4      4      pVP                  V4       . pV EF¼  pWÎ,          p\        P€                  ! 4       ;_uu_ 4        VPƒ                  VR5,          VR7,          3R R R=7      w  p p!VP‚                  P…                  4       p"R R R 4       VPq                  VR3,          X"X X!R)R>7      w  p#p$VR4,          pV#P†                  VP†                  8X  g   Q h\X        Pˆ                  ! V#V4      p%V#P‹                  4       p&^^V&V,          Pi                  4       ,          ^,           V&Pi                  4       VPi                  4       ,           ^,           ,          ,
          p'V%V',           p(\        PŒ                  ! V(4      '       g   Q hVP�                  4        V(P‘                  4        VP“                  4        VP_                  \•        V(PS                  4       4      4       EK¿  	  VP_                  \•        \–        P˜                  ! V4      4      4       \›        R?V^,           R@\�        VRQ,          ^4      RRA7       EKP  	  \"        RB,          p)\        Pž                  ! VPp                  P¡                  4       V)4       \"        RC,          P7                  \8        P:                  ! R
RDRE\1        \"        R,          4      RFV)P¢                  RG\1        V)4      RH\-        V4      RI^RJ^\-        V4      ,          RKVRLVRM\�        \<        P>                  ! 4       V
,
          ^4      RV	R,          /^R7      R,           4       R # u upi u upi u upi u upi u upi   + '       g   i     ELË; i)RNT)Ú	use_errnozParent exitedi:â ÚERROR)Úexist_okzprotocol.jsonÚconsumed_developmentÚsource_image_idÚformatzr117-source-decoder-protocol-v1Úinputs_sha256Úmobile_sam_sha256ÚseedÚ	train_idsÚidÚcomparison_idsÚ	selectiona  All 26 previously consumed source components train the decoder. All 25 previously consumed expansion components are a separate-image development comparison, not a final holdout; source image IDs do not overlap and a coarse similarity screen was already applied.Ú
preprocessuë   Native grayscale crop, downsize to fit a 512Ã—512 median-padded canvas. Positive click is the maximum-distance interior point from the source component mask. This is privileged reference-derived input, not automatic target acquisition.Útraininguc  Initialize the public MobileSAM checkpoint; freeze image and prompt encoders, cache 26 embeddings, train mask decoder only for 3 epochs using AdamW learning rate 1e-5, zero weight decay, binary cross entropy plus soft Dice on 128Ã—128 nearest-resampled target mask. Zero outside the target means not this queried instance, not semantic absence of diatoms.Ú
comparisonzýFixed original and adapted decoder on all 25 comparison components, identical reference-derived click and preprocessing; compute component IoU per object and counts at 0.5 and 0.75. Save all masks and predictions; no model selection based on comparison.ÚwarningaÉ  Both pools were previously inspected and used for synthetic development. Source labels and structurally inferred component associations are not independently adjudicated; near-duplicate screen cannot establish sample, site or pretraining independence. Scale and optics unknown. Oracle positive clicks and crops, plus unlabelled neighboring organisms, make this a development proxy, not automatic Academy or true overlap performance. Dataset CC BY-NC-SA 4.0.)ÚindentÚ
ÚmodelÚtaskÚsegmentÚmodeÚpredictÚimgszr
   ÚdeviceÚcpuÚretina_masksÚverboseFÚsaveÚconfg        )Ú	overridesr&   Úsource_component_maskÚfloat32Únearest)ÚsizerR   ÚfeaturesÚtargetÚpoints)ÚdtypeÚlabelsÚsource_mask_sha256Úmask_sha256Útarget_pixels_128gñhãˆµøä>)ÚlrÚweight_decay)rb   ÚboxesÚmasks)Úimage_embeddingsÚimage_peÚsparse_prompt_embeddingsÚdense_prompt_embeddingsÚmultimask_outputÚepochz/ 3; mean loss)Úflushz
decoder.ptztraining.jsonzr117-source-decoder-training-v1Úprotocol_sha256Ú
checkpointÚcheckpoint_sha256ÚsamplesÚepochsÚupdatesÚmean_epoch_lossesÚtraining_geometryÚelapsed_seconds)z.results/r112/gunduz-species-assets/assets.jsonz.results/r112/gunduz-species-assets/verify.jsonz0results/r112/gunduz-expansion-assets/assets.jsonz0results/r112/gunduz-expansion-assets/verify.jsonz,diatom_pilot/gunduz_species_asset_dataset.pyz.diatom_pilot/gunduz_expansion_asset_dataset.pyz%research/r117_source_decoder_train.pyz(research/r117_source_decoder_evaluate.py)NN)é€   r|   éÿÿÿÿ)RÚosÚgetppidÚctypesÚCDLLÚprctlÚsignalÚSIGTERMÚRuntimeErrorr   ÚsetNumThreadsÚtorchÚset_num_threadsÚmanual_seedÚrandomÚRandomr   ÚsetLevelÚOÚmkdirÚexistsr   r   ÚlenÚrecordsr   ÚRÚWÚ
write_textÚjsonÚdumpsÚtimeÚ	monotonicr   ÚstrÚ	enumerater9   Ú	set_imageÚcvtColorÚCOLOR_GRAY2BGRr`   ÚcloneÚdetachÚ
from_numpyr   ÚFÚinterpolater   ÚappendÚtensorr]   Úint64r   ÚsumrO   Ú
parametersÚrequires_grad_Úmask_decoderÚevalÚtrainÚoptimÚAdamWÚrangeÚlistÚshuffleÚno_gradÚprompt_encoderÚget_dense_per   Ú binary_cross_entropy_with_logitsÚsigmoidÚisfiniteÚ	zero_gradÚbackwardÚstepr%   r   ÚmeanÚprintr   rY   Ú
state_dictÚname)*ÚparentÚrngr«   ÚcompareÚrÚtrain_imagesÚcompare_imagesÚinputsÚfÚprotocolÚstartÚ	predictorrv   ÚgeometryÚiÚmetaÚitemr5   r'   r7   Úgeomr`   Úmask_tensorra   rO   ÚparamÚ	optimizerÚepoch_lossesrq   ÚorderÚlossesÚsampleÚsparseÚdenseÚpeÚlogitsÚ_ÚbceÚprobabilityÚdiceÚlossrt   s*                                             r8   ÚmainrÞ   1   sL  € Ü�ZŠZ‹\€FÜ�;Š;�t tÔ,×2Ñ2°1´f·n±nÓEÈÔJÐJÐJØ”—’“ÔÜ˜?Ó+Ð+Ü×Ò�aÔÜ	×Ò˜!ÔÜ	×Ò�gÔÜ
�-Š-˜Ó
 €CÜ
‡O‚O�GÔÜ‡G�G�T€GÔÜ�OÕ#×+Ñ+×-Ò-Ð-Ð-ÜÐ 6Ó7€EÜ#Ð$:Ó;€GÜˆu‹:˜Ô¤ G£°Ô 2Ð2Ð2Ø27·-²-Ó@±-¨QÐ'×(Ð(±-€LÐ@Ø4;·O²OÓD±O¨qÐ)×*Ð*±O€NÐDØ×,Ô,Ð,Ð,òc€Fð Ð;¸_ÑioÓNpÑioÐdeÔRXÔYZÐ]^ÕY^ÓR_ÒO_ÑioÑNpØ#¤V¬A£Y°¸Ø¨u¯}ª}Ó=©}¨! Ÿg˜g©}Ñ=Ð?OÐcj×crÒcrÓQsÑcrÐ^_ÐTX×RYÐRYÑcrÑQsØð  eØð  LØð  BØð  ^Øð  gðh€Hô ˆÕ×$Ñ$¤T§Z¢Z°ÀÔ%CÀdÕ%JÔKÜ�NŠNÓ€EÜ W¬c´!«f°f¸iÈÐQZÐ\cÐehØ%-¨u°nÀdÈIÐW\Ð^dÐfkÐmsÐuwð%yô z€Ià˜BˆXÜ˜UŸ]™]×+‰ˆØ�xˆÜ$+¨D°­M¸4Ð@WÕ;XÓ$YÑ!ˆ��e˜TØ×ÑœCŸLšL¨´×1CÑ1CÓDÔEØ×%Ñ%×+Ñ+Ó-×4Ñ4Ó6ˆÜ×&Ò& t§{¡{°9Ó'=Ó>¸zÕJˆÜ—’˜{°À)ÔLˆØ�z‰z�|Š| ¨1¡× 1Ñ 1× 3Ò 3Ð3Ð3Ø�‰˜
 H¨h¸Ø ¤%§,¢,°°¨yÄÇÁÔ"NØ ¤%§,¢,°°¨u¼E¿K¹KÔ"HðJô 	Kð 	�‰˜˜t D�zÐ+<¸dÐCTÕ>UØ-¨t°MÕ/BÐDWÔY\Ð]c×]gÑ]gÓ]iÓYjðtØnrðt÷ 	uñ ,ð �O‰O€EØ×!Ñ!Ö#ˆØ×Ñ˜UÖ#ñ $à×#Ñ#×.Ñ.Ö0ˆØ×Ñ˜TÖ"ñ 1à	‡J�J„LØ	×Ñ×ÑÔÜ—‘×!Ñ! %×"4Ñ"4×"?Ñ"?Ó"AÀdÐY[Ð!Ó\€IØ€LÜ�q—ˆÜ”Uœ3˜w›<Ó(Ó)ˆØ�‰�EÔØˆÜˆAØ•ZˆFÜ—’—•Ø %× 4Ñ 4¸VÀHÕ=MÈvÐV^ÕO_Ð<`ÐhlÐtxÐ 4Ó y‘�˜Ø×)Ñ)×6Ñ6Ó8�÷ !ð ×*Ñ*¸FÀ:Õ<NÐY[ØDJÐdiØ<Að +ó C‰IˆF�Að ˜HÕ%ˆFØ—<‘< 6§<¡<Ô/Ð/Ð/Ü×4Ò4°V¸VÓDˆCØ Ÿ.™.Ó*ˆKØ˜˜[¨6Õ1×6Ñ6Ó8Õ8¸1Õ<ÀÇÁÓARÐU[×U_ÑU_ÓUaÕAaÐdeÕAeÕfÕfˆDØ˜•:ˆDÜ—>’> $×'Ò'Ð'Ð'Ø×ÑÔ!Ø�M‰MŒOØ�N‰NÔØ�M‰Mœ% §¡£Ó.×/ñ% ð& 	×ÑœE¤"§'¢'¨&£/Ó2Ô3Üˆg�u˜Q•wÐ 0´%¸ÀRÕ8HÈ!Ó2LÐTX×YÐYñ1 ô2 �\Õ!€JÜ	‡J‚Jˆu×!Ñ!×,Ñ,Ó.°
Ô;ÜˆÕ×$Ñ$¤T§Z¢Z°Ð;\Ø0AÄ6Ì!ÈoÕJ]ÓC^Ø0<¸j¿o¹oÐObÔdjÐkuÓdvØ09¼3¸w»<ÈÐSTÐV_ÐabÔcfÐgnÓcoÕaoØ0CÀ\ÐSfÐhpØ0AÄ5ÌÏÊÓIYÐZ_ÕI_ÐabÓCcØ09¸8ÀIÕ;Nð1Pð YZô&[ð ^bõ&bö cùòK AùÚDùò Oqùâ=ùÒQs÷N !—�ús+   Å%bÆbÆ4b"
Ç5b'Èb,Ö-Ab1	â1cÚ__main__)$Ú__doc__r€   r•   r~   rŠ   rƒ   r—   Úpathlibr   r   Únumpyr   r‡   Útorch.nn.functionalÚnnÚ
functionalr¡   Úultralytics.models.samr   Úultralytics.utilsr   Ú+diatom_pilot.gunduz_expansion_asset_datasetr   Ú)diatom_pilot.gunduz_species_asset_datasetr   Ú diatom_pilot.ude_scaleup_datasetr   Ú__file__ÚresolveÚparentsr’   r�   r“   r9   rÞ   Ú__name__© r:   r8   Ú<module>rð      s�   ðÙ OÛ Û Û 	Û Û Û Ý ã 
Û Û ß Ð Ý ,Ý $å MÝ IÝ 3áˆƒN×ÑÓ×$Ñ$ QÕ'€ØˆÕ€ÙÐ	Ó€òZò.Zcðz ˆzÔÙ†Fñ r:   