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) | |