Пример #1
0
def test_view_as_windows_With_skip():
    A = np.arange(20).reshape((5, 4))
    B = view_as_windows(A, (2, 2), step=2)
    assert_equal(B, [[[[0, 1], [4, 5]], [[2, 3], [6, 7]]], [[[8, 9], [12, 13]], [[10, 11], [14, 15]]]])

    C = view_as_windows(A, (2, 2), step=4)
    assert_equal(C.shape, (1, 1, 2, 2))
Пример #2
0
def test_view_as_windows_optimal_step():
    A = np.arange(24).reshape((6, 4))
    B = view_as_windows(A, (3, 2), optimal_step=True)
    assert B.shape == (2, 2, 3, 2)
    assert B.size == A.size

    A = np.arange(512 * 512).reshape((512, 512))
    B = view_as_windows(A, (10, 10), optimal_step=True)
    assert B.shape == (56, 56, 10, 10)
    assert B.size >= A.size

    C = view_as_windows(A, (11, 9), optimal_step=True)
    assert C.shape == (51, 63, 11, 9)
    assert C.size >= A.size
Пример #3
0
    def perform(self, node, inputs, output_storage):
        features, kernel = inputs
        strides = self.strides

        featshp = features.shape
        kshp = kernel.shape

        produced_output_sz = (featshp[0], kshp[1], kshp[2] + strides[0] * (featshp[2] - 1), kshp[3] + strides[1] * (featshp[3] - 1))
        returned_output_sz = (featshp[0], kshp[1], self.imshp[2], self.imshp[3])

        k_rot = kernel[:,:,::-1,::-1]

        scipy_output = np.zeros(returned_output_sz,dtype=node.out.dtype)
        for im_i in range(featshp[0]):

            im_out = np.zeros(produced_output_sz[1:],dtype=node.out.dtype)
            im_outr = view_as_windows(im_out,(kshp[1],kshp[2],kshp[3]))[0,::strides[0],::strides[1],...]

            im_hatr = np.tensordot(features[im_i,...],k_rot,axes=((0,),(0,)))

            for a in range(im_hatr.shape[0]):
                for b in range(im_hatr.shape[1]):
                    im_outr[a,b,...] += im_hatr[a,b,...]

            if produced_output_sz[2] <= returned_output_sz[2]:
                scipy_output[im_i,:,:im_out.shape[1],:im_out.shape[2]] = im_out
            else:
                scipy_output[im_i,:,:,:] = im_out[:,:returned_output_sz[2],:returned_output_sz[3]]

        #print 'MyCorr, output.shape:', scipy_output.shape, 'strides', strides, 'featshp', featshp, 'kshp', kshp, 'imshp', self.imshp,\
        #'produced_output_sz', produced_output_sz, 'returned_output_sz', returned_output_sz, \
        #'im_outr.shape', im_outr.shape, 'im_hatr.shape', im_hatr.shape

        output_storage[0][0] = scipy_output
Пример #4
0
    def perform(self, node, inputs, output_storage):
        image_error, features = inputs
        kshp = self.kshp
        imshp = image_error.shape
        strides = self.strides

#        scipy_error_rot = np.transpose(image_error, [1, 0, 2, 3])
#        features_rot = np.transpose(features, [1, 0, 2, 3])
#
#        feat_expand = np.zeros((features_rot.shape[0],
#                                features_rot.shape[1],
#                                image_error.shape[2] - self.kshp[2] + 1,
#                                image_error.shape[3] - self.kshp[3] + 1), dtype=features.dtype)
#        feat_expand[:, :, ::strides[0], ::strides[1]] = features_rot
#
#        #scipy_derivative_rot = -scipy_convolve4d(scipy_error_rot[:, :, ::-1, ::-1], feat_expand)
#
#        feat_flipped = features_rot[:, :, ::-1, ::-1]

        from skimage.util.shape import view_as_windows

        image_error_view = view_as_windows(image_error, (imshp[0], kshp[1], kshp[2], kshp[3]))[0,0,::strides[0],::strides[1],...]
        # image_error_view.shape = (featszr, featszc, num_im, channels, ksz[2], ksz[3])
        # features.shape = (num_im, num_filters, featszr, featszc)
        kernel_derivative = - np.tensordot(features,image_error_view, axes=((0, 2, 3), (2, 0, 1)))
        # kernel_derivative_temp.shape = (num_filters, channels, ksz[2], ksz[3])

        output_storage[0][0] = kernel_derivative[:,:,::-1,::-1]
Пример #5
0
    def get_pooled_features(self, input_feature_map, filter_size=(19,19)):
        # assuming square filters and images
        filter_side = filter_size[0]

        # reshaping incoming features from 2d to 3d i.e. (3249,20) to (57,57,20)
        input_feature_map_shape = input_feature_map.shape
        if input_feature_map.ndim == 2:
            input_feature_map_side = int(np.sqrt(input_feature_map.shape[0]))
            input_feature_map = input_feature_map.reshape((input_feature_map_side, input_feature_map_side, input_feature_map_shape[-1]))
        assert input_feature_map.ndim == 3, "Input features dimension is %d instead of 3" %input_feature_map.ndim

        # get windows (57,57,20) to (3,3,1,19,19,20)
        input_feature_map_windows = view_as_windows(input_feature_map,
                                                    window_shape=(filter_size[0], filter_size[1], input_feature_map.shape[-1]),
                                                    step=filter_size[0])

        # reshape windows (3,3,1,19,19,20) to (3**2, 19**2, 20) == (9, 361, 20)
        input_feature_map_windows = input_feature_map_windows.reshape((input_feature_map_windows.shape[0]**2,
                                                                       filter_size[0]**2,
                                                                       input_feature_map.shape[-1]))

        # calculate norms (9, 361, 20) to (9,361)
        input_feature_map_window_norms = np.linalg.norm(input_feature_map_windows, ord=2, axis=-1)

        # calculate indexes of max norms per window (9,361) to (9,1). One max index per window.
        max_norm_indexes = np.argmax(input_feature_map_window_norms, axis=-1)

        # max pooled features are the features that have max norm indexes (9, 361, 20) to (9,20). One max index per window.
        pooled_features = input_feature_map_windows[np.arange(input_feature_map_windows.shape[0]), max_norm_indexes]

        # return pooled feature map
        return pooled_features
    def stochastic_pooling(self, X, n_layer):

        inh = X.shape[0] - self.shape_pool[0] + 1
        inw = X.shape[1] - self.shape_pool[1] + 1
        n_filter = self.n_filters[n_layer]
        filtersize = self.shape_pool[0] * self.shape_pool[1]

        randomsamples = random_sample((inh) * (inw) * n_filter).reshape(
            (inh), (inw), n_filter
        )  # generate random values
        randomsamples = np.repeat(randomsamples, repeats=filtersize, axis=2).reshape((inh), (inw), n_filter, filtersize)

        X_rfi = view_as_windows(X, self.shape_pool + (1,))
        sumpool = np.repeat(np.sum(X_rfi, axis=(3, 4, 5)), repeats=filtersize).reshape(
            (inh, inw, n_filter, self.shape_pool[0], self.shape_pool[1], 1)
        )
        probabilities = X_rfi / sumpool
        probabilities[np.isnan(probabilities)] = 1 / float(
            filtersize
        )  # get where the sum is zero and replace by one, so the division by zero error do not occur
        probabilities = probabilities.reshape((inh, inw, n_filter, filtersize))

        if self.training:

            bins = np.add.accumulate(probabilities, axis=3)
            binsbefore = np.concatenate((np.zeros((inh, inw, n_filter, 1)), bins[:, :, :, :-1]), axis=3)
            ret = X_rfi[np.where((((binsbefore <= randomsamples) * (bins > randomsamples))) == True)]
            ret = ret.reshape(((inh), (inw), n_filter))[:: self.stride_pool, :: self.stride_pool]

        else:  # for testing
            ret = probabilities * X_rfi
            sumpool[sumpool == 0.0] = 1.0
            ret = np.sum(ret, axis=(3)) / sumpool[:, :, :, 0, 0, 0]
            ret = ret[:: self.stride_pool, :: self.stride_pool]
Пример #7
0
def lpool4(arr_in, neighborhood,
           order=DEFAULT_ORDER,
           stride=DEFAULT_STRIDE, arr_out=None):
    """4D Local Pooling Operation

    XXX: docstring
    """
    assert arr_in.ndim == 4
    assert len(neighborhood) == 2

    order = np.array([order], dtype=arr_in.dtype)
    stride = np.int(stride)

    in_imgs, inh, inw, ind = arr_in.shape
    nbh, nbw = neighborhood
    assert nbh <= inh
    assert nbw <= inw

    if arr_out is not None:
        assert arr_out.dtype == arr_in.dtype
        assert arr_out.shape == (in_imgs,
                                 1 + (inh - nbh) / stride,
                                 1 + (inw - nbw) / stride,
                                 ind)

    _arr_out = ne.evaluate('arr_in ** order')
    _arr_out = view_as_windows(_arr_out, (1, 1, nbw, 1))
    _arr_out = ne.evaluate('sum(_arr_out, 6)')[:, :, ::stride, :, 0, 0, 0]
    # np.ascontiguousarray(_arr_out) necessary to avoid
    # skimage/util/shape.py:237: RuntimeWarning: Cannot provide views on a non-contiguous input array without copying.
    # warn(RuntimeWarning("Cannot provide views on a non-contiguous input 
    _arr_out = np.ascontiguousarray(_arr_out)
    _arr_out = view_as_windows(_arr_out, (1, nbh, 1, 1))
    _arr_out = ne.evaluate('sum(_arr_out, 5)')[:, ::stride, :, :, 0, 0, 0]

    _arr_out = ne.evaluate('_arr_out ** (1 / order)')

    if arr_out is not None:
        arr_out[:] = _arr_out
    else:
        arr_out = _arr_out

    assert arr_out.shape[0] == in_imgs

    assert arr_out.dtype == arr_in.dtype

    return arr_out
    def similarity3D(self, X, fb):
        assert X.ndim == 3
        assert fb.ndim == 4
        assert X.shape[-1] == fb.shape[2]

        Xw = view_as_windows(X, fb.shape[:3])
        Xwa = np.abs(Xw - fb)
        return Xwa.sum(axis=(3, 4, 5))
Пример #9
0
    def _process_one_op(self, arr, X, Y,
                        layer_idx, op_idx, kwargs,
                        op_params,
                        op_name, nbh, nbw, stride,
                        pad_apron=False,
                        interleave_stride=False):

        out_l = []

        # -- here we compute the pixel coordinates of
        #    the central pixel in a patch
        hc, wc = nbh / 2, nbw / 2

        if pad_apron:

            arr = filter_pad2d(arr, (nbh, nbw))
            X = np.squeeze(filter_pad2d(X[..., np.newaxis], (nbh, nbw),
                                        constant=-1))
            Y = np.squeeze(filter_pad2d(Y[..., np.newaxis], (nbh, nbw),
                                        constant=-1))

        if interleave_stride:

            for i in xrange(stride):
                for j in xrange(stride):

                    arr_out_ij = self._get_feature_map(arr[i::, j::, ...],
                                                  layer_idx, op_idx, kwargs,
                                                  op_params, op_name)
                    X_out_ij = view_as_windows(X[i::, j::],
                                               (nbh, nbw))[::stride, ::stride,
                                                           hc, wc]
                    Y_out_ij = view_as_windows(Y[i::, j::],
                                               (nbh, nbw))[::stride, ::stride,
                                                           hc, wc]
                    out_l += [(arr_out_ij, X_out_ij, Y_out_ij)]
        else:

            arr_out = self._get_feature_map(arr, layer_idx, op_idx, kwargs,
                                            op_params, op_name)
            X_out = view_as_windows(X, (nbh, nbw))[::stride, ::stride, hc, wc]
            Y_out = view_as_windows(Y, (nbh, nbw))[::stride, ::stride, hc, wc]
            out_l += [(arr_out, X_out, Y_out)]

        return out_l
def mcconv3(X, W):
    X_VAW = view_as_windows(X, W.shape[0:-1])
    Y_FPS = X_VAW.shape[0:2]
    X_VAW = X_VAW.reshape(Y_FPS[0] * Y_FPS[1], -1)
    W = W.reshape(-1, W.shape[-1])
    Y = np.dot(X_VAW, W)
    Y = Y.reshape(Y_FPS[0], Y_FPS[1], -1)

    return Y
def bxfilt2(X, F_SIZ, F_STRD):
    for i in reversed(xrange(2)):
        W_SIZ = np.ones(np.ndim(X))
        S_SIZ = np.ones(2)
        W_SIZ[i], S_SIZ[i] = F_SIZ, F_STRD
        X = np.squeeze(view_as_windows(X, tuple(W_SIZ)))[::S_SIZ[0], ::S_SIZ[1]]  # subsampling before summation
        X = np.sum(X, -1)

    return X
Пример #12
0
def lpool4(arr_in, neighborhood,
           order=DEFAULT_ORDER,
           stride=DEFAULT_STRIDE, arr_out=None):
    """4D Local Pooling Operation

    XXX: docstring
    """
    assert arr_in.ndim == 4
    assert len(neighborhood) == 2

    order = np.array([order], dtype=arr_in.dtype)
    stride = np.int(stride)

    in_imgs, inh, inw, ind = arr_in.shape
    nbh, nbw = neighborhood
    assert nbh <= inh
    assert nbw <= inw

    if arr_out is not None:
        assert arr_out.dtype == arr_in.dtype
        assert arr_out.shape == (in_imgs,
                                 1 + (inh - nbh) / stride,
                                 1 + (inw - nbw) / stride,
                                 ind)

    _arr_out = ne.evaluate('arr_in ** order')
    _arr_out = view_as_windows(_arr_out, (1, 1, nbw, 1))
    _arr_out = ne.evaluate('sum(_arr_out, 6)')[:, :, ::stride, :, 0, 0, 0]
    _arr_out = view_as_windows(_arr_out, (1, nbh, 1, 1))
    _arr_out = ne.evaluate('sum(_arr_out, 5)')[:, ::stride, :, :, 0, 0, 0]

    _arr_out = ne.evaluate('_arr_out ** (1 / order)')

    if arr_out is not None:
        arr_out[:] = _arr_out
    else:
        arr_out = _arr_out

    assert arr_out.shape[0] == in_imgs

    assert arr_out.dtype == arr_in.dtype

    return arr_out
Пример #13
0
def extractPatches(name, patch_shape, dataDir, vector, numberOfPatchesPerImage):
    imageName = '{0:s}{1:s}'.format(dataDir, name)
    npImage = cv2.imread(imageName)
    npImage = np.divide(npImage, vector)
    #npImage = cv2.resize(npImage, (65, 65))
    patchesSampled = np.empty(shape=(numberOfPatchesPerImage, patch_shape[0] * patch_shape[1], 3))
    for derp in range(npImage.shape[2]):
        patches = view_as_windows(npImage[:,:,derp], patch_shape)
        patches = patches.reshape(-1, patch_shape[0] * patch_shape[1])[::8]
        patchesSampled[:,:,derp] = patches[np.random.random_integers(0, patches.shape[0], numberOfPatchesPerImage), :]
    return(patchesSampled)
