コード例 #1
0
ファイル: WGAN.py プロジェクト: RSIP4SH/WGAN-for-image-fusion
def lowpass(image_batch, lda, npad):
    image = image_batch[0]
    image = np.squeeze(image)  # 去掉第一个维度,得到可直接处理的二维数据
    # 注意数组操作reshape和transpose的区别,transpose慎用(转置,改变原有数据顺序)
    # 定义存放高频信号和低频信号的batch
    low_filt_batch, high_filt_batch = tikhonov_filter(image, lda, npad)
    # 将low_filt_batch和high_filt_batch增加第一个维度
    low_filt_batch = np.expand_dims(low_filt_batch, axis=0)
    high_filt_batch = np.expand_dims(high_filt_batch, axis=0)
    # 将每个batch中的图像依次提取出来并分解,并各自合并成一个新的bacth
    for i in range(1, image_batch.shape[0]):
        image = image_batch[i]
        image = np.squeeze(image)
        low_filt_image, high_filt_image = tikhonov_filter(image, lda, npad)
        low_filt_image = np.expand_dims(low_filt_image, axis=0)
        high_filt_image = np.expand_dims(high_filt_image, axis=0)
        # 对第一个维度进行合并
        low_filt_batch = np.concatenate((low_filt_batch, low_filt_image),
                                        axis=0)
        high_filt_batch = np.concatenate((high_filt_batch, high_filt_image),
                                         axis=0)
    # 增加第二个维度
    low_filt_batch = np.expand_dims(low_filt_batch, axis=1)
    high_filt_batch = np.expand_dims(high_filt_batch, axis=1)

    return low_filt_batch, high_filt_batch
コード例 #2
0
def csc(imgs, D, args, lmbda=None, opt=None):
    if lmbda is None:
        lmbda = args.lmbda

    if opt is None:
        opt = sporco.admm.cbpdn.ConvBPDN.Options({
            'Verbose': False,
            'MaxMainIter': args.nInnerIter,
            'HighMemSolve': True,
            'RelStopTol': 5e-3,
            'AuxVarObj': False
        })

    s = np.transpose(imgs[..., 0], (1, 2, 0))
    sl, sh = util.tikhonov_filter(s, args.lmbdaPre)

    ys = []
    coefs = []
    for i in range(sh.shape[-1]):
        coef = sporco.cuda.cbpdn(D, sh[..., i], lmbda, opt, dev=args.device)
        y = np.sum(cp2np(sporco.cupy.linalg.fftconv(np2cp(D), np2cp(coef))),
                   -1) + sl[..., i]

        coefs.append(coef)
        ys.append(y[np.newaxis, ..., np.newaxis])

    return np.concatenate(ys, 0), np.array(coefs)
コード例 #3
0
ファイル: test.py プロジェクト: RSIP4SH/WGAN-for-image-fusion
def decomp_combine_image(ir_image, vi_image):
    # 转换为float类型 .astype(np.float)
    ir_low, ir_high = tikhonov_filter((ir_image - 127.5) / 127.5, 5, 16)
    vi_low, vi_high = tikhonov_filter((vi_image - 127.5) / 127.5, 5, 16)

    # 对红外和可见光图像的高频区域进行融合
    combine_high = ir_high
    row, col = ir_high.shape
    for m in range(row):
        for n in range(col):
            if abs(ir_high[m][n]) > abs(vi_high[m][n]):
                combine_high[m][n] = ir_high[m][n]
            else:
                combine_high[m][n] = vi_high[m][n]

    combine_low = (ir_low + vi_low) / 2
    combine_average = (ir_image + vi_image) / 2

    return combine_low
コード例 #4
0
def default_transform(blob, pad_size=None, tikhonov=True):
    if pad_size is not None:
        pad = [(pad_size, pad_size), (pad_size, pad_size)] + \
            [(0, 0) for _ in range(blob.ndim-2)]
        blob = np.pad(blob, pad, mode='constant')
    if tikhonov:
        # fix lambda to be 5
        sl, sh = su.tikhonov_filter(blob, 5.)
    else:
        sl, sh = np.zeros_like(blob), blob
    return sl, sh
