Esempio n. 1
0
        #print 'loading', f
        im = np.array(Image.open(f))
        im = utils.modcrop(im, UP_SCALE).astype(np.float32)
        im_gt += [im]

    im_l = []
    if len(IMAGE_FILE) > 0:
        assert (len(im_gt) == 1)
        im_l = [np.array(Image.open(IMAGE_FILE)).astype(np.float32)]
    else:  #down scale from ground truth using Matlab
        try:
            from pymatbridge import Matlab
            mlab = Matlab()
            mlab.start()
            for im in im_gt:
                mlab.set_variable('a', im)
                mlab.set_variable('s', 1.0 / UP_SCALE)
                mlab.run_code('b=imresize(a, s);')
                im_l += [mlab.get_variable('b')]
            mlab.stop()
        except:
            print 'failed to load Matlab!'
            assert (0)
            #im_l = utils.imresize(im_gt, 1.0/UP_SCALE)

    #upscaling
    #sr = Bicubic()
    sr = SCN(MODEL_FILE)
    res_all = []
    for i in range(len(im_l)):
        t = time.time()
Esempio n. 2
0
         ('constant', 'sp0'),
         ('symmetric', 'sym'),
         ('periodic', 'ppd'),
         ('smooth', 'sp1'),
         ('periodization', 'per')]

families = ('db', 'sym', 'coif', 'bior', 'rbio')
wavelets = sum([pywt.wavelist(name) for name in families], [])

rstate = np.random.RandomState(1234)
mlab.start()
try:
    all_matlab_results = {}
    for wavelet in wavelets:
        w = pywt.Wavelet(wavelet)
        mlab.set_variable('wavelet', wavelet)
        if size_set == 'full':
            data_sizes = list(range(w.dec_len, 40)) + \
                [100, 200, 500, 1000, 50000]
        else:
            data_sizes = (w.dec_len, w.dec_len + 1)
        for N in data_sizes:
            data = rstate.randn(N)
            mlab.set_variable('data', data)
            for pmode, mmode in modes:
                # Matlab result
                mlab_code = ("[ma, md] = dwt(data, wavelet, "
                             "'mode', '%s');" % mmode)
                res = mlab.run_code(mlab_code)
                if not res['success']:
                    raise RuntimeError(
eigvecs *= np.sign(eigvecs[np.newaxis, 0])
E = E * np.sign(E[np.newaxis, 0])

np.allclose(eigvecs, E)

# The second problem is that eigenvectors corresponding to the very small eigenvalues may differ a lot.

threshold = 1e-9
abs_threshold = max(eigvals) * threshold
np.allclose(eigvecs[:, eigvals > abs_threshold],
            E[:, D.squeeze() > abs_threshold])

# So, the results are equivalent. Let's check that both functions produce the same output if we ask them to remove eigenvalues below a given threshold.

matlab.set_variable(value=threshold, varname='threshold')
matlab.run_code('[E, D] = nt_pcarot(c0, [], threshold);')
E = matlab.get_variable('E')
D = matlab.get_variable('D')
eigvals, eigvecs = dss.covariance_pca(R0, threshold=threshold)

np.allclose(eigvals, D)

eigvecs *= np.sign(eigvecs[np.newaxis, 0])
E = E * np.sign(E[np.newaxis, 0])
np.allclose(eigvecs, E)

# # DSS from covariance matrices

# Here is how the dss is run in the example:
Esempio n. 4
0
# list of mode names in pywt and matlab
modes = [('zero', 'zpd'), ('constant', 'sp0'), ('symmetric', 'sym'),
         ('periodic', 'ppd'), ('smooth', 'sp1'), ('periodization', 'per')]

families = ('gaus', 'mexh', 'morl', 'cgau', 'shan', 'fbsp', 'cmor')
wavelets = sum([pywt.wavelist(name) for name in families], [])

rstate = np.random.RandomState(1234)
mlab.start()
try:
    all_matlab_results = {}
    for wavelet in wavelets:
        w = pywt.ContinuousWavelet(wavelet)
        if np.any((wavelet == np.array(['shan', 'cmor'])), axis=0):
            mlab.set_variable(
                'wavelet', wavelet + str(w.bandwidth_frequency) + '-' +
                str(w.center_frequency))
        elif wavelet == 'fbsp':
            mlab.set_variable(
                'wavelet', wavelet + str(w.fbsp_order) + '-' +
                str(w.bandwidth_frequency) + '-' + str(w.center_frequency))
        else:
            mlab.set_variable('wavelet', wavelet)
        if size_set == 'full':
            data_sizes = list(range(100, 101)) + \
                [100, 200, 500, 1000, 50000]
            Scales = (1, np.arange(1, 3), np.arange(1, 4), np.arange(1, 5))
        else:
            data_sizes = (1000, 1000 + 1)
            Scales = (1, np.arange(1, 3))
        mlab_code = ("psi = wavefun(wavelet,10)")
Esempio n. 5
0
         ('constant', 'sp0'),
         ('symmetric', 'sym'),
         ('periodic', 'ppd'),
         ('smooth', 'sp1'),
         ('periodization', 'per')]

