-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_process_chunks.py
303 lines (279 loc) · 9.14 KB
/
04_process_chunks.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import argparse
import boto3
import json
import os
from boto3.s3.transfer import TransferConfig
import torch
from utils.custom_models import load_models
from utils.inference_scripts import perform_inf
# Transfer configuration for optimised S3 download
transfer_config = TransferConfig(
max_concurrency=20, # Increase the number of concurrent transfers
multipart_threshold=8 * 1024 * 1024, # 8MB
max_io_queue=1000,
io_chunksize=262144, # 256KB
)
def initialise_session(credentials_file="credentials.json"):
"""
Load AWS and API credentials from a configuration file and initialise an AWS session.
Args:
credentials_file (str): Path to the credentials JSON file.
Returns:
boto3.Client: Initialised S3 client.
"""
with open(credentials_file, encoding="utf-8") as config_file:
aws_credentials = json.load(config_file)
session = boto3.Session(
aws_access_key_id=aws_credentials["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=aws_credentials["AWS_SECRET_ACCESS_KEY"],
region_name=aws_credentials["AWS_REGION"],
)
client = session.client("s3", endpoint_url=aws_credentials["AWS_URL_ENDPOINT"])
return client
def download_and_analyse(
keys,
output_dir,
bucket_name,
client,
remove_image=True,
perform_inference=True,
save_crops=False,
localisation_model=None,
box_threshold=0.99,
binary_model=None,
order_model=None,
order_labels=None,
species_model=None,
species_labels=None,
device=None,
order_data_thresholds=None,
top_n=5,
csv_file="results.csv",
):
"""
Download images from S3 and perform analysis.
Args:
keys (list): List of S3 keys to process.
output_dir (str): Directory to save downloaded files and results.
bucket_name (str): S3 bucket name.
client (boto3.Client): Initialised S3 client.
Other args: Parameters for inference and analysis.
"""
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
for key in keys:
local_path = os.path.join(output_dir, os.path.basename(key))
print(f"Downloading {key} to {local_path}")
client.download_file(bucket_name, key, local_path, Config=transfer_config)
# Perform image analysis if enabled
print(f"Analysing {local_path}")
if perform_inference:
perform_inf(
local_path,
bucket_name=bucket_name,
loc_model=localisation_model,
box_threshold=box_threshold,
binary_model=binary_model,
order_model=order_model,
order_labels=order_labels,
regional_model=species_model,
regional_category_map=species_labels,
proc_device=device,
order_data_thresholds=order_data_thresholds,
csv_file=csv_file,
top_n=top_n,
save_crops=save_crops
)
# Remove the image if cleanup is enabled
if remove_image:
os.remove(local_path)
def main(
chunk_id,
json_file,
output_dir,
bucket_name,
credentials_file="credentials.json",
remove_image=True,
perform_inference=True,
save_crops=False,
localisation_model=None,
box_threshold=0.99,
binary_model=None,
order_model=None,
order_labels=None,
species_model=None,
species_labels=None,
device=None,
order_data_thresholds=None,
top_n=5,
csv_file="results.csv",
):
"""
Main function to process a specific chunk of S3 keys.
Args:
chunk_id (str): ID of the chunk to process (e.g., chunk_0).
json_file (str): Path to the JSON file with key chunks.
output_dir (str): Directory to save results.
bucket_name (str): S3 bucket name.
Other args: Parameters for download and analysis.
"""
with open(json_file, "r") as f:
chunks = json.load(f)
if chunk_id not in chunks:
raise ValueError(f"Chunk ID {chunk_id} not found in JSON file.")
client = initialise_session(credentials_file)
keys = chunks[chunk_id]["keys"]
download_and_analyse(
keys=keys,
output_dir=output_dir,
bucket_name=bucket_name,
client=client,
remove_image=remove_image,
perform_inference=perform_inference,
save_crops=save_crops,
localisation_model=localisation_model,
box_threshold=box_threshold,
binary_model=binary_model,
order_model=order_model,
order_labels=order_labels,
species_model=species_model,
species_labels=species_labels,
device=device,
order_data_thresholds=order_data_thresholds,
top_n=top_n,
csv_file=csv_file,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process a specific chunk of S3 keys.")
parser.add_argument(
"--chunk_id",
required=True,
help="ID of the chunk to process (e.g., 0, 1, 2, 3).",
)
parser.add_argument(
"--json_file", required=True, help="Path to the JSON file with key chunks."
)
parser.add_argument(
"--output_dir",
required=True,
help="Directory to save downloaded files and analysis results.",
default="./data/",
)
parser.add_argument("--bucket_name", required=True, help="Name of the S3 bucket.")
parser.add_argument(
"--credentials_file",
default="credentials.json",
help="Path to AWS credentials file.",
)
parser.add_argument(
"--remove_image", action="store_true", help="Remove images after processing."
)
parser.add_argument(
"--perform_inference", action="store_true", help="Enable inference."
)
parser.add_argument(
"--save_crops", action="store_true", help="Whether to save the crops."
)
parser.add_argument(
"--localisation_model_path",
type=str,
help="Path to the localisation model weights.",
default="./models/v1_localizmodel_2021-08-17-12-06.pt",
)
parser.add_argument(
"--box_threshold",
type=float,
default=0.99,
help="Threshold for the confidence score of bounding boxes. Default: 0.99",
)
parser.add_argument(
"--binary_model_path",
type=str,
help="Path to the binary model weights.",
default="./models/moth-nonmoth-effv2b3_20220506_061527_30.pth",
)
parser.add_argument(
"--order_model_path",
type=str,
help="Path to the order model weights.",
default="./models/dhc_best_128.pth",
)
parser.add_argument(
"--order_labels", type=str, help="Path to the order labels file."
)
parser.add_argument(
"--species_model_path",
type=str,
help="Path to the species model weights.",
default="./models/turing-costarica_v03_resnet50_2024-06-04-16-17_state.pt",
)
parser.add_argument(
"--species_labels",
type=str,
help="Path to the species labels file.",
default="./models/03_costarica_data_category_map.json",
)
parser.add_argument(
"--device",
type=str,
default="cpu",
help="Device to run inference on (e.g., cpu or cuda).",
)
parser.add_argument(
"--order_thresholds_path",
type=str,
help="Path to the order data thresholds file.",
default="./models/thresholdsTestTrain.csv",
)
parser.add_argument(
"--top_n_species",
type=int,
help="The number of predictions to output.",
default=5,
)
parser.add_argument(
"--csv_file", default="results.csv", help="Path to save analysis results."
)
args = parser.parse_args()
if torch.cuda.is_available():
device = torch.device("cuda")
print(
"\033[95m\033[1mCuda available, using GPU "
+ "\N{White Heavy Check Mark}\033[0m\033[0m"
)
else:
device = torch.device("cpu")
print(
"\033[95m\033[1mCuda not available, using CPU "
+ "\N{Cross Mark}\033[0m\033[0m"
)
models = load_models(
device,
args.localisation_model_path,
args.binary_model_path,
args.order_model_path,
args.order_thresholds_path,
args.species_model_path,
args.species_labels,
)
main(
chunk_id=args.chunk_id,
json_file=args.json_file,
output_dir=args.output_dir,
bucket_name=args.bucket_name,
credentials_file=args.credentials_file,
remove_image=args.remove_image,
save_crops=args.save_crops,
perform_inference=args.perform_inference,
localisation_model=models["localisation_model"],
box_threshold=args.box_threshold,
binary_model=models["classification_model"],
order_model=models["order_model"],
order_labels=models["order_model_labels"],
order_data_thresholds=models["order_model_thresholds"],
species_model=models["species_model"],
species_labels=models["species_model_labels"],
device=device,
top_n=args.top_n_species,
csv_file=args.csv_file,
)