Python numpy.empty() Examples

The following are code examples for showing how to use numpy.empty(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: ieml   Author: IEMLdev   File: _test_tables.py    GNU General Public License v3.0 6 votes vote down vote up
def test_additive_script_layer_0(self):
        script = sc("I:")
        tables = script.tables
        row_headers = [self.parser.parse("I:"), ]
        tab_headers = [self.parser.parse("I:"),]

        cells = np.empty(6, dtype=object)

        cells[0] = self.parser.parse("E:")
        cells[1] = self.parser.parse("U:")
        cells[2] = self.parser.parse("A:")
        cells[3] = self.parser.parse("S:")
        cells[4] = self.parser.parse("B:")
        cells[5] = self.parser.parse("T:")


        row_col_h = list(tables[0].headers.values())[0]

        self.assertEqual(len(tables), 1, "Correct number of tables generated")
        self.assertTrue(tables[0].cells.shape == cells.shape, "Table has the correct shape")
        self.assertEqual(row_col_h[0], row_headers, "Row headers are generated correctly")
        self.assertEqual(list(tables[0].headers), tab_headers, "Tab headers are generated correctly")
        self.assertTrue((tables[0].cells == cells).all(), "Cells are generated correctly")
        self.assertTrue(tables[0].paradigm == script, "Table has correct paradigm") 
Example 2
Project: ieml   Author: IEMLdev   File: _test_tables.py    GNU General Public License v3.0 6 votes vote down vote up
def test_1d_multiplicative_script(self):
        script = self.parser.parse("E:S:O:.")
        tables = script.tables
        row_headers = [self.parser.parse("E:S:O:.")]
        col_headers = []
        tab_headers = [self.parser.parse("E:S:O:.")]

        cells = np.empty(2, dtype=object)

        cells[0] = self.parser.parse("E:S:U:.")
        cells[1] = self.parser.parse("E:S:A:.")
        row_col_h = list(tables[0].headers.values())[0]

        self.assertEqual(len(tables), 1, "Correct number of tables generated")
        self.assertTrue(tables[0].cells.shape == cells.shape, "Table has the correct shape")
        self.assertEqual(row_col_h[0], row_headers, "Row headers are generated correctly")
        self.assertTrue((row_col_h[1] == col_headers), "Column headers are generated correctly")
        self.assertEqual(list(tables[0].headers), tab_headers, "Tab headers are generated correctly")
        self.assertTrue((tables[0].cells == cells).all(), "Cells are generated correctly")
        self.assertTrue(tables[0].paradigm == script, "Table has correct paradigm") 
Example 3
Project: ieml   Author: IEMLdev   File: _test_tables.py    GNU General Public License v3.0 6 votes vote down vote up
def test_row_of_paradigm(self):
        script = self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'O:O:.-',")
        tables = script.tables
        cells = np.empty((2, 2), dtype=object)

        row_headers = [self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'U:O:.-',"),
                       self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'A:O:.-',")]
        col_headers = [self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'O:U:.-',"),
                       self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'O:A:.-',")]
        tab_headers = [script]

        cells[0][0] = self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'wo.-',")
        cells[0][1] = self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'wa.-',")
        cells[1][0] = self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'wu.-',")
        cells[1][1] = self.parser.parse("t.i.-s.i.-'u.S:.-U:.-'we.-',")

        row_col_h = list(tables[0].headers.values())[0]

        self.assertEqual(len(tables), 1, "Correct number of tables generated")
        self.assertTrue(tables[0].cells.shape == cells.shape, "Table has the correct shape")
        self.assertEqual(row_col_h[0], row_headers, "Row headers are generated correctly")
        self.assertTrue((row_col_h[1] == col_headers), "Column headers are generated correctly")
        self.assertEqual(list(tables[0].headers), tab_headers, "Tab headers are generated correctly")
        self.assertTrue((tables[0].cells == cells).all(), "Cells are generated correctly")
        self.assertTrue(tables[0].paradigm == script, "Table has correct paradigm") 
Example 4
Project: fbpconv_tf   Author: panakino   File: unet.py    GNU General Public License v3.0 6 votes vote down vote up
def predict(self, model_path, x_test):
        """
        Uses the model to create a prediction for the given data

        :param model_path: path to the model checkpoint to restore
        :param x_test: Data to predict on. Shape [n, nx, ny, channels]
        :returns prediction: The unet prediction Shape [n, px, py, labels] (px=nx-self.offset/2)
        """

        init = tf.global_variables_initializer()
        with tf.Session() as sess:
            # Initialize variables
            sess.run(init)

            # Restore model weights from previously saved model
            self.restore(sess, model_path)

            y_dummy = np.empty((x_test.shape[0], x_test.shape[1], x_test.shape[2], self.n_class))
            prediction = sess.run(self.predicter, feed_dict={self.x: x_test, self.y: y_dummy, self.keep_prob: 1.})

        return prediction 
Example 5
Project: models   Author: kipoi   File: dataloader_m.py    MIT License 6 votes vote down vote up
def map_values(values, pos, target_pos, dtype=None, nan=dat.CPG_NAN):
    """Maps `values` array at positions `pos` to `target_pos`.

    Inserts `nan` for uncovered positions.
    """
    assert len(values) == len(pos)
    assert np.all(pos == np.sort(pos))
    assert np.all(target_pos == np.sort(target_pos))

    values = values.ravel()
    pos = pos.ravel()
    target_pos = target_pos.ravel()
    idx = np.in1d(pos, target_pos)
    pos = pos[idx]
    values = values[idx]
    if not dtype:
        dtype = values.dtype
    target_values = np.empty(len(target_pos), dtype=dtype)
    target_values.fill(nan)
    idx = np.in1d(target_pos, pos).nonzero()[0]
    assert len(idx) == len(values)
    assert np.all(target_pos[idx] == pos)
    target_values[idx] = values
    return target_values 
Example 6
Project: skylab   Author: coenders   File: data.py    GNU General Public License v3.0 6 votes vote down vote up
def exp(N=100):
    r"""Create uniformly distributed data on sphere. """
    g = 3.7

    arr = np.empty((N, ), dtype=[("ra", np.float), ("sinDec", np.float),
                                 ("sigma", np.float), ("logE", np.float)])

    arr["ra"] = np.random.uniform(0., 2.*np.pi, N)
    arr["sinDec"] = np.random.uniform(-1., 1., N)

    E = np.log10(np.random.pareto(g, size=N) + 1)
    arr["sigma"] = np.random.lognormal(mean=np.log((mrs - mrs_min) * np.exp(-np.log(10)*E) + mrs_min),
                                       sigma=log_sig)
    arr["logE"] = E + logE_res * np.random.normal(size=N)

    return arr 
Example 7
Project: core   Author: lifemapper   File: mcpa.py    GNU General Public License v3.0 6 votes vote down vote up
def _beta_helper(mtx1, mtx2, weights):
    """This helper function avoids creating large temporary matrices

    Args:
        mtx1 (numpy array): A (n [sites] by i [predictors]) standardized matrix.
        mtx2 (numpy array): A (
        weights (numpy array): A (n [sites]) array of site weights
    """
    _, num_predictors = mtx1.shape
    _, num_k = mtx2.shape
    out_mtx = np.empty((num_predictors, num_k))
    for i in range(num_predictors):
        for j in range(i, num_k):
            v = np.sum(mtx1[:, i] * weights * mtx2[:, j])
            out_mtx[i, j] = v
    return out_mtx

# ............................................................................. 
Example 8
Project: core   Author: lifemapper   File: geotools.py    GNU General Public License v3.0 6 votes vote down vote up
def _cycleRow(self, scanline, arrtype, left, center, right):
      """
      @summary: Shift the values in a row to the right, so that the first 
                column in the row is shifted to the center.  Used for data in
                which a row begins with 0 degree longitude and ends with 360 
                degrees longitude (instead of -180 to +180) 
      @param scanline: Original row to shift.
      @param arrtype: Numpy datatype for scanline values
      @param left: Leftmost column index
      @param center: Center column index
      @param right: Rightmost column index
      """
      newline = numpy.empty((self.xsize), dtype=arrtype)
      c = 0
      for col in range(center, right):
         newline[c] = scanline[col]
         c += 1
      for col in range(left, center):
         newline[c] = scanline[col]
         c += 1
      return newline
   
   # ............................................................................ 
Example 9
Project: fuku-ml   Author: fukuball   File: Utility.py    MIT License 6 votes vote down vote up
def random_projection(X):

        data_demension = X.shape[1]

        new_data_demension = random.randint(2, data_demension)

        new_X = np.empty((data_demension, new_data_demension))

        minus_one = 0.1
        positive_one = 0.9

        for i in range(len(new_X)):
            for j in range(len(new_X[i])):
                rand = random.random()
                if rand < minus_one:
                    new_X[i][j] = -1.0
                elif rand >= positive_one:
                    new_X[i][j] = 1.0
                else:
                    new_X[i][j] = 0.0

        new_X = np.inner(X, new_X.T)

        return new_X 
Example 10
Project: Scene-Understanding   Author: foamliu   File: data_generator.py    MIT License 6 votes vote down vote up
def __getitem__(self, idx):
        i = idx * batch_size

        length = min(batch_size, (len(self.ids) - i))
        X = np.empty((length, img_rows, img_cols, 3), dtype=np.float32)
        Y = np.empty((length, img_rows, img_cols, num_classes), dtype=np.float32)

        for i_batch in range(length):
            id = self.ids[i + i_batch]
            name = self.names[id]
            image = get_image(name)
            category = get_category(id)
            image, category = random_crop(image, category)

            image = cv.cvtColor(image, cv.COLOR_BGR2RGB)

            X[i_batch] = image
            Y[i_batch] = to_categorical(category, num_classes)

        X = preprocess_input(X)

        return X, Y 
Example 11
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: utils.py    Apache License 2.0 6 votes vote down vote up
def sample_categorical(prob, rng):
    """Sample from independent categorical distributions

    Each batch is an independent categorical distribution.

    Parameters
    ----------
    prob : numpy.ndarray
      Probability of the categorical distribution. Shape --> (batch_num, category_num)
    rng : numpy.random.RandomState

    Returns
    -------
    ret : numpy.ndarray
      Sampling result. Shape --> (batch_num,)
    """
    ret = numpy.empty(prob.shape[0], dtype=numpy.float32)
    for ind in range(prob.shape[0]):
        ret[ind] = numpy.searchsorted(numpy.cumsum(prob[ind]), rng.rand()).clip(min=0.0,
                                                                                max=prob.shape[
                                                                                        1] - 0.5)
    return ret 
Example 12
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    Apache License 2.0 6 votes vote down vote up
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t):
    """Return a new handle with specified shape and context.

    Empty handle is only used to hold results.

    Returns
    -------
    handle
        A new empty `NDArray` handle.
    """
    hdl = NDArrayHandle()
    check_call(_LIB.MXNDArrayCreateEx(
        c_array_buf(mx_uint, native_array('I', shape)),
        mx_uint(len(shape)),
        ctypes.c_int(ctx.device_typeid),
        ctypes.c_int(ctx.device_id),
        ctypes.c_int(int(delay_alloc)),
        ctypes.c_int(int(_DTYPE_NP_TO_MX[np.dtype(dtype).type])),
        ctypes.byref(hdl)))
    return hdl 
Example 13
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    Apache License 2.0 6 votes vote down vote up
def asnumpy(self):
        """Returns a ``numpy.ndarray`` object with value copied from this array.

        Examples
        --------
        >>> x = mx.nd.ones((2,3))
        >>> y = x.asnumpy()
        >>> type(y)
        <type 'numpy.ndarray'>
        >>> y
        array([[ 1.,  1.,  1.],
               [ 1.,  1.,  1.]], dtype=float32)
        >>> z = mx.nd.ones((2,3), dtype='int32')
        >>> z.asnumpy()
        array([[1, 1, 1],
               [1, 1, 1]], dtype=int32)
        """
        data = np.empty(self.shape, dtype=self.dtype)
        check_call(_LIB.MXNDArraySyncCopyToCPU(
            self.handle,
            data.ctypes.data_as(ctypes.c_void_p),
            ctypes.c_size_t(data.size)))
        return data 
Example 14
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_operator.py    Apache License 2.0 6 votes vote down vote up
def check_softmax_with_shape(shape, xpu, preserve_shape=False):
    # bind with label
    X = mx.symbol.Variable('X')
    L = mx.symbol.Variable('L')
    Y = mx.symbol.SoftmaxOutput(data=X, label=L, preserve_shape=preserve_shape)
    x = mx.random.uniform(-1, 1, shape, ctx=xpu)
    l = mx.random.uniform(-1, 1, shape, ctx=xpu)
    l[:] = np_softmax(l.asnumpy())
    grad = mx.nd.empty(shape, ctx = xpu)
    exec1 = Y.bind(xpu, args = [x, l], args_grad = {'X': grad})
    exec1.forward(is_train=True)
    out = exec1.outputs[0].asnumpy()
    # Non-zero atol required by test_softmax with seed 781663739
    rtol = 1e-4
    atol = 1e-6
    assert_almost_equal(out, np_softmax(x.asnumpy()), rtol=rtol, atol=atol)
    exec1.backward()
    assert_almost_equal(grad.asnumpy(), np_softmax(x.asnumpy()) - l.asnumpy(), rtol=rtol, atol=atol) 
Example 15
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_operator.py    Apache License 2.0 6 votes vote down vote up
def test_scalarop():
    data = mx.symbol.Variable('data')
    shape = (3, 4)
    data_tmp = np.ones(shape)*5
    arr_data = mx.nd.array(data_tmp)
    arr_grad = mx.nd.empty(shape)
    arr_grad[:]=3

    test = 2 / (4-((1+data+1)*2/5)-0.8-(data!=0))

    npout_1 = (4-((1+data_tmp+1)*2/5)-0.8-(data_tmp!=0))
    npout = 2/npout_1

    check_symbolic_forward(test, [data_tmp], [npout])

    npout_grad = 2.*2/5
    npout_grad = 2*npout_grad /(npout_1 *npout_1 )

    check_symbolic_backward(test, [data_tmp], [np.ones(shape)*2], [npout_grad]) 
Example 16
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_operator.py    Apache License 2.0 6 votes vote down vote up
def test_shape_array():
    for i in range(1,6):
        shape = rand_shape_nd(i)
        x = mx.sym.var('x')
        y = mx.sym.shape_array(x)
        xa = mx.nd.array(np.random.ranf(shape))
        xg = mx.nd.empty(xa.shape)
        ya = np.shape(xa)
        yg = mx.nd.ones(ya)
        exe = y.bind(ctx=default_context(), args={'x': xa},
                     args_grad={'x': xg})
        exe.forward(is_train=True)
        exe.backward([yg])
        yo = exe.outputs[0].asnumpy()
        same(yo, ya)
        assert_almost_equal(xg.asnumpy(), np.zeros_like(xg.asnumpy())) 
Example 17
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_operator.py    Apache License 2.0 6 votes vote down vote up
def test_binary_op_duplicate_input():
    data = mx.symbol.Variable('data')
    shape = (3, 4)
    data_tmp = np.ones(shape)
    data_tmp[:] = 5
    arr_data = mx.nd.array(data_tmp)
    arr_grad = mx.nd.empty(shape)
    arr_grad[:] = 3
    out_grad = mx.nd.empty(shape)
    out_grad[:] = 1
    square = data * data
    exe_square = square.bind(default_context(), args=[arr_data], args_grad=[arr_grad])
    exe_square.forward(is_train=True)
    assert_almost_equal(exe_square.outputs[0].asnumpy(), data_tmp * data_tmp)
    exe_square.backward(out_grad)
    assert_almost_equal(arr_grad.asnumpy(), 2.0 * data_tmp) 
Example 18
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_operator.py    Apache License 2.0 6 votes vote down vote up
def test_sign():
    data = mx.symbol.Variable('data')
    shape = (3, 4)
    data_tmp = np.ones(shape)
    data_tmp[:]=5
    arr_data = mx.nd.array(data_tmp)
    arr_grad = mx.nd.empty(shape)
    arr_grad[:]=3

    test = mx.sym.sign(data)
    exe_test = test.bind(default_context(), args=[arr_data], args_grad=[arr_grad])
    exe_test.forward(is_train=True)
    out = exe_test.outputs[0].asnumpy()
    npout = np.sign(data_tmp)
    assert_almost_equal(out, npout)

    out_grad = mx.nd.empty(shape)
    out_grad[:] = 2;
    npout_grad = out_grad.asnumpy()
    npout_grad = 0;
    exe_test.backward(out_grad)
    assert_almost_equal(arr_grad.asnumpy(), npout_grad) 
Example 19
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_operator.py    Apache License 2.0 6 votes vote down vote up
def test_rsqrt_cos_sin():
    data = mx.symbol.Variable('data')
    shape = (3, 4)
    data_tmp = np.ones(shape)
    data_tmp[:]=5
    arr_data = mx.nd.array(data_tmp)
    arr_grad = mx.nd.empty(shape)
    arr_grad[:]=3

    test =  mx.sym.rsqrt(data) + mx.sym.cos(data) + mx.sym.sin(data)
    exe_test = test.bind(default_context(), args=[arr_data], args_grad=[arr_grad])
    exe_test.forward(is_train=True)
    out = exe_test.outputs[0].asnumpy()
    npout =  1/ np.sqrt(data_tmp) + np.cos(data_tmp) + np.sin(data_tmp)
    assert_almost_equal(out, npout)

    out_grad = mx.nd.empty(shape)
    out_grad[:] = 2;
    npout_grad = out_grad.asnumpy()
    npout_grad = npout_grad * -(1.0 / (2.0 * data_tmp * np.sqrt(data_tmp))) + npout_grad * -1 * np.sin(data_tmp) + npout_grad * np.cos(data_tmp)
    exe_test.backward(out_grad)
    assert_almost_equal(arr_grad.asnumpy(), npout_grad) 
Example 20
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_operator.py    Apache License 2.0 6 votes vote down vote up
def test_abs():
    data = mx.symbol.Variable('data')
    shape = (3, 4)
    data_tmp = np.ones(shape)
    data_tmp[:]=5
    arr_data = mx.nd.array(data_tmp)
    arr_grad = mx.nd.empty(shape)
    arr_grad[:]=3

    test = mx.sym.abs(data)
    exe_test = test.bind(default_context(), args=[arr_data], args_grad=[arr_grad])
    exe_test.forward(is_train=True)
    out = exe_test.outputs[0].asnumpy()
    npout = abs(data_tmp)
    assert_almost_equal(out, npout)

    out_grad = mx.nd.empty(shape)
    out_grad[:] = 2;
    npout_grad = out_grad.asnumpy()
    npout_grad = npout_grad * np.sign(data_tmp)
    exe_test.backward(out_grad)
    assert_almost_equal(arr_grad.asnumpy(), npout_grad) 
Example 21
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_operator.py    Apache License 2.0 6 votes vote down vote up
def check_pad_with_shape(shape, xpu, pad_width, mode, dtype="float64"):
    # bind with label
    X = mx.symbol.Variable('X', dtype=dtype)
    Y = mx.symbol.Pad(data=X, mode=mode, pad_width=pad_width)
    x = mx.random.uniform(-1, 1, shape, ctx=mx.cpu(), dtype=dtype).copyto(xpu)
    # numpy result
    pad_grouped = list(zip(*[iter(list(pad_width))] * 2))
    np_out = np.pad(x.asnumpy(), pad_grouped, mode)
    # mxnet result
    grad = mx.nd.empty(shape, ctx = xpu, dtype=dtype)
    exec1 = Y.bind(xpu, args = [x], args_grad = {'X': grad})
    exec1.forward(is_train=True)
    out = exec1.outputs[0].asnumpy()
    # compare numpy + mxnet
    assert_almost_equal(out, np_out)
    # grad check
    check_numeric_gradient(Y, [x.asnumpy()], numeric_eps=1e-2, rtol=1e-2) 
Example 22
Project: speed_estimation   Author: NeilNie   File: helper.py    MIT License 5 votes vote down vote up
def udacity_val_gen(data, batch_size):

    """
    Generate training image give image paths and associated steering angles
    """

    images = np.empty([batch_size, configs.LENGTH, configs.IMG_HEIGHT, configs.IMG_WIDTH, 3], dtype=np.int32)
    labels = np.empty([batch_size])

    while True:

        c = 0

        for index in np.random.permutation(data.shape[0]):

            imgs = np.empty([configs.LENGTH, configs.IMG_HEIGHT, configs.IMG_WIDTH, 3], dtype=np.int32)

            if index < configs.LENGTH:
                start = 0
                end = configs.LENGTH
            elif index + configs.LENGTH >= len(data):
                start = len(data) - configs.LENGTH - 1
                end = len(data) - 1
            else:
                start = index
                end = index + configs.LENGTH

            for i in range(start, end):
                center_path = '/home/neil/dataset/steering/test/center/' + str(data['frame_id'].loc[i]) + ".jpg"
                image = load_image(center_path)
                imgs[i - start] = image

            images[c] = imgs
            labels[c] = data['steering_angle'].loc[end]

            c += 1

            if c == batch_size:
                break

        yield images, labels 
Example 23
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection   Author: Sunarker   File: anchor_target_layer.py    MIT License 5 votes vote down vote up
def _unmap(data, count, inds, fill=0):
  """ Unmap a subset of item (data) back to the original set of items (of
  size count) """
  if len(data.shape) == 1:
    ret = np.empty((count,), dtype=np.float32)
    ret.fill(fill)
    ret[inds] = data
  else:
    ret = np.empty((count,) + data.shape[1:], dtype=np.float32)
    ret.fill(fill)
    ret[inds, :] = data
  return ret 
Example 24
Project: cgp-cnn   Author: sg-nm   File: cgp.py    MIT License 5 votes vote down vote up
def __init__(self, net_info):
        self.net_info = net_info
        self.gene = np.zeros((self.net_info.node_num + self.net_info.out_num, self.net_info.max_in_num + 1)).astype(int)
        self.is_active = np.empty(self.net_info.node_num + self.net_info.out_num).astype(bool)
        self.eval = None
        self.init_gene() 
Example 25
Project: cgp-cnn   Author: sg-nm   File: cgp.py    MIT License 5 votes vote down vote up
def evolution(self, max_eval=100, mutation_rate=0.01, log_file='./log.txt'):
        with open(log_file, 'w') as fw:
            writer = csv.writer(fw, lineterminator='\n')

            eval_flag = np.empty(self.lam)

            self._evaluation([self.pop[0]], np.array([True]))
            print(self._log_data(net_info_type='active_only'))

            while self.num_eval < max_eval:
                self.num_gen += 1

                # reproduction
                for i in range(self.lam):
                    self.pop[i+1].copy(self.pop[0])    # copy a parent
                    eval_flag[i] = self.pop[i+1].mutation(mutation_rate)    # mutation

                # evaluation and selection
                evaluations = self._evaluation(self.pop[1:], eval_flag=eval_flag)
                best_arg = evaluations.argmax()
                if evaluations[best_arg] >= self.pop[0].eval:
                    self.pop[0].copy(self.pop[best_arg+1])

                # display and save log
                if eval_flag.sum() > 0:
                    print(self._log_data(net_info_type='active_only'))
                    writer.writerow(self._log_data(net_info_type='full'))

    # Modified CGP (used for GECCO 2017 paper):
    #   At each iteration:
    #     - Generate lambda individuals in which at least one active node changes (i.e., forced mutation)
    #     - Mutate the best individual with neutral mutation (unchanging the active nodes)
    #         if the best individual is not updated. 
Example 26
Project: cgp-cnn   Author: sg-nm   File: cgp.py    MIT License 5 votes vote down vote up
def modified_evolution(self, max_eval=100, mutation_rate=0.01, log_file='./log.txt'):
        with open(log_file, 'w') as fw:
            writer = csv.writer(fw, lineterminator='\n')

            eval_flag = np.empty(self.lam)

            active_num = self.pop[0].count_active_node()
            while active_num < self.pop[0].net_info.min_active_num or active_num > self.pop[0].net_info.max_active_num:
                self.pop[0].mutation(1.0)
                active_num = self.pop[0].count_active_node()
            self._evaluation([self.pop[0]], np.array([True]))
            print(self._log_data(net_info_type='active_only'))

            while self.num_eval < max_eval:
                self.num_gen += 1

                # reproduction
                for i in range(self.lam):
                    eval_flag[i] = False
                    self.pop[i + 1].copy(self.pop[0])  # copy a parent
                    active_num = self.pop[i + 1].count_active_node()

                    # forced mutation
                    while not eval_flag[i] or active_num < self.pop[i + 1].net_info.min_active_num \
                            or active_num > self.pop[i + 1].net_info.max_active_num:
                        self.pop[i + 1].copy(self.pop[0])  # copy a parent
                        eval_flag[i] = self.pop[i + 1].mutation(mutation_rate)  # mutation
                        active_num = self.pop[i + 1].count_active_node()

                # evaluation and selection
                evaluations = self._evaluation(self.pop[1:], eval_flag=eval_flag)
                best_arg = evaluations.argmax()
                if evaluations[best_arg] > self.pop[0].eval:
                    self.pop[0].copy(self.pop[best_arg + 1])
                else:
                    self.pop[0].neutral_mutation(mutation_rate)  # neutral mutation

                # display and save log
                print(self._log_data(net_info_type='active_only'))
                writer.writerow(self._log_data(net_info_type='full')) 
Example 27
Project: FRIDA   Author: LCAV   File: mkl_fft.py    MIT License 5 votes vote down vote up
def cce2full(A):

    # Assume all square for now

    N = A.shape
    N_half = N[0]//2 + 1
    out = np.empty((A.shape[0], A.shape[0]), dtype=A.dtype)
    out[:, :N_half] = A

    out[1:, N_half:] = np.rot90(A[1:, 1:-1], 2).conj()

    # Complete the first row
    out[0, N_half:] = A[0, -2:0:-1].conj()

    return out 
Example 28
Project: SmartPiCam   Author: robodhhb   File: smartPiCamContr.py    MIT License 5 votes vote down vote up
def takePhoto(self):
        picData = np.empty((self.cameraResolution[1],
                            self.cameraResolution[0], 3),
                            dtype=np.uint8)
        self.cam.capture(picData, format= 'rgb', use_video_port=self.useVideoPort) #24bit rgb format
        # Coco-Model requires 300 x 300 resolution
        # Remove last 4 rows and last 4 colummns in all 3 dimensions
        picData= picData[:-4, :-4]
        return picData
    
    # Function to read labels from text files. 
Example 29
Project: ieml   Author: IEMLdev   File: _test_tables.py    GNU General Public License v3.0 5 votes vote down vote up
def test_2d_multiplicative_script(self):
        script = self.parser.parse("M:.E:A:M:.-")
        tables = script.tables
        row_headers = [self.parser.parse("S:.E:A:M:.-"), self.parser.parse("B:.E:A:M:.-"),
                       self.parser.parse("T:.E:A:M:.-")]
        col_headers = [self.parser.parse("M:.E:A:S:.-"), self.parser.parse("M:.E:A:B:.-"),
                       self.parser.parse("M:.E:A:T:.-")]
        tab_headers = [script]
        cells = np.empty((3, 3), dtype=object)

        cells[0][0] = self.parser.parse("S:.E:A:S:.-")
        cells[0][1] = self.parser.parse("S:.E:A:B:.-")
        cells[0][2] = self.parser.parse("S:.E:A:T:.-")
        cells[1][0] = self.parser.parse("B:.E:A:S:.-")
        cells[1][1] = self.parser.parse("B:.E:A:B:.-")
        cells[1][2] = self.parser.parse("B:.E:A:T:.-")
        cells[2][0] = self.parser.parse("T:.E:A:S:.-")
        cells[2][1] = self.parser.parse("T:.E:A:B:.-")
        cells[2][2] = self.parser.parse("T:.E:A:T:.-")

        row_col_h = list(tables[0].headers.values())[0]

        self.assertEqual(len(tables), 1, "Correct number of tables generated")
        self.assertTrue(tables[0].cells.shape == cells.shape, "Table has the correct shape")
        self.assertEqual(row_col_h[0], row_headers, "Row headers are generated correctly")
        self.assertTrue((row_col_h[1] == col_headers), "Column headers are generated correctly")
        self.assertEqual(list(tables[0].headers), tab_headers, "Tab headers are generated correctly")
        self.assertTrue((tables[0].cells == cells).all(), "Cells are generated correctly")
        self.assertTrue(tables[0].paradigm == script, "Table has correct paradigm") 
Example 30
Project: DataHack2018   Author: InnovizTech   File: math_utils.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def extract_quaternion(R):
    d = np.diagonal(R)
    t = np.sum(d)
    if t + 1 < 0.25:
        symmetric_mat = R + R.T
        asymmetric_mat = R - R.T
        symmetric_diag = np.diagonal(symmetric_mat)
        i_max = np.argmax(symmetric_diag)
        q = np.empty(4)
        if i_max == 0:
            q[1] = np.sqrt(symmetric_diag[0] - t + 1) / 2
            normalizer = 1 / q[1]
            q[2] = symmetric_mat[1, 0] / 4 * normalizer
            q[3] = symmetric_mat[2, 0] / 4 * normalizer
            q[0] = asymmetric_mat[2, 1] / 4 * normalizer
        elif i_max == 1:
            q[2] = np.sqrt(symmetric_diag[1] - t + 1) / 2
            normalizer = 1 / q[2]
            q[1] = symmetric_mat[1, 0] / 4 * normalizer
            q[3] = symmetric_mat[2, 1] / 4 * normalizer
            q[0] = asymmetric_mat[0, 2] / 4 * normalizer
        elif i_max == 2:
            q[3] = np.sqrt(symmetric_diag[2] - t + 1) / 2
            normalizer = 1 / q[3]
            q[1] = symmetric_mat[2, 0] / 4 * normalizer
            q[2] = symmetric_mat[1, 2] / 4 * normalizer
            q[0] = asymmetric_mat[1, 0] / 4 * normalizer
    else:
        r = np.sqrt(1+t)
        s = 0.5 / r
        q = np.array([0.5*r, (R[2, 1] - R[1, 2])*s, (R[0, 2] - R[2, 0])*s, (R[1, 0] - R[0, 1])*s])

    return q 
Example 31
Project: FasterRCNN_TF_Py3   Author: upojzsb   File: minibatch.py    MIT License 5 votes vote down vote up
def get_minibatch(roidb, num_classes):
    """Given a roidb, construct a minibatch sampled from it."""
    num_images = len(roidb)
    # Sample random scales to use for each image in this batch
    random_scale_inds = npr.randint(0, high=len(cfg.FLAGS2["scales"]),
                                    size=num_images)
    assert (cfg.FLAGS.batch_size % num_images == 0), 'num_images ({}) must divide BATCH_SIZE ({})'.format(num_images, cfg.FLAGS.batch_size)

    # Get the input image blob, formatted for caffe
    im_blob, im_scales = _get_image_blob(roidb, random_scale_inds)

    blobs = {'data': im_blob}

    assert len(im_scales) == 1, "Single batch only"
    assert len(roidb) == 1, "Single batch only"

    # gt boxes: (x1, y1, x2, y2, cls)
    if cfg.FLAGS.use_all_gt:
        # Include all ground truth boxes
        gt_inds = np.where(roidb[0]['gt_classes'] != 0)[0]
    else:
        # For the COCO ground truth boxes, exclude the ones that are ''iscrowd''
        gt_inds = np.where(roidb[0]['gt_classes'] != 0 & np.all(roidb[0]['gt_overlaps'].toarray() > -1.0, axis=1))[0]
    gt_boxes = np.empty((len(gt_inds), 5), dtype=np.float32)
    gt_boxes[:, 0:4] = roidb[0]['boxes'][gt_inds, :] * im_scales[0]
    gt_boxes[:, 4] = roidb[0]['gt_classes'][gt_inds]
    blobs['gt_boxes'] = gt_boxes
    blobs['im_info'] = np.array(
        [[im_blob.shape[1], im_blob.shape[2], im_scales[0]]],
        dtype=np.float32)

    return blobs 
Example 32
Project: FasterRCNN_TF_Py3   Author: upojzsb   File: anchor_target_layer.py    MIT License 5 votes vote down vote up
def _unmap(data, count, inds, fill=0):
    """ Unmap a subset of item (data) back to the original set of items (of
    size count) """
    if len(data.shape) == 1:
        ret = np.empty((count,), dtype=np.float32)
        ret.fill(fill)
        ret[inds] = data
    else:
        ret = np.empty((count,) + data.shape[1:], dtype=np.float32)
        ret.fill(fill)
        ret[inds, :] = data
    return ret 
Example 33
Project: disentangling_conditional_gans   Author: zalandoresearch   File: tfutil.py    MIT License 5 votes vote down vote up
def _init_graph(self):
        # Collect inputs.
        self.input_names = []
        for param in inspect.signature(self._build_func).parameters.values():
            if param.kind == param.POSITIONAL_OR_KEYWORD and param.default is param.empty:
                self.input_names.append(param.name)
        self.num_inputs = len(self.input_names)
        assert self.num_inputs >= 1

        # Choose name and scope.
        if self.name is None:
            self.name = self._build_func_name
        self.scope = tf.get_default_graph().unique_name(self.name.replace('/', '_'), mark_as_used=False)
        
        # Build template graph.
        with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE):
            assert tf.get_variable_scope().name == self.scope
            with absolute_name_scope(self.scope): # ignore surrounding name_scope
                with tf.control_dependencies(None): # ignore surrounding control_dependencies
                    self.input_templates = [tf.placeholder(tf.float32, name=name) for name in self.input_names]
                    out_expr = self._build_func(*self.input_templates, is_template_graph=True, **self.static_kwargs)
            
        # Collect outputs.
        assert is_tf_expression(out_expr) or isinstance(out_expr, tuple)
        self.output_templates = [out_expr] if is_tf_expression(out_expr) else list(out_expr)
        self.output_names = [t.name.split('/')[-1].split(':')[0] for t in self.output_templates]
        self.num_outputs = len(self.output_templates)
        assert self.num_outputs >= 1
        
        # Populate remaining fields.
        self.input_shapes   = [shape_to_list(t.shape) for t in self.input_templates]
        self.output_shapes  = [shape_to_list(t.shape) for t in self.output_templates]
        self.input_shape    = self.input_shapes[0]
        self.output_shape   = self.output_shapes[0]
        self.vars           = OrderedDict([(self.get_var_localname(var), var) for var in tf.global_variables(self.scope + '/')])
        self.trainables     = OrderedDict([(self.get_var_localname(var), var) for var in tf.trainable_variables(self.scope + '/')])

    # Run initializers for all variables defined by this network. 
Example 34
Project: Electrolyte_Analysis_FTIR   Author: Samuel-Buteau   File: Constant_run.py    MIT License 5 votes vote down vote up
def get(self, n):
        """
        will return a list of n random numbers in self.GetFresh_list
        - Samuel Buteau, October 2018
        """
        if n >= self.get_fresh_count:
            return numpy.concatenate((self.get(int(n/2)),self.get(n- int(n/2))))


        reshuffle_flag = False

        n_immediate_fulfill = min(n, self.get_fresh_count - self.get_fresh_pos)
        batch_of_indecies = numpy.empty([n], dtype=numpy.int32)
        for i in range(0, n_immediate_fulfill):
            batch_of_indecies[i] = self.GetFresh_list[i + self.get_fresh_pos]

        self.get_fresh_pos += n_immediate_fulfill
        if self.get_fresh_pos >= self.get_fresh_count:
            self.get_fresh_pos -= self.get_fresh_count
            reshuffle_flag = True

            # Now, the orders that needed to be satisfied are satisfied.
        n_delayed_fulfill = max(0, n - n_immediate_fulfill)
        if reshuffle_flag:
            numpy.random.shuffle(self.GetFresh_list)

        if n_delayed_fulfill > 0:
            for i in range(0, n_delayed_fulfill):
                batch_of_indecies[i + n_immediate_fulfill] = self.GetFresh_list[i]
            self.get_fresh_pos = n_delayed_fulfill

        return batch_of_indecies 
Example 35
Project: mmdetection   Author: open-mmlab   File: transforms.py    Apache License 2.0 5 votes vote down vote up
def _pad_masks(self, results):
        pad_shape = results['pad_shape'][:2]
        for key in results.get('mask_fields', []):
            padded_masks = [
                mmcv.impad(mask, pad_shape, pad_val=self.pad_val)
                for mask in results[key]
            ]
            if padded_masks:
                results[key] = np.stack(padded_masks, axis=0)
            else:
                results[key] = np.empty((0, ) + pad_shape, dtype=np.uint8) 
Example 36
Project: models   Author: kipoi   File: dataloader.py    MIT License 5 votes vote down vote up
def nans(shape, dtype=float):
    a = np.empty(shape, dtype)
    a.fill(np.nan)
    return a 
Example 37
Project: skylab   Author: coenders   File: utils.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, eta, df, loc=0., scale=1.):
        params = np.empty(shape=(2, 3))
        params[:, 0] = df
        params[:, 1] = loc
        params[:, 2] = scale

        self.eta = eta
        self.params = params

        self._chi2 = tuple(scipy.stats.chi2(*p) for p in params)

        self.eta_err = np.nan
        self.ks = (np.nan, np.nan) 
