
    9i<'                     r   S SK r S SKJr  S SKJrJr  S SKrS SK	r
S SKJr  S SKJr  S SKJrJrJrJrJr  S SKJr  S SKJr  S SKJr  S S	KJr  \" 5       r\R:                  " \
R<                  5      \R:                  " S
5      :  a  O\R?                  S\  S35        \RB                  " SS9 " S S\5      5       r"g)    N)DictOptional)version)tqdm)CartoonModelall_filesimple_superpixeltf_data_loaderwrite_batch_image)BaseTrainer)TRAINERS)	ModelFile)
get_logger2zTensorFlow version z< found, TF2.x is not supported by CartoonTranslationTrainer.zcartoon-translation)module_namec                   v   ^  \ rS rSr     SS\S\4U 4S jjjrS rS r SS\\   S\	\\
4   4S	 jjrS
rU =r$ )CartoonTranslationTrainer   modelcfg_filec                 J  > U R                  U5      n[        R                  " 5         Xl        [        R
                  " U[        R                  5      U l        Uc%  [        R
                  " U[        R                  5      n[        TU ]-  U5        0 U l        U R                  5         Ub  X0R                  S'   Ub  X@R                  S'   Ub  XPR                  S'   Ub  X`R                  S'   [        R                  R!                  U R                  S   5      (       d#  [        R"                  " U R                  S   5        [%        U R                  S   5      U l        [%        U R                  S   5      U l        [        R*                  " SS9n	SU	R,                  l        [        R0                  " U	S9U l        [        R4                  " [        R6                  U R                  S	   U R                  S
   U R                  S
   S/5      U l        [        R4                  " [        R6                  U R                  S	   U R                  S
   U R                  S
   S/5      U l        [        R4                  " [        R6                  U R                  S	   U R                  S
   U R                  S
   S/5      U l        [?        U R                  5      U l         U RA                  U R8                  U R<                  U R:                  5      n
U
S   U l!        U
S   U l"        U
S   U l#        [        RH                  RK                  SU RD                  5        [        RH                  RK                  SU RF                  5        [        RH                  RM                  U R                  S   S-   5      U l'        [        RH                  RQ                  5       U l)        [        RT                  " 5       nU Vs/ s H  nSURV                  ;   d  M  UPM     nnU Vs/ s H  nSURV                  ;   d  M  UPM     nn[        RX                  " [        RZ                  R\                  5      n[        R^                  " U5         [        R`                  Rc                  U R                  S   SSS9Re                  U RD                  US9U l3        [        R`                  Rc                  U R                  S   SSS9Re                  U RF                  US9U l4        SSS5        [        R`                  Rk                  SS9U l6        U R2                  Ro                  5        nURq                  [        Rr                  " 5       5        U R                  S   S:w  av  [t        Rw                  SU R                   35        U Rl                  Ry                  U[        R
                  " U R                  S[{        U R                  S   5      -   5      5        SSS5        gs  snf s  snf ! , (       d  f       GN= f! , (       d  f       g= f)as  
Args:
    model: the model_id of trained model
    cfg_file: the path of configuration file
    work_dir: the path to save training results
    photo: the path of photo images for training
    cartoon: the path of cartoon images for training
    max_steps: the number of total iteration for training
Returns:
    initialized trainer: object of CartoonTranslationTrainer
Nwork_dirphotocartoon	max_stepsT)allow_soft_placement)config
batch_size
patch_size   output_cartoong_lossd_lossz
/train_loggenediscadv_train_lrg      ?gGz?)beta1beta2)var_listi  )max_to_keepresume_epochr   zloading model from zmodel-)>get_or_download_model_dirtfreset_default_graph	model_dirospjoinr   TF_CHECKPOINT_FOLDER
model_pathCONFIGURATIONsuper__init__params_override_params_from_fileospathexistsmakedirsr   face_photo_listface_cartoon_listConfigProtogpu_optionsallow_growthSession_sessionplaceholderfloat32input_photoinput_superpixelinput_cartoonr   r   r!   r"   r#   summaryscalar
FileWritertrain_writer	merge_all
summary_optrainable_variablesnameget_collection	GraphKeys
UPDATE_OPScontrol_dependenciestrainAdamOptimizerminimizeg_optimd_optimSaversaver
as_defaultrunglobal_variables_initializerloggerinforestorestr)selfr   r   r   r   r   r   argskwargs	tf_configoutputall_varsvar	gene_vars	disc_vars
update_opssess	__class__s                    r/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/trainers/cv/cartoon_translation_trainer.pyr6   "CartoonTranslationTrainer.__init__!   s   ( ..u5
 ((5)*H*HIxxy'>'>?H"'')&.KK
##(KK %,KK	" '0KK$ww~~dkk*566KKJ/0'G(<=!)$++i*@!ANN=	-1	*

)4>>"**KK%t{{<'@KK%q7
  !#rzzKK%t{{<'@KK%q<
 !  ^^BJJKK%t{{<'@KK%q9
 
 "$..1
D,,d.@.@ 113$%56X&X&


(DKK0


(DKK0JJ11$++j2I4@3A B**..0))+$,CHS#((0BSH	C$,CHS#((0BSH	C&&r||'>'>?
$$Z08811$++n2MUX`d1e$++	: L8811$++n2MUX`d1e$++	: L 1 XX^^^5
]]%%'4HHR4467{{>*a/1$//1BCD

