Beispiel #1
0
def idct2d_bb(a, shape=None):
    """Computes the band-by-band inverse 2D Normalized DCT-II

    If the input a is a 3D data cube, the 2D dct will be computed for each 2D
    images staked along the 2nd axis.

    Arguments
    ---------
    A: (l, m*n) or (m, n, l) numpy array
        2D or 3D multi-band data DCT decomposition.
        If the data has 3 dimensions, the last axis is for spetra.
        If the data is 2D, the first axis is for spectra.
    shape: optional, (m, n, l) tuple
        This is the data shape. This parameter is required only if input data
        are 2D.

    Returns
    -------
    (l, m*n) or (m, n, l) numpy array
        The image matrix.
    """
    # If the data are 2D, it should be transformed back to 3D data.
    if a.ndim == 3:

        return fp.idctn(a, axes=(0, 1), norm='ortho')

    else:

        if shape is None:
            raise ValueError('shape parameter required for 2D data.')

        A = fp.idctn(a.T.reshape(shape), axes=(0, 1), norm='ortho')

        m, n, B = shape
        return A.reshape((m * n, B)).T
Beispiel #2
0
def predict_from_trend_unscaled(training_Ftxx, cycle_length, pred_length):
    (nk1, nk2) = training_Ftxx.shape[1:]
    # For now
    training_Ftkk = dctn(training_Ftxx, norm='ortho', axes=[1,
                                                            2])[:, :nk1, :nk2]

    (Ftkk_detrended, bkk,
     Ckk) = separate_trends_unscaled(training_Ftkk, cycle_length)

    t0 = training_Ftxx.shape[0]
    # Where we start predicting from
    training_ts = np.arange(0, t0)
    # ts corresponding to training
    pred_ts = np.arange(t0, t0 + pred_length)

    trend_descriptors = (bkk, Ckk, cycle_length)
    f0kk = Ftkk_detrended[-1]
    # We start where the training data ends

    predicted_Ftkk = kspace_predict_from_trend_unscaled(
        f0kk, trend_descriptors, pred_ts)

    predicted_Ftxx = idctn(predicted_Ftkk, norm='ortho', axes=[1, 2])

    return predicted_Ftxx, predicted_Ftkk
Beispiel #3
0
    def block_add_wm(self,block,index,i):
        
        i = i%(self.wm_shape[0]*self.wm_shape[1])

        wm_1 = self.wm_flatten[i]
        block_dct = dctn(block,norm='ortho')
        block_dct_flatten = block_dct.flatten().copy()
        
        block_dct_flatten = block_dct_flatten[index]
        block_dct_shuffled = block_dct_flatten.reshape(self.block_shape)
        U,s,V = np.linalg.svd(block_dct_shuffled)
        max_s = s[0]
        s[0] = (max_s-max_s%self.mod+3/4*self.mod) if wm_1>=128 else (max_s-max_s%self.mod+1/4*self.mod)
        if self.mod2:
            max_s = s[1]
            s[1] = (max_s-max_s%self.mod2+3/4*self.mod2) if wm_1>=128 else (max_s-max_s%self.mod2+1/4*self.mod2)
        # s[1] = (max_s-max_s%self.mod2+3/4*self.mod2) if wm_1<128 else (max_s-max_s%self.mod2+1/4*self.mod2)

        ###np.dot(U[:, :k], np.dot(np.diag(sigma[:k]),v[:k, :]))
        block_dct_shuffled = np.dot(U,np.dot(np.diag(s),V))

        block_dct_flatten = block_dct_shuffled.flatten()
   
        block_dct_flatten[index] = block_dct_flatten.copy()
        block_dct  = block_dct_flatten.reshape(self.block_shape)

        return idctn(block_dct,norm='ortho')
Beispiel #4
0
def dequantize_block(block, quant):
    # dequantization
    freq = (block.astype(np.float) * quant) / 50
    # inverse dct
    block = fftpack.idctn(freq, type=2, norm='ortho') + 0.5
    # clip to [0..1]
    return np.clip(block, 0, 1)
