AItool commited on
Commit
b0b5f60
·
verified ·
1 Parent(s): 415f411

Create inference_img.py

Browse files
Files changed (1) hide show
  1. inference_img.py +120 -0
inference_img.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import torch
4
+ import argparse
5
+ from torch.nn import functional as F
6
+ import warnings
7
+
8
+ OUTPUT_PATH = "/home/user/app/output/"
9
+
10
+ warnings.filterwarnings("ignore")
11
+
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+ torch.set_grad_enabled(False)
14
+ if torch.cuda.is_available():
15
+ torch.backends.cudnn.enabled = True
16
+ torch.backends.cudnn.benchmark = True
17
+
18
+ parser = argparse.ArgumentParser(description='Interpolation for a pair of images')
19
+ parser.add_argument('--img', dest='img', nargs=2, required=True)
20
+ parser.add_argument('--exp', default=2, type=int)
21
+ parser.add_argument('--ratio', default=0, type=float, help='inference ratio between two images with 0 - 1 range')
22
+ parser.add_argument('--rthreshold', default=0.02, type=float, help='returns image when actual ratio falls in given range threshold')
23
+ parser.add_argument('--rmaxcycles', default=8, type=int, help='limit max number of bisectional cycles')
24
+ parser.add_argument('--model', dest='modelDir', type=str, default='train_log', help='directory with trained model files')
25
+
26
+ args = parser.parse_args()
27
+
28
+ try:
29
+ from train_log.RIFE_HDv3 import Model
30
+ model = Model()
31
+ model.load_model(args.modelDir, -1)
32
+ print("Loaded RIFE_HDv3 model.")
33
+ print("Checkpoint reached RIFE!")
34
+ except:
35
+ from train_log.IFNet_HDv3 import Model
36
+ model = Model()
37
+ model.load_model(args.modelDir, -1)
38
+ print("Loaded IFNet_HDv3 model.")
39
+ print("Checkpoint reached IFNet!")
40
+
41
+ model.eval()
42
+ model.device()
43
+
44
+ if args.img[0].endswith('.exr') and args.img[1].endswith('.exr'):
45
+ img0 = cv2.imread(args.img[0], cv2.IMREAD_COLOR | cv2.IMREAD_ANYDEPTH)
46
+ img1 = cv2.imread(args.img[1], cv2.IMREAD_COLOR | cv2.IMREAD_ANYDEPTH)
47
+ img0 = (torch.tensor(img0.transpose(2, 0, 1)).to(device)).unsqueeze(0)
48
+ img1 = (torch.tensor(img1.transpose(2, 0, 1)).to(device)).unsqueeze(0)
49
+ else:
50
+ img0 = cv2.imread(args.img[0], cv2.IMREAD_UNCHANGED)
51
+ img1 = cv2.imread(args.img[1], cv2.IMREAD_UNCHANGED)
52
+ img0 = (torch.tensor(img0.transpose(2, 0, 1)).to(device) / 255.).unsqueeze(0)
53
+ img1 = (torch.tensor(img1.transpose(2, 0, 1)).to(device) / 255.).unsqueeze(0)
54
+
55
+ n, c, h, w = img0.shape
56
+ ph = ((h - 1) // 32 + 1) * 32
57
+ pw = ((w - 1) // 32 + 1) * 32
58
+ padding = (0, pw - w, 0, ph - h)
59
+ img0 = F.pad(img0, padding)
60
+ img1 = F.pad(img1, padding)
61
+
62
+ if args.ratio:
63
+ img_list = [img0]
64
+ img0_ratio = 0.0
65
+ img1_ratio = 1.0
66
+ if args.ratio <= img0_ratio + args.rthreshold / 2:
67
+ middle = img0
68
+ elif args.ratio >= img1_ratio - args.rthreshold / 2:
69
+ middle = img1
70
+ else:
71
+ tmp_img0 = img0
72
+ tmp_img1 = img1
73
+ for inference_cycle in range(args.rmaxcycles):
74
+ middle = model.inference(tmp_img0, tmp_img1)
75
+ middle_ratio = (img0_ratio + img1_ratio) / 2
76
+ if args.ratio - (args.rthreshold / 2) <= middle_ratio <= args.ratio + (args.rthreshold / 2):
77
+ break
78
+ if args.ratio > middle_ratio:
79
+ tmp_img0 = middle
80
+ img0_ratio = middle_ratio
81
+ else:
82
+ tmp_img1 = middle
83
+ img1_ratio = middle_ratio
84
+ img_list.append(middle)
85
+ img_list.append(img1)
86
+ else:
87
+ img_list = [img0, img1]
88
+ for i in range(args.exp):
89
+ tmp = []
90
+ for j in range(len(img_list) - 1):
91
+ mid = model.inference(img_list[j], img_list[j + 1])
92
+ tmp.append(img_list[j])
93
+ tmp.append(mid)
94
+ tmp.append(img1)
95
+ img_list = tmp
96
+
97
+ if not os.path.exists('output'):
98
+ os.mkdir('output')
99
+
100
+ print("Checkpoint reached! output folder ok")
101
+
102
+ for i in range(len(img_list)):
103
+ filename_exr = os.path.join(OUTPUT_PATH, f"img{i}.exr")
104
+ filename_png = os.path.join(OUTPUT_PATH, f"img{i}.png")
105
+
106
+ if args.img[0].endswith('.exr') and args.img[1].endswith('.exr'):
107
+ cv2.imwrite(filename_exr, (img_list[i][0]).cpu().numpy().transpose(1, 2, 0)[:h, :w], [cv2.IMWRITE_EXR_TYPE, cv2.IMWRITE_EXR_TYPE_HALF])
108
+
109
+ success = cv2.imwrite(filename_png, (img_list[i][0] * 255).byte().cpu().numpy().transpose(1, 2, 0)[:h, :w])
110
+ print(f"Saving to {filename_png} → success: {success}")
111
+ print("Saving to:", os.path.abspath(filename_png))
112
+
113
+ else:
114
+ success = cv2.imwrite(filename_png, (img_list[i][0] * 255).byte().cpu().numpy().transpose(1, 2, 0)[:h, :w])
115
+ print(f"Saving to {filename_png} → success: {success}")
116
+ print("Saving to:", os.path.abspath(filename_png))
117
+
118
+
119
+
120
+ print("Checkpoint reached!")