33import numpy
44import os
55import pickle
6- import six .moves .urllib
76
87import tools .tools as tls
98
10- def create_bsds (source_url , path_to_folder_bsds_original , path_to_bsds , path_to_list_rotation , path_to_tar = '' ):
9+ def create_bsds (source_url , path_to_folder_rgbs , path_to_bsds , path_to_list_rotation , path_to_tar = '' ):
1110 """Creates the BSDS test set.
1211
13- 100 BSDS RGB images are converted into luminance
14- images. The 1st row and the 1st column of each
15- luminance image are removed. Then, sideways
16- luminance images are rotated. Finally, the BSDS
17- test set is filled with the luminance images and
18- it is saved.
12+ 100 BSDS RGB images are converted into luminance. The
13+ 1st row and the 1st column of each luminance image are
14+ removed. Then, sideways luminance images are rotated.
15+ Finally, the BSDS test set is filled with the luminance
16+ images and it is saved.
1917
2018 Parameters
2119 ----------
2220 source_url : str
2321 URL of the original BSDS dataset.
24- path_to_folder_bsds_original : str
25- Path to the folder to which the original BSDS
26- dataset (training RGB images and test RGB images)
27- is extracted.
22+ path_to_folder_rgbs : str
23+ Path to the folder to which the original BSDS dataset
24+ (training RGB images and test RGB images) is extracted.
2825 path_to_bsds : str
2926 Path to the file in which the BSDS test
3027 set is saved. The path ends with ".npy".
@@ -37,8 +34,8 @@ def create_bsds(source_url, path_to_folder_bsds_original, path_to_bsds, path_to_
3734 Path to the downloaded archive containing the original
3835 BSDS dataset. The default value is ''. If the path
3936 is not the default path, the archive is extracted
40- to `path_to_folder_bsds_original ` before the function
41- starts creating the BSDS test set.
37+ to `path_to_folder_rgbs ` before the function starts
38+ creating the BSDS test set.
4239
4340 Raises
4441 ------
@@ -54,33 +51,38 @@ def create_bsds(source_url, path_to_folder_bsds_original, path_to_bsds, path_to_
5451 print ('"{0}" and "{1}" already exist.' .format (path_to_bsds , path_to_list_rotation ))
5552 print ('Delete them manually to recreate the BSDS test set.' )
5653 else :
57- download_option (source_url ,
58- path_to_folder_bsds_original ,
59- path_to_tar = path_to_tar )
60- h_bsds = 321
61- w_bsds = 481
54+ if path_to_tar :
55+ is_downloaded = tls .download_untar_archive (source_url ,
56+ path_to_folder_rgbs ,
57+ path_to_tar )
58+ if is_downloaded :
59+ print ('Successfully downloaded "{}".' .format (path_to_tar ))
60+ else :
61+ print ('"{}" already exists.' .format (path_to_tar ))
62+ print ('Delete it manually to re-download it.' )
6263
63- # The height and the width of luminance images we
64- # feed into the autoencoders must be divisible by 16.
65- reference_uint8 = numpy .zeros ((100 , h_bsds - 1 , w_bsds - 1 ), dtype = numpy .uint8 )
64+ # The height and width of the luminance images we
65+ # feed into the autoencoders has to be divisible by 16.
66+ height_bsds = 321
67+ width_bsds = 481
68+ reference_uint8 = numpy .zeros ((100 , height_bsds - 1 , width_bsds - 1 ), dtype = numpy .uint8 )
6669 list_rotation = []
6770
6871 # `os.listdir` returns a list whose order depends on the OS.
6972 # To make `create_bsds` independent of the OS, the output of
7073 # `os.listdir` is sorted.
71- path_to_folder_test = os .path .join (path_to_folder_bsds_original ,
74+ path_to_folder_test = os .path .join (path_to_folder_rgbs ,
7275 'BSDS300/images/test/' )
73- list_names = clean_sort_list_strings (os .listdir (path_to_folder_test ),
74- 'jpg' )
76+ list_names = tls . clean_sort_list_strings (os .listdir (path_to_folder_test ),
77+ 'jpg' )
7578 if len (list_names ) != 100 :
7679 raise RuntimeError ('The number of BSDS RGB images to be read is not 100.' )
7780 for i in range (100 ):
7881 path_to_file = os .path .join (path_to_folder_test ,
7982 list_names [i ])
8083
81- # The function `tls.read_image_mode` is not put
82- # into a `try` `except` condition as each BSDS300
83- # RGB image has to be read.
84+ # `tls.read_image_mode` is not put into a `try` `except` clause
85+ # as each BSDS300 RGB image has to be read.
8486 rgb_uint8 = tls .read_image_mode (path_to_file ,
8587 'RGB' )
8688
@@ -90,72 +92,17 @@ def create_bsds(source_url, path_to_folder_bsds_original, path_to_bsds, path_to_
9092 # and its 3rd dimension is equal to 3.
9193 luminance_uint8 = tls .rgb_to_ycbcr (rgb_uint8 )[:, :, 0 ]
9294 (height_image , width_image ) = luminance_uint8 .shape
93- if height_image == h_bsds and width_image == w_bsds :
94- reference_uint8 [i , :, :] = luminance_uint8 [1 :h_bsds , 1 :w_bsds ]
95- elif width_image == h_bsds and height_image == w_bsds :
96- reference_uint8 [i , :, :] = numpy .rot90 (luminance_uint8 [1 :w_bsds , 1 :h_bsds ])
95+ if height_image == height_bsds and width_image == width_bsds :
96+ reference_uint8 [i , :, :] = luminance_uint8 [1 :height_bsds , 1 :width_bsds ]
97+ elif width_image == height_bsds and height_image == width_bsds :
98+ reference_uint8 [i , :, :] = numpy .rot90 (luminance_uint8 [1 :width_bsds , 1 :height_bsds ])
9799 list_rotation .append (i )
98100 else :
99- raise ValueError ('"{0}" is neither {1}x{2}x3 nor {2}x{1}x3.' .format (path_to_file , h_bsds , w_bsds ))
101+ raise ValueError ('"{0}" is neither {1}x{2}x3 nor {2}x{1}x3.' .format (path_to_file , height_bsds , width_bsds ))
100102
101103 numpy .save (path_to_bsds ,
102104 reference_uint8 )
103105 with open (path_to_list_rotation , 'wb' ) as file :
104106 pickle .dump (list_rotation , file , protocol = 2 )
105107
106- def clean_sort_list_strings (list_strings , extension ):
107- """Removes from the list the strings that do not end with the given extension and sorts the list.
108-
109- Parameters
110- ----------
111- list_strings : list
112- List of strings.
113- extension : str
114- Given extension.
115-
116- Returns
117- -------
118- list
119- New list which contains the strings that
120- end with the given extension. This list
121- is sorted.
122-
123- """
124- list_strings_extension = [string for string in list_strings if string .endswith (extension )]
125- list_strings_extension .sort ()
126- return list_strings_extension
127-
128- def download_option (source_url , path_to_folder_bsds_original , path_to_tar = '' ):
129- """Downloads the original BSDS dataset and extracts it.
130-
131- Parameters
132- ----------
133- source_url : str
134- URL of the original BSDS dataset.
135- path_to_folder_bsds_original : str
136- Path to the folder to which the original BSDS
137- dataset (training RGB images and test RGB images)
138- is extracted.
139- path_to_tar : str, optional
140- Path to the downloaded archive containing the original
141- BSDS dataset. The default value is ''. If the path
142- is not the default path, the archive is extracted
143- to `path_to_folder_bsds_original` before the function
144- starts creating the BSDS test set.
145-
146- """
147- if path_to_tar :
148- if os .path .isfile (path_to_tar ):
149- print ('"{}" already exists.' .format (path_to_tar ))
150- print ('Delete it manually to re-download it.' )
151- else :
152- six .moves .urllib .request .urlretrieve (source_url ,
153- path_to_tar )
154- print ('Successfully downloaded "{}".' .format (path_to_tar ))
155-
156- # If the same extraction is run two times in a row,
157- # the result of the first extraction is overwritten.
158- tls .untar_archive (path_to_folder_bsds_original ,
159- path_to_tar )
160-
161108
0 commit comments