Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision 2b019233d6851facadec8e9215cc805eef47932c authored by Changjian Chen on 20 May 2024, 01:52:04 UTC, committed by Changjian Chen on 20 May 2024, 01:52:04 UTC
update readme
1 parent 08a8fb3
  • Files
  • Changes
  • 5c3066e
  • /
  • backend
  • /
  • data
  • /
  • __pycache__
  • /
  • coco17.cpython-36.pyc
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:2b019233d6851facadec8e9215cc805eef47932c
directory badge
swh:1:dir:3d1e63d905c7651894c8c68d3b1b329c5b11741f
content badge
swh:1:cnt:8d9d0a6bd6f2e2b863109c6d21bf6554e60e1610

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
coco17.cpython-36.pyc
3

�L`�S�Q@s�dZddlmZddlZddljZddlZddlZddl	j
jZddlj
Z
ddlZddlZddlZddlmZddlZddlZddlZddlZejed�ZdZdZd	Zd
ZdZdZd
Z ej!ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddgP�Z"dd�Z#Gdd�de$�Z%Gdd�de$�Z&Gdd�dej'�Z(dd�Z)dS)z�
Author: Changjian Chen
Date: 2020-11-27 10:23:23
LastEditTime: 2021-03-13 20:59:17
LastEditors: Changjian Chen
Description: 
FilePath: /WSL/CSD-SSD/data/coco17.py

�)�HOME�N)�loggerzWSL/Data/coco17/�images�annotationszcocoapi/PythonAPIzshrink_instances_{}.jsonzcaptions_{}.jsonzlabel_extraction_{}.pklzshrink_coco_labels.txtTFcCsbi}t|d�}g}xFt|�D]:\}}|jd�}t|�|t|d�<|j|djd��qW||fS)N�r�,r��
)�open�	enumerate�split�int�append�strip)�
label_file�	label_map�labelsZclass_names�idx�line�ids�r�*/data/changjian/WSL/CSD-SSD/data/coco17.py�
get_label_mapDs

rc@s eZdZdZdd�Zdd�ZdS)�COCO17EvalAnnotationTransformz�Transforms a COCO annotation into a Tensor of bbox coords and label index
    Initilized with a dictionary lookup of classnames to indexes
    cCsttjttt��\|_|_dS)N)r�osp�join�COCO17_ROOT�ANNOTATIONS�	LABEL_MAPr�classes)�selfrrr�__init__Rsz&COCO17EvalAnnotationTransform.__init__c
Cs�tj||||g�}g}x�|D]�}d|kr�|d}i}|j|d}	|j|	|d<d|d<t|d�dt|d�dt|d|d�dt|d|d�dg|d<|j|�qtd	�qW|S)
a
        Args:
            target (dict): COCO target json annotation as a python dict
            height (int): height
            width (int): width
        Returns:
            a list containing lists of bounding boxes  [bbox coords, class idx]
        �bbox�category_id�namerZ	difficultrr	�zno bbox problem!)�np�arrayrr rr�print)
r!�target�width�height�scaleZobjects�objr#Z
obj_struct�	label_idxrrr�__call__Us 	
z&COCO17EvalAnnotationTransform.__call__N)�__name__�
__module__�__qualname__�__doc__r"r0rrrrrNsrc@s eZdZdZdd�Zdd�ZdS)�COCO17AnnotationTransformz�Transforms a COCO annotation into a Tensor of bbox coords and label index
    Initilized with a dictionary lookup of classnames to indexes
    cCsttjttt��\|_}dS)N)rrrrrrr)r!�_rrrr"{sz"COCO17AnnotationTransform.__init__c
Cs�tj||||g�}g}x�|D]�}d|kr�|d}|d|d7<|d|d7<|j|d}ttj|�|�}	t|	dd�|	d<t|	dd�|	d<t|	dd�|	d<t|	dd�|	d<|	j|�||	g7}qtd	�qW|S)
a
        Args:
            target (dict): COCO target json annotation as a python dict
            height (int): height
            width (int): width
        Returns:
            a list containing lists of bounding boxes  [bbox coords, class idx]
        r#r	rr&rr$gg�?zno bbox problem!)r'r(r�list�max�minrr))
r!r*r+r,r-�resr.r#r/�	final_boxrrrr0~s"	

z"COCO17AnnotationTransform.__call__N)r1r2r3r4r"r0rrrrr5wsr5c@s�eZdZdZedde�ddddddddfdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�ZdS).�COCO17Detectiona�`MS Coco Detection <http://mscoco.org/dataset/#detections-challenge2016>`_ Dataset.
    Args:
        root (string): Root directory where images are downloaded to.
        set_name (string): Name of the specific set of COCO images.
        transform (callable, optional): A function/transform that augments the
                                        raw images`
        target_transform (callable, optional): A function/transform that takes
        in the target (bbox) and transforms it.
    Z	train2017N�COCOg�?TFc
s�tjjtj|t��ddlm}
ttjt	t
t��\�_�_
|�_tjttj|t
d�d�j���_|�_tj|t|��_|
tj|t
tj|����_|
tj|t
tj|����_tjj|t
d|d�}|jd�dk�r tj|��rtd�t|d	�}|j�j d
�j!d
�}dd�|D��_"nt#d
��ntd�t$�jj%j&���_"|�_'|�_(|�_)|�_*|�_+|�rv|�_,|t-�j"��_.nt/t-�j"�|��_,|�_.�j0|	�|jd�dk�r��j,}n|}�j(�r(tj|dj||j1dd����_2tjt�j2d�j���_3i}x(�j3D]}||t/|ddj4��<�qW|�_5�j'�r6tj|dd�}t6j|dddddd��_7�j7j8dd��_9ttjj|d�d	��F}g�_:i�_;x2t<|�D]&\}}|j d
��_:|�j;|j d
�<�q�WWdQRXttjj|d�d��}t=j>|��_?WdQRXd}|t=j@jAd�j?jBd"�dd}t=jC�j?|gdd��_D�jD�_D�j�r�tj|dj|���_Etjt�jEd�j���_F�fd d!��jD��_GdS)#Nr)r=z
add_annos.pkl�rbZshrink_z_random_list.txt�trainzreading from random_listrr
cSsg|]}t|��qSr)r)�.0�irrr�
<listcomp>�sz,COCO17Detection.__init__.<locals>.<listcomp>z#you should have randomlist.txt filezreading ids from origin dataz@aggre_last/coco17_text_shrink_aggre_last_1_1e-5_{}/{}_result.pklZ2017�Zimage_idZbottom_up_attentionz(coco17_resnet101_faster_rcnn_genome.lmdbrTF)�max_readers�readonly�lock�	readahead�meminit)�writezcoco_open_vocab.txtzcoco_open_vocab_300d.npyg���Q��?r	)�axisz,detections/processed_detections_train_{}.pklcsi|]}|�j|�qSr)r)r@�k)r!rr�
<dictcomp>	sz,COCO17Detection.__init__.<locals>.<dictcomp>�����)H�sys�pathrrr�COCO_API�pycocotools.cocor=rrrrrr �	image_set�pickle�loadsr�read�	add_annos�finetune�IMAGES�root�
INSTANCES_SET�format�coco�CAPTIONS_SET�	coco_caps�os�count�existsr)rr
�_ids�
ValueErrorr7�	imgToAnns�keys�text�extracting_label�	transform�target_transformr%�
supervise_num�len�supervise_percentr�set_only_supervise�replace�extracted_file�_results�decode�results�lmdbZenv_bua�begin�txn_buaZ_open_vocabulary_list�
verb_2_idxrr'�loadZ_open_vocabulary_word_embedding�random�rand�shape�concatenate�embedding_array_dataZdetection_file�
detections�inverse_label_map)r!rYrRrhri�dataset_namerlrjrg�only_supervise�evalrfrWr=Zrandom_list_fileZrandom_listrZtrained_data_lenrrrZbua_feature_file�frrZ
init_widthZoov_embr)r!rr"�s�



	


  


zCOCO17Detection.__init__cCsF||_|r,tjd�|jd|j�j�|_ntjd�|jj�|_dS)Nzusing supervised onlyz&using both supervised and unsupervised)r�r�inforbrj�copyr)r!r�rrrrms

z"COCO17Detection.set_only_supervisecCs|jS)N)r )r!rrrrszCOCO17Detection.get_label_mapcCs|jS)N)rR)r!rrr�get_set_typeszCOCO17Detection.get_set_typecCs|rt�|_dS)N)rri)r!r�rrr�set_evalszCOCO17Detection.set_evalcCs~|jr`|j|�}|j|�}|j|�}tj|jd|jdt|�f�jtj	�}||t
j|�|fS|j�r<|j
|�\}}}}	}
}|jr�|j|�}n
|j|�}|j|�\}
}|j|�}||
jt�||
jt�<g}|ddkr�|}nNx*|D]"}|t|d�dkr�|j|�q�Wt|�dk�r.tjtj|�|f�}|||||fS|j|�\}}}	}
}|j�rd|j|�}n
|j|�}||||fSdS)z�
        Args:
            index (int): Index
        Returns:
            tuple: Tuple (image, target).
                   target is the object returned by ``coco.loadAnns``.
        rrNrM)rf�pull_image_level_annos�pull_text_feature�pull_image_featurer'�onesrzrk�astype�float32�torch�
