-4

I am trying to build a convolutional neural network to detect emotion (so a simple image recognition ai). So far I have been able to resize my images to a 32, 32 size as well as add each pixel's RGB values to a list. How should I proceed now? I have been trying to add a convolutional layer but have been having some trouble with the implementation, I am just unsure with how the actual construction of the hidden layers should look like.

zsizakhan
  • 3
  • 1
  • 2
    Your question is too vague and general. Do you want to use Keras or TensorFlow? You have hundreds of tutorials of how to use any of them for image object recognition, for instance, which is not too different from image emotion recognition in the configuration. Just to point to the "official" tutorials: https://keras.io/#getting-started-30-seconds-to-keras https://www.tensorflow.org/tutorials/ – alexhg Aug 02 '19 at 05:27
  • I am currently using tensorflow. To be more specific, should I be using an already built pre-existing model for my image recognition software or write my own? Most of the tensorflow tutorials I have found focus on MNIST exclusively and have no mention on how to implement your own dataset. – zsizakhan Aug 02 '19 at 15:49

1 Answers1

0

Answering with an example using google colab and the MNIST dataset.

Step1: - Make the imports, get the dataset then scale and split into train and test. - Define the model (I'm using a model I found to be quite good, simpler models will work as well)

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from google.colab import files
from keras.preprocessing import image

class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('acc')>0.998):
      print("\nReached 99.8% accuracy so cancelling training!")
      self.model.stop_training = True

callbacks = myCallback()


mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images, test_images = training_images/255.0, test_images/255.0
training_images = training_images.reshape(60000, 28, 28, 1)
test_images = test_images.reshape(10000, 28, 28, 1)

model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
  tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
  tf.keras.layers.MaxPooling2D(2, 2),
  tf.keras.layers.Dropout(0.20),
  tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
  tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
  tf.keras.layers.MaxPooling2D(2, 2),
  tf.keras.layers.Dropout(0.25),
  tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding = "same"),
  tf.keras.layers.Dropout(0.25),
  tf.keras.layers.Flatten(),           
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.BatchNormalization(),
  tf.keras.layers.Dropout(0.25),                      
  tf.keras.layers.Dense(10, activation='softmax') 

])  


print (model.summary())

Step 2: Compile and train the model (also using the test set to evaluate during training)

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=20,callbacks =[callbacks])
test_loss = model.evaluate(test_images, test_labels)

Optional: Test with your own image. This uploading part is specific to google colab.

uploaded = files.upload()
for fn in uploaded.keys():

  path = '/content/' + fn
  img = image.load_img(path, target_size=(28, 28),color_mode = "grayscale")

Some scaling and tweaking is necassary to make the test picture "MNIST", before we predict it.

im2arr = np.array(img)
im2arr = im2arr-255.0
im2arr = im2arr*-1
image = im2arr
im2arr_scale = im2arr/255.0
im2arr = im2arr.reshape(1,28,28,1)
im2arr_scale = im2arr_scale.reshape(1,28,28,1)

y_pred = model.predict(im2arr)
y_pred2 = model.predict(im2arr_scale)



print("non scaled : " + str(np.argmax(y_pred)) + " probability: " + str(y_pred[0][np.argmax(y_pred)]))
print("scaled: " + str(np.argmax(y_pred2)) + " probability: " + str(y_pred2[0][np.argmax(y_pred2)]))
print (y_pred)
print (y_pred2)

Plotting the test image

plt.grid(False)
plt.gray()
plt.axis('off')
plt.imshow(image)
plt.show()
Maroder
  • 115
  • 4