<jupyter_start><jupyter_text>Reload Saved Model<jupyter_code>filename = "04_rock_p_s_model"
saved_model_filename = "G:\\My Drive\\ColabModels\\" + filename
saved_model_filename
new_model = tf.keras.models.load_model(saved_model_filename)
new_model.summary()
loss, acc = new_model.evaluate(validation_generator, verbose=2)
print(f'Restored model, accuracy: {100 * acc}')<jupyter_output>12/12 - 3s - loss: 0.1751 - accuracy: 0.9032
Restored model, accuracy: 90.32257795333862<jupyter_text>Find Class Labels<jupyter_code># optional
def find_class_labels(data_gen):
    data_gen.class_indices
    dataset_labels = sorted(data_gen.class_indices.items(),
                        key=lambda pair:pair[1])
    dataset_labels = np.array([key.title() for key, value in dataset_labels])
    return dataset_labels
 # any one will work
dataset_labels = find_class_labels(validation_generator)
dataset_labels = find_class_labels(train_generator)
dataset_labels = ['Paper','Rock', 'Scissors']
dataset_labels<jupyter_output><empty_output><jupyter_text>Prediction on validation batch<jupyter_code># Get data as a batch - batch has image and label size 100
val_image_batch, val_label_batch = next(iter(validation_generator))
print("Validation batch shape:", val_image_batch.shape)
print("Validation batch shape:", val_label_batch.shape)
# batch label : cat or dog
# val_label_batch<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code>model_predictions = new_model.predict(val_image_batch)
pred_df = pd.DataFrame(model_predictions)
pred_df.columns = dataset_labels
print("Prediction results for the first elements")
pred_df.head(10)<jupyter_output>Prediction results for the first elements<jupyter_text>Misclassified Examples<jupyter_code>predicted_label_ids = np.argmax(model_predictions, axis=-1)
n = 8 
print(val_label_batch[n])
a = int(np.argmax(val_label_batch[n]))
p = np.argmax(predicted_label_ids[n])
print(f"actual    : {a} -> {dataset_labels[a]}")
print(f"predicted : {p} -> {dataset_labels[p]}")

plt.figure(figsize=(20,15))
plt.subplots_adjust(hspace=0.5)
for n in range(20):
    a = int(np.argmax(val_label_batch[n]))
    p = np.argmax(predicted_label_ids[n])
    #if (int(np.argmax(predicted_label_ids[n])) == int(np.argmax(val_label_batch[n]))) :
    if (p == a) :
        color = 'green'
    else:
        color = 'red'
    plt.subplot(6,5,n+1)
    plt.imshow(val_image_batch[n])
    plt.title(f"{dataset_labels[a]} <is> {dataset_labels[p]}".title(), color=color)
    plt.axis('off')
    _ = plt.suptitle("Model predictions (green: correct, red: incorrect)")<jupyter_output><empty_output><jupyter_text>Reports<jupyter_code>from sklearn.metrics import classification_report,confusion_matrix
import numpy as np
from google.colab import files
from keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path = fn
  img = image.load_img(path, target_size=(150, 150))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=10)
  print(fn)
  print(classes)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12535976974736168, 'fraction_numerical': 0.02078669651423089, 'mean_word_length': 6.207373271889401, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '312278', 'license': 'MIT', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1106, 'n_words': 201, 'path': '2. Convolutional Neural Network in Tensorflow/AZ/04_rock_paper_scissors.ipynb', 'repo_name': 'AmirRazaMBA/TensorFlow-Certification', 'size': '3127'}