colin1842 commited on
Commit
f84d8cb
·
1 Parent(s): f029ef5

fix inden err

Browse files
Files changed (1) hide show
  1. script.py +114 -114
script.py CHANGED
@@ -28,7 +28,7 @@
28
  # print(f"Package installed successfully from {pth}")
29
  # except subprocess.CalledProcessError as e:
30
  # print(f"Failed to install package from {pth}. Error: {e}")
31
-
32
  # install_package_from_local_file('hoho')
33
 
34
  import hoho; hoho.setup() # YOU MUST CALL hoho.setup() BEFORE ANYTHING ELSE
@@ -55,7 +55,7 @@ import hoho; hoho.setup() # YOU MUST CALL hoho.setup() BEFORE ANYTHING ELSE
55
  # print(f"Package installed successfully from {pth}")
56
  # except subprocess.CalledProcessError as e:
57
  # print(f"Failed to install package from {pth}. Error: {e}")
58
-
59
 
60
  # pip download webdataset -d packages/webdataset --platform manylinux1_x86_64 --python-version 38 --only-binary=:all:
61
  # install_package_from_local_file('webdataset')
@@ -80,23 +80,23 @@ from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read
80
  from hoho import proc, Sample
81
 
82
  def convert_entry_to_human_readable(entry):
83
- out = {}
84
- already_good = ['__key__', 'wf_vertices', 'wf_edges', 'edge_semantics', 'mesh_vertices', 'mesh_faces', 'face_semantics', 'K', 'R', 't']
85
- for k, v in entry.items():
86
- if k in already_good:
87
- out[k] = v
88
- continue
89
- if k == 'points3d':
90
- out[k] = read_points3D_binary(fid=io.BytesIO(v))
91
- if k == 'cameras':
92
- out[k] = read_cameras_binary(fid=io.BytesIO(v))
93
- if k == 'images':
94
- out[k] = read_images_binary(fid=io.BytesIO(v))
95
- if k in ['ade20k', 'gestalt']:
96
- out[k] = [PImage.open(io.BytesIO(x)).convert('RGB') for x in v]
97
- if k == 'depthcm':
98
- out[k] = [PImage.open(io.BytesIO(x)) for x in entry['depthcm']]
99
- return out
100
 
101
  '''---end of compulsory---'''
102
 
@@ -106,109 +106,109 @@ import sys
106
  import os
107
 
108
  def download_packages(packages, folder='packages/torch'):
109
- """
110
- Downloads packages as .whl files into the specified folder using pip.
111
-
112
- Parameters:
113
- packages (list): List of packages to download with versions.
114
- folder (str): The folder where the .whl files will be saved.
115
- """
116
- Path(folder).mkdir(parents=True, exist_ok=True)
117
- try:
118
- subprocess.check_call([sys.executable, "-m", "pip", "download",
119
- "--platform", "manylinux1_x86_64",
120
- "--python-version", "38",
121
- "--only-binary=:all:",
122
- "-d", folder] + packages)
123
- print(f"Packages downloaded successfully into {folder}")
124
- except subprocess.CalledProcessError as e:
125
- print(f"Failed to download packages. Error: {e}")
126
 
127
  def install_package_from_local_file(package_name, folder='packages'):
128
- """
129
- Installs a package from a local .whl file or a directory containing .whl files using pip.
130
-
131
- Parameters:
132
- package_name (str): The name of the package to install.
133
- folder (str): The folder where the .whl files are located.
134
- """
135
- try:
136
- pth = str(Path(folder) / package_name)
137
- subprocess.check_call([sys.executable, "-m", "pip", "install",
138
- "--no-index", # Do not use package index
139
- "--find-links", pth, # Look for packages in the specified directory or at the file
140
- package_name]) # Specify the package to install
141
- print(f"Package installed successfully from {pth}")
142
- except subprocess.CalledProcessError as e:
143
- print(f"Failed to install package from {pth}. Error: {e}")
144
 
145
  def setup_environment():