Пример #14
0
def lpool3(arr_in, neighborhood,
           order=DEFAULT_ORDER,
           stride=DEFAULT_STRIDE, arr_out=None):
    """3D Local Pooling Operation

    XXX: docstring
    """
    assert arr_in.ndim == 3
    assert len(neighborhood) == 2

    order = np.array([order], dtype=arr_in.dtype)
    stride = np.int(stride)

    inh, inw, ind = arr_in.shape
    nbh, nbw = neighborhood
    assert nbh <= inh
    assert nbw <= inw

    if arr_out is not None:
        assert arr_out.dtype == arr_in.dtype
        assert arr_out.shape == (1 + (inh - nbh) / stride,
                                 1 + (inw - nbw) / stride,
                                 ind)

    _arr_out = ne.evaluate('arr_in ** order')
    _arr_out = view_as_windows(_arr_out, (1, nbw, 1))
    _arr_out = ne.evaluate('sum(_arr_out, 4)')[:, ::stride, :, 0, 0]
    _arr_out = view_as_windows(_arr_out, (nbh, 1, 1))
    _arr_out = ne.evaluate('sum(_arr_out, 3)')[::stride, :, :, 0, 0]
    # Note that you need to use '1' and not '1.0' so that the dtype of
    # the exponent does not change (i.e. get promoted)
    _arr_out = ne.evaluate('_arr_out ** (1 / order)')

    if arr_out is not None:
        arr_out[:] = _arr_out
    else:
        arr_out = _arr_out

    assert arr_out.dtype == arr_in.dtype

    return arr_out
Пример #15
0
def test_view_as_windows_1D():
    A = np.arange(10)
    window_shape = (3,)
    B = view_as_windows(A, window_shape)
    assert_equal(B, np.array([[0, 1, 2],
                              [1, 2, 3],
                              [2, 3, 4],
                              [3, 4, 5],
                              [4, 5, 6],
                              [5, 6, 7],
                              [6, 7, 8],
                              [7, 8, 9]]))
Пример #16
0
def test_view_as_windows_step_tuple():
    A = np.arange(24).reshape((6, 4))
    B = view_as_windows(A, (3, 2), step=3)
    assert B.shape == (2, 1, 3, 2)
    assert B.size != A.size

    C = view_as_windows(A, (3, 2), step=(3, 2))
    assert C.shape == (2, 2, 3, 2)
    assert C.size == A.size

    assert_equal(C, [[[[0,  1],
                       [4,  5],
                       [8,  9]],
                      [[2,  3],
                       [6,  7],
                       [10, 11]]],
                     [[[12, 13],
                         [16, 17],
                         [20, 21]],
                      [[14, 15],
                         [18, 19],
                         [22, 23]]]])
    def conv3D(self, X, fb):
        assert X.ndim == 3
        assert fb.ndim == 4
        assert X.shape[-1] == fb.shape[2]

        n_filters = fb.shape[-1]
        fb2 = fb.copy()
        fb2.shape = -1, n_filters

        X_rfi = view_as_windows(X, fb.shape[:3])
        outh, outw = X_rfi.shape[:2]
        X_rfim = X_rfi.reshape(outh * outw, -1)
        ret = np.dot(X_rfim, fb2)  # -- convolution as matrix multiplication
        return ret.reshape(outh, outw, n_filters)
Пример #18
0
    def gen_neg_examples(self):
        """
        Generates negative training patches
        from the dataset specified in the configuration
        """

        negfiles = os.listdir(self.negdatadir)
        negpatches = []

        for n in negfiles[:10]:
            f = self.negdatadir + "/" + n

            if "jpg" not in f:
                continue
            print "Processing image" + f
            im = cv2.imread(f)
            # gray = self.rgb2gray(im)
            gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
            # im =cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

            # Generate the negative patch
            # patch = image.extract_patches_2d(im, (256, 256)) # This generalates nultiple patches

            print np.shape(im)
            patch = view_as_windows(gray, (64, 64), 65)
            print np.shape(patch)
            # patch = [cv2.resize(x,(64,64)) for x in patch]

            # randomly choose 20 patches from all patches for this image
            fdimlen = np.shape(patch)[0]
            sdimlen = np.shape(patch)[1]
            patches = []

            # for i in range(len(self.trainpatches)):
            for i in range(8):  # we are getting too many negative examples, so reducing to 5

                randomdim = random.choice(range(fdimlen))
                randomsdim = random.choice(range(sdimlen))
                # randompatch = patch[randomdim][randomsdim][0] # if color un comment this
                randompatch = patch[randomdim][randomsdim]
                # randompatch = cv2.resize(randompatch, (64,64))
                patches.append(randompatch)

            negpatches += patches
            self.negpatches = negpatches
Пример #19
0
def montage_wb_ratio (input_image, patch_shape, n_filters, ele_print = False):

    from skimage.util.shape import view_as_windows
    from skimage.util.montage import montage2d
    from scipy.cluster.vq import kmeans2

    patches = view_as_windows(gray_crab, patch_shape)

    patches = patches.reshape(-1, patch_shape[0] * patch_shape[1])[::8]

    fb, _ = kmeans2(patches, n_filters, minit='points')

    fb = fb.reshape((-1,) + patch_shape)

    fb_montage = montage2d(fb, fill=False, rescale_intensity=True)

    elements = np.split(np.hstack(np.split(fb_montage, 4)), 16, axis=1)

    del elements[n_filters:]

    wb_ratios = []

    bin_elements = []

    for element in elements:

        thresh = threshold_otsu(element)

        binary = element > thresh

        ratio = np.sum(binary) / binary.size

        wb_ratios.append(ratio)

        if ele_print:
            bin_elements.append(binary)

    wb_ratios = sorted(wb_ratios, reverse = True)

    if ele_print:

        show_images(elements)
        show_images(bin_elements)

    return(wb_ratios)
Пример #20
0
def test_view_as_windows_2D():

    A = np.arange(5 * 4).reshape(5, 4)
    window_shape = (4, 3)
    B = view_as_windows(A, window_shape)
    assert_equal(B.shape, (2, 2, 4, 3))
    assert_equal(
        B,
        np.array(
            [
                [[[0, 1, 2], [4, 5, 6], [8, 9, 10], [12, 13, 14]], [[1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15]]],
                [
                    [[4, 5, 6], [8, 9, 10], [12, 13, 14], [16, 17, 18]],
                    [[5, 6, 7], [9, 10, 11], [13, 14, 15], [17, 18, 19]],
                ],
            ]
        ),
    )
Пример #21
0
def scipy_correlate4d_view(features,kernel,stride=1):

    #imshp = (3,2,20,20) # num images, channels, szy, szx
    #kshp = (10,2,10,10) # features, channels, szy, szx
    #featshp = (3,10,11,11) # num images, features, szy, szx

    from skimage.util.shape import view_as_windows

    featshp = features.shape
    kshp = kernel.shape

    output_sz = (featshp[0], kshp[1], kshp[2] + stride*featshp[2] - 1, kshp[3] + stride*featshp[3] - 1)

    k_rot = kernel[:,:,::-1,::-1]

    scipy_output = np.zeros(output_sz)
    for im_i in range(featshp[0]):

        im_out = np.zeros(output_sz[1:])
        im_outr = view_as_windows(im_out,(kshp[1],kshp[2],kshp[3]))[0,::stride,::stride,...]

        im_hatr = np.tensordot(np.squeeze(features[im_i,...]),k_rot,axes=((0,),(0,)))
        #import IPython; ipshell = IPython.embed; ipshell(banner1='ipshell')

#        for a in range(im_hatr.shape[0]):
#            im_outr[a,:,...] += im_hatr[a,:,...]

        for a in range(im_hatr.shape[0]):
            for b in range(im_hatr.shape[1]):
                im_outr[a,b,...] += im_hatr[a,b,...]

#        for d in range(im_hatr.shape[3]):
#            for e in range(im_hatr.shape[4]):
#                im_outr[:,:,:,d,e] += im_hatr[:,:,:,d,e]

#                for c in range(im_hatr.shape[2]):
#                    for d in range(im_hatr.shape[3]):
#                        for e in range(im_hatr.shape[4]):
#                            im_outr[a,b,c,d,e] += im_hatr[a,b,c,d,e]
#        im_outr += im_hatr

        scipy_output[im_i,...] = im_out

    return scipy_output
Пример #22
0
def fbcorr4(arr_in, arr_fb, stride=DEFAULT_STRIDE, arr_out=None):

    """4D Filterbank Correlation
    XXX: docstring
    """

    assert arr_in.ndim == 4
    assert arr_fb.ndim == 4
    assert arr_fb.dtype == arr_in.dtype

    in_imgs, inh, inw, ind = arr_in.shape
    fbh, fbw, fbd, fbn = arr_fb.shape

    f_size = fbh * fbw * fbd

    assert fbn > 1
    assert fbh <= inh
    assert fbw <= inw
    assert fbd == ind

    if arr_out is not None:
        assert arr_out.dtype == arr_in.dtype
        assert arr_out.shape == (in_imgs,
                                 1 + (inh - fbh) / stride,
                                 1 + (inw - fbw) / stride,
                                 fbn)

    # -- reshape arr_in
    arr_inr = view_as_windows(arr_in, (1, fbh, fbw, fbd))[::stride, ::stride]

    n_imgs, outh, outw = arr_inr.shape[:3]
    assert n_imgs == in_imgs

    arr_inrm = arr_inr.reshape(n_imgs * outh * outw, f_size)

    # -- reshape arr_fb
    arr_fbm = arr_fb.reshape((f_size, fbn))

    # -- correlate!
    arr_out = np.dot(arr_inrm, arr_fbm)
    arr_out = arr_out.reshape(n_imgs, outh, outw, -1)

    return arr_out
    def divisive_normalization(self, X, filter_norm):
        # -- pre nonlinearities
        rf_shape_side = (np.asarray(filter_norm.shape) - 1) / 2
        if len(X.shape) == 2:
            X.shape = X.shape + (1,)

        inh, inw, _ = X.shape

        ret = X ** 2

        # ret = self.conv3D(ret, filter_norm)
        X_rfi = view_as_windows(X, filter_norm.shape[:3])
        ret = X_rfi.sum(axis=(2, 3, 4))
        # -- post nonlinearities
        ret = np.sqrt(ret)

        np.maximum(ret, 1.0, ret)  # avoids that very small numbers will cause the nominator have an greater value

        return X[rf_shape_side[0] : inh - rf_shape_side[0], rf_shape_side[1] : inw - rf_shape_side[1], :] / ret
Пример #24
0
def convolve4d_view(image,kernel,mode='valid',stride=(1,1)):

    from skimage.util.shape import view_as_windows

    imshp = image.shape
    kshp = kernel.shape

    offset = None
    if mode=='valid':
        featshp = (imshp[0],kshp[0],(imshp[2] - kshp[2])/stride[0] + 1,(imshp[3] - kshp[3])/stride[1] + 1) # num images, features, szy, szx
    elif mode == 'same':
        assert stride == (1,1)
        featshp = (imshp[0],kshp[0],imshp[2],imshp[3]) # num images, features, szy, szx
        offset = (kshp[2]/2, kshp[3]/2)
    #elif mode == 'full':
    #    assert stride == (1,1)
    #    featshp = (imshp[0],kshp[0],imshp[2] + kshp[2] - 1,imshp[3] + kshp[3] - 1) # num images, features, szy, szx
    else:
        raise NotImplemented, 'Unkonwn mode %s'%mode

    kernel_flipped = kernel[:,:,::-1,::-1]

    output = np.zeros(featshp)
    this_image = None
    for im_i in range(imshp[0]):

        if mode == 'valid':
            this_image = image[im_i,...]
        elif mode == 'same':
            if this_image is None:
                this_image_shp = (imshp[1], imshp[2] + kshp[2] - 1, imshp[3] + kshp[3] - 1)
                this_image = np.zeros(this_image_shp,dtype=image.dtype)
            this_image[:,offset[0]:(offset[0]+imshp[2]),offset[1]:(offset[1]+imshp[3])] = image[im_i,...]
        else:
            raise NotImplemented
        imager = view_as_windows(this_image,(kshp[1],kshp[2],kshp[3]))[0,::stride[0],::stride[1],...]
        # imager.shape = (featszr, featszc, channels, ksz[2], ksz[3])
        feat = np.tensordot(kernel_flipped,imager,axes=((1,2,3),(2,3,4)))

        output[im_i,...] = feat

    return output
Пример #25
0
    def split_into_patches(self,
                           channel=None,
                           patch_shape=(256, 256, 3),
                           overlap=0):
        """
        Split an MxNxC image (where C is the number of channels) into patches.

        Inputs:
        - channel: Image channel as indicated by.
        - patch_shape: .
        :param patch_shape:
        :param channel:
        """

        # TODO: determine whether patch all channels at once, or individual channels
        # Note: use skimage.util.shape.view_as_windows()
        # Stride = shape[0] - overlap
        # or if you want overlap in both x and y dimens stride = patch_shape - overlap
        # where type(stride)==type(patch_shape)==type(overlap) == tuple of length n dimensions\
        # overlap cannot be larger than patch_shape - 1 nor small

        # USING view_as_windows(image, patch_shape, step)
        # for and MxNx3 image, I want each patch to tilesize x tilesize x 3 patch (3D) patch
        # so patch_shape=(tilesize, tilesize, 3)
        # for nonoverlapping patches, our step in dims 0 and 1 should be the same as tilesize
        # so, step(tilesize, tilesize, 1)

        # TODO: LOTS OF ERROR CHECKING!!
        if channel:
            im = self.images[channel]
        else:
            im = self.images[Strings.IF]
        if patch_shape != im.shape:
            patch_shape = (256, 256, im.shape[2])

        step = tuple((patch_shape[0] - overlap, patch_shape[1] - overlap, 1))

        self.patches = shape.view_as_windows(im, patch_shape, step)
Пример #26
0
def fbcorr3(arr_in, arr_fb, stride=DEFAULT_STRIDE, arr_out=None):
    """3D Filterbank Correlation
    XXX: docstring
    """

    assert arr_in.ndim == 3
    assert arr_fb.ndim == 4
    assert arr_fb.dtype == arr_in.dtype

    inh, inw, ind = arr_in.shape
    fbh, fbw, fbd, fbn = arr_fb.shape

    assert fbn > 1
    assert fbh <= inh
    assert fbw <= inw
    assert fbd == ind

    if arr_out is not None:
        assert arr_out.dtype == arr_in.dtype
        assert arr_out.shape == (1 + (inh - fbh) / stride,
                                 1 + (inw - fbw) / stride,
                                 fbn)

    # -- reshape arr_in
    arr_inr = view_as_windows(arr_in, (fbh, fbw, fbd))[::stride, ::stride]
    outh, outw = arr_inr.shape[:2]
    arr_inrm = arr_inr.reshape(outh * outw, -1)

    # -- reshape arr_fb
    arr_fbm = arr_fb.reshape((fbh * fbw * fbd, fbn))

    # -- correlate !
    arr_out = np.dot(arr_inrm, arr_fbm)
    arr_out = arr_out.reshape(outh, outw, -1)

    assert arr_out.dtype == arr_in.dtype  # XXX: should go away

    return arr_out
def extract_non_faces(output_size):
    file_name = "{}_{}_non_faces".format(*(output_size))
    new_dir_name = os.path.join(image_save_path, file_name)

    i = 0

    if not os.path.exists(new_dir_name):
        os.makedirs(new_dir_name)

    file_paths = []

    for path, subdirs, files in os.walk(image_path):
        for name in files:
            if not fnmatch(name, face_dir_pattern):
                file_paths.append(os.path.join(path, name))

    window_shape = (32, 32)

    for file_path in file_paths:
        image = io.imread(file_path)

        if len(image.shape) < 3:
            continue

        if image.shape[2] == 1:
            continue

        windows = view_as_windows(image, window_shape)

        for window in windows:
            save_file_string = os.path.join(new_dir_name, "{0:06d}".format(i) + ".jpg")

            im = resize(window, output_size)
            io.imsave(save_file_string, im)

            i += 1
