File size: 8,429 Bytes
3715c63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35adcb2
 
 
3715c63
 
 
 
 
 
35adcb2
3715c63
 
 
 
 
 
35adcb2
3715c63
 
 
 
 
 
4c59c61
3715c63
 
 
 
 
 
 
 
 
35adcb2
3715c63
35adcb2
 
 
 
3715c63
35adcb2
 
 
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import datetime
import math
import os

import torch
import time

import skimage.io
import skimage.transform
import matplotlib.pyplot as plt
import glob

import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from skimage import exposure

toTensor = transforms.ToTensor()
toPIL = transforms.ToPILImage()


import numpy as np
from PIL import Image

from models import *

os.environ["CUDA_VISIBLE_DEVICES"] = "0"

def remove_dataparallel_wrapper(state_dict):
	r"""Converts a DataParallel model to a normal one by removing the "module."
	wrapper in the module dictionary

	Args:
		state_dict: a torch.nn.DataParallel state dictionary
	"""
	from collections import OrderedDict

	new_state_dict = OrderedDict()
	for k, vl in state_dict.items():
		name = k[7:] # remove 'module.' of DataParallel
		new_state_dict[name] = vl

	return new_state_dict

from argparse import Namespace


def GetOptions():
    # training options
    opt = Namespace()
    opt.model = 'rcan'
    opt.n_resgroups = 3
    opt.n_resblocks = 10
    opt.n_feats = 96
    opt.reduction = 16
    opt.narch = 0
    opt.norm = 'minmax'

    opt.cpu = False
    opt.multigpu = False
    opt.undomulti = False
    opt.device = torch.device('cuda' if torch.cuda.is_available() and not opt.cpu else 'cpu')

    opt.imageSize = 512
    opt.weights = "model/simrec_simin_gtout_rcan_512_2_ntrain790-final.pth"
    opt.root = "model/0080.jpg"
    opt.out = "model/myout"

    opt.task = 'simin_gtout'
    opt.scale = 1
    opt.nch_in = 9
    opt.nch_out = 1


    return opt


def GetOptions_allRnd_0215():
    # training options
    opt = Namespace()
    opt.model = 'rcan'
    opt.n_resgroups = 3
    opt.n_resblocks = 10
    opt.n_feats = 48
    opt.reduction = 16
    opt.narch = 0
    opt.norm = 'adapthist'

    opt.cpu = False
    opt.multigpu = False
    opt.undomulti = False
    opt.device = torch.device('cuda' if torch.cuda.is_available() and not opt.cpu else 'cpu')

    opt.imageSize = 512
    opt.weights = "model/0216_SIMRec_0214_rndAll_rcan_continued.pth"
    opt.root = "model/0080.jpg"
    opt.out = "model/myout"

    opt.task = 'simin_gtout'
    opt.scale = 1
    opt.nch_in = 9
    opt.nch_out = 1


    return opt



def GetOptions_allRnd_0317():
    # training options
    opt = Namespace()
    opt.model = 'rcan'
    opt.n_resgroups = 3
    opt.n_resblocks = 10
    opt.n_feats = 96
    opt.reduction = 16
    opt.narch = 0
    opt.norm = 'minmax'

    opt.cpu = False
    opt.multigpu = False
    opt.undomulti = False
    opt.device = torch.device('cuda' if torch.cuda.is_available() and not opt.cpu else 'cpu')

    opt.imageSize = 512
    opt.weights = "model/DIV2K_randomised_3x3_20200317.pth"
    opt.root = "model/0080.jpg"
    opt.out = "model/myout"

    opt.task = 'simin_gtout'
    opt.scale = 1
    opt.nch_in = 9
    opt.nch_out = 1


    return opt



def LoadModel(opt):
    print('Loading model')
    print(opt)

    net = GetModel(opt)
    print('loading checkpoint',opt.weights)
    checkpoint = torch.load(opt.weights,map_location=opt.device)

    if type(checkpoint) is dict:
        state_dict = checkpoint['state_dict']
    else:
        state_dict = checkpoint

    if opt.undomulti:
        state_dict = remove_dataparallel_wrapper(state_dict)
    net.load_state_dict(state_dict)

    return net