Beispiel #5
0
def main(args):
    # get image data
    img = io.imread(args.image).astype('float32') / 255
    img = _img_reduce_constast(img, args)
    enc_msg = _get_hidden_image_string(args)
    # compute dct transform
    img_dct = dctn(img, axes=[0, 1], norm='ortho')
    # check encoded message length
    max_length = _max_msg_length(img_dct, enc_msg, args)
    assert len(enc_msg) < max_length,\
        'hidden image too large, needs to be resized under {}KB'.format(max_length >> 10)
    # encode computation
    _encode_length(img_dct, enc_msg)
    _encode_image(img_dct, enc_msg)
    # compute inverse dct transform
    img_ret = idctn(img_dct, axes=[0, 1], norm='ortho')
    if img_ret.min() < 0 or img_ret.max() > 1:
        print(
            'Warning: data encoded may not be perfectly recovered or not at all. Consider lowering reduce ratio'
        )
        img_ret = np.clip(img_ret, 0, 1)
    # quantization
    img_ret = (img_ret * QUANT_SCALE).astype('uint16')
    # save to disk
    assert args.output.endswith(
        '.tiff'), 'output image has to be in tiff format'
    io.imsave(args.output, img_ret)
 def idct_decoder(self, TransAllQuant_list, Q, blocksize=8):
     h, w = TransAllQuant_list[0].shape
     c = len(TransAllQuant_list)
     B = blocksize
     DecAll = np.zeros((h, w, c), np.uint8)
     for idx, channel in enumerate(TransAllQuant_list):
         channelrows = channel.shape[0]
         channelcols = channel.shape[1]
         blocks = self.blockshaped(channel, blocksize, blocksize)
         dequantblocks = blocks * Q[idx]
         idct_blocks = fftpack.idctn(dequantblocks,
                                     axes=(-2, -1),
                                     norm='ortho')
         idct_blocks = np.round(
             idct_blocks
         ) + 128  # inverse shiftign of the shift of the pixel values, sucht that their value range is [0,...,255].
         idct_blocks[idct_blocks > 255] = 255
         idct_blocks[idct_blocks < 0] = 0
         idct_arr = self.unblockshaped(idct_blocks, channelrows,
                                       channelcols).astype(np.uint8)
         if idx != 0:
             idct_arr = cv2.resize(
                 idct_arr, (w, h)
             )  # the subsampled chrominance channels are interpolated, using cv2.INTER_LINEAR in default.
         DecAll[:, :, idx] = np.round(idct_arr)
     return DecAll
Beispiel #7
0
 def test_idct2_3(self):
     image = np.array(io.imread('images/sample2048x2048.jpg'),
                      dtype=np.float)
     idct_image = idct2(image)
     blocks = skimage.util.view_as_blocks(image, (8, 8))
     blocks[:] = idctn(blocks, axes=(2, 3), norm='ortho')
     self.assertTrue(np.max(np.abs(image - idct_image)) < 1e-10)
Beispiel #8
0
def img_recov(img, q_table, original_shape):
    m, n = img.shape[:2]
    n_channels = img.shape[2] if len(img.shape) > 2 else 1

    r_img = np.zeros(img.shape)

    for ch in range(n_channels):
        cur_channel = img if n_channels == 1 else img[:, :, ch]

        for i in range(0, m, 8):
            for j in range(0, n, 8):
                b = cur_channel[i:i + 8, j:j + 8]

                # get coeficients back
                b_dct = np.multiply(b, q_table)

                # apply idct type 2 (DCT III) to get the image in the spatial domain
                # r_b: retrieved block
                r_b = idctn(b_dct, norm='ortho')

                if n_channels == 1:
                    r_img[i:i + 8, j:j + 8] = r_b
                else:
                    r_img[i:i + 8, j:j + 8, ch] = r_b

    # Important to NOT normalize here!! otherwise we will lose the embed data!!
    o_w, o_h = original_shape[:2]
    return r_img[:o_w, :o_h]