コード例 #5
0
def recons(name):
    if not os.path.exists(os.path.join(out_path, name)):
        impath = os.path.join(im_path, name)
        im = get_im(impath)
        sl, sh = util.tikhonov_filter(im, fltlmbd, npd)
        opt = cbpdn.ConvBPDN.Options({
            'Verbose': False,
            'MaxMainIter': 200,
            'RelStopTol': 5e-3,
            'AuxVarObj': False
        })
        b = cbpdn.ConvBPDN(basis, sh, lmbda, opt)
        X = b.solve()
        print("ConvBPDN solve time: %.2fs" % b.timer.elapsed('solve'))
        shr = b.reconstruct().squeeze()
        imgr = sl + shr
        cv2.imwrite(os.path.join(out_path, name), 255 * imgr / imgr.max())
    else:
        #    print(name)
        pass
コード例 #6
0
def main():
    """Main entry."""
    args = parse_args()
    if not os.path.exists(args.output_path):
        os.makedirs(args.output_path)
    log_name = os.path.join(
        args.output_path, '{:s}.{:%Y-%m-%d_%H-%M-%S}.log'.format(
            args.name,
            datetime.datetime.now(),
        ))
    logger = setup_logging(__name__, log_name)
    train_data = image_dataset.create_image_blob('fruit',
                                                 np.float32,
                                                 scaled=True,
                                                 gray=False)
    sl, sh = su.tikhonov_filter(train_data, 5, 16)
    D0 = np.random.randn(8, 8, 3, 64)
    solvers = [
        globals()['setup_{}'.format(s)](D0, sh, args.lmbda)
        for s in ('ConvBPDNDictLearn_FISTA', 'ConvBPDNSliceDictLearnFISTA')
    ]
    for s in solvers:
        logger.info('Solving CDL algorithm: %s', type(s).__name__)
        D1 = s.solve()
        logger.info('%s solve time: %.2fs',
                    type(s).__name__, s.timer.elapsed('solve'))
        np.save(
            os.path.join(args.output_path,
                         '{}_dict.npy'.format(type(s).__name__)), D1.squeeze())
    fig = plt.figure()
    for s in solvers:
        plt.plot(getattr(s.getitstat(), args.xaxis),
                 getattr(s.getitstat(), args.yaxis),
                 label=type(s).__name__)
    plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
    plt.legend()
    fig.savefig(os.path.join(args.output_path,
                             '{}_{}.pdf'.format(args.xaxis, args.yaxis)),
                bbox_inches='tight')
コード例 #7
0
def train_batch_model(D0, train_blob, opt, args):
    """Train for batch dictlrn model."""
    logger = logging.getLogger(__name__)
    dname = args.dataset if not args.use_gray else args.dataset + '.gray'
    if os.path.exists(os.path.join(args.output_path, 'iter_record.mat')):
        iter_record = sio.loadmat(
            os.path.join(args.output_path, 'iter_record.mat'))['iter_record']
        selected = list(set(iter_record.ravel().tolist()))
        if args.batch_max_samples > 0 and args.batch_max_samples < len(
                selected):
            selected = selected[:args.batch_max_samples]
        if len(selected) < train_blob.shape[-1]:
            logger.info(
                'Selected %d -> %d train samples for training batch model',
                train_blob.shape[-1], len(selected))
            train_blob = train_blob[..., selected]
    if not args.no_tikhonov_filter:
        # fix lambda to be 5
        _, train_blob = su.tikhonov_filter(train_blob, 5.)
    path = os.path.join(args.output_path, 'ConvBPDNDictLearn')
    if not os.path.exists(path):
        os.makedirs(path)

    def _callback(d):
        """Snapshot dictionaries for every iteration."""
        _D = d.getdict().squeeze()
        np.save(os.path.join(path, '{}.{}.npy'.format(dname, d.j)), _D)
        return 0

    opt['Callback'] = _callback
    solver = cbpdndl.ConvBPDNDictLearn(D0,
                                       train_blob,
                                       args.lmbda,
                                       opt=opt,
                                       xmethod='admm',
                                       dmethod='cns')
    solver.solve()
    return solver
