
    |2gc%                        d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
Z
d dlmZ d dlmZmZmZmZmZ d dlmZ ddlmZ dd	lmZ 	 d d
lmZ ej4                  Ze	j8                  j;                  e
j<                        e	j8                  j;                  d      k  r ej>                  d       g dZ  e       Z!dddddddddd	Z"de#de#fdZ$d Z%d Z&dee#   fdZ'ejP                  jS                   ejT                  dd             Z+e
jX                  j[                         rd!nd"d#dfd$e#d%ee#e
j\                  f   d&e/d'e#fd(Z0d-d)ee#ee#   f   d*e1d+e/dee
jd                  e
jf                  f   fd,Z4y# e$ r ej4                  ZY +w xY w).    N)AnyUnionList)	packaging)Image)ComposeResize
CenterCropToTensor	Normalize)tqdm   )build_model)SimpleTokenizer)InterpolationModez1.7.1z.PyTorch version 1.7.1 or higher is recommended)available_modelsloadtokenizezwhttps://openaipublic.azureedge.net/clip/models/afeb0e10f9e5a86da6080e35cf09123aca3b358a0c3e3b6c78a7b63bc04b6762/RN50.ptzxhttps://openaipublic.azureedge.net/clip/models/8fa8567bab74a42d41c5915025a8e4538c3bdbe8804a470a72f30b0d94fab599/RN101.ptzyhttps://openaipublic.azureedge.net/clip/models/7e526bd135e493cef0776de27d5f42653e6b4c8bf9e0f653bb11773263205fdd/RN50x4.ptzzhttps://openaipublic.azureedge.net/clip/models/52378b407f34354e150460fe41077663dd5b39c54cd0bfd2b27167a4a06ec9aa/RN50x16.ptzzhttps://openaipublic.azureedge.net/clip/models/be1cfb55d75a9666199fb2206c106743da0f6468c9d327f3e0d0a543a9919d9c/RN50x64.ptz{https://openaipublic.azureedge.net/clip/models/40d365715913c9da98579312b702a82c18be219cc2a73407c4526f58eba950af/ViT-B-32.ptz{https://openaipublic.azureedge.net/clip/models/5806e77cd80f8b59890b7e101eabd078d9fb84e6937f9e85e4ecb61988df416f/ViT-B-16.ptz{https://openaipublic.azureedge.net/clip/models/b8cca3fd41ae0c99ba7e8951adf17d267cdb84cd88be6f7c2e0eca1737a03836/ViT-L-14.ptzhttps://openaipublic.azureedge.net/clip/models/3035c92b350959924f9f00213499208652fc7ea050643e8b385c2dac08641f02/ViT-L-14-336px.pt)	RN50RN101RN50x4RN50x16RN50x64zViT-B/32zViT-B/16zViT-L/14zViT-L/14@336pxurlrootc           	         t        j                  |d       t         j                  j                  |       }| j	                  d      d   }t         j                  j                  ||      }t         j                  j                  |      r-t         j                  j                  |      st        | d      t         j                  j                  |      rXt        j                  t        |d      j                               j                         |k(  r|S t        j                  | d       t!        d|         t"        j$                  j'                  |       5 }t        |d	      5 }t)        t+        |j-                         j/                  d
            dddd      5 }	 |j                  d      }|sn,|j1                  |       |j3                  t5        |             @	 d d d        d d d        d d d        t        j                  t        |d      j                               j                         |k7  rt        d      |S # 1 sw Y   dxY w# 1 sw Y   hxY w# 1 sw Y   lxY w)NT)exist_ok/z! exists and is not a regular filerbzH exists, but the SHA256 checksum does not match; re-downloading the filezDownloading CLIP model from wbzContent-LengthP   iBi   )totalncolsunit
