@@ -854,7 +854,7 @@ def _annotation_file_name(self, fold, train):
854854
855855 def _create_annotation_file (self , root , name , video_files ):
856856 with open (pathlib .Path (root ) / name , "w" ) as fh :
857- fh .writelines (f"{ file } \n " for file in sorted (video_files ))
857+ fh .writelines (f"{ str ( file ). replace ( os . sep , '/' ) } \n " for file in sorted (video_files ))
858858
859859
860860class LSUNTestCase (datasets_utils .ImageDatasetTestCase ):
@@ -2169,6 +2169,194 @@ def inject_fake_data(self, tmpdir, config):
21692169 return num_sequences * (num_examples_per_sequence - 1 )
21702170
21712171
2172+ class Food101TestCase (datasets_utils .ImageDatasetTestCase ):
2173+ DATASET_CLASS = datasets .Food101
2174+ FEATURE_TYPES = (PIL .Image .Image , int )
2175+
2176+ ADDITIONAL_CONFIGS = datasets_utils .combinations_grid (split = ("train" , "test" ))
2177+
2178+ def inject_fake_data (self , tmpdir : str , config ):
2179+ root_folder = pathlib .Path (tmpdir ) / "food-101"
2180+ image_folder = root_folder / "images"
2181+ meta_folder = root_folder / "meta"
2182+
2183+ image_folder .mkdir (parents = True )
2184+ meta_folder .mkdir ()
2185+
2186+ num_images_per_class = 5
2187+
2188+ metadata = {}
2189+ n_samples_per_class = 3 if config ["split" ] == "train" else 2
2190+ sampled_classes = ("apple_pie" , "crab_cakes" , "gyoza" )
2191+ for cls in sampled_classes :
2192+ im_fnames = datasets_utils .create_image_folder (
2193+ image_folder ,
2194+ cls ,
2195+ file_name_fn = lambda idx : f"{ idx } .jpg" ,
2196+ num_examples = num_images_per_class ,
2197+ )
2198+ metadata [cls ] = [
2199+ "/" .join (fname .relative_to (image_folder ).with_suffix ("" ).parts )
2200+ for fname in random .choices (im_fnames , k = n_samples_per_class )
2201+ ]
2202+
2203+ with open (meta_folder / f"{ config ['split' ]} .json" , "w" ) as file :
2204+ file .write (json .dumps (metadata ))
2205+
2206+ return len (sampled_classes * n_samples_per_class )
2207+
2208+
2209+ class DTDTestCase (datasets_utils .ImageDatasetTestCase ):
2210+ DATASET_CLASS = datasets .DTD
2211+ FEATURE_TYPES = (PIL .Image .Image , int )
2212+
2213+ ADDITIONAL_CONFIGS = datasets_utils .combinations_grid (
2214+ split = ("train" , "test" , "val" ),
2215+ # There is no need to test the whole matrix here, since each fold is treated exactly the same
2216+ partition = (1 , 5 , 10 ),
2217+ )
2218+
2219+ def inject_fake_data (self , tmpdir : str , config ):
2220+ data_folder = pathlib .Path (tmpdir ) / "dtd" / "dtd"
2221+
2222+ num_images_per_class = 3
2223+ image_folder = data_folder / "images"
2224+ image_files = []
2225+ for cls in ("banded" , "marbled" , "zigzagged" ):
2226+ image_files .extend (
2227+ datasets_utils .create_image_folder (
2228+ image_folder ,
2229+ cls ,
2230+ file_name_fn = lambda idx : f"{ cls } _{ idx :04d} .jpg" ,
2231+ num_examples = num_images_per_class ,
2232+ )
2233+ )
2234+
2235+ meta_folder = data_folder / "labels"
2236+ meta_folder .mkdir ()
2237+ image_ids = [str (path .relative_to (path .parents [1 ])).replace (os .sep , "/" ) for path in image_files ]
2238+ image_ids_in_config = random .choices (image_ids , k = len (image_files ) // 2 )
2239+ with open (meta_folder / f"{ config ['split' ]} { config ['partition' ]} .txt" , "w" ) as file :
2240+ file .write ("\n " .join (image_ids_in_config ) + "\n " )
2241+
2242+ return len (image_ids_in_config )
2243+
2244+
2245+ class FER2013TestCase (datasets_utils .ImageDatasetTestCase ):
2246+ DATASET_CLASS = datasets .FER2013
2247+ ADDITIONAL_CONFIGS = datasets_utils .combinations_grid (split = ("train" , "test" ))
2248+
2249+ FEATURE_TYPES = (PIL .Image .Image , (int , type (None )))
2250+
2251+ def inject_fake_data (self , tmpdir , config ):
2252+ base_folder = os .path .join (tmpdir , "fer2013" )
2253+ os .makedirs (base_folder )
2254+
2255+ num_samples = 5
2256+ with open (os .path .join (base_folder , f"{ config ['split' ]} .csv" ), "w" , newline = "" ) as file :
2257+ writer = csv .DictWriter (
2258+ file ,
2259+ fieldnames = ("emotion" , "pixels" ) if config ["split" ] == "train" else ("pixels" ,),
2260+ quoting = csv .QUOTE_NONNUMERIC ,
2261+ quotechar = '"' ,
2262+ )
2263+ writer .writeheader ()
2264+ for _ in range (num_samples ):
2265+ row = dict (
2266+ pixels = " " .join (
2267+ str (pixel ) for pixel in datasets_utils .create_image_or_video_tensor ((48 , 48 )).view (- 1 ).tolist ()
2268+ )
2269+ )
2270+ if config ["split" ] == "train" :
2271+ row ["emotion" ] = str (int (torch .randint (0 , 7 , ())))
2272+
2273+ writer .writerow (row )
2274+
2275+ return num_samples
2276+
2277+
2278+ class GTSRBTestCase (datasets_utils .ImageDatasetTestCase ):
2279+ DATASET_CLASS = datasets .GTSRB
2280+ FEATURE_TYPES = (PIL .Image .Image , int )
2281+
2282+ ADDITIONAL_CONFIGS = datasets_utils .combinations_grid (train = (True , False ))
2283+
2284+ def inject_fake_data (self , tmpdir : str , config ):
2285+ root_folder = os .path .join (tmpdir , "GTSRB" )
2286+ os .makedirs (root_folder , exist_ok = True )
2287+
2288+ # Train data
2289+ train_folder = os .path .join (root_folder , "Training" )
2290+ os .makedirs (train_folder , exist_ok = True )
2291+
2292+ num_examples = 3
2293+ classes = ("00000" , "00042" , "00012" )
2294+ for class_idx in classes :
2295+ datasets_utils .create_image_folder (
2296+ train_folder ,
2297+ name = class_idx ,
2298+ file_name_fn = lambda image_idx : f"{ class_idx } _{ image_idx :05d} .ppm" ,
2299+ num_examples = num_examples ,
2300+ )
2301+
2302+ total_number_of_examples = num_examples * len (classes )
2303+ # Test data
2304+ test_folder = os .path .join (root_folder , "Final_Test" , "Images" )
2305+ os .makedirs (test_folder , exist_ok = True )
2306+
2307+ with open (os .path .join (root_folder , "GT-final_test.csv" ), "w" ) as csv_file :
2308+ csv_file .write ("Filename;Width;Height;Roi.X1;Roi.Y1;Roi.X2;Roi.Y2;ClassId\n " )
2309+
2310+ for _ in range (total_number_of_examples ):
2311+ image_file = datasets_utils .create_random_string (5 , string .digits ) + ".ppm"
2312+ datasets_utils .create_image_file (test_folder , image_file )
2313+ row = [
2314+ image_file ,
2315+ torch .randint (1 , 100 , size = ()).item (),
2316+ torch .randint (1 , 100 , size = ()).item (),
2317+ torch .randint (1 , 100 , size = ()).item (),
2318+ torch .randint (1 , 100 , size = ()).item (),
2319+ torch .randint (1 , 100 , size = ()).item (),
2320+ torch .randint (1 , 100 , size = ()).item (),
2321+ torch .randint (0 , 43 , size = ()).item (),
2322+ ]
2323+ csv_file .write (";" .join (map (str , row )) + "\n " )
2324+
2325+ return total_number_of_examples
2326+
2327+
2328+ class CLEVRClassificationTestCase (datasets_utils .ImageDatasetTestCase ):
2329+ DATASET_CLASS = datasets .CLEVRClassification
2330+ FEATURE_TYPES = (PIL .Image .Image , (int , type (None )))
2331+
2332+ ADDITIONAL_CONFIGS = datasets_utils .combinations_grid (split = ("train" , "val" , "test" ))
2333+
2334+ def inject_fake_data (self , tmpdir , config ):
2335+ data_folder = pathlib .Path (tmpdir ) / "clevr" / "CLEVR_v1.0"
2336+
2337+ images_folder = data_folder / "images"
2338+ image_files = datasets_utils .create_image_folder (
2339+ images_folder , config ["split" ], lambda idx : f"CLEVR_{ config ['split' ]} _{ idx :06d} .png" , num_examples = 5
2340+ )
2341+
2342+ scenes_folder = data_folder / "scenes"
2343+ scenes_folder .mkdir ()
2344+ if config ["split" ] != "test" :
2345+ with open (scenes_folder / f"CLEVR_{ config ['split' ]} _scenes.json" , "w" ) as file :
2346+ json .dump (
2347+ dict (
2348+ info = dict (),
2349+ scenes = [
2350+ dict (image_filename = image_file .name , objects = [dict ()] * int (torch .randint (10 , ())))
2351+ for image_file in image_files
2352+ ],
2353+ ),
2354+ file ,
2355+ )
2356+
2357+ return len (image_files )
2358+
2359+
21722360class OxfordIIITPetTestCase (datasets_utils .ImageDatasetTestCase ):
21732361 DATASET_CLASS = datasets .OxfordIIITPet
21742362 FEATURE_TYPES = (PIL .Image .Image , (int , PIL .Image .Image , tuple , type (None )))
0 commit comments