Beispiel #9
0
def decomp_tcd (src):
    return fttp.idctn(src, axes=[1,3], norm='ortho'
                      ).reshape((
                          src.shape[0]*16,
                          src.shape[2]*16,
                          3
                          ))
Beispiel #10
0
    def reverse_process(self, stegano_blocks):
        """
        reversed process of the quantify and DCT transformation
        :param stegano_blocks:written quantified matrix
        :return:
        """

        img_arrs = []
        for i in range(3):
            quant_tbl = quant_tbl_1 if i == 0 else quant_tbl_2
            iquant_blocks = [[block * quant_tbl for block in line]
                             for line in stegano_blocks[i]]
            idct_blocks = [[
                np.array(fftpack.idctn(block), dtype=int) // 256
                for block in line
            ] for line in iquant_blocks]
            img_arrs.append(
                np.array(np.vstack([np.hstack(line) for line in idct_blocks]),
                         dtype=np.uint8))

        img_shape = img_arrs[0].shape
        tmp_arrs = np.zeros((img_shape[0], img_shape[1], 3), dtype=np.uint8)
        for i in range(3):
            tmp_arrs[:, :, i] = img_arrs[i]
        Image.fromarray(np.array(tmp_arrs)).save('reversed_img.bmp')
Beispiel #11
0
def embed_bit(block, bit):
    patch = block.copy()
    coefs = dctn(patch)
    while not valid_coefficients(coefs, bit,
                                 P) or (bit != retrieve_bit(patch)):
        coefs = change_coefficients(coefs, bit)
        patch = double_to_byte(idctn(coefs) / (2 * n)**2)
    return patch
Beispiel #12
0
def freq2im(f):
    """ Applies an inverse discrete Fourier transform

    Args:
        f: the coefficients of an image
    Returns:
        the image which Fourier coefficients correspond to f
    """
    return fp.idctn(f, norm='ortho')
Beispiel #13
0
        def mult_efficient(v):
            """Efficient matrix-vector multiplication for the adjoint of the measurement operator."""

            temp = np.zeros(n)
            for kk in range(k):
                temp += S[:, kk] * idctn((z[kk * n:(kk + 1) * n] * dctn(
                    (S[:, kk] * v.reshape(-1)).reshape(n_img, n_img),
                    norm='ortho').reshape(n)).reshape(n_img, n_img),
                                         norm='ortho').reshape(n)
            return temp
Beispiel #14
0
    def invert_quantize_dct(array, table):
        print("invert_dct_quantize")
        row = 8
        col = 8
        invert_quantize = lambda x: Encode.invert_quantize(x, table)
        new_array = Encode.apply_fn(array, row, col, invert_quantize)
        idct = lambda x: idctn(x, norm='ortho')
        new_array = Encode.apply_fn(new_array, row, col, idct)
        new_array = new_array + 128

        return np.clip(new_array, a_min=0, a_max=255)
 def label_idct(self, TransAll, blocksize=8):
     B = blocksize
     h, w = TransAll.shape
     blocks = self.blockshaped(TransAll, B, B)
     idct_blocks = fftpack.idctn(blocks, axes=(-2, -1), norm='ortho')
     idct_blocks = np.round(
         idct_blocks
     ) + 128  # inverse shiftign of the shift of the pixel values, sucht that their value range is [0,...,255].
     idct_blocks[idct_blocks > 255] = 255
     idct_blocks[idct_blocks < 0] = 0
     idct_arr = self.unblockshaped(idct_blocks, h, w).astype(np.uint8)
     return idct_arr
