Upload 2 files
Browse files- README.md +41 -0
- dataset_135807734_code_image_audio.py +123 -0
README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- active
|
| 5 |
+
- adaptive
|
| 6 |
+
- code
|
| 7 |
+
- image-audio
|
| 8 |
+
- light
|
| 9 |
+
- npy-sharded
|
| 10 |
+
- pseudo-label
|
| 11 |
+
- stratified-90-10
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# dataset_135807734_code_image_audio.py
|
| 15 |
+
|
| 16 |
+
## Dataset Summary
|
| 17 |
+
|
| 18 |
+
A **code** dataset with **image audio** modality, stored in **npy sharded** format.
|
| 19 |
+
|
| 20 |
+
## Preprocessing & Augmentation
|
| 21 |
+
|
| 22 |
+
- **Preprocessing**: adaptive
|
| 23 |
+
- **Augmentation**: light
|
| 24 |
+
|
| 25 |
+
## Splits & Sampling
|
| 26 |
+
|
| 27 |
+
- **Split strategy**: stratified 90 10
|
| 28 |
+
- **Sampling**: active
|
| 29 |
+
|
| 30 |
+
## Quality & Labeling
|
| 31 |
+
|
| 32 |
+
- **Quality filtering**: adaptive
|
| 33 |
+
- **Labeling**: pseudo label
|
| 34 |
+
|
| 35 |
+
## Files
|
| 36 |
+
|
| 37 |
+
- `dataset_135807734_code_image_audio.py` — main artifact of this repository
|
| 38 |
+
|
| 39 |
+
## License
|
| 40 |
+
|
| 41 |
+
See the license field above.
|
dataset_135807734_code_image_audio.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json, os, hashlib
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
# code dataset processor
|
| 5 |
+
# modality: image_audio, preprocessing: adaptive
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# --- real data source: code ---
|
| 9 |
+
TV_DATASET = None
|
| 10 |
+
HF_CANDIDATES = ['code_search_net', 'codeparrot/github-code']
|
| 11 |
+
IMAGE_FIELD = None
|
| 12 |
+
TEXT_FIELD = 'func_code_string'
|
| 13 |
+
LABEL_FIELD = 'func_documentation_string'
|
| 14 |
+
PROMPT_TEMPLATE = 'a photo of a {label}'
|
| 15 |
+
DATASET_URL = 'https://huggingface.co/datasets/code_search_net'
|
| 16 |
+
|
| 17 |
+
def fetch_real_samples(max_samples=5000, cache_dir='./_cache'):
|
| 18 |
+
# 本地没有数据时自动下载真实公开数据集: torchvision -> HuggingFace -> 手动说明
|
| 19 |
+
out = []
|
| 20 |
+
if TV_DATASET is not None:
|
| 21 |
+
try:
|
| 22 |
+
import torchvision
|
| 23 |
+
ctor = getattr(torchvision.datasets, TV_DATASET)
|
| 24 |
+
try:
|
| 25 |
+
ds = ctor(root=cache_dir, split='train', download=True)
|
| 26 |
+
except TypeError:
|
| 27 |
+
try:
|
| 28 |
+
ds = ctor(root=cache_dir, train=True, download=True)
|
| 29 |
+
except TypeError:
|
| 30 |
+
ds = ctor(root=cache_dir, download=True)
|
| 31 |
+
classes = getattr(ds, 'classes', None)
|
| 32 |
+
os.makedirs(os.path.join(cache_dir, 'tv'), exist_ok=True)
|
| 33 |
+
for i, item in enumerate(ds):
|
| 34 |
+
if len(out) >= max_samples:
|
| 35 |
+
break
|
| 36 |
+
img, label = item[0], item[1]
|
| 37 |
+
name = classes[label] if classes else str(label)
|
| 38 |
+
p = os.path.join(cache_dir, 'tv', str(i) + '.png')
|
| 39 |
+
try:
|
| 40 |
+
img.save(p)
|
| 41 |
+
except Exception:
|
| 42 |
+
continue
|
| 43 |
+
out.append({'image': p, 'text': PROMPT_TEMPLATE.format(label=name)})
|
| 44 |
+
if out:
|
| 45 |
+
return out
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print('torchvision load failed:', e)
|
| 48 |
+
for repo in HF_CANDIDATES:
|
| 49 |
+
try:
|
| 50 |
+
from datasets import load_dataset
|
| 51 |
+
try:
|
| 52 |
+
ds = load_dataset(repo, split='train', streaming=True)
|
| 53 |
+
except Exception:
|
| 54 |
+
ds = load_dataset(repo, split='train')
|
| 55 |
+
img_dir = os.path.join(cache_dir, 'hf_images')
|
| 56 |
+
os.makedirs(img_dir, exist_ok=True)
|
| 57 |
+
for i, ex in enumerate(ds):
|
| 58 |
+
if len(out) >= max_samples:
|
| 59 |
+
break
|
| 60 |
+
txt = None
|
| 61 |
+
if TEXT_FIELD is not None and TEXT_FIELD in ex:
|
| 62 |
+
v = ex[TEXT_FIELD]
|
| 63 |
+
txt = v if isinstance(v, str) else ' '.join(map(str, v if isinstance(v, (list, tuple)) else [v]))
|
| 64 |
+
if txt is None and LABEL_FIELD in ex:
|
| 65 |
+
txt = PROMPT_TEMPLATE.format(label=ex[LABEL_FIELD])
|
| 66 |
+
if txt is None:
|
| 67 |
+
continue
|
| 68 |
+
out.append({'text': txt})
|
| 69 |
+
if out:
|
| 70 |
+
return out
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print('HF load failed for', repo, ':', e)
|
| 73 |
+
print('Automatic download failed. Please get the data manually from:')
|
| 74 |
+
print(' ' + DATASET_URL)
|
| 75 |
+
return out
|
| 76 |
+
|
| 77 |
+
def build_dataset(src, dst, sz=224):
|
| 78 |
+
samples = []
|
| 79 |
+
for f in Path(src).glob('*.jsonl'):
|
| 80 |
+
with open(f) as fp:
|
| 81 |
+
for line in fp:
|
| 82 |
+
if line.strip():
|
| 83 |
+
samples.append(json.loads(line))
|
| 84 |
+
|
| 85 |
+
if not samples:
|
| 86 |
+
samples = fetch_real_samples()
|
| 87 |
+
|
| 88 |
+
# dedup
|
| 89 |
+
seen = set()
|
| 90 |
+
unique = []
|
| 91 |
+
for s in samples:
|
| 92 |
+
fp = s.get('image', s.get('audio', ''))
|
| 93 |
+
if fp and os.path.exists(fp):
|
| 94 |
+
h = hashlib.md5(open(fp, 'rb').read()).hexdigest()
|
| 95 |
+
if h not in seen:
|
| 96 |
+
seen.add(h)
|
| 97 |
+
unique.append(s)
|
| 98 |
+
else:
|
| 99 |
+
unique.append(s)
|
| 100 |
+
|
| 101 |
+
os.makedirs(dst, exist_ok=True)
|
| 102 |
+
out = []
|
| 103 |
+
for s in unique:
|
| 104 |
+
item = {}
|
| 105 |
+
if 'image' in s:
|
| 106 |
+
from PIL import Image
|
| 107 |
+
img = Image.open(s['image']).convert('RGB').resize((sz, sz))
|
| 108 |
+
p = os.path.join(dst, os.path.basename(s['image']))
|
| 109 |
+
img.save(p, 'JPEG', quality=95)
|
| 110 |
+
item['image'] = p
|
| 111 |
+
item['text'] = s.get('code', s.get('text', ''))
|
| 112 |
+
item['domain'] = 'code'
|
| 113 |
+
out.append(item)
|
| 114 |
+
|
| 115 |
+
with open(os.path.join(dst, 'dataset.jsonl'), 'w') as f:
|
| 116 |
+
for d in out:
|
| 117 |
+
f.write(json.dumps(d, ensure_ascii=False) + '\n')
|
| 118 |
+
return out
|
| 119 |
+
|
| 120 |
+
if __name__ == '__main__':
|
| 121 |
+
import sys
|
| 122 |
+
result = build_dataset(sys.argv[1], sys.argv[2])
|
| 123 |
+
print(f'Processed {len(result)} samples')
|