Python numpy.min() Examples
The following are 30 code examples for showing how to use numpy.min(). 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: cgp-cnn Author: sg-nm File: cgp.py License: MIT License | 6 votes |
def mutation(self, mutation_rate=0.01): active_check = False for n in range(self.net_info.node_num + self.net_info.out_num): t = self.gene[n][0] # mutation for type gene type_num = self.net_info.func_type_num if n < self.net_info.node_num else self.net_info.out_type_num if np.random.rand() < mutation_rate and type_num > 1: self.gene[n][0] = self.__mutate(self.gene[n][0], 0, type_num) if self.is_active[n]: active_check = True # mutation for connection gene col = np.min((int(n / self.net_info.rows), self.net_info.cols)) max_connect_id = col * self.net_info.rows + self.net_info.input_num min_connect_id = (col - self.net_info.level_back) * self.net_info.rows + self.net_info.input_num \ if col - self.net_info.level_back >= 0 else 0 in_num = self.net_info.func_in_num[t] if n < self.net_info.node_num else self.net_info.out_in_num[t] for i in range(self.net_info.max_in_num): if np.random.rand() < mutation_rate and max_connect_id - min_connect_id > 1: self.gene[n][i+1] = self.__mutate(self.gene[n][i+1], min_connect_id, max_connect_id) if self.is_active[n] and i < in_num: active_check = True self.check_active() return active_check
Example 2
Project: cgp-cnn Author: sg-nm File: cgp.py License: MIT License | 6 votes |
def neutral_mutation(self, mutation_rate=0.01): for n in range(self.net_info.node_num + self.net_info.out_num): t = self.gene[n][0] # mutation for type gene type_num = self.net_info.func_type_num if n < self.net_info.node_num else self.net_info.out_type_num if not self.is_active[n] and np.random.rand() < mutation_rate and type_num > 1: self.gene[n][0] = self.__mutate(self.gene[n][0], 0, type_num) # mutation for connection gene col = np.min((int(n / self.net_info.rows), self.net_info.cols)) max_connect_id = col * self.net_info.rows + self.net_info.input_num min_connect_id = (col - self.net_info.level_back) * self.net_info.rows + self.net_info.input_num \ if col - self.net_info.level_back >= 0 else 0 in_num = self.net_info.func_in_num[t] if n < self.net_info.node_num else self.net_info.out_in_num[t] for i in range(self.net_info.max_in_num): if (not self.is_active[n] or i >= in_num) and np.random.rand() < mutation_rate \ and max_connect_id - min_connect_id > 1: self.gene[n][i+1] = self.__mutate(self.gene[n][i+1], min_connect_id, max_connect_id) self.check_active() return False
Example 3
Project: MPContribs Author: materialsproject File: pre_submission.py License: MIT License | 6 votes |
def load_RSM(filename): om, tt, psd = xu.io.getxrdml_map(filename) om = np.deg2rad(om) tt = np.deg2rad(tt) wavelength = 1.54056 q_y = (1 / wavelength) * (np.cos(tt) - np.cos(2 * om - tt)) q_x = (1 / wavelength) * (np.sin(tt) - np.sin(2 * om - tt)) xi = np.linspace(np.min(q_x), np.max(q_x), 100) yi = np.linspace(np.min(q_y), np.max(q_y), 100) psd[psd < 1] = 1 data_grid = griddata( (q_x, q_y), psd, (xi[None, :], yi[:, None]), fill_value=1, method="cubic" ) nx, ny = data_grid.shape range_values = [np.min(q_x), np.max(q_x), np.min(q_y), np.max(q_y)] output_data = ( Panel(np.log(data_grid).reshape(nx, ny, 1), minor_axis=["RSM"]) .transpose(2, 0, 1) .to_frame() ) return range_values, output_data
Example 4
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset_tool.py License: MIT License | 6 votes |
def create_mnist(tfrecord_dir, mnist_dir): print('Loading MNIST from "%s"' % mnist_dir) import gzip with gzip.open(os.path.join(mnist_dir, 'train-images-idx3-ubyte.gz'), 'rb') as file: images = np.frombuffer(file.read(), np.uint8, offset=16) with gzip.open(os.path.join(mnist_dir, 'train-labels-idx1-ubyte.gz'), 'rb') as file: labels = np.frombuffer(file.read(), np.uint8, offset=8) images = images.reshape(-1, 1, 28, 28) images = np.pad(images, [(0,0), (0,0), (2,2), (2,2)], 'constant', constant_values=0) assert images.shape == (60000, 1, 32, 32) and images.dtype == np.uint8 assert labels.shape == (60000,) and labels.dtype == np.uint8 assert np.min(images) == 0 and np.max(images) == 255 assert np.min(labels) == 0 and np.max(labels) == 9 onehot = np.zeros((labels.size, np.max(labels) + 1), dtype=np.float32) onehot[np.arange(labels.size), labels] = 1.0 with TFRecordExporter(tfrecord_dir, images.shape[0]) as tfr: order = tfr.choose_shuffled_order() for idx in range(order.size): tfr.add_image(images[order[idx]]) tfr.add_labels(onehot[order]) #----------------------------------------------------------------------------
Example 5
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset_tool.py License: MIT License | 6 votes |
def create_mnistrgb(tfrecord_dir, mnist_dir, num_images=1000000, random_seed=123): print('Loading MNIST from "%s"' % mnist_dir) import gzip with gzip.open(os.path.join(mnist_dir, 'train-images-idx3-ubyte.gz'), 'rb') as file: images = np.frombuffer(file.read(), np.uint8, offset=16) images = images.reshape(-1, 28, 28) images = np.pad(images, [(0,0), (2,2), (2,2)], 'constant', constant_values=0) assert images.shape == (60000, 32, 32) and images.dtype == np.uint8 assert np.min(images) == 0 and np.max(images) == 255 with TFRecordExporter(tfrecord_dir, num_images) as tfr: rnd = np.random.RandomState(random_seed) for idx in range(num_images): tfr.add_image(images[rnd.randint(images.shape[0], size=3)]) #----------------------------------------------------------------------------
Example 6
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset_tool.py License: MIT License | 6 votes |
def create_cifar100(tfrecord_dir, cifar100_dir): print('Loading CIFAR-100 from "%s"' % cifar100_dir) import pickle with open(os.path.join(cifar100_dir, 'train'), 'rb') as file: data = pickle.load(file, encoding='latin1') images = data['data'].reshape(-1, 3, 32, 32) labels = np.array(data['fine_labels']) assert images.shape == (50000, 3, 32, 32) and images.dtype == np.uint8 assert labels.shape == (50000,) and labels.dtype == np.int32 assert np.min(images) == 0 and np.max(images) == 255 assert np.min(labels) == 0 and np.max(labels) == 99 onehot = np.zeros((labels.size, np.max(labels) + 1), dtype=np.float32) onehot[np.arange(labels.size), labels] = 1.0 with TFRecordExporter(tfrecord_dir, images.shape[0]) as tfr: order = tfr.choose_shuffled_order() for idx in range(order.size): tfr.add_image(images[order[idx]]) tfr.add_labels(onehot[order]) #----------------------------------------------------------------------------
Example 7
Project: dustmaps Author: gregreen File: test_bayestar.py License: GNU General Public License v2.0 | 6 votes |
def test_equ_random_sample_scalar(self): """ Test that random sample of reddening at arbitary distance is actually from the set of possible reddening samples at that distance. Uses vector of coordinates/distances as input. Uses single set of coordinates/distance as input. """ for d in self._test_data: # Prepare coordinates (with random distances) l = d['l']*units.deg b = d['b']*units.deg dm = 3. + (25.-3.)*np.random.random() dist = 10.**(dm/5.-2.) c = coords.SkyCoord(l, b, distance=dist*units.kpc, frame='galactic') ebv_data = self._interp_ebv(d, dist) ebv_calc = self._bayestar(c, mode='random_sample') d_ebv = np.min(np.abs(ebv_data[:] - ebv_calc)) np.testing.assert_allclose(d_ebv, 0., atol=0.001, rtol=0.0001)
Example 8
Project: dustmaps Author: gregreen File: test_bayestar.py License: GNU General Public License v2.0 | 6 votes |
def test_equ_random_sample_nodist_vector(self): """ Test that a random sample of the reddening vs. distance curve is drawn from the full set of samples. Uses vector of coordinates as input. """ # Prepare coordinates l = [d['l']*units.deg for d in self._test_data] b = [d['b']*units.deg for d in self._test_data] c = coords.SkyCoord(l, b, frame='galactic') ebv_data = np.array([d['samples'] for d in self._test_data]) ebv_calc = self._bayestar(c, mode='random_sample') # print 'vector random sample:' # print 'ebv_data.shape = {}'.format(ebv_data.shape) # print 'ebv_calc.shape = {}'.format(ebv_calc.shape) # print ebv_data[0] # print ebv_calc[0] d_ebv = np.min(np.abs(ebv_data[:,:,:] - ebv_calc[:,None,:]), axis=1) np.testing.assert_allclose(d_ebv, 0., atol=0.001, rtol=0.0001)
Example 9
Project: Deep_VoiceChanger Author: pstuvwx File: dataset.py License: MIT License | 6 votes |
def wave2input_image(wave, window, pos=0, pad=0): wave_image = np.hstack([wave[pos+i*sride:pos+(i+pad*2)*sride+dif].reshape(height+pad*2, sride) for i in range(256//sride)])[:,:254] wave_image *= window spectrum_image = np.fft.fft(wave_image, axis=1) input_image = np.abs(spectrum_image[:,:128].reshape(1, height+pad*2, 128), dtype=np.float32) np.clip(input_image, 1000, None, out=input_image) np.log(input_image, out=input_image) input_image += bias input_image /= scale if np.max(input_image) > 0.95: print('input image max bigger than 0.95', np.max(input_image)) if np.min(input_image) < 0.05: print('input image min smaller than 0.05', np.min(input_image)) return input_image
Example 10
Project: neuropythy Author: noahbenson File: labels.py License: GNU Affero General Public License v3.0 | 6 votes |
def cmap(self, data=None): ''' lblidx.cmap() yields a colormap for the given label index object that assumes that the data being plotted will be rescaled such that label 0 is 0 and the highest label value in the label index is equal to 1. lblidx.cmap(data) yields a colormap that will correctly color the labels given in data if data is scaled such that its minimum and maximum value are 0 and 1. ''' import matplotlib.colors from_list = matplotlib.colors.LinearSegmentedColormap.from_list if data is None: return self.colormap data = np.asarray(data).flatten() (vmin,vmax) = (np.min(data), np.max(data)) ii = np.argsort(self.ids) ids = np.asarray(self.ids)[ii] if vmin == vmax: (vmin,vmax,ii) = (vmin-0.5, vmax+0.5, vmin) clr = self.color_lookup(ii) return from_list('label1', [(0, clr), (1, clr)]) q = (ids >= vmin) & (ids <= vmax) ids = ids[q] clrs = self.color_lookup(ids) vals = (ids - vmin) / (vmax - vmin) return from_list('label%d' % len(vals), list(zip(vals, clrs)))
Example 11
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: image.py License: Apache License 2.0 | 6 votes |
def resize(im, short, max_size): """ only resize input image to target size and return scale :param im: BGR image input by opencv :param short: one dimensional size (the short side) :param max_size: one dimensional max size (the long side) :return: resized image (NDArray) and scale (float) """ im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(short) / float(im_size_min) # prevent bigger axis from being more than max_size: if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) return im, im_scale
Example 12
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: quantization.py License: Apache License 2.0 | 6 votes |
def collect(self, name, arr): """Callback function for collecting min and max values from an NDArray.""" name = py_str(name) if self.include_layer is not None and not self.include_layer(name): return handle = ctypes.cast(arr, NDArrayHandle) arr = NDArray(handle, writable=False) min_range = ndarray.min(arr).asscalar() max_range = ndarray.max(arr).asscalar() if name in self.min_max_dict: cur_min_max = self.min_max_dict[name] self.min_max_dict[name] = (min(cur_min_max[0], min_range), max(cur_min_max[1], max_range)) else: self.min_max_dict[name] = (min_range, max_range) if self.logger is not None: self.logger.info("Collecting layer %s output min_range=%f, max_range=%f" % (name, min_range, max_range))
Example 13
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_quantization.py License: Apache License 2.0 | 6 votes |
def test_quantize_float32_to_int8(): shape = rand_shape_nd(4) data = rand_ndarray(shape, 'default', dtype='float32') min_range = mx.nd.min(data) max_range = mx.nd.max(data) qdata, min_val, max_val = mx.nd.contrib.quantize(data, min_range, max_range, out_type='int8') data_np = data.asnumpy() min_range = min_range.asscalar() max_range = max_range.asscalar() real_range = np.maximum(np.abs(min_range), np.abs(max_range)) quantized_range = 127.0 scale = quantized_range / real_range assert qdata.dtype == np.int8 assert min_val.dtype == np.float32 assert max_val.dtype == np.float32 assert same(min_val.asscalar(), -real_range) assert same(max_val.asscalar(), real_range) qdata_np = (np.sign(data_np) * np.minimum(np.abs(data_np) * scale + 0.5, quantized_range)).astype(np.int8) assert_almost_equal(qdata.asnumpy(), qdata_np, atol = 1)
Example 14
Project: DOTA_models Author: ringringyi File: nav_env.py License: Apache License 2.0 | 6 votes |
def get_optimal_action(self, current_node_ids, step_number): """Returns the optimal action from the current node.""" goal_number = step_number / self.task_params.num_steps gtG = self.task.gtG a = np.zeros((len(current_node_ids), self.task_params.num_actions), dtype=np.int32) d_dict = self.episode.dist_to_goal[goal_number] for i, c in enumerate(current_node_ids): neigh = gtG.vertex(c).out_neighbours() neigh_edge = gtG.vertex(c).out_edges() ds = np.array([d_dict[i][int(x)] for x in neigh]) ds_min = np.min(ds) for i_, e in enumerate(neigh_edge): if ds[i_] == ds_min: _ = gtG.ep['action'][e] a[i, _] = 1 return a
Example 15
Project: cat-bbs Author: aleju File: plotting.py License: MIT License | 5 votes |
def _line_to_xy(self, line_x, line_y, limit_y_min=None, limit_y_max=None): point_every = max(1, int(len(line_x) / self.nb_points_max)) points_x = [] points_y = [] curr_sum = 0 counter = 0 last_idx = len(line_x) - 1 for i in range(len(line_x)): batch_idx = line_x[i] if batch_idx > self.start_batch_idx: curr_sum += line_y[i] counter += 1 if counter >= point_every or i == last_idx: points_x.append(batch_idx) y = curr_sum / counter if limit_y_min is not None and limit_y_max is not None: y = np.clip(y, limit_y_min, limit_y_max) elif limit_y_min is not None: y = max(y, limit_y_min) elif limit_y_max is not None: y = min(y, limit_y_max) points_y.append(y) counter = 0 curr_sum = 0 return points_x, points_y
Example 16
Project: cat-bbs Author: aleju File: predict_video.py License: MIT License | 5 votes |
def _heatmap_to_rects(self, grid_pred, bb_img): """Convert a heatmap to rectangles / bounding box candidates.""" grid_pred = np.squeeze(grid_pred) # (1, H, W) => (H, W) # remove low activations grid_thresh = grid_pred >= self.heatmap_activation_threshold # find connected components grid_labeled, num_labels = morphology.label( grid_thresh, background=0, connectivity=1, return_num=True ) # for each connected components, # - draw a bounding box around it, # - shrink the bounding box to optimal size # - estimate a score/confidence value bbs = [] for label in range(1, num_labels+1): (yy, xx) = np.nonzero(grid_labeled == label) min_y, max_y = np.min(yy), np.max(yy) min_x, max_x = np.min(xx), np.max(xx) rect = RectangleOnImage(x1=min_x, x2=max_x+1, y1=min_y, y2=max_y+1, shape=grid_labeled) activation = self._rect_to_score(rect, grid_pred) rect_shrunk, activation_shrunk = self._shrink(grid_pred, rect) rect_rs_shrunk = rect_shrunk.on(bb_img) bbs.append((rect_rs_shrunk, activation_shrunk)) return bbs
Example 17
Project: aospy Author: spencerahill File: var.py License: Apache License 2.0 | 5 votes |
def mask_unphysical(self, data): """Mask data array where values are outside physically valid range.""" if not self.valid_range: return data else: return np.ma.masked_outside(data, np.min(self.valid_range), np.max(self.valid_range))
Example 18
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: blob.py License: MIT License | 5 votes |
def prep_im_for_blob(im, pixel_means, target_size, max_size): """Mean subtract and scale an image for use in a blob.""" im = im.astype(np.float32, copy=False) im -= pixel_means im_shape = im.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > max_size: im_scale = float(max_size) / float(im_size_max) im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) return im, im_scale
Example 19
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: test.py License: MIT License | 5 votes |
def _get_image_blob(im): """Converts an image into a network input. Arguments: im (ndarray): a color image in BGR order Returns: blob (ndarray): a data blob holding an image pyramid im_scale_factors (list): list of image scales (relative to im) used in the image pyramid """ im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS im_shape = im_orig.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) processed_ims = [] im_scale_factors = [] for target_size in cfg.TEST.SCALES: im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE: im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max) im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scale_factors.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, np.array(im_scale_factors)
Example 20
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection Author: Sunarker File: test_train.py License: MIT License | 5 votes |
def _get_image_blob(im): """Converts an image into a network input. Arguments: im (ndarray): a color image in BGR order Returns: blob (ndarray): a data blob holding an image pyramid im_scale_factors (list): list of image scales (relative to im) used in the image pyramid """ im_orig = im.astype(np.float32, copy=True) im_orig -= cfg.PIXEL_MEANS im_shape = im_orig.shape im_size_min = np.min(im_shape[0:2]) im_size_max = np.max(im_shape[0:2]) processed_ims = [] im_scale_factors = [] for target_size in cfg.TEST.SCALES: im_scale = float(target_size) / float(im_size_min) # Prevent the biggest axis from being more than MAX_SIZE if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE: im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max) im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im_scale_factors.append(im_scale) processed_ims.append(im) # Create a blob to hold the input images blob = im_list_to_blob(processed_ims) return blob, np.array(im_scale_factors)
Example 21
Project: cgp-cnn Author: sg-nm File: cgp.py License: MIT License | 5 votes |
def init_gene(self): # intermediate node for n in range(self.net_info.node_num + self.net_info.out_num): # type gene type_num = self.net_info.func_type_num if n < self.net_info.node_num else self.net_info.out_type_num self.gene[n][0] = np.random.randint(type_num) # connection gene col = np.min((int(n / self.net_info.rows), self.net_info.cols)) max_connect_id = col * self.net_info.rows + self.net_info.input_num min_connect_id = (col - self.net_info.level_back) * self.net_info.rows + self.net_info.input_num \ if col - self.net_info.level_back >= 0 else 0 for i in range(self.net_info.max_in_num): self.gene[n][i + 1] = min_connect_id + np.random.randint(max_connect_id - min_connect_id) self.check_active()
Example 22
Project: cgp-cnn Author: sg-nm File: cgp_config.py License: MIT License | 5 votes |
def __call__(self, net_lists): evaluations = np.zeros(len(net_lists)) for i in np.arange(0, len(net_lists), self.gpu_num): process_num = np.min((i + self.gpu_num, len(net_lists))) - i pool = mp.Pool(process_num) arg_data = [(cnn_eval, net_lists[i+j], j, self.epoch_num, self.batchsize, self.dataset, self.valid_data_ratio, self.verbose) for j in range(process_num)] evaluations[i:i+process_num] = pool.map(arg_wrapper_mp, arg_data) pool.terminate() return evaluations
Example 23
Project: FRIDA Author: LCAV File: doa.py License: MIT License | 5 votes |
def polar_distance(x1, x2): """ Given two arrays of numbers x1 and x2, pairs the cells that are the closest and provides the pairing matrix index: x1(index(1,:)) should be as close as possible to x2(index(2,:)). The function outputs the average of the absolute value of the differences abs(x1(index(1,:))-x2(index(2,:))). :param x1: vector 1 :param x2: vector 2 :return: d: minimum distance between d index: the permutation matrix """ x1 = np.reshape(x1, (1, -1), order='F') x2 = np.reshape(x2, (1, -1), order='F') N1 = x1.size N2 = x2.size diffmat = np.arccos(np.cos(x1 - np.reshape(x2, (-1, 1), order='F'))) min_N1_N2 = np.min([N1, N2]) index = np.zeros((min_N1_N2, 2), dtype=int) if min_N1_N2 > 1: for k in range(min_N1_N2): d2 = np.min(diffmat, axis=0) index2 = np.argmin(diffmat, axis=0) index1 = np.argmin(d2) index2 = index2[index1] index[k, :] = [index1, index2] diffmat[index2, :] = float('inf') diffmat[:, index1] = float('inf') d = np.mean(np.arccos(np.cos(x1[:, index[:, 0]] - x2[:, index[:, 1]]))) else: d = np.min(diffmat) index = np.argmin(diffmat) if N1 == 1: index = np.array([1, index]) else: index = np.array([index, 1]) return d, index
Example 24
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset_tool.py License: MIT License | 5 votes |
def create_cifar10(tfrecord_dir, cifar10_dir): print('Loading CIFAR-10 from "%s"' % cifar10_dir) import pickle images = [] labels = [] for batch in range(1, 6): with open(os.path.join(cifar10_dir, 'data_batch_%d' % batch), 'rb') as file: data = pickle.load(file, encoding='latin1') images.append(data['data'].reshape(-1, 3, 32, 32)) labels.append(data['labels']) images = np.concatenate(images) labels = np.concatenate(labels) assert images.shape == (50000, 3, 32, 32) and images.dtype == np.uint8 assert labels.shape == (50000,) and labels.dtype == np.int32 assert np.min(images) == 0 and np.max(images) == 255 assert np.min(labels) == 0 and np.max(labels) == 9 onehot = np.zeros((labels.size, np.max(labels) + 1), dtype=np.float32) onehot[np.arange(labels.size), labels] = 1.0 with TFRecordExporter(tfrecord_dir, images.shape[0]) as tfr: order = tfr.choose_shuffled_order() for idx in range(order.size): tfr.add_image(images[order[idx]]) tfr.add_labels(onehot[order]) #----------------------------------------------------------------------------
Example 25
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset_tool.py License: MIT License | 5 votes |
def create_svhn(tfrecord_dir, svhn_dir): print('Loading SVHN from "%s"' % svhn_dir) import pickle images = [] labels = [] for batch in range(1, 4): with open(os.path.join(svhn_dir, 'train_%d.pkl' % batch), 'rb') as file: data = pickle.load(file, encoding='latin1') images.append(data[0]) labels.append(data[1]) images = np.concatenate(images) labels = np.concatenate(labels) assert images.shape == (73257, 3, 32, 32) and images.dtype == np.uint8 assert labels.shape == (73257,) and labels.dtype == np.uint8 assert np.min(images) == 0 and np.max(images) == 255 assert np.min(labels) == 0 and np.max(labels) == 9 onehot = np.zeros((labels.size, np.max(labels) + 1), dtype=np.float32) onehot[np.arange(labels.size), labels] = 1.0 with TFRecordExporter(tfrecord_dir, images.shape[0]) as tfr: order = tfr.choose_shuffled_order() for idx in range(order.size): tfr.add_image(images[order[idx]]) tfr.add_labels(onehot[order]) #----------------------------------------------------------------------------
Example 26
Project: dustmaps Author: gregreen File: test_bayestar.py License: GNU General Public License v2.0 | 5 votes |
def test_equ_random_sample_vector(self): """ Test that random sample of reddening at arbitary distance is actually from the set of possible reddening samples at that distance. Uses vector of coordinates/distances as input. """ # Prepare coordinates (with random distances) l = [d['l']*units.deg for d in self._test_data] b = [d['b']*units.deg for d in self._test_data] dm = 3. + (25.-3.)*np.random.random(len(self._test_data)) dist = [d*units.kpc for d in 10.**(dm/5.-2.)] dist_unitless = [d for d in 10.**(dm/5.-2.)] c = coords.SkyCoord(l, b, distance=dist, frame='galactic') ebv_data = np.array([ self._interp_ebv(datum, d) for datum,d in zip(self._test_data, dist_unitless) ]) ebv_calc = self._bayestar(c, mode='random_sample') d_ebv = np.min(np.abs(ebv_data[:,:] - ebv_calc[:,None]), axis=1) # print 'vector arbitrary distance random sample:' # print r'% residual:' # for ec,de in zip(ebv_calc, d_ebv): # print ' {: >8.3f} {: >8.5f}'.format(ec, de) np.testing.assert_allclose(d_ebv, 0., atol=0.001, rtol=0.0001)
Example 27
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_generate_np_clip_works_as_expected(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) x_adv = self.attack.generate_np(x_val, eps=0.5, ord=np.inf, clip_min=-0.2, clip_max=0.1) self.assertClose(np.min(x_adv), -0.2) self.assertClose(np.max(x_adv), 0.1)
Example 28
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_generate_np_gives_clipped_adversarial_examples(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) x_adv = self.attack.generate_np(x_val, max_iterations=10, binary_search_steps=1, learning_rate=1e-3, initial_const=1, clip_min=-0.2, clip_max=0.3, batch_size=100) self.assertTrue(-0.201 < np.min(x_adv)) self.assertTrue(np.max(x_adv) < .301)
Example 29
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_generate_np_high_confidence_targeted_examples(self): trivial_model = TrivialModel() for CONFIDENCE in [0, 2.3]: x_val = np.random.rand(10, 1) - .5 x_val = np.array(x_val, dtype=np.float32) feed_labs = np.zeros((10, 2)) feed_labs[np.arange(10), np.random.randint(0, 2, 10)] = 1 attack = CarliniWagnerL2(trivial_model, sess=self.sess) x_adv = attack.generate_np(x_val, max_iterations=100, binary_search_steps=2, learning_rate=1e-2, initial_const=1, clip_min=-10, clip_max=10, confidence=CONFIDENCE, y_target=feed_labs, batch_size=10) new_labs = self.sess.run(trivial_model.get_logits(x_adv)) good_labs = new_labs[np.arange(10), np.argmax(feed_labs, axis=1)] bad_labs = new_labs[np.arange( 10), 1 - np.argmax(feed_labs, axis=1)] self.assertClose(CONFIDENCE, np.min(good_labs - bad_labs), atol=1e-1) self.assertTrue(np.mean(np.argmax(new_labs, axis=1) == np.argmax(feed_labs, axis=1)) > .9)
Example 30
Project: neural-fingerprinting Author: StephanZheng File: test_attacks.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_generate_np_high_confidence_untargeted_examples(self): trivial_model = TrivialModel() for CONFIDENCE in [0, 2.3]: x_val = np.random.rand(10, 1) - .5 x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(trivial_model.get_logits(x_val)), axis=1) attack = CarliniWagnerL2(trivial_model, sess=self.sess) x_adv = attack.generate_np(x_val, max_iterations=100, binary_search_steps=2, learning_rate=1e-2, initial_const=1, clip_min=-10, clip_max=10, confidence=CONFIDENCE, batch_size=10) new_labs = self.sess.run(trivial_model.get_logits(x_adv)) good_labs = new_labs[np.arange(10), 1 - orig_labs] bad_labs = new_labs[np.arange(10), orig_labs] self.assertTrue(np.mean(np.argmax(new_labs, axis=1) == orig_labs) == 0) self.assertTrue(np.isclose( 0, np.min(good_labs - (bad_labs + CONFIDENCE)), atol=1e-1))