Categories
Misc

converting kerasTensor to numpy using custom layer and tf.py_function

Hi everybody,

I am newbie to keras and tensorflow. Please i need help to convert feature maps generated by a conv layer to numpy to do some computation and then convert them back to tensor to be fed to next layer in the model.

i believe this is easy to you. here is a dummy sample of code to show the problem :

def convert_to_numpy(tensor): grab_the_new_feature_maps = [] #to grab every feature map feature_maps_arry = tensor.numpy() # convert tensor to array for i in range(feature_maps_arry.shape[2]): single_fm = feature_maps_arry[i] max_value= np.max(single_fm) #find the maximum pixel value in fm min_value= np.min(single_fm) #find the minimum pixel value in fm ########### do the rest of conputations ########## grab_the_new_feature_maps.append(single_fm) back_to_tensor = tf.convert_to_tensor(grab_the_new_feature_maps) return back_to_tensor 

Note the custom layer should not create new layer but use the weights and bias of the received tensor and convert it to numpy, do the computation, and then return the tensor with updated feature maps to the model: My custom layer is as the following

class Mylayer(tf.keras.layers.Layer):def __init__(self): super(Mylayer, self).__init__() def call(self, recieved_tensor, **kwargs): tensor_with_new_fm = tf.py_function(convert_to_numpy(recieved_tensor), [recieved_tensor], 'float32', name='py_function') return tensor_with_new_fm 

model design :

input = Input(shape=(48, 48, 1)) conv1 = Conv2D(32, kernel_size=5, padding='same')(input) test_layer = Mylayer()(conv1) # here i need to convert feature maps of conv1 to numpy conv1 = Flatten()(test_layer) dense1 = Dense(units=12, activation=tf.nn.relu)(conv1) output = Dense(units=10, activation=tf.nn.sigmoid)(dense1) model = Model(inputs=input, outputs=output) print(model.summary()) 

submitted by /u/Ali_Q_Saeed
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *