def test_Floor(tmpdir): data = np.asarray([0.2, 1.3, 4., 5.5, 0.0], dtype=np.float32) model = C.floor(data) verify_no_input(model, tmpdir, 'Floor_0') x = C.input_variable(data.shape) model = C.floor(x) verify_one_input(model, data, tmpdir, 'Floor_1')
def test_Floor(tmpdir, dtype): with C.default_options(dtype=dtype): data = np.asarray([0.2, 1.3, 4., 5.5, 0.0], dtype=dtype) model = C.floor(data) verify_no_input(model, tmpdir, 'Floor_0') x = C.input_variable(data.shape) model = C.floor(x) verify_one_input(model, data, tmpdir, 'Floor_1')
def test_Floor(tmpdir, dtype): with C.default_options(dtype = dtype): data = np.asarray([0.2, 1.3, 4., 5.5, 0.0], dtype=dtype) model = C.floor(data) verify_no_input(model, tmpdir, 'Floor_0') x = C.input_variable(data.shape) model = C.floor(x) verify_one_input(model, data, tmpdir, 'Floor_1')
def grid_coord(guide, xx, yy, sz, small_sz, sigma_r, bs): gx = ((xx + 0.5) / sz) * small_sz gy = ((yy + 0.5) / sz) * small_sz expanded_guide = C.reshape(guide, [bs, 1, sz, sz]) gz = expanded_guide * sigma_r fx = C.floor(gx - 0.5) fy = C.floor(gy - 0.5) fz = C.clip(C.floor(gz - 0.5), 0, sigma_r - 1) cx = C.element_min(fx + 1, small_sz - 1) cy = C.element_min(fy + 1, small_sz - 1) cz = C.clip(fz + 1, 0, sigma_r - 1) return gx, gy, gz, fx, fy, fz, cx, cy, cz
def floor(arg, name=''): ''' The output of this operation is the element wise value rounded to the largest integer less than or equal to the input. Example: >>> C.eval(C.floor([0.2, 1.3, 4., 5.5, 0.0])) [array([[ 0., 1., 4., 5., 0.]])] >>> C.eval(C.floor([[0.6, 3.3], [1.9, 5.6]])) [array([[[ 0., 3.], [ 1., 5.]]])] >>> C.eval(C.floor([-5.5, -4.2, -3., -0.7, 0])) [array([[-6., -5., -3., -1., 0.]])] >>> C.eval(C.floor([[-0.6, -4.3], [1.9, -3.2]])) [array([[[-1., -5.], [ 1., -4.]]])] Args: arg: input tensor name (str): the name of the node in the network (optional) Returns: :class:`cntk.Function` ''' from cntk import floor left = sanitize_input(left, get_data_type(right)) right = sanitize_input(right, get_data_type(left)) return floor(arg, name).output()
def inner(a): p = position(a) quotient = p / s # every s sequence item will be an integer decimals = quotient - C.floor( quotient) # every s sequence item will be a zero valid = C.equal(decimals, 0) result = C.sequence.gather(a, valid) return result
def hierarchical_softmax_layer(input_var, label_index, label_dim, label_classes=None): ''' A two layers hierarchical softmax function: Args: input_var: Variable with shape: [#,*](dim_x) label_index: index of label's category: [#,*](1) label_dim: number of the label categories label_classes: number of classes of the label categories Returns: output_prob: the probability of the given label [#,*](1) class_probs: the probability of all the label classes [#,*](label_classes) all_probs: the probability of all label classes ''' input_dim = input_var.shape[0] if not label_classes: label_classes = int(np.ceil(np.sqrt(float(label_dim)))) n_outputs_per_class = int(np.ceil(label_dim / label_classes)) target_class = C.floor((label_index + 0.5) / n_outputs_per_class) target_output_in_class = C.round(label_index - target_class * n_outputs_per_class) w1 = parameter(shape=(input_dim, label_classes), init=C.glorot_normal(), name='hsoftmax_w1') b1 = parameter(shape=(label_classes), init=C.glorot_normal(), name='hsoftmax_b1') w2s = parameter(shape=(label_classes, input_dim, n_outputs_per_class,), init=C.glorot_normal(), name='hsoftmax_w2s') b2s = parameter(shape=(label_classes, n_outputs_per_class,), init=C.glorot_normal(), name='hsoftmax_b2s') class_probs = softmax(b1 + times(input_var, w1)) # TODO: fix the bug in backprop for sparse, and use sparse embedding to accelerate target_class_one_hot = C.one_hot(target_class, num_classes=label_classes, sparse_output=False) w2 = C.reshape(C.times(target_class_one_hot, w2s, output_rank=2), [input_dim, -1]) b2 = C.reshape(times(target_class_one_hot, b2s, output_rank=1), [-1]) probs_in_class = softmax(b2 + times(input_var, w2)) prob_in_class = C.times_transpose(C.one_hot(target_output_in_class, num_classes=n_outputs_per_class, sparse_output=False), probs_in_class) class_prob = C.times_transpose(C.one_hot(target_class, num_classes=label_classes, sparse_output=False), class_probs) output_prob = prob_in_class * class_prob # this is for calculating all the outputs' probabilities all_probs = [] for i in range(label_classes): ci = C.constant(i) ci_one_hot = C.one_hot(ci, num_classes=label_classes, sparse_output=False) w2a = C.times(ci_one_hot, w2s, output_rank=2) b2a = C.times(ci_one_hot, b2s, output_rank=1) probs_in_classa = C.softmax(b2a + times(input_var, w2a)) class_proba = C.times_transpose(ci_one_hot, class_probs) output_proba = probs_in_classa * class_proba all_probs.append(output_proba) return output_prob, class_probs, all_probs
def inner(x, y): quotient = C.element_divide(x, y) integers = C.floor(quotient) decimals = quotient - integers remaining_value = decimals * y return remaining_value
def inner(x, y): quotient = C.element_divide(x, y) integers = C.floor(quotient) return integers
def test_floor(): assert_cntk_ngraph_array_equal(C.floor([0.2, 1.3, 4., 5.5, 0.0])) assert_cntk_ngraph_array_equal(C.floor([[0.6, 3.3], [1.9, 5.6]])) assert_cntk_ngraph_array_equal(C.floor([-5.5, -4.2, -3., -0.7, 0])) assert_cntk_ngraph_array_equal(C.floor([[-0.6, -4.3], [1.9, -3.2]]))
def main(): show_image = False if show_image: bs = 1 ci = 3 co = 3 cg = co * (ci + 1) gd = 8 gh = 64 gw = 64 h = 256 w = 256 else: bs = 1 ci = 3 co = 3 cg = co * (ci + 1) gd = 8 gh = 64 gw = 64 h = 1024 w = 1024 im = C.input_variable([bs, ci, h, w], needs_gradient=True, dynamic_axes=[]) guide = C.input_variable([bs, h, w], needs_gradient=True, dynamic_axes=[]) guide_no_grad = C.input_variable([bs, h, w], needs_gradient=False, dynamic_axes=[]) grid = C.input_variable([bs, cg, gd, gh, gw], needs_gradient=True, dynamic_axes=[]) # Create indices xx = np.arange(0, w).reshape(1, -1).repeat(h, 0).astype(np.float32) yy = np.arange(0, h).reshape(-1, 1).repeat(w, 1).astype(np.float32) xx = C.Constant(xx, xx.shape) yy = C.Constant(yy, yy.shape) gx = ((xx + 0.5) / w) * gw gy = ((yy + 0.5) / h) * gh gz = C.clip(guide, 0.0, 1.0) * gd gz_no_grad = C.clip(guide_no_grad, 0.0, 1.0) * gd fx = C.element_max(C.floor(gx - 0.5), 0.0) fy = C.element_max(C.floor(gy - 0.5), 0.0) fz = C.element_max(C.floor(gz - 0.5), 0.0) fz_no_grad = C.element_max(C.floor(gz_no_grad - 0.5), 0.0) wx = gx - 0.5 - fx wy = gy - 0.5 - fy wx = C.expand_dims(C.expand_dims(wx, -1 - len(wx.shape)), -1 - len(wx.shape)) wy = C.expand_dims(C.expand_dims(wy, -1 - len(wy.shape)), -1 - len(wy.shape)) wz = C.abs(gz - 0.5 - fz) wz = C.expand_dims(wz, 0) fx = C.expand_dims(C.expand_dims(fx, -1 - len(fx.shape)), -1 - len(fx.shape)) fy = C.expand_dims(C.expand_dims(fy, -1 - len(fy.shape)), -1 - len(fy.shape)) cx = C.element_min(fx + 1, gw - 1) cy = C.element_min(fy + 1, gh - 1) cz = C.element_min(fz_no_grad + 1, gd - 1) batch_idx = np.arange(bs).reshape(bs, 1, 1, 1).astype(np.float32) batch_idx = C.Constant(batch_idx, batch_idx.shape) out = [] flat_grid = C.reshape(grid, [-1]) for c_ in range(co): c_idx = np.arange((ci + 1) * c_, (ci + 1) * (c_ + 1)).reshape(1, ci + 1, 1, 1).astype(np.float32) c_idx = C.Constant(c_idx, c_idx.shape) def flatten_and_gather(x, y, z): linear_idx = x + gw * y + gw * gh * z + c_idx * gw * gh * gd + batch_idx * gw * gh * gd * cg flat_linear_idx = C.reshape(linear_idx, [-1]) return C.reshape(C.gather(flat_grid, flat_linear_idx), linear_idx.shape) gather_fff = flatten_and_gather(fx, fy, fz_no_grad) gather_ffc = flatten_and_gather(fx, fy, cz) gather_fcf = flatten_and_gather(fx, cy, fz_no_grad) gather_fcc = flatten_and_gather(fx, cy, cz) gather_cff = flatten_and_gather(cx, fy, fz_no_grad) gather_cfc = flatten_and_gather(cx, fy, cz) gather_ccf = flatten_and_gather(cx, cy, fz_no_grad) gather_ccc = flatten_and_gather(cx, cy, cz) a = gather_fff*(1-wx)*(1-wy)*(1-wz) + \ gather_ffc*(1-wx)*(1-wy)*( wz) + \ gather_fcf*(1-wx)*( wy)*(1-wz) + \ gather_fcc*(1-wx)*( wy)*( wz) + \ gather_cff*( wx)*(1-wy)*(1-wz) + \ gather_cfc*( wx)*(1-wy)*( wz) + \ gather_ccf*( wx)*( wy)*(1-wz) + \ gather_ccc*( wx)*( wy)*( wz) o = C.reduce_sum(a[:, :-1, ...] * im, 1) + a[:, -1, ...] print(o.shape) out.append(C.expand_dims(o, 0)) out = C.splice(*out, axis=1) loss = C.reduce_l2(out) grid_val = np.random.rand(bs, cg, gd, gh, gw).astype(np.float32) if show_image: guide_val = skio.imread("/data/rgb.png").mean(2)[:h, :w].astype( np.float32) guide_val = np.expand_dims(guide_val / 255.0, 0) im_val = np.tile(np.expand_dims(guide_val, 1), [1, 3, 1, 1]) out_val = out.eval({ im: im_val, guide: guide_val, guide_no_grad: guide_val, grid: grid_val }) out_val = np.clip(np.transpose(np.squeeze(out_val), [1, 2, 0]), 0, 1) skio.imsave("/output/imout.png", out_val) else: im_val = np.random.randn(bs, ci, h, w) guide_val = np.random.rand(bs, h, w).astype(np.float32) # burning iteration for it in range(5): print('burning (', it, ')') g = loss.grad({ im: im_val, guide: guide_val, guide_no_grad: guide_val, grid: grid_val }) # actual iterations start = time.time() for it in range(50): print('profiling (', it, ')') g = loss.grad({ im: im_val, guide: guide_val, guide_no_grad: guide_val, grid: grid_val }) end = time.time() runtime = (end - start) * 1000.0 / 50.0 print('Runtime:', runtime)
def main(): bs = 4 c = 64 h = 512 w = 512 im = C.input_variable([bs, c, h, w], needs_gradient=True, dynamic_axes=[]) warp = C.input_variable([bs, 2, h, w], needs_gradient=True, dynamic_axes=[]) warp_ng = C.input_variable([bs, 2, h, w], needs_gradient=False, dynamic_axes=[]) # Create indices dx = 0.5 * (warp[:, 0, :, :] + 1.0) dy = 0.5 * (warp[:, 1, :, :] + 1.0) new_x = C.clip(dx * w, 0, w) new_y = C.clip(dy * h, 0, h) fx = C.clip(C.floor(new_x), 0, w - 2) fy = C.clip(C.floor(new_y), 0, h - 2) wx = new_x - fx wy = new_y - fy dx_ng = 0.5 * (warp_ng[:, 0, :, :] + 1.0) dy_ng = 0.5 * (warp_ng[:, 1, :, :] + 1.0) new_x_ng = C.clip(dx_ng * w, 0, w) new_y_ng = C.clip(dy_ng * h, 0, h) fx_ng = C.clip(C.floor(new_x_ng), 0, w - 2) fy_ng = C.clip(C.floor(new_y_ng), 0, h - 2) chan_idx = np.arange(c).reshape(1, c, 1, 1) chan_idx = C.Constant(chan_idx, chan_idx.shape) batch_idx = np.arange(bs).reshape(bs, 1, 1, 1) batch_idx = C.Constant(batch_idx, batch_idx.shape) flat_im = C.reshape(im, [-1]) def flatten_and_gather(x, y): linear_idx = x + w * y + w * h * chan_idx + w * h * c * batch_idx flat_linear_idx = C.reshape(linear_idx, [-1]) return C.reshape(C.gather(flat_im, flat_linear_idx), linear_idx.shape) gather_ff = flatten_and_gather(fx_ng, fy_ng) gather_fc = flatten_and_gather(fx_ng, fy_ng + 1) gather_cf = flatten_and_gather(fx_ng + 1, fy_ng) gather_cc = flatten_and_gather(fx_ng + 1, fy_ng + 1) out = gather_ff*(1-wx)*(1-wy) + \ gather_fc*(1-wx)*( wy) + \ gather_cf*( wx)*(1-wy) + \ gather_cc*( wx)*( wy) loss = C.reduce_l2(out) im_val = np.random.randn(bs, c, h, w).astype(np.float32) warp_val = np.random.rand(bs, 2, h, w).astype(np.float32) # burning iteration for it in range(5): print('burning (', it, ')') g = loss.grad({im: im_val, warp: warp_val, warp_ng: warp_val}) # actual iterations start = time.time() for it in range(50): print('profiling (', it, ')') g = loss.grad({im: im_val, warp: warp_val, warp_ng: warp_val}) end = time.time() runtime = (end - start) * 1000.0 / 50.0 print('Runtime:', runtime)
def hierarchical_softmax_layer(input_var, label_index, label_dim, label_classes=None): ''' A two layers hierarchical softmax function: Args: input_var: Variable with shape: [#,*](dim_x) label_index: index of label's category: [#,*](1) label_dim: number of the label categories label_classes: number of classes of the label categories Returns: output_prob: the probability of the given label [#,*](1) class_probs: the probability of all the label classes [#,*](label_classes) all_probs: the probability of all label classes ''' input_dim = input_var.shape[0] if not label_classes: label_classes = int(np.ceil(np.sqrt(float(label_dim)))) n_outputs_per_class = int(np.ceil(label_dim / label_classes)) target_class = C.floor((label_index + 0.5) / n_outputs_per_class) target_output_in_class = C.round(label_index - target_class * n_outputs_per_class) w1 = parameter(shape=(input_dim, label_classes), init=C.glorot_normal(), name='hsoftmax_w1') b1 = parameter(shape=(label_classes), init=C.glorot_normal(), name='hsoftmax_b1') w2s = parameter(shape=( label_classes, input_dim, n_outputs_per_class, ), init=C.glorot_normal(), name='hsoftmax_w2s') b2s = parameter(shape=( label_classes, n_outputs_per_class, ), init=C.glorot_normal(), name='hsoftmax_b2s') class_probs = softmax(b1 + times(input_var, w1)) # TODO: fix the bug in backprop for sparse, and use sparse embedding to accelerate target_class_one_hot = C.one_hot(target_class, num_classes=label_classes, sparse_output=False) w2 = C.reshape(C.times(target_class_one_hot, w2s, output_rank=2), [input_dim, -1]) b2 = C.reshape(times(target_class_one_hot, b2s, output_rank=1), [-1]) probs_in_class = softmax(b2 + times(input_var, w2)) prob_in_class = C.times_transpose( C.one_hot(target_output_in_class, num_classes=n_outputs_per_class, sparse_output=False), probs_in_class) class_prob = C.times_transpose( C.one_hot(target_class, num_classes=label_classes, sparse_output=False), class_probs) output_prob = prob_in_class * class_prob # this is for calculating all the outputs' probabilities all_probs = [] for i in range(label_classes): ci = C.constant(i) ci_one_hot = C.one_hot(ci, num_classes=label_classes, sparse_output=False) w2a = C.times(ci_one_hot, w2s, output_rank=2) b2a = C.times(ci_one_hot, b2s, output_rank=1) probs_in_classa = C.softmax(b2a + times(input_var, w2a)) class_proba = C.times_transpose(ci_one_hot, class_probs) output_proba = probs_in_classa * class_proba all_probs.append(output_proba) return output_prob, class_probs, all_probs