Team Ai
Apppublic

David310/Detect_AI-generated_Image

sourceHugging Faceupdated 2y agoView on Hugging Face
4likes
clip.cpython-39.pyc106 linesDownload Raw Back to __pycache__
1a

2� �c�$�3@s�ddlZddlZddlZddlZddlmZmZmZddlm	Z	ddl4Z5ddlmZddl
mZmZmZmZmZddlmZddlmZdd	lmZzdd6l
mZejZWney�ejZYn0e	j�e7j�e	j�d�kr�e�d�gd
�Z e�Z!dddddddddd�	Z"e#e#d�dd�Z$dd�Z%dd�Z&ee#d�d d!�Z'e8j(�)��rPd"nd#d$dfe#ee#e9j*fe+e#d%�d&d'�Z,d,ee#ee#fe-e+ee10j.e11j/fd)�d*d+�Z0dS)-�N)�Any�Union�List)�	packaging)�Image)�Compose�Resize�12CenterCrop�ToTensor�	Normalize)�tqdm�)�build_model)�SimpleTokenizer)�InterpolationModez1.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.ptz�https://openaipublic.azureedge.net/clip/models/3035c92b350959924f9f00213499208652fc7ea050643e8b385c2dac08641f02/ViT-L-14-336px.pt)	ZRN50ZRN101ZRN50x4ZRN50x16ZRN50x64zViT-B/32zViT-B/16zViT-L/14zViT-L/14@336px)�url�rootc	13Cs�tj|dd�tj�|�}|�d�d}tj�||�}tj�|�r\tj�|�s\t|�d���tj�|�r�t	�14t|d�����
�|kr�|St�|�d��tj�|���}t|d��~}tt|���d	��d15dddd
��<}|�d�}|s�q|�|�|�t|��q�Wd�n1�s0YWd�n1�s60YWd�n1�sV0Yt	�16t|d�����
�|k�r�td��|S)NT)�exist_ok�/�����z! exists and is not a regular file�rbzH exists, but the SHA256 checksum does not match; re-downloading the file�wbzContent-Length�PZiBi)�total�ncols�unit�17unit_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�urllib�request�urlopenr�int�info�get�write�update�len)	rr�filenameZexpected_sha256Zdownload_target�source�output�loop�buffer�r?�M/nobackup2/yuheng-data/forensic_diffusion/linear_probe/my_models/clip/clip.py�	_download+s("1819prAcCs20|�d�S)N�RGB)�convert)�imager?r?r@�_convert_image_to_rgbKsrEcCs&tt|td�t|�tt�tdd�g�S)N)�
interpolation)g3<�4'��?gwg��M�?gy{�� �?)g��B�91�?g�w�t.��?g��	U��?)rr�BICUBICr	rEr21r)Zn_pxr?r?r@�22_transformOs23�rH)�returncCstt���S)z*Returns the names of available CLIP models)�list�_MODELS�keysr?r?r?r@rYsr�cuda�cpuF)�name�device�jit�
download_rootc
	s�|tvr$tt||ptj�d��}n(tj�|�r6|}ntd|�dt�����t|d��p}z$t	j24j||rj�ndd���}d}Wn:ty�|r�t
