'In keras/ tensorflow, Is there a way to add a preprocessing layer to the output, similar to TargetTransformRegressor in sklearn?
I want to use keras to build a neural network regression model from X_train -> Y_train
. In this example, however, I need to perform a preprocessing transform on both the input AND the output. So the model needs to be
X_train ->(preproc) X_hat ->(model) -> Y_hat <-(preproc) Y_train
The preprocessing on the input is straightforward using keras' preprocessing layers, but it doesn't apply to the output. I can do this in sklearn using the TargetTransformRegressor, but I am not aware of anything similar in keras. My best guess is that I will need to create a custom model that performs this preprocessing, fitting and prediction. Any ideas?
Solution 1:[1]
Do you mean something like this? You can applied image pre-process array such Dense, Conv, Normalize and etc. if you want. This is sample but applied for entire image or partial .
Sample:
y1 = tf.keras.layers.Cropping2D(cropping=((30, pic_height - box_height - 30), (5, ( pic_width - box_width - 5 ) )))(input)
y1 = tf.keras.preprocessing.image.array_to_img( 255 * np.reshape(y1, (26, 15, 3)), data_format=None, scale=True )
previous = tf.keras.layers.Cropping2D(cropping=((30, pic_height - box_height - 30), (start, pic_width - box_width - start)))(input)
previous = tf.keras.preprocessing.image.array_to_img( 255 * np.reshape(previous, (26, 15, 3)), data_format=None, scale=True
temp_3 = tf.where(tf.math.greater( np.asarray(y1, dtype=np.float32), np.asarray(previous, dtype=np.float32)), [1.0], [0.0]).numpy()
temp_3 = tf.math.multiply( temp_3, y1, name=None )
temp_3 = tf.keras.preprocessing.image.array_to_img( 255 * np.reshape(temp_3, (26, 15, 3)), data_format=None, scale=True )
...
output:
plt.subplot(A,B,C)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(temp_3)
plt.xlabel(str(i + 1) + ": " + str(tf.reduce_mean(np.asarray(temp_3)).numpy()))
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Martijn Pieters |