Example 38
Project: skylab   Author: coenders   File: utils.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, df=np.nan, left={}, right={}):
        self.df = np.empty(2)
        self.df[:] = df
        self.others = (left, right) 
Example 39
Project: core   Author: lifemapper   File: geotools.py    GNU General Public License v3.0 5 votes vote down vote up
def getArray(self, bandnum, doFlip=False, doShift=False):
      """
      @summary: Read the dataset into numpy array  
      @param bandnum: The band number to read.
      @param doFlip: True if data begins at the southern edge of the region
      @param doShift: True if the leftmost edge of the data should be shifted 
             to the center (and right half shifted around to the beginning) 
      """
      if 'numpy' in dir():
         inds = gdal.Open(self.dlocation, gdalconst.GA_ReadOnly)
         inband = inds.GetRasterBand(bandnum)
         arrtype = self._getNumpyType(self.gdalBandType)
         outArr = numpy.empty([self.ysize, self.xsize], dtype=arrtype)
         
         for row in range(self.ysize):
            scanline = inband.ReadAsArray(0, row, self.xsize, 1, self.xsize, 1)
            
            if doShift:
               scanline = self._cycleRow(scanline, arrtype, 0, self.xsize/2, 
                                         self.xsize)
            if doFlip:
               newrow = self.ysize-row-1
            else:
               newrow = row
   
            outArr[newrow] = scanline
   
         inds = None   
         return outArr
      else:
         raise LMError('numpy missing - unable to getArray')
      
