Upload script.py
Browse files
script.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### This is example of the script that will be run in the test environment.
|
| 2 |
+
### Some parts of the code are compulsory and you should NOT CHANGE THEM.
|
| 3 |
+
### They are between '''---compulsory---''' comments.
|
| 4 |
+
### You can change the rest of the code to define and test your solution.
|
| 5 |
+
### However, you should not change the signature of the provided function.
|
| 6 |
+
### The script would save "submission.parquet" file in the current directory.
|
| 7 |
+
### The actual logic of the solution is implemented in the `handcrafted_solution.py` file.
|
| 8 |
+
### The `handcrafted_solution.py` file is a placeholder for your solution.
|
| 9 |
+
### You should implement the logic of your solution in that file.
|
| 10 |
+
### You can use any additional files and subdirectories to organize your code.
|
| 11 |
+
|
| 12 |
+
'''---compulsory---'''
|
| 13 |
+
# import subprocess
|
| 14 |
+
# from pathlib import Path
|
| 15 |
+
# def install_package_from_local_file(package_name, folder='packages'):
|
| 16 |
+
# """
|
| 17 |
+
# Installs a package from a local .whl file or a directory containing .whl files using pip.
|
| 18 |
+
|
| 19 |
+
# Parameters:
|
| 20 |
+
# path_to_file_or_directory (str): The path to the .whl file or the directory containing .whl files.
|
| 21 |
+
# """
|
| 22 |
+
# try:
|
| 23 |
+
# pth = str(Path(folder) / package_name)
|
| 24 |
+
# subprocess.check_call([subprocess.sys.executable, "-m", "pip", "install",
|
| 25 |
+
# "--no-index", # Do not use package index
|
| 26 |
+
# "--find-links", pth, # Look for packages in the specified directory or at the file
|
| 27 |
+
# package_name]) # Specify the package to install
|
| 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
|
| 35 |
+
# import subprocess
|
| 36 |
+
# import importlib
|
| 37 |
+
# from pathlib import Path
|
| 38 |
+
# import subprocess
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ### The function below is useful for installing additional python wheels.
|
| 42 |
+
# def install_package_from_local_file(package_name, folder='packages'):
|
| 43 |
+
# """
|
| 44 |
+
# Installs a package from a local .whl file or a directory containing .whl files using pip.
|
| 45 |
+
|
| 46 |
+
# Parameters:
|
| 47 |
+
# path_to_file_or_directory (str): The path to the .whl file or the directory containing .whl files.
|
| 48 |
+
# """
|
| 49 |
+
# try:
|
| 50 |
+
# pth = str(Path(folder) / package_name)
|
| 51 |
+
# subprocess.check_call([subprocess.sys.executable, "-m", "pip", "install",
|
| 52 |
+
# "--no-index", # Do not use package index
|
| 53 |
+
# "--find-links", pth, # Look for packages in the specified directory or at the file
|
| 54 |
+
# package_name]) # Specify the package to install
|
| 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')
|
| 62 |
+
# install_package_from_local_file('tqdm')
|
| 63 |
+
|
| 64 |
+
### Here you can import any library or module you want.
|
| 65 |
+
### The code below is used to read and parse the input dataset.
|
| 66 |
+
### Please, do not modify it.
|
| 67 |
+
|
| 68 |
+
import webdataset as wds
|
| 69 |
+
from tqdm import tqdm
|
| 70 |
+
from typing import Dict
|
| 71 |
+
import pandas as pd
|
| 72 |
+
from transformers import AutoTokenizer
|
| 73 |
+
import os
|
| 74 |
+
import time
|
| 75 |
+
import io
|
| 76 |
+
from PIL import Image as PImage
|
| 77 |
+
import numpy as np
|
| 78 |
+
|
| 79 |
+
from hoho.read_write_colmap import read_cameras_binary, read_images_binary, read_points3D_binary
|
| 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 |
+
|
| 103 |
+
### The part below is used to define and test your solution.
|
| 104 |
+
|
| 105 |
+
from pathlib import Path
|
| 106 |
+
def save_submission(submission, path):
|
| 107 |
+
"""
|
| 108 |
+
Saves the submission to a specified path.
|
| 109 |
+
|
| 110 |
+
Parameters:
|
| 111 |
+
submission (List[Dict[]]): The submission to save.
|
| 112 |
+
path (str): The path to save the submission to.
|
| 113 |
+
"""
|
| 114 |
+
sub = pd.DataFrame(submission, columns=["__key__", "wf_vertices", "wf_edges"])
|
| 115 |
+
sub.to_parquet(path)
|
| 116 |
+
print(f"Submission saved to {path}")
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
from handcrafted_solution import predict
|
| 120 |
+
print ("------------ Loading dataset------------ ")
|
| 121 |
+
params = hoho.get_params()
|
| 122 |
+
dataset = hoho.get_dataset(decode=None, split='all', dataset_type='webdataset')
|
| 123 |
+
|
| 124 |
+
print('------------ Now you can do your solution ---------------')
|
| 125 |
+
solution = []
|
| 126 |
+
from concurrent.futures import ProcessPoolExecutor
|
| 127 |
+
with ProcessPoolExecutor(max_workers=8) as pool:
|
| 128 |
+
results = []
|
| 129 |
+
for i, sample in enumerate(tqdm(dataset)):
|
| 130 |
+
results.append(pool.submit(predict, sample, visualize=False))
|
| 131 |
+
|
| 132 |
+
for i, result in enumerate(tqdm(results)):
|
| 133 |
+
key, pred_vertices, pred_edges = result.result()
|
| 134 |
+
solution.append({
|
| 135 |
+
'__key__': key,
|
| 136 |
+
'wf_vertices': pred_vertices.tolist(),
|
| 137 |
+
'wf_edges': pred_edges
|
| 138 |
+
})
|
| 139 |
+
if i % 100 == 0:
|
| 140 |
+
# incrementally save the results in case we run out of time
|
| 141 |
+
print(f"Processed {i} samples")
|
| 142 |
+
# save_submission(solution, Path(params['output_path']) / "submission.parquet")
|
| 143 |
+
print('------------ Saving results ---------------')
|
| 144 |
+
save_submission(solution, Path(params['output_path']) / "submission.parquet")
|
| 145 |
+
print("------------ Done ------------ ")
|