unit_scaleunit_divisori    zDModel has been downloaded but the SHA256 checksum does not not match)osmakedirspathbasenamesplitjoinexistsisfileRuntimeErrorhashlibsha256openread	hexdigestwarningswarnprinturllibrequesturlopenr   intinfogetwriteupdatelen)	r   r   filenameexpected_sha256download_targetsourceoutputloopbuffers	            C/home/cameronsmith/repos/FeatUp/featup/featurizers/maskclip/clip.py	_downloadrK   *   s   KKt$ww$HiinR(Oggll42O	ww~~o&rww~~o/No..OPQQ	ww~~o&>>$5::<=GGI_\""MM_--uvw	(
./				$ 	)_d0K 	)vFKKM--.>?@QUbf#% 	)(,T*V$CK(  	)	) 	) ~~d?D16689CCEXabb	) 	)	) 	) 	) 	)s=   I(7IAI"I*II
II	II&c                 $    | j                  d      S )NRGB)convert)images    rJ   _convert_image_to_rgbrP   L   s    ==    c           
      ~    t        t        | t              t        |       t        t               t        dd      g      S )N)interpolation)g3<4'?gwgM?gy{ ?)gB91?gwt.?g	U?)r   r	   BICUBICr
   rP   r   r   )n_pxs    rJ   
_transformrV   P   s:    t7+4
57[\  rQ   returnc                  <    t        t        j                               S )z*Returns the names of available CLIP models)list_MODELSkeys rQ   rJ   r   r   Z   s    rQ   z$TORCH_HUB_ROOTz$HOME/.torch_hubcudacpuFnamedevicejitdownload_rootc                    | t         v rt        t         |    |xs t              }n;t        j                  j                  |       r| }nt        d|  dt                      t        |d      5 }	 t        j                  j                  ||rnd      j                         }d}ddd       |skt        xs j!                               j#                        }t%              dk(  r|j'                          |t)        |j*                  j,                        fS t        j                  j/                  fd
g       }|j0                  j3                  d      D 	cg c]  }	dt5        |	      v s|	 c}	d   fd}
j7                  |
        |
|j8                          |
|j:                         t%              dk(  rt        j                  j/                  d g       }t=        |j0                  j?                  d      jA                               d   }|jC                         fd}|j7                  |        ||j8                          ||j:                         |j'                          |t)        |j,                  jE                               fS # t        $ r8 |rt        j                  d| d       d	}t        j                  |d      }Y 1w xY w# 1 sw Y   7xY wc c}	w )a  Load a CLIP model

    Parameters
    ----------
    name : str
        A model name listed by `clip.available_models()`, or the path to a model checkpoint containing the state_dict

    device : Union[str, torch.device]
        The device to put the loaded model

    jit : bool
        Whether to load the optimized JIT model or more hackable non-JIT model (default).

    download_root: str
        path to download the model files; by default, it uses "~/.torch_hub/clip"

    Returns
    -------
    model : torch.nn.Module
        The CLIP model

    preprocess : Callable[[PIL.Image], torch.Tensor]
        A torchvision transform that converts a PIL image into a tensor that the returned model can take as its input
    zModel z not found; available models = r    r^   )map_locationNzFile z6 is not a JIT archive. Loading as a state dict insteadFc                  r    t        j                  g       j                  t        j                               S N)torchonestor`   )r`   s   rJ   <lambda>zload.<locals>.<lambda>   s#    EJJrN,=,=ell6>R,S rQ   )example_inputsprim::ConstantDevicec                    	 t        | d      r| j                  gng }t        | d      r%|j                  | j                  j                         |D ]Z  }|j                  d      D ]D  }d|j                         v st        |d         j                  d      s4|j                         F \ y # t        $ r g }Y w xY w)Ngraphforward1rl   valuer]   )