families = ('db', 'sym', 'coif', 'bior', 'rbio')
wavelets = sum([pywt.wavelist(name) for name in families], [])

rstate = np.random.RandomState(1234)
mlab.start()
try:
    all_matlab_results = {}
    for wavelet in wavelets:
        w = pywt.Wavelet(wavelet)
        mlab.set_variable('wavelet', wavelet)
        if size_set == 'full':
            data_sizes = list(range(w.dec_len, 40)) + \
                [100, 200, 500, 1000, 50000]
        else:
            data_sizes = (w.dec_len, w.dec_len + 1)
        for N in data_sizes:
            data = rstate.randn(N)
            mlab.set_variable('data', data)
            for pmode, mmode in modes:
                # Matlab result
                if np.any((wavelet == np.array(['coif6', 'coif7', 'coif8', 'coif9', 'coif10', 'coif11', 'coif12', 'coif13', 'coif14', 'coif15', 'coif16', 'coif17'])),axis=0):
                    mlab.set_variable('Lo_D', w.dec_lo)
                    mlab.set_variable('Hi_D', w.dec_hi)
                    mlab_code = ("[ma, md] = dwt(data, Lo_D, Hi_D, "
                                 "'mode', '%s');" % mmode)
Esempio n. 6
0
        #print 'loading', f
        im = np.array(Image.open(f))
        im = utils.modcrop(im, UP_SCALE).astype(np.float32)
        im_gt += [im]

    im_l = []
    if len(IMAGE_FILE)>0:
        assert(len(im_gt)==1)
        im_l = [np.array(Image.open(IMAGE_FILE)).astype(np.float32)]
    else: #down scale from ground truth using Matlab
        try:
            from pymatbridge import Matlab
            mlab = Matlab()
            mlab.start()
            for im in im_gt:
                mlab.set_variable('a', im)
                mlab.set_variable('s', 1.0/UP_SCALE)
                mlab.run_code('b=imresize(a, s);')
                im_l += [mlab.get_variable('b')]
            mlab.stop()
        except:
            print 'failed to load Matlab!'
            assert(0)
            #im_l = utils.imresize(im_gt, 1.0/UP_SCALE)

    #upscaling
    #sr = Bicubic()
    sr = SCN(MODEL_FILE)
    res_all = []
    for i in range(len(im_l)):
        t=time.time();
Esempio n. 7
0
size_set = 'reduced'

# list of mode names in pywt and matlab
modes = [('zpd', 'zpd'), ('cpd', 'sp0'), ('sym', 'sym'), ('ppd', 'ppd'),
         ('sp1', 'sp1'), ('per', 'per')]

families = ('db', 'sym', 'coif', 'bior', 'rbio')
wavelets = sum([pywt.wavelist(name) for name in families], [])

rstate = np.random.RandomState(1234)
mlab.start()
try:
    all_matlab_results = {}
    for wavelet in wavelets:
        w = pywt.Wavelet(wavelet)
        mlab.set_variable('wavelet', wavelet)
        if size_set == 'full':
            data_sizes = list(range(w.dec_len, 40)) + \
                [100, 200, 500, 1000, 50000]
        else:
            data_sizes = (w.dec_len, w.dec_len + 1)
        for N in data_sizes:
            data = rstate.randn(N)
            mlab.set_variable('data', data)
            for pmode, mmode in modes:
                # Matlab result
                mlab_code = ("[ma, md] = dwt(data, wavelet, "
                             "'mode', '%s');" % mmode)
                res = mlab.run_code(mlab_code)
                if not res['success']:
                    raise RuntimeError(
         ('symmetric', 'sym'),
         ('periodic', 'ppd'),
         ('smooth', 'sp1'),
         ('periodization', 'per')]

families = ('gaus', 'mexh', 'morl', 'cgau', 'shan', 'fbsp', 'cmor')
wavelets = sum([pywt.wavelist(name) for name in families], [])

rstate = np.random.RandomState(1234)
mlab.start()
try:
    all_matlab_results = {}
    for wavelet in wavelets:
        w = pywt.Wavelet(wavelet)
        if np.any((wavelet == np.array(['shan', 'cmor'])),axis=0):
            mlab.set_variable('wavelet', wavelet+str(w.bandwidth_frequency)+'-'+str(w.center_frequency))
        elif wavelet == 'fbsp':
            mlab.set_variable('wavelet', wavelet+str(w.fbsp_order)+'-'+str(w.bandwidth_frequency)+'-'+str(w.center_frequency))
        else:
            mlab.set_variable('wavelet', wavelet)
        if size_set == 'full':
            data_sizes = list(range(100, 101)) + \
                [100, 200, 500, 1000, 50000]
            Scales = (1,np.arange(1,3),np.arange(1,4),np.arange(1,5))
        else:
            data_sizes = (1000, 1000 + 1)
            Scales = (1,np.arange(1,3))
        mlab_code = ("psi = wavefun(wavelet,10)")
        res = mlab.run_code(mlab_code)
        if not res['success']:
            raise RuntimeError(
Esempio n. 9
0
class MatlabBridgeDriver(MatlabDriver):
    """MATLAB driver which uses pymatbridge to do IPC with MATLAB."""

    # TODO(andrei): Consider reusing MATLAB instances across iterations by
    # using process-level locals, if something like that exists.

    def __init__(self):
        super().__init__()
        self.matlab = Matlab()

        # As of July 2016, there seems to be a bug which wrecks the data
        # dimensionality when feeding it to MATLAB, causing a matrix dimension
        # mismatch to happen.
        raise ValueError("MATLAB interop via pymatbridge doesn't work.")

    def start(self):
        """Starts MATLAB so that we may send commands to it.

        Blocks until MATLAB is started and a ZMQ connection to it is
        established.

        This is a very sensitive piece of code which can fail due to numerous
        misconfigurations. For instance, on ETH's Euler cluster, one must ensure
        that the proper modules are loaded before starting MATLAB, and that
        the MATLAB one is the first one loaded because of PATH concerns.

        Getting this to run might not be straightforward, and may require
        installing 'libzmq', 'pyzmq', and 'pymatbridge' from scratch on Euler.

        The process has not been tested on regular commodity hardware, such as
        AWS, but it should be much easier to run there due to the increased
        access to installing new packages directly via a package manager.

        TODO(andrei): Write guide for this.
        TODO(andrei): Maybe have a retry mechanic in case something fails.
        """
        super().start()
        self.matlab.start()
        self.matlab.run_code(r'''addpath(genpath('./matlab'))''')

    def _run_matlab_script(self, script, in_map):
        super()._run_matlab_script(script, in_map)

        start_ms = int(time.time() * 1000)

        logging.info("Have %d variables to set.", len(in_map))
        for vn, v in in_map.items():
            self.matlab.set_variable(vn, v)
        logging.info("Set all variables OK.")

        mlab_res = self.matlab.run_code('rungp_fn')
        print(mlab_res)

        if not mlab_res['success']:
            raise RuntimeError("Could not run MATLAB. Got error message: {0}"
                               .format(mlab_res['content']))

        result = self.matlab.get_variable('prob')
        print(result)

        # self.matlab.run_func('matlab/rungp_fn.m',
        #                      in_map['X'],
        #                      in_map['y'],
        #                      in_map['X_test'])

        # script_cmd = '{0} ; '.format(script)
        # self.matlab.run_code(script_cmd)
        end_ms = int(time.time() * 1000)
        time_ms = end_ms - start_ms
        logging.info("Ran MATLAB code using pymatbridge in %dms.", time_ms)

        # Dirty trick for testing
        # exit(-1)

        return result[:, 0]