tensorflowimage-processingmodelcustomizationlayer# Custom layer using tensorflow

I am trying to make customized layer which reduces channels by reduce_sum(axis=-1)

For example when input shape is (32,32,128)

I want to change the input shape from (32,32,128) to (None,32,32,128) and

And if the channels have index which will be like [0],[1],[2],[3]………[126],[127] And my customized layer want to do is adding 2or 3 or 4…N channels

Lets say if i want to add only 2 channels Whoch will be [0]+[1],[1]+[2]….[126]+[127] And the output shape will be (32,32,64)

also (None,32,32,64)

For more details lets say i want to add 3 channels which will be [0]+[1]+[2],[3]+[4]+[4]… … [123]+[124]+[125],[126]+[127] And the output shape will be (32,32,44)

Here too (None,32,32,44)

So is it possible to make it?

Is there index in channels? If so it would be kinda easy to make it I think…

Solution

```
x = tf.random.normal(shape=(32,32,128))
sum_dim = 2
x = tf.reduce_sum(tf.reshape(x, (x.shape[0], x.shape[1], -1, sum_dim)), -1)
#[32, 32, 64]
```

If you want to write the keras layer:

```
class ChannelSum(keras.layers.Layer):
def __init__(self, sum_dim=2):
super(ChannelSum, self).__init__()
self.sum_dim = sum_dim
def call(self, x ):
return tf.reduce_sum(tf.reshape(x, (-1, x.shape[1], x.shape[2], x.shape[3]//self.sum_dim, self.sum_dim)), -1)
```

- Install an older version of Tensorflow GPU
- How do I build a permutation invariance neural network in keras?
- Why does keras model predict slower after compile?
- Attribute Error: 'Embedding' object has no attribute 'embeddings' - TensorFlow & Keras
- ValueError: setting an array element with a sequence in Python numpy array
- TensorFlow libdevice not found. Why is it not found in the searched path?
- Keras: Accuracy Drops While Finetuning Inception
- What is the logic of the extra columns in Tensorflow categorical encoding?
- Categorical Variables in TensorFlow
- Tensorflow object_detection correct way to save and load fine tune model
- Can't install tensorflow in Docker on Silicon mac
- Multiple outputs in Keras
- Model trained on google colab not saving to my drive
- KerasTuner: Custom Metrics (e.g., F1 Score, AUC) in Objective with RandomSearch Error
- Tensorflow 2.0 & Java API
- Tensorflow: how to minimize under constraints
- Unable to install Tensorflow on Anaconda Environment
- Error while import tensorflow module
- Keras Tensorflow 'Cannot apply softmax to a tensor that is 1D'
- Tensor flow Getting started
- How to install just XLA?
- tensorflow output layer configuration for classification task
- i have problem in my end point for saved ai model
- DataType 20 is not recognized in Java (version 1.15.0)
- Error when calling the method timeseries_dataset_from_array from Keras
- Pandas Dataframe to sliding window
- How to get PI in tensorflow?
- model prediction using CNN
- Cuda 12 + tf-nightly 2.12: Could not find cuda drivers on your machine, GPU will not be used, while every checking is fine and in torch it works
- Why my LSTM Model forecasts almost straight line on validation set?