Example #1
0
    def sliding_concentric_windows(self, img):

        n_rows, n_cols = img.shape[:2]

        y_a, x_a = 5, 5
        y_b, x_b = 11, 11

        new_img = np.zeros((n_rows, n_cols), dtype=np.uint)
        img_a = util.pad(img, ((y_a/2, y_a/2), (x_a/2, x_a/2)), mode='constant')
        img_b = util.pad(img, ((y_b/2, y_b/2), (x_b/2, x_b/2)), mode='constant')

        blocks_a = util.view_as_windows(img_a, (y_a, x_a), step=1)
        blocks_b = util.view_as_windows(img_b, (y_b, x_b), step=1)

        for row in xrange(n_rows):
            for col in xrange(n_cols):
                mean_a = blocks_a[row, col].mean()
                mean_b = blocks_b[row, col].mean()

                r_mean = 0
                if mean_a != 0:
                    r_mean = mean_b/float(mean_a)

                if r_mean > 1.0:
                    new_img[row, col] = 0
                else:
                    new_img[row, col] = 255

        return new_img
def extract_patches(path, numPatchesPerImage, patchSize):

    """
    :param path: path to a RGB fundus image
    :param numPatchesPerImage: number of patches to extract per image
    :param patchSize: patch is nxn size
    :return: patches: matrix with an image patch in each row
    """

    img = load(path)
    img = img[:,:,1]
    #contrast enhancemenet
    img = equalize_adapthist(img)
    windows = view_as_windows(img, (patchSize,patchSize))
    j = 0
    patches = np.zeros((numPatchesPerImage, patchSize*patchSize))
    while(j < numPatchesPerImage):
        
        sx = np.random.randint(0, windows.shape[0] - 1)  
        sy = np.random.randint(0, windows.shape[0] - 1)
        x = (patchSize/2 - 1) + sx
        y = (patchSize/2 - 1) + sy
        r = (img.shape[0]/2) - 1

        if np.sqrt((x - r) ** 2 + (y - r) **2 ) < r:
            patch = windows[sx, sy, :].flatten()            
            patches[j,:] = patch
            j += 1 
        else:
            if j > 0:
                j -= 1 
    
    return patches