# ............................................. 
Example 40
Project: mmi-tagger   Author: karlstratos   File: evaluate.py    MIT License 5 votes vote down vote up
def compute_v_measure(tseqs, zseqs):
    num_instances = 0
    t2i = {}
    z2i = {}
    cocount = Counter()
    for i in range(len(tseqs)):
        for (t, z) in zip(tseqs[i], zseqs[i]):
            num_instances += 1
            if not t in t2i: t2i[t] = len(t2i)
            if not z in z2i: z2i[z] = len(z2i)
            cocount[(t2i[t], z2i[z])] += 1

    B = np.empty([len(t2i), len(z2i)])
    for i in range(len(t2i)):
        for j in range(len(z2i)):
            B[i, j] = cocount[(i, j)] / num_instances

    p_T = np.sum(B, axis=1)
    p_Z = np.sum(B, axis=0)
    H_T = sum([- p_T[i] * np.log2(p_T[i]) for i in range(len(t2i))])
    H_Z = sum([- p_Z[i] * np.log2(p_Z[i]) for i in range(len(z2i))])

    H_T_given_Z = 0
    for j in range(len(z2i)):
        for i in range(len(t2i)):
            if B[i, j] > 0.0:
                H_T_given_Z -= B[i, j] * \
                               (np.log2(B[i, j]) - np.log2(p_Z[j]))
    H_Z_given_T = 0
    for j in range(len(t2i)):
        for i in range(len(z2i)):
            if B[j, i] > 0.0:
                H_Z_given_T -= B[j, i] * \
                               (np.log2(B[j, i]) - np.log2(p_T[j]))

    h = 1 if len(t2i) == 1 else 1 - H_T_given_Z / H_T
    c = 1 if len(z2i) == 1 else 1 - H_Z_given_T / H_Z

    return 2 * h * c / (h + c) * 100.0 
