![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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/11.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/37.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022060500/5f1a94c5ad935934526f9fd6/html5/thumbnails/38.jpg)
PRÁTICA - Avaliação da CNN
38
▰ score = cnn.evaluate(X_test, y_test)▰ print(“Acuracia:” score[1])