I'm trying to make a denoise autoencoder wherein the encoder part is vgg16 and decoder is opposite of vgg16(encoder) network. My dataset consists of 5K images in grayscale and these are the steps i've followed to prepare and normalize:
input_X = [], (list having 5K (noisy)images of dims 224,224,1)
input_Y = [], (list having 5K (ground truth)images of dims 224,224,1)
test_X = [] (list having 1K (noisy)images for testing of dims 224,224,1)
input_X = input_X/255.0
input_Y = input_Y/255.0
test_X = test_X/255.0
print(input_X.shape)
print(input_Y.shape)
print(test_X.shape)
input_X = np.repeat(input_X[..., np.newaxis], 3, -1) #creating 3 channels
input_Y = np.repeat(input_Y[..., np.newaxis], 3, -1)
test_X = np.repeat(test_X[..., np.newaxis], 3, -1)
print(input_X.shape) (5000,224,224,3)
print(input_Y.shape)
print(test_X.shape)
encoder network:
vggmodel = keras.applications.vgg16.VGG16()
model_encoder = Sequential()
num = 0
for i, layer in enumerate(vggmodel.layers):
if i<19:
model_encoder.add(layer)
model_encoder.summary()
for layer in model_encoder.layers:
layer.trainable=False
output:
Metal device set to: Apple M1 Pro
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
decoder network:
encoder_output = Input(shape=(7, 7, 512,))
x = Conv2D(512, (3, 3), activation='relu', padding='same')(encoder_output)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2,2))(x)
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2,2))(x)
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2,2))(x)
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2,2))(x)
# Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
x = UpSampling2D((2, 2))(x)
model_decoder = Model(inputs=encoder_output, outputs=x)
training:
model_decoder.compile(optimizer='Adam', loss='mean_squared_error' , metrics=['accuracy'])
model_decoder.fit(trainx_encoded, train_Y,
epochs=no_epocs,
batch_size=batch_size,
validation_split=validation_split)
Now while training, the loss and accuracy doesn't changes. I can think of reducing filters in the initial decoder layers but i fear that's going to affect the autoencoder.
Here, i'm really clueless about what approach to follow.
training output:
Epoch 1/50
2022-04-27 22:10:05.336322: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:113] Plugin optimizer for device_type GPU is enabled.
27/27 [==============================] - ETA: 0s - loss: 0.1678 - accuracy: 0.9689
2022-04-27 22:11:34.044172: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:113] Plugin optimizer for device_type GPU is enabled.
27/27 [==============================] - 97s 4s/step - loss: 0.1678 - accuracy: 0.9689 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 2/50
27/27 [==============================] - 87s 3s/step - loss: 0.1645 - accuracy: 1.0000 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 3/50
27/27 [==============================] - 86s 3s/step - loss: 0.1645 - accuracy: 1.0000 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 4/50
27/27 [==============================] - 86s 3s/step - loss: 0.1645 - accuracy: 1.0000 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 5/50
27/27 [==============================] - 91s 3s/step - loss: 0.1645 - accuracy: 1.0000 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 6/50
27/27 [==============================] - 85s 3s/step - loss: 0.1645 - accuracy: 1.0000 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 7/50
27/27 [==============================] - 87s 3s/step - loss: 0.1645 - accuracy: 1.0000 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 8/50
27/27 [==============================] - 91s 3s/step - loss: 0.1645 - accuracy: 1.0000 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 9/50
27/27 [==============================] - 85s 3s/step - loss: 0.1645 - accuracy: 1.0000 - val_loss: 0.1732 - val_accuracy: 1.0000
Epoch 10/50
27/27 [==============================] - ETA: 0s - loss: 0.1645 - accuracy: 1.0000
VGG16 is not trained to be used as an encoder for image reconstruction, it is trained to extract features from an image using which we can do classification task on the image.
This is why, you cannot use VGG16 as the encoder part of your denoise autoencoder.
However, if you want, you can use that architecture of VGG16 as the encoder part of your autoencoder, by just retraining those layers of VGG16,
vggmodel = keras.applications.vgg16.VGG16()
model_encoder = Sequential()
num = 0
for i, layer in enumerate(vggmodel.layers):
if i<19:
model_encoder.add(layer)
model_encoder.summary()
for layer in model_encoder.layers:
layer.trainable=True # Set encoder to trainable, and your autoencoder should work.
Again, there is a problem with your current architecture of autoencoder -it downsamples too much, leading to a significant loss of data. A smaller architecture would work better. For example:
model = Sequential()
model.add(Input(shape=(224,224,3)))
model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
model.add(MaxPool2D((2,2)))
model.add(Conv2D(128, (3,3), activation='relu', padding='same'))
model.add(Conv2D(128, (3,3), activation='relu', padding='same'))
model.add(MaxPool2D((2,2)))
model.add(Conv2D(256, (3,3), activation='relu', padding='same'))
model.add(Conv2D(256, (3,3), activation='relu', padding='same'))
model.add(Conv2D(256, (3,3), activation='relu', padding='same'))
model.add(UpSampling2D((2,2)))
model.add(Conv2D(128, (3,3), activation='relu', padding='same'))
model.add(Conv2D(128, (3,3), activation='relu', padding='same'))
model.add(UpSampling2D((2,2)))
model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
model.add(Conv2D(3, (3,3), activation='relu', padding='same'))
Related
I'm currently learning about gensim word2vec, and in the middle of making a model that can predict what the rating would be based on the words. All of my code compiles fine, however each epoch posses the same val_accuracy, and almost the exact same training accuracy. I've tried changing the architecture and hyperparameters, but have had no luck whatsoever. Any advice would be appreciated!
from keras.preprocessing.sequence import pad_sequences
word_index = {w: i+1 for i,w in enumerate(index_to_key) if i < max_words-1} # Keep just max_words (zero is reserved for unknown)
sequences = [[word_index.get(w, 0) for w in sent] for sent in reviews] # code the sentences
seqs_truncated = pad_sequences(sequences, maxlen=max_review_length, padding="pre", truncating="post")
ratings = np.asarray(Ratings)
# prepare training and validation data
x_val = seqs_truncated[:len_val]
partial_x_train = seqs_truncated[len_val:]
y_val = to_categorical(ratings[:len_val]-1, num_classes=5)
partial_y_train = to_categorical(ratings[len_val:]-1, num_classes=5)
print('Length of validation set =', len(x_val))
print('Length of training set =', len(partial_x_train))
def sample_network(embedding):
network = Sequential()
network.add(embedding)
network.add(Bidirectional(LSTM(64)))
network.add(Dense(64, activation='relu'))
network.add(Dense(32, activation="relu"))
network.add(Dense(16, activation="relu"))
network.add(Dropout(0.5))
network.add(Dense(5,activation='softmax'))
return network```
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_24 (Embedding) (None, 100, 300) 1500000
_________________________________________________________________
bidirectional_5 (Bidirection (None, 128) 186880
_________________________________________________________________
dense_16 (Dense) (None, 64) 8256
_________________________________________________________________
dense_17 (Dense) (None, 32) 2080
_________________________________________________________________
dense_18 (Dense) (None, 16) 528
_________________________________________________________________
dropout_10 (Dropout) (None, 16) 0
_________________________________________________________________
dense_19 (Dense) (None, 5) 85
=================================================================
Total params: 1,697,829
Trainable params: 1,697,829
Non-trainable params: 0 ```
hist_word_vec =network_wordvec.fit(partial_x_train,partial_y_train, epochs=no_epochs,
batch_size=256, validation_data=(x_val,y_val)
65/65 [==============================] - 39s 606ms/step - loss: 1.5886 - accuracy: 0.4449 - val_loss: 1.5750 - val_accuracy: 0.3875
Epoch 2/8
65/65 [==============================] - 38s 587ms/step - loss: 1.5491 - accuracy: 0.4554 - val_loss: 1.5458 - val_accuracy: 0.3875
Epoch 3/8
65/65 [==============================] - 37s 564ms/step - loss: 1.5152 - accuracy: 0.4554 - val_loss: 1.5215 - val_accuracy: 0.3875
this is my code :
number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
absPath='F:/Projects/AI/Tensorflow/verificationcode/image/'
imagePaths=os.listdir('./image')
model=tf.keras.models.Sequential([
Conv2D(32,kernel_size=3, activation='relu'),
Conv2D(32,kernel_size=3, activation='relu'),
MaxPool2D((2, 2)),
Conv2D(64, kernel_size=3, activation='relu'),
Conv2D(64, kernel_size=3, activation='relu'),
MaxPool2D((2, 2)),
Conv2D(128, kernel_size=3, activation='relu'),
Conv2D(128, kernel_size=3, activation='relu'),
MaxPool2D((2, 2)),
Conv2D(256, kernel_size=3, activation='relu'),
Conv2D(256, kernel_size=3, activation='relu'),
MaxPool2D((2, 2)),
Flatten(),
Dropout(0.25),
Dense(40,activation='softmax')
])
model(inputs=tf.keras.Input(shape=(80,170,3)))
model.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
history = model.fit(x_train, y_train, batch_size=32,shuffle=True, epochs=5, validation_freq=1)
this is my model:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 78, 168, 32) 896
_________________________________________________________________
conv2d_1 (Conv2D) (None, 76, 166, 32) 9248
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 38, 83, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 36, 81, 64) 18496
_________________________________________________________________
conv2d_3 (Conv2D) (None, 34, 79, 64) 36928
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 17, 39, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 15, 37, 128) 73856
_________________________________________________________________
conv2d_5 (Conv2D) (None, 13, 35, 128) 147584
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 6, 17, 128) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 4, 15, 256) 295168
_________________________________________________________________
conv2d_7 (Conv2D) (None, 2, 13, 256) 590080
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 1, 6, 256) 0
_________________________________________________________________
flatten (Flatten) (None, 1536) 0
_________________________________________________________________
dropout (Dropout) (None, 1536) 0
_________________________________________________________________
dense (Dense) (None, 40) 61480
=================================================================
Total params: 1,233,736
Trainable params: 1,233,736
Non-trainable params: 0
I use 3935 samples to train,it is result:
3616/3935 [==========================>...] - ETA: 10s - loss: 14.7558 - accuracy: 0.0105
3648/3935 [==========================>...] - ETA: 9s - loss: 14.7558 - accuracy: 0.0112
3680/3935 [===========================>..] - ETA: 8s - loss: 14.7558 - accuracy: 0.0128
3712/3935 [===========================>..] - ETA: 7s - loss: 14.7558 - accuracy: 0.0129
3744/3935 [===========================>..] - ETA: 6s - loss: 14.7558 - accuracy: 0.0134
3776/3935 [===========================>..] - ETA: 5s - loss: 14.7558 - accuracy: 0.0143
3808/3935 [============================>.] - ETA: 4s - loss: 14.7558 - accuracy: 0.0142
3840/3935 [============================>.] - ETA: 3s - loss: 14.7558 - accuracy: 0.0148
3872/3935 [============================>.] - ETA: 2s - loss: 14.7558 - accuracy: 0.0155
3904/3935 [============================>.] - ETA: 1s - loss: 14.7558 - accuracy: 0.0166
3935/3935 [==============================] - 135s 34ms/sample - loss: 14.7558 - accuracy: 0.0170
this is a captcha image:
captcha image
the loss unchanged, the accuracy is very low
How to solve it ?? thanks!
I build a CNN based on the Chest X-Ray Images (Pneumonia) dataset and for some reason when I train the model I get the same accuracy and val_accuracy over epochs.
train_ds = ImageDataGenerator()
traindata = train_ds.flow_from_directory(directory="../input/chest-xray-pneumonia/chest_xray/train",target_size=(IMG_HEIGHT,IMG_WIDTH),shuffle=True)
// Found 5216 images belonging to 2 classes.
test_ds = ImageDataGenerator()
testdata = test_ds.flow_from_directory(directory="../input/chest-xray-pneumonia/chest_xray/test",target_size=(IMG_HEIGHT,IMG_WIDTH),shuffle=True)
//Found 624 images belonging to 2 classes.
model = keras.Sequential([
keras.layers.Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding="same", activation="relu"),
keras.layers.Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2)),
keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2)),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2)),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2)),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2)),
keras.layers.Flatten(),
keras.layers.Dense(units=4096,activation="relu"),
# keras.layers.Dropout(.5),
keras.layers.Dense(units=4096,activation="relu"),
# keras.layers.Dropout(.5),
keras.layers.Dense(units=2, activation="softmax"),
])
opt = keras.optimizers.Adam(lr=0.001)
model.compile(optimizer=opt,
loss="categorical_crossentropy",
metrics=['accuracy'])
logdir = "logs\\training\\" + datetime.now().strftime("%Y%m%d-%H%M%S")
checkpoint = keras.callbacks.ModelCheckpoint("vgg16_1.h5", verbose=1, monitor='val_accuracy', save_best_only=True, mode='auto')
early = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)
hist = model.fit(traindata,
steps_per_epoch=STEPS_PER_EPOCH,
epochs=100,
validation_data=testdata,
validation_steps=VALIDATION_STEPS,
callbacks=[early, tensorboard_callback])
Epoch 1/100
163/163 [==============================] - 172s 1s/step - loss: 62.6885 - accuracy: 0.7375 - val_loss: 0.6827 - val_accuracy: 0.6250
Epoch 2/100
163/163 [==============================] - 157s 961ms/step - loss: 0.5720 - accuracy: 0.7429 - val_loss: 0.7133 - val_accuracy: 0.6250
Epoch 3/100
163/163 [==============================] - 159s 975ms/step - loss: 0.5725 - accuracy: 0.7429 - val_loss: 0.6691 - val_accuracy: 0.6250
Epoch 4/100
163/163 [==============================] - 159s 973ms/step - loss: 0.5721 - accuracy: 0.7429 - val_loss: 0.7036 - val_accuracy: 0.6250
Epoch 5/100
163/163 [==============================] - 158s 971ms/step - loss: 0.5715 - accuracy: 0.7429 - val_loss: 0.7169 - val_accuracy: 0.6250
Epoch 6/100
163/163 [==============================] - 160s 983ms/step - loss: 0.5718 - accuracy: 0.7429 - val_loss: 0.6982 - val_accuracy: 0.6250
I've tried changing the activation function for the last layer, adding dropout layers and toyed around with the number of neurons but nothing seemed to work. does anyone have an ideas what causes this strange behaviour?
You only have a small dataset. From the looks of your training loss, I would suppose that your network actually already converged after 1 epoch. (with a substantial amount of overfit)
For the amount of data you have, I would suggest trying a way smaller network, or work with data augmentation techniques to regularize your model.
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
from keras.datasets import mnist
from keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
I am getting an error:
ValueError: Input 0 of layer sequential_22 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [28, 28, 1]
I tried using the Keras 2.8, fashion_mnist dataset and it is working.
import keras
print(keras.__version__)
from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
from keras.datasets import fashion_mnist
from tensorflow.keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)
Output:
2.8.0
Epoch 1/5
938/938 [==============================] - 53s 55ms/step - loss: 0.5523 - accuracy: 0.7965
Epoch 2/5
938/938 [==============================] - 51s 55ms/step - loss: 0.3333 - accuracy: 0.8780
Epoch 3/5
938/938 [==============================] - 54s 58ms/step - loss: 0.2832 - accuracy: 0.8968
Epoch 4/5
938/938 [==============================] - 52s 55ms/step - loss: 0.2508 - accuracy: 0.9079
Epoch 5/5
938/938 [==============================] - 51s 55ms/step - loss: 0.2264 - accuracy: 0.9168
<keras.callbacks.History at 0x7f1c1b0a5390>
we have a few problems with our val_loss and val_acc. After a few epochs (around 30) the val_acc is going down at around 50-60% and the val_loss is increasing to between 0.98 - 1.4 (see the pictures below). At the end of the post is the end of the 45th epoch.
[
import pickle
from datetime import time
import matplotlib.pyplot as plt
import numpy as np
import tf as tf
from keras import optimizers
from keras.models import Sequential
from keras.layers import *
from keras.callbacks import TensorBoard
from keras.utils import np_utils
pickle_in = open("X.pickle", "rb")
X = pickle.load(pickle_in)
pickle_in = open("y.pickle", "rb")
y = pickle.load(pickle_in)
pickle_in = open("PredictionData\\X_Test.pickle", "rb")
X_Test = pickle.load(pickle_in)
X = X/255.0
X_Test = X_Test/255.0
y = np_utils.to_categorical(y, 5)
NAME = "Emotion Detection"
model = Sequential()
model.add(Conv2D(32, (1, 1), activation="relu", use_bias=True,
bias_initializer="Ones",
input_shape=(145, 65, 1),
dim_ordering="th"))
model.add(Conv2D(64, (3, 3),
activation="relu"))
model.add(Conv2D(128, (3, 3),
activation="relu"))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3),
activation="relu"))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(128,
activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(32,
activation="relu"))
model.add(Dense(5,
activation='sigmoid'))
tensorboard = TensorBoard(log_dir="Tensorboard\\".format(time))
sgd = optimizers.SGD(lr=0.001, decay=1e-6,
momentum=0.9, nesterov=True)
model.compile(loss="categorical_crossentropy",
optimizer=sgd,
metrics=['accuracy'])
history = model.fit(X, y, batch_size=16,
epochs=45, validation_split=0.12,
callbacks=[tensorboard])
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Accuracy', 'Val_Accuracy'], loc='upper left')
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Loss', 'Val_Loss'], loc='upper left')
plt.show()
classes = model.predict(X_Test)
plt.bar(range(5), classes[0])
plt.show()
print("prediction: class", np.argmax(classes[0]))
model.summary()
model.save("TrainedModel\\emotionDetector.h5")
2493/2493 [==============================] - 35s 14ms/step - loss: 0.2324 - accuracy: 0.9202 - val_loss: 1.3789 - val_accuracy: 0.6353
_________________________________________________________________
Layer (type) Output Shape Param
=================================================================
conv2d_1 (Conv2D) (None, 32, 65, 1) 4672
_________________________________________________________________
conv2d_2 (Conv2D) (None, 30, 63, 64) 640
_________________________________________________________________
conv2d_3 (Conv2D) (None, 28, 61, 128) 73856
_________________________________________________________________
dropout_1 (Dropout) (None, 28, 61, 128) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 26, 59, 64) 73792
_________________________________________________________________
flatten_1 (Flatten) (None, 98176) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 12566656
_________________________________________________________________
dropout_2 (Dropout) (None, 128) 0
_________________________________________________________________
dense_2 (Dense) (None, 32) 4128
_________________________________________________________________
dense_3 (Dense) (None, 5) 165
_________________________________________________________________
Total params: 12,723,909
Trainable params: 12,723,909
Non-trainable params: 0
_________________________________________________________________
Hopefully you can help us. Thanks in advance.
These plots are a classic example of overfitting. I recommend watching it https://en.wikipedia.org/wiki/Overfitting