【发布时间】:2020-11-06 02:25:37
【问题描述】:
我是神经网络的新手(使用 Keras、tf 后端),我正在尝试训练一个网络来发现 200x200 图像中的特定像素。输入数据具有格式为(数量,200,200,1)的 np 数组,因为它们是黑白图像,数组值范围从 -0.5 到 0.5。标记的 y 数据具有相同的格式,每个像素的值为 0,每个像素的值为 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 数据维度。
我很乐意提供任何帮助,了解如何以最佳方式尝试和训练网络以找到搜索的像素。
【问题讨论】:
-
您使用的
architecture用于Image Classification,即Network预测Class属于哪个Image。但是,当您想预测图像的一部分时,您的问题是 Object Detection 问题。请参考此Tensorflow Tutorial(tensorflow.org/hub/tutorials/object_detection) 或此 Github 链接 (github.com/tensorflow/models/tree/master/research/…) 了解更多详情。谢谢!
标签: python tensorflow keras neural-network