Пример #28
0
def main():
    # global scale_factor
    # global TPB
    # global dim

    # parser = argparse.ArgumentParser(description='GPU Pathfinding')
    # parser.add_argument('scale_factor', type=int, help='Scale factor (power of 2)')
    # parser.add_argument('TPB', type=int, help='Block width')
    # args = parser.parse_args()
    # scale_factor = args.scale_factor
    # TPB = args.TPB
    # dim = int(math.pow(2, scale_factor)), int(math.pow(2, scale_factor))

    width, height = dim

    print('----- Preparing Grid -----')
    # create grid from image dataset
    # grid = np.zeros(dim, dtype=np.int32)
    # createGridFromDatasetImage('dataset/da2-png', grid, dim)
    grid = np.ones(dim, dtype=np.int32)

    # generate random start and goal
    # start = [-1, -1]
    # goal = [-1, -1]
    # randomStartGoal(grid, start, goal)
    start = [0, 0]
    goal = [grid.shape[0] - 1, grid.shape[1] - 1]
    start = np.array(start)
    goal = np.array(goal)

    # debugging purposes: use guide for 1D mapping of indexes
    guide = np.arange(dim[0] * dim[1]).reshape(dim).astype(np.int32)

    # initialize essential arrays for search algorithm
    print('----- Initializing Variables -----')

    H_goal = np.empty(dim, dtype=np.int32)
    H_goal[:] = UNEXPLORED
    H_start = np.empty(dim, dtype=np.int32)
    H_start[:] = UNEXPLORED

    # compute heuristics towards start and goal
    threadsperblock = (TPB, TPB)
    blockspergrid_x = math.ceil(grid.shape[0] / threadsperblock[0])
    blockspergrid_y = math.ceil(grid.shape[1] / threadsperblock[1])
    blockspergrid = (blockspergrid_x, blockspergrid_y)
    print('THREADS PER BLOCK: ', threadsperblock)
    print('BLOCKS PER GRID: ', blockspergrid)
    print('----- Computing Heuristics -----')
    computeHeuristics[blockspergrid, threadsperblock](grid, start, goal,
                                                      H_start, H_goal)
    print(blockspergrid)

    # prepare grid blocking guide
    block = np.zeros(dim, dtype=np.int32)
    block = blockshaped(block, TPB, TPB)
    for i in range(block.shape[0]):
        block[i, :] = i
    block = unblockshaped(block, width, height)

    # prepare padded grid, guide, H_goal
    padded_grid = np.zeros((width + 2, height + 2), dtype=np.int32)
    padded_guide = np.empty((width + 2, height + 2), dtype=np.int32)
    padded_guide[:] = -1
    padded_block = np.empty((width + 2, height + 2), dtype=np.int32)
    padded_block[:] = -1
    padded_H_goal = np.empty((width + 2, height + 2), dtype=np.int32)
    padded_H_goal[:] = UNEXPLORED
    padGrid[blockspergrid, threadsperblock](grid, padded_grid)
    padGrid[blockspergrid, threadsperblock](guide, padded_guide)
    padGrid[blockspergrid, threadsperblock](block, padded_block)
    padGrid[blockspergrid, threadsperblock](H_goal, padded_H_goal)

    # print(padded_grid)
    # print(padded_guide)
    # print(padded_H_goal)

    # prepare local grids
    grid_blocks = view_as_windows(padded_grid, (TPB + 2, TPB + 2), step=TPB)
    grid_blocks = grid_blocks.reshape(
        grid_blocks.shape[0] * grid_blocks.shape[1], grid_blocks.shape[2],
        grid_blocks.shape[3])

    guide_blocks = view_as_windows(padded_guide, (TPB + 2, TPB + 2), step=TPB)
    guide_blocks = guide_blocks.reshape(
        guide_blocks.shape[0] * guide_blocks.shape[1], guide_blocks.shape[2],
        guide_blocks.shape[3])

    H_goal_blocks = view_as_windows(padded_H_goal, (TPB + 2, TPB + 2),
                                    step=TPB)
    H_goal_blocks = H_goal_blocks.reshape(
        H_goal_blocks.shape[0] * H_goal_blocks.shape[1],
        H_goal_blocks.shape[2], H_goal_blocks.shape[3])

    blocks = view_as_windows(padded_block, (TPB + 2, TPB + 2), step=TPB)
    blocks = blocks.reshape(blocks.shape[0] * blocks.shape[1], blocks.shape[2],
                            blocks.shape[3])
    # print(grid_blocks.shape)

    print('Start: ', start)
    print('Goal: ', goal)
    print('Grid')
    print(grid)
    print('Grid Index Guide: ')
    print(guide)
    # print('Start H: ')
    # print(H_start)
    print('Goal H: ')
    print(H_goal)
    # print('Grid Blocking:')
    # print(block)
    # print('Padded Grid Blocks:')
    # print(grid_blocks)
    # print('Padded Guide Blocks:')
    # print(guide_blocks)
    # print('Padded Goal H Blocks:')
    # print(H_goal_blocks)
    # print('Padded Block Blocks:')
    # print(blocks)

    # parents array contains info where tiles came from
    parents = np.empty((width, height, TPB + 2, TPB + 2), np.int32)
    parents[:] = -1

    established_goal = np.zeros(dim, np.int32)
    established_local_goal = np.zeros(dim, np.int32)

    # print(parents)

    # Simultaneous local search
    print('----- Simulataneously Searching for SubPaths -----')
    x, y = start
    # x, y = goal
    s = timer()
    counter = np.zeros(dim, np.int32)
    # GridDecompSearch[blockspergrid, threadsperblock](grid, start, goal, H_goal, block, parents, grid_blocks, guide_blocks, H_goal_blocks, blocks)
    GridDecompSearch[blockspergrid,
                     threadsperblock](grid, H_goal, block, grid_blocks, start,
                                      goal, parents, H_goal_blocks,
                                      guide_blocks, blocks, counter,
                                      established_goal, established_local_goal)
    # print(parents)
    # print(counter)
    print(grid_blocks[block[x, y]])
    print(guide_blocks[block[x, y]])
    print(H_goal_blocks[block[x, y]])
    print(parents[x, y])
    print(guide)
    print(H_goal)
    print(established_goal)
    print(established_local_goal)
    e = timer()
    print('kernel launch (+ compilation) done in ', e - s, 's')

    # time_ave = 0
    # runs = 10
    # for run in range(runs):
    #     counter[:] = 0
    #     s = timer()
    #     GridDecompSearch[blockspergrid, threadsperblock](grid, H_goal, block, grid_blocks, start, goal, parents, H_goal_blocks, guide_blocks, blocks, counter)
    #     print(counter)
    #     e = timer()
    #     time_ave += (e-s)
    #     print('%dth kernel launch done in ' %(run), e-s, 's')
    # time_ave = time_ave/runs
    # print('Average runtime in ', runs, ' runs: ', time_ave)

    # trying to recreate path
    print('----- Reconstructing Path -----')
    start_1d_index = start[0] * width + start[1]
    goal_1d_index = goal[0] * width + goal[1]
    current_index = start_1d_index
    print('START IN 1D: ', start_1d_index)
    print('GOAL IN 1D: ', goal_1d_index)
    path = []
    ctr = 0
    while current_index != goal_1d_index:
        if ctr > width * 2:  # just in case there is infinite loop
            print('Timeout!')
            break
        path.append(current_index)
        # calculate 2D index from 1D
        current_x = int((current_index - (current_index % width)) / width)
        current_y = current_index % width
        # get the established goal using 2D index
        # set current index to established goal (1D) index
        current_index = established_goal[current_x, current_y]
        ctr += 1
    current_x = int((current_index - (current_index % width)) / width)
    current_y = current_index % width
    current_index = established_goal[current_x, current_y]
    path.append(current_index)
    print('paths connecting blocks: ', path)

    print('----- Reconstructing Subpaths -----')
    subpaths = []
    for start_index in path:
        start_x = int((start_index - (start_index % width)) / width)
        start_y = start_index % width
        start_block = block[start_x, start_y]
        subpath = []
        print()
        print('BLOCK: ', start_block, 'LOCAL GOAL: ',
              established_local_goal[start_x, start_y])
        reconstructPathV3(parents[start_x, start_y], guide_blocks[start_block],
                          established_local_goal[start_x, start_y], subpath)
        print('start: ', start_index, 'subpath: ', subpath)
        subpaths = subpaths + subpath
    print('full path: (w/ duplicates) ', subpaths)
Пример #29
0
def extract_patches_Step(image, patch_size, step_patches=24):
    i_h, i_w = image.shape[:2]
    p_h, p_w = patch_size

    if p_h > i_h:
        raise ValueError("Height of the patch should be less than the height"
                         " of the image.")

    if p_w > i_w:
        raise ValueError("Width of the patch should be less than the width"
                         " of the image.")
    print("PATCH SIZE")
    print(patch_size)
    cnt = 0
    cnt_h = 0
    cnt_w = 0
    patches = []

    border_crop = 2

    #new_patch= np.zeros( (p_h,p_w,3))
    for w in range(i_w - p_w):
        if w == 0 or w % step_patches == 0:
            if w == 0:
                crop_w = 0
            else:
                crop_w = 0
            cnt_h = 0
            for h in range(i_h - p_h):
                if h == 0 or h % step_patches == 0:
                    if h == 0:
                        crop_h = 0
                    else:
                        crop_h = 0

                    #patchul=image[h:h + p_h, w:w + p_w]
                    patchul = image[h - crop_h:h + p_h + crop_h,
                                    w - crop_w:w + p_w + crop_w]

                    #patches[cnt]=patchul
                    patches.append(patchul)
                    #imgmap[cnt_h,cnt_w]=cnt
                    #print("Number of patches  = %d, Patch Shape h w= (%d, %d)" % (cnt, patchul.shape[0], patchul.shape[1]))
                    cnt += 1

                    cnt_h += 1

            cnt_w += 1
    print(cnt_h)
    i = 0
    out_patch = np.zeros((cnt, p_h, p_w, 3))
    for p in patches:
        out_patch[i] = p
        i += 1

    #return patches , (cnt_h,cnt_w)

    #return
    #Rebuild part
    cnt_bild = 0
    #imgageRebuild = np.zeros( (i_h+p_h,i_w+p_w,3))
    imgageRebuild = np.zeros((i_h, i_w, 3))
    for w in range(cnt_w):
        for h in range(cnt_h):
            a = imgageRebuild[h * step_patches:h * step_patches + p_h,
                              w * step_patches:w * step_patches + p_w]
            #print("pozitie h w of patches  = (%d,  %d) , Patch Shape h w= (%d, %d)" % (h*step_patches  ,w*step_patches , a.shape[0], a.shape[1]))
            print(a.shape)
            print(cnt_bild)
            imgageRebuild[h * step_patches:h * step_patches + p_h,
                          w * step_patches:w * step_patches +
                          p_w] = out_patch[cnt_bild]
            cnt_bild += 1

    imsave('/home/www/imgsuper/val_images/test.png', imgageRebuild)

    return out_patch, (cnt_h, cnt_w)

    #B = view_as_windows(image, window_shape)
    image = image.reshape((i_h, i_w, -1))
    n_colors = image.shape[-1]
    patch_shape = (p_h, p_w, n_colors)
    print(image.ndim)
    print(image.shape)

    B = view_as_windows(image, patch_shape, step_patches)
    print(B.shape)
    patches = B.reshape(-1, p_h, p_w, n_colors)
    print('----')
    print(patches.shape)
    return patches
Пример #30
0
def get_features(image_list, n_words=256, learn_ratio=50):

    # This for loop passes the window "patch_shape" to extract individual 8x8x3
    # patches all along the tiles.
    # The extracted patches are used to fit the kmeans classifier
    features = []
    image_list_path = os.path.dirname(image_list[0])
    image_list_path = os.path.dirname(image_list_path)
    print('Extracting feature from images in '.format(image_list_path))

    inertia = []
    pdab_list = []
    ph_list = []
    patch_shape = (8, 8)
    print('****')
    print('Step 0: Sliding window')
    start = time.time()
    # Fits k-means in 1/50 of the images
    for i in tqdm(range(0, len(image_list), learn_ratio)):
        with Image.open(image_list[i]) as image:
            image = numpy.asarray(rgb2hed(image))
            if (image.shape[0] == image.shape[1]):
                im_dab = image[:, :, 2]
                im_h = image[:, :, 0]
                im_dab = preprocess_input(im_dab)
                im_h = preprocess_input(im_h)
                im_dab = im_dab.astype(float)
                im_h = im_h.astype(float)
                p_dab = view_as_windows(im_dab, patch_shape)
                p_h = view_as_windows(im_h, patch_shape)
                p_dab = get_patch_reshaped(p_dab, patch_shape)
                p_h = get_patch_reshaped(p_h, patch_shape)
                pdab_list.extend(p_dab)
                ph_list.extend(p_h)
    end = time.time()
    print('Total time Sliding Window: {:.4f} s'.format(end - start))
    print()

    print('****')
    print('Step 1: KMeans fitting (MiniBatchKMeans)')
    start = time.time()
    kmeans_dab = MiniBatchKMeans(n_clusters=n_words)
    kmeans_h = MiniBatchKMeans(n_clusters=n_words)
    kmeans_dab.fit(p_dab_list)
    kmeans_h.fit(p_h_list)
    end = time.time()
    print('Inertia DAB: {}'.format(kmeans_dab.inertia_))
    print('Inertia H: {}'.format(kmeans_h.inertia_))
    print()
    print('Total time MiniBatchKMeans fitting: {:.4f} s'.format(end - start))
    print()

    print('****')
    print('Step 1: KMeans fitting (KMeansTF)')
    start = time.time()
    kmeanstf_dab = KMeansTF(n_clusters=n_words)
    kmeanstf_h = KMeansTF(n_clusters=n_words)
    kmeanstf_dab.fit(p_dab_list)
    kmeanstf_h.fit(p_h_list)
    end = time.time()
    print('Inertia DAB: {}'.format(kmeanstf_dab.inertia_))
    print('Inertia H: {}'.format(kmeanstf_h.inertia_))
    print()
    print('Total time KMeansTF fitting: {:.4f} s'.format(end - start))
    print()

    # Generate the filter set for the convolution layer
    kernel_in = []
    k_shape = (8, 8, 1)
    for i in range(k_shape[0]):
        for j in range(k_shape[1]):
            k = numpy.zeros(k_shape)
            k[i, j, :] = 1
            kernel_in.append(k)

    kernel_in = numpy.asarray(kernel_in)
    kernel_in = kernel_in.reshape(
        (k_shape[0], k_shape[1], k_shape[2], k_shape[0] * k_shape[1]))
    kernel_in = tf.constant(kernel_in, dtype=tf.float32)

    inertia = []
    pdab_list = []
    ph_list = []
    patch_shape = (8, 8)
    print('****')
    print('Step 0: Tensorflow Conv2D')
    start = time.time()
    # Fits k-means in 1/50 of the images
    for i in tqdm(range(0, len(image_list), learn_ratio)):
        with Image.open(image_list[i]) as image:
            image = numpy.asarray(rgb2hed(image))
            if (image.shape[0] == image.shape[1]):
                im_dab = image[:, :, 2]
                im_h = image[:, :, 0]
                im_dab = preprocess_input(im_dab)
                im_h = preprocess_input(im_h)
                im_dab = im_dab.astype(float)
                im_h = im_h.astype(float)
                im_dab = im_dab.reshape(1, 224, 224, 1)
                im_h = im_h.reshape(1, 224, 224, 1)
                im_dab = tf.constant(im_dab, dtype=tf.float32)
                im_h = tf.constant(im_h, dtype=tf.float32)
                p_dab = tf.nn.conv2d(input=im_dab,
                                     filters=kernel_in,
                                     strides=1,
                                     padding='SAME')
                p_h = tf.nn.conv2d(input=im_h,
                                   filters=kernel_in,
                                   strides=1,
                                   padding='SAME')
                shape = p_dab.numpy().shape
                pdab_list.extend(p_dab.numpy().reshape(
                    shape[0] * shape[1] * shape[2], shape[3]))
                ph_list.extend(p_h.numpy().reshape(
                    shape[0] * shape[1] * shape[2], shape[3]))
    end = time.time()
    print('Total time Tensorflow Conv2D: {:.4f} s'.format(end - start))
    print()

    inertia = []
    pdab_list = []
    ph_list = []
    patch_shape = (8, 8)
    print('****')
    print('Step 0: Tensorflow ExtractPatches')
    start = time.time()
    # Fits k-means in 1/50 of the images
    for i in tqdm(range(0, len(image_list), learn_ratio)):
        with Image.open(image_list[i]) as image:
            image = numpy.asarray(rgb2hed(image))
            if (image.shape[0] == image.shape[1]):
                im_dab = image[:, :, 2]
                im_h = image[:, :, 0]
                im_dab = preprocess_input(im_dab)
                im_h = preprocess_input(im_h)
                im_dab = im_dab.astype(float)
                im_h = im_h.astype(float)
                im_dab = im_dab.reshape(1, 224, 224, 1)
                im_h = im_h.reshape(1, 224, 224, 1)
                im_dab = tf.constant(im_dab, dtype=tf.float32)
                im_h = tf.constant(im_h, dtype=tf.float32)
                p_dab = tf.image.extract_patches(
                    im_dab,
                    sizes=[1, k_shape[0], k_shape[1], 1],
                    strides=[1, 1, 1, 1],
                    rates=[1, 1, 1, 1],
                    padding='VALID')
                p_h = tf.image.extract_patches(
                    im_h,
                    sizes=[1, k_shape[0], k_shape[1], 1],
                    strides=[1, 1, 1, 1],
                    rates=[1, 1, 1, 1],
                    padding='VALID')
                shape = p_dab.numpy().shape
                pdab_list.extend(p_dab.numpy().reshape(
                    shape[0] * shape[1] * shape[2], shape[3]))
                ph_list.extend(p_h.numpy().reshape(
                    shape[0] * shape[1] * shape[2], shape[3]))
    end = time.time()
    print('Total time Tensorflow ExtractPatches: {:.4f} s'.format(end - start))
    print()

    return features, kmeans_dab, kmeans_h, kmeanstf_dab, kmeanstf_h