Example 41
Project: AnisotropicMultiStreamCNN   Author: AnnekeMeyer   File: data_generation.py    MIT License 5 votes vote down vote up
def __data_generation(self, list_IDs_temp):
        'Generates data containing batch_size samples'  # X : (n_samples, *dim, n_channels)
        # Initialization
        X_tra = np.empty([self.batch_size, self.volumeSize_slices, 4*self.volumeSize_slices, 4*self.volumeSize_slices,self.n_channels])
        X_sag = np.empty([self.batch_size, 4*self.volumeSize_slices,4*self.volumeSize_slices,self.volumeSize_slices, self.n_channels])
        X_cor = np.empty([self.batch_size, 4*self.volumeSize_slices,self.volumeSize_slices,4*self.volumeSize_slices, self.n_channels])
        Y = np.empty([self.batch_size, 4*self.volumeSize_slices,4*self.volumeSize_slices,4*self.volumeSize_slices,1], dtype=np.uint8)


        # Generate data
        for i, ID in enumerate(list_IDs_temp):
            # load sample
            roi_tra = sitk.ReadImage(self.data_dir + '/' + ID + '/roi_tra.nrrd')
            roi_cor = sitk.ReadImage(self.data_dir + '/' + ID + '/roi_cor.nrrd')
            roi_sag = sitk.ReadImage(self.data_dir + '/' + ID + '/roi_sag.nrrd')
            roi_GT = sitk.ReadImage(self.data_dir + '/' + ID + '/roi_GT.nrrd')

            # augment sample
            augm_tra, augm_sag, augm_cor, augm_GT = dataAugmentation.augmentImages(roi_tra, roi_sag, roi_cor, roi_GT)

            # crop ROIS to input size of network
            # get size of uncropped ROI
            inPlaneSize = roi_tra.GetSize()[0]
            a = int((inPlaneSize - 4*self.volumeSize_slices) / 2)
            b = int((inPlaneSize/4 - self.volumeSize_slices) / 2)
            augm_tra = utils.cropImage(augm_tra, [a, a, b], [a, a, b])
            augm_cor = utils.cropImage(augm_cor, [a, b, a], [a, b, a])
            augm_sag = utils.cropImage(augm_sag, [b, a, a], [b, a, a])
            augm_GT = utils.cropImage(augm_GT, [a, a, a], [a, a, a])


            # store augmented sample
            X_tra[i, :, :, :, 0] = sitk.GetArrayFromImage(augm_tra)
            X_cor[i, :, :, :, 0] = sitk.GetArrayFromImage(augm_cor)
            X_sag[i, :, :, :, 0] = sitk.GetArrayFromImage(augm_sag)
            Y[i, :, :, :, 0] = sitk.GetArrayFromImage(augm_GT)


        return [X_tra, X_cor,X_sag] , [Y] 
