-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
174 lines (157 loc) · 6.26 KB
/
test.py
File metadata and controls
174 lines (157 loc) · 6.26 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import torch
from train import loadBestModel
from torchvision.io import read_image
import torchvision.transforms.v2 as v2
import torchvision
import matplotlib.pyplot as plt
from postProcessing import thresholdImage
from random import randint
from torchvision.transforms import ToTensor
from models.loss import *
import argparse
import cv2
import torch.nn as nn
import os
import time
import gc # Garbage collector
if __name__ == "__main__":
argParser = argparse.ArgumentParser(
"Model Tester",
description="Loads best model and tests a part of the dataset on it",
)
argParser.add_argument("-d", "--data", help="Path of the dataset", required=True)
argParser.add_argument(
"-p",
"--postprocess",
help="Should the images be post-processed after being treated",
default=False,
action="store_true",
)
argParser.add_argument(
"-s",
"--show",
help="Show the treated images",
action="store_true",
default=False,
)
argParser.add_argument(
"-n", "--n", help="Number of images to treat", default=10, type=int
)
argParser.add_argument(
"-b", "--batch", help="Batch size for processing images", default=1, type=int
)
argParser.add_argument(
"-l",
"--loss",
action="store_true",
help="Print or not the loss of the treated images",
default=False,
)
argParser.add_argument(
"-r", "--random", action="store_true", help="Random page number", default=True
)
args = argParser.parse_args()
torch.cuda.empty_cache()
network = loadBestModel()[0]
network.eval()
network.to("cuda")
numberOfImagesToTest = args.n
batchSize = args.batch
imgs = []
noLinesImgs = []
start = randint(
1,
(
(
len(os.listdir(os.path.join(args.data, "generated-pages-blocks")))
- numberOfImagesToTest
)
if args.random
else 2
),
)
if args.show:
plt.figure(figsize=(numberOfImagesToTest * 3, 15)) # width scales with N
transforms = v2.Compose([v2.RandomResizedCrop(size=(512, 512), scale=(0.6, 1.0)), v2.ToDtype(torch.float32, scale=True)])
print("[LineRemoverNN] [Tester] Loading images...")
for i in range(start, start + numberOfImagesToTest):
pathImageTest = os.path.join(args.data, f"generated-pages-blocks/{i}.png")
pathImageNoLineTest = os.path.join(
args.data, f"generated-nolines-pages-blocks/{i}.png"
)
img = read_image(pathImageTest, torchvision.io.ImageReadMode.GRAY)
imgNoLine = read_image(pathImageNoLineTest, torchvision.io.ImageReadMode.GRAY)
if args.show:
plt.subplot(5, numberOfImagesToTest, (i - start) + 1)
plt.title(f"Lines {(i - start)}")
plt.imshow(img.squeeze().numpy(), cmap="gray")
plt.subplot(
5, numberOfImagesToTest, numberOfImagesToTest * 4 + (i - start) + 1
)
plt.title(f"Goal {(i - start)}")
plt.imshow(imgNoLine.squeeze().numpy(), cmap="gray")
imgs.append(img.float() / 255.0)
noLinesImgs.append(imgNoLine.float() / 255.0)
imgs = torch.stack(imgs)
noLinesImgs = torch.stack(noLinesImgs)
print("[LineRemoverNN] [Tester] Using RMSE Loss...")
loss = combined_loss
startedTime = time.time_ns()
print("[LineRemoverNN] [Tester] Treating images...")
totalLoss = 0
for batchStart in range(0, len(imgs), batchSize):
batchEnd = min(batchStart + batchSize, len(imgs))
batchImgs = imgs[batchStart:batchEnd].to("cuda")
batchNoLinesImgs = noLinesImgs[batchStart:batchEnd].to("cuda")
outputs = network(batchImgs)
for idx, outputImg in enumerate(outputs):
imgIdx = batchStart + idx
print(batchImgs[idx].min(), batchImgs[idx].max())
print(outputImg.min(), outputImg.max())
print(batchNoLinesImgs[idx].min(), batchNoLinesImgs[idx].max())
final = (outputImg.detach().cpu().squeeze() * 255).numpy()
_in = (batchImgs[idx].detach().cpu().squeeze() * 255).numpy()
if args.show:
plt.subplot(5, numberOfImagesToTest, numberOfImagesToTest + imgIdx + 1)
plt.title(f"Detected {imgIdx}")
plt.imshow(final, cmap="gray")
plt.subplot(
5, numberOfImagesToTest, numberOfImagesToTest * 2 + imgIdx + 1
)
plt.title(f"Output {imgIdx}")
plt.imshow(final, cmap="gray")
plt.subplot(
5, numberOfImagesToTest, numberOfImagesToTest * 3 + imgIdx + 1
)
plt.title(f"Final {imgIdx}")
plt.imshow(
thresholdImage(final) if args.postprocess else final, cmap="gray"
)
pixelLoss = loss(outputImg.unsqueeze(0), batchNoLinesImgs[idx].unsqueeze(0))
postProcessed = thresholdImage(final) if args.postprocess else final
tensorPostProcessed = (
ToTensor()(postProcessed).to("cuda").permute(0, 2, 1).unsqueeze(0)
)
pPLoss = loss(tensorPostProcessed, batchNoLinesImgs[idx].unsqueeze(0))
pPlos = 10
if args.loss:
print(
f"[LineRemoverNN] [Tester] Image {imgIdx} loss : {pixelLoss.item()} final loss : {5}"
)
totalLoss += pixelLoss.item()
del pixelLoss
#del tensorPostProcessed, pixelLoss, pPLoss, postProcessed
torch.cuda.empty_cache()
del batchImgs, batchNoLinesImgs, outputs
torch.cuda.empty_cache()
gc.collect()
elapsedTime = time.time_ns() - startedTime
elapsedTimeSeconds = elapsedTime / 1e9
print(
f"[LineRemoverNN] [Tester] Treated {len(imgs)} images in {elapsedTimeSeconds:.2f} seconds"
)
if args.loss:
print(f"[LineRemoverNN] [Tester] Avg loss : {totalLoss / len(imgs):.6f}")
if args.show:
plt.subplots_adjust(hspace=0.5)
plt.show()