o
    Ni('                     @   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ZddlZ	ddl
m  mZ ddlmZ dZdZg dZd	d
gZdZdZdZG dd dejjZG dd dejjZdd Zdd Zdd Zdd Z dS )z?BigEarthNet remote sensing dataset of Sentinel-2 image patches.    )absolute_import)division)print_functionNa  @article{Sumbul2019BigEarthNetAL,
  title={BigEarthNet: A Large-Scale Benchmark Archive For Remote Sensing Image Understanding},
  author={Gencer Sumbul and Marcela Charfuelan and Beg{"u}m Demir and Volker Markl},
  journal={CoRR},
  year={2019},
  volume={abs/1902.06148}
}a  The BigEarthNet is a new large-scale Sentinel-2 benchmark archive, consisting of
590,326 Sentinel-2 image patches. The image patch size on the ground is
1.2 x 1.2 km with variable image size depending on the channel resolution.
This is a multi-label dataset with 43 imbalanced labels.

To construct the BigEarthNet, 125 Sentinel-2
tiles acquired between June 2017 and May 2018 over the 10 countries (Austria,
Belgium, Finland, Ireland, Kosovo, Lithuania, Luxembourg, Portugal, Serbia,
Switzerland) of Europe were initially selected. All the tiles were
atmospherically corrected by the Sentinel-2 Level 2A product generation and
formatting tool (sen2cor). Then, they were divided into 590,326 non-overlapping
image patches. Each image patch was annotated by the multiple land-cover classes
(i.e., multi-labels) that were provided from the CORINE Land Cover database of
the year 2018 (CLC 2018).

Bands and pixel resolution in meters:

* B01: Coastal aerosol; 60m
* B02: Blue; 10m
* B03: Green; 10m
* B04: Red; 10m
* B05: Vegetation red edge; 20m
* B06: Vegetation red edge; 20m
* B07: Vegetation red edge; 20m
* B08: NIR; 10m
* B09: Water vapor; 60m
* B11: SWIR; 20m
* B12: SWIR; 20m
* B8A: Narrow NIR; 20m

License: Community Data License Agreement - Permissive, Version 1.0.

URL: http://bigearth.net/
)+zAgro-forestry areasAirportsz,Annual crops associated with permanent cropsz	Bare rockzBeaches, dunes, sandszBroad-leaved forestzBurnt areaszCoastal lagoonszComplex cultivation patternszConiferous forestzConstruction siteszContinuous urban fabriczDiscontinuous urban fabricz
Dump sites	Estuariesz!Fruit trees and berry plantationszGreen urban areaszIndustrial or commercial unitszInland marsheszIntertidal flatszVLand principally occupied by agriculture, with significant areas of natural vegetationzMineral extraction siteszMixed forestzMoors and heathlandzNatural grasslandzNon-irrigated arable landzOlive grovesPasturesPeatbogszPermanently irrigated landz
Port areaszRice fieldsz*Road and rail networks and associated landSalineszSalt marsheszSclerophyllous vegetationzSea and oceanzSparsely vegetated areaszSport and leisure facilitieszTransitional woodland/shrub	VineyardszWater bodieszWater coursesrgballz5http://bigearth.net/downloads/BigEarthNet-v1.0.tar.gzzBigEarthNet-v1.0g     @@c                       s"   e Zd ZdZd fdd	Z  ZS )BigearthnetConfigzBuilderConfig for Bigearthnet.Nc                    sF   |t vr
tdt  tjdd}tt| jdd|i| || _dS )zConstructs a BigearthnetConfig.

    Args:
      selection: `str`, one of `_DATA_OPTIONS`.
      **kwargs: keyword arguments forwarded to super.
    zselection must be one of %sz1.0.0z6New split API (https://tensorflow.org/datasets/splits)versionN )	_DATA_OPTIONS
ValueErrortfdscoreVersionsuperr   __init__	selection)selfr   kwargsv100	__class__r   h/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image_classification/bigearthnet.pyr   o   s   
zBigearthnetConfig.__init__)N)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r   l   s    r   c                   @   sD   e Zd ZdZeddddeddddgZdd Zd	d
 Zdd ZdS )Bigearthnetz?Bigearthnet remote sensing dataset of Sentinel-2 image patches.r   zSentinel-2 RGB channels)r   namedescriptionr   z13 Sentinel-2 channelsc                 C   s  t jt j tjtjtjtjdt j t j d}| jjdkrDt jt jjg ddt j	t jj
