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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
| import os.path
import logging
import torch
from utils import utils_logger
from utils import utils_image as util
# from utils import utils_model
from models.network_rrdbnet import RRDBNet as net
"""
Spyder (Python 3.6-3.7)
PyTorch 1.4.0-1.8.1
Windows 10 or Linux
Kai Zhang (cskaizhang@gmail.com)
github: https://github.com/cszn/BSRGAN
https://github.com/cszn/KAIR
If you have any question, please feel free to contact with me.
Kai Zhang (e-mail: cskaizhang@gmail.com)
by Kai Zhang ( March/2020 --> March/2021 --> )
This work was previously submitted to CVPR2021.
# --------------------------------------------
@inproceedings{zhang2021designing,
title={Designing a Practical Degradation Model for Deep Blind Image Super-Resolution},
author={Zhang, Kai and Liang, Jingyun and Van Gool, Luc and Timofte, Radu},
booktitle={arxiv},
year={2021}
}
# --------------------------------------------
"""
testsets = 'testsets'
testset_H = 'H_Image_Lora'
model_names = ['BSRGAN']
save_results = True
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def getModel():
utils_logger.logger_info('blind_sr_log', log_path='blind_sr_log.log')
logger = logging.getLogger('blind_sr_log')
# print(torch.__version__) # pytorch version
# print(torch.version.cuda) # cuda version
# print(torch.backends.cudnn.version()) # cudnn version
#testsets = 'testsets' # fixed, set path of testsets
#testset_H = 'H_Image_Lora' # ['RealSRSet','DPED']
#model_names = ['RRDB','ESRGAN','FSSR_DPED','FSSR_JPEG','RealSR_DPED','RealSR_JPEG']
#model_names = ['BSRGAN'] # 'BSRGANx2' for scale factor 2
#save_results = True
sf = 4
#device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
for model_name in model_names:
if model_name in ['BSRGANx2']:
sf = 2
model_path = os.path.join('model_zoo', model_name+'.pth') # set model path
logger.info('{:>16s} : {:s}'.format('Model Name', model_name))
# torch.cuda.set_device(0) # set GPU ID
logger.info('{:>16s} : {:<d}'.format('GPU ID', torch.cuda.current_device()))
torch.cuda.empty_cache()
# --------------------------------
# define network and load model
# --------------------------------
model = net(in_nc=3, out_nc=3, nf=64, nb=23, gc=32, sf=sf) # define network
# model_old = torch.load(model_path)
# state_dict = model.state_dict()
# for ((key, param),(key2, param2)) in zip(model_old.items(), state_dict.items()):
# state_dict[key2] = param
# model.load_state_dict(state_dict, strict=True)
model.load_state_dict(torch.load(model_path), strict=True)
model.eval()
for k, v in model.named_parameters():
v.requires_grad = False
model = model.to(device)
torch.cuda.empty_cache()
return model
'''
for testset_L in testset_Ls:
L_path = os.path.join(testsets, testset_L)
#E_path = os.path.join(testsets, testset_L+'_'+model_name)
E_path = os.path.join(testsets, testset_L+'_results_x'+str(sf))
util.mkdir(E_path)
logger.info('{:>16s} : {:s}'.format('Input Path', L_path))
logger.info('{:>16s} : {:s}'.format('Output Path', E_path))
idx = 0
for img in util.get_image_paths(L_path):
print('test print image')
print(img)
# --------------------------------
# (1) img_L
# --------------------------------
idx += 1
img_name, ext = os.path.splitext(os.path.basename(img))
logger.info('{:->4d} --> {:<s} --> x{:<d}--> {:<s}'.format(idx, model_name, sf, img_name+ext))
img_L = util.imread_uint(img, n_channels=3)
img_L = util.uint2tensor4(img_L)
img_L = img_L.to(device)
# --------------------------------
# (2) inference
# --------------------------------
img_E = model(img_L)
# --------------------------------
# (3) img_E
# --------------------------------
img_E = util.tensor2uint(img_E)
if save_results:
util.imsave(img_E, os.path.join(E_path, img_name+'_'+model_name+'.png'))
'''
import sys
def main(img):
#print('main')
#img = '/home/nvidia/BSRGAN/testsets/L_Image_Lora/Lincoln.png'
try:
OutPath = os.path.join(testsets, testset_H)
img_name, ext = os.path.splitext(os.path.basename(img))
img_L = util.imread_uint(img, n_channels=3)
img_L = util.uint2tensor4(img_L)
img_L = img_L.to(device)
#model = getModel()
img_E = model(img_L)
img_E = util.tensor2uint(img_E)
util.imsave(img_E, os.path.join(OutPath, img_name+'_' + 'H' + '.png'))
print('written')
except Interrupt:
return
def RUN():
import serial
import time
from os.path import join, dirname, realpath
import os
import threading
import filetype
port = serial.Serial(
port='/dev/ttyUSB0',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0
)
print('Connected to : ' + port.port)
portBuffer = []
recvTime = 0
numPacket = 0
imgNum = 0
rand_name = 'image_' + str(time.time()) + '.jpg'
path = join(join(dirname(realpath(__file__)), 'testsets/L_Image_Lora'), rand_name)
f = open(path, 'wb')
while True:
try:
dataShow = []
while port.inWaiting() > 0:
recvTime = time.time()
readByte = port.read()
portBuffer.append(readByte)
dataShow.append(readByte)
if len(dataShow) > 20:
dataShow = []
time.sleep(1)
if time.time() - recvTime > 4 and len(portBuffer) != 0:
rand_name = 'image_' + str(time.time()) + '.jpg'
imageBuffer = []
imgNum += 1
dataPath = join(join(dirname(realpath(__file__)), 'testsets/L_Image_Lora'), rand_name)
print('len portBuffer: ', len(portBuffer))
if f.closed:
print('closed')
f = open(dataPath, 'wb')
for i in range(0, len(portBuffer)):
if i < len(portBuffer) - 5:
if portBuffer[i] == b'\x05' and portBuffer[i+1] == b'\x00' and portBuffer[i+2] == b'\x82':
numPacket += 1
for j in range(0, int.from_bytes(portBuffer[i+7], 'big') - 2):
imageBuffer.append(portBuffer[i+10+j])
f.write(portBuffer[i+10+j])
print('Number of packet = %d' % numPacket)
numPacket = 0
print('image size = %d' % len(imageBuffer))
if len(imageBuffer) < 100:
print('closed file')
nameFile = f.name
f.close()
kind = filetype.guess(nameFile)
if kind is None:
print('failed')
else:
print(kind.mime)
print('call high resolution module')
main(nameFile)
portBuffer = []
except KeyBoardInterrupt:
print('Good bye')
break
if __name__ == '__main__':
param1 = '/home/nvidia/BSRGAN/testsets/L_Image_Lora/Lincoln.png'
if len(sys.argv) > 1:
param1 = sys.argv[1]
model = getModel()
#main(param1)
RUN()
|