�d|�d	��d25}t	j|dd�}Yn0Wd�n1s�0Y|�st|p�|������}t��dk�r|��|t|jj�fSt	j26j�fdd�gd
�}dd�|j�d�D�d��fdd�}	|�|	�|	|j�|	|j�t��dk�r�t	j27jdd�gd
�}28t|29j�d����d}|� ���fdd�}|�|�||j�||j�|��|t|j�!��fS)a�Load a CLIP model30 31    Parameters32    ----------33    name : str34        A model name listed by `clip.available_models()`, or the path to a model checkpoint containing the state_dict35 36    device : Union[str, torch.device]37        The device to put the loaded model38 39    jit : bool40        Whether to load the optimized JIT model or more hackable non-JIT model (default).41 42    download_root: str43        path to download the model files; by default, it uses "~/.cache/clip"44 45    Returns46    -------47    model : torch.nn.Module48        The CLIP model49 50    preprocess : Callable[[PIL.Image], torch.Tensor]51        A torchvision transform that converts a PIL image into a tensor that the returned model can take as its input52    z
~/.cache/clipzModel z not found; available models = rrN)�map_locationNzFile z6 is not a JIT archive. Loading as a state dict insteadFcst�g��t����S�N)�torch�ones�torPr?)rPr?r@�<lambda>��zload.<locals>.<lambda>)�example_inputscSsg|]}dt|�vr|�qS)�Device)�repr)�.0�nr?r?r@�53<listcomp>�rYzload.<locals>.<listcomp>�prim::Constant�����cs�zt|d�r|jgng}Wnty0g}Yn0t|d�rJ|�|jj�|D]<}|�d�D],}d|��vr\t|d��d�r\|�	��q\qNdS)N�graph�forward1r`�valuerM)54�hasattrrbr)�appendrc�findAllNodes�attributeNames�str�55startswith�copyAttributes)�module�graphsrb�node)�device_noder?r@�patch_device�s5657zload.<locals>.patch_devicecSst�g���SrT)rUrV�floatr?r?r?r@rX�rY�aten::tor
cs�zt|d�r|jgng}Wnty0g}Yn0t|d�rJ|�|jj�|D]P}|�d�D]@}t|���}dD]*}||��ddkrp||���	��qpq\qNdS)Nrbrcrr)r
�rd�)58rerbr)rfrcrgrJ�inputsrnrk)rlrmrbrnru�i)�59float_noder?r@�patch_float�s6061zload.<locals>.patch_float)"rKrAr!r#�62expanduserr(r)rr,rUrQr�evalr/r0r�63state_dictrWrirqrHZvisualZinput_resolution�tracerbrg�apply�encode_imageZencode_textrJ�findNoderurn�item)
rOrPrQrRZ64model_path�opened_file�modelr{Z
device_holderrpZfloat_holderZfloat_inputrxr?)rProrwr@r^sF2656667686970r�M)�texts�context_length�truncaterIcs�t|t�r|g}tjd�tjd���fdd�|D�}tj�tj�tj�d�krjtj	t71|�|tjd�}ntj	t72|�|tjd�}t
|�D]`\}}t73|�|kr�|r�|d|�}�|d<ntd	||�d74|����t�|�||dt75|��f<q�|S)a�76    Returns the tokenized representation of given input string(s)77 78    Parameters79    ----------80    texts : Union[str, List[str]]81        An input string or a list of input strings to tokenize82 83    context_length : int84        The context length to use; all CLIP models use 77 as the context length85 86    truncate: bool87        Whether to truncate the text in case its encoding is longer than the context length88 89    Returns90    -------91    A two-dimensional tensor containing the resulting tokens, shape = [number of input strings, context_length].92    We return LongTensor when torch version is <1.8.0, since older index_select requires indices to be long.93    z<|startoftext|>z
<|endoftext|>cs"g|]}�gt�|��g�qSr?)�94_tokenizer�encode)r]�text�Z	eot_tokenZ	sot_tokenr?r@r_�rYztokenize.<locals>.<listcomp>z1.8.0)�dtypeNrazInput z  is too long for context length )�95isinstancerir��encoderr�version�parserU�__version__�zerosr9�longr4�	enumerater)�tensor)r�r�r�Z96all_tokens�resultrv�tokensr?r�r@r�s 979899100r)r�F)1r*r!r1r/�typingrrr�
pkg_resourcesrrU�PILr�torchvision.transformsrrr	r101rrr�rZsimple_tokenizerrZ102_TokenizerrrG�ImportErrorr�r�r�r0�__all__r�rKrirArErHrrM�is_availablerP�boolrr4�	IntTensor�103LongTensorrr?r?r?r@�<module>sH104105�
 1066g