【发布时间】:2019-12-27 12:33:35
【问题描述】:
通常我会在将数据输入模型进行分类之前对其进行预处理。
然而这是不可能的,因此要么进一步(以某种方式)增强模型的性能,要么直接在模型中包含有用的预处理步骤。
我该怎么做?到目前为止,我发现的最佳解决方案包括使用 Keras 后端重新实现我想要的功能。这远不是一个好的解决方案,因此我希望有人有一个想法,如何挽救这种情况。
以下是我发现有用的链接 + 我当前的代码。
有用的链接:
Keras Custom Layer with advanced calculations
How to Switch from Keras Tensortype to numpy array for a custom layer?
到目前为止我的代码:
def freezeBaseModelLayers(baseModel):
for layer in baseModel.layers:
layer.trainable = False
def preprocess_input(x):
# TODO: Not working, but intention should be clear
numpy_array = tf.unstack(tf.unstack(tf.unstack(x, 224, 0), 224, 0), 1, 0)
from skimage.feature import hog
from skimage import data, exposure
img_adapteq = exposure.equalize_adapthist(numpy_array, orientations=8, pixels_per_cell=(3, 3),
cells_per_block=(1, 1), visualize=True, multichannel=False)
[x1, x2, x3] = tf.constant(img_adapteq), tf.constant(img_adapteq), tf.constant(img_adapteq)
img_conc = Concatenate([x1, x2, x3])
return img_conc
def create(x):
is_training = tf.get_variable('is_training', (), dtype=tf.bool, trainable=False)
with tf.name_scope('pretrained'):
# Add preprocess step here...
input_layer = Lambda(preprocess_input(x), input_shape=(224, 224, 1), output_shape=(224, 224, 3))
baseModel = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
freezeBaseModelLayers(baseModel)
layer = baseModel(input_layer)
layer = GlobalMaxPooling2D()(layer)
layer = Dense(1024, activation='relu')(layer)
layer = Dense(2, activation=None)(layer)
model = Model(input=input_layer.input, output=layer)
output = model(x)
return output
I would like to include prepocessing steps inside my model
The models I am working with are receiving noisy data. In order to enhance the performance of the models, I would like to do some preprocessing steps e.g. equalize_adapthist.
【问题讨论】:
-
我能想到的最好的方法是在自定义生成器或
keras.utils.Sequence中使用此预处理并使用fit_generator(...workers=many, queue=many2)- 否则只能使用后端重新实现功能。
标签: python numpy tensorflow keras scikit-image