146
- subprocess.check_call([sys.executable, "-m", "pip", "install", "git+http://hf.co/usm3d/tools.git"])
147
- import hoho
148
- hoho.setup()
149
-
150
- # Download required packages
151
- packages_to_download = ['torch==2.0.1', 'torchvision==0.15.2', 'torchaudio==2.0.2']
152
- download_packages(packages_to_download, folder='packages/torch')
153
-
154
- # Install packages from local files
155
- install_package_from_local_file('torch', folder='packages/torch')
156
- install_package_from_local_file('torchvision', folder='packages/torch')
157
- install_package_from_local_file('torchaudio', folder='packages/torch')
158
-
159
- subprocess.check_call([sys.executable, "-m", "pip", "install", "scikit-learn"])
160
- subprocess.check_call([sys.executable, "-m", "pip", "install", "tqdm"])
161
- subprocess.check_call([sys.executable, "-m", "pip", "install", "scipy"])
162
- subprocess.check_call([sys.executable, "-m", "pip", "install", "open3d"])
163
- subprocess.check_call([sys.executable, "-m", "pip", "install", "easydict"])
164
-
165
- # pc_util_path = os.path.join(os.getcwd(), 'pc_util')
166
- # if os.path.isdir(pc_util_path):
167
- # os.chdir(pc_util_path)
168
- # subprocess.check_call([sys.executable, "setup.py", "install"])
169
- # else:
170
- # print(f"Directory {pc_util_path} does not exist")
171
-
172
  from pathlib import Path
173
  def save_submission(submission, path):
174
- """
175
- Saves the submission to a specified path.
176
 
177
- Parameters:
178
- submission (List[Dict[]]): The submission to save.
179
- path (str): The path to save the submission to.
180
- """
181
- sub = pd.DataFrame(submission, columns=["__key__", "wf_vertices", "wf_edges"])
182
- sub.to_parquet(path)
183
- print(f"Submission saved to {path}")
184
 
185
  if __name__ == "__main__":
186
  setup_environment()
187
 
188
- from handcrafted_solution import predict
189
- print ("------------ Loading dataset------------ ")
190
- params = hoho.get_params()
191
- dataset = hoho.get_dataset(decode=None, split='all', dataset_type='webdataset')
192
-
193
- print('------------ Now you can do your solution ---------------')
194
- solution = []
195
- from concurrent.futures import ProcessPoolExecutor
196
- with ProcessPoolExecutor(max_workers=8) as pool:
197
- results = []
198
- for i, sample in enumerate(tqdm(dataset)):
199
- results.append(pool.submit(predict, sample, visualize=False))
200
-
201
- for i, result in enumerate(tqdm(results)):
202
- key, pred_vertices, pred_edges = result.result()
203
- solution.append({
204
- '__key__': key,
205
- 'wf_vertices': pred_vertices.tolist(),
206
- 'wf_edges': pred_edges
207
- })
208
- if i % 100 == 0:
209
- # incrementally save the results in case we run out of time
210
- print(f"Processed {i} samples")
211
- # save_submission(solution, Path(params['output_path']) / "submission.parquet")
212
- print('------------ Saving results ---------------')
213
- save_submission(solution, Path(params['output_path']) / "submission.parquet")
214
- print("------------ Done ------------ ")
 
28
  # print(f"Package installed successfully from {pth}")
29
  # except subprocess.CalledProcessError as e:
30
  # print(f"Failed to install package from {pth}. Error: {e}")
31
+
32
  # install_package_from_local_file('hoho')
33
 
34
  import hoho; hoho.setup() # YOU MUST CALL hoho.setup() BEFORE ANYTHING ELSE
 
55
  # print(f"Package installed successfully from {pth}")
56
  # except subprocess.CalledProcessError as e:
57
  # print(f"Failed to install package from {pth}. Error: {e}")
58
+
59
 
60
  # pip download webdataset -d packages/webdataset --platform manylinux1_x86_64 --python-version 38 --only-binary=:all:
61
  # install_package_from_local_file('webdataset')
 
80
  from hoho import proc, Sample
81
 
82
  def convert_entry_to_human_readable(entry):
83
+ out = {}
84
+ already_good = ['__key__', 'wf_vertices', 'wf_edges', 'edge_semantics', 'mesh_vertices', 'mesh_faces', 'face_semantics', 'K', 'R', 't']
85
+ for k, v in entry.items():
86
+ if k in already_good:
87
+ out[k] = v
88
+ continue
89
+ if k == 'points3d':
90
+ out[k] = read_points3D_binary(fid=io.BytesIO(v))
91
+ if k == 'cameras':
92
+ out[k] = read_cameras_binary(fid=io.BytesIO(v))
93
+ if k == 'images':
94
+ out[k] = read_images_binary(fid=io.BytesIO(v))
95
+ if k in ['ade20k', 'gestalt']:
96
+ out[k] = [PImage.open(io.BytesIO(x)).convert('RGB') for x in v]
97
+ if k == 'depthcm':
98
+ out[k] = [PImage.open(io.BytesIO(x)) for x in entry['depthcm']]
99
+ return out
100
 
101
  '''---end of compulsory---'''
102
 
 
106
  import os
107
 
108
  def download_packages(packages, folder='packages/torch'):
109
+ """
110
+ Downloads packages as .whl files into the specified folder using pip.
111
+
112
+ Parameters:
113
+ packages (list): List of packages to download with versions.
114
+ folder (str): The folder where the .whl files will be saved.
115
+ """
116
+ Path(folder).mkdir(parents=True, exist_ok=True)
117
+ try:
118
+ subprocess.check_call([sys.executable, "-m", "pip", "download",
119
+ "--platform", "manylinux1_x86_64",
120
+ "--python-version", "38",
121
+ "--only-binary=:all:",
122
+ "-d", folder] + packages)
123
+ print(f"Packages downloaded successfully into {folder}")
124
+ except subprocess.CalledProcessError as e:
125
+ print(f"Failed to download packages. Error: {e}")
126
 
127
  def install_package_from_local_file(package_name, folder='packages'):
128
+ """
129
+ Installs a package from a local .whl file or a directory containing .whl files using pip.
130
+
131
+ Parameters:
132
+ package_name (str): The name of the package to install.
133
+ folder (str): The folder where the .whl files are located.
134
+ """
135
+ try:
136
+ pth = str(Path(folder) / package_name)
137
+ subprocess.check_call([sys.executable, "-m", "pip", "install",
138
+ "--no-index", # Do not use package index
139
+ "--find-links", pth, # Look for packages in the specified directory or at the file
140
+ package_name]) # Specify the package to install
141
+ print(f"Package installed successfully from {pth}")
142
+ except subprocess.CalledProcessError as e:
143
+ print(f"Failed to install package from {pth}. Error: {e}")
144
 
145
  def setup_environment():
146
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "git+http://hf.co/usm3d/tools.git"])
147
+ import hoho
148
+ hoho.setup()
149
+
150
+ # Download required packages
151
+ packages_to_download = ['torch==2.0.1', 'torchvision==0.15.2', 'torchaudio==2.0.2']
152
+ download_packages(packages_to_download, folder='packages/torch')
153
+
154
+ # Install packages from local files
155
+ install_package_from_local_file('torch', folder='packages/torch')
156
+ install_package_from_local_file('torchvision', folder='packages/torch')
157
+ install_package_from_local_file('torchaudio', folder='packages/torch')
158
+
159
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "scikit-learn"])
160
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "tqdm"])
161
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "scipy"])
162
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "open3d"])
163
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "easydict"])
164
+
165
+ # pc_util_path = os.path.join(os.getcwd(), 'pc_util')
166
+ # if os.path.isdir(pc_util_path):
167
+ # os.chdir(pc_util_path)
168
+ # subprocess.check_call([sys.executable, "setup.py", "install"])
169
+ # else:
170
+ # print(f"Directory {pc_util_path} does not exist")
171
+
172
  from pathlib import Path
173
  def save_submission(submission, path):
174
+ """
175
+ Saves the submission to a specified path.
176
 
177
+ Parameters:
178
+ submission (List[Dict[]]): The submission to save.
179
+ path (str): The path to save the submission to.
180
+ """
181
+ sub = pd.DataFrame(submission, columns=["__key__", "wf_vertices", "wf_edges"])
182
+ sub.to_parquet(path)
183
+ print(f"Submission saved to {path}")
184
 
185
  if __name__ == "__main__":
186
  setup_environment()
187
 
188
+ from handcrafted_solution import predict
189
+ print ("------------ Loading dataset------------ ")
190
+ params = hoho.get_params()
191
+ dataset = hoho.get_dataset(decode=None, split='all', dataset_type='webdataset')
192
+
193
+ print('------------ Now you can do your solution ---------------')
194
+ solution = []
195
+ from concurrent.futures import ProcessPoolExecutor
196
+ with ProcessPoolExecutor(max_workers=8) as pool:
197
+ results = []
198
+ for i, sample in enumerate(tqdm(dataset)):
199
+ results.append(pool.submit(predict, sample, visualize=False))
200
+
201
+ for i, result in enumerate(tqdm(results)):
202
+ key, pred_vertices, pred_edges = result.result()
203
+ solution.append({
204
+ '__key__': key,
205
+ 'wf_vertices': pred_vertices.tolist(),
206
+ 'wf_edges': pred_edges
207
+ })
208
+ if i % 100 == 0:
209
+ # incrementally save the results in case we run out of time
210
+ print(f"Processed {i} samples")
211
+ # save_submission(solution, Path(params['output_path']) / "submission.parquet")
212
+ print('------------ Saving results ---------------')
213
+ save_submission(solution, Path(params['output_path']) / "submission.parquet")
214
+ print("------------ Done ------------ ")