-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference.py
50 lines (40 loc) · 1.78 KB
/
inference.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
import torch
import os
import argparse
from tqdm import tqdm
from omegaconf import OmegaConf
from torchvision import transforms
from torchvision.utils import save_image
from PIL import Image
from enhancement_model import load_enhancement_model
def create_transform(size=None):
transform_list = [
transforms.Lambda(lambda img: img.convert("RGB") if img.mode != "RGB" else img), # Ensure image is in RGB
transforms.ToTensor(),
transforms.Lambda(lambda x: x.cuda())
]
if size:
transform_list.insert(1, transforms.Resize((size, size), Image.ANTIALIAS)) # Adjusted index for Resize
return transforms.Compose(transform_list)
def enhance_image(image_path, output_path, model, transform):
img = Image.open(image_path)
img_tensor = transform(img).unsqueeze(0)
print(img_tensor.shape)
light_map = model(img_tensor)
enhanced = torch.clamp(img_tensor / light_map, 0, 1)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
save_image(enhanced, output_path)
def process_directory(input_dir, output_dir, model, size=None):
transform = create_transform(size)
with torch.no_grad():
for filename in tqdm(os.listdir(input_dir), desc="Enhancing images"):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename.replace('.JPG', '.png'))
enhance_image(input_path, output_path, model, transform)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', type=str, default="./configs/inference/inference.yaml")
args = parser.parse_args()
config = OmegaConf.load(args.cfg)
model = load_enhancement_model(config, padding_mode='reflect')
process_directory(config.data.input, config.data.output, model)