from_numpyrW�pull_detrg�pull_extracted_labels�pull_add_annos�boolrr�vstackr(�	pull_item)r!�index�labelZtext_featureZ
image_feature�masks�im�gt�det�h�w�semi�image_level_target�
text_add_anno�img_add_annoZ	gt_targetZnew_gt�drrr�__getitem__ s:






zCOCO17Detection.__getitem__cCs
t|j�S)N)rkr)r!rrr�__len__JszCOCO17Detection.__len__cs�j|}|�jkr"tjdg�}ntjdg�}�jj|}�jj|d�}tj�j|�}||dd�d
fdk}�fdd�|D�}�jj|�}|ddkr�g}t	|�}t	|�}	||}
t
j�j�jj
|�dd�}t
j|�s�td	j|���tjt
j�j|��}|j\}
}}�jdk	�r&�j|
||
�}�jdk	�r�tj|�}�j||dd�dd
�f|dd�d
fd�\}}}}|dd�dd�df}tj|tj|dd�f�}|dk�r�tj||	�jt�}|d|d|�j��}||d|�j�d�}tj|�jddd�|||
||fS)Nrr)�imgIdsr	g�������?cs0g|](}|dd�j��jt|d�d��qS)N�r)r#r$rM)�tolistr~r)r@r�)r!rrrBWsz,COCO17Detection.pull_det.<locals>.<listcomp>�	file_namezImage path does not exist: {}r�T)rJ�����)r	rr)rrjr'r(r\rd�	getAnnIdsr}�loadAnnsrkrrrY�loadImgsra�AssertionErrorr[�cv2�imreadrzrirh�hstack�expand_dimsr�r�r��sumr�r��permute)r!r��img_idr�r*�ann_idsr�Znp_detZ
target_numZdet_numZorigin_combinedrO�imgr,r+r6�combined�boxesr�maskr)r!rr�MsB