Example 42
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: atari_game.py    Apache License 2.0 5 votes vote down vote up
def __init__(self,
                 rom_path=_default_rom_path,
                 frame_skip=4, history_length=4,
                 resize_mode='scale', resized_rows=84, resized_cols=84, crop_offset=8,
                 display_screen=False, max_null_op=30,
                 replay_memory_size=1000000,
                 replay_start_size=100,
                 death_end_episode=True):
        super(AtariGame, self).__init__()
        self.rng = get_numpy_rng()
        self.ale = ale_load_from_rom(rom_path=rom_path, display_screen=display_screen)
        self.start_lives = self.ale.lives()
        self.action_set = self.ale.getMinimalActionSet()
        self.resize_mode = resize_mode
        self.resized_rows = resized_rows
        self.resized_cols = resized_cols
        self.crop_offset = crop_offset
        self.frame_skip = frame_skip
        self.history_length = history_length
        self.max_null_op = max_null_op
        self.death_end_episode = death_end_episode
        self.screen_buffer_length = 2
        self.screen_buffer = numpy.empty((self.screen_buffer_length,
                                          self.ale.getScreenDims()[1], self.ale.getScreenDims()[0]),
                                         dtype='uint8')
        self.replay_memory = ReplayMemory(state_dim=(resized_rows, resized_cols),
                                          history_length=history_length,
                                          memory_size=replay_memory_size,
                                          replay_start_size=replay_start_size)
        self.start() 
