Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a 1D Convolutional Auto-encoder in Keras for vector data?

My input vector to the auto-encoder is of size 128. I have 730 samples in total (730x128). I am trying to use a 1D CNN auto-encoder. I would like to use the hidden layer as my new lower dimensional representation later. My code right now runs, but my decoded output is not even close to the original input. Here is the code:

input_sig = Input(batch_shape=(None,128,1))
x = Conv1D(64,3, activation='relu', padding='valid')(input_sig)
x1 = MaxPooling1D(2)(x)
x2 = Conv1D(32,3, activation='relu', padding='valid')(x1)
x3 = MaxPooling1D(2)(x2)
flat = Flatten()(x3)
encoded = Dense(32,activation = 'relu')(flat)

print("shape of encoded {}".format(K.int_shape(flat)))

x2_ = Conv1D(32, 3, activation='relu', padding='valid')(x3)
x1_ = UpSampling1D(2)(x2_)
x_ = Conv1D(64, 3, activation='relu', padding='valid')(x1_)
upsamp = UpSampling1D(2)(x_)
flat = Flatten()(upsamp)
decoded = Dense(128,activation = 'relu')(flat)
decoded = Reshape((128,1))(decoded)

print("shape of decoded {}".format(K.int_shape(x1_)))

autoencoder = Model(input_sig, decoded)
autoencoder.compile(optimizer='adam', loss='mse', metrics=['accuracy'])

The input to the autoencoder is then --> (730,128,1) But when I plot the original signal against the decoded, they are very different!! Appreciate your help on this.

like image 236
Mohammad Riazi Avatar asked Sep 02 '25 06:09

Mohammad Riazi


1 Answers

You need to have a single channel convolution layer with "sigmoid" activation to reconstruct the decoded image. Take a look at the example below. You can compile it with the loss='mse' and optimizer='adam'

input_sig = Input(batch_shape=(1,128,1))
x = Conv1D(8,3, activation='relu', padding='same',dilation_rate=2)(input_sig)
x1 = MaxPooling1D(2)(x)
x2 = Conv1D(4,3, activation='relu', padding='same',dilation_rate=2)(x1)
x3 = MaxPooling1D(2)(x2)
x4 = AveragePooling1D()(x3)
flat = Flatten()(x4)
encoded = Dense(2)(flat)
d1 = Dense(64)(encoded)
d2 = Reshape((16,4))(d1)
d3 = Conv1D(4,1,strides=1, activation='relu', padding='same')(d2)
d4 = UpSampling1D(2)(d3)
d5 = Conv1D(8,1,strides=1, activation='relu', padding='same')(d4)
d6 = UpSampling1D(2)(d5)
d7 = UpSampling1D(2)(d6)
decoded = Conv1D(1,1,strides=1, activation='sigmoid', padding='same')(d7)
model= Model(input_sig, decoded)
like image 71
Nehad Hirmiz Avatar answered Sep 05 '25 01:09

Nehad Hirmiz