File size: 1,130 Bytes
48d1312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import numpy as np


def modify_string(s: str) -> str:
    # Split the content inside parenthesis by '-'
    parts = s.split('-')

    # Check the last part
    if parts[-1] not in ["padding", "resize", "crop"]:
        parts.append("crop")

    # Combine everything back to the original format
    new_string = '-'.join(parts)

    return new_string


def modify_npz_files(root_dir):
    # Loop through the directory
    for dirpath, dirnames, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename.endswith('.npz'):
                file_path = os.path.join(dirpath, filename)
                data = np.load(file_path, allow_pickle=True)

                modified_data = {}

                # Check for fields containing the substring 'vgg'
                for field in data.files:

                    field_new = modify_string(field)
                    modified_data[field_new] = data[field]

                np.savez(file_path, **modified_data)


if __name__ == "__main__":
    directory = input("Enter the directory path to start search from: ")
    modify_npz_files(directory)