Python numpy.dstack() Examples
The following are 30 code examples for showing how to use numpy.dstack(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: argus-freesound Author: lRomul File: tiles.py License: MIT License | 6 votes |
def merge(self, tiles: List[np.ndarray], dtype=np.float32): if len(tiles) != len(self.crops): raise ValueError channels = 1 if len(tiles[0].shape) == 2 else tiles[0].shape[2] target_shape = self.image_height + self.margin_bottom + self.margin_top, self.image_width + self.margin_right + self.margin_left, channels image = np.zeros(target_shape, dtype=np.float64) norm_mask = np.zeros(target_shape, dtype=np.float64) w = np.dstack([self.weight] * channels) for tile, (x, y, tile_width, tile_height) in zip(tiles, self.crops): # print(x, y, tile_width, tile_height, image.shape) image[y:y + tile_height, x:x + tile_width] += tile * w norm_mask[y:y + tile_height, x:x + tile_width] += w # print(norm_mask.min(), norm_mask.max()) norm_mask = np.clip(norm_mask, a_min=np.finfo(norm_mask.dtype).eps, a_max=None) normalized = np.divide(image, norm_mask).astype(dtype) crop = self.crop_to_orignal_size(normalized) return crop
Example 2
Project: Chinese-Character-and-Calligraphic-Image-Processing Author: MingtaoGuo File: stylize.py License: MIT License | 6 votes |
def mapping(img): return 255.0 * (img - np.min(img)) / (np.max(img) - np.min(img)) # def read_data(path, batch_size): # filenames = os.listdir(path) # filenames_len = filenames.__len__() # rand_select = np.random.randint(0, filenames_len, [batch_size]) # batch_data = np.zeros([batch_size, 256, 256, 3]) # for i in range(batch_size): # img = np.array(Image.open(path + filenames[rand_select[i]]).resize([256, 256])) # try: # if img.shape.__len__() == 3: # batch_data[i, :, :, :] = img[:256, :256, :3] # else: # batch_data[i, :, :, :] = np.dstack((img, img, img))[:256, :256, :] # except: # img = np.array(Image.open(path + filenames[0])) # batch_data[i, :, :, :] = img[:256, :256, :3] # return batch_data
Example 3
Project: Chinese-Character-and-Calligraphic-Image-Processing Author: MingtaoGuo File: style_transfer_realtime.py License: MIT License | 6 votes |
def mapping(img): return 255.0 * (img - np.min(img)) / (np.max(img) - np.min(img)) # def read_data(path, batch_size): # filenames = os.listdir(path) # filenames_len = filenames.__len__() # rand_select = np.random.randint(0, filenames_len, [batch_size]) # batch_data = np.zeros([batch_size, 256, 256, 3]) # for i in range(batch_size): # img = np.array(Image.open(path + filenames[rand_select[i]]).resize([256, 256])) # try: # if img.shape.__len__() == 3: # batch_data[i, :, :, :] = img[:256, :256, :3] # else: # batch_data[i, :, :, :] = np.dstack((img, img, img))[:256, :256, :] # except: # img = np.array(Image.open(path + filenames[0])) # batch_data[i, :, :, :] = img[:256, :256, :3] # return batch_data
Example 4
Project: Chinese-Character-and-Calligraphic-Image-Processing Author: MingtaoGuo File: pix2pix.py License: MIT License | 6 votes |
def train(self): list = os.listdir(self.path) nums_file = list.__len__() saver = tf.train.Saver() for i in range(10000): rand_select = np.random.randint(0, nums_file, [self.batch_size]) INPUTS = np.zeros([self.batch_size, self.img_h, self.img_w, 3]) INPUTS_CONDITION = np.zeros([self.batch_size, self.img_h, self.img_w, 3]) for j in range(self.batch_size): img = np.array(Image.open(self.path + list[rand_select[j]])) img_h, img_w = img.shape[0], img.shape[1] INPUT_CON = misc.imresize(img[:, :img_w//2], [self.img_h, self.img_w]) / 127.5 - 1.0 INPUTS_CONDITION[j] = np.dstack((INPUT_CON, INPUT_CON, INPUT_CON)) INPUT = misc.imresize(img[:, img_w//2:], [self.img_h, self.img_w]) / 127.5 - 1.0 INPUTS[j] = np.dstack((INPUT, INPUT, INPUT)) self.sess.run(self.opt_dis, feed_dict={self.inputs: INPUTS, self.inputs_condition: INPUTS_CONDITION}) self.sess.run(self.opt_gen, feed_dict={self.inputs: INPUTS, self.inputs_condition: INPUTS_CONDITION}) if i % 10 == 0: [G_LOSS, D_LOSS] = self.sess.run([self.g_loss, self.d_loss], feed_dict={self.inputs: INPUTS, self.inputs_condition: INPUTS_CONDITION}) print("Iteration: %d, d_loss: %f, g_loss: %f"%(i, D_LOSS, G_LOSS)) if i % 100 == 0: saver.save(self.sess, "./save_para//model.ckpt")
Example 5
Project: pyLucid Author: yelantingfeng File: lucidDream.py License: MIT License | 6 votes |
def thin_plate_transform(x,y,offw,offh,imshape,shift_l=-0.05,shift_r=0.05,num_points=5,offsetMatrix=False): rand_p=np.random.choice(x.size,num_points,replace=False) movingPoints=np.zeros((1,num_points,2),dtype='float32') fixedPoints=np.zeros((1,num_points,2),dtype='float32') movingPoints[:,:,0]=x[rand_p] movingPoints[:,:,1]=y[rand_p] fixedPoints[:,:,0]=movingPoints[:,:,0]+offw*(np.random.rand(num_points)*(shift_r-shift_l)+shift_l) fixedPoints[:,:,1]=movingPoints[:,:,1]+offh*(np.random.rand(num_points)*(shift_r-shift_l)+shift_l) tps=cv2.createThinPlateSplineShapeTransformer() good_matches=[cv2.DMatch(i,i,0) for i in range(num_points)] tps.estimateTransformation(movingPoints,fixedPoints,good_matches) imh,imw=imshape x,y=np.meshgrid(np.arange(imw),np.arange(imh)) x,y=x.astype('float32'),y.astype('float32') newxy=tps.applyTransformation(np.dstack((x.ravel(),y.ravel())))[1] newxy=newxy.reshape([imh,imw,2]) if offsetMatrix: return newxy,newxy-np.dstack((x,y)) else: return newxy
Example 6
Project: lattice Author: tensorflow File: lattice_test.py License: Apache License 2.0 | 6 votes |
def _GetTrainingInputsAndLabels(self, config): """Generates training inputs and labels. Args: config: Dictionary with config for this unit test. Returns: Tuple `(training_inputs, training_labels, raw_training_inputs)` where `training_inputs` and `training_labels` are data for training and `raw_training_inputs` are representation of training_inputs for visualisation. """ raw_training_inputs = config["x_generator"]( num_points=config["num_training_records"], lattice_sizes=config["lattice_sizes"]) if isinstance(raw_training_inputs, tuple): # This means that raw inputs are 2-d mesh grid. Convert them into list of # 2-d points. training_inputs = list(np.dstack(raw_training_inputs).reshape((-1, 2))) else: training_inputs = raw_training_inputs training_labels = [config["y_function"](x) for x in training_inputs] return training_inputs, training_labels, raw_training_inputs
Example 7
Project: Advanced_Lane_Lines Author: ChengZhongShen File: image_process.py License: MIT License | 6 votes |
def color_grid_thresh(img, s_thresh=(170,255), sx_thresh=(20, 100)): img = np.copy(img) # Convert to HLS color space and separate the V channel hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS) l_channel = hls[:,:,1] s_channel = hls[:,:,2] # Sobel x sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0) # Take the derivateive in x abs_sobelx = np.absolute(sobelx) # Absolute x derivateive to accentuate lines scaled_sobel = np.uint8(255*abs_sobelx/np.max(abs_sobelx)) # Threshold x gradient sxbinary = np.zeros_like(scaled_sobel) sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1 # Threshold color channel s_binary = np.zeros_like(s_channel) s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1 # combine the two binary binary = sxbinary | s_binary # Stack each channel (for visual check the pixal sourse) # color_binary = np.dstack((np.zeros_like(sxbinary), sxbinary,s_binary)) * 255 return binary
Example 8
Project: Advanced_Lane_Lines Author: ChengZhongShen File: image_process.py License: MIT License | 6 votes |
def draw_lane_fit(undist, warped ,Minv, left_fitx, right_fitx, ploty): # Drawing # Create an image to draw the lines on warp_zero = np.zeros_like(warped).astype(np.uint8) color_warp = np.dstack((warp_zero, warp_zero, warp_zero)) # Recast the x and y points into usable format for cv2.fillPoly() pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))]) pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))]) pts = np.hstack((pts_left, pts_right)) # Draw the lane onto the warped blank image cv2.fillPoly(color_warp, np.int_([pts]), (0,255,0)) # Warp the blank back to original image space using inverse perspective matrix(Minv) newwarp = cv2.warpPerspective(color_warp, Minv, (undist.shape[1], undist.shape[0])) # Combine the result with the original image result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0) return result
Example 9
Project: ibllib Author: int-brain-lab File: population.py License: MIT License | 6 votes |
def _symmetrize_correlograms(correlograms): """Return the symmetrized version of the CCG arrays.""" n_clusters, _, n_bins = correlograms.shape assert n_clusters == _ # We symmetrize c[i, j, 0]. # This is necessary because the algorithm in correlograms() # is sensitive to the order of identical spikes. correlograms[..., 0] = np.maximum( correlograms[..., 0], correlograms[..., 0].T) sym = correlograms[..., 1:][..., ::-1] sym = np.transpose(sym, (1, 0, 2)) return np.dstack((sym, correlograms))
Example 10
Project: aboleth Author: gradientinstitute File: test_hlayers.py License: Apache License 2.0 | 6 votes |
def test_concat(make_data): """Test concatenation layer.""" x, _, X = make_data # This replicates the input layer behaviour f = ab.InputLayer('X', n_samples=3) g = ab.InputLayer('Y', n_samples=3) catlayer = ab.Concat(f, g) F, KL = catlayer(X=x, Y=x) tc = tf.test.TestCase() with tc.test_session(): forked = F.eval() orig = X.eval() assert forked.shape == orig.shape[0:2] + (2 * orig.shape[2],) assert np.all(forked == np.dstack((orig, orig))) assert KL.eval() == 0.0
Example 11
Project: rl_algorithms Author: DanielTakeshi File: plotting.py License: MIT License | 6 votes |
def plot_cost_to_go_mountain_car(env, estimator, num_tiles=20): x = np.linspace(env.observation_space.low[0], env.observation_space.high[0], num=num_tiles) y = np.linspace(env.observation_space.low[1], env.observation_space.high[1], num=num_tiles) X, Y = np.meshgrid(x, y) Z = np.apply_along_axis(lambda _: -np.max(estimator.predict(_)), 2, np.dstack([X, Y])) fig = plt.figure(figsize=(10, 5)) ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=matplotlib.cm.coolwarm, vmin=-1.0, vmax=1.0) ax.set_xlabel('Position') ax.set_ylabel('Velocity') ax.set_zlabel('Value') ax.set_title("Mountain \"Cost To Go\" Function") fig.colorbar(surf) plt.show()
Example 12
Project: mars Author: mars-project File: test_merge_execute.py License: Apache License 2.0 | 6 votes |
def testDStackExecution(self): a_data = np.random.rand(10) b_data = np.random.rand(10) a = tensor(a_data, chunk_size=4) b = tensor(b_data, chunk_size=4) c = dstack([a, b]) res = self.executor.execute_tensor(c, concat=True)[0] expected = np.dstack([a_data, b_data]) self.assertTrue(np.array_equal(res, expected)) a_data = np.random.rand(10, 20) b_data = np.random.rand(10, 20) a = tensor(a_data, chunk_size=3) b = tensor(b_data, chunk_size=4) c = dstack([a, b]) res = self.executor.execute_tensor(c, concat=True)[0] expected = np.dstack([a_data, b_data]) self.assertTrue(np.array_equal(res, expected))
Example 13
Project: bop_toolkit Author: thodan File: misc.py License: MIT License | 6 votes |
def depth_im_to_dist_im(depth_im, K): """Converts a depth image to a distance image. :param depth_im: hxw ndarray with the input depth image, where depth_im[y, x] is the Z coordinate of the 3D point [X, Y, Z] that projects to pixel [x, y], or 0 if there is no such 3D point (this is a typical output of the Kinect-like sensors). :param K: 3x3 ndarray with an intrinsic camera matrix. :return: hxw ndarray with the distance image, where dist_im[y, x] is the distance from the camera center to the 3D point [X, Y, Z] that projects to pixel [x, y], or 0 if there is no such 3D point. """ xs, ys = np.meshgrid( np.arange(depth_im.shape[1]), np.arange(depth_im.shape[0])) Xs = np.multiply(xs - K[0, 2], depth_im) * (1.0 / K[0, 0]) Ys = np.multiply(ys - K[1, 2], depth_im) * (1.0 / K[1, 1]) dist_im = np.sqrt( Xs.astype(np.float64)**2 + Ys.astype(np.float64)**2 + depth_im.astype(np.float64)**2) # dist_im = np.linalg.norm(np.dstack((Xs, Ys, depth_im)), axis=2) # Slower. return dist_im
Example 14
Project: pyswarms Author: ljvmiranda921 File: formatters.py License: MIT License | 6 votes |
def compute_history_3d(self, pos_history): """Compute a 3D position matrix The first two columns are the 2D position in the x and y axes respectively, while the third column is the fitness on that given position. Parameters ---------- pos_history : numpy.ndarray Two-dimensional position matrix history of shape :code:`(iterations, n_particles, 2)` Returns ------- numpy.ndarray 3D position matrix of shape :code:`(iterations, n_particles, 3)` """ fitness = np.array(list(map(self.func, pos_history))) return np.dstack((pos_history, fitness))
Example 15
Project: snowy Author: prideout File: test_color.py License: MIT License | 6 votes |
def test_luminance(): source = sn.load('tests/sobel_input.png')[:,:,:3] L = rgb2gray(source) skresult = np.dstack([L, L, L]) small_skresult = sn.resize(skresult, width=256) L = sn.rgb_to_luminance(source) snresult = np.dstack([L, L, L]) small_snresult = sn.resize(snresult, width=256) L = skimage_sobel(source) sksobel = np.dstack([L, L, L]) small_sksobel = sn.resize(sksobel, width=256) L = sn.rgb_to_luminance(source) L = sn.compute_sobel(L) snsobel = np.dstack([L, L, L]) small_snsobel = sn.resize(snsobel, width=256) sn.show(np.hstack([ small_skresult, small_snresult, small_sksobel, small_snsobel]))
Example 16
Project: ARU-Net Author: TobiasGruening File: util.py License: GNU General Public License v2.0 | 5 votes |
def elastic_transform(image,elastic_value_x ,elastic_value_y): """Elastic deformation of images as described in [Simard2003]_ (with modifications JUST in Y-DIRECTION). .. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for Convolutional Neural Networks applied to Visual Document Analysis", in Proc. of the International Conference on Document Analysis and Recognition, 2003. Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5 """ shape = image.shape random_state = np.random.RandomState(None) nY = shape[0] // 25 nX = shape[1] // 25 sigma = min(shape[1], shape[0]) * 0.0025 alpha_X = elastic_value_x * min(shape[0], shape[1]) alpha_Y = elastic_value_y * min(shape[0], shape[1]) dx = gaussian_filter((random_state.rand(nY, nX) * 2 - 1), sigma) dy = gaussian_filter((random_state.rand(nY, nX) * 2 - 1), sigma) x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2])) dx = misc.imresize(dx, [shape[0], shape[1]], interp='bicubic') dy = misc.imresize(dy, [shape[0], shape[1]], interp='bicubic') # plt.imshow(dx, cmap=plt.cm.gray) # plt.show() dxT = [] dyT = [] for dummy in range(shape[2]): dxT.append(dx) dyT.append(dy) dx = np.dstack(dxT) dy = np.dstack(dyT) dx = dx * alpha_X dy = dy * alpha_Y indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1)), np.reshape(z, (-1, 1)) image = map_coordinates(image, indices, order=1).reshape(shape) return image
Example 17
Project: scarlet Author: pmelchior File: display.py License: MIT License | 5 votes |
def img_to_rgb(img, channel_map=None, fill_value=0, norm=None, mask=None): """Convert images to normalized RGB. If normalized values are outside of the range [0..255], they will be truncated such as to preserve the corresponding color. Parameters ---------- img: array_like This should be an array with dimensions (channels, height, width). channel_map: array_like Linear mapping with dimensions (3, channels) fill_value: float, default=`0` Value to use for any masked pixels. norm: `scarlet.display.Norm`, default `None` Norm to use for mapping in the allowed range [0..255]. If `norm=None`, `scarlet.display.LinearPercentileNorm` will be used. mask: array_like A [0,1] binary mask to apply over the top of the image, where pixels with mask==1 are masked out. Returns ------- rgb: numpy array with dimensions (3, height, width) and dtype uint8 """ RGB = img_to_3channel(img, channel_map=channel_map) if norm is None: norm = LinearMapping(image=RGB) rgb = norm.make_rgb_image(*RGB) if mask is not None: rgb = np.dstack([rgb, ~mask * 255]) return rgb
Example 18
Project: scarlet Author: pmelchior File: interpolation.py License: MIT License | 5 votes |
def get_filter_coords(filter_values, center=None): """Create filter coordinate grid needed for the apply filter function Parameters ---------- filter_values: array The 2D array of the filter to apply. center: tuple The center (y,x) of the filter. If `center` is `None` then `filter_values` must have an odd number of rows and columns and the center will be set to the center of `filter_values`. Returns ------- coords: array The coordinates of the pixels in `filter_values`, where the coordinates of the `center` pixel are `(0,0)`. """ if len(filter_values.shape) != 2: raise ValueError("`filter_values` must be 2D") if center is None: if filter_values.shape[0] % 2 == 0 or filter_values.shape[1] % 2 == 0: msg = """Ambiguous center of the `filter_values` array, you must use a `filter_values` array with an odd number of rows and columns or calculate `coords` on your own.""" raise ValueError(msg) center = [filter_values.shape[0]//2, filter_values.shape[1]//2] x = np.arange(filter_values.shape[1]) y = np.arange(filter_values.shape[0]) x, y = np.meshgrid(x, y) x -= center[1] y -= center[0] coords = np.dstack([y, x]) return coords
Example 19
Project: Chinese-Character-and-Calligraphic-Image-Processing Author: MingtaoGuo File: stylize.py License: MIT License | 5 votes |
def read_data(path_perfect, batch_size, img_size=256): def random_crop(img, crop_size=256): h = img.shape[0] w = img.shape[1] if h < crop_size or w < crop_size: if h < w: img = misc.imresize(img[:, :h], [crop_size, crop_size]) else: img = misc.imresize(img[:w, :], [crop_size, crop_size]) return img start_y = np.random.randint(0, h-crop_size+1) start_x = np.random.randint(0, w-crop_size+1) return img[start_y:start_y+crop_size, start_x:start_x+crop_size] filenames_perfect = os.listdir(path_perfect) data_perfect = np.zeros([batch_size, img_size, img_size, 3]) rand_select_perfect = np.random.randint(0, filenames_perfect.__len__(), [batch_size]) for i in range(batch_size): #read the perfect data img = np.array(Image.open(path_perfect+filenames_perfect[rand_select_perfect[i]])) shape_list = img.shape if shape_list.__len__() < 3: img = np.dstack((img, img, img)) # h, w = shape_list[0], shape_list[1] # img = misc.imresize(img, [int(256 * h / w), 256]) data_perfect[i] = random_crop(img[:,:,:3], crop_size=256) return data_perfect
Example 20
Project: Chinese-Character-and-Calligraphic-Image-Processing Author: MingtaoGuo File: style_transfer_realtime.py License: MIT License | 5 votes |
def read_data(path_perfect, batch_size, img_size=256): def random_crop(img, crop_size=256): h = img.shape[0] w = img.shape[1] if h < crop_size or w < crop_size: if h < w: img = misc.imresize(img[:, :h], [crop_size, crop_size]) else: img = misc.imresize(img[:w, :], [crop_size, crop_size]) return img start_y = np.random.randint(0, h-crop_size+1) start_x = np.random.randint(0, w-crop_size+1) return img[start_y:start_y+crop_size, start_x:start_x+crop_size] filenames_perfect = os.listdir(path_perfect) data_perfect = np.zeros([batch_size, img_size, img_size, 3]) rand_select_perfect = np.random.randint(0, filenames_perfect.__len__(), [batch_size]) for i in range(batch_size): #read the perfect data img = np.array(Image.open(path_perfect+filenames_perfect[rand_select_perfect[i]])) shape_list = img.shape if shape_list.__len__() < 3: img = np.dstack((img, img, img)) # h, w = shape_list[0], shape_list[1] # img = misc.imresize(img, [int(256 * h / w), 256]) data_perfect[i] = random_crop(img[:,:,:3], crop_size=256) return data_perfect
Example 21
Project: EarthSim Author: pyviz-topics File: grabcut.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def _process(self, element, key=None): try: import cv2 as cv except: # HACK: Avoids error loading OpenCV the first time # ImportError dlopen: cannot load any more object with static TLS try: import cv2 as cv except ImportError: raise ImportError('GrabCut algorithm requires openCV') if isinstance(self.p.foreground, hv.Polygons): rasterize_op = rasterize_polygon else: rasterize_op = rasterize.instance(aggregator=ds.any()) kwargs = {'dynamic': False, 'target': element} fg_mask = rasterize_op(self.p.foreground, **kwargs) bg_mask = rasterize_op(self.p.background, **kwargs) fg_mask = fg_mask.dimension_values(2, flat=False) bg_mask = bg_mask.dimension_values(2, flat=False) if fg_mask[np.isfinite(fg_mask)].sum() == 0 or bg_mask[np.isfinite(bg_mask)].sum() == 0: return element.clone([], vdims=['Foreground'], new_type=gv.Image, crs=element.crs) mask = np.where(fg_mask, 1, 2) mask = np.where(bg_mask, 0, mask).copy() bgdModel = np.zeros((1,65), np.float64) fgdModel = np.zeros((1,65), np.float64) if isinstance(element, hv.RGB): img = np.dstack([element.dimension_values(d, flat=False) for d in element.vdims]) else: img = element.dimension_values(2, flat=False) mask, _, _ = cv.grabCut(img, mask.astype('uint8'), None, bgdModel, fgdModel, self.p.iterations, cv.GC_INIT_WITH_MASK) fg_mask = np.where((mask==2)|(mask==0),0,1).astype('bool') xs, ys = (element.dimension_values(d, expanded=False) for d in element.kdims) return element.clone((xs, ys, fg_mask), vdims=['Foreground'], new_type=gv.Image, crs=element.crs)
Example 22
Project: pyscf Author: pyscf File: mpi_helper.py License: Apache License 2.0 | 5 votes |
def generate_task_list(chunk_size, array_size): segs = [range(int(numpy.ceil(array_size[i]*1./chunk_size[i]))) for i in range(len(array_size))] task_id = numpy.array(cartesian_prod(segs)) task_ranges_lower = task_id * numpy.array(chunk_size) task_ranges_upper = numpy.minimum((task_id+1)*numpy.array(chunk_size),array_size) return list(numpy.dstack((task_ranges_lower,task_ranges_upper)))
Example 23
Project: kaggle-carvana-2017 Author: killthekitten File: predict_multithreaded.py License: MIT License | 5 votes |
def data_loader(q, ): for start in tqdm(range(0, len(filenames), batch_size)): x_batch = [] end = min(start + batch_size, len(filenames)) filenames_batch = filenames[start:end] for filename in filenames_batch: img = load_img(filename) stacked_channels = [] for i in range(args.stacked_channels): channel_path = os.path.join(args.stacked_channels_dir, str(i), filename.split('/')[-1].replace('.jpg', '.png')) stacked_channel = load_img(channel_path, grayscale=True) stacked_channels.append(stacked_channel) stacked_img = np.dstack((img, *stacked_channels)) x_batch.append(img_to_array(stacked_img)) x_batch = preprocess_input(np.array(x_batch, np.float32), mode=args.preprocessing_function) if args.pred_tta: x_batch = do_tta(x_batch, args.pred_tta) padded_x = np.zeros((batch_size, 1280, 1920, args.stacked_channels + 3)) padded_x[:, :, 1:-1, :] = x_batch q.put((filenames_batch, padded_x)) for gpu in gpus: q.put((None, None))
Example 24
Project: kaggle-carvana-2017 Author: killthekitten File: datasets.py License: MIT License | 5 votes |
def build_batch_generator(filenames, img_dir=None, batch_size=None, shuffle=False, transformations=None, out_size=None, crop_size=None, mask_dir=None, aug=False): mask_function = ImageWithMaskFunction(out_size=out_size, crop_size=crop_size, mask_dir=mask_dir) while True: # @TODO: Should we fixate the seed here? if shuffle: filenames = sklearn.utils.shuffle(filenames) for start in range(0, len(filenames), batch_size): batch_x = [] end = min(start + batch_size, len(filenames)) train_batch = filenames[start:end] for filename in train_batch: img = imread(os.path.join(img_dir, filename)) stacked_channels = [] for i in range(args.stacked_channels): channel_path = os.path.join(args.stacked_channels_dir, str(i), filename.replace('.jpg', '.png')) stacked_channel = imread(channel_path, mode='L') stacked_channels.append(stacked_channel) stacked_img = np.dstack((img, *stacked_channels)) batch_x.append(stacked_img) batch_x = np.array(batch_x, np.float32) batch_x, masks = mask_function.mask_pred(batch_x, train_batch, range(batch_size), aug) if crop_size is None: # @TODO: Remove hardcoded padding batch_x, masks = pad(batch_x, 1, 0), pad(masks, 1, 0) yield imagenet_utils.preprocess_input(batch_x, mode=args.preprocessing_function), masks
Example 25
Project: GroundedTranslation Author: elliottd File: plot_tsne.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def gray_to_color(img): if len(img.shape) == 2: img = np.dstack((img, img, img)) return img
Example 26
Project: pyLucid Author: yelantingfeng File: lucidDream.py License: MIT License | 5 votes |
def blend_mask_multi(seg_or, img, back_img, mask): if img.ndim==2 or img.shape[2]==1: img=np.dstack((img,)*3) M,N=np.where(mask>0) if M.size==0: return back_img,seg_or topM,bottomM=M.min(),M.max() leftN,rightN=N.min(),N.max() msk2=mask[topM:bottomM+1,leftN:rightN+1] img2=img[topM:bottomM+1,leftN:rightN+1,:] scale_l=0.85 scale_r=1.15 scaleval=np.random.rand()*(scale_r-scale_l)+scale_l ih=min(max(int(round(msk2.shape[0]*scaleval)),100),back_img.shape[0]-50) iw=min(max(int(round(msk2.shape[1]*scaleval)),100),back_img.shape[1]-50) imMask=cv2.resize(msk2,(iw,ih),interpolation=cv2.INTER_NEAREST) img2=cv2.resize(img2,(iw,ih)) max_offY=back_img.shape[0]-imMask.shape[0] max_offX=back_img.shape[1]-imMask.shape[1] offY=np.random.randint(max_offY+1) offX=np.random.randint(max_offX+1) clonedIm2,new_msk=SeamlessClone_trimap(img2,back_img,imMask,offX,offY) new_msk2=seg_or.copy() new_msk2[new_msk>0]=new_msk[new_msk>0] return clonedIm2,new_msk2
Example 27
Project: DPC Author: TengdaHan File: augmentation.py License: MIT License | 5 votes |
def grayscale(self, img): channel = np.random.choice(3) np_img = np.array(img)[:,:,channel] np_img = np.dstack([np_img, np_img, np_img]) img = Image.fromarray(np_img, 'RGB') return img
Example 28
Project: lattice Author: tensorflow File: linear_test.py License: Apache License 2.0 | 5 votes |
def _GetTrainingInputsAndLabels(self, config): """Generates training inputs and labels. Args: config: Dict with config for this unit test. Returns: Tuple `(training_inputs, training_labels, raw_training_inputs)` where `training_inputs` and `training_labels` are data for training and `raw_training_inputs` are representation of `training_inputs` for visualisation. """ raw_training_inputs = config["x_generator"]( num_points=config["num_training_records"], num_dims=config["num_input_dims"], input_min=config["input_min"], input_max=config["input_max"]) if isinstance(raw_training_inputs, tuple): # This means that raw inputs are 2-d mesh grid. Convert them into list of # 2-d points. training_inputs = list(np.dstack(raw_training_inputs).reshape((-1, 2))) else: training_inputs = raw_training_inputs training_labels = [config["y_function"](x) for x in training_inputs] return training_inputs, training_labels, raw_training_inputs
Example 29
Project: Advanced_Lane_Lines Author: ChengZhongShen File: image_process.py License: MIT License | 5 votes |
def color_grid_thresh_dynamic(img, s_thresh=(170,255), sx_thresh=(20, 100)): img = np.copy(img) height = img.shape[0] width = img.shape[1] # Convert to HLS color space and separate the V channel hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS) l_channel = hls[:,:,1] s_channel = hls[:,:,2] # Sobel x sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0) # Take the derivateive in x abs_sobelx = np.absolute(sobelx) # Absolute x derivateive to accentuate lines scaled_sobel = np.uint8(255*abs_sobelx/np.max(abs_sobelx)) # Threshold x gradient sxbinary = np.zeros_like(scaled_sobel) sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1 # Threshold color channel s_binary = np.zeros_like(s_channel) s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1 sxbinary[:, :width//2] = 0 # use the left side s_binary[:,width//2:] = 0 # use the right side # combine the two binary binary = sxbinary | s_binary # Stack each channel (for visual check the pixal sourse) # color_binary = np.dstack((np.zeros_like(sxbinary), sxbinary,s_binary)) * 255 return binary
Example 30
Project: Advanced_Lane_Lines Author: ChengZhongShen File: image_process.py License: MIT License | 5 votes |
def yellow_grid_thresh(img, y_low=(10,50,0), y_high=(30,255,255), sx_thresh=(20, 100)): img = np.copy(img) # Convert to HLS color space and separate the V channel hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS) l_channel = hls[:,:,1] s_channel = hls[:,:,2] # Sobel x sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0) # Take the derivateive in x abs_sobelx = np.absolute(sobelx) # Absolute x derivateive to accentuate lines scaled_sobel = np.uint8(255*abs_sobelx/np.max(abs_sobelx)) # Threshold x gradient sxbinary = np.zeros_like(scaled_sobel) sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1 # # Threshold color channel # s_binary = np.zeros_like(s_channel) # s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1 yellow_filtered = yellow_filter(img, y_low, y_high) yellow_filtered[yellow_filtered > 0] = 1 # transfer to binary # combine the two binary, right and left sxbinary[:,:640] = 0 # use right side of sxbinary yellow_filtered[:,640:] = 0 # use left side of yellow filtered binary = sxbinary | yellow_filtered # Stack each channel (for visual check the pixal sourse) # color_binary = np.dstack((np.zeros_like(sxbinary), sxbinary,s_binary)) * 255 return binary