""HHT__%DKK,G(HHJK	 (' DC00 ('s2   "W8:W8W=W=*BXB.X
X
X"c                    U R                   S   S   U R                  S'   U R                   S   S   U R                  S'   U R                   S   S   U R                  S'   U R                   S   S   U R                  S'   U R                   S   S   U R                  S'   U R                   S   S   U R                  S'   U R                   S   S   U R                  S'   U R                   S   S	   U R                  S	'   U R                   S   S
   U R                  S
'   U R                   S   S   U R                  S'   U R                   S   S   U R                  S'   g )NrU   r   r   r   r   r   r&   r   logging_intervalckpt_period_intervalr+   num_gpus)cfgr7   )rc   s    ro   r8   4CartoonTranslationTrainer._override_params_from_file   s<   #xx09G!%'!29!=I$(HHW$5l$CL!"&((7"3J"?J$(HHW$5l$CL!&*hhw&7&GN##'88G#4[#AK *.((7*;<N*O&'.2hhw.?"/$*+&*hhw&7&GN#"&((7"3J"?J    c                    [         R                  S5        [        U R                  U R                  S   5      n[        U R
                  U R                  S   5      nUR                  5       nUR                  5       nUR                  5       nUR                  5       n[        R                  R                  (       a  SOSn	[        R                  " U	5         [        [        U R                  S   5      5       GH  n
U R                  R                  UR                   5        U R                  R                  UR                   5        U R                  R                  Xx/5      u  pU R                  R                  U R"                  U R$                  U0S9n['        USS9nU R                  R                  U R(                  U R*                  /U R$                  UU R,                  UU R.                  U0S9u  nnU R                  R                  U R0                  U R2                  U R4                  /U R$                  UU R,                  UU R.                  U0S9u  nnnU R6                  R9                  UU
5        [:        R<                  " U
S	-   U R                  S
   5      S:X  d	  U
S:X  d  GM  [         R                  SU
 SU SU 35        [:        R<                  " U
S	-   U R                  S   5      S:X  d	  U
S:X  d  GM  U R>                  RA                  U R                  U R                  S   S-   SU
S9  U R                  R                  U R"                  U R$                  UU R,                  UU R.                  U0S9n[C        UU R                  S   S-   [E        SU
-  5      S-   S5        [C        XR                  S   S-   [E        SU
-  5      S-   S5        GM     S S S 5        g ! , (       d  f       g = f)Nz'Begin local cartoon translator trainingr   zgpu:0zcpu:0r   )	feed_dict   )seg_num   rr   r   zIter: z
, d_loss: z
, g_loss: rs   r   z/saved_models/modelF)write_meta_graphglobal_stepz/imagesz%8dz_face_result.jpg   z_face_photo.jpg)#r_   r`   r
   r=   r7   r>   make_initializable_iteratorget_nextr-   testis_gpu_availabledevicer   rangerC   r]   initializerr!   rF   r	   r"   rX   rG   rH   r#   rY   rN   rL   add_summarynpmodr[   saver   rb   )rc   rd   re   photo_ds
cartoon_dsphoto_iteratorcartoon_iterator
photo_nextcartoon_nextr   r   photo_batchcartoon_batchtransfer_resrG   r"   _r#   
train_inforesult_faces                       ro   rU   CartoonTranslationTrainer.train   s   =>!$"6"6"&++l";=#D$:$:$(KK$=?
!==?%AAC#,,.
'002GG44'YYv!%K(@"AB	!!."<"<=!!"2">">?-1]]->->..0*  $}}00''#//=  1  ? $5\3#O  MM--[[$,,/((+--/?**M . 	 )-(9(9[[$,,@((+--/?**M ): )%: !!--j)D66)a-5G)H  !$-NKK :fXZxP vvi!m"kk*@A #$%(1Q

 MM KK
36KK-2(1	 ( 3 '+mm&7&7 // $ 0 0+ $ 5 5{ $ 2 2M' '8 ' *'Z)@9)L	 125GGL *'Z)@9)L	 125FFKu C s   GO'AO9CO
O,checkpoint_pathreturnc                     [        S5      e)a  evaluate a dataset

evaluate a dataset via a specific model from the `checkpoint_path` path, if the `checkpoint_path`
does not exist, read from the config file.

Args:
    checkpoint_path (Optional[str], optional): the model path. Defaults to None.

Returns:
    Dict[str, float]: the results about the evaluation
    Example:
    {"accuracy": 0.5091743119266054, "f1": 0.673780487804878}
z6evaluate is not supported by CartoonTranslationTrainer)NotImplementedError)rc   r   rd   re   s       ro   evaluate"CartoonTranslationTrainer.evaluate   s    " "DF 	Frw   )rC   r#   rY   r>   r=   r"   rX   rH   rF   rG   r   r/   r3   r!   r7   r[   rN   rL   )NNNNN)N)__name__
__module____qualname____firstlineno__rb   r6   r8   rU   r   r   floatr   __static_attributes____classcell__)rn   s   @ro   r   r      sw    
 "&`K`K`K `KD@KK\ 37F"*3-F #3:.F Frw   r   )#r9   os.pathr:   r0   typingr   r   numpyr   
tensorflowr-   	packagingr   r   modelscope.models.cv.cartoonr   r   r	   r
   r   modelscope.trainers.baser   modelscope.trainers.builderr   modelscope.utils.constantr   modelscope.utils.loggerr   r_   parse__version__r`   _tf_versionregister_moduler    rw   ro   <module>r      s    
  !    = = 1 0 / .	
== 7==#55
KK
k]*fg
 
&<=RF RF >RFrw   