Example 43
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: replay_memory.py    Apache License 2.0 5 votes vote down vote up
def sample_last(self, batch_size, states, offset):
        assert self.size >= batch_size and self.replay_start_size >= self.history_length
        assert(0 <= self.size <= self.memory_size)
        assert(0 <= self.top <= self.memory_size)
        if self.size <= self.replay_start_size:
            raise ValueError("Size of the effective samples of the ReplayMemory must be "
                             "bigger than start_size! Currently, size=%d, start_size=%d"
                             %(self.size, self.replay_start_size))
        actions = numpy.empty((batch_size,) + self.action_dim, dtype=self.actions.dtype)
        rewards = numpy.empty(batch_size, dtype='float32')
        terminate_flags = numpy.empty(batch_size, dtype='bool')
        counter = 0
        first_index = self.top - self.history_length - 1
        while counter < batch_size:
            full_indices = numpy.arange(first_index, first_index + self.history_length+1)
            end_index = first_index + self.history_length
            if numpy.any(self.terminate_flags.take(full_indices[0:self.history_length], mode='wrap')):
                # Check if terminates in the middle of the sample!
                first_index -= 1
                continue
            states[counter + offset] = self.states.take(full_indices, axis=0, mode='wrap')
            actions[counter] = self.actions.take(end_index, axis=0, mode='wrap')
            rewards[counter] = self.rewards.take(end_index, mode='wrap')
            terminate_flags[counter] = self.terminate_flags.take(end_index, mode='wrap')
            counter += 1
            first_index -= 1
        return actions, rewards, terminate_flags 