Пример #31
0
    def max_pooling(self, X, n_layer):

        X_rfi = view_as_windows(X, self.filters_pooling[n_layer].shape + (1, ))
        X_rfi = X_rfi[::self.stride_pool[n_layer][0], ::self.
                      stride_pool[n_layer][1], :, :, :, :]
        return np.amax(X_rfi, axis=(3, 4, 5))
Пример #32
0
 def prepare(self, image_left, image_right, window_size):
     window_shape = (window_size, window_size)
     self.window_size = window_size
     self.windows_left = view_as_windows(image_left, window_shape)
     self.windows_right = view_as_windows(image_right, window_shape)
Пример #33
0
def pred_ICIAR_test(model_name):

    # get test images
    test_images_dir = "/global/home/hpc4535/CISC881FinalProject/ICIAR2018_BACH_Challenge_TestDataset/Photos/"
    test_images_fnames = os.listdir(test_images_dir)
    test_images_fnames.sort(key=lambda f: int("".join(filter(str.isdigit, f))))

    # load model
    pred_datagen = ImageDataGenerator(rescale=1. / 255)

    model_dir = "/global/home/hpc4535/CISC881FinalProject/ICIAR2018classification/4_CNN9layerModel_patches/outputs/"
    model = load_model(os.path.join(model_dir, (model_name + ".h5")))

    # predict test images by patches
    all_images_path = test_images_dir

    pred_label_list = []
    pred_conf_list = []

    for case in range(len(test_images_fnames)):
        #     print("test #%s label = %d" % (case, test_labels_shuffled[case]))
        #     print("test #%s fname = %s" % (case, test_images_fnames[case]))

        img_BGR = cv2.imread(
            os.path.join(all_images_path, test_images_fnames[case]))
        print("%s.shape: %s" % (test_images_fnames[case], img_BGR.shape))

        window_shape = (1400, 1400)
        step = 100
        img_patches_B = view_as_windows(img_BGR[:, :, 0],
                                        window_shape,
                                        step=step)
        img_patches_G = view_as_windows(img_BGR[:, :, 1],
                                        window_shape,
                                        step=step)
        img_patches_R = view_as_windows(img_BGR[:, :, 2],
                                        window_shape,
                                        step=step)

        ################################################################
        # get bkgd and non-bkgd patches
        non_bkgd_patches = []
        bkgd_patches = []

        resize = 300

        for i in range(img_patches_B.shape[0]):
            for j in range(img_patches_B.shape[1]):
                img_patch = np.dstack(
                    (img_patches_B[i, j], img_patches_G[i,
                                                        j], img_patches_R[i,
                                                                          j]))
                img_patch = cv2.resize(img_patch, (resize, resize),
                                       interpolation=cv2.INTER_AREA)

                thresh = 200
                img_patch_thresh = cv2.threshold(img_patch, thresh, 255,
                                                 cv2.THRESH_BINARY)[1]

                white_thresh = 254
                nwhite_total = img_patch_thresh.shape[
                    0] * img_patch_thresh.shape[1]
                nwhite = 0

                for p in range(img_patch_thresh.shape[0]):
                    for q in range(img_patch_thresh.shape[1]):
                        if all(pixel > white_thresh
                               for pixel in list(img_patch_thresh[p, q, :])):
                            nwhite += 1
        #         all_patches.append(img_patch)

                bkgd_thresh = 0.7
                if nwhite / nwhite_total < bkgd_thresh:
                    non_bkgd_patches.append(img_patch)
                else:
                    bkgd_patches.append(img_patch)

        non_bkgd_patches_arr = np.array(non_bkgd_patches)
        print("case", test_images_fnames[case],
              "non_bkgd_patches_arr.shape = ", non_bkgd_patches_arr.shape)

        ################################################################
        # make prediction of patches
        print("predicting patches for case: ", test_images_fnames[case])
        pred = model.predict(pred_datagen.flow(non_bkgd_patches_arr))

        ################################################################
        # get prediction labels
        pred_argmax = np.array([np.argmax(p) for p in pred])

        pred_dict = {}
        for i in range(4):
            pred_dict[i] = np.count_nonzero(pred_argmax == i)

        photo_wide_label = max(pred_dict.items(),
                               key=operator.itemgetter(1))[0]
        pred_label_list.append(photo_wide_label)

        pred_conf = max(pred_dict.values()) / non_bkgd_patches_arr.shape[0]
        pred_conf_list.append(pred_conf)

        print("%spredicted label = %d" %
              (test_images_fnames[case], photo_wide_label))
        print("%spred_confidence = %.1f" %
              (test_images_fnames[case], pred_conf))
        print("\n")

    # write prediction csv
    test_images_fnames_num = [
        int("".join(filter(str.isdigit, f))) for f in test_images_fnames
    ]

    zippedList = list(zip(test_images_fnames_num, pred_label_list))
    prediction_df = pd.DataFrame(zippedList, columns=["case", "class"])
    prediction_df.to_csv("./pred.csv", index=False)
Пример #34
0
def test_view_as_windows_input_not_array():

    A = [1, 2, 3, 4, 5]
    view_as_windows(A, (2, ))
Пример #35
0
    user_to_zip = loadFile[1:, 1].astype('int')
    num_users_per_grp = 2000

    # getting the number of days for each month
    month_end_dates = (dates[dates.is_month_end])[::24]
    numberDaysMonth = np.array(month_end_dates.day)
    # For the case that the last day of the data is not the last day of the month,
    # for example: if data has 8760 time stamps and it is leap year, then last day is 7/30
    if month_end_dates[-1].month != dates[-1].month:
        numberDaysMonth = np.concatenate(
            [numberDaysMonth, (np.array([dates[-1].day]))])

    sizeFrameMonth = 3  # 3 months train and one extra for test

    # Generate 2D arrays representing each training months (Last one does not have test data)
    xMonths = view_as_windows(np.arange(numberDaysMonth.size),
                              (sizeFrameMonth, ))

    # Iterate 59 groups
    for group_idx in xrange(1, 60):
        print 'Starting group ', group_idx
        # Read load file for one group
        loadPath = data_dir + '/user_grp_' + str(group_idx) + '.csv'
        loadFile = np.loadtxt(loadPath, delimiter=',', dtype=np.str)
        load = loadFile[1:, 1:].astype('float')
        # Remove empty data
        idx = np.logical_or(np.isnan(load), np.isinf(load))
        load[idx] = 0.0

        # Get zip for each user in the group
        zipcodes = user_to_zip[num_users_per_grp *
                               (group_idx - 1):num_users_per_grp * (group_idx)]
Пример #36
0
def feng_threshold(img, w_size1=15, w_size2=30,
                   k1=0.15, k2=0.01, alpha1=0.1):
    """ Runs the Feng's thresholding algorithm.

    Reference:
    Algorithm proposed in: Meng-Ling Feng and Yap-Peng Tan, “Contrast adaptive
    thresholding of low quality document images”, IEICE Electron. Express,
    Vol. 1, No. 16, pp.501-506, (2004).

    Modifications: Using integral images to compute the local mean and the
    standard deviation

    @param img: The input image. Must be a gray scale image
    @type img: ndarray
    @param w_size1: The size of the primary local window to compute
        each pixel threshold. Should be an odd window
    @type w_size1: int
    @param w_size2: The size of the secondary local window to compute
        the dynamic range standard deviation. Should be an odd window
    @type w_size2: int
    @param k1: Parameter value that lies in the interval [0.15, 0.25].
    @type k1: float
    @param k2: Parameter value that lies in the interval [0.01, 0.05].
    @type k2: float
    @param alpha1: Parameter value that lies in the interval [0.15, 0.25].
    @type alpha1: float

    @return: The estimated local threshold for each pixel
    @rtype: ndarray
    """
    # Obtaining rows and cols
    rows, cols = img.shape
    i_rows, i_cols = rows + 1, cols + 1

    # Computing integral images
    # Leaving first row and column in zero for convenience
    integ = np.zeros((i_rows, i_cols), np.float)
    sqr_integral = np.zeros((i_rows, i_cols), np.float)

    integ[1:, 1:] = np.cumsum(np.cumsum(img.astype(np.float), axis=0), axis=1)
    sqr_img = np.square(img.astype(np.float))
    sqr_integral[1:, 1:] = np.cumsum(np.cumsum(sqr_img, axis=0), axis=1)

    # Defining grid
    x, y = np.meshgrid(np.arange(1, i_cols), np.arange(1, i_rows))

    # Obtaining local coordinates
    hw_size = w_size1 // 2
    x1 = (x - hw_size).clip(1, cols)
    x2 = (x + hw_size).clip(1, cols)
    y1 = (y - hw_size).clip(1, rows)
    y2 = (y + hw_size).clip(1, rows)

    # Obtaining local areas size
    l_size = (y2 - y1 + 1) * (x2 - x1 + 1)

    # Computing sums
    sums = (integ[y2, x2] - integ[y2, x1 - 1] -
            integ[y1 - 1, x2] + integ[y1 - 1, x1 - 1])
    sqr_sums = (sqr_integral[y2, x2] - sqr_integral[y2, x1 - 1] -
                sqr_integral[y1 - 1, x2] + sqr_integral[y1 - 1, x1 - 1])

    # Computing local means
    means = sums / l_size

    # Computing local standard deviation
    stds = np.sqrt(sqr_sums / l_size - np.square(means))

    # Obtaining windows
    padded_img = np.ones((rows + w_size1 - 1, cols + w_size1 - 1)) * np.nan
    padded_img[hw_size: -hw_size, hw_size: -hw_size] = img

    winds = view_as_windows(padded_img, (w_size1, w_size1))

    # Obtaining maximums and minimums
    mins = np.nanmin(winds, axis=(2, 3))

    # Obtaining local coordinates for std range calculations
    hw_size = w_size2 // 2
    x1 = (x - hw_size).clip(1, cols)
    x2 = (x + hw_size).clip(1, cols)
    y1 = (y - hw_size).clip(1, rows)
    y2 = (y + hw_size).clip(1, rows)

    # Obtaining local areas size
    l_size = (y2 - y1 + 2) * (x2 - x1 + 2)

    # Computing sums
    sums = (integ[y2, x2] - integ[y2, x1 - 1] -
            integ[y1 - 1, x2] + integ[y1 - 1, x1 - 1])
    sqr_sums = (sqr_integral[y2, x2] - sqr_integral[y2, x1 - 1] -
                sqr_integral[y1 - 1, x2] + sqr_integral[y1 - 1, x1 - 1])

    # Computing local means2
    means2 = sums / l_size

    # Computing standard deviation range
    std_ranges = np.sqrt(sqr_sums / l_size - np.square(means2))

    # Computing normalized standard deviations and extra alpha parameters
    n_stds = stds / std_ranges
    n_sqr_std = np.square(n_stds)
    alpha2 = k1 * n_sqr_std
    alpha3 = k2 * n_sqr_std

    thresholds = ((1 - alpha1) * means + alpha2 * n_stds
                  * (means - mins) + alpha3 * mins)

    return thresholds
Пример #37
0
def test_view_as_windows_negative_window_length():

    A = np.arange(10)
    view_as_windows(A, (-1, ))
Пример #38
0
def test_view_as_windows_wrong_window_dimension():

    A = np.arange(10)
    view_as_windows(A, (2, 2))
Пример #39
0
def lcdnorm4(arr_in,
             neighborhood,
             contrast=DEFAULT_CONTRAST,
             divisive=DEFAULT_DIVISIVE,
             stretch=DEFAULT_STRETCH,
             threshold=DEFAULT_THRESHOLD,
             stride=DEFAULT_STRIDE,
             arr_out=None):
    """4D Local Contrast Divisive Normalization

    XXX: docstring
    """

    assert arr_in.ndim == 4
    assert len(neighborhood) == 2
    assert isinstance(contrast, bool)
    assert isinstance(divisive, bool)
    assert contrast or divisive

    in_imgs, inh, inw, ind = arr_in.shape

    nbh, nbw = neighborhood
    assert nbh <= inh
    assert nbw <= inw

    nb_size = 1. * nbh * nbw * ind

    if arr_out is not None:
        assert arr_out.dtype == arr_in.dtype
        assert arr_out.shape == (in_imgs, 1 + (inh - nbh) / stride,
                                 1 + (inw - nbw) / stride, ind)

    # -- prepare arr_out
    lys = nbh / 2
    lxs = nbw / 2
    rys = (nbh - 1) / 2
    rxs = (nbw - 1) / 2
    _arr_out = arr_in[:, lys:inh - rys, lxs:inw - rxs][::stride, ::stride]

    # -- Contrast Normalization
    if contrast:

        # -- local sums
        arr_sum = arr_in.sum(-1)
        arr_sum = view_as_windows(arr_sum, (1, 1, nbw)).sum(-1)[:, :, ::stride,
                                                                0, 0]
        arr_sum = view_as_windows(arr_sum, (1, nbh, 1)).sum(-2)[:, ::stride, :,
                                                                0]

        # -- remove the mean
        _arr_out = _arr_out - arr_sum / nb_size

    # -- Divisive (gain) Normalization
    if divisive:

        # -- local sums of squares
        arr_ssq = (arr_in**2.0).sum(-1)
        arr_ssq = view_as_windows(arr_ssq, (1, 1, nbw)).sum(-1)[:, :, ::stride,
                                                                0, 0]
        arr_ssq = view_as_windows(arr_ssq, (1, nbh, 1)).sum(-2)[:, ::stride, :,
                                                                0]

        # -- divide by the euclidean norm
        if contrast:
            l2norms = (arr_ssq - (arr_sum**2.0) / nb_size)
        else:
            l2norms = arr_ssq

        np.putmask(l2norms, l2norms < 0., 0.)
        l2norms = np.sqrt(l2norms) + EPSILON

        if stretch != 1:
            _arr_out *= stretch
            l2norms *= stretch

        np.putmask(l2norms, l2norms < (threshold + EPSILON), 1.0)

        _arr_out = _arr_out / l2norms

    if arr_out is not None:
        arr_out[:] = _arr_out
    else:
        arr_out = _arr_out

    assert arr_out.shape[0] == in_imgs

    return arr_out