Beispiel #16
0
    def alter_freq(self):
        self.progress.setValue(0)
        self.progress.show()

        round_image = np.vectorize(self.round_image_)

        # Applicazione dct
        # d_img = dct(dct(self.img.T, norm='ortho').T, norm='ortho')
        d_img = dctn(self.img, norm='ortho')

        self.progress.setValue(25)

        # modifica frequenze
        for i, row in enumerate(d_img):
            self.progress.setValue(26 + (45 * i) / d_img.shape[0])
            for j, col in enumerate(row):
                if i + j >= self.d:
                    d_img[i, j] *= self.beta


        # Applicazione inversa dct e arrotondamento
        # i_img = round_image(idct(idct(d_img.T, norm='ortho').T, norm='ortho'))
        i_img = round_image(idctn(d_img, norm='ortho'))


        self.progress.setValue(self.progress.value() + 25)

        plt.figure(1)
        # Visualizzazione
        plt.subplot(122)
        plt.imshow(i_img, cmap=plt.get_cmap('gray'), vmin=0, vmax=255)
        plt.title('Immagine alterata')

        plt.subplot(121)
        plt.imshow(self.img, cmap=plt.get_cmap('gray'), vmin=0, vmax=255)
        plt.title('Immagine originale')

        fig = plt.gcf()
        fig.canvas.manager.window.wm_geometry("+%d+%d" % (0, 0))
        manager = plt.get_current_fig_manager()
        manager.resize(*manager.window.maxsize())

        self.progress.setValue(self.progress.value() + 5)

        plt.figure(2)
        sub_img = cv2.absdiff(self.img, i_img.astype(float))
        plt.imshow(sub_img, cmap=plt.get_cmap('gray'), vmin=0, vmax=255)
        plt.title('Immagine differenza')

        plt.show()
Beispiel #17
0
Datei: dct.py Projekt: nmheim/esn
def idct2_sequence(Ftkk, xsize):
    """Inverse Discrete Cosine Transform of a sequence of 2D images.

    Params
    ------
    Ftkk : ndarray with shape (time, nk1, nk2)
    size : (ny, nx) determines the resolution of the image

    Returns
    -------
    Ftxx: ndarray with shape (time, ny, nx)
    """
    Ftxx = idctn(Ftkk, norm='ortho', shape=xsize, axes=[1, 2])
    return Ftxx
Beispiel #18
0
def dct_compression(image, F, d):
    #compressed_image = image #copy to store the original image
    h = image.shape[0]
    print(h)
    w = image.shape[1]
    print(w)
    if (h % F != 0):
        h = int(h / F) * F
        print(h)
    if (w % F != 0):
        w = int(w / F) * F
        print(w)
    compressed_image = image[0:h, 0:w]
    print(h)
    print(w)
    # cycle the image in step of F
    for x in range(0, h, F):
        for y in range(0, w, F):
            cell = compressed_image[x:x + F, y:y +
                                    F]  # width of cell = F, height of cell = F
            #print("first cell:\n")
            #print(cell)
            cell = dctn(
                cell,
                norm='ortho')  # discrete cosine transform of the selected cell

            c_h = cell.shape[0]
            c_w = cell.shape[1]
            # delete the frequencies in the cell making reference to d parameter
            for i in range(0, c_h):
                for j in range(0, c_w):
                    if i + j >= d:
                        cell[i, j] = 0

            # compute the inverse dct of the cell
            cell = idctn(cell, norm='ortho')

            #round of ff at the nearest integer, put to 0 negative values, put to 255 bigger values
            for i in range(0, c_h):
                for j in range(0, c_w):
                    value = round(cell[i, j])
                    if value < 0:
                        value = 0
                    elif value > 255:
                        value = 255
                    cell[i, j] = value
            compressed_image[x:x + F, y:y + F] = cell

    return compressed_image
