o
    Nic1                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	Z
ddlm  mZ ddlmZ ddlmZ dZd	Zd
ZdgZdZdd ZdddZ	dddZG dd dejjZdd ZG dd dejj Z!dS )z#SUN (Scene UNderstanding) datasets.    )absolute_import)division)print_functionN)logging)utilsa  @INPROCEEDINGS{Xiao:2010,
author={J. {Xiao} and J. {Hays} and K. A. {Ehinger} and A. {Oliva} and A. {Torralba}},
booktitle={2010 IEEE Computer Society Conference on Computer Vision and Pattern Recognition},
title={SUN database: Large-scale scene recognition from abbey to zoo},
year={2010},
volume={},
number={},
pages={3485-3492},
keywords={computer vision;human factors;image classification;object recognition;visual databases;SUN database;large-scale scene recognition;abbey;zoo;scene categorization;computer vision;scene understanding research;scene category;object categorization;scene understanding database;state-of-the-art algorithms;human scene classification performance;finer-grained scene representation;Sun;Large-scale systems;Layout;Humans;Image databases;Computer vision;Anthropometry;Bridges;Legged locomotion;Spatial databases}, 
doi={10.1109/CVPR.2010.5539970},
ISSN={1063-6919},
month={June},}
a  The database contains 108,753 images of 397 categories, used in the
Scene UNderstanding (SUN) benchmark. The number of images varies across
categories, but there are at least 100 images per category.

Several configs of the dataset are made available through TFDS:
- A custom (random) partition of the whole dataset with 76,128 training images,
  10,875 validation images and 21,750 test images. Images have been resized to
  have at most 120,000 pixels, and encoded as JPEG with quality of 72.
- "standard-part1-120k", "standard-part2-120k", ..., "standard-part10-120k":
  Each of the 10 official train/test partitions with 50 images per class in each
  split. Images have been resized to have at most 120,000 pixels, and encoded
  as JPEG with quality of 72.
z/https://vision.princeton.edu/projects/2010/SUN/z0SUN397/c/church/outdoor/sun_bhenjvsvrtumjuri.jpgzTrain and test splits from the official partition number %d. Images are resized to have at most %s pixels, and compressed with 72 JPEG quality.c                 C   s   |   }tjjjjtj|tjddd}|du r?t	
d| ztjj|dd}||}W n tjjy>   t	d| Y nw t|jdkrP||jd	d }|S )
aq  Reads and decodes an image from a file object as a Numpy array.

  The SUN dataset contains images in several formats (despite the fact that
  all of them have .jpg extension). Some of them are:
    - BMP (RGB)
    - PNG (grayscale, RGBA, RGB interlaced)
    - JPEG (RGB)
    - GIF (1-frame RGB)
  Since TFDS assumes that all images have the same number of channels, we
  convert all of them to RGB.

  Args:
    fobj: File object to read from.
    session: TF session used to decode the images.
    filename: Filename of the original image in the archive.

  Returns:
    Numpy array with shape (height, width, channels).
  )dtype   )flagsNz;Image %s could not be decoded by OpenCV, falling back to TF)channelsz+Image %s could not be decoded by Tensorflow      )readtfdscorelazy_importscv2imdecodenp
fromstringuint8r   warningtfimagedecode_imagerunerrorsInvalidArgumentErrorfatallenshapereshape)fobjsessionfilenamebufr    r%   `/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image_classification/sun.py_decode_imageK   s"   
r'   c                 C   sH   t jjj}|rt|j|ggng }|jd| g|R  \}}t|	 S )Nz.jpg)
r   r   r   r   intIMWRITE_JPEG_QUALITYimencodeioBytesIOtostring)r   qualityr   
extra_args_buffr%   r%   r&   _encode_jpegs   s   
r2   c                 C   s`   t | ||d}|j\}}}|| }	|r*|	|kr*t||	 }
tjjjj|d|
|
d}t	||dS )z%Process image files from the dataset.)r#   N)dsizefxfy)r.   )
r'   r   r   sqrtr   r   r   r   resizer2   )r!   r"   r#   r.   target_pixelsr   heightwidthr0   actual_pixelsfactorr%   r%   r&   _process_image_filez   s   
r=   c                       sH   e Zd ZdZ	d fdd	Zedd Zedd Zed	d
 Z  Z	S )Sun397Configz"BuilderConfig for Sun 397 dataset.Nc                    s,   || _ || _|| _tt| jdi | d S )Nr%   )_target_pixels
_partition_qualitysuperr>   __init__)selfr8   	partitionr.   kwargs	__class__r%   r&   rC      s   zSun397Config.__init__c                 C      | j S N)r?   rD   r%   r%   r&   r8         zSun397Config.target_pixelsc                 C   rI   rJ   )r@   rK   r%   r%   r&   rE      rL   zSun397Config.partitionc                 C   rI   rJ   )rA   rK   r%   r%   r&   r.      rL   zSun397Config.quality)NNN)
__name__
__module____qualname____doc__rC   propertyr8   rE   r.   __classcell__r%   r%   rG   r&   r>      s    