Example 44
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: replay_memory.py    Apache License 2.0 5 votes vote down vote up
def sample_mix(self, batch_size, states, offset, current_index):
        assert self.size >= batch_size and self.replay_start_size >= self.history_length
        assert(0 <= self.size <= self.memory_size)
        assert(0 <= self.top <= self.memory_size)
        if self.size <= self.replay_start_size:
            raise ValueError("Size of the effective samples of the ReplayMemory must be bigger than "
                             "start_size! Currently, size=%d, start_size=%d"
                             %(self.size, self.replay_start_size))
        actions = numpy.empty((batch_size,) + self.action_dim, dtype=self.actions.dtype)
        rewards = numpy.empty(batch_size, dtype='float32')
        terminate_flags = numpy.empty(batch_size, dtype='bool')
        counter = 0
        first_index = self.top - self.history_length + current_index
        thisid = first_index
        while counter < batch_size:
            full_indices = numpy.arange(thisid, thisid + self.history_length+1)
            end_index = thisid + self.history_length
            if numpy.any(self.terminate_flags.take(full_indices[0:self.history_length], mode='wrap')):
                # Check if terminates in the middle of the sample!
                thisid -= 1
                continue
            states[counter+offset] = self.states.take(full_indices, axis=0, mode='wrap')
            actions[counter] = self.actions.take(end_index, axis=0, mode='wrap')
            rewards[counter] = self.rewards.take(end_index, mode='wrap')
            terminate_flags[counter] = self.terminate_flags.take(end_index, mode='wrap')
            counter += 1
            thisid = self.rng.randint(low=self.top - self.size, high=self.top - self.history_length-1)
        return actions, rewards, terminate_flags 