tdt j |d}d}n| jjd	krt jt jjd
d
gtjdt jjddgtjdt jjddgtjdt jjddgtjdt jjddgtjdt jjddgtjdt jjddgtjdt jjddgtjdt jjd
d
gtjdt jjddgtjdt jjddgtjdt jjddgtjdt j	t jj
tdt j |d}d }t jj| t||dtdS )N)lrxlryulxuly)acquisition_datecoordinates
projectiontile_sourcer   )x   r.      )shape)names)imagelabelsfilenamemetadata)r2   r3   r      )r0   dtyper.   <   )B01B02B03B04B05B06B07B08B09B11B12B8Ar3   r4   r5   zhttp://bigearth.net)builderr%   featuressupervised_keyshomepagecitation)r   rF   FeaturesDictTexttfint64builder_configr   ImageSequence
ClassLabel_LABELSTensorfloat32r   DatasetInfo_DESCRIPTION	_CITATION)r   metadata_dictrF   rG   r   r   r   _info   sZ   
 zBigearthnet._infoc                 C   s$   | t}tjjtjjd|idgS )zReturns SplitGenerators.archive_path)r$   
gen_kwargs)download	_ZIP_FILEr   r   SplitGeneratorSplitTRAIN)r   
dl_managerdl_pathr   r   r   _split_generators   s   
zBigearthnet._split_generatorsc                 C   sT   t jjj}| jj}|d||g? B d|t|? B d|j	
 ? B d|t|? B S )zGenerates examples as dicts.ArchivePathReadArchive	ReshuffleProcessExamples)r   r   lazy_importsapache_beamrN   r   CreateFlatMap_read_archive
transformsrf   Map_process_example)r   pipelinerZ   beamr   r   r   r   _build_pcollection   s   
zBigearthnet._build_pcollectionN)	r   r   r    r!   r   BUILDER_CONFIGSrY   rc   rr   r   r   r   r   r#      s     Dr#   c           
      c   s    i }d}t jjj| D ]Z\}}|d7 }|tjj\}}}|	dr+|
 |d< n-|	drR|dd }	|dksB|dkrQ|	d	v rQ|
 ||	< |d
g |	 ntd| |dkrg||d< |V  i }d}qdS )z-Yields non-processed examples out of archive.r      z_labels_metadata.jsonr5   z.tifir   >   r:   r;   r<   bandszUnexpected file: %s   r4   N)r   r   r\   	extractoriter_tar_streamsplitospathsependswithread
setdefaultappendAssertionError)
rZ   r   exampleread_band_filesfpathfobj_
patch_namefnamebandr   r   r   rl      s2   


rl   c                 C   sl   |   } t| d | d< | d d| d< | dpg D ]
}t| | | |< q|dkr0t|  | d | fS )Nr5   r3   rv   r   r4   )copyjsonloadspop	_load_tif_create_rgb_image)r   r   r   r   r   r   ro     s   ro   c                 C   sP   t j| d| d| dgdd}|t d }t |ddt j| d	< d
S )zCreates and rescales RGB image.r<   r;   r:      )axisg     o@r      r2   N)npstackr   _OPTICAL_MAX_VALUEclipastypeuint8)dimgr   r   r   r     s   &r   c                 C   s8   t jjjt| }t|	 
|jtj}|S )z2Loads TIF file and returns as float32 numpy array.)r   r   rh   	PIL_ImageopenioBytesIOr   arraygetdatareshapesizer   rT   )datar   r   r   r   r     s   r   )!r!   
__future__r   r   r   r   r   r{   numpyr   tensorflow.compat.v2compatv2rL   tensorflow_datasets.public_api
public_apir   rW   rV   rR   r   r]   _ZIP_SUBIDRr   r   BuilderConfigr   BeamBasedBuilderr#   rl   ro   r   r   r   r   r   r   <module>   s.   	$j