zCOCO17Detection.pull_detc
CsV|j|}|jj|}|jj|d�}|jj|�}tj|j|jj|�dd�}tj	|�sft
dj|���tj
tj|j|��}|j\}}}	|jdk	r�|j|||�}|jdk	�rtj|�}|j||dd�dd�f|dd�df�\}}
}|dd�dd�d
f}tj|
tj|dd	�f�}||jk�r,tjdg�}ntjdg�}tj|�jddd�||||fS)z�
        Args:
            index (int): Index
        Returns:
            tuple: Tuple (image, target, height, width).
                   target is the object returned by ``coco.loadAnns``.
        )r�rr�zImage path does not exist: {}Nr�r	r)rJ)r	rr)rr\rdr�r�rrrYr�rar�r[r�r�rzrirhr'r(r�r�rjr�r�r�)
r!r�r�r*r�rOr�r,r+r6r�rr�rrrr�us(


zCOCO17Detection.pull_itemcCs6|j|}|jj|�dd}tjtj|j|�tj�S)aReturns the original image object at index in PIL form

        Note: not using self.__getitem__(), as any transformations passed in
        could mess up this functionality.

        Argument:
            index (int): index of img to show
        Return:
            cv2 img
        rr�)	rr\r�r�r�rrrY�IMREAD_COLOR)r!r�r�rOrrr�
