text: from keras.layers import Dense, Input, Dropout, GlobalAveragePooling2D, Flatten, Conv2D, BatchNormalization, Activation, MaxPooling2D
  from keras.models import Model, Sequential
  from keras.optimizers import Adam
  from keras.preprocessing.image import ImageDataGenerator
  from keras.callbacks import ModelCheckpoint
  import numpy as np
  from keras.preprocessing.image import load_img, img_to_array
  import matplotlib.pyplot as plt
  import os
  
  #definir  nºclasses de emoção
  nb_classes = 7
  #Diretórios
  train_dir = 'treino/'
  val_dir = 'teste/'
  
  num_train = 28709
  num_val = 7178
  batch_size = 64
  num_epoch = 50
  
  train_datagen = ImageDataGenerator(rescale=1./255)
  val_datagen = ImageDataGenerator(rescale=1./255)
  
  train_generator = train_datagen.flow_from_directory(
      train_dir,
      target_size=(48, 48),
      batch_size=batch_size,
      color_mode="grayscale",
      class_mode='categorical')
  
  validation_generator = val_datagen.flow_from_directory(
      val_dir,
      target_size=(48, 48),
      batch_size=batch_size,
      color_mode="grayscale",
      class_mode='categorical')
  
  
  #Criar modelo
  model = Sequential()
  
  
  model.add(Conv2D(64, (3, 3), padding='same', input_shape=(48, 48, 1)))
  model.add(BatchNormalization())
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))
  
  
  model.add(Conv2D(128, (5, 5), padding='same'))
  model.add(BatchNormalization())
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))
  
  model.add(Conv2D(512, (3, 3), padding='same'))
  model.add(BatchNormalization())
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))
  
  
  model.add(Conv2D(512, (3, 3), padding='same'))
  model.add(BatchNormalization())
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))
  
  
  model.add(Flatten())
  
  
  model.add(Dense(256))
  model.add(BatchNormalization())
  model.add(Activation('relu'))
  model.add(Dropout(0.25))
  
  
  model.add(Dense(512))
  model.add(BatchNormalization())
  model.add(Activation('relu'))
  model.add(Dropout(0.25))
  
  model.add(Dense(nb_classes, activation='softmax'))
  
  opt = Adam(lr=0.0001)
  model.compile(optimizer=opt, loss='categorical_crossentropy',
                metrics=['accuracy'])
  checkpoint = ModelCheckpoint(
      "model.h5", monitor='val_acc', verbose=1, save_best_only=True, mode='max')
  callbacks_list = [checkpoint]
  
  history = model.fit_generator(generator=train_generator,
                                steps_per_epoch=train_generator.n//train_generator.batch_size,
                                epochs=num_epoch,
                                validation_data=validation_generator,
                                validation_steps=validation_generator.n//validation_generator.batch_size,
                                callbacks=callbacks_list
                                )
  model_json = model.to_json()
  with open("model.json", "w") as json_file:
      json_file.write(model_json)
  
  plt.figure(figsize=(20, 10))
  plt.subplot(1, 2, 1)
  plt.suptitle('Optimizer : Adam', fontsize=10)
  plt.ylabel('Loss', fontsize=16)
  plt.plot(history.history['loss'], label='Training Loss')
  plt.plot(history.history['val_loss'], label='Validation Loss')
  plt.legend(loc='upper right')
  
  plt.subplot(1, 2, 2)
  plt.ylabel('Accuracy', fontsize=16)
  plt.plot(history.history['acc'], label='Training Accuracy')
  plt.plot(history.history['val_acc'], label='Validation Accuracy')
  plt.legend(loc='lower right')
  plt.show()
  