introdução a convolutional neural networks com...

39
Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA Orientador: Geraldo Braz Junior

Upload: others

Post on 03-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

Introdução a Convolutional Neural Networks com Keras API

LUCAS BEZERRA MAIA

Orientador: Geraldo Braz Junior

Page 2: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

SUMÁRIO

▰ Introdução;

▰ Deep Features;

▰ Tipos de Inicialização de Treinamento;

▰ Implementando CNN com Keras

2

Page 3: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

Introdução

3

1

Page 4: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

REDE NEURAL CONVOLUCIONAL (CNN)

44

Page 5: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

Deep Features

5

2

Page 6: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

DEEP FEATURES

66

Page 7: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

DEEP FEATURES

77

Page 8: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

Tipos de Inicialização de Treino

8

3

Page 9: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

TREINO “DO ZERO”

▰ Inicia Rede com pesos randômicos;

▰ Efetua os ajustes dos pesos do zero;

▰ Pode demorar encontrar modelo ótimo;

▰ Precisa de mais amostras de treinamento;

9

Page 10: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

TREINO “FINE TUNING”

▰ Inicia Rede com pesos já treinados em outras bases;

▻ Conceito de Transfer Learning

▰ Efetua os ajustes dos pesos com a nova base;

▰ Pode encontrar modelo ótimo de forma mais rápida;

▰ Precisa de menos amostras de treinamento;

10

Page 11: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

TREINO “FINE TUNING” - IMAGENET CHALLENGE

▰ Inicia Rede com pesos já treinados em outras bases;

▻ Conceito de Transfer Learning

▰ Efetua os ajustes dos pesos com a nova base;

▰ Pode encontrar modelo ótimo de forma mais rápida;

▰ Precisa de menos amostras de treinamento;

11

Page 12: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

Usando Keras API para treinar uma CNN simples

12

Prática

Page 13: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Objetivo

13

▰ Objetivo: ▻ Aprender fundamentos de Deep Learning,

usando keras

Page 14: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - CNN

14

Page 15: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Git Repository

15

▰ git clone https://gitlab.com/lucasmaia1202/vc_cnn.git

Page 16: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Libs

16

▰ #Verificar se todas a libs estão instaladas▰ terminal$: python checkup.py

Page 17: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Libs

17

▰ from setup▰ from ip▰ from keras

Page 18: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Libs

18

▰ from keras.models import Sequential▰ from keras.layers import Dense, Dropout, Flatten▰ from keras.layers import Conv2D, MaxPooling2D

Page 19: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Validação

19

TREINAR A CNN60%

VALIDAR O TREINO10%

TESTAR A CNN30%

Page 20: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Separar Conjuntos

20

▰ # Declarar lista de dicionários informando as bases▰ database = [{“url”: “Digitos/”, “img_type”: “jpg”}]

Page 21: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Separar Conjuntos

21

▰ # Declarar sets▰ (train_list, y_train), (test_list, y_test), (valid_list, y_valid) =

setup.config_base(database=database, test_prop=0.3, valid_prop=0.1)

Page 22: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Variáveis Necessárias

22

▰ img_rows = 32▰ img_cols = 32▰ channels = 3

Page 23: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Carregar Imagens em Arrays

23

▰ X_train = ip.list_to_array(train_list, (img_rows, img_cols), channels)▰ X_test = ip.list_to_array(test_list, (img_rows, img_cols), channels)▰ X_valid = ip.list_to_array(valid_list, (img_rows, img_cols), channels)

Page 24: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Imprimir Shapes

24

▰ # Salvar arquivo e roda▰ print(X_train.shape)▰ print(X_test.shape)▰ print(X_valid.shape)

Page 25: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Transformar Saídas

25

▰ num_classes = 10▰ y_train = keras.utils.to_categorical(y_train, num_classes)▰ y_test = keras.utils.to_categorical(y_test, num_classes)▰ y_valid = keras.utils.to_categorical(y_valid, num_classes)

Page 26: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

26

▰ cnn = Sequential()▰ cnn.add(Conv2D(

filters=6, kernel_size=(3, 3), activation='relu', input_shape=(img_rows, img_cols, channels)

))

Page 27: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

27

▰ cnn.add(MaxPooling2D(pool_size=(2, 2))

Page 28: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

28

▰ cnn.add(Conv2D(filters=6, kernel_size=(3, 3), activation='relu'))

Page 29: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

29

▰ cnn.add(MaxPooling2D(pool_size=(2, 2))

Page 30: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

30

▰ cnn.add(Dropout(0.25))

Page 31: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

31

▰ cnn.add(Flatten())

Page 32: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

32

▰ cnn.add(Dense(units=120, activation=”relu”))

Page 33: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

33

▰ cnn.add(Dense(units=84, activation=”relu”))

Page 34: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

34

▰ cnn.add(Dropout(0.5))

Page 35: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Agora SIM a CNN

35

▰ cnn.add(Dense(units=num_classes, activation=”softmax”))

Page 36: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Só falta compilar a CNN criada

36

▰ cnn.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.SGD(),metrics=[‘accuracy’]

)

Page 37: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Treino da CNN

37

▰ cnn.fit(X_train, y_train,batch_size=128,epochs=12,verbose=1,validation_data=(X_valid, y_valid)

)

Page 38: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

PRÁTICA - Avaliação da CNN

38

▰ score = cnn.evaluate(X_test, y_test)▰ print(“Acuracia:” score[1])

Page 39: Introdução a Convolutional Neural Networks com Kerasnca.ufma.br/~geraldo/vcm/VC_MESTRADO_KERAS.pdf · Introdução a Convolutional Neural Networks com Keras API LUCAS BEZERRA MAIA

OBRIGADO!Dúvidas?

[email protected]