I need some help to understand what's going on here.
My goal is to have a network that receives sizeXsize
images and returns sizeXsize
binary matrices. The output of the network should be a binary sizeXsize
matrix that indicates if a pixel has a feature or not.
For example, think of a corner detection network where the output layer tells if a pixel is exactly a tip of the corner. Namely, we want to detect only the pixel of this corner:
The first layers in the networks are defined as follows:
from keras import models, layers
import numpy as np
size=5
input_image = layers.Input(shape=(size, size, 1))
b = layers.Conv2D(5, (3,3), activation='relu', padding='same')(input_image)
b = layers.MaxPooling2D((2,2), strides=1, padding='same')(b)
b = layers.Conv2D(5, (3,3), activation='relu', padding='same')(b)
b_out = layers.MaxPooling2D((2,2),strides=1 ,padding='same')(b)
Until now I maintained the dimensions of the original input layer (sizeXsize
).
Now I would like to have a dense layer as an output layer with sizeXsize
pixels.
If I use output = layers.Dense(size, activation='sigmoid')(b_out)
the layer built is sizeXsizeXsize
, and if I do output = layers.Dense(1, activation='sigmoid')(b_out)
the size is sizeXsize
, how comes?!
This is the building and the compilation part of the code:
model = models.Model(input_image, output)
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
What do I miss here? Isn't output = layers.Dense(1, activation='sigmoid')(b_out)
just a single neuron?
The thing is that if I train:
n_images=100
data = np.random.randint(0,2,(n_images,size,size,1))
labels = np.random.randint(0,2,(n_images,size,size,1))
labels = data
model.fit(data, labels, verbose=1, batch_size=4, epochs=20)
and if I test it:
data1 = np.random.randint(0,2,(n_images,size,size,1))
score, acc = model.evaluate(data1,data1, verbose=1)
print('Test score:', score)
print('Test accuracy:', acc)
a=np.random.randint(0,2,(1,size,size,1))
prediction = model.predict(a)
print(a==np.round(prediction))
I get a good accuracy, and it seems the sizes are correct for the output layer:
100/100 [==============================] - 0s 349us/step
Test score: 0.187119951248
Test accuracy: 0.926799981594
[[[[ True]
[ True]
[ True]
[ True]
[ True]]
[[ True]
[ True]
[ True]
[ True]
[ True]]
[[ True]
[ True]
[ True]
[ True]
[ True]]
[[ True]
[ True]
[ True]
[ True]
[ True]]
[[ True]
[ True]
[ True]
[ True]
[ True]]]]
If I read Dense documentation:
units: Positive integer, dimensionality of the output space.
So how comes if I put layers.Dense(1, activation='sigmoid')(b_out)
I get an output layer of sizeXsize
?