Пример #40
0
def test_view_as_windows_step_below_one():
    A = np.arange(10)
    view_as_windows(A, (11,), step=0.9)
Пример #41
0
    for nid, img in enumerate(neg_img):
        print "cas_lv ", cascade_lv, " ", nid+1,"/",len(neg_img), "th image...", "neg_db size: ", neg_db_num, "thr_12: ", thr_12, "thr_24: ", thr_24
        pyramid = tuple(pyramid_gaussian(img, downscale = etc.downscale))
        neg_db_for_scale = [0 for _ in xrange(len(pyramid))]
        for scale in xrange(len(pyramid)): 
            
            X = pyramid[scale]
            img_row = np.shape(X)[0]
            img_col = np.shape(X)[1]
            
            if img_row < etc.img_size_12 or img_col < etc.img_size_12:
                break
            if etc.img_size_12 * (etc.downscale ** scale) < etc.face_minimum:
                continue
            
            win_list = view_as_windows(X, (etc.img_size_12, etc.img_size_12, etc.input_channel), etc.window_stride) 
            win_col = np.shape(win_list)[1]
            win_list = np.reshape(win_list, (-1, etc.input_channel * etc.img_size_12 * etc.img_size_12))
            result = h_fc2_12.eval(feed_dict={x_12:win_list})
            result_id = np.where(result > thr_12)[0]
            
            detected_box = [0 for _ in xrange(len(result_id))]
            for id_, rid in enumerate(result_id):
                lt_x = (rid % win_col) * etc.window_stride
                lt_y = (rid / win_col) * etc.window_stride
                rb_x = lt_x + etc.img_size_12
                rb_y = lt_y + etc.img_size_12

                lt_x *= etc.downscale ** scale
                lt_y *= etc.downscale ** scale
                rb_x *= etc.downscale ** scale
Пример #42
0
def test_view_as_windows_window_not_tuple():

    A = np.arange(10)
    view_as_windows(A, [2])
Пример #43
0
def testImage(imagePath,
              decisionThreshold=cfg.decision_threshold,
              applyNMS=True):

    file = open(cfg.modelPath, 'rb')
    svc = pickle.load(file)

    image = io.imread(imagePath, as_grey=True)
    image = util.img_as_ubyte(
        image)  #Read the image as bytes (pixels with values 0-255)

    rows, cols = image.shape
    pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor))

    scale = 0
    boxes = None
    scores = None

    for p in pyramid[0:]:
        #We now have the subsampled image in p

        #Add padding to the image, using reflection to avoid border effects
        if cfg.padding > 0:
            p = pad(p, cfg.padding, 'reflect')

        try:
            views = view_as_windows(p, cfg.window_shape, step=cfg.window_step)
        except ValueError:
            #block shape is bigger than image
            break

        num_rows, num_cols, width, height = views.shape
        for row in range(0, num_rows):
            for col in range(0, num_cols):

                #Get current window
                subImage = views[row, col]
                #Extract features
                feats = feature_extractor.extractFeatures(subImage)
                #Obtain prediction score
                # print(feats)
                # print(feats[0].shape)
                # print(len(feats))
                decision_func = svc.decision_function(
                    np.reshape(feats, (1, -1)))

                if decision_func > decisionThreshold:
                    # Pedestrian found!
                    h, w = cfg.window_shape
                    scaleMult = math.pow(cfg.downScaleFactor, scale)

                    x1 = int(scaleMult * (col * cfg.window_step - cfg.padding +
                                          cfg.window_margin))
                    y1 = int(scaleMult * (row * cfg.window_step - cfg.padding +
                                          cfg.window_margin))
                    x2 = int(x1 + scaleMult * (w - 2 * cfg.window_margin))
                    y2 = int(y1 + scaleMult * (h - 2 * cfg.window_margin))

                    bbox = (x1, y1, x2, y2)
                    score = decision_func[0]

                    if boxes is not None:
                        boxes = np.vstack((bbox, boxes))
                        scores = np.hstack((score, scores))
                    else:
                        boxes = np.array([bbox])
                        scores = np.array([score])
        scale += 1

    if applyNMS:
        #From all the bounding boxes that are overlapping, take those with maximum score.
        boxes, scores = nms.non_max_suppression_fast(boxes, scores,
                                                     cfg.nmsOverlapThresh)

    return boxes, scores
Пример #44
0
def compute_Iw(I0, I1, Id, patch_size=None, mode='constant', isophase=np.pi):
    '''Computes weighted sum of image pair (I0,I1).

    Parameters
    ----------
    I0 : array_like
        1st of pair of diagonal images (relative phase cycle of 0).
    I1 : array_like
        2nd of pair of diagonal images (relative phase cycle of 180
        deg).
    Id : array_like
        result of regularized direct solution.
    patch_size : tuple, optional
        size of patches in pixels (x, y).  Defaults to (5, 5).
    mode : {'contant', 'edge'}, optional
        mode of numpy.pad. Probably choose 'constant' or 'edge'.
    isophase : float
        Only neighbours with isophase max phase difference contribute.

    Returns
    -------
    Iw : array_like
        The weighted sum of image pair (I0,I1), equation [14]

    Notes
    -----
    Image pair (I0,I1) are phase cycled bSSFP images that are
    different by 180 degrees.  Id is the image given by the direct
    method (Equation [13]) after regularization by the complex sum.
    This function solves for the weights by regional differential
    energy minimization.  The 'regional' part means that the image is
    split into patches of size patch_size with edge boundary
    conditions (pads with the edge values given by mode option).  The
    weighted sum of the image pair is returned.

    The isophase does not appear in the paper, but appears in Hoff's
    MATLAB code.  It appears that we only want to consider pixels in
    the patch that have similar tissue properties - in other words,
    have similar phase.  The default isophase is pi as in Hoff's
    implementation.

    This function implements Equations [14,18], or steps 4--5 from
    Fig. 2 in [1]_.
    '''

    # Make sure we have a patch size
    if patch_size is None:
        patch_size = (5, 5)

    # Expressions for the numerator and denominator
    numerator = np.conj(I1 - Id) * (I1 - I0) + np.conj(I1 - I0) * (I1 - Id)
    den = np.conj(I0 - I1) * (I0 - I1)

    # We'll have trouble with a 1d input if we don't do this
    numerator = np.atleast_2d(numerator)
    den = np.atleast_2d(den)

    # Pad the image so we can generate patches where we need them
    edge_pad = [int(p / 2) for p in patch_size]
    numerator = np.pad(numerator, pad_width=edge_pad, mode=mode)
    den = np.pad(den, pad_width=edge_pad, mode=mode)

    # Separate out into patches of size patch_size
    numerator_patches = view_as_windows(numerator, patch_size)
    den_patches = view_as_windows(den, patch_size)

    # Make sure the phase difference is below a certan bound to
    # include point in weights
    mask = mask_isophase(numerator_patches, patch_size, isophase)
    numerator_patches *= mask
    den_patches *= mask

    numerator_weights = np.sum(numerator_patches, axis=(-2, -1))
    den_weights = np.sum(den_patches, axis=(-2, -1))

    # Equation [18]
    weights = numerator_weights / (2 * den_weights + np.finfo(float).eps)

    # Find Iw, the weighted sum of image pair (I0,I1), equation [14]
    Iw = I0 * weights + I1 * (1 - weights)
    return Iw
Пример #45
0
kernel_params = []
for theta in (0, 1, 2, 3, 4, 5, 6, 7):
    theta = theta / 4.0 * np.pi
    for frequency in (0.1, 0.4):
        kernel = gabor_kernel(frequency, theta=theta)
        params = "theta=%d,\nfrequency=%.2f" % (theta * 180 / np.pi, frequency)
        kernel_params.append(params)
        # Save kernel and the power image for each image
        results.append((kernel, [power(image_gray, kernel)]))

output = power(image_gray, kernel)
# astro = color.rgb2gray(data.astronaut())
astro = output

# -- filterbank1 on original image
patches3 = view_as_windows(astro, patch_shape)
patches1 = patches3.reshape(-1, patch_shape[0] * patch_shape[1])[::8]
fb3, _ = kmeans2(patches1, n_filters, minit="points")
fb1 = fb3.reshape((-1,) + patch_shape)
fb1_montage = montage2d(fb1, rescale_intensity=True)

# -- filterbank2 LGN-like image
astro_dog = ndi.gaussian_filter(astro, 0.5) - ndi.gaussian_filter(astro, 1)
patches2 = view_as_windows(astro_dog, patch_shape)
patches2 = patches2.reshape(-1, patch_shape[0] * patch_shape[1])[::8]
fb2, _ = kmeans2(patches2, n_filters, minit="points")
fb2 = fb2.reshape((-1,) + patch_shape)
fb2_montage = montage2d(fb2, rescale_intensity=True)

# --
fig, axes = plt.subplots(2, 2, figsize=(7, 6))
Пример #46
0
for voice in all_voice_files:
    print("Sample", indexer, "|", "Mean:", np.mean(voice), "|", "Max:", np.max(voice), "|", \
          "Min:", np.min(voice), "|", "Std Dev:", np.std(voice))
    indexer += 1
"""
Find all 200ms samples that have clear audio data in them
"""
#in each sample, find 200ms chunks that have a mean over the total average + standard deviation
window_shape = 48000 / 5

voice_data = []
voice_labels = []
voice_number = 0

for voice in all_voice_files:
    positive_full_array = view_as_windows(np.absolute(voice), window_shape)
    positive_full_array = positive_full_array[::int(
        window_shape /
        2)]  #keep every nth row, where n is window_shape/2 (For some overlap)
    temp_full_array = view_as_windows(voice, window_shape)
    temp_full_array = temp_full_array[::int(window_shape / 2)]

    for window_index in range(len(temp_full_array)):
        if np.mean(positive_full_array[window_index]) > (np.mean(voice) +
                                                         np.std(voice)):
            voice_data.append(temp_full_array[window_index])
            voice_labels.append(voice_number)

    voice_number += 1

voice_data = np.array(voice_data)
Пример #47
0
import numpy as np
from skimage.util.shape import view_as_windows
A = np.arange(4 * 4 * 4).reshape(4, 4, 4)
B = np.arange(10).reshape(10, 1)
print(B)
C = B[0:9]
print(B[9])

print(A[1, 0, 0])
window_shape = (1, 2, 2)
B = view_as_windows(A, window_shape)
B = np.reshape(B, (9, 4, 2, 2))
print(B.shape)
print(B)
Пример #48
0
def convertFile(inFile, outFile):

    # Open file and extract events
    oldFile = h5.File(inFile)
    newFile = h5.File(outFile, "w")

    ECAL = oldFile["ECAL"][()]
    HCAL = oldFile["HCAL"][()]

    # copy ECAL and HCAL cell info to new file
    newFile.create_dataset("ECAL", data=ECAL, compression='gzip')
    newFile.create_dataset("HCAL", data=HCAL, compression='gzip')

    # if 'GAN' in inFile: # match Geant units
    # ECAL = ECAL/100
    # HCAL = HCAL/100

    # copy over all other existing arrays from the old file
    for key in oldFile.keys():
        if key in ['ECAL', 'HCAL']: continue
        newFile.create_dataset(key, data=oldFile[key][()], compression='gzip')
    oldFile.close()

    # Calorimeter total energy and number of hits
    ECAL_E = np.sum(np.sum(np.sum(ECAL, axis=1), axis=1), axis=1)
    ECAL_nHits = np.sum(np.sum(np.sum(ECAL > 0.1, axis=1), axis=1), axis=1)
    newFile.create_dataset("ECAL_E", data=ECAL_E, compression='gzip')
    newFile.create_dataset("ECAL_nHits", data=ECAL_nHits, compression='gzip')

    HCAL_E = np.sum(np.sum(np.sum(HCAL, axis=1), axis=1), axis=1)
    HCAL_nHits = np.sum(np.sum(np.sum(HCAL > 0.1, axis=1), axis=1), axis=1)
    newFile.create_dataset("HCAL_E", data=HCAL_E, compression='gzip')
    newFile.create_dataset("HCAL_nHits", data=HCAL_nHits, compression='gzip')

    # Ratio of HCAL/ECAL energy, and other ratios
    newFile.create_dataset("HCAL_ECAL_ERatio",
                           data=safeDivide(HCAL_E, ECAL_E),
                           compression='gzip')
    newFile.create_dataset("HCAL_ECAL_nHitsRatio",
                           data=safeDivide(HCAL_nHits, ECAL_nHits),
                           compression='gzip')
    ECAL_E_firstLayer = np.sum(np.sum(ECAL[:, :, :, 0], axis=1), axis=1)
    HCAL_E_firstLayer = np.sum(np.sum(HCAL[:, :, :, 0], axis=1), axis=1)
    newFile.create_dataset("ECAL_ratioFirstLayerToTotalE",
                           data=safeDivide(ECAL_E_firstLayer, ECAL_E),
                           compression='gzip')
    newFile.create_dataset("HCAL_ratioFirstLayerToTotalE",
                           data=safeDivide(HCAL_E_firstLayer, HCAL_E),
                           compression='gzip')
    ECAL_E_secondLayer = np.sum(np.sum(ECAL[:, :, :, 1], axis=1), axis=1)
    HCAL_E_secondLayer = np.sum(np.sum(HCAL[:, :, :, 1], axis=1), axis=1)
    newFile.create_dataset("ECAL_ratioFirstLayerToSecondLayerE",
                           data=safeDivide(ECAL_E_firstLayer,
                                           ECAL_E_secondLayer),
                           compression='gzip')
    newFile.create_dataset("HCAL_ratioFirstLayerToSecondLayerE",
                           data=safeDivide(HCAL_E_firstLayer,
                                           HCAL_E_secondLayer),
                           compression='gzip')

    # ECAL moments
    ECALprojX = np.sum(np.sum(ECAL, axis=3), axis=2)
    ECALprojY = np.sum(np.sum(ECAL, axis=3), axis=1)
    ECALprojZ = np.sum(np.sum(ECAL, axis=1), axis=1)  # z = direction into calo
    totalE = np.sum(ECALprojX, axis=1)
    nEvents, ECAL_sizeX, ECAL_sizeY, ECAL_sizeZ = ECAL.shape
    # ECAL_midX = (ECAL_sizeX-1)/2
    # ECAL_midY = (ECAL_sizeY-1)/2
    # ECAL_midZ = (ECAL_sizeZ-1)/2
    ECAL_midX = np.zeros(nEvents)
    ECAL_midY = np.zeros(nEvents)
    ECAL_midZ = np.zeros(nEvents)
    for i in range(6):
        relativeIndices = np.tile(np.arange(ECAL_sizeX), (nEvents, 1))
        moments = np.power(
            (relativeIndices.transpose() - ECAL_midX).transpose(), i + 1)
        ECAL_momentX = safeDivide(umath.inner1d(ECALprojX, moments), totalE)
        if i == 0: ECAL_midX = ECAL_momentX.transpose()
        newFile.create_dataset("ECALmomentX" + str(i + 1),
                               data=ECAL_momentX,
                               compression='gzip')
    for i in range(6):
        relativeIndices = np.tile(np.arange(ECAL_sizeY), (nEvents, 1))
        moments = np.power(
            (relativeIndices.transpose() - ECAL_midY).transpose(), i + 1)
        ECAL_momentY = safeDivide(umath.inner1d(ECALprojY, moments), totalE)
        if i == 0: ECAL_midY = ECAL_momentY.transpose()
        newFile.create_dataset("ECALmomentY" + str(i + 1),
                               data=ECAL_momentY,
                               compression='gzip')
    for i in range(6):
        relativeIndices = np.tile(np.arange(ECAL_sizeZ), (nEvents, 1))
        moments = np.power(
            (relativeIndices.transpose() - ECAL_midZ).transpose(), i + 1)
        ECAL_momentZ = safeDivide(umath.inner1d(ECALprojZ, moments), totalE)
        if i == 0: ECAL_midZ = ECAL_momentZ.transpose()
        newFile.create_dataset("ECALmomentZ" + str(i + 1),
                               data=ECAL_momentZ,
                               compression='gzip')

    # HCAL moments
    HCALprojX = np.sum(np.sum(HCAL, axis=3), axis=2)
    HCALprojY = np.sum(np.sum(HCAL, axis=3), axis=1)
    HCALprojZ = np.sum(np.sum(HCAL, axis=1), axis=1)
    totalE = np.sum(HCALprojX, axis=1)
    HCAL_sizeX, HCAL_sizeY, HCAL_sizeZ = HCAL[0].shape
    # HCAL_midX = (HCAL_sizeX-1)/2
    # HCAL_midY = (HCAL_sizeY-1)/2
    # HCAL_midZ = (HCAL_sizeZ-1)/2
    HCAL_midX = np.zeros(nEvents)
    HCAL_midY = np.zeros(nEvents)
    HCAL_midZ = np.zeros(nEvents)
    for i in range(6):
        relativeIndices = np.tile(np.arange(HCAL_sizeX), (nEvents, 1))
        moments = np.power(
            (relativeIndices.transpose() - HCAL_midX).transpose(), i + 1)
        HCAL_momentX = safeDivide(umath.inner1d(HCALprojX, moments), totalE)
        if i == 0: HCAL_midX = HCAL_momentX.transpose()
        newFile.create_dataset("HCALmomentX" + str(i + 1),
                               data=HCAL_momentX,
                               compression='gzip')
    for i in range(6):
        relativeIndices = np.tile(np.arange(HCAL_sizeY), (nEvents, 1))
        moments = np.power(
            (relativeIndices.transpose() - HCAL_midY).transpose(), i + 1)
        HCAL_momentY = safeDivide(umath.inner1d(HCALprojY, moments), totalE)
        if i == 0: HCAL_midY = HCAL_momentY.transpose()
        newFile.create_dataset("HCALmomentY" + str(i + 1),
                               data=HCAL_momentY,
                               compression='gzip')
    for i in range(6):
        relativeIndices = np.tile(np.arange(HCAL_sizeZ), (nEvents, 1))
        moments = np.power(
            (relativeIndices.transpose() - HCAL_midZ).transpose(), i + 1)
        HCAL_momentZ = safeDivide(umath.inner1d(HCALprojZ, moments), totalE)
        if i == 0: HCAL_midZ = HCAL_momentZ.transpose()
        newFile.create_dataset("HCALmomentZ" + str(i + 1),
                               data=HCAL_momentZ,
                               compression='gzip')

    # R9
    ECAL_z = np.sum(ECAL, axis=3)  # sum in z
    R9 = [
        view_as_windows(event, (3, 3)).sum(axis=(-2, -1)).max() /
        event.sum() if event.sum() > 0 else 0 for event in ECAL_z
    ]
    newFile.create_dataset("R9", data=np.array(R9), compression='gzip')

    newFile.close()