Beispiel #19
0
    def invert_process(array, table):
        row = 8
        col = 8

        (origin_row, origin_col) = np.shape(array)
        dct_array = np.zeros((origin_row, origin_col), dtype=float)
        for i in range(0, origin_row, row):
            for j in range(0, origin_col, col):
                sub_array = array[i:i + row, j:j + col]
                sub_array = sub_array / table
                dct_array[i:i + row, j:j + col] = idctn(sub_array,
                                                        norm='ortho')

        new_array = dct_array + 128
        return np.clip(new_array, a_min=0, a_max=255)
Beispiel #20
0
def idct2d(a):
    """
    Computes the 2D Normalized Inverse DCT-II.

    Arguments
    ---------
    A: (m, n) numpy array
        DCT coefficient matrix

    Returns
    -------
    (m, n) numpy array
        2D image.
    """
    return fp.idctn(a, norm='ortho')
Beispiel #21
0
    def reverse_process(self, stegano_blocks):
        """
        reversed process of the quantify and DCT transformation
        :param stegano_blocks:written quantified matrix
        :return:
        """

        iquant_blocks = [[block * quant_tbl for block in line]
                         for line in stegano_blocks]
        idct_blocks = [[
            np.array(fftpack.idctn(block), dtype=int) // 256 for block in line
        ] for line in iquant_blocks]
        stack_arr = np.array(np.vstack(
            [np.hstack(line) for line in idct_blocks]),
                             dtype=np.uint8)
        Image.fromarray(stack_arr).save('reversed_img.bmp')
Beispiel #22
0
def idct_2d(im: np.ndarray, ) -> np.ndarray:
    """Computes the Inverse Discrete Cosine Transform of an image

    Given an input image, this function computes its inverse discrete cosine
    transform. This result is identical to the result of the idct2() Matlab
    function. Internally, this function is computed using
    ``scipy.fftpack.idctn(im, norm='ortho')``.

    This function is essentially a wrapper for `scipy.fftpack.idctn`_,
    so more detailed documentation may be found there.

    :type im: ``numpy.ndarray``
    :param im: An image to be processed.
    :rtype: ``numpy.ndarray``
    :return: The inverse discrete cosine transform of the input image.

    .. note::
        This function wraps around functions from other packages. Reading
        these functions' documentations may be useful. See the **See also**
        section for more information.

    .. seealso::
        `scipy.fftpack.idctn`_
            Documentation of the dctn function from Scipy

    .. _scipy.fftpack.idctn: https://docs.scipy.org/doc/scipy/reference
        /generated/scipy.fftpack.idctn.html

    Examples:

    >>> import numpy as np
    >>> im_DCT = np.zeros((8, 8))
    >>> im_DCT[0, 2] = 1
    >>> np.set_printoptions(precision=2, suppress=True)
    >>> idct_2d(im_DCT)
    array([[ 0.16,  0.07, -0.07, -0.16, -0.16, -0.07,  0.07,  0.16],
           [ 0.16,  0.07, -0.07, -0.16, -0.16, -0.07,  0.07,  0.16],
           [ 0.16,  0.07, -0.07, -0.16, -0.16, -0.07,  0.07,  0.16],
           [ 0.16,  0.07, -0.07, -0.16, -0.16, -0.07,  0.07,  0.16],
           [ 0.16,  0.07, -0.07, -0.16, -0.16, -0.07,  0.07,  0.16],
           [ 0.16,  0.07, -0.07, -0.16, -0.16, -0.07,  0.07,  0.16],
           [ 0.16,  0.07, -0.07, -0.16, -0.16, -0.07,  0.07,  0.16],
           [ 0.16,  0.07, -0.07, -0.16, -0.16, -0.07,  0.07,  0.16]])

    """
    return fftpack.idctn(im, norm='ortho')
def inverse_partitioned(patches, T):

    # Set empty array for recovered set of patches image
    i_patches = np.zeros((prange, prange, 8, 8), dtype=np.float64)

    for i in range(prange):
        for j in range(prange):
            if T == 'c':
                i_patches[i, j] = idctn(patches[i, j],
                                        type=2,
                                        shape=[8, 8],
                                        norm='ortho')
            else:
                ifft = np.fft.ifft2(patches[i, j], s=[8, 8])
                i_patches[i, j] = [np.absolute(k) for k in ifft]

    return i_patches
