Upload NewDataset.py with huggingface_hub
Browse files- NewDataset.py +11 -8
NewDataset.py
CHANGED
|
@@ -6,7 +6,7 @@ import zipfile
|
|
| 6 |
|
| 7 |
class NewDataset(datasets.GeneratorBasedBuilder):
|
| 8 |
def _info(self):
|
| 9 |
-
return datasets.DatasetInfo(features=datasets.Features({'image':datasets.Image(),'label':datasets.
|
| 10 |
|
| 11 |
def extract_all(self, dir):
|
| 12 |
zip_files = glob(dir+'/**/**.zip', recursive=True)
|
|
@@ -25,9 +25,14 @@ class NewDataset(datasets.GeneratorBasedBuilder):
|
|
| 25 |
def _split_generators(self, dl_manager):
|
| 26 |
url = [os.path.abspath(os.path.expanduser(dl_manager.manual_dir))]
|
| 27 |
downloaded_files = dl_manager.download_and_extract(url)
|
| 28 |
-
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'filepaths':{'inputs':sorted(glob(downloaded_files[0]+'/data
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def read_image(self, filepath):
|
| 32 |
if filepath.endswith('.jpg') or filepath.endswith('.png'):
|
| 33 |
raw_data = {'bytes':[filepath]}
|
|
@@ -39,13 +44,11 @@ class NewDataset(datasets.GeneratorBasedBuilder):
|
|
| 39 |
_id = 0
|
| 40 |
for i,filepath in enumerate(filepaths['inputs']):
|
| 41 |
df = self.read_image(filepath)
|
| 42 |
-
|
| 43 |
-
dfs.append(self.read_image(filepaths['targets1'][i]))
|
| 44 |
-
df = pd.concat(dfs, axis = 1)
|
| 45 |
-
if len(df.columns) != 2:
|
| 46 |
continue
|
| 47 |
-
df.columns = ['image'
|
|
|
|
| 48 |
for _, record in df.iterrows():
|
| 49 |
-
yield str(_id), {'image':record['image'],'label':
|
| 50 |
_id += 1
|
| 51 |
|
|
|
|
| 6 |
|
| 7 |
class NewDataset(datasets.GeneratorBasedBuilder):
|
| 8 |
def _info(self):
|
| 9 |
+
return datasets.DatasetInfo(features=datasets.Features({'image':datasets.Image(),'label': datasets.features.ClassLabel(names=['dogs', 'cats'])}))
|
| 10 |
|
| 11 |
def extract_all(self, dir):
|
| 12 |
zip_files = glob(dir+'/**/**.zip', recursive=True)
|
|
|
|
| 25 |
def _split_generators(self, dl_manager):
|
| 26 |
url = [os.path.abspath(os.path.expanduser(dl_manager.manual_dir))]
|
| 27 |
downloaded_files = dl_manager.download_and_extract(url)
|
| 28 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={'filepaths':{'inputs':sorted(glob(downloaded_files[0]+'/data/**/**.png')),} })]
|
| 29 |
|
| 30 |
|
| 31 |
+
def get_label_from_path(self, labels, label):
|
| 32 |
+
for l in labels:
|
| 33 |
+
if l == label:
|
| 34 |
+
return label
|
| 35 |
+
|
| 36 |
def read_image(self, filepath):
|
| 37 |
if filepath.endswith('.jpg') or filepath.endswith('.png'):
|
| 38 |
raw_data = {'bytes':[filepath]}
|
|
|
|
| 44 |
_id = 0
|
| 45 |
for i,filepath in enumerate(filepaths['inputs']):
|
| 46 |
df = self.read_image(filepath)
|
| 47 |
+
if len(df.columns) != 1:
|
|
|
|
|
|
|
|
|
|
| 48 |
continue
|
| 49 |
+
df.columns = ['image']
|
| 50 |
+
label = self.get_label_from_path(['dogs', 'cats'], filepath.split('/')[-2])
|
| 51 |
for _, record in df.iterrows():
|
| 52 |
+
yield str(_id), {'image':record['image'],'label':str(label)}
|
| 53 |
_id += 1
|
| 54 |
|