def rms_profile(my_signal, w_window, w_move, method=1):
    """
    Compute the rms profile for a sampled waveform through sliding windows.

    Given a sampled waveform, the correspoding rms profile is computed through sliding windows with user-specified length and
    time resolution. Two computation methods are implemented in this fuction: method 1 is a vectorized version using window
    views provided in the library skimage and, therefore, present high speed optimization; on the other hand, method 2 is a
    naive implementation through Python loops. This is the most general function for rms profile computation.

    Parameters
    ----------
    my_signal: 1D numpy array
        Input sampled waveform.
    w_window: integer
        Length of each sliding window, in samples. In general, it should represent an integer number of full cycles.
    w_move: integer
        Step size between successive sliding windows, in samples (i.e., it represents the time resolution of the output rms
        profile).
    method: integer (either 1 or 2) [optional; default: 1]
        Selector for the computation method employed in obtaining the rms profile. Option 1 is a vectorized version where
        Python loops are avoided, so that it has a better computational performance.

    Returns
    -------
    rms_profile: 1D numpy array
        Output rms profile correspoding to the input sampled waveform.
    idx_rms_profile: 1D numpy array
        Indices corresponding to the rms update instants.

    Latest update: Jul 20, 2019 by Alvaro Furlani Bastos
    """

    # Compute the square value for each input sample (it avoids repetitive computation when adjacent windows overlap)
    my_signal_2 = my_signal**2
    w_move = int(w_move)

    # Method 1: using views of rolling windows (package scikit-image)
    if method == 1:

        # Create array with window views and compute the rms value for each window
        array_windows = view_as_windows(my_signal_2, window_shape=(w_window,), step=w_move)
        rms_profile = np.sqrt(np.mean(array_windows, axis=1))

    # Method 2: naive implementation through loops
    elif method == 2:

        # Initialize array for storing the rms profile
        rms_profile = []

        # Range of indices for the currently selected sliding window
        range_aux = np.arange(0, w_window)

        # Run through the entire signal and store the rms values
        while len(my_signal) > range_aux[-1]:
            rms_profile.append(np.sqrt(np.mean(my_signal_2[range_aux])))
            range_aux += w_move

    # Determine the time indices for each rms update operation
    idx_rms_profile = np.arange(w_window-1, w_window+(len(rms_profile)-1)*w_move, w_move)

    # Return the desired profiles (as 1D numpy arrays)
    return np.array(rms_profile), idx_rms_profile
