<jupyter_start><jupyter_text>Final Project DL model Notebook - sinple CNN model with two convolutional layers - v02 - load from existing weightsusing this notebook to validate performance of the model  imports, settings and constants run log: - local- sample data set- using trained network<jupyter_code>import sys

from keras.models import load_model
import numpy as np

from pathlib import Path

sys.path.append(str(Path.cwd().parent))
from utils import *

current_dir = Path.cwd()
HOME_DIR = current_dir.parent
DATA_DIR = HOME_DIR.joinpath('data')
MODEL_PATH = HOME_DIR.joinpath('savedModels/30122017/CNN_two_convs_30122017_1700.h5')

# comment out one of the two path options to toggle between sample directory and all data
# path = DATA_DIR
path = DATA_DIR.joinpath('sample')
val_path = path.joinpath('valid')
test_path = path.joinpath('test')

# general variables
batch_size = 64

# make sure that plotted images show up in the notebook
%matplotlib inline<jupyter_output><empty_output><jupyter_text>defining CNN model<jupyter_code>model = load_model(MODEL_PATH)
model.summary()<jupyter_output>/Users/phillipgieshen/anaconda/lib/python3.6/site-packages/keras/models.py:287: UserWarning: Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
  warnings.warn('Error in loading the saved optimizer '<jupyter_text>test performance and plot confusion matrix on one sample batch of 200 images<jupyter_code>val_batches = get_in_batches(val_path, shuffle=False, class_mode=None, batch_size=200)
pred_classes = model.predict_generator(val_batches, 1)
pred_classes = np.argmax(pred_classes, axis=1)
act_classes = val_batches.classes
cm = confusion_matrix(act_classes, pred_classes)
plot_confusion_matrix(cm, val_batches.class_indices)
plt.figure()
plt.show()<jupyter_output>[[20  0  0  0  0  0  0  0  0  0]
 [ 0 20  0  0  0  0  0  0  0  0]
 [ 0  0 20  0  0  0  0  0  0  0]
 [ 0  0  0 20  0  0  0  0  0  0]
 [ 0  0  0  0 20  0  0  0  0  0]
 [ 0  0  0  0  0 20  0  0  0  0]
 [ 0  0  0  0  0  0 20  0  0  0]
 [ 0  0  0  0  0  0  0 20  0  0]
 [ 0  0  0  0  0  0  0  0 20  0]
 [ 0  0  0  0  0  0  0  0  1 19]]<jupyter_text>validating the model performance on the val setRunning the evaluate generator returns the cost and accuracy of the model. Doing it in a loop allows us to confirm that the performance is stable. Results should be very similar for all runs. Fixed issue that was causing slow performance by changing second parameter of evaluate_generator to 1.<jupyter_code>rnd_batches = get_in_batches(val_path, batch_size=batch_size, shuffle=True)
val_res = [model.evaluate_generator(rnd_batches, 1) for i in range(10)]
np.round(val_res, 3)<jupyter_output>Found 200 images belonging to 10 classes.<jupyter_text>now we manually load a handful of images from the test set and output the prediction on them<jupyter_code>test_batches = get_in_batches(test_path, batch_size=4, class_mode=None)
imgs = test_batches.next()
plots(imgs)
prediction = model.predict(imgs)
np.round(prediction, 3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07579700715679896, 'fraction_numerical': 0.05204944697462589, 'mean_word_length': 4.550541516245487, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '491101', 'license': 'MIT', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1059, 'n_words': 364, 'path': 'nbs/finalProjectModelNotebook_CNN_two_convs_validate.ipynb', 'repo_name': 'SchlachterSchmidt/precognitive-octo-engine', 'size': '3074'}