<jupyter_start><jupyter_text>Cifar10 - exercicio de classificar 3 classes Fazer classificação de 3 classes usando rede neural convolucional.Não utilizar o pacote sklearn. Apenas o Keras e o NumPy. Não utilizar a função de acompanhamento de gráficos durante o treinamento.Gerar uma figura mosaic que contenha as 5 imagens de classificação correta de menor probabilidade de predição.Gerar esta figura com o nome: cifar_fig.png  Importação de bibliotecas<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt

import numpy as np

from keras.applications.vgg16 import VGG16
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.models import Sequential, load_model, Model, Input
from keras.optimizers import RMSprop, Adam, SGD
from keras.preprocessing.image import ImageDataGenerator
import keras.regularizers as reg
from keras.utils import np_utils
import keras.backend as K
K.set_image_data_format('channels_last')
K.set_floatx('float32')<jupyter_output><empty_output><jupyter_text>Carregamento do dataset<jupyter_code>data = np.load('/etc/jupyterhub/ia368z_2s2017/datasets/cifar10-redux.npz')
X = data['X_train']
y = data['y_train']
X_test = data['X_test']
y_test = data['y_test']

X = np.swapaxes(X, 1, 2)
X = np.swapaxes(X, 2, 3)
X_test = np.swapaxes(X_test, 1, 2)
X_test = np.swapaxes(X_test, 2, 3)
X.shape, y.shape, X_test.shape, y_test.shape
X.dtype, y.dtype, X_test.dtype, y_test.dtype<jupyter_output><empty_output><jupyter_text>Ajuste dos dados<jupyter_code>X = X.astype('float32')
X /= 255.
X_test = X_test.astype('float32')
X_test /= 255.

# transforma labels em 0, 1 e 2, ao invés de 3, 4 e 5
y = y - 3
y_test = y_test - 3<jupyter_output><empty_output><jupyter_text>Separação dos dados de treinamento em treinamento e validação<jupyter_code># numero de amostras
nb_data = X.shape[0]

# semente fixa para dar reproducibilidade
seed = 13
np.random.seed(seed)

msk = np.random.rand(nb_data) < 0.80
X_train = X[msk]
X_validate = X[~msk]
y_train = y[msk]
y_validate = y[~msk]

nb_train = X_train.shape[0]
nb_validate = X_validate.shape[0]

print(nb_train)
print(nb_validate)
print(X_train.shape)
print(X_validate.shape)
print(y_train.shape)
print(y_validate.shape)<jupyter_output>1605
395
(1605, 32, 32, 3)
(395, 32, 32, 3)
(1605,)
(395,)<jupyter_text>Transforma o vetor de labels para o formato de one-hot encoding.<jupyter_code>nb_classes = 3

y_oh = np_utils.to_categorical(y, nb_classes)
y_train_oh = np_utils.to_categorical(y_train, nb_classes)
y_validate_oh = np_utils.to_categorical(y_validate, nb_classes)
y_test_oh = np_utils.to_categorical(y_test, nb_classes)<jupyter_output><empty_output><jupyter_text>Criando a rede VGG16<jupyter_code>i = Input(shape=(32,32, 3))
vgg = VGG16(include_top=False, weights='imagenet', input_tensor=i)
vgg.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_4 (InputLayer)         (None, 32, 32, 3)         0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 32, 32, 64)        1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 32, 32, 64)        36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 16, 16, 64)        0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 16, 16, 128)       73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 16, 16, 128)       147584    
_________________________________________________________________
block2_poo[...]<jupyter_text>Ajustando a rede para os dados do CIFAR-10<jupyter_code># com a rede completa: #41%
#vgg.layers.pop() # 48.80%
#vgg.layers.pop() # 45.00%
#vgg.layers.pop() # 44.80%
#vgg.layers.pop() # 46.80%

vgg.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_4 (InputLayer)         (None, 32, 32, 3)         0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 32, 32, 64)        1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 32, 32, 64)        36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 16, 16, 64)        0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 16, 16, 128)       73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 16, 16, 128)       147584    
_________________________________________________________________
block2_poo[...]<jupyter_text>Obtendo as features com a VGG<jupyter_code>train_features = vgg.predict(X_train)
valid_features = vgg.predict(X_validate)
test_features = vgg.predict(X_test)

