MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _channel_flatten_input

Function _channel_flatten_input

tensorflow/python/ops/parallel_for/pfor.py:1516–1555  ·  view source on GitHub ↗

Merge the stack dimension with the channel dimension. If S is pfor's stacking dimension, then, - for SNCHW, we transpose to NSCHW. If N dimension has size 1, the transpose should be cheap. - for SNHWC, we transpose to NHWCS. We then merge the S and C dimension. Args: x: ops

(x, data_format)

Source from the content-addressed store, hash-verified

1514
1515
1516def _channel_flatten_input(x, data_format):
1517 """Merge the stack dimension with the channel dimension.
1518
1519 If S is pfor's stacking dimension, then,
1520 - for SNCHW, we transpose to NSCHW. If N dimension has size 1, the transpose
1521 should be cheap.
1522 - for SNHWC, we transpose to NHWCS.
1523 We then merge the S and C dimension.
1524
1525 Args:
1526 x: ops.Tensor to transform.
1527 data_format: "NCHW" or "NHWC".
1528
1529 Returns:
1530 A 3-element tuple with the transformed value, along with the shape for
1531 reshape and order for transpose required to transform back.
1532 """
1533
1534 graph = ops.get_default_graph()
1535 cache_key = (graph, x.experimental_ref(), data_format)
1536 if cache_key not in _channel_flatten_input_cache:
1537 x_shape = array_ops.shape(x)
1538 if data_format == b"NCHW":
1539 order = [1, 0, 2, 3, 4]
1540 shape = array_ops.concat([x_shape[1:2], [-1], x_shape[3:]], axis=0)
1541 reverse_order = order
1542 else:
1543 order = [1, 2, 3, 0, 4]
1544 shape = array_ops.concat([x_shape[1:4], [-1]], axis=0)
1545 reverse_order = [3, 0, 1, 2, 4]
1546 # Move S dimension next to C dimension.
1547 x = array_ops.transpose(x, order)
1548 reverse_shape = array_ops.shape(x)
1549 # Reshape to merge the S and C dimension.
1550 x = array_ops.reshape(x, shape)
1551 outputs = x, reverse_order, reverse_shape
1552 _channel_flatten_input_cache[cache_key] = outputs
1553 else:
1554 outputs = _channel_flatten_input_cache[cache_key]
1555 return outputs
1556
1557
1558# Note that with training=True, running FusedBatchNormV3 on individual examples

Calls 5

transposeMethod · 0.80
reshapeMethod · 0.80
experimental_refMethod · 0.45
shapeMethod · 0.45
concatMethod · 0.45

Tested by

no test coverage detected