-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memap dataset for multimodal data #47
Draft
spravil
wants to merge
17
commits into
main
Choose a base branch
from
feat/multimodal_mem_map_dataset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ac6c94c
refactor: Rewrite class resolver register to be independent of specif…
19a1d85
feat: Support multiple modalities in MemMapDataset
307733b
test: Adapt test to new memory format of MemMapDataset and implement …
1feaaf2
fix: formatting
thomaschhh a5ae389
feat: add audio codec WIP
thomaschhh 0af4540
feat: add missing fixture
thomaschhh 49d9c1c
fix: audio codec
220493a
refactor: clean-up test fixtures
thomaschhh 7191ba1
refactor: run pre-commit hooks
70747f7
fix: add pillow to requirements
19e95d9
refactor: fix type
8945b0c
chore: remove unused feature
thomaschhh 16d0fb0
refactor: TorchaudioAudioCodec init
thomaschhh ccce2ba
fix: add soundfile to requirements for audio processing
340fab5
fix: manually resolve config type for codecs to avoid pydantic parsin…
fdb0815
feat: Merge branch 'feat/audio-codec' into feat/multimodal_mem_map_da…
3a16d6e
fix: fixed issue that discards all-zero bytes at the end of a read op…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
features: | ||
- jq_pattern: .cls | ||
codec: | ||
type_hint: HfTokenizerCodec | ||
config: | ||
add_eos_token: true | ||
tokenizer: | ||
type_hint: GPT2TokenizerFast | ||
config: | ||
tokenizer_file: ./data/tokenizer/tokenizer.json | ||
- jq_pattern: .img_path | ||
codec: | ||
type_hint: PillowImageCodec | ||
config: | ||
save_format: png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import TypeVar, Generic, Optional, Dict, Any | ||
|
||
from io import BytesIO | ||
from PIL import Image | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing pip install |
||
from transformers import PreTrainedTokenizer | ||
|
||
T = TypeVar("T") | ||
|
||
class Codec(ABC, Generic[T]): | ||
@abstractmethod | ||
def encode(self, obj: T) -> bytes: | ||
pass | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def decode(serialized_obj: bytes) -> T: | ||
pass | ||
|
||
|
||
class FixSizedCodec(Codec[T]): | ||
"""Base class for fix-sized Codecs | ||
|
||
Fix-sized codecs are special in that they encode a sequence of values where | ||
each value is encoded by a fix number of bytes. The length of thegenerated | ||
bytestring is an integer multiple of `num_bytes_per_value`. | ||
""" | ||
|
||
@classmethod | ||
@abstractmethod | ||
def num_bytes_per_value(cls) -> int: | ||
raise NotImplementedError | ||
|
||
|
||
class HfTokenizerCodec(FixSizedCodec[str]): | ||
|
||
TOKEN_SIZE_IN_BYTES = 4 | ||
|
||
@classmethod | ||
def num_bytes_per_value(cls) -> int: | ||
return cls.TOKEN_SIZE_IN_BYTES | ||
|
||
def __init__( | ||
self, | ||
tokenizer: PreTrainedTokenizer, | ||
max_length: Optional[int] = None, | ||
add_eos_token: bool = True | ||
) -> None: | ||
|
||
# instantiate | ||
self.tokenizer = tokenizer | ||
self.add_eos_token = add_eos_token | ||
|
||
if add_eos_token: | ||
# get eos token in bytes to append to the end of each sequence | ||
eos_token = self.tokenizer.convert_tokens_to_ids(self.tokenizer.eos_token) | ||
self.eos_token = eos_token.to_bytes(type(self).TOKEN_SIZE_IN_BYTES, byteorder="big") | ||
|
||
self.tokenizer_kwargs = {} if max_length is None else dict( | ||
max_length=max_length - int(add_eos_token), | ||
truncation=True | ||
) | ||
|
||
def encode(self, text: str) -> bytes: | ||
# tokenize text and convert the token ids to bytes | ||
tokens = [ | ||
t.to_bytes(type(self).TOKEN_SIZE_IN_BYTES, byteorder="big") | ||
for t in self.tokenizer(text, **self.tokenizer_kwargs)["input_ids"] | ||
] | ||
# | ||
if len(tokens) == 0: | ||
raise ValueError("Received empty sample") | ||
# add special eos token | ||
if self.add_eos_token: | ||
tokens.append(self.eos_token) | ||
|
||
# join byte strings | ||
return b"".join(tokens) | ||
|
||
@classmethod | ||
def decode(cls, serialized_tokens: bytes) -> str: | ||
return [ | ||
int.from_bytes( | ||
serialized_tokens[i:i+cls.TOKEN_SIZE_IN_BYTES], | ||
byteorder="big" | ||
) | ||
for i in range(0, len(serialized_tokens), cls.TOKEN_SIZE_IN_BYTES) | ||
] | ||
|
||
|
||
class PillowImageCodec(Codec[str]): | ||
|
||
def __init__( | ||
self, | ||
save_format: str = "png" | ||
) -> None: | ||
self._format = save_format | ||
|
||
def encode(self, img_file_path: str) -> bytes: | ||
buf = BytesIO() | ||
# write image to buffer | ||
with Image.open(img_file_path) as img: | ||
img.save(buf, format=self._format) | ||
# retuen buffer content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
buf.seek(0) | ||
return buf.read() | ||
|
||
@staticmethod | ||
def decode(serialized_img: bytes) -> str: | ||
return Image.open(BytesIO(serialized_img)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused imports