首页 > 解决方案 > 转化率 神经网络、输出和 y 数据维度

问题描述

我是神经网络的新手(使用 Keras,tf 后端),我正在尝试训练一个网络来发现 200x200 图像中的特定像素。输入数据具有格式为(数量,200,200,1)的 np 数组,因为它们是黑白图像,数组值范围从 -0.5 到 0.5。标记的 y 数据具有相同的格式,每个像素的值为 0 和 1 个像素的值为 1。

导入数据后,如下所示:

输入 输出

我的网络如下所示:

train_images = np.expand_dims(train_images, axis=3)
train_labels = np.expand_dims(train_labels, axis=3)
test_images = np.expand_dims(test_images, axis=3)
test_labels = np.expand_dims(test_labels, axis=3)

print(train_images.shape) # (example, 200, 200, 1)
print(train_labels.shape) # (example,  200, 200, 1) 
print(test_images.shape)
print(test_labels.shape)

num_filters = 16
filter_size = 5
filter_size2 =3
pool_size = 2
input_shape=(200,200,1)
output_shape = input_shape

inputs = tf.keras.Input(shape=input_shape)
x= tf.keras.layers.Conv2D(num_filters, filter_size, activation=tf.nn.relu)input_shape=input_shape, padding='same')(inputs)
x= tf.keras.layers.Conv2D(num_filters, filter_size2, padding='same')(x)
x= tf.keras.layers.MaxPooling2D(pool_size=pool_size)(x)
x= tf.keras.layers.Dropout(0.25)(x)
x= tf.keras.layers.Flatten()(x)
x= tf.keras.layers.Dense(64, activation=tf.nn.relu)(x)
x= tf.keras.layers.Dropout(0.5)(x)
x= tf.keras.layers.Dense(1024, activation=tf.nn.relu)(x)
outputs = tf.keras.layers.Dense(output_shape, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)

model.summary()

model.compile('Adam', loss='categorical_crossentropy',metrics=['accuracy'],)

history = model.fit(train_images, to_categorical(train_labels), epochs=3,
          validation_data=(test_images, to_categorical(test_labels)))

当我将图层展平以使用 Dense 图层时,我希望 softmax 计算每个像素的概率,但 Dense 似乎只采用一维数字而没有数组格式的元组。我还考虑将网络更改为 2 个节点(x 和 y 坐标)的输出,但是当我上传具有 2 个坐标的数据时,它会再次具有格式(数量,1,1)。所以输出维度不适合 y 数据维度。

对于如何尝试训练网络以找到搜索的像素的最佳方式,我会很高兴提供任何帮助。

标签: pythontensorflowkerasneural-network

解决方案


来自评论

您使用的架构是Image Classification网络预测图像属于哪个类。但是,当您想预测图像的一部分时,您的图像是Object Detection problem. 有关更多详细信息,请参阅Tensorflow 教程或Github 链接(转述自 Tensorflow 支持)


推荐阅读