Upload extensions_built_in/dataset_tools/tools/dataset_tools_config_modules.py with huggingface_hub
Browse files
extensions_built_in/dataset_tools/tools/dataset_tools_config_modules.py
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from typing import Literal, Type, TYPE_CHECKING
|
| 3 |
+
|
| 4 |
+
Host: Type = Literal['unsplash', 'pexels']
|
| 5 |
+
|
| 6 |
+
RAW_DIR = "raw"
|
| 7 |
+
NEW_DIR = "_tmp"
|
| 8 |
+
TRAIN_DIR = "train"
|
| 9 |
+
DEPTH_DIR = "depth"
|
| 10 |
+
|
| 11 |
+
from .image_tools import Step, img_manipulation_steps
|
| 12 |
+
from .caption import caption_manipulation_steps
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class DatasetSyncCollectionConfig:
|
| 16 |
+
def __init__(self, **kwargs):
|
| 17 |
+
self.host: Host = kwargs.get('host', None)
|
| 18 |
+
self.collection_id: str = kwargs.get('collection_id', None)
|
| 19 |
+
self.directory: str = kwargs.get('directory', None)
|
| 20 |
+
self.api_key: str = kwargs.get('api_key', None)
|
| 21 |
+
self.min_width: int = kwargs.get('min_width', 1024)
|
| 22 |
+
self.min_height: int = kwargs.get('min_height', 1024)
|
| 23 |
+
|
| 24 |
+
if self.host is None:
|
| 25 |
+
raise ValueError("host is required")
|
| 26 |
+
if self.collection_id is None:
|
| 27 |
+
raise ValueError("collection_id is required")
|
| 28 |
+
if self.directory is None:
|
| 29 |
+
raise ValueError("directory is required")
|
| 30 |
+
if self.api_key is None:
|
| 31 |
+
raise ValueError(f"api_key is required: {self.host}:{self.collection_id}")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class ImageState:
|
| 35 |
+
def __init__(self, **kwargs):
|
| 36 |
+
self.steps_complete: list[Step] = kwargs.get('steps_complete', [])
|
| 37 |
+
self.steps_to_complete: list[Step] = kwargs.get('steps_to_complete', [])
|
| 38 |
+
|
| 39 |
+
def to_dict(self):
|
| 40 |
+
return {
|
| 41 |
+
'steps_complete': self.steps_complete
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class Rect:
|
| 46 |
+
def __init__(self, **kwargs):
|
| 47 |
+
self.x = kwargs.get('x', 0)
|
| 48 |
+
self.y = kwargs.get('y', 0)
|
| 49 |
+
self.width = kwargs.get('width', 0)
|
| 50 |
+
self.height = kwargs.get('height', 0)
|
| 51 |
+
|
| 52 |
+
def to_dict(self):
|
| 53 |
+
return {
|
| 54 |
+
'x': self.x,
|
| 55 |
+
'y': self.y,
|
| 56 |
+
'width': self.width,
|
| 57 |
+
'height': self.height
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class ImgInfo:
|
| 62 |
+
def __init__(self, **kwargs):
|
| 63 |
+
self.version: int = kwargs.get('version', None)
|
| 64 |
+
self.caption: str = kwargs.get('caption', None)
|
| 65 |
+
self.caption_short: str = kwargs.get('caption_short', None)
|
| 66 |
+
self.poi = [Rect(**poi) for poi in kwargs.get('poi', [])]
|
| 67 |
+
self.state = ImageState(**kwargs.get('state', {}))
|
| 68 |
+
self.caption_method = kwargs.get('caption_method', None)
|
| 69 |
+
self.other_captions = kwargs.get('other_captions', {})
|
| 70 |
+
self._upgrade_state()
|
| 71 |
+
self.force_image_process: bool = False
|
| 72 |
+
self._requested_steps: list[Step] = []
|
| 73 |
+
|
| 74 |
+
self.is_dirty: bool = False
|
| 75 |
+
|
| 76 |
+
def _upgrade_state(self):
|
| 77 |
+
# upgrades older states
|
| 78 |
+
if self.caption is not None and 'caption' not in self.state.steps_complete:
|
| 79 |
+
self.mark_step_complete('caption')
|
| 80 |
+
self.is_dirty = True
|
| 81 |
+
if self.caption_short is not None and 'caption_short' not in self.state.steps_complete:
|
| 82 |
+
self.mark_step_complete('caption_short')
|
| 83 |
+
self.is_dirty = True
|
| 84 |
+
if self.caption_method is None and self.caption is not None:
|
| 85 |
+
# added caption method in version 2. Was all llava before that
|
| 86 |
+
self.caption_method = 'llava:default'
|
| 87 |
+
self.is_dirty = True
|
| 88 |
+
|
| 89 |
+
def to_dict(self):
|
| 90 |
+
return {
|
| 91 |
+
'version': self.version,
|
| 92 |
+
'caption_method': self.caption_method,
|
| 93 |
+
'caption': self.caption,
|
| 94 |
+
'caption_short': self.caption_short,
|
| 95 |
+
'poi': [poi.to_dict() for poi in self.poi],
|
| 96 |
+
'state': self.state.to_dict(),
|
| 97 |
+
'other_captions': self.other_captions
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
def mark_step_complete(self, step: Step):
|
| 101 |
+
if step not in self.state.steps_complete:
|
| 102 |
+
self.state.steps_complete.append(step)
|
| 103 |
+
if step in self.state.steps_to_complete:
|
| 104 |
+
self.state.steps_to_complete.remove(step)
|
| 105 |
+
self.is_dirty = True
|
| 106 |
+
|
| 107 |
+
def add_step(self, step: Step):
|
| 108 |
+
if step not in self.state.steps_to_complete and step not in self.state.steps_complete:
|
| 109 |
+
self.state.steps_to_complete.append(step)
|
| 110 |
+
|
| 111 |
+
def trigger_image_reprocess(self):
|
| 112 |
+
if self._requested_steps is None:
|
| 113 |
+
raise Exception("Must call add_steps before trigger_image_reprocess")
|
| 114 |
+
steps = self._requested_steps
|
| 115 |
+
# remove all image manipulationf from steps_to_complete
|
| 116 |
+
for step in img_manipulation_steps:
|
| 117 |
+
if step in self.state.steps_to_complete:
|
| 118 |
+
self.state.steps_to_complete.remove(step)
|
| 119 |
+
if step in self.state.steps_complete:
|
| 120 |
+
self.state.steps_complete.remove(step)
|
| 121 |
+
self.force_image_process = True
|
| 122 |
+
self.is_dirty = True
|
| 123 |
+
# we want to keep the order passed in process file
|
| 124 |
+
for step in steps:
|
| 125 |
+
if step in img_manipulation_steps:
|
| 126 |
+
self.add_step(step)
|
| 127 |
+
|
| 128 |
+
def add_steps(self, steps: list[Step]):
|
| 129 |
+
self._requested_steps = [step for step in steps]
|
| 130 |
+
for stage in steps:
|
| 131 |
+
self.add_step(stage)
|
| 132 |
+
|
| 133 |
+
# update steps if we have any img processes not complete, we have to reprocess them all
|
| 134 |
+
# if any steps_to_complete are in img_manipulation_steps
|
| 135 |
+
|
| 136 |
+
is_manipulating_image = any([step in img_manipulation_steps for step in self.state.steps_to_complete])
|
| 137 |
+
order_has_changed = False
|
| 138 |
+
|
| 139 |
+
if not is_manipulating_image:
|
| 140 |
+
# check to see if order has changed. No need to if already redoing it. Will detect if ones are removed
|
| 141 |
+
target_img_manipulation_order = [step for step in steps if step in img_manipulation_steps]
|
| 142 |
+
current_img_manipulation_order = [step for step in self.state.steps_complete if
|
| 143 |
+
step in img_manipulation_steps]
|
| 144 |
+
if target_img_manipulation_order != current_img_manipulation_order:
|
| 145 |
+
order_has_changed = True
|
| 146 |
+
|
| 147 |
+
if is_manipulating_image or order_has_changed:
|
| 148 |
+
self.trigger_image_reprocess()
|
| 149 |
+
|
| 150 |
+
def set_caption_method(self, method: str):
|
| 151 |
+
if self._requested_steps is None:
|
| 152 |
+
raise Exception("Must call add_steps before set_caption_method")
|
| 153 |
+
if self.caption_method != method:
|
| 154 |
+
self.is_dirty = True
|
| 155 |
+
# move previous caption method to other_captions
|
| 156 |
+
if self.caption_method is not None and self.caption is not None or self.caption_short is not None:
|
| 157 |
+
self.other_captions[self.caption_method] = {
|
| 158 |
+
'caption': self.caption,
|
| 159 |
+
'caption_short': self.caption_short,
|
| 160 |
+
}
|
| 161 |
+
self.caption_method = method
|
| 162 |
+
self.caption = None
|
| 163 |
+
self.caption_short = None
|
| 164 |
+
# see if we have a caption from the new method
|
| 165 |
+
if method in self.other_captions:
|
| 166 |
+
self.caption = self.other_captions[method].get('caption', None)
|
| 167 |
+
self.caption_short = self.other_captions[method].get('caption_short', None)
|
| 168 |
+
else:
|
| 169 |
+
self.trigger_new_caption()
|
| 170 |
+
|
| 171 |
+
def trigger_new_caption(self):
|
| 172 |
+
self.caption = None
|
| 173 |
+
self.caption_short = None
|
| 174 |
+
self.is_dirty = True
|
| 175 |
+
# check to see if we have any steps in the complete list and move them to the to_complete list
|
| 176 |
+
for step in self.state.steps_complete:
|
| 177 |
+
if step in caption_manipulation_steps:
|
| 178 |
+
self.state.steps_complete.remove(step)
|
| 179 |
+
self.state.steps_to_complete.append(step)
|
| 180 |
+
|
| 181 |
+
def to_json(self):
|
| 182 |
+
return json.dumps(self.to_dict())
|
| 183 |
+
|
| 184 |
+
def set_version(self, version: int):
|
| 185 |
+
if self.version != version:
|
| 186 |
+
self.is_dirty = True
|
| 187 |
+
self.version = version
|