Example #3
0
def generic2dFilter(frame, shape, func, mode='reflect', out=None, padded=False
                    , step=1):
    '''
    generic2dFilter

    Apply a generic function to blocks of a 2d array. Function must accept
    arguments in the form (array, axis, out) where array is a 3d array of
    flattened blocks from the original image, axis is the axis that sequences
    the blocks and out is an optional user input array to be assigned to.

    parameters
    ----------
    mode : string
    Decides how to pad the array. See numpy's pad function for a
    description.

    frame : 2 dimensional array
        2 dimensional array
    step : integer
        Describes the stride used when iterating over the array.
    '''
    bSize= shape[0]//2, shape[1]//2

    if padded:
        paddedFrame = frame
        reshapeSz = [(frame.shape[0]-2*bSize[0])//step
                     , (frame.shape[1]-2*bSize[1])//step
                     , -1]
    else:
        paddedFrame = np.pad(frame, bSize, mode=mode)
        reshapeSz = [frame.shape[0]//step, frame.shape[1]//step, -1]

    return func(view_as_windows(paddedFrame,shape,step=step).reshape(reshapeSz)
                ,axis=2, out=out)
    def get_ss_ftr(self, channels, smp_loc=None):
        """
        Compute self-similarity features

        :param channels: shrunk channels for self-similarity features
        :param smp_loc: shrunk sample locations (None for all)
        :return: self-similarity features
        """

        shrink = self.options["shrink"]
        p_size = self.options["p_size"] / shrink
        n_r, n_c, n_ch = channels.shape

        ss_ftr = view_as_windows(channels, (p_size, p_size, n_ch))

        if smp_loc is not None:
            ss_ftr = ss_ftr.reshape((n_r - p_size + 1, n_c - p_size + 1,
                                     p_size ** 2, n_ch))
            r_pos = [r - p_size / 2 for r, _ in smp_loc]
            c_pos = [c - p_size / 2 for _, c in smp_loc]
            ss_ftr = ss_ftr[r_pos, c_pos]
        else:
            ss_ftr = ss_ftr.reshape((-1, p_size ** 2, n_ch))

        n_cell = self.options["n_cell"]
        half_cell_size = int(round(p_size / (2.0 * n_cell)))
        grid_pos = [int(round((i + 1) * (p_size + 2 * half_cell_size - 1) / \
                              (n_cell + 1.0) - half_cell_size))
                    for i in xrange(n_cell)]
        grid_pos = [r * p_size + c for r in grid_pos for c in grid_pos]
        ss_ftr = ss_ftr[:, grid_pos]

        ss_ftr = pdist(ss_ftr)
        return ss_ftr.reshape((ss_ftr.shape[0], -1))
Example #5
0
def im2col(im, fit_rect, fit_stride):
    from skimage.util import view_as_windows
    im_chan = im.shape[0]
    row_num = im_chan * fit_rect[0] * fit_rect[1]
    return view_as_windows(
                im, 
                (im_chan, fit_rect[0], fit_rect[1]), 
                (1, fit_stride[0], fit_stride[1])).reshape((-1,row_num)).T
Example #6
0
def dim2col(dcol, col, im, fit_rect, fit_stride):
    from skimage.util import view_as_windows
    im_chan = im.shape[0]
    dim = np.zeros(im.shape)
    v = view_as_windows(
                dim, 
                (im_chan, fit_rect[0], fit_rect[1]), 
                (1, fit_stride[0], fit_stride[1]))
    v += dcol.T.reshape(v.shape)
    return dim * (1 / (fit_rect[0] * fit_rect[1]))
Example #7
0
    def loadFromFolder( self, folder, labelImage, patch_size = (23, 23), step_size = 4 ):
        self.featureNames = []
        self.features = []
        for fname in glob.glob( folder + "/*.nrrd" ):
            fsplit = fname.split('_');
            name = "image-" + fsplit[2] + "-" + fsplit[4]
            self.featureNames.append( name )
            
            imageType = itk.Image[itk.F, 3]
            reader = itk.ImageFileReader[imageType].New()
            reader.SetFileName( fname )
            reader.Update()
            image = itk.GetArrayFromImage( reader.GetOutput() ).squeeze()
            image = image -  np.min(image)
            image = image  / np.max(image)
            patches = util.view_as_windows(  image, patch_size, step_size )
            patch_shape = ( patches.shape[0]*patches.shape[1], patches.shape[2], patches.shape[3])
            patches = np.reshape( patches, patch_shape, 'F' )
            print(patches.shape)
            self.features.append( patches )
        
            print( patches.shape )

        self.features = np.stack(self.features, axis=-1)  
        
        
        imageType = itk.Image[itk.UC, 3]
        reader = itk.ImageFileReader[imageType].New()
        reader.SetFileName( labelImage )
        reader.Update()

        labelImage = itk.GetArrayFromImage( reader.GetOutput() ).squeeze() 
        print("label: ")
        print( labelImage.shape )
        patches = util.view_as_windows(  labelImage, patch_size, step_size )
        patch_shape = ( patches.shape[0]*patches.shape[1], patches.shape[2], patches.shape[3])
        patches = np.reshape( patches, patch_shape, 'F' )
        self.labels = np.zeros( patches.shape[0]  )
        for i in range(0, patches.shape[0] ):
            a, b = np.unique( patches[i,].flatten(), return_inverse=True )
            c = np.argmax( np.bincount(b) )
            self.labels[i] = a[c]
def patch_extract(image, patch_width, stride):
	# Ugly code, extracts r, g, b patches from the image convolutionally.

	patches_r = util.view_as_windows(image[:, :, 0], (patch_width, patch_width), stride)
	patches_g = util.view_as_windows(image[:, :, 1], (patch_width, patch_width), stride)
	patches_b = util.view_as_windows(image[:, :, 2], (patch_width, patch_width), stride)

	patches_r = numpy.reshape(patches_r, ((image.shape[0]-patch_width+1)**2, patch_width, patch_width), order="F")
	patches_r = numpy.reshape(patches_r, ((image.shape[0]-patch_width+1)**2, patch_width*patch_width), order="F")

	patches_g = numpy.reshape(patches_g, ((image.shape[0]-patch_width+1)**2, patch_width, patch_width), order="F")
	patches_g = numpy.reshape(patches_g, ((image.shape[0]-patch_width+1)**2, patch_width*patch_width), order="F")

	patches_b = numpy.reshape(patches_b, ((image.shape[0]-patch_width+1)**2, patch_width, patch_width), order="F")
	patches_b = numpy.reshape(patches_b, ((image.shape[0]-patch_width+1)**2, patch_width*patch_width), order="F")

	patches = numpy.append(patches_r, patches_g, 1);
	patches = numpy.append(patches, patches_b, 1);

	return patches
Example #9
0
 def getZMatrix(self, X):
     """
     Input: (n_instances, n_channels, input_size, input_size)
     
     Output: (n_instances, n_patchs, m)
     """
     
     Z = view_as_windows(X, (1, X.shape[1], self.filter_size, self.filter_size))
     Z = Z.reshape(np.prod(Z.shape[:4]), np.prod(Z.shape[4:]))
     Q = self.rbf_feature.transform(Z).astype(np.float16)
     
     return Q.reshape(X.shape[0], self.n_patchs, -1)
Example #10
0
def get_training_data(noisy_image, clear_image, patch_size=3):
    noisy_patches = view_as_windows(
        noisy_image, (patch_size, patch_size))
    training_inputs = [
        noisy_patches[index].ravel()
        for index in np.ndindex(
            noisy_patches.shape[0], noisy_patches.shape[1])
    ]
    patch_offset = (patch_size - 1) / 2
    training_outputs = clear_image[
        patch_offset:clear_image.shape[0] - patch_offset,
        patch_offset:clear_image.shape[1] - patch_offset
    ].ravel()
    return training_inputs, training_outputs
def extractLBPfeatures(img):

    lbp = local_binary_pattern(img, cfg.lbp_n_points, cfg.lbp_radius, cfg.lbp_METHOD)
    lbp_windows = view_as_windows(lbp, window_shape=cfg.lbp_win_shape, step=cfg.lbp_win_step)
    features = []
    count = 0
    for windows_list in lbp_windows:
        for window in windows_list:
            lbp_hist, bin_edges = np.histogram(window, bins=cfg.lbp_n_bins)
            lbp_hist_norm = sum(abs(lbp_hist))
            lbp_hist_l1sqrtnorm = np.sqrt(lbp_hist/float(lbp_hist_norm))
            features.append(lbp_hist_l1sqrtnorm)
            count += 1
    features_flatten = [item for sublist in features for item in sublist]
    return features_flatten
Example #12
0
def reshape_to_blocks(data, block_shape):
    overlap_window = 0.5
    step = (int(block_shape[0] * overlap_window),
            int(block_shape[1] * overlap_window))
    blocks = view_as_windows(data, block_shape, step)
    axis_0 = np.expand_dims(np.arange(0, step[0] * blocks.shape[0], step[0]),
                            axis=-1)
    axis_1 = np.expand_dims(np.arange(0, step[1] * blocks.shape[1], step[1]),
                            axis=0)
    indices = np.stack([
        np.tile(axis_0, (1, blocks.shape[1])),
        np.tile(axis_1, (blocks.shape[0], 1))
    ],
                       axis=-1)
    #flatten the two first dimension
    blocks = blocks.reshape(-1, blocks.shape[2], blocks.shape[3], 1)
    return indices, blocks  #expand dims to fit keras format
Example #13
0
def csvfft(f):

	for line in f:
		try:
			M = 2205 #window size
			f_s = 100
			#initials	
			song = line[:-1] #song?
			print('Song = ' + str(song))
			rate, audio = wavfile.read(song) #read wav file
			audio = np.mean(audio, axis=1) #average all the values
			N = audio.shape[0] #grab mono channel

			slices = util.view_as_windows(audio, window_shape=(M,), step = 2205) #get slices
			win = np.hanning(M+1)[:-1]
			slices = slices * win
			slices = slices.T #organize into columns
			spectrum = np.fft.fft(slices, axis=0) #do fft of audio
			spectrum = np.abs(spectrum)
			freqs = fftpack.fftfreq(len(spectrum[0]))
			for i in range(len(freqs)):
				freqs[i] = freqs[i] * 1000
			freqs = freqs[:len(freqs)/2]
			df = pd.DataFrame(index = range(len(spectrum)), columns = freqs)
			print(len(freqs))
			print(len(spectrum))
			print(len(spectrum[0])/2)
			temp = spectrum
			for i in range(len(spectrum)):
				#print(float(i) / len(spectrum) * 100)
				temp = spectrum[i]
				#print('Spectrum size = ' + str(spectrum.size/2))
				#print(spectrum)
				temp = temp[:(temp.size/2)]
				df.loc[i] = temp
			df.to_csv(line[:-4] + 'csv')
			#freqs = np.fft.fftfreq(len(spectrum[300])) * f_s
			#freqs = fftpack.fftfreq(len(spectrum[200]))
			#for i in range(len(freqs)):
			#	freqs[i] = freqs[i] * 1000
			#for i in range(len(spectrum[30])):
			#	print(freqs[i])
			#plt.plot(np.abs(freqs), spectrum[250], 'r')
			#plt.show()
		except ValueError:
			pass
Example #14
0
def matrix_median(R, n_med=4):
  """
  median filtering on matrix rows
  """

  from skimage.util import view_as_windows

  # half median size
  n_h = n_med // 2

  # shape
  m, n = R.shape

  # filter matrix to be filtered
  R_fil = view_as_windows(np.pad(R, ((0, 0), (n_h, n_h))), (1, n_med+1), step=1).reshape(m, n, n_med+1)

  return np.median(R_fil, axis=2)
Example #15
0
def main():
    args = parser.parse_args()
    img = cv2.imread(args.file, cv2.IMREAD_GRAYSCALE)
    patch_size = args.patch
    patches = view_as_windows(img, (patch_size, patch_size), step=patch_size)
    hmap = None
    print("Total %d, %d" % patches.shape[:-2])
    for i, r in enumerate(patches):
        for j, template in enumerate(r):
            print("Processing %d, %d" % (i, j), end="\r")
            res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
            # create a mask and threshold off only local region to the patch
            cut_val = args.threshold * np.max(res)
            res[res < cut_val] = 0
            if hmap is not None: hmap += res
            else: hmap = res
    plot(img, hmap)
Example #16
0
def predict_on_image(image,
                     weights,
                     network='unet',
                     n_class=1,
                     pad_width=16,
                     window_shape=(288, 288),
                     step=256):
    """Returns a network prediction on an image — i.e., a 2D object.

    Parameters
    ----------
    image : (M, N) array
        The input data.
    weights : TensorFlow weights
        A file with TensorFlow weights for the desired network.
    network : str, optional (default : 'unet')
        The network to use in prediction, corresponding to the weights file.
    n_class : int, optional (default : 1)
        Number of classes to predict.
    pad_width : int, optional (default : 16)
        Width of the padding to add in the borders of data.
    window_shape : (M, N) list, optional (default : (288, 288))
        Size of the window to process.
    step : int, optional (default : 16)
        Size of the step used to chunk the data. Used as an overlap feature.

    Returns
    -------
    prediction : (M, N) array
        Array containing predictions on the input data.
    """
    model = network_models(network, window_shape=window_shape, n_class=n_class)
    if network is None:
        raise (f'Network {network} not available.')
    model.load_weights(weights)

    image = np.pad(image, pad_width=pad_width)
    image_crop = np.vstack(
        util.view_as_windows(image, window_shape=window_shape, step=step))
    crop_steps = image_crop.shape[0]
    image_gen = tensor_generator(image_crop)

    results = model.predict(image_gen, steps=crop_steps, verbose=1)
    prediction = _aux_predict(results)

    return prediction
Example #17
0
def haralick_features_ssm(img, win, d):
    win_sz = 2 * win + 1
    window_shape = (win_sz, win_sz)
    arr = np.pad(img, win, mode='reflect')
    windows = util.view_as_windows(arr, window_shape)
    Nd = len(d)
    feats = np.zeros(shape=windows.shape[:2] + (Nd, 9), dtype=np.float64)
    for m in tqdm_notebook(range(windows.shape[0]),
                           desc='02_rows',
                           leave=False):
        for n in tqdm_notebook(range(windows.shape[1]),
                               desc='03_cols',
                               leave=False):
            for i, di in enumerate(d):
                w = windows[m, n, :, :]
                feats[m, n, i, :] = calc_haralick(w, distance=di)
    return feats.reshape(feats.shape[:2] + (-1, ))
Example #18
0
def generate_real_samples(n_batch):
    """get random sampples and do the last preprocessing on them"""
    while True:
        # get random sample of indices from the precomputed indices
        # for this we generate random indices for the index list (confusing termoonology, since we use
        # indices to index the list of indices...
        ixs = np.random.randint(n_samples, size=n_batch)
        idcs_batch = indices_all[ixs]

        # now we select the data corresponding to these indices

        data_wview = view_as_windows(data, (1, 1, ndomain, ndomain))[..., 0,
                                                                     0, :, :]
        batch = data_wview[idcs_batch[:, 0], :, idcs_batch[:, 1],
                           idcs_batch[:, 2]]
        # add empty channel dimension (necessary for keras, which expects a channel dimension)
        batch = np.expand_dims(batch, -1)
        # compute daily sum (which is the condition)
        batch_cond = np.sum(batch, axis=1)  # daily sum

        # the data now is in mm/hour, but we want it as fractions of the daily sum for each day
        for i in range(n_batch):
            batch[i] = batch[i] / batch_cond[i]

        # normalize daily sum
        batch_cond = batch_cond / norm_scale

        # add lon
        batch_lon = idcs_batch[:, 2]
        # normalize to [0,1]
        batch_lon = (batch_lon - min_lonidx) / max_lonidx
        # repeat it to ndomain x ndomain x 1  (last is channel dimension)
        batch_lon = np.tile(batch_lon, (1, ndomain, ndomain, 1)).T
        # now it has n_batch x ndomain x ndomain x 1

        # and add it as additional varialbes
        batch_cond = np.concatenate([batch_cond, batch_lon], axis=-1)

        assert (batch.shape == (n_batch, nhours, ndomain, ndomain, 1))
        assert (batch_cond.shape == (n_batch, ndomain, ndomain, n_channel))
        assert (~np.any(np.isnan(batch)))
        assert (~np.any(np.isnan(batch_cond)))
        assert (np.max(batch) <= 1)
        assert (np.min(batch) >= 0)

        yield [batch, batch_cond]
Example #19
0
def window_region_merge_grey(input_im, b):
    block_size = (b, b)
    pad_width = []
    reshape_size = b * b
    for i in range(len(block_size)):
        if input_im.shape[i] % block_size[i] != 0:
            after_width = block_size[i] - (input_im.shape[i] % block_size[i])
        else:
            after_width = 0
        pad_width.append((0, after_width))
    input_im = np.pad(input_im, pad_width=pad_width, mode='constant')
    view = view_as_windows(input_im, block_size)
    print(view.shape)
    flatten_view = np.transpose(view, axes=(0, 1, 3, 2))
    flatten_view = flatten_view.reshape(flatten_view.shape[0],
                                        flatten_view.shape[1], reshape_size)
    return np.squeeze(mode(flatten_view, axis=2)[0])
Example #20
0
def get_sequences(melgram, _n_mels):
    """
    Transform 2 or 3D mel spectrogram into 3D array of windows

    :param melgram: 2 or 3D mel spectrogram (if 3D, squeezed to 2D)
    :param _n_mels: number of mel buckets
    :return: array of sequences. shape (n_frames, seq_len, n_mels)
    """
    if len(melgram.shape) == 3:
        melgram = np.squeeze(melgram)

    seq_len = hyper_params.sample_frames

    sequences = view_as_windows(melgram, window_shape=(seq_len, _n_mels))
    sequences = np.squeeze(sequences, 1)

    return sequences
Example #21
0
def make_dataset(gesture_path, save_dir, pattern, winSize = 128,step = 2):
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    data = load_data(gesture_path)
    # data = sp.despikeSeries(data)
    x_train_signals_paths = [
        os.path.join(save_dir ,signal + ".csv" )for signal in INPUT_SIGNAL_TYPES
    ]
    x_train_files = [csv.writer(open( x_train_signals_path,"a",newline=''))for x_train_signals_path in x_train_signals_paths]
    y_train_file = csv.writer(open(os.path.join(save_dir,"y.csv"),"a",newline=''))

    for i ,signals in enumerate(data):
        segmented_signal = util.view_as_windows(signals, (winSize,),step = step)
        for signal in segmented_signal:
            x_train_files[i].writerow(signal)
            if i ==0:
                y_train_file.writerow([pattern])
def Shrink(X, shrinkArg, max_pooling=True, padding=False):

    win = shrinkArg['win']
    max_pooling = shrinkArg['max_pooling']
    padding = shrinkArg['padding']
    if max_pooling:
        X = block_reduce(X, (1, 2, 2, 1), np.average)
    if padding:
        new_X = []
        for each in X:
            each = cv2.copyMakeBorder(each, win // 2, win // 2, win // 2, win // 2, cv2.BORDER_REFLECT)
            new_X.append(each)
        new_X = np.array(new_X)[:, :, :, None]
    else:
        new_X = X
    X = view_as_windows(new_X, (1, win, win, 1))
    X = X.reshape(X.shape[0], X.shape[1], X.shape[2], -1)
    return X
Example #23
0
def extractLBPfeatures(img):

    lbp = local_binary_pattern(img, cfg.lbp_n_points, cfg.lbp_radius,
                               cfg.lbp_METHOD)
    lbp_windows = view_as_windows(lbp,
                                  window_shape=cfg.lbp_win_shape,
                                  step=cfg.lbp_win_step)
    features = []
    count = 0
    for windows_list in lbp_windows:
        for window in windows_list:
            lbp_hist, bin_edges = np.histogram(window, bins=cfg.lbp_n_bins)
            lbp_hist_norm = sum(abs(lbp_hist))
            lbp_hist_l1sqrtnorm = np.sqrt(lbp_hist / float(lbp_hist_norm))
            features.append(lbp_hist_l1sqrtnorm)
            count += 1
    features_flatten = [item for sublist in features for item in sublist]
    return features_flatten
def generate_latent_points(n_batch):
    # generate points in the latent space and a random condition
    latent = np.random.normal(size=(n_batch, latent_dim))
    # randomly select conditions
    ixs = np.random.randint(0, n_samples, size=n_batch)
    idcs_batch = indices_all[ixs]

    data_wview = view_as_windows(data, (1, 1, ndomain, ndomain))[..., 0,
                                                                 0, :, :]
    batch = data_wview[idcs_batch[:, 0], :, idcs_batch[:, 1], idcs_batch[:, 2]]
    # add empty channel dimension (necessary for keras, which expects a channel dimension)
    batch = np.expand_dims(batch, -1)
    batch_cond = np.sum(batch, axis=1)  # daily sum
    # normalize daily sum
    batch_cond = batch_cond / norm_scale
    assert (batch_cond.shape == (n_batch, ndomain, ndomain, 1))
    assert (~np.any(np.isnan(batch_cond)))
    return [latent, batch_cond]
Example #25
0
def rolling_window(image,
                   clf,
                   scaler,
                   stepSize=8,
                   Zstep=4,
                   windowSize=(1, 70, 70)):
    z, x, y = image.shape
    rol_view = util.view_as_windows(image, (1, 70, 70), step=(4, 8, 8))
    rol_view_flat = rol_view.reshape(-1, 70, 70)
    a, b, c = rol_view_flat.shape
    list_proba = joblib.Parallel(n_jobs=-1)(
        joblib.delayed(_task_predict)(window, clf, scaler)
        for window in rol_view_flat)
    list_proba_array = np.asarray(list_proba)
    array_proba = np.asarray(list_proba).reshape(int(a / (52 * 52)), 52, 52)
    result = np.zeros((z, x - 70, y - 70))
    result[::4, ::8, ::8] = array_proba
    return (result)
Example #26
0
def min_distance(patch, size, v):
    print(patch, size)
    x = patch.mean()
    min_d = 999999999
    dist = {}
    for pat1 in (view_as_windows(v, size)):
        for pat in pat1:
            print(pat.shape)
            a = abs(x - pat.mean())
            a = math.pow(a, 2)
            b = (size[0] * size[1])
            d = a / b
            dist[d] = pat
            if (d < min_d):
                min_d = d
    dist = collections.OrderedDict(sorted(dist.items()))
    dist = list(dist.values())
    return min_d, (dist[0:1])
Example #27
0
def generate_sparse_matrix(shape, op):
    count = product(shape)
    indices = np.arange(count).reshape(shape)
    view = view_as_windows(indices, op.shape)
    window_shape = view.shape[0:len(shape)]
    X, Y, W = [], [], []
    cnt = op.size
    center = cnt // 2
    opr = op.ravel()

    for idx in rangeN(window_shape):
        v = view[idx].ravel()
        for p in range(cnt):
            if opr[p] != 0:
                X.append(v[center])
                Y.append(v[p])
                W.append(opr[p])
    inside = to_csr_matrix(W, X, Y, dtype=np.float, shape=(count, count))
    return inside
Example #28
0
File: OCR.py Project: OlavH96/OCR
def sliding_window_prediction(d, step):
    windows = view_as_windows(d, window_shape=(window_x, window_y), step=step)
    indexes = []
    for i, p in enumerate(windows):
        for j, w in enumerate(p):

            should_skip = white_pixel_filter(w)
            if should_skip: continue

            w = np.reshape(w, newshape=(1, window_x, window_y, 1))
            prediction = model.predict(w)
            certainty = prediction.max()

            if certainty < CUTOFF: continue

            predict = prediction.argmax(axis=1)
            label = Labels.from_int(predict.tolist()[0])

            indexes.append((i, j, label, certainty))
    return indexes
Example #29
0
def extract_patches(
    image: np.ndarray,
    patch_shape: Tuple[int, int] = (300, 300),
    overlap: Union[Optional[int], Optional[float],
                   Tuple[Optional[Union[float, int]],
                         Optional[Union[float, int]]], ] = None,
):
    if len(image.shape) > 3:
        raise ValueError("Expected single image")

    patch_h, patch_w = patch_shape

    stride_h, stride_w = normalize_overlap(overlap, patch_shape)

    window_shape = ((patch_h, patch_w, image.shape[2]) if image.ndim == 3 else
                    (patch_h, patch_w))
    step = (stride_h, stride_w, 1) if image.ndim == 3 else (stride_h, stride_w)
    patches = view_as_windows(image, window_shape, step)
    # patches = patches.reshape(-1, patch_h, patch_w, image.shape[2])
    return patches
Example #30
0
 def patches_true(self, iteration):
     subjects_true = self.data_true(iteration)
     patches_true = np.empty(
         [self.batch_size * self.num_patches, self.width_patch + self.margin, self.heigth_patch + self.margin,
          self.depth_patch + self.margin, 1])
     i = 0
     for subject in subjects_true:
         patch = view_as_windows(subject, window_shape=(
             (self.width_patch + self.margin), (self.heigth_patch + self.margin), (self.depth_patch + self.margin)),
                                 step=(self.width_patch - self.margin, self.heigth_patch - self.margin,
                                       self.depth_patch - self.margin))
         for d in range(patch.shape[0]):
             for v in range(patch.shape[1]):
                 for h in range(patch.shape[2]):
                     p = patch[d, v, h, :]
                     p = p[:, np.newaxis]
                     p = p.transpose((0, 2, 3, 1))
                     patches_true[i] = p
                     i = i + 1
     return patches_true
def denoise_image(In, stride):
    In_p = skimage.util.pad(In, 32, 'reflect')
    In_p = In_p[:, :, 32:(2 * 32 + 3) - 32]
    Pn_p = view_as_windows(In_p[:, :, :],
                           window_shape=(64, 64, 3),
                           step=stride)

    ss = Pn_p.shape
    Pn_p = Pn_p.reshape(ss[0] * ss[1], ss[3], ss[4], 3) / 255.0
    Pp_p = numpy.zeros_like(Pn_p)
    #  r=0
    for ind in range(0, ss[0] * ss[1], batch_size):
        #  print ind,ind+batch_size
        mini_batch = Pn_p[ind:ind + batch_size]
        #    print "{} = {}".format("Input shape",mini_batch[0].shape)
        #    print mini_batch[0]
        #    np.save('ex1'+str(r+1)+'.npy',mini_batch)

        predk = E.sess.run([y_],
                           feed_dict={
                               x_: mini_batch,
                               phase_train: False
                           })
        #    np.save('ez1'+str(r+1)+'.npy',predk)
        #    print "{} = {}".format("Shape of the batch",predk[0].shape)
        #    print "{} = {}".format("Max of the batch",np.max(predk[0]))
        #    print "{} = {}".format("Shape of the input",mini_batch.shape)
        #    print "{} = {}".format("Max of the input",np.max(mini_batch))
        #   r = r + 1
        #    print "{} ={}".format("Shape of the output",predk[0][0].shape)
        #    print predk[0][0]
        #   r=r+1
        #   print "{} = {}".format("MSE of inpit and output",((predk[0][0] - mini_batch[0])**2).mean(axis=None))
        #    print "{} = {}".format("PSNR of input and output",measure.compare_psnr(predk[0][0],mini_batch[0], dynamic_range = 1.0))
        #    if( r==2):
        #     sys.exit(0)
        #   print "{} = {}".format("MSE OF INPUT AND RECONSTRUCTED",((predk[0] - mini_batch)**2).mean(axis = None))
        Pp_p[ind:ind + batch_size, :, :, :] = predk[0]
    Pp_p = Pp_p * 255.0
    Ip = reconstruct_image_from_patches(Pp_p, ss, In_p)
    return post_process(Ip)
Example #32
0
def form_visibility(data, rate, fc, bw, T_sti, T_stationarity):
    """
    Parameter
    ---------
    data : :py:class:`~numpy.ndarray`
        (N_sample, N_channel) antenna samples. (float)
    rate : int
        Sample rate [Hz]
    fc : float
        Center frequency [Hz] around which visibility matrices are formed.
    bw : float
        Double-wide bandwidth [Hz] of the visibility matrix.
    T_sti : float
        Integration time [s]. (time-series)
    T_stationarity : float
        Integration time [s]. (visibility)

    Returns
    -------
    S : :py:class:`~numpy.ndarray`
        (N_slot, N_channel, N_channel) visibility matrices.

    Note
    ----
    Visibilities computed directly in the frequency domain.
    For some reason visibilities are computed correctly using
    `x.reshape(-1, 1).conj() @ x.reshape(1, -1)` and not the converse.
    Don't know why at the moment.
    """
    S_sti = (statistics.TimeSeries(data, rate).extract_visibilities(T_sti,
                                                                    fc,
                                                                    bw,
                                                                    alpha=1.0))

    N_sample, N_channel = data.shape
    N_sti_per_stationary_block = int(T_stationarity / T_sti) + 1
    S = (skutil.view_as_windows(
        S_sti, (N_sti_per_stationary_block, N_channel, N_channel),
        (N_sti_per_stationary_block, N_channel, N_channel)).squeeze(
            axis=(1, 2)).sum(axis=1))
    return S
Example #33
0
def xpSample():

    song = 'AlbenizI-Tango.wav'
    rate, audio = wavfile.read(song)
    audio = np.mean(audio, axis=1)
    N = audio.shape[0]
    M = 2205
    slices = util.view_as_windows(audio, window_shape=(M, ), step=2205)
    win = np.hanning(M + 1)[:-1]
    slices = slices * win
    slices = slices.T
    spectrum = np.fft.fft(slices, axis=0)
    spectrum = np.abs(spectrum)
    #spectrum = 20 * np.log10(spectrum / spectrum.max())
    f_s = 100
    freqs = np.fft.fftfreq(len(spectrum[300])) * f_s
    L = N / rate
    print('Audio Length: {:.2f} seconds'.format(L))
    print(len(audio))
    #print(slices.shape)
    #fig, ax = plt.subplots()
    #print(len(spectrum))
    #print(len(spectrum[300]))
    #print(spectrum)
    #spectrum = np.fft.fft(spectrum)
    #spectrum = np.abs(spectrum)
    #print(spectrum[i])
    freqs = fftpack.fftfreq(len(spectrum[200]))
    print(len(freqs))
    print(len(spectrum))
    print(len(spectrum[0]))
    for i in range(len(freqs)):
        freqs[i] = freqs[i] * 1000
    for i in range(len(spectrum[30])):
        print(freqs[i])
    plt.plot(np.abs(freqs), spectrum[250], 'r')
    #plt.plot(np.abs(spectrum[300]),freqs, 'r')
    #plt.show()
    #ax.plot(freqs, np.abs(spectrum[1]), 'r')
    plt.show()
    '''
Example #34
0
def denoise_image(In, stride):
    In_p = skimage.util.pad(In, 32, 'reflect')
    Pn_p = view_as_windows(In_p, window_shape=(64, 64), step=stride)
    ss = Pn_p.shape
    Pn_p = Pn_p.reshape(ss[0] * ss[1], ss[2], ss[3], 1) / 255.0
    Pp_p = numpy.zeros_like(Pn_p)

    for ind in range(0, ss[0] * ss[1], batch_size):
        #print ind,ind+batch_size
        mini_batch = Pn_p[ind:ind + batch_size]
        #print mini_batch.shape
        predk = E.sess.run([y_],
                           feed_dict={
                               x_: mini_batch,
                               phase_train: False
                           })
        #print numpy.asarray(predk).shape
        Pp_p[ind:ind + batch_size, :, :, :] = predk[0]
    Pp_p = Pp_p * 255.0
    Ip = reconstruct_image_from_patches(Pp_p, ss, In_p)
    return post_process(Ip)
Example #35
0
 def __init__(self, mat_path, patch_size=12, step_size=1, normalize=False):
     dataset = sio.loadmat(mat_path)
     images = np.ascontiguousarray(
         dataset['IMAGES'])  # shape: (512, 512, 10)
     self.patches = np.squeeze(
         view_as_windows(images, (patch_size, patch_size, 10),
                         step=step_size))  # shape: (., ., PS, PS, 10)
     self.patches = self.patches.transpose((0, 1, 4, 2, 3))
     self.patches = self.patches.reshape((-1, patch_size, patch_size))
     if normalize:
         # normalize to range [0, 1]
         _min = self.patches.min()
         _max = self.patches.max()
         self.patches = (self.patches - _min) / (_max - _min)
     if self.patches.dtype != np.float:
         print('converting data type from %r to np.float' %
               self.patches.dtype)
         self.patches = self.patches.astype(np.float)
     print('image statistics:')
     print('min: %r, mean: %r, max: %r' %
           (self.patches.min(), self.patches.mean(), self.patches.max()))
def image_to_crops(image, patch_size, stride, augment_fn=None):
    """
    Returns a grid of patches from the given image, taking into account patch size and stride.

    :param image: input image with dimensions (x, y, ch).
    :param patch_size: size of crop/patch.
    :param stride: size of stride in pixels. For example, 50% overlapping means stride=patch_size//2.
    :param augment_fn: augmentation function applied to crops of image in float [-1, +1] format and
        dimensions [n_crops, n_crops, x, y, ch].
    :return: crops with dimensions [n_crops, n_crops, x, y, ch]
    """

    # Get patches
    crops = view_as_windows(image, (patch_size, patch_size, 3),
                            (stride, stride, 1)).squeeze()

    # Augment
    if augment_fn is not None:
        crops = augment_fn(crops)

    return crops
Example #37
0
def patchify(image: np.ndarray,
             patch_size: Imsize,
             step: int = 1) -> np.ndarray:
    """
    Split a 2D or 3D image into small patches given the patch size.

    Parameters
    ----------
    image: the image to be split. It can be 2d (m, n) or 3d (k, m, n)
    patch_size: the size of a single patch
    step: the step size between patches

    Examples
    --------
    >>> image = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
    >>> patches = patchify(image, (2, 2), step=1)  # split image into 2*3 small 2*2 patches.
    >>> assert patches.shape == (2, 3, 2, 2)
    >>> reconstructed_image = unpatchify(patches, image.shape)
    >>> assert (reconstructed_image == image).all()
    """
    return view_as_windows(image, patch_size, step)
Example #38
0
    def bump_z(self, surface):

        points = list(self.__points)

        image = surface.z_image

        window_shape = (3, 3)
        windows = util.view_as_windows(image, window_shape)

        for i, point in enumerate(points):
            query_coord = (point[1] - 1, point[0] - 1)
            local_window = windows[query_coord]

            indices = np.where(local_window == local_window.min())
            local_coords = indices[0][0], indices[1][0]
            global_coords = local_coords[0] + query_coord[0], local_coords[
                1] + query_coord[1]

            points[i] = global_coords[1], global_coords[0]

        return Path(points)
Example #39
0
def Shrink(X, shrinkArg):
    win = shrinkArg['win']
    stride = shrinkArg['stride']
    ith_hop = shrinkArg['ith_hop']

    if ith_hop == 1:
        X_new = X
    else:
        X_new = np.zeros([X.shape[0], X.shape[1] // 2, X.shape[2] // 2, X.shape[3]])
        for i in range(0, X.shape[0]):
            for j in range(0, X.shape[3]):
                for m in range(0, X.shape[1], 2):
                    for n in range(0, X.shape[2], 2):
                        pixel_list = [X[i, m, n, j], X[i, m + 1, n, j],
                                      X[i, m, n + 1, j], X[i, m + 1, n + 1, j]]
                        max_pixel = max(pixel_list)
                        X_new[i, m // 2, n // 2, j] = max_pixel

    ch = X_new.shape[-1]
    X_new = view_as_windows(X_new, (1, win, win, ch), (1, stride, stride, ch))
    return X_new.reshape(X_new.shape[0], X_new.shape[1], X_new.shape[2], -1)
Example #40
0
def extractLBPfeatures(img, detector):
    lbp = local_binary_pattern(img, cfg.lbp_n_points, cfg.lbp_radius, cfg.lbp_METHOD)
    lbp_windows = view_as_windows(lbp, window_shape=cfg.lbp_win_shape, step=cfg.lbp_win_step)
    features = []
    count = 0
    ws = cfg.lbp_win_shape[1]
    kpts = detector.detect(img)
    loc = [(int(x.pt[0]),int(x.pt[1])) for x in kpts]
    for poi in loc:
        window = Image.fromarray(img).crop((poi[0]-ws,poi[1]-ws,poi[0]+ws,poi[1]+ws))
        window = np.array(window)
 #   for windows_list in lbp_windows:
#       for window in windows_list:
        lbp_hist, bin_edges = np.histogram(window, bins=cfg.lbp_n_bins)
        lbp_hist_norm = sum(abs(lbp_hist))
        lbp_hist_l1sqrtnorm = np.sqrt(lbp_hist/float(lbp_hist_norm))
        features.append(lbp_hist_l1sqrtnorm)
        count += 1
    #features_flatten = [item for sublist in features for item in sublist]
    features = np.asarray(features)
    return features
Example #41
0
def parse_wave_csv(filename,
                   fft=False,
                   window_stride=0.001,
                   window_size=0.02,
                   sampling_rate=44100):

    data = np.genfromtxt(filename, delimiter=',')[:30 * sampling_rate,
                                                  1:].flatten()

    if fft:
        fft_data = []
        N = 2**int(np.ceil(np.log2(window_size * sampling_rate)))
        assert N <= len(data), "Segment too short"
        hann = np.hanning(N)
        stride = int(round(window_stride * sampling_rate))
        windows = util.view_as_windows(data, window_shape=N, step=stride)
        windows = hann * windows
        spectrum = np.fft.fft(windows)
        return np.mean(np.abs(spectrum)[:, 1:(N - 1) // 2 + 1], axis=0)

    return data
Example #42
0
def get_dense_patches(image, patch_size = 8, sample_rate = 4):
    """Gets dense patches of pixels from an image.

    This uses the view_as_windows function from scikit-image.

    Parameters
    ----------
    image: ndarray
        Image to find dense patches within.
    patch_size: int (default: 8)
        Size of each patch. 8 x 8 patches are recommended.
    sample_rate: int (default: 4)
        How many pixels apart each patch should be chosen in both x and y
        directions.

    Returns
    -------
    out: ndarray
        An array of which the rows make up a ravel of each dense patch.
    """

    window_shape = (patch_size, patch_size)
    patches = view_as_windows(image, window_shape = window_shape,
                                    step = sample_rate)

    shp = patches.shape
    # Resize the array so that each row is one dense patch.
    out = np.reshape(patches, (shp[0]*shp[1], shp[2]*shp[3]))

    # Normalize the data
    m = out.mean(axis = 1)

    # Have to transpose as it won't take subtract along columns properly
    out = (np.transpose(out) - m).transpose()

    # Make unit length along the rows
    out = normalize(out, axis = 1)

    return out
  def f_unsup(img):
    img_ptchs = view_as_windows(img, (w, w), step=s)
    orig_dim = (img_ptchs.shape[0], img_ptchs.shape[1], k_means.cluster_centers_.shape[0])
    img_ptchs = numpy.reshape(img_ptchs, (img_ptchs.shape[0] * img_ptchs.shape[0], w * w))
    # normalize the patches, per patch sample
    img_ptchs = preprocessing.scale(img_ptchs, axis=1)
    # apply whitening transform
    img_ptchs = numpy.dot(img_ptchs - mu, Wh)   # uses multiprocessing!
    Z = k_means.transform(img_ptchs)
    # sparsity constraint threshold q
    uZ = numpy.percentile(Z, q, axis=1, keepdims=True)
    tri_Z = numpy.maximum(0, uZ - Z)
    tri_Z_image = numpy.reshape(tri_Z, orig_dim)

    # pooling of averages at mesh resolution
    pooled_Z = []
    pool_dim1 = orig_dim[0]/mesh_r or orig_dim[0]
    pool_dim2 = orig_dim[1]/mesh_r or orig_dim[1]
    for i in xrange(0,orig_dim[0], pool_dim1):
      for j in xrange(0,orig_dim[1], pool_dim2):
        POOL_MSH = numpy.mean(tri_Z_image[i:(i + pool_dim1), j:(j + pool_dim2)], axis=(0, 1))
        pooled_Z.append(POOL_MSH)
    return numpy.hstack(pooled_Z)
    def get_reg_ftr(self, channels, smp_loc=None):
        """
        Compute regular features.

        :param channels: shrunk channels for regular features
        :param smp_loc: shrunk sample locations (None for all)
        :return: regular features
        """

        shrink = self.options["shrink"]
        p_size = self.options["p_size"] / shrink
        n_r, n_c, n_ch = channels.shape

        reg_ftr = view_as_windows(channels, (p_size, p_size, n_ch))
        reg_ftr = reg_ftr.reshape((n_r - p_size + 1, n_c - p_size + 1,
                                   p_size ** 2 * n_ch))

        if smp_loc is not None:
            r_pos = [r - p_size / 2 for r, _ in smp_loc]
            c_pos = [c - p_size / 2 for _, c in smp_loc]
            reg_ftr = reg_ftr[r_pos, c_pos]

        return reg_ftr
    def process_single_file(self, filename):
        '''
        processes a single image file
        '''
        img = io.imread(filename)
        try:
            img = img.transpose(2,0,1)
            img = transform.resize(img, (3,227,227))
        except:
            print "image didn't have correct shape, continuing"
            return None, None
        img -= self.mean_image
        label = filename.split('/')[-2] # HACKY!
        y = self.l2i[label]

        # view is 1 x a x b x window shape.
        view = util.view_as_windows(img, window_shape=self.window_shape, step=self.window_step)
        # we want this to be num_windows x windows shape, so:
        batches = view.reshape(np.prod(view.shape[:-3]), *view.shape[3:])
        new_batches = []
        for batch in batches:
            new_batches.append(transform.resize(batch,(3,227,227)))
        return np.array(new_batches), y
    def extractRandomPatches(self, patchSize, numPatches, stride=1):
        size = self.info()['entries']
        data = self.next()
        patches = np.zeros((numPatches*size, patchSize[0]*patchSize[1]*patchSize[2]))

        j = 0
        for z in range(0, size):                               
            start = time.time()
            img = np.transpose(data[1], (1,2,0))               
            windows = view_as_windows(img, patchSize, stride)
            r = np.random.randint(0, windows.shape[0] - windows.shape[3], (numPatches,))
            c = np.random.randint(0, windows.shape[1]- windows.shape[4], (numPatches,))
            for i in range(0, numPatches):
                patch = np.reshape(windows[r[i],c[i],0,:], windows.shape[3]*windows.shape[4]*windows.shape[5])                    
                patches[j,:] = patch
                if j % 100 == 0:
                    print "Extracted {0}th patch of {1} patches totally".format(j, numPatches*size)
                j += 1
            data = self.next()
            end = time.time()
            print (end - start)
        
        return  patches
Example #47
0
    def __init__(self, pixelArray, blockSizeX, blockSizeY, overlap, cellSizeX, cellSizeY):
        """Constructor from an input array. Array should be 2x2 etc.

        Args:
            pixelArray: The array of pixels.
            blockSizeX: The number of cells on the x axis of this Block. The
                size in pixels is blockSizeX*cellSizeX
            blockSizeY: The number of cells on the y axis of this Block. The
                size in pixels is blockSizeY*cellSizeY
            overlap: Fraction of overlap between Blocks.
            cellSizeX: The number of pixels on the x axis of the Cells within
                this Block.
            cellSizeY: The number of pixels on the y axis of the Cells within
                this Block.
        """
        self._pixelArray = pixelArray

        # if we want no overlap then overlap = 0 but need to alter so that
        # step isn't zero and is the maximum of the blockSize
        if overlap == 0:
            step = max(blockSizeY, blockSizeX)
        else:
            # Calculate step from overlap and block size
            # NB This will be calculated as the minimum of either the x or y steps
            step_x = np.floor(overlap * blockSizeX)
            step_y = np.floor(overlap * blockSizeY)
            step = min(step_x, step_y)

        block_shape = (blockSizeY, blockSizeX)

        self.blockArray = view_as_windows(pixelArray, block_shape, step)

        # Create cell object
        self.cell = Cell(self.blockArray, cellSizeX, cellSizeY)

        self.cellArray = self.cell.cellArray
patches = np.zeros((total_numPatches, rfSize*rfSize*3))
whitening = True
maxIter = 50
batchSize = 1000
j = 0

values = labels_dict.values()

for each in images:
    if labels_dict[each.split('.')[0]] > 0:
        numPatches = 140
    else:
        numPatches = 40

    img = load_image('../data/resized/trainOriginal/' + each)
    windows = view_as_windows(img, (rfSize, rfSize, 3))
    r = np.random.randint(0, windows.shape[0] - windows.shape[3], (numPatches,))
    c = np.random.randint(0, windows.shape[1]- windows.shape[4], (numPatches,))
    for i in range(0, numPatches):
        patch = np.reshape(windows[r[i],c[i],0,:], windows.shape[3]*windows.shape[4]*windows.shape[5])
        patches[j,:] = patch
        if j % 100 == 0:
            print "Extracted {0}th patch of {1} patches totally".format(j,total_numPatches)        
        j += 1

numCentroids = int(np.sqrt(total_numPatches/2))

patchesMean = np.mean(patches, axis=1, dtype=np.float32, keepdims=True)
patchesVar = np.var(patches, axis=1, dtype=np.float32, keepdims=True)
offsetMatrix = 10.0 * np.ones(patchesVar.shape)
patches = (patches - patchesMean) / np.sqrt(patchesVar + offsetMatrix)
Example #49
0
 def windows(self, size, step=1):
     '''windows are overlapping sub-images'''
     size = list(size)
     size.append(self.channels)
     return [Image(i) for i in util.view_as_windows(self, size, step)]
Example #50
0
def doConvolution(lX,ly=None,maxPixel=None,patchSize=5,n_components=15,stride=2,pad=1,blocksize=4,showEigenImages=False):
    """
    Extract patches....
    """
    #@TODO incl testset....
    #http://scikit-learn.org/stable/auto_examples/cluster/plot_dict_face_patches.html
    #http://scikit-learn.org/stable/modules/preprocessing.html
    #http://nbviewer.ipython.org/github/dmrd/hackclass-sklearn/blob/master/image_classification.ipynb
    #http://scikit-learn.org/stable/auto_examples/applications/face_recognition.html
    max_patches=None    
    whiten_pca=True
    pooling=True   
    
    #data_type='float32'
    
    print "Image width:",maxPixel
    print "Image height:",maxPixel
    print "Pad         :",pad
    print "Patch size  :",patchSize
    print "Stride      :",stride
    print "PCA components:",n_components
    print "Pooling:",pooling
    print "Pooling patches blocksize:",blocksize
      
    #npatches = (maxPixel - patchSize +1+2*pad)*(maxPixel - patchSize +1+2*pad)/stride
    npatchesx = (maxPixel+2*pad - patchSize)/stride +1
    npatches = npatchesx * npatchesx
    print "number of patches:",npatches
    
    print "Original shape:",lX.shape, " size (MB):",float(lX.values.nbytes)/1.0E6, " dtype:",lX.values.dtype
    tmpy=[]    
    t0 = time()
    #data = np.zeros((lX.shape[0],npatches,patchSize*patchSize),dtype=np.float32)
    data = np.zeros((lX.shape[0],npatches,patchSize*patchSize),dtype=np.uint8)
    #Online...!?
    for i,img in enumerate(lX.values):
	img = np.reshape(img*255, (maxPixel, maxPixel)).astype('uint8')
	#standardize patch
	#img = (img -img.mean())/np.sqrt(img.var()+0.001) 
	
	#img = np.reshape(img, (maxPixel, maxPixel)).astype('float32')
	if pad:
	  img = padImage(img,pad=pad)
	patches = view_as_windows(img,(patchSize, patchSize),stride)
	patches = np.reshape(patches, (patches.shape[0]*patches.shape[1],patchSize, patchSize))
	patches = np.reshape(patches, (len(patches), -1))
	data[i,:,:]=patches
	#data.append(patches)


    print("Extract patches done in %0.3fs" % (time() - t0))
    
    data = np.reshape(data,(lX.shape[0]*npatches,patchSize*patchSize))
    
    print "Extract patches, new shape:",data.shape, " size (MB):",float(data.nbytes)/1.0E6, " dtype:",data.dtype
    
    #if whiten_pca:
    #  print "Intermediate PCA!"
    #  pca = RandomizedPCA(patchSize*patchSize, whiten=whiten_pca)
    #  data = pca.fit_transform(data)
    #do a pca first
    #pca = RandomizedPCA(n_components, whiten=whiten_pca)
    #http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html
    #http://ufldl.stanford.edu/wiki/index.php/Implementing_PCA/Whitening
    #pca=TruncatedSVD(n_components=n_components, algorithm='randomized', n_iter=5, tol=0.0)
    pca0 = MiniBatchKMeans(init='k-means++', n_clusters=n_components, n_init=5)
    #pca = FastICA(n_components=n_components,whiten=whiten_pca)
    pca0.fit(data)
    #pca = MiniBatchSparsePCA(n_components=n_components,alpha=0.5,n_jobs=4)
    #pca = MiniBatchDictionaryLearning(n_components=n_components,alpha=0.5,n_jobs=4)
    #pca = SparseCoder(dictionary=pca0.cluster_centers_,transform_n_nonzero_coefs=n_components,transform_algorithm='lars',transform_alpha=None,n_jobs=4)
    pca = SparseCoder(dictionary=pca0.cluster_centers_,transform_n_nonzero_coefs=n_components,transform_algorithm='omp',transform_alpha=None,n_jobs=4)
    print pca
    data = pca.fit_transform(data)     
    #transform data to 64bit!!!
    
    #data = pca.fit(data[::4])
    #data = pca.transform(data)
    

    if isinstance(pca,RandomizedPCA):
      print "PCA components shape:",pca.components_.shape
      print("Explained variance",pca.explained_variance_ratio_.sum())
      plt.plot(pca.explained_variance_ratio_)
      plt.show()
      
    if isinstance(pca,MiniBatchKMeans):
      ck = pca.cluster_centers_
      print "Cluster centers (aka dictionary D (ncomponents x nfeatures) S aka code (nsamples x ncomponents):  SD=Xi):",ck.shape
    
    if showEigenImages:
      #eigenfaces = pca.components_.reshape((n_components, patchSize, patchSize)) 
      eigenfaces = pca.cluster_centers_.reshape((n_components, patchSize, patchSize))
      #eigenfaces = pca.components_
      plt.figure(figsize=(4.2, 4))
      for i, comp in enumerate(eigenfaces[:n_components]):
	  plt.subplot(n_components/3, 3, i + 1)
	  plt.imshow(comp, cmap=plt.cm.gray_r,
		    interpolation='nearest')
	  plt.xticks(())
	  plt.yticks(())
      plt.suptitle('Dictionary learned from patches\n')
      plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)
      plt.show()
      #for img in eigenfaces:
      #	  showImage(img,patchSize,fac=10,matrix=True)
    #  
    #print "Eigen patches shape:",eigenfaces.shape
    print "Extracted patches, after PCA shape:",data.shape, " size (MB):",float(data.nbytes)/1.0E6, " dtype:",data.dtype
    
    ##pooling
    nsamples = data.shape[0]/npatches
    print "number of samples",nsamples
    
    data = np.reshape(data, (nsamples,npatches,-1))
    print "New shape:",data.shape      

    masks = extractBlocks(npatches,blocksize=blocksize)
    #print masks
    
    arrays = np.split(data, nsamples)
    tmp = []
    for i,ar in enumerate(arrays):
      ar = np.reshape(ar, (npatches,n_components))
      if pooling:	
	#pool
	pool = np.zeros((masks.shape[0],n_components))
	for j,m in enumerate(masks):
	  ar_tmp = np.sum(ar[m],axis=0)#sum or mean does not matter
	  #ar_tmp = np.amax(ar[m],axis=0)
	  #ar_tmp = np.mean(ar[m],axis=0)
	  pool[j]=ar_tmp
      
	pool = np.reshape(pool,(masks.shape[0]*n_components))
	tmp.append(pool)
      else:
	ar = np.reshape(ar, (npatches*n_components))
	tmp.append(ar)

      
    #data = np.concatenate(tmp, axis=0) 
    data = np.asarray(tmp)
    print "New shape after pooling:",data.shape, " size (MB):",float(data.nbytes)/1.0E6
  
    lX = pd.DataFrame(data,dtype=np.float32)
    print "datatype:",lX.dtypes[0]
    if ly is not None:
      return (lX,ly)
    else:
      return lX
  params = suggestion.assignments
  print params
  w = int(params['filter_w'])              # SIGOPT param  (filter width in pixels)
  s = int(params['slide_w'])               # SIGOPT param  (held fixed at 2)
  q = params['sparse_p']                   # SIGOPT param  (percentile of active centroid distances)
  n_clust = int(params['km_n_clusters'])   # SIGOPT param  (num of centroids to learn)
  mesh_r  = 2                              # (what is the pooling res in pixels)
  zca_eps = math.exp(params['zca_eps'])

  # stack a random subset image patches, 125K
  X_unlab_patches = []
  random.seed(42)
  print "Gathering examples..."
  # Use subsample of 200K for k-means and covariance estimates
  for i in random.sample(range(0, unlab_X.shape[2]), 200000):
    patches = view_as_windows(unlab_X[:, :, i], (w, w), step=s)
    re_shaped = numpy.reshape(patches, (patches.shape[0]*patches.shape[0], w * w))
    # normalize the patches, per sample
    re_shaped = preprocessing.scale(re_shaped, axis=1)
    X_unlab_patches.append(re_shaped)
  X_unlab_patches = numpy.vstack(X_unlab_patches)

  # build whitening transform matrix
  print "Fitting ZCA Whitening Transform..."
  cov = LedoitWolf()
  cov.fit(X_unlab_patches)  # fit covariance estimate
  D, U = numpy.linalg.eigh(cov.covariance_)
  V = numpy.sqrt(numpy.linalg.inv(numpy.diag(D + zca_eps)))
  Wh = numpy.dot(numpy.dot(U, V), U.T)
  mu = numpy.mean(X_unlab_patches, axis=0)
  X_unlab_patches = numpy.dot(X_unlab_patches-mu, Wh)
def generate_bounding_boxes(model, image, downscale, step, min_height, min_width, file_name, grd_truth_path,
                            trainingFlag, min_prob, result_file, fp_name, overlapThresh=0.5):
    from skimage.util import view_as_windows
    import numpy as np
    from theano import tensor as T
    from theano import function

    arr_tensor4 = T.tensor4('arr', dtype='float32')
    arr_shuffler = arr_tensor4.dimshuffle((1, 0, 2, 3))
    shuffle_function = function([arr_tensor4], arr_shuffler)
    boxes = []
    pyramid_list, scale_list = pyramid(image, downscale, min_height, min_width)
    total_window_list = []
    xy_num_list = []

    for i in xrange(0, len(pyramid_list)):
        window_channels = []
        for channels in xrange(0, len(pyramid_list[i])):
            window = view_as_windows(pyramid_list[i][channels], window_shape=(min_height, min_width), step=step)
            # window.shape[0]= number of columns of windows, window.shape[1]= the number of rows of windows
            xy_num_list.append([window.shape[1], window.shape[0]])
            # min_height & min_width info. should be discarded later for memory saving
            window_reshaped = np.reshape(window, newshape=(window.shape[0] * window.shape[1], min_height, min_width))
            # position of window must be calculated here : window_pos_list
            window_channels.append(window_reshaped)
        # window_list for one pyramid picture
        window_list = np.asarray(window_channels)
        # (3, n, H, W) to (n, 3, H, W)
        window_list = shuffle_function(window_list)
        total_window_list.extend(window_list)

    total_window_list = np.asarray(total_window_list)
    total_window_pos_list = cal_window_position(scale_list, xy_num_list, min_height, min_width, step)
    # classification
    CNN_box_list, CNN_box_pos_list, CNN_prob_list = classify_windows_with_CNN(model, total_window_list,
                                                                              total_window_pos_list, accuracy=min_prob)
    # NMS (overlap threshold can be modified)
    sup_box_list, sup_box_pos_list, sup_box_prob_list = non_max_suppression_fast(CNN_box_list, CNN_box_pos_list,
                                                                                 CNN_prob_list, overlapThresh=0.6)
    # temporary code
    print "Suppressed box list (length: " + str(len(sup_box_pos_list)) + ")"
    for i in range(len(sup_box_pos_list)):
        print "box #" + str(i + 1) + " pos: " + str(sup_box_pos_list[i]) + "  prob: " + str(sup_box_prob_list[i])

    # save suppressed box list to "res" file format
    # format : [frame, x_start, y_start, x_delta, y_delta, prob]
    # annotation file name is 0 based, res frame is 1 based
    # ex) I00000.txt -> frame #1

    # ad-hoc frame naming : take [1:6] (5 digits) and cast as int
    if trainingFlag == 0:
        frame_idx = int(file_name[1:6]) + 1
        for idx in xrange(0, len(sup_box_pos_list)):
            x_start = sup_box_pos_list[idx][0]
            x_delta = sup_box_pos_list[idx][2] - x_start
            y_start = sup_box_pos_list[idx][1]
            y_delta = sup_box_pos_list[idx][3] - y_start
            prob = sup_box_prob_list[idx]
            line = str(frame_idx)+','+str(x_start)+','+str(y_start)+\
                   ','+str(x_delta)+','+str(y_delta)+','+str(prob)+'\n'
            result_file.write(line)

    ground_truth, ground_truth_with_ignore = extract_Daimler_ground_truth(file_name, grd_truth_path, trainingFlag)

    # temporary code
    # ground_truth=[[1,1,10,10]]

    count_tp = extract_fp_examples(file_name, ground_truth, sup_box_list, sup_box_pos_list, sup_box_prob_list, fp_name=fp_name, including_ignore=0,
                                   accThresh=0.5)
    count_tp_with_ignore = extract_fp_examples(file_name, ground_truth_with_ignore, sup_box_list, sup_box_pos_list, sup_box_prob_list, fp_name=fp_name,
                                               including_ignore=1, accThresh=0.5)

    draw_rectangle(sup_box_pos_list, sup_box_prob_list, image, file_name, fp_name)

    if len(sup_box_pos_list) != 0:
        fppi = len(sup_box_pos_list) - count_tp_with_ignore
    else:
        fppi = 0
    if len(ground_truth) != 0:
        miss_rate_per_image = 1 - float(count_tp) / len(ground_truth)
    else:
        miss_rate_per_image = 0
    # temporary return
    return fppi, miss_rate_per_image
    
    return matrix

window_size = 15

img_l = io.imread('HW3_images/scene_l.bmp')
img_r = io.imread('HW3_images/scene_r.bmp')

# Integer division
pad_width = window_size / 2
window_shape = (window_size, window_size)

img_l_padded = pad(img_l, pad_width=pad_width, mode='symmetric')
img_r_padded = pad(img_r, pad_width=pad_width, mode='symmetric')

img_l_view = view_as_windows(img_l_padded, window_shape=window_shape, step=1)
img_r_view = view_as_windows(img_r_padded, window_shape=window_shape, step=1)

img_height, img_width = img_l.shape

result = np.zeros(img_l.shape)

for row in xrange(img_height):
    for col in xrange(img_width):
        
        first_patch = img_l_view[row, col]
        
        first_patch_norm = normalize_matrix(first_patch)
        
        current_scanline = img_r_view[row, :]
        scores = np.zeros(img_width)
def extract_features_img(path, centroids, rfSize, M, U, stride, normal_pooling=True):
    """
    :param path: path to RGB retina image
    :param centroids: learned centroids
    :param rfSize: receptive field size
    :param M: whitening parameter
    :param P: whitening parameter
    :param stride: parameter that defines the density of windows that are extracted from an image
    :param normal_pooling: if true:
                               divide in 4 regions and pool each one
                           else: divide in 16 regions and pool each one
    :return:feature_vector
    """

    img = load(path)
    try:
        img = img[:,:,1]
    except:
        return None

    #contrast enhancing
    img = equalize_adapthist(img)
    numFeats = img.shape[0] * img.shape[1]
    numCentroids = centroids.shape[0]

    #extract dense patches with predefined stride
    #smaller the stride, slower the function
    windows = view_as_windows(img, (rfSize, rfSize), stride)
    patches = np.reshape(windows, (windows.shape[0]*windows.shape[1], rfSize*rfSize))

    #data normalization
    p_mean = np.mean(patches, axis=1, dtype=np.float32, keepdims=True)
    p_var = np.var(patches, axis=1, dtype=np.float32, ddof=1, keepdims=True)
    off_matrix = 10.0 * np.ones(p_var.shape)
    patches = (patches - p_mean) / np.sqrt(p_var + off_matrix)
    
    patches = np.dot((patches - M), U)
    
    #calculate distance from all patches to all centroids
    z = native_cdist(patches, centroids)
    
    #mean distance from each patch to all centroids
    #triangle activation function
    mu = np.tile(np.array([np.mean(z, axis = 1)]).T, (1, centroids.shape[0]))
    patches = np.maximum(mu - z, np.zeros(mu.shape))

    rows = (img.shape[0] - rfSize + stride)/stride
    columns = (img.shape[1] - rfSize + stride)/stride
        
    patches = np.reshape(patches, (rows, columns, numCentroids))

    #starting points
    #central point # of the patches "image"
    halfr = np.round(float(rows)/2)
    halfc = np.round(float(columns)/2)

    #pool quadrants
    if normal_pooling:       
        q1 = np.array([np.sum(np.sum(patches[0:halfc, 0:halfr, :], axis = 1),axis = 0)])
        q2 = np.array([np.sum(np.sum(patches[halfc:patches.shape[0], 0:halfr, :], axis = 1),axis = 0)])
        q3 = np.array([np.sum(np.sum(patches[0:halfc, halfr:patches.shape[1], :], axis = 1),axis = 0)])
        q4 = np.array([np.sum(np.sum(patches[halfc:patches.shape[0], halfr:patches.shape[1], :], axis = 1),axis =     0)])
        feature_vector = np.vstack((q1,q2,q3,q4)).flatten()

    else:
        
        quartr = np.round(float(rows)/4)
        quartc = np.round(float(columns)/2)
        q1 = pool_quadrant(patches, 0, 0, quartc, quartr, halfc, halfr)        
        q2 = pool_quadrant(patches, halfc, 0, quartc, quartr, patches.shape[0], halfr) 
        q3 = pool_quadrant(patches, 0, halfr, quartc, quartr, halfc, patches.shape[1])
        q4 = pool_quadrant(patches, halfc, halfr, quartc, quartr, patches.shape[0], patches.shape[1])
        feature_vector = np.vstack((q1, q2, q3, q4)).flatten()

               
    return feature_vector
def extract_features(path, keys, centroids, rfSize, ImageDim, whitening, M, P, stride):
    #assert(nargin == 4 || nargin == 6)
    numCentroids = centroids.shape[0]
    numSamples = len(keys)
    numFeats = ImageDim[0]*ImageDim[1]*ImageDim[2]
    # compute features for all training images
    XC = np.zeros((numSamples, numCentroids*4))
    labels = np.zeros((numSamples, 1))
    j = 0
    for i in range(numSamples):

        total_start = time.time()
        print "Sample {0}".format(i)
        if (np.mod(i,2000) == 0):
            np.save('test_features_windowsize_{0}_iteration_{1}'.format(rfSize,i), XC)
            print 'Extracting features: ' + str(i) + '/' + str(numSamples)
        
        img = load_image(path + keys[i])
#        X = np.transpose(reader.next()[1]).flatten()
        X = img.flatten()

        # extract overlapping sub-patches into rows of 'patches'
        start = time.time()
        patches = np.vstack(
                (im2col(np.reshape(X[0:numFeats/3],ImageDim[0:2],'F'), (rfSize, rfSize), stride),
                im2col(np.reshape(X[numFeats/3:numFeats*2/3],ImageDim[0:2],'F'), (rfSize, rfSize), stride), im2col(np.reshape(X[numFeats*2/3:numFeats],ImageDim[0:2],'F'), (rfSize, rfSize), stride))).T
        end = time.time()
        
        w = view_as_windows(img, (rfSize,rfsize, 3), stride)
        w = w.reshape((w.shape[0]*w.shape[1],w.shape[3]*w.shape[4]*w.shape[5]))
        print np.array_equal(patches, w)
        from time import sleep
        for i in xrange(w.shape[0]):
            print w[i,0:20]
            print patches[i,500:520]
            sleep(1)

        print "Extract overlapping sub-patches time:{0}".format(end - start)

        # do preprocessing for each patch

    
        # normalize for contrast
        start = time.time()
        patchesMean = np.mean(patches, axis=1, dtype=np.float32, keepdims=True)
        patchesVar = np.var(patches, axis=1, dtype=np.float32, ddof=1, keepdims=True)
        offsetMatrix = 10.0 * np.ones(patchesVar.shape)        
        patches = (patches - patchesMean) / np.sqrt(patchesVar + offsetMatrix)
        end = time.time()
        print "Preprocessing time:{0}".format(end - start)
        # whiten
        if (whitening):
            patches = np.dot((patches - M), P)
        # compute 'triangle' activation function
        start = time.time()
        z = native_cdist(patches, centroids)
        end = time.time()
        print "Triangle time:{0}".format(end - start)

        start = time.time()
        mu = np.tile(np.array([np.mean(z, axis = 1)]).T, (1, centroids.shape[0])) # average distance to centroids for each patch
        patches = np.maximum(mu - z, np.zeros(mu.shape))
        end = time.time()
        print "Distance calculation time:{0}".format(end - start)
        # patches is now the data matrix of activations for each patch

        # reshape to numCentroids-channel image
        start = time.time()
        prows = (ImageDim[0]-rfSize + 1*stride)/stride
        pcols = (ImageDim[1]-rfSize + 1*stride)/stride
        patches = np.reshape(patches, (prows, pcols, numCentroids),'F')
        end = time.time()
        print "Reshaping time:{0}".format(end - start)
        start = time.time()
        # pool over quadrants
        halfr = np.round(float(prows)/2)
        halfc = np.round(float(pcols)/2)
        q1 = np.array([np.sum(np.sum(patches[0:halfc, 0:halfr, :], axis = 1),axis = 0)])
        q2 = np.array([np.sum(np.sum(patches[halfc:patches.shape[0], 0:halfr, :], axis = 1),axis = 0)])
        q3 = np.array([np.sum(np.sum(patches[0:halfc, halfr:patches.shape[1], :], axis = 1),axis = 0)])
        q4 = np.array([np.sum(np.sum(patches[halfc:patches.shape[0], halfr:patches.shape[1], :], axis = 1),axis = 0)])
        end = time.time()
        print "Pooling time:{0}".format(end - start)

        # concatenate into feature vector
        XC[j,:] = np.vstack((q1,q2,q3,q4)).flatten()
        j += 1
        total_end = time.time()
        print "Iteration time:{0}".format(total_end - total_start)

    return XC
Example #56
0
 sv = svm.SVC(verbose = True)
 
 f=np.load('C:/users/attialex/Desktop/training_data.npz')
 X=f['X']
 Y=f['Y']
 print 'training the forest...'
 sv.fit(X,Y)
 print 'done'
 
 
 im = io.imread('M:/attialex/images/lfm/bernhard/template_6_2.png')
 
 imex = im[100:300,200:350]
 
 
 winds = util.view_as_windows(imex,(40,40),step=1)
 h = feature.hog(winds[0][0])
 print 'testing...'
 
 td = np.zeros((winds.shape[0]*winds.shape[1],len(h)))
 counter = 0
 
 print 'creating the feature vector'
 for ii in range(0,winds.shape[0]):
     for jj in range(0,winds.shape[1]):
         td[counter,:]=feature.hog(winds[ii][jj])
         counter+=1
 print 'predicting'
 probs = sv.predict(td)
 
 
Example #57
0
def im2col(img, window_size, stride=1):
    arr = view_as_windows(img, window_size)
    arr = arr[::stride, ::stride, :, :]
    arr = arr.reshape(np.prod(arr.shape[:-2]), window_size, window_size)
    return arr