def compress_image(image, window, cutoff, output=OUTPUT_COMPRESSED_IMAGE):
    gray = util.im2gray(image)

    cropped = util.crop(gray, window)
    shaped = util.blockshaped(cropped, window)

    dct = fft.dctn(shaped, axes=[1, 2], type=2, norm='ortho')
    compressed, mask = util.compress(dct, cutoff)
    idct = fft.idctn(compressed, axes=[1, 2], type=2, norm='ortho')

    info = np.iinfo(image.dtype)
    to_range = (info.min, info.max)
    
    if output == OUTPUT_DCT:
        out = dct
        from_range = (out.min(), out.max())
    elif output == OUTPUT_MASK:
        out = mask
        from_range = (out.min(), out.max())
    elif output == OUTPUT_COMPRESSED_DCT:
        out = compressed
        from_range = (out.min(), out.max())
    elif output == OUTPUT_COMPRESSED_IMAGE:
        out = idct
        from_range = to_range # No interpolation needed, just clipping
    else:
        raise ValueError("Invalid value for 'output' parameter.")

    # If min == max, min should map to 0
    if from_range[0] == from_range[1]:
        from_range = (from_range[0], from_range[1] + 1)
    
    normalized = np.interp(out, from_range, to_range)
    converted = np.round(normalized).astype(image.dtype)

    if output == OUTPUT_MASK:
        result = converted
    else:
        result = util.unblockshaped(converted, cropped.shape)

    return result
Beispiel #25
0
    def __init__(self, images, params, scale_images=True):
        self.params = params;
        self.train_length = params.train_length
        self.pred_length = params.pred_length
        self.nr_sequences = images.shape[0] - self.train_length - self.pred_length
        self.max = None
        self.min = None

        self.dtype = np.dtype(params.dtype)
        
        if "cycle_length" in params.dict:
            cycle_length    = params.cycle_length
            cycle_timescale = params.dict.get('cycle_timescale',1);            

            logger.info(f"Detrending and removing average length-{cycle_length} cycle")            
            
            Ftkk = dctn(images,norm='ortho',axes=[1,2]);

            if cycle_timescale == 1:
                (ftkk,bkk,Ckk) = detrend.separate_trends_unscaled(Ftkk,cycle_length);
            else:
                nT = int((cycle_timescale*Ftkk.shape[0]).round());
                (ftkk,bkk,Ckk) = detrend.separate_trends_scaled(Ftkk,nT,cycle_length);
                
            ftxx = idctn(ftkk,norm='ortho',axes=[1,2]);

            self.quadratic_trend = bkk;
            self.mean_cycle      = Ckk;
            self.cycle_timescale = cycle_timescale;
            self.cycle_lengtth   = cycle_length;            
            
            self.detrend_training_data = params.dict.get('detrend_training_data',False);

            if self.detrend_training_data:
                images = ftxx;
            
        if scale_images:
            logger.debug("Scaling input images to (-1, 1)")
            images = self.scale(images)
        self._images = images.astype(self.dtype)
        self.image_shape = images.shape[1:]
Beispiel #26
0
    def compress_image(self):
        source = self.originalImage.pixmap.toImage()
        width, height = [source.width(), source.height()]

        # bits() -> deep copy // constBits() -> references
        values = source.constBits()
        values.setsize(height * width * 4)

        pixels = np.frombuffer(values, np.uint8).reshape(
            (height, width, 4)).copy()
        pixels = pixels[:, :, 0].astype(np.float)

        F = self.fSlider.value()
        d = self.dSlider.value()

        height_blocks_count = int(height / F)
        width_blocks_count = int(width / F)

        for r in range(height_blocks_count):
            for c in range(width_blocks_count):
                block = dctn(pixels[r * F:(r + 1) * F, c * F:(c + 1) * F],
                             norm='ortho')

                for k in range(F):
                    for l in range(F):
                        if k + l >= d:
                            block[k, l] = 0

                pixels[r * F:(r + 1) * F,
                       c * F:(c + 1) * F] = idctn(block, norm='ortho')

        pixels = pixels.clip(0, 255).round().astype(np.uint8)

        target = QImage(bytes(pixels), pixels.shape[1], pixels.shape[0],
                        int(pixels.nbytes / height), QImage.Format_Grayscale8)
        self.compressedImage.setPixmap(QPixmap.fromImage(target))