def gsn(data_path,
        data_name,
        tail,
        fs,
        duration,
        overlap,
        channel,
        sample_ratio,
        result_path,
        randseed,
        bad_channel=None,
        bad_sample_ratio=None,
        packet=1,
        regularizer_weight=1.0,
        batch_size=128,
        epochs=1200,
        harmonic_wavelet_interval=16,
        loss_weight_rate=32):

    #%% 1 Load data and prepare file and folder names

    data_raw = hdf5storage.loadmat(data_path + data_name + tail)
    data_all = data_raw[data_name]
    del data_raw

    if channel == 'all':
        channel = np.arange(0, data_all.shape[1])

    channel_num = len(channel)
    channel_str_abbr = tidy_name(abbr(channel))

    if bad_channel is None:

        result_folder = data_name + '_' + str(duration) + '_' + str(packet) + '_[' + channel_str_abbr + ']_' + \
                        '%.2f' % sample_ratio + '_' + str(randseed) + \
                        '_rw_' + str(regularizer_weight) + '_bs_' + str(batch_size) + '_epo_' + str(epochs) + \
                        '_hw_interval_' + str(hw_interval) + \
                        '_lw_rate_' + '%03d' % loss_weight_rate + '/'

        result_file = data_name + '_' + str(duration) + '_' + str(packet) + '_[' + channel_str_abbr + ']_' + \
                      '%.2f' % sample_ratio + '_' + str(randseed) + \
                      '_rw_' + str(regularizer_weight) + '_bs_' + str(batch_size) + '_epo_' + str(epochs) + \
                      '_hw_interval_' + str(hw_interval) + \
                      '_lw_rate_' + '%03d' % loss_weight_rate
    else:
        bad_channel_str = [str(i) for i in bad_channel]
        bad_channel_str_stack = '_'.join(bad_channel_str)
        bad_sample_ratio_str = ['%.2f' % i for i in bad_sample_ratio]
        bad_sample_ratio_str_stack = '_'.join(bad_sample_ratio_str)

        result_folder = data_name + '_' + str(duration) + '_' + str(packet) + '_[' + channel_str_abbr + ']_' + \
                        '%.2f' % sample_ratio + '_' + str(randseed) + \
                        '_rw_' + str(regularizer_weight) + '_bs_' + str(batch_size) + '_epo_' + str(epochs) + \
                        '_hw_interval_' + str(hw_interval) + \
                        '_lw_rate_' + '%03d' % loss_weight_rate + \
                        '__[' + bad_channel_str_stack + ']_' + bad_sample_ratio_str_stack + '/'

        result_file = data_name + '_' + str(duration) + '_' + str(packet) + '_[' + channel_str_abbr + ']_' + \
                      '%.2f' % sample_ratio + '_' + str(randseed) + \
                      '_rw_' + str(regularizer_weight) + '_bs_' + str(batch_size) + '_epo_' + str(epochs) + \
                      '_hw_interval_' + str(hw_interval) + \
                      '_lw_rate_' + '%03d' % loss_weight_rate + \
                      '__[' + bad_channel_str_stack + ']_' + bad_sample_ratio_str_stack

    create_folder(result_path + result_folder)
    channel_str = ['ch %d' % (i + 1) for i in channel]

    #%% 2 Preprocess data

    data_all = data_all[:, channel]
    data_split = view_as_windows(np.ascontiguousarray(data_all),
                                 (duration, data_all.shape[1]),
                                 duration - overlap)
    data_split = np.squeeze(data_split, axis=1)
    slice_num = data_split.shape[0]  # number of sections, split by duration
    print('Shape of data_split: ', data_split.shape)
    data_split_offset = np.nanmean(data_split, axis=1)
    data_split_norm = np.zeros(data_split_offset.shape)
    data_split_normalized = np.zeros(data_split.shape)

    for slice in range(slice_num):
        data_split_norm[slice] = np.nanmax(
            np.abs(data_split[slice] -
                   data_split_offset[slice].reshape((1, channel_num))),
            axis=0)

        data_split_normalized[slice] = np.true_divide(
            data_split[slice] - data_split_offset[slice].reshape(
                (1, channel_num)), data_split_norm[slice].reshape(
                    (1, channel_num)))

    data_hat = np.zeros(data_split.shape, dtype=np.complex128)
    data_masked_ignore_zero_all = np.zeros(data_split.shape)
    mask_matrix_all = np.zeros(data_split.shape)
    weights_complex = np.zeros(data_split.shape, dtype=np.complex128)
    recon_error_time_domain = np.zeros([slice_num, len(channel)])
    recon_error_freq_domain = np.zeros([slice_num, len(channel)])

    for slice in range(slice_num):
        start_time = time.time()
        data_time = data_split_normalized[slice]
        print('shape of data:\n', data_time.shape)
        dt = 1. / fs
        t = np.arange(0., duration / fs, dt)

        for f in range(len(channel)):
            fig = plt.figure(figsize=(18, 4))
            plt.plot(t, data_time[:, f])
            ax = plt.gca()
            ax.tick_params(axis='both', which='major', labelsize=12)
            ax.tick_params(axis='both', which='minor', labelsize=12)
            plt.legend([channel_str[f]], loc=1, fontsize=12)
            plt.xlabel('Time (s)', fontsize=12)
            plt.ylabel(data_unit(data_name), fontsize=12)
            plt.xlim(0, max(t))
            # plt.show()
            plt.tight_layout()

            result_folder_signal = 'signal_%02d/' % (channel[f] + 1)
            create_folder(result_path + result_folder + result_folder_signal)
            fig.savefig(result_path + result_folder + result_folder_signal +
                        'signal_%d_slice-%02d.png' %
                        (channel[f] + 1, slice + 1))
            plt.close()
            time.sleep(0.1)

        fig = plt.figure(figsize=(18, 4))
        plt.plot(t, data_time)
        # matplotlib.rcParams.update({'font.size': 12})
        ax = plt.gca()
        ax.tick_params(axis='both', which='major', labelsize=12)
        ax.tick_params(axis='both', which='minor', labelsize=12)
        # matplotlib.rc('xtick', labelsize=12)
        # matplotlib.rc('ytick', labelsize=12)
        plt.legend(channel_str, loc=1, fontsize=12)
        plt.xlabel('Time (s)', fontsize=12)
        plt.ylabel(data_unit(data_name), fontsize=12)
        plt.xlim(0, max(t))
        plt.tight_layout()
        # plt.show()
        fig.savefig(result_path + result_folder + 'signal_all_slice-%02d.png' %
                    (slice + 1))
        time.sleep(0.1)

        plt.close('all')

        #%% 3 Generate mask matrix

        mask_matrix = ~np.isnan(data_time) * 1

        np.random.seed(randseed + slice)  # generate random seed

        # if packet > 1:
        #     remain = np.mod(duration, packet)
        #     if bad_channel is None:
        #         mask_matrix_condensed = np.random.choice([0, 1],
        #                                                  size=(data_time.shape[0] // packet, data_time.shape[1]),
        #                                                  p=[1 - sample_ratio, sample_ratio])
        #         mask_matrix = np.vstack(
        #             (np.repeat(mask_matrix_condensed, packet, axis=0), np.ones((remain, data_time.shape[1]))))
        #     else:
        #         mask_matrix_condensed = np.random.choice([0, 1],
        #                                                  size=(data_time.shape[0] // packet, data_time.shape[1]),
        #                                                  p=[1 - sample_ratio, sample_ratio])
        #         for b in range(len(bad_channel)):
        #             mask_col = np.random.choice([0, 1], size=(data_time.shape[0] // packet),
        #                                         p=[1 - bad_sample_ratio[b], bad_sample_ratio[b]])
        #             mask_matrix_condensed[:, bad_channel[b]] = mask_col
        #         mask_matrix = np.vstack(
        #             (np.repeat(mask_matrix_condensed, packet, axis=0), np.ones((remain, data_time.shape[1]))))
        # else:
        #     if bad_channel is None:
        #         mask_matrix = np.random.choice([0, 1], size=data_time.shape, p=[1 - sample_ratio, sample_ratio])
        #     else:
        #         mask_matrix = np.random.choice([0, 1], size=data_time.shape, p=[1 - sample_ratio, sample_ratio])
        #         for b in range(len(bad_channel)):
        #             mask_col = np.random.choice([0, 1], size=(data_time.shape[0]),
        #                                         p=[1 - bad_sample_ratio[b], bad_sample_ratio[b]])
        #             mask_matrix[:, bad_channel[b]] = mask_col

        mask_matrix_all[slice] = mask_matrix
        print('shape of mask_matrix:', mask_matrix.shape, '\n')

        #%% 4 Make masked data

        data_masked = np.nan_to_num(data_time) * mask_matrix

        #%% 5 Generate basis matrix

        basis_folder = 'basis_matrix/'
        basis_file = 'basis_' + str(duration) + '.npy'

        print('\nGenerating basis matrix...\n')
        m = 0
        n = hw_interval
        basis_matrix = harmonic_wavelet(duration, m, n)
        # basis_matrix = dft2d(duration)
        create_folder(result_path + basis_folder)
        print('\nShape of basis_matrix:\n', basis_matrix.shape, '\n')
        print('\nBasis_matrix:\n', basis_matrix, '\n')

        #%% 6 Construct neural network

        data_input1 = np.real(basis_matrix)
        data_input2 = np.imag(basis_matrix)
        data_input3 = mask_matrix
        data_output1 = np.real(data_masked.astype(complex))
        data_output2 = np.imag(data_masked.astype(complex))

        # Expand input dimension for CNN
        data_input1 = np.expand_dims(data_input1, 0)
        data_input1 = np.expand_dims(data_input1, -1)

        data_input2 = np.expand_dims(data_input2, 0)
        data_input2 = np.expand_dims(data_input2, -1)

        data_input3 = np.expand_dims(data_input3, 0)
        data_input3 = np.expand_dims(data_input3, -1)

        data_output1 = np.expand_dims(data_output1, 0)
        data_output1 = np.expand_dims(data_output1, -1)

        data_output2 = np.expand_dims(data_output2, 0)
        data_output2 = np.expand_dims(data_output2, -1)

        np.random.seed(randseed)
        tf.set_random_seed(randseed)

        class Regularizer(object):
            """Regularizer base class.
            """
            def __call__(self, x):
                return 0.

            @classmethod
            def from_config(cls, config):
                return cls(**config)

        class GroupSparseRegularizer(Regularizer):
            def __init__(self, regularizer_weight):
                self.regularizer_weight = regularizer_weight

            def __call__(self, weight_matrix):
                return self.regularizer_weight * K.sum(
                    tf.norm(weight_matrix, ord='euclidean',
                            axis=3))  # Frobenius norm

            def get_config(self):
                return {'regularizer_weight': float(self.regularizer_weight)}

        def model(data_input1, data_input2, data_input3, data_output1,
                  data_output2, duration, num_channel):

            # input layer
            basis_real = Input(shape=(duration, duration, 1),
                               name='Basis_real')
            basis_imag = Input(shape=(duration, duration, 1),
                               name='Basis_imag')

            # Convolutional layer
            coeff_real = Conv2D(
                num_channel,
                kernel_size=(1, duration),
                activation='linear',
                input_shape=(duration, duration, 1),
                name='Coeff_real',
                use_bias=False,
                # kernel_regularizer=GroupSparseRegularizer(regularizer_weight=regularizer_weight))
                kernel_regularizer=regularizers.l1(
                    regularizer_weight))  # regularizers.l1(0.1)
            coeff_imag = Conv2D(
                num_channel,
                kernel_size=(1, duration),
                activation='linear',
                input_shape=(duration, duration, 1),
                name='Coeff_imag',
                use_bias=False,
                # kernel_regularizer=GroupSparseRegularizer(regularizer_weight=regularizer_weight))
                kernel_regularizer=regularizers.l1(regularizer_weight))

            mask = Input(shape=(duration, num_channel, 1), name='Mask')

            # Feature maps as intermediate layer (need permute dimensions)
            item_ac = coeff_real(basis_real)
            item_ad = coeff_imag(basis_real)
            item_bc = coeff_real(basis_imag)
            item_bd = coeff_imag(basis_imag)

            item_ac_permuted = Lambda(
                lambda x: K.permute_dimensions(x, (0, 1, 3, 2)))(item_ac)
            item_ad_permuted = Lambda(
                lambda x: K.permute_dimensions(x, (0, 1, 3, 2)))(item_ad)
            item_bc_permuted = Lambda(
                lambda x: K.permute_dimensions(x, (0, 1, 3, 2)))(item_bc)
            item_bd_permuted = Lambda(
                lambda x: K.permute_dimensions(x, (0, 1, 3, 2)))(item_bd)

            # Recovery
            signal_real = Subtract(name='Signal_real')(
                [item_ac_permuted, item_bd_permuted])
            signal_imag = Add(name='Signal_imag')(
                [item_ad_permuted, item_bc_permuted])

            # masking
            signal_real_masked = Multiply(name='Sparse_signal_real')(
                [signal_real, mask])
            signal_imag_masked = Multiply(name='Sparse_signal_imag')(
                [signal_imag, mask])

            model = Model(inputs=[basis_real, basis_imag, mask],
                          outputs=[signal_real_masked, signal_imag_masked])

            return model

        model = model(data_input1, data_input2, data_input3, data_output1,
                      data_output2, duration, data_input3.shape[2])
        model.summary()

        if not os.path.exists(result_path + result_folder + 'model_v6.png'):
            plot_model(model,
                       to_file='model_v6.png',
                       show_shapes=True,
                       show_layer_names=True)

        #%% 7 Solving

        opt = Adam(lr=0.0001)
        ##
        lw_real = K.variable(1.)
        lw_imag = K.variable(1.)
        model.compile(optimizer=opt,
                      loss='mean_squared_error',
                      loss_weights=[lw_real, lw_imag],
                      metrics=['mae'])

        class LossWeightsScheduler(Callback):
            def __init__(self, lw_real, lw_imag, epochs, loss_weight_rate):
                self.lw_real = lw_real
                self.lw_imag = lw_imag
                self.epochs = epochs
                self.loss_weight_rate = loss_weight_rate

            def on_epoch_begin(self, epoch, logs={}):
                if epoch <= self.epochs:
                    K.set_value(
                        self.lw_real,
                        math.pow(
                            2,
                            (epoch / self.epochs * self.loss_weight_rate + 1)))
                    K.set_value(
                        self.lw_imag,
                        math.pow(
                            2,
                            (epoch / self.epochs * self.loss_weight_rate + 1)))

        class LossHistory(Callback):
            def on_train_begin(self, logs={}):
                self.losses = []

            def on_batch_end(self, batch, logs={}):
                self.losses.append(logs.get('loss'))

        loss_history = LossHistory()

        weights_folder = 'weights_history/'
        weights_slice_folder = 'slice_%02d/' % (slice + 1)
        create_folder(result_path + result_folder + weights_folder +
                      weights_slice_folder)

        file_path_of_weights = result_path + result_folder + weights_folder + weights_slice_folder + \
                               'weights_slice-%02d-{epoch:03d}.hdf5' % (slice + 1)
        checkpoint = ModelCheckpoint(file_path_of_weights,
                                     monitor='loss',
                                     verbose=0,
                                     save_best_only=False,
                                     save_weights_only=True,
                                     mode='auto',
                                     period=50)

        history = model.fit(x=[data_input1, data_input2, data_input3],
                            y=[data_output1, data_output2],
                            epochs=epochs,
                            batch_size=batch_size,
                            callbacks=[
                                loss_history,
                                LossWeightsScheduler(lw_real, lw_imag, epochs,
                                                     loss_weight_rate),
                                checkpoint
                            ])

        #%% 8 Check results

        # Plot training history
        print(history.history.keys())
        # print(len(loss_history.losses))

        # summarize history for loss
        fig = plt.figure(figsize=(6, 4))
        plt.semilogy(history.history['Sparse_signal_real_loss'],
                     linestyle='-',
                     color='red')
        plt.semilogy(history.history['Sparse_signal_imag_loss'],
                     linestyle='--',
                     color='blue')
        ax = plt.gca()
        ax.tick_params(axis='both', which='major', labelsize=12)
        ax.tick_params(axis='both', which='minor', labelsize=12)
        # plt.title('Model loss')
        plt.xlabel('Epoch', fontsize=12)
        plt.ylabel('Loss', fontsize=12)
        plt.legend(['real part', 'imaginary part'],
                   loc='upper right',
                   fontsize=12)
        plt.tight_layout()
        plt.show()
        file_name = result_path + result_folder + 'training_history_slice-%02d' % (
            slice + 1)
        fig.savefig(file_name + '.png')
        fig.savefig(file_name + '.svg')
        fig.savefig(file_name + '.eps')
        fig.savefig(file_name + '.pdf')
        subprocess.call('C:/Program Files/Inkscape/inkscape.exe ' + file_name +
                        '.svg '
                        '--export-emf=' + file_name + '.emf')
        time.sleep(0.1)

        # Get basis coeffs
        weights_real = np.squeeze(model.get_weights()[0])
        print('shape of weights_real:', weights_real.shape, '\n')

        weights_imag = np.squeeze(model.get_weights()[1])
        print('shape of weights_imag:', weights_imag.shape, '\n')

        weights_complex[slice] = weights_real + weights_imag * 1j
        print('shape of weights_complex:', weights_complex.shape, '\n')

        # Plot heatmap for the real part of coefficients
        fig = plt.figure(figsize=(3, 60))  # figsize=(6, 6)
        ax = sns.heatmap(weights_imag,
                         cmap='coolwarm',
                         square=True,
                         xticklabels=1,
                         yticklabels=128)
        sns.set(font_scale=1.0)
        # cbar_axes = ax.figure.axes[-1]
        # ax.figure.axes[-1].yaxis.label.set_size(12)
        # ax.tick_params(axis='both', which='major', labelsize=12)
        # ax.tick_params(axis='both', which='minor', labelsize=12)
        # plt.title('Imaginary part of basis coefficients')  # , fontsize=12
        # plt.xlabel('Column')
        # plt.ylabel('Row')
        # plt.xticks(np.arange(0, 2048, 32))
        plt.tight_layout()
        # plt.show()
        fig.savefig(result_path + result_folder +
                    'basis_coeffs_signal_all_imag_slice-%02d_heatmap.pdf' %
                    (slice + 1))
        plt.close()
        sns.reset_orig()

        # Plot heatmap for the imaginary part of coefficients for all channels
        fig = plt.figure(figsize=(3, 60))  # figsize=(6, 6)
        ax = sns.heatmap(weights_real,
                         cmap='coolwarm',
                         square=True,
                         xticklabels=1,
                         yticklabels=128)
        # sns.set(font_scale=0.05)
        # cbar_axes = ax.figure.axes[-1]
        # ax.figure.axes[-1].yaxis.label.set_size(12)
        # ax.tick_params(axis='both', which='major', labelsize=12)
        # ax.tick_params(axis='both', which='minor', labelsize=12)
        # plt.title('Real part of basis coefficients')  # , fontsize=12
        # plt.xlabel('Column')
        # plt.ylabel('Row')
        # plt.xticks(np.arange(0, 2048, 32))
        plt.tight_layout()
        # plt.show()
        fig.savefig(result_path + result_folder +
                    'basis_coeffs_signal_all_real_slice-%02d_heatmap.pdf' %
                    (slice + 1))
        plt.close()
        sns.reset_orig()

        # Plot real-part coefficients for all channels
        fig = plt.figure(figsize=(17, 4))
        plt.plot(weights_real)
        ax = plt.gca()
        ax.tick_params(axis='both', which='major', labelsize=12)
        ax.tick_params(axis='both', which='minor', labelsize=12)
        plt.title('Real part', fontsize=12)
        plt.xlabel('Point', fontsize=12)
        plt.ylabel('Amplitude', fontsize=12)
        plt.legend(channel_str, loc=9, ncol=len(channel_str), fontsize=12)
        plt.tight_layout()
        # plt.show()
        fig.savefig(result_path + result_folder +
                    'basis_coeffs_signal_all_real_slice-%02d.png' %
                    (slice + 1))
        time.sleep(0.1)

        # Plot imaginary-part coefficients for all channels
        fig = plt.figure(figsize=(17, 4))
        plt.plot(weights_imag)
        ax = plt.gca()
        ax.tick_params(axis='both', which='major', labelsize=12)
        ax.tick_params(axis='both', which='minor', labelsize=12)
        plt.title('Imaginary part', fontsize=12)
        plt.xlabel('Point', fontsize=12)
        plt.ylabel('Amplitude', fontsize=12)
        plt.legend(channel_str, loc=9, ncol=len(channel_str), fontsize=12)
        plt.tight_layout()
        # plt.show()
        fig.savefig(result_path + result_folder +
                    'basis_coeffs_signal_all_imag_slice-%02d.png' %
                    (slice + 1))
        time.sleep(0.1)

        # Plot coefficients of each channel respectively
        for f in range(len(channel)):
            fig = plt.figure(figsize=(12, 8))

            plt.subplot(2, 1, 1)
            plt.plot(weights_real[:, f])
            ax = plt.gca()
            ax.tick_params(axis='both', which='major', labelsize=12)
            ax.tick_params(axis='both', which='minor', labelsize=12)
            plt.title('Real part', fontsize=12)
            plt.legend([channel_str[f]], loc=1, fontsize=12)
            plt.xlabel('Point', fontsize=12)
            plt.ylabel('Amplitude', fontsize=12)
            plt.xlim(0, duration)

            plt.subplot(2, 1, 2)
            plt.plot(weights_imag[:, f])
            ax = plt.gca()
            ax.tick_params(axis='both', which='major', labelsize=12)
            ax.tick_params(axis='both', which='minor', labelsize=12)
            plt.title('Imaginary part', fontsize=12)
            plt.legend([channel_str[f]], loc=1, fontsize=12)
            plt.xlabel('Point', fontsize=12)
            plt.ylabel('Amplitude', fontsize=12)
            plt.xlim(0, duration)
            # plt.show()
            plt.tight_layout()

            result_folder_signal = 'signal_%02d/' % (channel[f] + 1)
            create_folder(result_path + result_folder + result_folder_signal)
            fig.savefig(result_path + result_folder + result_folder_signal +
                        'basis_coeffs_signal_%d_slice-%02d.png' %
                        (channel[f] + 1, slice + 1))
            plt.close()
            time.sleep(0.1)

        #%% Check reconstructed data

        data_hat_time = np.matmul(basis_matrix, weights_complex[slice])
        data_hat_time = data_hat_time * data_split_norm[
            slice] + data_split_offset[slice]
        data_time = data_time * data_split_norm[slice] + data_split_offset[
            slice]
        data_masked = data_masked * data_split_norm[slice] + data_split_offset[
            slice]

        data_hat[slice] = data_hat_time
        print('shape of data_hat_time:', data_hat_time.shape)

        recon_error_time_domain[slice] = np.linalg.norm((np.real(data_hat_time - data_time)), ord=2, axis=0) / \
                                         np.linalg.norm(np.real(data_time), ord=2, axis=0)
        print('\nrecon_error_time_domain:', recon_error_time_domain[slice])

        #%% Time domain:

        data_masked_ignore_zero = np.array(data_masked)
        data_masked_ignore_zero[data_masked_ignore_zero ==
                                0] = np.nan  # data ignored zero for plotting
        data_masked_ignore_zero_all[slice] = data_masked_ignore_zero

        for f in channel:
            print('\nPlotting signal ' + str(f) + '\n')
            fig = plt.figure(figsize=(17, 4))
            plot_origin = plt.plot(t,
                                   np.real(data_time[:,
                                                     list(channel).index(f)]),
                                   'red')  # xkcd:lightish green
            plot_recover = plt.plot(
                t, np.real(data_hat_time[:, list(channel).index(f)]), 'blue')

            # plot background
            p = 0
            while p < duration:
                if mask_matrix[p, list(channel).index(f)] == 0:
                    plot_section = plt.axvspan(p / fs, (p + packet) / fs,
                                               facecolor='red',
                                               alpha=0.08)
                    p = p + packet
                else:
                    p = p + 1

            ax = plt.gca()
            ax.tick_params(axis='both', which='major', labelsize=12)
            ax.tick_params(axis='both', which='minor', labelsize=12)
            plt.title('Signal %s - Slice %03d - Comparison' %
                      (f + 1, slice + 1),
                      fontsize=12)
            plt.xlabel('Time (sec)', fontsize=12)
            plt.ylabel(data_unit(data_name), fontsize=12)
            plt.legend(['Original', 'Recovered'], fontsize=12)
            plt.xlim(0, max(t))
            plt.grid(True)
            plt.tight_layout()

            result_folder_signal = 'signal_%02d/' % (f + 1)
            create_folder(result_path + result_folder + result_folder_signal)
            fig.savefig(result_path + result_folder + result_folder_signal +
                        'compare_time_signal_' + str(f + 1) +
                        '_slice-%03d.png' % (slice + 1))
            fig.savefig(result_path + result_folder + result_folder_signal +
                        'compare_time_signal_' + str(f + 1) +
                        '_slice-%03d.pdf' % (slice + 1))
            time.sleep(0.1)
            plt.close('all')

        #%% Frequency domain:

        data_freq = np.fft.fft(np.real(data_time), axis=0)
        data_hat_freq = np.fft.fft(np.real(data_hat_time), axis=0)

        recon_error_freq_domain[slice] = np.linalg.norm((data_hat_freq - data_freq), ord=2, axis=0) / \
                                         np.linalg.norm(data_freq, ord=2, axis=0)

        x_axis_freq = np.arange(0, fs, fs / duration)
        for f in channel:
            print('\nPlotting signal ' + str(f) + '\n')
            fig = plt.figure(figsize=(17, 4))
            plt.plot(x_axis_freq, np.abs(data_freq[:,
                                                   list(channel).index(f)]),
                     'red')  # xkcd:lightish green
            plt.plot(x_axis_freq,
                     np.abs(data_hat_freq[:, list(channel).index(f)]), 'blue')
            ax = plt.gca()
            ax.tick_params(axis='both', which='major', labelsize=12)
            ax.tick_params(axis='both', which='minor', labelsize=12)
            plt.title('Signal %s - Slice %03d - Comparison' %
                      (f + 1, slice + 1),
                      fontsize=12)
            plt.xlabel('Frequency (Hz)', fontsize=12)
            plt.ylabel(data_unit(data_name), fontsize=12)
            plt.legend(['Original', 'Recovered'], fontsize=12)
            plt.xlim(0, fs / 2 + 1)
            plt.grid(True)
            plt.tight_layout()

            result_folder_signal = 'signal_%02d/' % (f + 1)
            create_folder(result_path + result_folder + result_folder_signal)
            fig.savefig(result_path + result_folder + result_folder_signal +
                        'compare_freq_signal_' + str(f + 1) +
                        '_slice-%03d.png' % (slice + 1))
            fig.savefig(result_path + result_folder + result_folder_signal +
                        'compare_freq_signal_' + str(f + 1) +
                        '_slice-%03d.pdf' % (slice + 1))
            time.sleep(0.1)
            plt.close('all')

        elapsed_time = time.time() - start_time
        print(
            time.strftime('Elapsed time: %H:%M:%S\n',
                          time.gmtime(elapsed_time)))

    #%% Combine segments

    if overlap == 0:
        linked_data_time = data_split[:, :, :].reshape(-1, data_split.shape[2])

        linked_data_hat_time = data_hat[:, :, :].reshape(-1, data_hat.shape[2])

        linked_data_masked_ignore_zero_all = data_masked_ignore_zero_all[:, :, :].reshape(
            -1, data_masked_ignore_zero_all.shape[2])

        linked_mask_matrix_all = mask_matrix_all[:, :, :].reshape(
            -1, mask_matrix_all.shape[2])

    else:

        linked_data_time = data_split[:,
                                      int(overlap /
                                          2):int(-overlap / 2), :].reshape(
                                              -1, data_split.shape[2])

        linked_data_hat_time = data_hat[:,
                                        int(overlap /
                                            2):int(-overlap / 2), :].reshape(
                                                -1, data_hat.shape[2])

        linked_data_masked_ignore_zero_all = \
            data_masked_ignore_zero_all[:, int(overlap / 2):int(-overlap / 2), :].reshape(-1, data_masked_ignore_zero_all.shape[2])

        linked_mask_matrix_all = mask_matrix_all[:,
                                                 int(overlap /
                                                     2):int(-overlap /
                                                            2), :].reshape(
                                                                -1,
                                                                mask_matrix_all
                                                                .shape[2])

    #%% 9 Save results

    results = {
        'data_split': data_split,
        'data_hat': data_hat,
        'data_masked_ignore_zero_all': data_masked_ignore_zero_all,
        'mask_matrix_all': mask_matrix_all,
        'weights_complex': weights_complex,
        'recon_error_time_domain': recon_error_time_domain,
        'recon_error_freq_domain': recon_error_freq_domain,
        'linked_data_time': linked_data_time,
        'linked_data_hat_time': linked_data_hat_time,
        'linked_data_masked_ignore_zero_all':
        linked_data_masked_ignore_zero_all,
        'linked_mask_matrix_all': linked_mask_matrix_all
    }

    # Collect metrics for parallel plot
    collect_results_folder = 'collect_results/'
    collect_results_file = 'collect_results'
    create_folder(result_path + collect_results_folder)

    recon_error_time_domain_mean_all = float(np.mean(recon_error_time_domain))

    with open(
            result_path + collect_results_folder + collect_results_file +
            '.txt', 'a+') as f:
        f.write('%5.2f, %03d, %03d, %02d, %04d, %7.5f\n' %
                (regularizer_weight, batch_size, harmonic_wavelet_interval,
                 loss_weight_rate, epochs, recon_error_time_domain_mean_all))
    f.close()

    # Save metrics in txt locally
    np.savetxt(result_path + result_folder + result_file + '.txt',
               recon_error_time_domain * 100,
               fmt='%.2f',
               header='row: slice, column: channel')
    print('\nResults metrics saved in: ' + result_file + '.txt')

    # Save results file locally
    with open(result_path + result_folder + result_file + '.pickle',
              'wb') as f:
        pickle.dump(results, f)
    f.close()
    print('\nResults saved in: ' + result_file + '.pickle')

    hdf5storage.savemat(result_path + result_folder + result_file + '.mat',
                        results)
    print('\nResults saved in: ' + result_file + '.mat')

    K.clear_session()