コード例 #8
0
def train_models(D0, solvers, train_blob, args):
    """Function for training every solvers."""
    epochs = args.epochs if args.epochs > 0 else None
    batch_size = args.batch_size if args.batch_size > 0 else None
    loader = BlobLoader(train_blob, epochs, batch_size)
    sample = loader.random_sample()
    solvers = {
        k: sol_name(D0, sample, args.lmbda, opt=opt)
        for k, (sol_name, opt) in solvers.items()
    }
    dname = args.dataset if not args.use_gray else args.dataset + '.gray'
    for e, blob in enumerate(loader):
        if args.pad_boundary:
            assert args.patch_size % 2 == 1, 'Patch size should be odd'
            radius = args.patch_size // 2
            pad = [(radius, radius), (radius, radius)] + \
                [(0, 0) for _ in range(blob.ndim-2)]
            #  pad = [(0, args.patch_size-1), (0, args.patch_size-1)] + \
            #      [(0, 0) for _ in range(blob.ndim-2)]
            blob = np.pad(blob, pad, mode='constant')
        if not args.no_tikhonov_filter:
            # fix lambda to be 5
            _, blob = su.tikhonov_filter(blob, 5.)
        for k, s in solvers.items():
            s.solve(blob.copy())
            path = os.path.join(args.output_path, k)
            np.save(os.path.join(path, '{}.{}.npy'.format(dname, e)),
                    s.getdict().squeeze())
            if args.visdom is not None:
                tiled_dict = su.tiledict(s.getdict().squeeze())
                if not args.use_gray:
                    tiled_dict = tiled_dict.transpose(2, 0, 1)
                args.visdom.image(tiled_dict, opts=dict(caption=f'{k}.{e}'))
    # snapshot iteration record
    sio.savemat(os.path.join(args.output_path, 'iter_record.mat'),
                {'iter_record': loader.record})
    return solvers
コード例 #9
0
# In[10]:


if showPlots:
    plt.figure(figsize=[6,6])
    plt.imshow(imgs[0, 128:-128, 128:-128, 0].T, 'gray', vmin=args.vmin, vmax=args.vmax)


# In[20]:


# training, offline learning
np.random.seed(0)

s = np.transpose(imgs[..., 0], (1, 2, 0))
sl, sh = util.tikhonov_filter(s, args.lmbdaPre)

D0 = np.random.normal(size = args.kernelSize + [args.nChannels]).astype(np.float32)

opt = cbpdndl.ConvBPDNDictLearn.Options({'Verbose': True, 'MaxMainIter': args.nPreIter,
                                         'CBPDN': {'rho': 100*args.lmbda + 1, 
                                                   'AutoRho': {'Enabled': True}, 
                                                   'RelaxParam': 1.8, 
                                                   'RelStopTol': 1e-7},
                                         'CCMOD': {'rho': 10.0, 'ZeroMean': False}}, 
                                        dmethod='cns')
learner = cbpdndl.ConvBPDNDictLearn(D0, sh, args.lmbda, opt, dmethod='cns')
learner.solve()

D1 = learner.getdict()
D1 = D1.squeeze()
コード例 #10
0
def lowpass(s, lda, npad):
    return tikhonov_filter(s, lda, npad)
コード例 #11
0
# Dr = mysolve(cri, Dr0, Xr, Shr, 1e-4, maxitr=50, debug_dir='./debug')
# # Dr = nakashizuka_solve(cri, Dr0, Xr, Shr, debug_dir='./debug')
# # Dr = sporcosolve(cri, Dr, Shr)
# # fig = plot.figure(figsize=(7, 7))
# # plot.imview(util.tiledict(Dr.squeeze()), fig=fig)
# # fig.savefig('dict.png')
# # # evaluate_result(cri, Dr0, Dr, Shr, Sr_add=Slr)


exim1 = util.ExampleImages(scaled=True, zoom=0.5, pth='./')
S1_test = exim1.image('couple.tiff')
exim2 = util.ExampleImages(scaled=True, zoom=1, pth='./')
S2_test = exim2.image('LENNA.bmp')
S_test = np.dstack((S1_test, S2_test))
cri_test = cnvrep.CSC_ConvRepIndexing(D, S_test)
Sl_test, Sh_test = util.tikhonov_filter(S_test, 5, 16)
Slr_test = np.asarray(Sl_test.reshape(cri_test.shpS), dtype=S_test.dtype)
Shr_test = np.asarray(Sh_test.reshape(cri_test.shpS), dtype=S_test.dtype)

# evaluate_result(cri, Dr0, Dr, Shr_test, Sr_add=Slr_test, lmbda=5e-3)

outdir = './no_low-pass'


#--------実験を行う手法を指定-------
prefix = 'nakashizuka_solve' # 中静先生の論文
# prefix = 'mysolve' # 提案手法
# prefix = 'sporcosolve' # sporcoライブラリに実装された方法(B. Wohlbergによる)
#----------------------------------

if prefix == 'nakashizuka_solve':
コード例 #12
0
def crop(x, n=8):

    return x[n:-n, n:-n]


# img = util.ExampleImages().image('monarch.png', zoom=0.5, scaled=True,
#                                   gray=True, idxexp=np.s_[:, 160:672])

