i tried following code for facial emotion recognition using deep learning based model LSTM but its not work.using python 3.7 anaconda3. this code works on CNN model.
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('D:\\Datasets\\gender CK+\\training_set',
target_size = (64, 64), batch_size = 32, class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('D:\\Datasets\\gender CK+\\test_set',
target_size = (64, 64), batch_size = 32, class_mode = 'categorical')
seq = Sequential()
seq.add(ConvLSTM2D(filters=32, kernel_size=(3, 3),input_shape=(None, 64, 64, 40),
padding='same', return_sequences=True))seq.add(Batch Normalization())
seq.add(Dense(2,activation="sigmoid"))
seq.compile(loss='binary_crossentropy', optimizer='rmsprop',metrics=['accuracy'])
seq.summary()
seq.fit_generator(training_set, steps_per_epoch = 100, epochs = 4, validation_data = test_set, validation_steps = 200)
following error occurs
ValueError: Error when checking input: expected conv_lst_m2d_4_input
to have 5 dimensions, but got array with shape (5, 64, 64, 3)
Related
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
'animals/training_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')
test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
'animals/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')
cnn = tf.keras.models.Sequential()
cnn.add(tf.keras.layers.Conv2D(filters = 32, kernel_size = 2, activation = 'relu', input_shape = [64,
64, 3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size = 2, strides = 2))
cnn.add(tf.keras.layers.Conv2D(filters = 32, kernel_size = 2, activation = 'relu', input_shape = [64,
64, 3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size = 2, strides = 2))
cnn.add(tf.keras.layers.Dense(units = 128, activation = 'relu'))
cnn.add(tf.keras.layers.Dense(units = 3, activation = 'softmax'))
cnn.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
cnn.fit(x = training_set, validatian_data = test_set, epochs = 15)
The Following error pops out:
ValueError: A target array with shape (32, 3) was passed for an output of shape (None, 15, 15, 3) while using as loss categorical_crossentropy. This loss expects targets to have the same shape as the output.
You have to add a tf.keras.layers.Flatten layer after your last Maxpool2D in order to use the Dense layer on 1D data. Else, the Dense layers apply to 2D data which causes the mismatch.
In the second cnn.add(tf.keras.layers.Conv2D()) function you must not pass the input shape. Input shapes are passed only for the first layer.
enter code here
classifier = Sequential()
classifier.add(Convolution2D(32, kernel_size=3, input_shape = (50, 50 , 1), activation =
'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Convolution2D(32, kernel_size=3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Dropout(0.35))
classifier.add(Flatten())
classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dropout(0.04))
classifier.add(Dense(1, activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
validation_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('/...',
target_size = (50, 50),
batch_size = 32,
class_mode = 'binary')
validation_set = validation_datagen.flow_from_directory('/…..',
target_size = (50, 50),
batch_size = 32,
class_mode = 'binary')
history=classifier.fit_generator(training_set,
samples_per_epoch = 5187,
nb_epoch = 25,
validation_data = validation_set,
nb_val_samples = 1287)
This is the simple cnn architecture I have made. The image I have used is grey scale .
If I specify channel value as 1 specified in bold
classifier.add(Convolution2D(32, kernel_size=3, input_shape = (50, 50 , 1), activation = 'relu'))
Im getting error as
Error when checking input: expected conv2d_1_input to have shape (50, 50, 1) but got array with shape (50, 50, 3)
But if I use filter size as 3 I'm not getting any error but it might be a logical error of using 3 channel for grey scale images...Please clarify on this
flow_from_directory takes a color_mode parameter that specifies the number of channels the loaded images have. If you want to use grayscale images, you need to specify it (it defaults to 'rgb'):
train_datagen.flow_from_directory('/...',
color_mode='grayscale', #<<<<<<<<<<<<<<<<<<<<<
target_size = (50, 50),
batch_size = 32,
class_mode = 'binary')
I am training a Keras model for multi-label image classification, i.e. 3 classes namely flood, wildfire, storm.
But I am getting only [[1.]] instead of something like [0 0 1]. So if third bit is one, its a storm. But I don't know why it's returning just a single value [[1.]].
# # Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
import numpy as np
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def create_model() :
# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
return classifier
def train_save_model():
classifier = create_model()
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('validation_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 1407,
epochs = 1,
validation_data = test_set,
validation_steps = 100)
classifier.save_weights("model.h5")
# Part 3 - Making new predictions
def test_model():
classifier = create_model()
classifier.load_weights("model.h5")
test_image = image.load_img('validation_set/tornado/110.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
# print(test_image)
result = classifier.predict(test_image)
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
)
training_set = train_datagen.flow_from_directory('training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
training_set.class_indices
# print(training_set.class_indices)
print(result)
train_save_model()
test_model()
result = classifier.predict(test_image)
I tried printing this result variable and I get [[1.]]. I cannot understand at all how's that happening.
If you have N labels, then the last layer (i.e. the sigmoid classifier layer) must also have N neurons, one for each of the classes:
classifier.add(Dense(units=3, activation='sigmoid'))
Then the output of the model, for each input sample, would be 3 numbers corresponding to three labels.
Update: Remove the class_mode = 'binary' from all flow_from_directory calls. That's because you are doing classification among multiple classes and therefore the generated labels should be either categorical (default behavior) or sparse (i.e. class_mode='sparse'). Further, after reading the relevant parts of your code, it seems that you are doing multi-class classification, and not multi-label classification. Read this answer to make sure and also to find out which activation and loss function you should use.
As loss function use categorical_crossentropy instead of binary_crossentropy.
I am using Convolutional Neural Networking for vehicle identification, my first time. Currently, I am working with just 2 classes(bike and car). Training set: 420 car images and 825 bike images. Test set: 44 car images and 110 bike images Car and Bike images are in different format(bmp,jpg). In single prediction, I am always getting 'bike'. I have tried using the Sigmoid function in the output layer. Then I get only 'car'. My code is like following: ``
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense,Dropout
classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (128, 128, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (3, 3)))
# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (3, 3)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dropout(0.3))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
rotation_range= 3,
fill_mode = 'nearest',
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
rotation_range= 3,
fill_mode = 'nearest',
horizontal_flip = True)
training_set = train_datagen.flow_from_directory('dataset/training_set',
target_size = (128, 128),
batch_size = 10,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('dataset/test_set',
target_size = (128, 128),
batch_size = 10,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 1092//10,
epochs = 3,
validation_data = test_set,
validation_steps = 20)
classifier.save("car_bike.h5")
And I wanted to test a single image like the following:
test_image = image.load_img('dataset/single_prediction/download (3).jpg', target_size = (128, 128))
test_image = image.img_to_array(test_image)
test_image *= (1/255.0)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
if result[0][0] == 1:
prediction = 'bike'
else:
prediction = 'car'
print(" {}".format(prediction))
If you print your result matrix you'll see that it doesn't have only 1s and 0s but floats between these numbers. You may pick a threshold and set values that exceed it to 1 and everything else to 0.
I have trained an image classifier using keras and it gave a very good accuracy. I've saved the model using the save()and saved it using the h5 format. How can I make a prediction using the model?
The code is:
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 8000,
epochs = 5,
validation_data = test_set,
validation_steps = 2000)
classifier.save('classifier.h5')
Thanks in Advance..!!
The first step is to import your model using load_model method.
from keras.models import load_model
model = load_model('my_model.h5')
Then you have to compile the model in order to make predictions.
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
Now you can predict results for a new entry image.
from keras.preprocessing import image
test_image = image.load_img(imagePath, target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
#predict the result
result = model.predict(test_image)