r>   c               
   C   sZ   t jd} td| dddg}tddD ]}t|df }|td	| |d| |d
 q|S )z8Return the BuilderConfig objects for the SUN397 dataset.z4.0.0r   i zTFDS partition with random train/validation/test splits with 70%/10%/20% of the images, respectively. Images are resized to have at most 120,000 pixels, and are compressed with 72 JPEG quality.)nameversionr8   descriptionr      z120,000zstandard-part%d-120k)rS   rE   r8   rT   rU   )r   r   Versionr>   range*_SUN397_BUILDER_CONFIG_DESCRIPTION_PATTERNappend)rT   builder_configsrE   rU   r%   r%   r&   _generate_builder_configs   s,   r\   c                       sX   e Zd ZdZe Zd fdd	Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Z  ZS )Sun397z#Sun397 Scene Recognition Benchmark.Nc                    s\   t t| jdi | |s)dddd}| D ]\}}tjtj	d|||< q|| _
d S )Nzsun397_tfds_tr.txtzsun397_tfds_te.txtzsun397_tfds_va.txt)trtevaimage_classificationr%   )rB   r]   rC   itemsr   r   get_tfds_pathospathjoin_tfds_split_files)rD   tfds_split_filesrF   splitr#   rG   r%   r&   rC      s   

zSun397.__init__c              
   C   sV   t jtjdd}t jj| tt j	t j
 t jjddt jj|ddttdS )Nra   zsun397_labels.txt)NNr   )r   )
names_file	file_namer   label)builderrU   featureshomepagecitation)r   r   rc   rd   re   rf   DatasetInfo_SUN397_DESCRIPTIONro   FeaturesDictTextImage
ClassLabel_SUN397_URL_SUN397_CITATION)rD   rj   r%   r%   r&   _info   s   zSun397._infoc                 C   sJ  | tjjtd tjjjdtd d}t|ts)t	j
|dt	j
|dd}tjj|d tjjjd}| jjdkrx|  }tjjtjjt|||d	 d
dtjjtjjt|||d d
dtjjtjjt|||d d
dgS | |d }tjjtjjt|||d	 d
dtjjtjjt|||d d
dgS )NzSUN397.tar.gz)urlextract_methodzdownload/Partitions.zip)images
partitions
Partitionsr}   )re   r|   r   r^   )archivesubset_images)rS   
gen_kwargsr_   r`   r~   )download_and_extractr   downloadResourcerx   ExtractMethod
NO_EXTRACT
isinstancedictrd   re   rf   TAR_GZ_STREAMbuilder_configrS   _get_tfds_subsets_imagesr   SplitGeneratorSplitTRAINiter_archiveTEST
VALIDATION_get_partition_subsets_images)rD   
dl_managerpathsr}   r   r%   r%   r&   _split_generators   sn   
zSun397._split_generatorsc              
   c   s    t d}t  ^ t A}|D ]6\}}||d  dd}||v rId|dd d }t	|||| j
j| j
jd}	||	|d}
||
fV  qW d    n1 sTw   Y  W d    d S W d    d S 1 slw   Y  d S )NSUN397\/)r.   r8   rk   )r   r   Graph
as_defaultr   nogpu_sessionreplacerf   ri   r=   r   r.   r8   )rD   r   r   
prefix_lensessfilepathr!   r#   rm   r   recordr%   r%   r&   _generate_examples  s0   

"zSun397._generate_examplesc                 C   s*   i }| j  D ]\}}| |||< q|S rJ   )rg   rb   _load_image_set_from_file)rD   splits_setsri   r   r%   r%   r&   r   +  s   zSun397._get_tfds_subsets_imagesc           	      C   s   t  }|   D ]}|| q	d| jj d| jj d}i }| D ]\}}tj	||}| 
|||< q$||d |d B  |d< |S )NzTraining_%02d.txtzTesting_%02d.txt)r^   r_   r^   r_   r`   )setr   valuesupdater   rE   rb   rd   re   rf   r   )	rD   partitions_dir
all_imagessplit_images	filenamesr   ri   r#   r   r%   r%   r&   r   1  s   

z$Sun397._get_partition_subsets_imagesc                 C   sH   t jjj|dd}tdd |D W  d    S 1 sw   Y  d S )Nr)modec                 S   s   g | ]}|  qS r%   )strip).0liner%   r%   r&   
<listcomp>E  s    z4Sun397._load_image_set_from_file.<locals>.<listcomp>)r   r+   gfileGFiler   )rD   r   fr%   r%   r&   r   C  s   $z Sun397._load_image_set_from_filerJ   )rM   rN   rO   rP   r\   BUILDER_CONFIGSrC   rz   r   r   r   r   r   rR   r%   r%   rG   r&   r]      s    3r]   rJ   )NN)"rP   
__future__r   r   r   r+   rd   abslr   numpyr   tensorflow.compat.v2compatv2r   tensorflow_datasets.corer   tensorflow_datasets.public_api
public_apir   ry   rs   rx   _SUN397_IGNORE_IMAGESrY   r'   r2   r=   r   BuilderConfigr>   r\   GeneratorBasedBuilderr]   r%   r%   r%   r&   <module>   s2   
(