def prepimg(stack,self):

    inputimg = stack[:9]

    if self.nch_in == 6:
        inputimg = inputimg[[0,1,3,4,6,7]]
    elif self.nch_in == 3:
        inputimg = inputimg[[0,4,8]]

    if inputimg.shape[1] > 512 or inputimg.shape[2] > 512:
        print('Over 512x512! Cropping')
        inputimg = inputimg[:,:512,:512]


    if self.norm == 'convert': # raw img from microscope, needs normalisation and correct frame ordering
        print('Raw input assumed - converting')
        # NCHW
        # I = np.zeros((9,opt.imageSize,opt.imageSize),dtype='uint16')

        # for t in range(9):
        #     frame = inputimg[t]
        #     frame = 120 / np.max(frame) * frame
        #     frame = np.rot90(np.rot90(np.rot90(frame)))
        #     I[t,:,:] = frame
        # inputimg = I

        inputimg = np.rot90(inputimg,axes=(1,2))
        inputimg = inputimg[[6,7,8,3,4,5,0,1,2]] # could also do [8,7,6,5,4,3,2,1,0]
        for i in range(len(inputimg)):
            inputimg[i] = 100 / np.max(inputimg[i]) * inputimg[i]
    elif 'convert' in self.norm:
        fac = float(self.norm[7:])
        inputimg = np.rot90(inputimg,axes=(1,2))
        inputimg = inputimg[[6,7,8,3,4,5,0,1,2]] # could also do [8,7,6,5,4,3,2,1,0]
        for i in range(len(inputimg)):
            inputimg[i] = fac * 255 / np.max(inputimg[i]) * inputimg[i]


    inputimg = inputimg.astype('float') / np.max(inputimg) # used to be /255
    widefield = np.mean(inputimg,0)

    if self.norm == 'adapthist':
        for i in range(len(inputimg)):
            inputimg[i] = exposure.equalize_adapthist(inputimg[i],clip_limit=0.001)
        widefield = exposure.equalize_adapthist(widefield,clip_limit=0.001)
    else:
        # normalise
        inputimg = torch.tensor(inputimg).float()
        widefield = torch.tensor(widefield).float()
        widefield = (widefield - torch.min(widefield)) / (torch.max(widefield) - torch.min(widefield))

        if self.norm == 'minmax':
            for i in range(len(inputimg)):
                inputimg[i] = (inputimg[i] - torch.min(inputimg[i])) / (torch.max(inputimg[i]) - torch.min(inputimg[i]))
        elif 'minmax' in self.norm:
            fac = float(self.norm[6:])
            for i in range(len(inputimg)):
                inputimg[i] = fac * (inputimg[i] - torch.min(inputimg[i])) / (torch.max(inputimg[i]) - torch.min(inputimg[i]))



    # otf = torch.tensor(otf.astype('float') / np.max(otf)).unsqueeze(0).float()
    # gt = torch.tensor(gt.astype('float') / 255).unsqueeze(0).float()
    # simimg = torch.tensor(simimg.astype('float') / 255).unsqueeze(0).float()
    # widefield = torch.mean(inputimg,0).unsqueeze(0)


    # normalise
    # gt = (gt - torch.min(gt)) / (torch.max(gt) - torch.min(gt))
    # simimg = (simimg - torch.min(simimg)) / (torch.max(simimg) - torch.min(simimg))
    # widefield = (widefield - torch.min(widefield)) / (torch.max(widefield) - torch.min(widefield))
    inputimg = torch.tensor(inputimg).float()
    widefield = torch.tensor(widefield).float()
    return inputimg,widefield

def save_image(data, filename,cmap):
    sizes = np.shape(data)
    fig = plt.figure()
    fig.set_size_inches(1. * sizes[0] / sizes[1], 1, forward = False)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)
    ax.imshow(data, cmap=cmap)
    plt.savefig(filename, dpi = sizes[0])
    plt.close()


def EvaluateModel(net,opt,stack):

    outfile = datetime.datetime.utcnow().strftime('%H-%M-%S')
    outfile = 'ML-SIM_%s' % outfile

    os.makedirs(opt.out, exist_ok=True)

    print(stack.shape)
    inputimg, widefield = prepimg(stack, opt)

    if opt.norm == 'convert' or 'minmax' in opt.norm or 'adapthist' in opt.norm:
        cmap = 'viridis'
    else:
        cmap = 'gray'

    # skimage.io.imsave('%s_wf.png' % outfile,(255*widefield.numpy()).astype('uint8'))
    wf = (255*widefield.numpy()).astype('uint8')
    wf_upscaled = skimage.transform.rescale(wf,1.5,order=3,multichannel=False) # should ideally be done by drawing on client side, in javascript
    save_image(wf_upscaled,'%s_wf.png' % outfile,cmap)

    # skimage.io.imsave('%s.tif' % outfile, inputimg.numpy())

    inputimg = inputimg.unsqueeze(0)

    with torch.no_grad():
        sr = net(inputimg.to(opt.device))
        sr = sr.cpu()
        sr = torch.clamp(sr,min=0,max=1)
        print('min max',inputimg.min(),inputimg.max())

        pil_sr_img = toPIL(sr[0])

    if opt.norm == 'convert':
        pil_sr_img = transforms.functional.rotate(pil_sr_img,-90)

    # pil_sr_img.save('%s.png' % outfile) # true output for downloading, no LUT
    sr_img = np.array(pil_sr_img)
    # sr_img = exposure.equalize_adapthist(sr_img,clip_limit=0.01)
    skimage.io.imsave('%s.png' % outfile, sr_img) # true out for downloading, no LUT

    sr_img = skimage.transform.rescale(sr_img,1.5,order=3,multichannel=False) # should ideally be done by drawing on client side, in javascript

    save_image(sr_img,'%s_sr.png' % outfile,cmap)
    return outfile + '_sr.png', outfile + '_wf.png', outfile + '.png'
    # return wf, sr_img, outfile