print(train_features.shape[1:])
print(valid_features.shape)
print(test_features.shape)<jupyter_output>(1, 1, 512)
(395, 1, 1, 512)
(500, 1, 1, 512)<jupyter_text>Construíndo a CNN com o Keras<jupyter_code># Definindo a rede
model = Sequential()
model.add(Flatten(input_shape=train_features.shape[1:]))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.75))
model.add(Dense(nb_classes, kernel_regularizer=reg.l2(0.025)))
model.add(Activation('softmax'))

model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_2 (Flatten)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 128)               65664     
_________________________________________________________________
activation_3 (Activation)    (None, 128)               0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 3)                 387       
_________________________________________________________________
activation_4 (Activation)    (None, 3)                 0         
=================================================================
Total para[...]<jupyter_text>Compilando a rede<jupyter_code>loss = 'categorical_crossentropy'
#opt = RMSprop()
opt = Adam()

model.compile(loss=loss, optimizer=opt, metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>Callbacks<jupyter_code>checkpointer = ModelCheckpoint(filepath='cifar10.hdf5', monitor='val_acc', verbose=1, save_best_only=True)
earlystopper = EarlyStopping(monitor='val_acc', min_delta=0, patience=20, verbose=1, mode='auto')
lrreduction = ReduceLROnPlateau(monitor='val_acc', patience=7, verbose=1, factor=0.8, min_lr=0.00001)<jupyter_output><empty_output><jupyter_text>Treinamento da rede<jupyter_code>batch_size = 40
epochs = 100

model.fit(train_features, y_train_oh, 
          batch_size=batch_size, 
          epochs=epochs,
          validation_data=(valid_features, y_validate_oh),
          callbacks=[checkpointer, lrreduction])
          #callbacks=[checkpointer, earlystopper, lrreduction])<jupyter_output>Train on 1605 samples, validate on 395 samples
Epoch 1/100
1440/1605 [=========================>....] - ETA: 0s - loss: 1.3012 - acc: 0.4389Epoch 00000: val_acc improved from -inf to 0.61772, saving model to cifar10.hdf5
1605/1605 [==============================] - 0s - loss: 1.2851 - acc: 0.4436 - val_loss: 1.0115 - val_acc: 0.6177
Epoch 2/100
1480/1605 [==========================>...] - ETA: 0s - loss: 1.0318 - acc: 0.5466Epoch 00001: val_acc improved from 0.61772 to 0.61772, saving model to cifar10.hdf5
1605/1605 [==============================] - 0s - loss: 1.0341 - acc: 0.5483 - val_loss: 0.9415 - val_acc: 0.6177
Epoch 3/100
1480/1605 [==========================>...] - ETA: 0s - loss: 0.9650 - acc: 0.5858Epoch 00002: val_acc improved from 0.61772 to 0.62785, saving model to cifar10.hdf5
1605/1605 [==============================] - 0s - loss: 0.9606 - acc: 0.5894 - val_loss: 0.8920 - val_acc: 0.6278
Epoch 4/100
1480/1605 [==========================>...] - ETA: 0s - loss: 0.9080 - a[...]<jupyter_text>Apaga modelo atual<jupyter_code>del model<jupyter_output><empty_output><jupyter_text>Carrega modelo salvo em disco<jupyter_code>model = load_model('cifar10.hdf5')<jupyter_output><empty_output><jupyter_text>Avaliação do treinamento no conjunto de testes com o melhor modelo<jupyter_code>loss, accuracy = model.evaluate(test_features, y_test_oh, batch_size=10)
print("\n[INFO] accuracy on the test data set: {:.2f}% [{:.5f}]".format(accuracy * 100, loss))<jupyter_output>250/500 [==============>...............] - ETA: 0s
[INFO] accuracy on the test data set: 68.60% [0.75198]<jupyter_text>Construindo a nova rede com Fine Tunning<jupyter_code>vgg = VGG16(include_top=False, weights='imagenet', classes=nb_classes, pooling='max')
#vgg.output.set_shape([None, 1, 1, 512])
#x = Flatten(input_shape=vgg.output.shape[1:])(vgg.output)
x = Dense(1728, activation='relu', name='f1')(vgg.output)
x = Dense(128, activation='relu', name='f2')(x)
x = Dropout(0.75)(x)
x = Dense(nb_classes, activation='softmax', kernel_regularizer=reg.l2(0.025), name='f3')(x)

model = Model(inputs=vgg.input, outputs=x)

model.summary()

w0, b0, w1, b1, w2, b2, w3, b3, w4, b4 = load_model('my_cifar_dataplus.hdf5').get_weights()

# Coloco nas camadas densas finais da rede
model.layers[21].set_weights([w3, b3])
model.layers[23].set_weights([w4, b4])

# camadas que não serão treinadas
for layer in model.layers[:15]:
        layer.trainable = False

model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, None, None, 3)     0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, None, None, 64)    1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, None, None, 64)    36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, None, None, 64)    0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, None, None, 128)   73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, None, None, 128)   147584    
_________________________________________________________________
block2_poo[...]<jupyter_text>Compilando a rede<jupyter_code>loss = 'categorical_crossentropy'
#opt = RMSprop()
opt = Adam()
#opt = SGD(lr=0.0001, momentum=0.9, nesterov=True)

