File size: 3,170 Bytes
f907e43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import json
import os
import shutil
from typing import Any
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm


def read_json(path: str) -> Any:
    with open(path, "r", encoding="utf-8") as f:
        return json.load(f)


def write_json(path: str, data: Any):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=4)


def copy_one(src: str, dst: str):
    if not os.path.exists(src):
        raise FileNotFoundError(src)

    os.makedirs(os.path.dirname(dst), exist_ok=True)

    # 如果目标已经存在,可以跳过,适合断点续跑
    if os.path.exists(dst):
        return

    shutil.copy2(src, dst)


def parallel_copy(copy_tasks, desc: str, colour: str = "green", max_workers: int = 32):
    with ThreadPoolExecutor(max_workers=max_workers) as executor:
        futures = [
            executor.submit(copy_one, src, dst)
            for src, dst in copy_tasks
        ]

        for future in tqdm(as_completed(futures), total=len(futures), desc=desc, colour=colour):
            future.result()


EditScore_source_dir = "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EditScore-Reward-Data"
EditScore_dst_dir = "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/ESData/EditScore-Reward-Data"

EditReward_source_dir = "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EditReward-Data/Images"
EditReward_dst_dir = "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/ESData/EditReward-Data"

editscore_data = read_json(
    "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EditScore-Reward-Data/filter_metadata.json"
)

editreward_data = read_json(
    "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/EditReward-Data/unique_matadata_type.json"
)


# EditScore copy tasks
editscore_tasks = []
for item in editscore_data:
    for img in item["images"]:
        src = os.path.join(EditScore_source_dir, img)
        dst = os.path.join(EditScore_dst_dir, img)
        editscore_tasks.append((src, dst))

parallel_copy(editscore_tasks, desc="Copy EditScore", colour="green", max_workers=32)

write_json(
    "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/ESData/EditScore-Reward-Data/data.json",
    editscore_data,
)


# EditReward filter + copy tasks
filtered_editreward_data = []
editreward_tasks = []

for item in editreward_data:
    item = dict(item)

    item.pop("status", None)

    if item.get("confidence", 0) < 0.9:
        continue

    item.pop("confidence", None)

    for img in item["images"]:
        src = os.path.join(EditReward_source_dir, img)
        dst = os.path.join(EditReward_dst_dir, img)
        editreward_tasks.append((src, dst))

    filtered_editreward_data.append(item)

print(len(filtered_editreward_data))
parallel_copy(editreward_tasks, desc="Copy EditReward", colour="red", max_workers=48)

write_json(
    "/inspire/qb-ilm/project/deepgen/wangdianyi-240107110022/BXH/RL_Data/ESData/EditReward-Data/data.json",
    filtered_editreward_data,
)