def test_average_pool(self): # TODO: fix this test return device = "CUDA" if not supports_device(device): raise unittest.SkipTest( "Backend doesn't support device {}".format(device)) shape = [1, 1, 40, 40] node_def = helper.make_node("AveragePool", ["X"], ["Y"], kernel_shape=[1, 2], pads=[1, 1], strides=[1, 1]) x = self._get_rnd(shape) output = run_node(node_def, [x], device=device) test_output = np.zeros(shape) for i1 in range(0, shape[0]): for i2 in range(0, shape[1]): for j1 in range(0, shape[2]): for j2 in range(0, shape[3]): test_output[i1][i2][j1][j2] = 0 count = 0 for k in range(j2, min(j2 + 2, shape[3])): test_output[i1][i2][j1][j2] += x[i1][i2][j1][k] count += 1 test_output[i1][i2][j1][j2] /= count np.testing.assert_almost_equal(output["Y"], test_output)
def test_conv_transpose(self): # Fix test in the future. return device = "CUDA" if not supports_device(device): raise unittest.SkipTest( "Backend doesn't support device {}".format(device)) node_def = helper.make_node("ConvTranspose", ["X", "weights"], ["Y"], pads=[1, 1]) x_shape = [1, 5, 4] x = self._get_rnd(x_shape) weight_shape = [5, 3, 2] weights = self._get_rnd(weight_shape) output = run_node(node_def, [x, weights], device=device) out_shape = [x_shape[0], weight_shape[1], x_shape[2]] test_output = np.zeros(out_shape) for b in range(0, x_shape[0]): for m in range(0, weight_shape[1]): for h in range(0, x_shape[2]): v = 0 for c in range(0, x_shape[1]): for k in range(h, min(h + weight_shape[2], x_shape[2])): v += x[b][c][k] * weights[c][m][k - h] test_output[b][m][h] = v np.testing.assert_almost_equal(output["Y"], test_output, decimal=5)
def c_first_cuda_only(cls, darknet_func, inputs, attrs): """ Handle operator that channel first is only supported by CUDA. When using CPU, two transposes should be added. :param darknet_func: Callable Darknet function. :param inputs: Inputs tensor. :param attrs: Attributes. :return: Tensor. """ support_cuda = supports_device("CUDA") if not support_cuda: return cls._tuck_transpose(darknet_func, inputs, attrs) return cls._run_darknet_func(darknet_func, inputs, attrs)
def test_conv(self): device = "CUDA" if not supports_device(device): raise unittest.SkipTest( "Backend doesn't support device {}".format(device)) N, C, H, W = 4, 3, 5, 5 x_shape = [N, C, H, W] K, kH, kW = 6, 3, 3 weight_shape = [K, C, kH, kW] node_def = helper.make_node("Conv", ["X", "weights"], ["Y"], pads=[1, 1, 1, 1], kernel_shape=[kH, kW]) x = self._get_rnd(x_shape) weights = self._get_rnd(weight_shape) output = run_node(node_def, [x, weights], device=device) out_shape = [N, K, H, W] test_output = np.zeros(out_shape) for n in range(N): for c in range(C): for h in range(H): for w in range(W): for k in range(K): for kh in range(kH): for kw in range(kW): h_in_range = (h - kH // 2 + kh) < H and ( h - kH // 2 + kh) >= 0 w_in_range = (w - kW // 2 + kw) < W and ( w - kW // 2 + kw) >= 0 if h_in_range and w_in_range: test_output[n][k][h][w] += ( x[n][c][h - kH // 2 + kh][w - kW // 2 + kw] * weights[k][c][kh][kw]) np.testing.assert_almost_equal(output["Y"], test_output, decimal=5)
def pool(cls, node, input_dict, pool_func, pooling_type, strict=True): x = input_dict[node.inputs[0]] x_rank = len(x.get_shape()) x_shape = x.get_shape().as_list() spatial_size = x_rank - 2 support_cuda = supports_device("CUDA") storage_format, compute_format = get_data_format(x_rank) kernel_shape = node.attrs["kernel_shape"] strides = node.attrs.get("strides", [1] * spatial_size) pads = node.attrs.get("pads", None) pad = PAD_TF_INCOMPATIBLE # from version 7 count_include_pad = node.attrs.get("count_include_pad", 0) # If padding is specified, try to recover it from explicit padding # specification to tensorflow padding mode: if pads is not None: pad = cls._get_tf_pad(x_shape[2:], kernel_shape, strides, pads) else: # Neither pad nor auto_pad is specified, assume no padding. if "auto_pad" not in node.attrs: pad = "VALID" # We consult auto_pad if pad is not specified and auto_pad # is available. else: if node.attrs["auto_pad"] == "SAME_UPPER": pad = "SAME" elif node.attrs["auto_pad"] == "VALID": pad = "VALID" elif node.attrs["auto_pad"] == "SAME_LOWER": pad = PAD_TF_INCOMPATIBLE if count_include_pad == 1: _, pads = cls._pool_get_shapes(node.attrs["auto_pad"], x_shape[2:], kernel_shape, strides, [0] * spatial_size * 2) if pooling_type in ("AVG", "MAX"): if strict and count_include_pad == 0: if pad is PAD_TF_INCOMPATIBLE: return cls._compatibility_pool(node, input_dict, pooling_type) else: if pads != [0] * spatial_size * 2: x = PadMixin.get_padding_as_op(x, pads) pad = "VALID" elif pooling_type == "MAX_WITH_ARGMAX": if pad is PAD_TF_INCOMPATIBLE: exception.OP_UNSUPPORTED_EXCEPT( "MaxPoolWithArgmax with pad is None or incompatible mode", "Tensorflow") if x_rank != 4: exception.OP_UNSUPPORTED_EXCEPT( "MaxPoolWithArgmax with {}D input".format(x_rank), "Tensorflow") if node.attrs.get("storage_order", 0) != 0: exception.OP_UNSUPPORTED_EXCEPT("MaxPoolWithArgmax with column major", "Tensorflow") need_trans = storage_format != "NHWC" if need_trans: x = tf.transpose(x, perm=get_perm_from_formats(storage_format, "NHWC")) pooled, argmax = pool_func( x, [1] + kernel_shape + [1], padding=pad, strides=[1] + strides + [1]) if need_trans: pooled = tf.transpose( pooled, perm=get_perm_from_formats("NHWC", storage_format)) argmax = tf.transpose( argmax, perm=get_perm_from_formats("NHWC", storage_format)) return [pooled, argmax] if support_cuda: pooled = pool_func( x, kernel_shape, padding=pad, strides=strides, data_format=compute_format) else: x = tf.transpose( x, perm=get_perm_from_formats(storage_format, compute_format)) pooled = pool_func( x, kernel_shape, padding=pad, strides=strides, data_format=compute_format) pooled = tf.transpose( pooled, perm=get_perm_from_formats(compute_format, storage_format)) return [pooled]