Пример #51
0
        # Remove empty data
        idx = np.logical_or(np.isnan(load), np.isinf(load))
        load[idx] = 0.0
        idx = load < 0.0
        load[idx] = 0.0

        # Get zip for each user in the group
        zipcodes = user_to_zip[num_users_per_grp * (group_idx - 1):num_users_per_grp * (group_idx)]

        print 'calculating xTemp for all zipcodes.'
        xTemp_by_hour = {}
        for h in hours:
            xTemp_by_hour[h] = {}
        for zipcode in df_tempByZip.keys():
            serie = np.array(df_tempByZip[zipcode][:])
            matrixTemp = view_as_windows(serie, window_shape)

            for h in hours:
                xTemp_by_hour[h][int(zipcode)] = matrixTemp[:-h, :]
        print 'finished all xTemp'

        for typeModel in typeModels:
            pathForecast = join(working_dir,
                                typeModel,
                                str(dates[-1].month) + '_' + str(dates[-1].year),
                                'user_grp_' + str(group_idx))

            if not os.path.exists(pathForecast):
                os.makedirs(pathForecast)

            truePath = join(working_dir,
Пример #52
0
def test_view_as_windows_window_too_large():

    A = np.arange(10)
    view_as_windows(A, (11, ))
Пример #53
0
train_images, train_labels, test_images, test_labels, class_list = data.import_data(F.useclasses)
GetKernel(train_images, train_labels, class_list,
          './weight/pca_params_compact_inv.pkl')
'''
fr = open('./weight/pca_params_compact_inv.pkl', 'rb')
pca_params = pickle.load(fr)
fr.close()

k0 = np.array(pca_params['Layer_0/kernel'])
k1 = np.array(pca_params['Layer_1/kernel'])

b1 = pca_params['Layer_1/bias'].astype(np.float32)

#  read image and get features
img = cv2.imread('4.png', 0)
patch = view_as_windows(img, (4, 4), step=(4, 4)).reshape(8, 8, 1 * 4 * 4)
patch = np.dot(patch, np.transpose(k0))
patch = view_as_windows(patch.copy(), (4, 4, 1), step=(4, 4, 1))
patch = patch.reshape(2, 2, s1 * 16)
patch = patch + 1 / np.sqrt(s1 * s2) * b1
patch = np.dot(patch, np.transpose(k1))

#  inverse transforation
ipatch = np.dot(patch, LA.pinv(np.transpose(k1)))
ipatch = ipatch - 1 / np.sqrt(s1 * s2) * b1
ipatch = ipatch.reshape(2, 2, s1, 16)
ipatch = np.moveaxis(ipatch, 2, 3)
xx = np.zeros((8, 8, s1))
for i in range(0, 2):
    for j in range(0, 2):
        for k in range(0, 16):
Пример #54
0
nUsers = words.shape[0]
window_shape = (window_size,)
stepAhead = 24

ini_users=int(sys.argv[1])
end_users = int(sys.argv[2])

sizeSerie = words.shape[1] - (window_size + stepAhead - 1)

# in order to run it in many instances
#meterToXTest = np.empty((nUsers, sizeTest, window_size), dtype=np.int16)
meterToXTest = np.empty((end_users-ini_users, sizeTest, window_size), dtype=np.int16)

for u in xrange(ini_users,end_users):
    serie = words[u, :]
    xLoad = (view_as_windows(serie, window_shape))[:-stepAhead, :]
    yLoad24 = np.empty((serie.size - (window_size + stepAhead - 1), stepAhead), dtype=np.int16)
    yLoad24[:, -1] = serie[(window_size + stepAhead - 1):]
    for i in xrange(stepAhead - 1):
        yLoad24[:, i] = serie[(window_size + i):-(stepAhead - (i + 1))]
    #meterToXTest[u] = xLoad[yTrain_idx == 0, :]
    meterToXTest[u-ini_users] = xLoad[yTrain_idx==0, :]
#-----------------------------
print('removing words from memory')
del words
#-----------------------------
# test
print('starting test...')
#shapeYtest=(nUsers, sizeTest, stepAhead)
shapeYtest=(end_users-ini_users, sizeTest, stepAhead)
meterToYTest_pred=np.empty(shapeYtest,dtype=np.int16)
Пример #55
0
import matplotlib.pyplot as plt

from skimage import data
from skimage import color
from skimage.util.shape import view_as_windows
from skimage.util import montage

np.random.seed(42)

patch_shape = 8, 8
n_filters = 49

astro = color.rgb2gray(data.astronaut())

# -- filterbank1 on original image
patches1 = view_as_windows(astro, patch_shape)
patches1 = patches1.reshape(-1, patch_shape[0] * patch_shape[1])[::8]
fb1, _ = kmeans2(patches1, n_filters, minit='points')
fb1 = fb1.reshape((-1,) + patch_shape)
fb1_montage = montage(fb1, rescale_intensity=True)

# -- filterbank2 LGN-like image
astro_dog = ndi.gaussian_filter(astro, .5) - ndi.gaussian_filter(astro, 1)
patches2 = view_as_windows(astro_dog, patch_shape)
patches2 = patches2.reshape(-1, patch_shape[0] * patch_shape[1])[::8]
fb2, _ = kmeans2(patches2, n_filters, minit='points')
fb2 = fb2.reshape((-1,) + patch_shape)
fb2_montage = montage(fb2, rescale_intensity=True)

# -- plotting
fig, axes = plt.subplots(2, 2, figsize=(7, 6))
Пример #56
0
def testImage(imagePath, decisionThreshold = cfg.decision_threshold, applyNMS=True):

    fileList = os.listdir(cfg.modelRootPath)

    # Filter all model files
    modelsList = filter(lambda element: '.model' in element, fileList)

    # Filter our specific feature method
    currentModel = cfg.model+'_'+cfg.modelFeatures
    currentModelsList = filter(lambda element: currentModel in element, modelsList)

    models = []
    subImages = [] #To save backgorund crops

    for modelname in currentModelsList:

        file = open(cfg.modelRootPath + modelname, 'r')
        svc = pickle.load(file)
        models.append(svc)

        file.close()

    image = io.imread(imagePath, as_grey=True)
    image = util.img_as_ubyte(image) #Read the image as bytes (pixels with values 0-255)

    rows, cols = image.shape
    pyramid = tuple(pyramid_gaussian(image, downscale=cfg.downScaleFactor))

    scale = 0
    boxes = None
    scores = None


    for p in pyramid[0:]:
        #We now have the subsampled image in p
        window_shape = (32,32)

        #Add padding to the image, using reflection to avoid border effects
        if cfg.padding > 0:
            p = pad(p,cfg.padding,'reflect')

        try:
            views = view_as_windows(p, window_shape, step=cfg.window_step)
        except ValueError:
            #block shape is bigger than image
            break

        num_rows, num_cols, width, height = views.shape

        for row in range(0, num_rows):
            for col in range(0, num_cols):
                #Get current window
                subImage = views[row, col]
                # subImages.append(subImage)   #To save backgorund crops: Accumulate them in an array
                #Extract features
                feats = feature_extractor.extractFeatures(subImage)

                #Obtain prediction score for each model
                for model in models:

                    decision_func = model.decision_function(feats)

                    # if decision_func > decisionThreshold:
                    if decision_func > 0.2:  #For bootstrapping
                        # Signal found!
                        h, w = window_shape
                        scaleMult = math.pow(cfg.downScaleFactor, scale)

                        x1 = int(scaleMult * (col*cfg.window_step - cfg.padding + cfg.window_margin))
                        y1 = int(scaleMult * (row*cfg.window_step - cfg.padding + cfg.window_margin))
                        x2 = int(x1 + scaleMult*(w - 2*cfg.window_margin))
                        y2 = int(y1 + scaleMult*(h - 2*cfg.window_margin))

                        #bootstrapping: Save image (if positive)
                        subImages.append(subImage)

                        bbox = (x1, y1, x2, y2)
                        score = decision_func[0]

                        if boxes is not None:
                            boxes = np.vstack((bbox, boxes))
                            scores = np.hstack((score, scores))
                        else:
                            boxes = np.array([bbox])
                            scores = np.array([score])
                        break

        scale += 1


    # To save backgorund crops
    # numSubImages = len(subImages)
    # for x in range(0,10): #Save 10 crops for each background image
    #     randomIndex = random.randint(1,numSubImages-1) #Get a random window index
    #     imageName = imagePath.split('/')  #Working on the crop name...
    #     imageName = imageName[len(imageName)-1]
    #     filename = (imageName[:-4]+'-'+str(x)+'.jpg')
    #     io.imsave('Results/'+filename, subImages[randomIndex])  #Save the crop
    #end To save backgorund crops

    # To save bootstrapping windows
    numSubImages = len(subImages)
    length = min(10, len(subImages))
    for x in range(0,length) : #Save windows with detections (max 10)
        if numSubImages == 1:
            randomIndex = 0
        else:
            randomIndex = random.randint(1, numSubImages-1) #Get a random window index
        imageName = imagePath.split('/')  #Working on the crop name...
        imageName = imageName[len(imageName)-1]
        filename = (imageName[:-4]+'-'+str(x)+'.jpg')
        io.imsave('Bootstrapping/'+filename, subImages[randomIndex])  #Save the crop
    #end To save bootstrapping windows


    if applyNMS:
        #From all the bounding boxes that are overlapping, take those with maximum score.
        boxes, scores = nms.non_max_suppression_fast(boxes, scores, cfg.nmsOverlapThresh)

    return boxes, scores
Пример #57
0
    # im_, gt_ = 'PaviaU', 'PaviaU_gt'
    # im_, gt_ = 'Salinas_corrected', 'Salinas_gt'
    # im_, gt_ = 'Botswana', 'Botswana_gt'
    # im_, gt_ = 'KSC', 'KSC_gt'

    img_path = root + im_ + '.mat'
    gt_path = root + gt_ + '.mat'
    print(img_path)

    p = Processor()
    img, gt = p.prepare_data(img_path, gt_path)
    # Img, Label = Img[:256, :, :], Label[:256, :]
    n_row, n_column, n_band = img.shape
    X_img = minmax_scale(img.reshape(n_row * n_column, n_band)).reshape(
        (n_row, n_column, n_band))
    img_block = view_as_windows(X_img, (7, 7, n_band), step=1)
    # img_block_2 = view_as_windows(X_img, (32, 32, n_band), step=6)
    img_block = img_block.reshape(
        (-1, img_block.shape[-3], img_block.shape[-2], img_block.shape[-1]))
    # img_block_2 = img_block_2.reshape((-1, img_block_2.shape[-3], img_block_2.shape[-2], img_block_2.shape[-1]))

    print('img block shape: ', img_block.shape)

    # img_blocks_nonzero, gt_blocks_nonzero = p.divide_img_blocks(X_img, gt, block_size=(8, 8))
    # img_blocks_nonzero = np.transpose(img_blocks_nonzero, [0, 2, 3, 1]).astype('float32')
    # print(img_blocks_nonzero.shape)

    LR, BATCH_SIZE, EPOCH = 0.0005, 16, 100
    N_BAND = 25
    time_start = time.clock()
    acnn = BS_Net_Conv(LR, BATCH_SIZE, EPOCH, N_BAND)