rshrott commited on
Commit
498fbdd
1 Parent(s): d2ef3dd

Update renovation.py

Browse files
Files changed (1) hide show
  1. renovation.py +34 -28
renovation.py CHANGED
@@ -3,6 +3,7 @@ import random
3
  import datasets
4
  import requests
5
  import os
 
6
 
7
  from PIL import Image
8
  from io import BytesIO
@@ -24,7 +25,11 @@ _DESCRIPTION = """\
24
  This dataset contains images of various properties, along with labels indicating the quality of renovation - 'cheap', 'average', 'expensive'.
25
  """
26
 
27
- _URL = "https://huggingface.co/datasets/rshrott/renovation/raw/main/labels.csv"
 
 
 
 
28
 
29
  _NAMES = ["cheap", "average", "expensive"]
30
 
@@ -50,54 +55,55 @@ class RenovationQualityDataset(datasets.GeneratorBasedBuilder):
50
  )
51
 
52
  def _split_generators(self, dl_manager):
53
- csv_path = dl_manager.download(_URL)
54
- with open(csv_path, "r") as f:
55
- reader = csv.reader(f)
56
- next(reader) # skip header
57
- rows = list(reader)
58
-
59
- # Shuffle rows
60
- random.shuffle(rows)
 
 
 
 
 
 
61
 
62
  # 80% for training, 10% for validation, 10% for testing
63
- train_end = int(0.8 * len(rows))
64
- val_end = int(0.9 * len(rows))
65
 
66
  return [
67
  datasets.SplitGenerator(
68
  name=datasets.Split.TRAIN,
69
  gen_kwargs={
70
- "rows": rows[:train_end],
71
  },
72
  ),
73
  datasets.SplitGenerator(
74
  name=datasets.Split.VALIDATION,
75
  gen_kwargs={
76
- "rows": rows[train_end:val_end],
77
  },
78
  ),
79
  datasets.SplitGenerator(
80
  name=datasets.Split.TEST,
81
  gen_kwargs={
82
- "rows": rows[val_end:],
83
  },
84
  ),
85
  ]
86
 
87
  def _generate_examples(self, rows):
88
- def url_to_image(url):
89
- response = requests.get(url)
90
- img = Image.open(BytesIO(response.content))
91
  return img
92
 
93
- for id_, row in enumerate(rows):
94
- if len(row) < 2:
95
- print(f"Row with id {id_} has less than 2 elements: {row}")
96
- else:
97
- image_file_path = str(row[0])
98
- image = url_to_image(image_file_path)
99
- yield id_, {
100
- 'image_file_path': image_file_path,
101
- 'image': image,
102
- 'labels': row[1],
103
- }
 
3
  import datasets
4
  import requests
5
  import os
6
+ import py7zr # for extracting .7z files
7
 
8
  from PIL import Image
9
  from io import BytesIO
 
25
  This dataset contains images of various properties, along with labels indicating the quality of renovation - 'cheap', 'average', 'expensive'.
26
  """
27
 
28
+ _URLS = {
29
+ "cheap": "https://huggingface.co/datasets/rshrott/renovation/raw/main/cheap.7z",
30
+ "average": "https://huggingface.co/datasets/rshrott/renovation/raw/main/average.7z",
31
+ "expensive": "https://huggingface.co/datasets/rshrott/renovation/raw/main/expensive.7z",
32
+ }
33
 
34
  _NAMES = ["cheap", "average", "expensive"]
35
 
 
55
  )
56
 
57
  def _split_generators(self, dl_manager):
58
+ # Download and extract images
59
+ image_paths = []
60
+ for label, url in _URLS.items():
61
+ download_path = dl_manager.download(url)
62
+ extract_path = dl_manager.extract(download_path)
63
+
64
+ # Get image paths
65
+ for root, _, files in os.walk(extract_path):
66
+ for file in files:
67
+ if file.endswith(".jpeg"): # Assuming all images are .jpeg
68
+ image_paths.append((os.path.join(root, file), label))
69
+
70
+ # Shuffle image paths
71
+ random.shuffle(image_paths)
72
 
73
  # 80% for training, 10% for validation, 10% for testing
74
+ train_end = int(0.8 * len(image_paths))
75
+ val_end = int(0.9 * len(image_paths))
76
 
77
  return [
78
  datasets.SplitGenerator(
79
  name=datasets.Split.TRAIN,
80
  gen_kwargs={
81
+ "rows": image_paths[:train_end],
82
  },
83
  ),
84
  datasets.SplitGenerator(
85
  name=datasets.Split.VALIDATION,
86
  gen_kwargs={
87
+ "rows": image_paths[train_end:val_end],
88
  },
89
  ),
90
  datasets.SplitGenerator(
91
  name=datasets.Split.TEST,
92
  gen_kwargs={
93
+ "rows": image_paths[val_end:],
94
  },
95
  ),
96
  ]
97
 
98
  def _generate_examples(self, rows):
99
+ def file_to_image(file_path):
100
+ img = Image.open(file_path)
 
101
  return img
102
 
103
+ for id_, (image_file_path, label) in enumerate(rows):
104
+ image = file_to_image(image_file_path)
105
+ yield id_, {
106
+ 'image_file_path': image_file_path,
107
+ 'image': image,
108
+ 'labels': label,
109
+ }