img = mpimg.imread('barbara1.png')
np.random.seed(12345)
imgn = img + np.random.normal(0.0, 0.1, img.shape)

print("Noisy image PSNR:    %5.2f dB" % sm.psnr(img, imgn))
npd = 16
fltlmbd = 5.0
imgnl, imgnh = util.tikhonov_filter(imgn, fltlmbd, npd)
D = util.convdicts()['G:8x8x32']
D = D[:, :, 0:14]
# D = np.random.randn(8, 8, 14)
imgnpl, imgnph = util.tikhonov_filter(pad(imgn), fltlmbd, npd)
W = spl.irfftn(
    np.conj(spl.rfftn(D, imgnph.shape,
                      (0, 1))) * spl.rfftn(imgnph[..., np.newaxis], None,
                                           (0, 1)), imgnph.shape, (0, 1))
W = W**2
W = 1.0 / (np.maximum(np.abs(W), 1e-8))

lmbda = 1.5e-2
mu = 0.005
opt1 = cbpdn.ConvBPDN.Options({
    'Verbose': True,
コード例 #13
0
from sporco.dictlrn import cbpdndl
from sporco import util
from sporco import plot
plot.config_notebook_plotting()
# %%
exim = util.ExampleImages(scaled=True, zoom=0.25, gray=True)
S1 = exim.image('barbara.png', idxexp=np.s_[10:522, 100:612])
S2 = exim.image('kodim23.png', idxexp=np.s_[:, 60:572])
S3 = exim.image('monarch.png', idxexp=np.s_[:, 160:672])
S4 = exim.image('sail.png', idxexp=np.s_[:, 210:722])
S5 = exim.image('tulips.png', idxexp=np.s_[:, 30:542])
S = np.dstack((S1, S2, S3, S4, S5))
npd = 16
fltlmbd = 5
sl, sh = util.tikhonov_filter(S, fltlmbd, npd)
D0 = np.random.randn(10, 10, 32)
lmbda = 0.1
opt = cbpdndl.ConvBPDNDictLearn.Options(
    {
        'Verbose': True,
        'MaxMainIter': 200,
        'CBPDN': {
            'rho': 50.0 * lmbda + 0.5
        },
        'CCMOD': {
            'rho': 10.0,
            'ZeroMean': True
        }
    },
    dmethod='cns')
コード例 #14
0
def lowpass(
    s, lda, npad
):  # In this function, low pass filtering is done by using Tikhonov filter.
    return tikhonov_filter(s, lda, npad)
コード例 #15
0
zoom = 0.25
N = int(512 * 0.25)
K = 5

exim = util.ExampleImages(scaled=True, zoom=zoom, gray=True)
s1 = exim.image('barbara.png', idxexp=np.s_[10:522, 100:612])
s2 = exim.image('kodim23.png', idxexp=np.s_[:, 60:572])
s3 = exim.image('monarch.png', idxexp=np.s_[:, 160:672])
s4 = exim.image('sail.png', idxexp=np.s_[:, 210:722])
s5 = exim.image('tulips.png', idxexp=np.s_[:, 30:542])
ori = np.asarray([s1, s2, s3, s4, s5])

npd = 16
fltlmbd = 10

sl, sh = util.tikhonov_filter(np.moveaxis(ori, 0, -1), fltlmbd, npd)
sl = np.moveaxis(sl, -1, 0)
sh = np.moveaxis(sh, -1, 0)

S = sh.reshape(5, N, N)

# 辞書枚数
M = 16
d_size = 10
K = len(S)

D0 = np.random.randn(M, d_size, d_size)

roopcount = 0

# %%
コード例 #16
0
cap = cv2.VideoCapture(pth)
k = 0
while (cap.isOpened()):
    ret, frame = cap.read()
    if ret is False:
        break
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    vid[..., k] = gray
    k += 1
cap.release()
vid = vid[50:114, 50:114, 0:32] / 255.0

# Highpass filter frames
npd = 16
fltlmbd = 5
vl, vh = util.tikhonov_filter(vid, fltlmbd, npd)

# Initial dictionary
np.random.seed(12345)
D0 = np.random.randn(5, 5, 3, 75)

# Set ConvBPDNDictLearn parameters
lmbda = 0.1
opt = cbpdndl.ConvBPDNDictLearn.Options({
    'Verbose': True,
    'MaxMainIter': 200,
    'CBPDN': {
        'rho': 1e3 * lmbda,
        'AutoRho': {
            'Enabled': True
        }
コード例 #17
0
ファイル: gwnden_gry.py プロジェクト: bwohlberg/sporco
Load a reference image and corrupt it with Gaussian white noise with $\sigma = 0.1$. (The call to ``numpy.random.seed`` ensures that the pseudo-random noise is reproducible.)
"""

img = util.ExampleImages().image('monarch.png', zoom=0.5, scaled=True,
                                 gray=True, idxexp=np.s_[:, 160:672])
np.random.seed(12345)
imgn = img + np.random.normal(0.0, 0.1, img.shape)


"""
Highpass filter test image.
"""

npd = 16
fltlmbd = 5.0
imgnl, imgnh = util.tikhonov_filter(imgn, fltlmbd, npd)


"""
Load dictionary.
"""

D = util.convdicts()['G:8x8x128']


"""
Set solver options. See Section 8 of :cite:`wohlberg-2017-convolutional2` for details of construction of $\ell_1$ weighting matrix $W$.
"""

imgnpl, imgnph = util.tikhonov_filter(pad(imgn), fltlmbd, npd)
W = spl.irfftn(np.conj(spl.rfftn(D, imgnph.shape, (0, 1))) *
コード例 #18
0
ファイル: test_util.py プロジェクト: bwohlberg/sporco
 def test_12(self):
     img = np.random.randn(64, 64)
     iml, imh = util.tikhonov_filter(img, 5.0)
コード例 #19
0
ファイル: test_util.py プロジェクト: bwohlberg/sporco
 def test_13(self):
     img = np.random.randn(16, 16, 16)
     iml, imh = util.tikhonov_filter(img, 2.0, npd=8)
コード例 #20
0
"""
Load example image.
"""

img = util.ExampleImages().image('kodim23.png', scaled=True,
                                 idxexp=np.s_[160:416,60:316])


"""
Highpass filter example image.
"""

npd = 16
fltlmbd = 10
slc, shc = util.tikhonov_filter(img, fltlmbd, npd)


"""
Load greyscale convolutional dictionary.
"""

D = util.convdicts()['G:8x8x64']


"""
Learn a standard dictionary $B$ to represent all pixel colours in the example image. Since the standard dictionary is a $3 \times 6$ matrix, the sparse representation $X$ has 6 pseudo-channels, which are converted to the 3 channels of the example image by right-multiplication by the dictionary $B$, giving $XB$.
"""

S = shc.reshape((-1, shc.shape[-1])).T
np.random.seed(12345)
コード例 #21
0
exim = util.ExampleImages(scaled=True, zoom=0.25)
S1 = exim.image('barbara.png', idxexp=np.s_[10:522, 100:612])
S2 = exim.image('kodim23.png', idxexp=np.s_[:, 60:572])
S3 = exim.image('monarch.png', idxexp=np.s_[:, 160:672])
S4 = exim.image('sail.png', idxexp=np.s_[:, 210:722])
S5 = exim.image('tulips.png', idxexp=np.s_[:, 30:542])
S = np.stack((S1, S2, S3, S4, S5), axis=3)


"""
Highpass filter training images.
"""

npd = 16
fltlmbd = 5
sl, sh = util.tikhonov_filter(S, fltlmbd, npd)


"""
Construct initial dictionary.
"""

np.random.seed(12345)
D0 = np.random.randn(8, 8, 3, 64)


"""
Set regularization parameter and options for dictionary learning solver.
"""

lmbda = 0.2
コード例 #22
0
 def test_13(self):
     img = np.random.randn(16, 16, 16)
     iml, imh = util.tikhonov_filter(img, 2.0, npd=8)
コード例 #23
0
 def test_07(self):
     img = np.random.randn(64, 64)
     iml, imh = util.tikhonov_filter(img, 5.0)
コード例 #24
0
ファイル: cbpdndl_video.py プロジェクト: bwohlberg/sporco
nfrm = reader.get_length()
frmlst = []
for i, frm in enumerate(reader):
    if i >= 250:
        frm = zoom(util.rgb2gray(frm.astype(np.float32)/255.0), 0.25)
        frmlst.append(frm[20:-20, 70:-70])
vid = np.stack(frmlst, axis=2)


"""
Highpass filter video frames.
"""

npd = 16
fltlmbd = 10
vl, vh = util.tikhonov_filter(vid, fltlmbd, npd)


"""
Construct initial dictionary.
"""

np.random.seed(12345)
D0 = np.random.randn(5, 5, 3, 25)


"""
Set regularization parameter and options for dictionary learning solver.
"""

lmbda = 0.1