Example 45
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: image.py    Apache License 2.0 5 votes vote down vote up
def get_image(roi_rec, short, max_size, mean, std):
    """
    read, resize, transform image, return im_tensor, im_info, gt_boxes
    roi_rec should have keys: ["image", "boxes", "gt_classes", "flipped"]
    0 --- x (width, second dim of im)
    |
    y (height, first dim of im)
    """
    im = imdecode(roi_rec['image'])
    if roi_rec["flipped"]:
        im = im[:, ::-1, :]
    im, im_scale = resize(im, short, max_size)
    height, width = im.shape[:2]
    im_info = np.array([height, width, im_scale], dtype=np.float32)
    im_tensor = transform(im, mean, std)

    # gt boxes: (x1, y1, x2, y2, cls)
    if roi_rec['gt_classes'].size > 0:
        gt_inds = np.where(roi_rec['gt_classes'] != 0)[0]
        gt_boxes = np.empty((len(gt_inds), 5), dtype=np.float32)
        gt_boxes[:, 0:4] = roi_rec['boxes'][gt_inds, :]
        gt_boxes[:, 4] = roi_rec['gt_classes'][gt_inds]
        # scale gt_boxes
        gt_boxes[:, 0:4] *= im_scale
    else:
        gt_boxes = np.empty((0, 5), dtype=np.float32)

    return im_tensor, im_info, gt_boxes 
Example 46
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    Apache License 2.0 5 votes vote down vote up
def _new_empty_handle():
    """Returns a new empty handle.

    Empty handle can be used to hold a result.

    Returns
    -------
    handle
        A new empty `NDArray` handle.
    """
    hdl = NDArrayHandle()
    check_call(_LIB.MXNDArrayCreateNone(ctypes.byref(hdl)))
    return hdl 
Example 47
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    Apache License 2.0 5 votes vote down vote up
def astype(self, dtype, copy=True):
        """Returns a copy of the array after casting to a specified type.

        Parameters
        ----------
        dtype : numpy.dtype or str
            The type of the returned array.
        copy : bool
            Default `True`. By default, astype always returns a newly
            allocated ndarray on the same context. If this is set to
            `False`, and the dtype requested is the same as the ndarray's
            dtype, the ndarray is returned instead of a copy.

        Returns
        -------
        NDArray, CSRNDArray or RowSparseNDArray
            The copied array after casting to the specified type, or
            the same array if copy=False and dtype is the same as the input
            array.

        Examples
        --------
        >>> x = mx.nd.zeros((2,3), dtype='float32')
        >>> y = x.astype('int32')
        >>> y.dtype
        <type 'numpy.int32'>
        """

        if not copy and np.dtype(dtype) == self.dtype:
            return self

        res = empty(self.shape, ctx=self.context, dtype=dtype)
        self.copyto(res)
        return res 
Example 48
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    Apache License 2.0 5 votes vote down vote up
def ones(shape, ctx=None, dtype=None, **kwargs):
    """Returns a new array filled with all ones, with the given shape and type.

    Parameters
    ----------
    shape : int or tuple of int or list of int
        The shape of the empty array.
    ctx : Context, optional
        An optional device context.
        Defaults to the current default context (``mxnet.context.current_context()``).
    dtype : str or numpy.dtype, optional
        An optional value type (default is `float32`).
    out : NDArray, optional
        The output NDArray (default is `None`).

    Returns
    -------
    NDArray
        A new array of the specified shape filled with all ones.

    Examples
    --------
    >>> mx.nd.ones(1).asnumpy()
    array([ 1.], dtype=float32)
    >>> mx.nd.ones((1,2), mx.gpu(0))
    <NDArray 1x2 @gpu(0)>
    >>> mx.nd.ones((1,2), dtype='float16').asnumpy()
    array([[ 1.,  1.]], dtype=float16)
    """
    # pylint: disable= unused-argument
    if ctx is None:
        ctx = current_context()
    dtype = mx_real_t if dtype is None else dtype
    # pylint: disable= no-member, protected-access
    return _internal._ones(shape=shape, ctx=ctx, dtype=dtype, **kwargs)
    # pylint: enable= no-member, protected-access 
Example 49
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    Apache License 2.0 5 votes vote down vote up
def array(source_array, ctx=None, dtype=None):
    """Creates an array from any object exposing the array interface.

    Parameters
    ----------
    source_array : array_like
        An object exposing the array interface, an object whose `__array__`
        method returns an array, or any (nested) sequence.
    ctx : Context, optional
        Device context (default is the current default context).
    dtype : str or numpy.dtype, optional
        The data type of the output array. The default dtype is ``source_array.dtype``
        if `source_array` is an `NDArray`, `float32` otherwise.

    Returns
    -------
    NDArray
        An `NDArray` with the same contents as the `source_array`.
    """
    if isinstance(source_array, NDArray):
        dtype = source_array.dtype if dtype is None else dtype
    else:
        dtype = mx_real_t if dtype is None else dtype
        if not isinstance(source_array, np.ndarray):
            try:
                source_array = np.array(source_array, dtype=dtype)
            except:
                raise TypeError('source_array must be array like object')
    arr = empty(source_array.shape, ctx, dtype)
    arr[:] = source_array
    return arr 
Example 50
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    Apache License 2.0 5 votes vote down vote up
def zeros(shape, ctx=None, dtype=None, **kwargs):
    """Returns a new array filled with all zeros, with the given shape and type.

    Parameters
    ----------
    shape : int or tuple of int
        The shape of the empty array.
    ctx : Context, optional
        An optional device context (default is the current default context).
    dtype : str or numpy.dtype, optional
        An optional value type (default is `float32`).
    out : NDArray, optional
        The output NDArray (default is `None`).

    Returns
    -------
    NDArray
        A created array

    Examples
    --------
    >>> mx.nd.zeros(1).asnumpy()
    array([ 0.], dtype=float32)
    >>> mx.nd.zeros((1,2), mx.gpu(0))
    <NDArray 1x2 @gpu(0)>
    >>> mx.nd.zeros((1,2), mx.gpu(0), 'float16').asnumpy()
    array([[ 0.,  0.]], dtype=float16)
    """
    # pylint: disable= unused-argument
    if ctx is None:
        ctx = current_context()
    dtype = mx_real_t if dtype is None else dtype
    # pylint: disable= no-member, protected-access
    return _internal._zeros(shape=shape, ctx=ctx, dtype=dtype, **kwargs)
    # pylint: enable= no-member, protected-access