Beispiel #27
0
def decode_dct(orig, bx, by):
    return sfft.idctn(orig, axes=[1,3], norm='ortho'
    ).reshape((
        orig.shape[0]*bx,
        orig.shape[2]*by
    ))
Beispiel #28
0
def test_all(net, datasets, args):
    batch_size = {'val': args['batch_size']}
    data_loader = {
        phase: uData.DataLoader(datasets[phase],
                                batch_size=batch_size[phase],
                                shuffle=False,
                                num_workers=args['num_workers'],
                                pin_memory=True)
        for phase in _modes
    }
    num_data = {phase: len(datasets[phase]) for phase in _modes}
    num_iter_epoch = {
        phase: ceil(num_data[phase] / batch_size[phase])
        for phase in _modes
    }
    step_img = args['step_img'] if False else {x: 0 for x in _modes}
    writer = SummaryWriter(str(Path(args['log_dir'])))

    mae_epoch = {'val': 0}

    # test stage
    tic = time.time()
    net['D'].eval()
    psnr_per_epoch = ssim_per_epoch = 0
    phase = 'val'
    for ii, data in enumerate(data_loader[phase]):
        im_noisy, im_gt = [x.cuda() for x in data]
        with torch.set_grad_enabled(False):
            im_denoise = im_noisy - net['D'](im_noisy)

        mae_iter = F.l1_loss(im_denoise, im_gt)
        # im_denoise.clamp_(0.0, 1.0) ####
        ########################################
        # HU_min = -160
        # HU_range = 400
        # im_denoise_ = (im_denoise - HU_min)/HU_range
        # im_gt_ = (im_gt - HU_min)/HU_range
        # im_denoise_.clamp_(0.0, 1.0)
        # im_gt_.clamp_(0.0, 1.0)
        # # # # # # # # # # # # # # # #
        im_gt_np = im_gt.cpu().numpy()
        im_denoise_np = im_denoise.cpu().numpy()
        im_noisy_np = im_noisy.cpu().numpy()
        im_gt = torch.from_numpy(fftpack.idctn(im_gt_np, axes=(3, 2))) / 4
        im_denoise = torch.from_numpy(fftpack.idctn(im_denoise_np,
                                                    axes=(3, 2))) / 4
        im_noisy = torch.from_numpy(fftpack.idctn(im_noisy_np,
                                                  axes=(3, 2))) / 4
        # # # # # # # # # # # # # # # #
        im_gt_ = from4kto400(im_gt)
        im_denoise_ = from4kto400(im_denoise)
        im_noisy_ = from4kto400(im_noisy)
        ########################################
        mae_epoch[phase] += mae_iter
        psnr_iter = batch_PSNR(im_denoise_, im_gt_)
        psnr_per_epoch += psnr_iter
        ssim_iter = batch_SSIM(im_denoise_, im_gt_)
        ssim_per_epoch += ssim_iter
        # print statistics every log_interval mini_batches
        if (ii + 1) % 25 == 0:
            log_str = 'val:{:0>3d}/{:0>3d}, mae={:.2e}, psnr={:4.2f}, ssim={:5.4f}'
            print(
                log_str.format(ii + 1, num_iter_epoch[phase], mae_iter,
                               psnr_iter, ssim_iter))
            # tensorboard summary
            x1 = vutils.make_grid(im_denoise[:, 1, :, :].unsqueeze(1).repeat(
                1, 3, 1, 1),
                                  normalize=True,
                                  scale_each=True)
            writer.add_image(phase + ' Denoised images', x1, step_img[phase])
            x2 = vutils.make_grid(im_gt[:, 1, :, :].unsqueeze(1).repeat(
                1, 3, 1, 1),
                                  normalize=True,
                                  scale_each=True)
            writer.add_image(phase + ' GroundTruth', x2, step_img[phase])
            x3 = vutils.make_grid(im_gt_[:, 1, :, :].unsqueeze(1).repeat(
                1, 3, 1, 1),
                                  normalize=True,
                                  scale_each=False,
                                  range=(0, 1))
            writer.add_image(phase + ' GroundTruth but must be [-160, 240]',
                             x3, step_img[phase])
            x4 = vutils.make_grid(im_gt_[:, 1, :, :].unsqueeze(1).repeat(
                1, 3, 1, 1),
                                  normalize=True,
                                  scale_each=False,
                                  range=(.25, .75))
            writer.add_image(phase + ' GroundTruth test on [-60, 140]', x4,
                             step_img[phase])
            x5 = vutils.make_grid(im_noisy[:, 1, :, :].unsqueeze(1).repeat(
                1, 3, 1, 1),
                                  normalize=True,
                                  scale_each=True)
            writer.add_image(phase + ' Noisy Image', x5, step_img[phase])
            x6 = vutils.make_grid(im_noisy_,
                                  normalize=False,
                                  scale_each=False,
                                  range=(0, 1))
            writer.add_image(phase + ' Noisy Image 3 slices, [-160, 240]', x6,
                             step_img[phase])
            x7 = vutils.make_grid(
                im_denoise_[:, 1, :, :].unsqueeze(1).repeat(1, 3, 1, 1) -
                im_gt_,
                normalize=False,
                scale_each=False,
                range=(-.5, .5))
            writer.add_image(phase + ' difference 3 slices (4k->400)', x7,
                             step_img[phase])
            x8 = vutils.make_grid(
                im_noisy_[:, 1, :, :].unsqueeze(1).repeat(1, 3, 1, 1) - im_gt_,
                normalize=False,
                scale_each=False,
                range=(-.5, .5))
            writer.add_image(phase + ' gt noise diff (4k->400)', x8,
                             step_img[phase])
            x9 = vutils.make_grid(
                im_noisy[:, 1, :, :].unsqueeze(1).repeat(1, 3, 1, 1) - im_gt,
                normalize=False,
                scale_each=False,
                range=(-.5, .5))
            writer.add_image(phase + ' gt noise diff (0,1 as it is)', x9,
                             step_img[phase])
            x10 = vutils.make_grid(
                im_noisy[:, 1, :, :].unsqueeze(1).repeat(1, 3, 1, 1) - im_gt,
                normalize=True,
                scale_each=False,
                range=(-.5, .5))
            writer.add_image(phase + ' same thing but norm=True', x10,
                             step_img[phase])
            step_img[phase] += 1

    psnr_per_epoch /= (ii + 1)
    ssim_per_epoch /= (ii + 1)
    mae_epoch[phase] /= (ii + 1)
    print('mae={:.3e}, PSNR={:4.2f}, SSIM={:5.4f}'.format(
        mae_epoch[phase], psnr_per_epoch, ssim_per_epoch))
    print('-' * 150)

    toc = time.time()
    print('This test take time {:.2f}'.format(toc - tic))
    writer.close()
    print('Test done!')
def decode_dct(orig):
    return fftpack.idctn(orig, axes=[1, 3], norm='ortho').reshape(
        (orig.shape[0] * 16, orig.shape[2] * 16, 3))
Beispiel #30
0
def A_opr_blur(im, h):
    return A_opr(idctn(im.astype(float), norm='ortho'), h)