pull_image�s
zCOCO17Detection.pull_imagecCs:|j|}|jj|d�}|jj|�}|j|dd�}||fS)a�Returns the original annotation of image at index

        Note: not using self.__getitem__(), as any transformations passed in
        could mess up this functionality.

        Argument:
            index (int): index of img to get annotation of
        Return:
            list:  [img_id, [(label, bbox coords),...]]
                eg: ('001718', [('dog', (96, 13, 438, 332))])
        )r�r)rr\r�r�ri)r!r�r�r��annor�rrr�	pull_anno�s

zCOCO17Detection.pull_annocCs�|j|}|jj|ggd��}|d}|d}tjt|j��}t|�dkrXd|tj|�<tjt|j��}t|�dkr�d|tj|�<||fS)N)�imagerfr�rfrr)rrV�getr'�zerosrkr r()r!r�r�Zadd_annor�r�Zimg_ohZtext_ohrrrr��s
zCOCO17Detection.pull_add_annoscCsf|j|}|jj|d�}|jj|�}g}x8|D]0}|djd�jd�}dd�|D�}|j|�q.W|S)N)r��caption�.� cSsg|]}|j��qSr)�lower)r@�arrrrB�sz-COCO17Detection.pull_text.<locals>.<listcomp>)rr^r�r�rr
�extend)r!r�r�r�Zannosrfr��caprrr�	pull_text�s

zCOCO17Detection.pull_textcsL�j|�}�fdd�|D�}tjdd�|D��}�j|}tj|jtj��S)Ncsg|]}�jj|d��qS)�oov)rvr�)r@�t)r!rrrB�sz5COCO17Detection.pull_text_feature.<locals>.<listcomp>cSsg|]}|dkr|�qS)r�r)r@r�rrrrB�s)r�r'r(r|r�r�r�r�)r!r�rfZ	token_idsZ	token_embr)r!rr��s


z!COCO17Detection.pull_text_featurecCs2|j|}|j|}t|d�dk}|djt�S)N�logitsg�?r)rrr�sigmoidr��float)r!r�r�r:�predrrrr��s

z%COCO17Detection.pull_extracted_labelscCs|j|}|j|}|S)N)rrr)r!r�r�r:rrr�
pull_metadata�s

zCOCO17Detection.pull_metadatacCsp|j|}tj|jjt|�j���}t|d�}tj	t
j|d�tjd�j
|d�}|dks^t�tj|jtj��S)N�	num_boxes�features)�dtypei�$)rrSrTrur��str�encoderr'�
frombuffer�base64�	b64decoder��reshaper�r�r�r�)r!r�r�Zbua_metadatar�Zbua_featurerrrr��s
z"COCO17Detection.pull_image_featurecCs:|j|�\}}d}tj|�}x|D]}d||d<q"W|S)N�ArrM)r�r'r�)r!r�r�r�Zcat_numr�grrrr��s

z&COCO17Detection.pull_image_level_annosc	Cs�d|jjd}|dj|j��7}|dj|j�7}d}|dj||jj�jdddt|���7}d}|d	j||j	j�jdddt|���7}|S)
NzDataset r
z    Number of datapoints: {}
z    Root Location: {}
z    Transforms (if any): z{0}{1}
r�z     Target Transforms (if any): z{0}{1})
�	__class__r1r[r�rYrh�__repr__rnrkri)r!�fmt_str�tmprrrr�s**zCOCO17Detection.__repr__)r1r2r3r4rr5r"rmrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr<�s.	a	*(#
	r<cCsddtj|�}|S)Nr)r'�exp)�x�srrrr�sr�)*r4�configrr_�os.pathrOrrNr��torch.utils.data�utils�dataZtorchvision.transforms�
transformsr��numpyr'rx�utils.loggerrrSrsr�r�rrrXrrPrZr]Z	LABEL_SETrr(Zselected_catr�objectrr5�Datasetr<r�rrrr�<module>	sJ


)&u
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API