<jupyter_start><jupyter_text>Leitura dos Arquivos no Drive<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
!ls "/content/drive/My Drive/Colab Notebooks"
import numpy as np
X_test=np.load("/content/drive/My Drive/Colab Notebooks/imagens_teste.npy")
y_test=np.load("/content/drive/My Drive/Colab Notebooks/labels_teste.npy")
imagens_treino=np.load("/content/drive/My Drive/Colab Notebooks/imagens_treino.npy")
labels_treino=np.load("/content/drive/My Drive/Colab Notebooks/labels_treino.npy")
from sklearn.model_selection import train_test_split

X_train, X_val, y_train, y_val = train_test_split(imagens_treino, labels_treino, test_size=0.15, random_state=42)

new_y_test=[]
for i in range(len(y_test)):
  for j in range(len(y_test[i])):
    if y_test[i][j]==1:
      new_y_test.append(j)
new_y_test=np.asarray(new_y_test)
new_y_train=[]
for i in range(len(y_train)):
  for j in range(len(y_train[i])):
    if y_train[i][j]==1:
      new_y_train.append(j)
new_y_train=np.asarray(new_y_train)
new_y_val=[]
for i in range(len(y_val)):
  for j in range(len(y_val[i])):
    if y_val[i][j]==1:
      new_y_val.append(j)
new_y_val=np.asarray(new_y_val)<jupyter_output><empty_output><jupyter_text>Montagem da rede Simpson<jupyter_code>from keras.applications import VGG19

 

conv_base = VGG19(weights='imagenet',

                  include_top=False,

                  input_shape=(64, 64, 3))

 

conv_base.summary()
from keras import models, layers, optimizers

model = models.Sequential()

model.add(conv_base)


model.add(layers.Flatten())

model.add(layers.Dense(1024, activation='relu'))

model.add(layers.Dropout(0.5))

model.add(layers.Dense(18, activation='softmax'))

 


model.compile(optimizer=optimizers.RMSprop(lr=1e-5),

              loss='sparse_categorical_crossentropy',

              metrics=['acc'])

 

history = model.fit(X_train,new_y_train,

                    epochs=10,

#                    steps_per_epoch=100,

                    batch_size=100,

                    validation_data=(X_val, new_y_val))
import matplotlib.pyplot as plt
def plot_model_history(model_history):
    fig, axs = plt.subplots(1,2,figsize=(15,5))
    # summarize history for accuracy
    axs[0].plot(range(1,len(model_history.history['acc'])+1),model_history.history['acc'])
    axs[0].plot(range(1,len(model_history.history['val_acc'])+1),model_history.history['val_acc'])
    axs[0].set_title('Model Accuracy')
    axs[0].set_ylabel('Accuracy')
    axs[0].set_xlabel('Epoch')
    axs[0].set_xticks(np.arange(1,len(model_history.history['acc'])+1),len(model_history.history['acc'])/10)
    axs[0].legend(['train', 'val'], loc='best')
    # summarize history for loss
    axs[1].plot(range(1,len(model_history.history['loss'])+1),model_history.history['loss'])
    axs[1].plot(range(1,len(model_history.history['val_loss'])+1),model_history.history['val_loss'])
    axs[1].set_title('Model Loss')
    axs[1].set_ylabel('Loss')
    axs[1].set_xlabel('Epoch')
    axs[1].set_xticks(np.arange(1,len(model_history.history['loss'])+1),len(model_history.history['loss'])/10)
    axs[1].legend(['train', 'val'], loc='best')
    plt.show()
plot_model_history(history)
model.evaluate(X_test,new_y_test)<jupyter_output>890/890 [==============================] - 2s 2ms/step<jupyter_text>Previsão e Avaliação do modelo<jupyter_code>y_pred = model.predict(X_test)
import sklearn
map_characters = {0: 'abraham_grampa_simpson', 1: 'apu_nahasapeemapetilon', 2: 'bart_simpson', 
        3: 'charles_montgomery_burns', 4: 'chief_wiggum', 5: 'comic_book_guy', 6: 'edna_krabappel', 
        7: 'homer_simpson', 8: 'kent_brockman', 9: 'krusty_the_clown', 10: 'lisa_simpson', 
        11: 'marge_simpson', 12: 'milhouse_van_houten', 13: 'moe_szyslak', 
        14: 'ned_flanders', 15: 'nelson_muntz', 16: 'principal_skinner', 17: 'sideshow_bob'}

print('\n', sklearn.metrics.classification_report(np.where(y_test > 0)[1], 
                                                  np.argmax(y_pred, axis=1), 
                                                  target_names=list(map_characters.values())), sep='')
import matplotlib.pyplot as plt
plt.figure(figsize=(8,8))
cnf_matrix = sklearn.metrics.confusion_matrix(np.where(y_test > 0)[1], np.argmax(y_pred, axis=1))
classes = list(map_characters.values())
plt.imshow(cnf_matrix)
plt.colorbar()
tick_marks = np.arange(len(classes))
_ = plt.xticks(tick_marks, classes, rotation=90)
_ = plt.yticks(tick_marks, classes)
print(cnf_matrix)

model.save_weights('/content/drive/My Drive/Colab Notebooks/modelo_vgg19simpson.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16021739130434784, 'fraction_numerical': 0.025434782608695652, 'mean_word_length': 4.708436724565757, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '71672', 'license': 'MIT', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1746, 'n_words': 236, 'path': 'experiment/train/Simpson_train_vgg19.ipynb', 'repo_name': 'gabrieltnishimura/simpsons-tinnvestigate', 'size': '4600'}