Python numpy.newaxis() Examples
The following are 30
code examples of numpy.newaxis().
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 also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: __init__.py From EDeN with MIT License | 11 votes |
def plot_confusion_matrix(y_true, y_pred, size=None, normalize=False): """plot_confusion_matrix.""" cm = confusion_matrix(y_true, y_pred) fmt = "%d" if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] fmt = "%.2f" xticklabels = list(sorted(set(y_pred))) yticklabels = list(sorted(set(y_true))) if size is not None: plt.figure(figsize=(size, size)) heatmap(cm, xlabel='Predicted label', ylabel='True label', xticklabels=xticklabels, yticklabels=yticklabels, cmap=plt.cm.Blues, fmt=fmt) if normalize: plt.title("Confusion matrix (norm.)") else: plt.title("Confusion matrix") plt.gca().invert_yaxis()
Example #2
Source File: video_transforms.py From DDPAE-video-prediction with MIT License | 7 votes |
def resize(video, size, interpolation): if interpolation == 'bilinear': inter = cv2.INTER_LINEAR elif interpolation == 'nearest': inter = cv2.INTER_NEAREST else: raise NotImplementedError shape = video.shape[:-3] video = video.reshape((-1, *video.shape[-3:])) resized_video = np.zeros((video.shape[0], size[1], size[0], video.shape[-1])) for i in range(video.shape[0]): img = cv2.resize(video[i], size, inter) if len(img.shape) == 2: img = img[:, :, np.newaxis] resized_video[i] = img return resized_video.reshape((*shape, size[1], size[0], video.shape[-1]))
Example #3
Source File: test_masks.py From mmdetection with Apache License 2.0 | 7 votes |
def test_bitmap_mask_resize(): # resize with empty bitmap masks raw_masks = dummy_raw_bitmap_masks((0, 28, 28)) bitmap_masks = BitmapMasks(raw_masks, 28, 28) resized_masks = bitmap_masks.resize((56, 72)) assert len(resized_masks) == 0 assert resized_masks.height == 56 assert resized_masks.width == 72 # resize with bitmap masks contain 1 instances raw_masks = np.diag(np.ones(4, dtype=np.uint8))[np.newaxis, ...] bitmap_masks = BitmapMasks(raw_masks, 4, 4) resized_masks = bitmap_masks.resize((8, 8)) assert len(resized_masks) == 1 assert resized_masks.height == 8 assert resized_masks.width == 8 truth = np.array([[[1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1]]]) assert (resized_masks.masks == truth).all()
Example #4
Source File: utils.py From mmdetection with Apache License 2.0 | 6 votes |
def encode_mask_results(mask_results): """Encode bitmap mask to RLE code. Args: mask_results (list | tuple[list]): bitmap mask results. In mask scoring rcnn, mask_results is a tuple of (segm_results, segm_cls_score). Returns: list | tuple: RLE encoded mask. """ if isinstance(mask_results, tuple): # mask scoring cls_segms, cls_mask_scores = mask_results else: cls_segms = mask_results num_classes = len(cls_segms) encoded_mask_results = [[] for _ in range(num_classes)] for i in range(len(cls_segms)): for cls_segm in cls_segms[i]: encoded_mask_results[i].append( mask_util.encode( np.array( cls_segm[:, :, np.newaxis], order='F', dtype='uint8'))[0]) # encoded with RLE if isinstance(mask_results, tuple): return encoded_mask_results, cls_mask_scores else: return encoded_mask_results
Example #5
Source File: test_xrft.py From xrft with MIT License | 6 votes |
def test_dft_2d(self): """Test the discrete Fourier transform on 2D data""" N = 16 da = xr.DataArray(np.random.rand(N,N), dims=['x','y'], coords={'x':range(N),'y':range(N)} ) ft = xrft.dft(da, shift=False) npt.assert_almost_equal(ft.values, np.fft.fftn(da.values)) ft = xrft.dft(da, shift=False, window=True, detrend='constant') dim = da.dims window = np.hanning(N) * np.hanning(N)[:, np.newaxis] da_prime = (da - da.mean(dim=dim)).values npt.assert_almost_equal(ft.values, np.fft.fftn(da_prime*window)) da = xr.DataArray(np.random.rand(N,N), dims=['x','y'], coords={'x':range(N,0,-1),'y':range(N,0,-1)} ) assert (xrft.power_spectrum(da, shift=False, density=True) >= 0.).all()
Example #6
Source File: test_xrft.py From xrft with MIT License | 6 votes |
def test_cross_phase_2d(self, dask): Ny, Nx = (32, 16) x = np.linspace(0, 1, num=Nx, endpoint=False) y = np.ones(Ny) f = 6 phase_offset = np.pi/2 signal1 = np.cos(2*np.pi*f*x) # frequency = 1/(2*pi) signal2 = np.cos(2*np.pi*f*x - phase_offset) da1 = xr.DataArray(data=signal1*y[:,np.newaxis], name='a', dims=['y','x'], coords={'y':y, 'x':x}) da2 = xr.DataArray(data=signal2*y[:,np.newaxis], name='b', dims=['y','x'], coords={'y':y, 'x':x}) with pytest.raises(ValueError): xrft.cross_phase(da1, da2, dim=['y','x']) if dask: da1 = da1.chunk({'x': 16}) da2 = da2.chunk({'x': 16}) cp = xrft.cross_phase(da1, da2, dim=['x']) actual_phase_offset = cp.sel(freq_x=f).values npt.assert_almost_equal(actual_phase_offset, phase_offset)
Example #7
Source File: xrft.py From xrft with MIT License | 6 votes |
def _radial_wvnum(k, l, N, nfactor): """ Creates a radial wavenumber based on two horizontal wavenumbers along with the appropriate index map """ # compute target wavenumbers k = k.values l = l.values K = np.sqrt(k[np.newaxis,:]**2 + l[:,np.newaxis]**2) nbins = int(N/nfactor) if k.max() > l.max(): ki = np.linspace(0., l.max(), nbins) else: ki = np.linspace(0., k.max(), nbins) # compute bin index kidx = np.digitize(np.ravel(K), ki) # compute number of points for each wavenumber area = np.bincount(kidx) # compute the average radial wavenumber for each bin kr = (np.bincount(kidx, weights=K.ravel()) / np.ma.masked_where(area==0, area)) return ki, kr[1:-1]
Example #8
Source File: test_train.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License | 6 votes |
def _project_im_rois(im_rois, scales): """Project image RoIs into the image pyramid built by _get_image_blob. Arguments: im_rois (ndarray): R x 4 matrix of RoIs in original image coordinates scales (list): scale factors as returned by _get_image_blob Returns: rois (ndarray): R x 4 matrix of projected RoI coordinates levels (list): image pyramid levels used by each projected RoI """ im_rois = im_rois.astype(np.float, copy=False) if len(scales) > 1: widths = im_rois[:, 2] - im_rois[:, 0] + 1 heights = im_rois[:, 3] - im_rois[:, 1] + 1 areas = widths * heights scaled_areas = areas[:, np.newaxis] * (scales[np.newaxis, :] ** 2) diff_areas = np.abs(scaled_areas - 224 * 224) levels = diff_areas.argmin(axis=1)[:, np.newaxis] else: levels = np.zeros((im_rois.shape[0], 1), dtype=np.int) rois = im_rois * scales[levels] return rois, levels
Example #9
Source File: test.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License | 6 votes |
def _project_im_rois(im_rois, scales): """Project image RoIs into the image pyramid built by _get_image_blob. Arguments: im_rois (ndarray): R x 4 matrix of RoIs in original image coordinates scales (list): scale factors as returned by _get_image_blob Returns: rois (ndarray): R x 4 matrix of projected RoI coordinates levels (list): image pyramid levels used by each projected RoI """ im_rois = im_rois.astype(np.float, copy=False) if len(scales) > 1: widths = im_rois[:, 2] - im_rois[:, 0] + 1 heights = im_rois[:, 3] - im_rois[:, 1] + 1 areas = widths * heights scaled_areas = areas[:, np.newaxis] * (scales[np.newaxis, :] ** 2) diff_areas = np.abs(scaled_areas - 224 * 224) levels = diff_areas.argmin(axis=1)[:, np.newaxis] else: levels = np.zeros((im_rois.shape[0], 1), dtype=np.int) rois = im_rois * scales[levels] return rois, levels
Example #10
Source File: video_transforms.py From DDPAE-video-prediction with MIT License | 6 votes |
def __call__(self, video): """ Args: img (numpy array): Input image, shape (... x H x W x C), dtype uint8. Returns: PIL Image: Color jittered image. """ transforms = self.get_params(self.brightness, self.contrast, self.saturation, self.hue) reshaped_video = video.reshape((-1, *video.shape[-3:])) n_channels = video.shape[-1] for i in range(reshaped_video.shape[0]): img = reshaped_video[i] if n_channels == 1: img = img.squeeze(axis=2) img = Image.fromarray(img) for t in transforms: img = t(img) img = np.array(img) if n_channels == 1: img = img[..., np.newaxis] reshaped_video[i] = img video = reshaped_video.reshape(video.shape) return video
Example #11
Source File: moving_mnist.py From DDPAE-video-prediction with MIT License | 6 votes |
def generate_moving_mnist(self, num_digits=2): ''' Get random trajectories for the digits and generate a video. ''' data = np.zeros((self.n_frames_total, self.image_size_, self.image_size_), dtype=np.float32) for n in range(num_digits): # Trajectory start_y, start_x = self.get_random_trajectory(self.n_frames_total) ind = random.randint(0, self.mnist.shape[0] - 1) digit_image = self.mnist[ind] for i in range(self.n_frames_total): top = start_y[i] left = start_x[i] bottom = top + self.digit_size_ right = left + self.digit_size_ # Draw digit data[i, top:bottom, left:right] = np.maximum(data[i, top:bottom, left:right], digit_image) data = data[..., np.newaxis] return data
Example #12
Source File: convert_story.py From gated-graph-transformer-network with MIT License | 6 votes |
def convert(story): # import pdb; pdb.set_trace() sentence_arr, graphs, query_arr, answer_arr = story node_id_w = graphs[2].shape[2] edge_type_w = graphs[3].shape[3] all_node_strengths = [np.zeros([1])] all_node_ids = [np.zeros([1,node_id_w])] for num_new_nodes, new_node_strengths, new_node_ids, _ in zip(*graphs): last_strengths = all_node_strengths[-1] last_ids = all_node_ids[-1] cur_strengths = np.concatenate([last_strengths, new_node_strengths], 0) cur_ids = np.concatenate([last_ids, new_node_ids], 0) all_node_strengths.append(cur_strengths) all_node_ids.append(cur_ids) all_edges = graphs[3] full_n_nodes = all_edges.shape[1] all_node_strengths = np.stack([np.pad(x, ((0, full_n_nodes-x.shape[0])), 'constant') for x in all_node_strengths[1:]]) all_node_ids = np.stack([np.pad(x, ((0, full_n_nodes-x.shape[0]), (0, 0)), 'constant') for x in all_node_ids[1:]]) all_node_states = np.zeros([len(all_node_strengths), full_n_nodes,0]) return tuple(x[np.newaxis,...] for x in (all_node_strengths, all_node_ids, all_node_states, all_edges))
Example #13
Source File: swiftshader_renderer.py From DOTA_models with Apache License 2.0 | 6 votes |
def render(self, take_screenshot=False, output_type=0): # self.render_timer.tic() self._actual_render() # self.render_timer.toc(log_at=1000, log_str='render timer', type='time') np_rgb_img = None np_d_img = None c = 1000. if take_screenshot: if self.modality == 'rgb': screenshot_rgba = np.zeros((self.height, self.width, 4), dtype=np.uint8) glReadPixels(0, 0, self.width, self.height, GL_RGBA, GL_UNSIGNED_BYTE, screenshot_rgba) np_rgb_img = screenshot_rgba[::-1,:,:3]; if self.modality == 'depth': screenshot_d = np.zeros((self.height, self.width, 4), dtype=np.uint8) glReadPixels(0, 0, self.width, self.height, GL_RGBA, GL_UNSIGNED_BYTE, screenshot_d) np_d_img = screenshot_d[::-1,:,:3]; np_d_img = np_d_img[:,:,2]*(255.*255./c) + np_d_img[:,:,1]*(255./c) + np_d_img[:,:,0]*(1./c) np_d_img = np_d_img.astype(np.float32) np_d_img[np_d_img == 0] = np.NaN np_d_img = np_d_img[:,:,np.newaxis] glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) return np_rgb_img, np_d_img
Example #14
Source File: model.py From models with MIT License | 6 votes |
def predict_on_batch(self, inputs): if inputs.shape == (2,): inputs = inputs[np.newaxis, :] # Encode max_len = len(max(inputs, key=len)) one_hot_ref = self.encode(inputs[:,0]) one_hot_alt = self.encode(inputs[:,1]) # Construct dummy library indicator indicator = np.zeros((inputs.shape[0],2)) indicator[:,1] = 1 # Compute fold change for all three frames fc_changes = [] for shift in range(3): if shift > 0: shifter = np.zeros((one_hot_ref.shape[0],1,4)) one_hot_ref = np.concatenate([one_hot_ref, shifter], axis=1) one_hot_alt = np.concatenate([one_hot_alt, shifter], axis=1) pred_ref = self.model.predict_on_batch([one_hot_ref, indicator]).reshape(-1) pred_variant = self.model.predict_on_batch([one_hot_alt, indicator]).reshape(-1) fc_changes.append(np.log2(pred_variant/pred_ref)) # Return return {"mrl_fold_change":fc_changes[0], "shift_1":fc_changes[1], "shift_2":fc_changes[2]}
Example #15
Source File: common.py From cat-bbs with MIT License | 6 votes |
def draw_heatmap(img, heatmap, alpha=0.5): """Draw a heatmap overlay over an image.""" assert len(heatmap.shape) == 2 or \ (len(heatmap.shape) == 3 and heatmap.shape[2] == 1) assert img.dtype in [np.uint8, np.int32, np.int64] assert heatmap.dtype in [np.float32, np.float64] if img.shape[0:2] != heatmap.shape[0:2]: heatmap_rs = np.clip(heatmap * 255, 0, 255).astype(np.uint8) heatmap_rs = ia.imresize_single_image( heatmap_rs[..., np.newaxis], img.shape[0:2], interpolation="nearest" ) heatmap = np.squeeze(heatmap_rs) / 255.0 cmap = plt.get_cmap('jet') heatmap_cmapped = cmap(heatmap) heatmap_cmapped = np.delete(heatmap_cmapped, 3, 2) heatmap_cmapped = heatmap_cmapped * 255 mix = (1-alpha) * img + alpha * heatmap_cmapped mix = np.clip(mix, 0, 255).astype(np.uint8) return mix
Example #16
Source File: dump_dataloader_files.py From models with MIT License | 5 votes |
def transform(self, x): # impute missing values and rescale the distances xnew = self.preproc_pipeline.transform(self.imp.transform(x)) # convert distances to spline bases dist = {"dist_" + k: encodeSplines(xnew[:, i, np.newaxis], start=0, end=1) for i, k in enumerate(self.POS_FEATURES)} return dist
Example #17
Source File: test_sparse_ndarray.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_sparse_nd_slice(): shape = (rnd.randint(2, 10), rnd.randint(2, 10)) stype = 'csr' A, _ = rand_sparse_ndarray(shape, stype) A2 = A.asnumpy() start = rnd.randint(0, shape[0] - 1) end = rnd.randint(start + 1, shape[0]) assert same(A[start:end].asnumpy(), A2[start:end]) assert same(A[start - shape[0]:end].asnumpy(), A2[start:end]) assert same(A[start:].asnumpy(), A2[start:]) assert same(A[:end].asnumpy(), A2[:end]) ind = rnd.randint(-shape[0], shape[0] - 1) assert same(A[ind].asnumpy(), A2[ind][np.newaxis, :]) start_col = rnd.randint(0, shape[1] - 1) end_col = rnd.randint(start_col + 1, shape[1]) result = mx.nd.slice(A, begin=(start, start_col), end=(end, end_col)) result_dense = mx.nd.slice(mx.nd.array(A2), begin=(start, start_col), end=(end, end_col)) assert same(result_dense.asnumpy(), result.asnumpy()) A = mx.nd.sparse.zeros('csr', shape) A2 = A.asnumpy() assert same(A[start:end].asnumpy(), A2[start:end]) result = mx.nd.slice(A, begin=(start, start_col), end=(end, end_col)) result_dense = mx.nd.slice(mx.nd.array(A2), begin=(start, start_col), end=(end, end_col)) assert same(result_dense.asnumpy(), result.asnumpy()) def check_slice_nd_csr_fallback(shape): stype = 'csr' A, _ = rand_sparse_ndarray(shape, stype) A2 = A.asnumpy() start = rnd.randint(0, shape[0] - 1) end = rnd.randint(start + 1, shape[0]) # non-trivial step should fallback to dense slice op result = mx.nd.sparse.slice(A, begin=(start,), end=(end + 1,), step=(2,)) result_dense = mx.nd.slice(mx.nd.array(A2), begin=(start,), end=(end + 1,), step=(2,)) assert same(result_dense.asnumpy(), result.asnumpy()) shape = (rnd.randint(2, 10), rnd.randint(1, 10)) check_slice_nd_csr_fallback(shape)
Example #18
Source File: test_image.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def _generate_objects(): num = np.random.randint(1, 10) xy = np.random.rand(num, 2) wh = np.random.rand(num, 2) / 2 left = (xy[:, 0] - wh[:, 0])[:, np.newaxis] right = (xy[:, 0] + wh[:, 0])[:, np.newaxis] top = (xy[:, 1] - wh[:, 1])[:, np.newaxis] bot = (xy[:, 1] + wh[:, 1])[:, np.newaxis] boxes = np.maximum(0., np.minimum(1., np.hstack((left, top, right, bot)))) cid = np.random.randint(0, 20, size=num) label = np.hstack((cid[:, np.newaxis], boxes)).ravel().tolist() return [2, 5] + label
Example #19
Source File: bbox.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def bbox_pred(boxes, box_deltas, box_stds): """ Transform the set of class-agnostic boxes into class-specific boxes by applying the predicted offsets (box_deltas) :param boxes: !important [N 4] :param box_deltas: [N, 4 * num_classes] :return: [N 4 * num_classes] """ if boxes.shape[0] == 0: return np.zeros((0, box_deltas.shape[1])) widths = boxes[:, 2] - boxes[:, 0] + 1.0 heights = boxes[:, 3] - boxes[:, 1] + 1.0 ctr_x = boxes[:, 0] + 0.5 * (widths - 1.0) ctr_y = boxes[:, 1] + 0.5 * (heights - 1.0) dx = box_deltas[:, 0::4] * box_stds[0] dy = box_deltas[:, 1::4] * box_stds[1] dw = box_deltas[:, 2::4] * box_stds[2] dh = box_deltas[:, 3::4] * box_stds[3] pred_ctr_x = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis] pred_ctr_y = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis] pred_w = np.exp(dw) * widths[:, np.newaxis] pred_h = np.exp(dh) * heights[:, np.newaxis] pred_boxes = np.zeros(box_deltas.shape) # x1 pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * (pred_w - 1.0) # y1 pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * (pred_h - 1.0) # x2 pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * (pred_w - 1.0) # y2 pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * (pred_h - 1.0) return pred_boxes
Example #20
Source File: kde.py From svviz with MIT License | 5 votes |
def evaluate(self, points): points = atleast_2d(points) d, m = points.shape if d != self.d: if d == 1 and m == self.d: # points was passed in as a row vector points = reshape(points, (self.d, 1)) m = 1 else: msg = "points have dimension %s, dataset has dimension %s" % (d, self.d) raise ValueError(msg) result = zeros((m,), dtype=np.float) if m >= self.n: # there are more points than data, so loop over data for i in range(self.n): diff = self.dataset[:, i, newaxis] - points tdiff = dot(self.inv_cov, diff) energy = sum(diff*tdiff,axis=0) / 2.0 result = result + exp(-energy) else: # loop over points for i in range(m): diff = self.dataset - points[:, i, newaxis] tdiff = dot(self.inv_cov, diff) energy = sum(diff * tdiff, axis=0) / 2.0 result[i] = sum(exp(-energy), axis=0) result = result / self._norm_factor return result
Example #21
Source File: bbox.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def im_detect(rois, scores, bbox_deltas, im_info, bbox_stds, nms_thresh, conf_thresh): """rois (nroi, 4), scores (nrois, nclasses), bbox_deltas (nrois, 4 * nclasses), im_info (3)""" rois = rois.asnumpy() scores = scores.asnumpy() bbox_deltas = bbox_deltas.asnumpy() im_info = im_info.asnumpy() height, width, scale = im_info # post processing pred_boxes = bbox_pred(rois, bbox_deltas, bbox_stds) pred_boxes = clip_boxes(pred_boxes, (height, width)) # we used scaled image & roi to train, so it is necessary to transform them back pred_boxes = pred_boxes / scale # convert to per class detection results det = [] for j in range(1, scores.shape[-1]): indexes = np.where(scores[:, j] > conf_thresh)[0] cls_scores = scores[indexes, j, np.newaxis] cls_boxes = pred_boxes[indexes, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores)) keep = nms(cls_dets, thresh=nms_thresh) cls_id = np.ones_like(cls_scores) * j det.append(np.hstack((cls_id, cls_scores, cls_boxes))[keep, :]) # assemble all classes det = np.concatenate(det, axis=0) return det
Example #22
Source File: model.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def train_step(self, env_xs, env_as, env_rs, env_vs): # NOTE(reed): Reshape to set the data shape. self.model.reshape([('data', (len(env_xs), self.input_size))]) xs = mx.nd.array(env_xs, ctx=self.ctx) as_ = np.array(list(chain.from_iterable(env_as))) # Compute discounted rewards and advantages. advs = [] gamma, lambda_ = self.config.gamma, self.config.lambda_ for i in range(len(env_vs)): # Compute advantages using Generalized Advantage Estimation; # see eqn. (16) of [Schulman 2016]. delta_t = (env_rs[i] + gamma*np.array(env_vs[i][1:]) - np.array(env_vs[i][:-1])) advs.extend(self._discount(delta_t, gamma * lambda_)) # Negative generalized advantage estimations. neg_advs_v = -np.asarray(advs) # NOTE(reed): Only keeping the grads for selected actions. neg_advs_np = np.zeros((len(advs), self.act_space), dtype=np.float32) neg_advs_np[np.arange(neg_advs_np.shape[0]), as_] = neg_advs_v neg_advs = mx.nd.array(neg_advs_np, ctx=self.ctx) # NOTE(reed): The grads of values is actually negative advantages. v_grads = mx.nd.array(self.config.vf_wt * neg_advs_v[:, np.newaxis], ctx=self.ctx) data_batch = mx.io.DataBatch(data=[xs], label=None) self._forward_backward(data_batch=data_batch, out_grads=[neg_advs, v_grads]) self._update_params()
Example #23
Source File: model.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def act(self, ps): us = np.random.uniform(size=ps.shape[0])[:, np.newaxis] as_ = (np.cumsum(ps, axis=1) > us).argmax(axis=1) return as_
Example #24
Source File: super_resolution.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def get_test_image(): """Download and process the test image""" # Load test image input_image_dim = 224 img_url = 'https://s3.amazonaws.com/onnx-mxnet/examples/super_res_input.jpg' download(img_url, 'super_res_input.jpg') img = Image.open('super_res_input.jpg').resize((input_image_dim, input_image_dim)) img_ycbcr = img.convert("YCbCr") img_y, img_cb, img_cr = img_ycbcr.split() input_image = np.array(img_y)[np.newaxis, np.newaxis, :, :] return input_image, img_cb, img_cr
Example #25
Source File: compare_layers.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def read_image(img_path, image_dims=None, mean=None): """ Reads an image from file path or URL, optionally resizing to given image dimensions and subtracting mean. :param img_path: path to file, or url to download :param image_dims: image dimensions to resize to, or None :param mean: mean file to subtract, or None :return: loaded image, in RGB format """ import urllib filename = img_path.split("/")[-1] if img_path.startswith('http'): urllib.urlretrieve(img_path, filename) img = cv2.imread(filename) else: img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) if image_dims is not None: img = cv2.resize(img, image_dims) # resize to image_dims to fit model img = np.rollaxis(img, 2) # change to (c, h, w) order img = img[np.newaxis, :] # extend to (n, c, h, w) if mean is not None: mean = np.array(mean) if mean.shape == (3,): mean = mean[np.newaxis, :, np.newaxis, np.newaxis] # extend to (n, c, 1, 1) img = img.astype(np.float32) - mean # subtract mean return img
Example #26
Source File: model.py From models with MIT License | 5 votes |
def predict_on_batch(self, seq): preds = self.model.predict_on_batch({"seq": seq, **self.neutral_bias_inputs(len(seq), seqlen=seq.shape[1])}) pred_dict = {target: preds[i] for i, target in enumerate(self.target_names)} return {task: softmax(pred_dict[f'{task}/profile']) * np.exp(pred_dict[f'{task}/counts'][:, np.newaxis]) for task in self.tasks}
Example #27
Source File: anchor.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def _mkanchors(ws, hs, x_ctr, y_ctr): """ Given a vector of widths (ws) and heights (hs) around a center (x_ctr, y_ctr), output a set of anchors (windows). """ ws = ws[:, np.newaxis] hs = hs[:, np.newaxis] anchors = np.hstack((x_ctr - 0.5 * (ws - 1), y_ctr - 0.5 * (hs - 1), x_ctr + 0.5 * (ws - 1), y_ctr + 0.5 * (hs - 1))) return anchors
Example #28
Source File: dataloader.py From models with MIT License | 5 votes |
def transform(self, x): # impute missing values and rescale the distances xnew = self.minmax_scaler.transform(self.funct_transform.transform(self.imp.transform(x))) # convert distances to spline bases dist = {"dist_" + k: encodeSplines(xnew[:, i, np.newaxis], start=0, end=1, warn=False) for i, k in enumerate(self.POS_FEATURES)} return dist
Example #29
Source File: utils.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _logits_op(self, x, name=None): with tf.name_scope(name, "logits", [x]) as name: num_classes = self.adversarial.num_classes() def _backward_py(gradient_y, x): x = np.squeeze(x, axis=0) gradient_y = np.squeeze(gradient_y, axis=0) gradient_x = self.adversarial.backward(gradient_y, x) gradient_x = gradient_x.astype(np.float32) return gradient_x[np.newaxis] def _backward_tf(op, grad): images = op.inputs[0] gradient_x = tf.py_func( _backward_py, [grad, images], tf.float32) gradient_x.set_shape(images.shape) return gradient_x def _forward_py(x): predictions = self.adversarial.batch_predictions( x, strict=False)[0] predictions = predictions.astype(np.float32) return predictions op = py_func_grad( _forward_py, [x], [tf.float32], name=name, grad=_backward_tf) logits = op[0] logits.set_shape((x.shape[0], num_classes)) return logits
Example #30
Source File: main.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def attack(model, session, a): fgsm = FastGradientMethod(model, sess=session) image = a.original_image[np.newaxis] return fgsm.generate_np(image)