model.compile(loss=loss, optimizer=opt, metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>Callbacks<jupyter_code>checkpointer = ModelCheckpoint(filepath='cifar10.hdf5', monitor='val_acc', verbose=1, save_best_only=True)
earlystopper = EarlyStopping(monitor='val_acc', min_delta=0, patience=20, verbose=1, mode='auto')
lrreduction = ReduceLROnPlateau(monitor='val_acc', patience=7, verbose=1, factor=0.8, min_lr=0.00001)<jupyter_output><empty_output><jupyter_text>Fine tunning da rede<jupyter_code>batch_size = 10
epochs = 50

model.fit(X_train, y_train_oh, 
          batch_size=batch_size, 
          epochs=epochs,
          validation_data=(X_validate, y_validate_oh),
          callbacks=[checkpointer, earlystopper, lrreduction])<jupyter_output>Train on 1605 samples, validate on 395 samples
Epoch 1/50
1590/1605 [============================>.] - ETA: 0s - loss: 1.2500 - acc: 0.3660Epoch 00000: val_acc improved from -inf to 0.32405, saving model to cifar10.hdf5
1605/1605 [==============================] - 7s - loss: 1.2490 - acc: 0.3651 - val_loss: 1.2251 - val_acc: 0.3241
Epoch 2/50
1590/1605 [============================>.] - ETA: 0s - loss: 1.1641 - acc: 0.3396Epoch 00001: val_acc did not improve
1605/1605 [==============================] - 5s - loss: 1.1639 - acc: 0.3396 - val_loss: 1.1408 - val_acc: 0.3241
Epoch 3/50
1590/1605 [============================>.] - ETA: 0s - loss: 1.1331 - acc: 0.3541Epoch 00002: val_acc did not improve
1605/1605 [==============================] - 5s - loss: 1.1332 - acc: 0.3514 - val_loss: 1.1286 - val_acc: 0.3241
Epoch 4/50
1590/1605 [============================>.] - ETA: 0s - loss: 1.1224 - acc: 0.3516- ETA: 0s - loss: 1.1227 - acc: 0.3Epoch 00003: val_acc did not improve
1605/1605 [=====[...]<jupyter_text>Apaga modelo atual<jupyter_code>del model<jupyter_output><empty_output><jupyter_text>Carrega modelo salvo em disco<jupyter_code>model = load_model('cifar10.hdf5')<jupyter_output><empty_output><jupyter_text>Avaliação do treinamento no conjunto de testes com o melhor modelo<jupyter_code>loss, accuracy = model.evaluate(X_test, y_test_oh, batch_size=10)
print("\n[INFO] accuracy on the test data set: {:.2f}% [{:.5f}]".format(accuracy * 100, loss))<jupyter_output>420/500 [========================>.....] - ETA: 0s
[INFO] accuracy on the test data set: 34.60% [1.20269]
{'fraction_non_alphanumeric': 0.16135819487774794, 'fraction_numerical': 0.07255314517884351, 'mean_word_length': 5.091029606716748, 'pattern_counts': {'":': 0, '<': 80, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '161241', 'license': 'Unlicense', 'n_tokens_mistral': 5184, 'n_tokens_neox': 4477, 'n_words': 972, 'path': 'week06/my_cifar_transferlearning.ipynb', 'repo_name': 'willianrampazzo/ia368z', 'size': '13783'}