简体   繁体   中英

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?

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()))
...

预处理图像 (1)

预处理图像 (2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM