-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
52 lines (46 loc) · 1.91 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
51
52
import numpy as np
import argparse
import os
import torch
from torchvision.transforms import transforms
from torchvision.utils import save_image
from PIL import Image
import time
from src import Generator
transform = transforms.Compose([
transforms.ToTensor(),
])
def main(args):
## Check if CUDA is available
if args.device == 'cuda' and not torch.cuda.is_available():
print('CUDA is not available. Switching to CPU')
args.device = 'cpu'
device = torch.device(args.device)
generator = Generator().to(device)
generator.load_state_dict(torch.load(args.model_path, map_location=device))
print('Model loaded successfully')
print(f'Inference using {str(device).upper()}')
images = get_images_batch(args.image_path, device)
for image in images:
start = time.time()
with torch.no_grad():
generated = generator(image[0])
save_image(generated.detach(), args.save_path + image[1].split('.')[0] + '_generated.png')
end = time.time()
print(f'Generated {image[1]} done in {end-start} seconds')
def get_images_batch(image_path, device):
image_paths = os.listdir(image_path)
images = []
for path in image_paths:
img = Image.open(os.path.join(image_path, path))
img = transform(img).unsqueeze(0).to(device)
images.append([img, path])
return images
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Inference with SRGAN model')
parser.add_argument('--model_path', type=str, help='Path to the generator model', default='SRGAN.pth')
parser.add_argument('--image_path', type=str, help='Path to the images', default='inputs/')
parser.add_argument('--save_path', type=str, help='Path to save the generated images', default='outputs/')
parser.add_argument('--device', type=str, help='Device to use for inference', default='cuda')
args = parser.parse_args()
main(args)