hasattrrp   r1   appendrq   findAllNodesattributeNamesstr
startswithcopyAttributes)modulegraphsrp   nodedevice_nodes       rJ   patch_devicezload.<locals>.patch_device   s    	'.vw'?fll^RF 6:&MM&////0 	5E**+;< 5d1133DM8J8U8UV\8]''45	5  	F	s   B/ /B=<B=c                  H    t        j                  g       j                         S rf   )rg   rh   floatr\   rQ   rJ   rj   zload.<locals>.<lambda>   s    uzz"~/C/C/E rQ   aten::tor   c                    	 t        | d      r| j                  gng }t        | d      r%|j                  | j                  j                         |D ]t  }|j                  d      D ]^  }t        |j                               }dD ]>  }||   j                         d   dk(  s||   j                         j                         @ ` v y # t        $ r g }Y w xY w)Nrp   rq   r   )r      rr      )
rs   rp   r1   rt   rq   ru   rY   inputsr|   ry   )rz   r{   rp   r|   r   i
float_nodes         rJ   patch_floatzload.<locals>.patch_float   s    +267+C&,, vz*foo334 H!..z: HD!$++-0F# H!!9>>+G49"1INN,;;JGHHH   s   C	 	CC)#rZ   rK   TORCH_HUB_ROOTr)   r+   r0   r1   r   r4   rg   ra   r   evalr7   r8   r   
state_dictri   rw   r   rV   visualinput_resolutiontracerp   ru   reprapplyencode_imageencode_textrY   findNoder   r|   item)r_   r`   ra   rb   
model_pathopened_filemodelr   device_holdernr~   float_holderfloat_inputr   r}   r   s    `            @@rJ   r   r   b   s{   < wwt}m.M~N
		
VD6)HIYI[H\]^^	j$	 
E;		EIINN;sVPUNV[[]EJ	
E J<%*:*:*<=@@Hv;%KKMj!>!>??? IIOO$SdfOgM+11>>?OPhT\`def`gTg1hiklK5 
KK##$""# 6{eyy'EVXY<--66zBIIKLQO %%'
	H  	K E&&'E%%&*U3388:;;;  	Ej\1ghiKeDJ	E
E 
E( is6   /K15JK(/K(=KKKKK%textscontext_lengthtruncatec                    t        | t              r| g} t        j                  d   }t        j                  d   }| D cg c]  }|gt        j	                  |      z   |gz   ! }}t
        j                  j                  t        j                        t
        j                  j                  d      k  r0t        j                  t        |      |t        j                        }n/t        j                  t        |      |t        j                        }t        |      D ]Y  \  }}	t        |	      |kD  r!|r|	d| }	||	d<   nt        d| |    d|       t        j                   |	      ||dt        |	      f<   [ |S c c}w )	a  
    Returns the tokenized representation of given input string(s)

    Parameters
    ----------
    texts : Union[str, List[str]]
        An input string or a list of input strings to tokenize

    context_length : int
        The context length to use; all CLIP models use 77 as the context length

    truncate: bool
        Whether to truncate the text in case its encoding is longer than the context length

    Returns
    -------
    A two-dimensional tensor containing the resulting tokens, shape = [number of input strings, context_length].
    We return LongTensor when torch version is <1.8.0, since older index_select requires indices to be long.
    z<|startoftext|>z<|endoftext|>z1.8.0)dtypeNrn   zInput z  is too long for context length )
isinstancerw   
_tokenizerencoderencoder   versionparserg   __version__zerosrB   longr=   	enumerater1   tensor)
r   r   r   	sot_token	eot_tokentext
all_tokensresultr   tokenss
             rJ   r   r      sM   * %""#45I""?3IRWX$9+
 1 1$ 779+EXJXu001I4E4E4K4KG4TTS_nEJJOS_nEIINz* 7	6v;'0&r
"VE!H:5UVdUe#fgg"',,v"6q,3v;,7 M Ys   $E9)M   F)5r2   r)   r:   r7   typingr   r   r   pkg_resourcesr   rg   PILr   torchvision.transformsr   r	   r
   r   r   r   r   r   simple_tokenizerr   
_Tokenizerr   rT   ImportErrorr   r   r   r8   __all__r   rZ   rw   rK   rP   rV   r   r+   
expandvarsgetenvr   r]   is_availabler`   boolr   r=   	IntTensor
LongTensorr   r\   rQ   rJ   <module>r      s    	   # # #   S S   ;8''G 5,,-	0A0A0G0G0PPHMMBC
2\
 F H J L L N N N Z
3 c D  $s)  
 ##IBII.?AS$TU
 6;ZZ5L5L5N6TY!	i<i<c5<<'(i< i< 	i<X)E#tCy.) )3 )t )`e	OOU%%%a' )u  mmGs   E5 5F
	F
