<jupyter_start><jupyter_text>Keras dataset preprocessing utilities, located at tf.keras.preprocessing, help you go from raw data on disk to a tf.data.Dataset object that can be used to train a model.See https://keras.io/api/preprocessing/Once the dataset is preprocessed and loaded, it can be directly used in calls to model.fit<jupyter_code>## loading training data
training_ds = tf.keras.preprocessing.image_dataset_from_directory(
    'data/MaskNoMask/data',
    validation_split=0.2,
    subset= "training",
    seed=42,
    image_size= (img_height, img_width),
    batch_size=batch_size

)
## loading testing data
testing_ds = tf.keras.preprocessing.image_dataset_from_directory(
'data/MaskNoMask/data',
    validation_split=0.2,
    subset= "validation",
    seed=42,
    image_size= (img_height, img_width),
    batch_size=batch_size

)
training_ds
class_names = training_ds.class_names
plt.figure(figsize=(10, 10))
for images, labels in training_ds.take(1):
  for i in range(12):
    ax = plt.subplot(3, 4, i + 1)
    plt.imshow(images[i].numpy().astype("uint8"))
    plt.title(class_names[labels[i]])
    plt.grid(True)
## Configuring dataset for performance
AUTOTUNE = tf.data.experimental.AUTOTUNE
training_ds = training_ds.cache().prefetch(buffer_size=AUTOTUNE)
testing_ds = testing_ds.cache().prefetch(buffer_size=AUTOTUNE)<jupyter_output><empty_output><jupyter_text>Now build a deep neural network and train it and see how you do Import Resnet50 Model<jupyter_code>from keras.applications import InceptionV3
from tensorflow.keras.layers import Input
from keras.models       import Model
from keras.layers import Dense
import keras
from sklearn.preprocessing import LabelEncoder
from numpy import array
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input<jupyter_output><empty_output><jupyter_text>Build a model from Resnet50<jupyter_code>resnet50 = keras.applications.resnet50.ResNet50(include_top=True)
# first freeze all the resnet layers so they can't be trained
for layer in resnet50.layers:
    layer.trainable=False

# index -2 is the penultimate layer.
embedding_layer = resnet50.get_layer(index=-2)
embedding_layer_out = embedding_layer.output
embedding_layer
resnet50_input = resnet50.input
new_classification_output = Dense(1, activation='sigmoid')(embedding_layer_out)
new_model = Model(inputs=resnet50_input, outputs=new_classification_output)

#new_model.summary()<jupyter_output><empty_output><jupyter_text>I only did three epochs due to lack of computing power for my model...it still took 2 hrs and 13 minutes<jupyter_code>#f1 = (2*(keras.metrics.Precision()*keras.metrics.Recall())/(keras.metrics.Precision()+keras.metrics.Recall()))
new_model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'
                      , keras.metrics.Recall(), keras.metrics.Precision(), keras.metrics.TruePositives()
                      , keras.metrics.FalsePositives(), keras.metrics.TrueNegatives(), keras.metrics.FalseNegatives()
                      ])
history = new_model.fit(
  training_ds,
  validation_data=testing_ds,
  epochs=3,
  use_multiprocessing=True
)
n_epochs = 3
f1_train = (2*(history.history['recall'][n_epochs - 1]*history.history['precision'][n_epochs - 1])/(history.history['recall'][n_epochs - 1]+history.history['precision'][n_epochs - 1]))
f1_test = (2*(history.history['val_recall'][n_epochs - 1]*history.history['val_precision'][n_epochs - 1])/(history.history['val_recall'][n_epochs - 1]+history.history['val_precision'][n_epochs - 1]))
print(f1_train)
print(f1_test)
print(history.history['accuracy'])
print(history.history['val_accuracy'])

print(history.history['loss'])
print(history.history['val_loss'])
print("      Train ")
print(f"   | True   | False")
print(f"1  | {history.history['true_positives'][2]} | {history.history['false_positives'][2]}" )
print(f"0  | {history.history['false_negatives'][2]}   | {history.history['true_negatives'][2]}")
print("      Test ")
print(f"   | True  | False")
print(f"1  | {history.history['val_true_positives'][2]} | {history.history['val_false_positives'][2]}" )
print(f"0  | {history.history['val_false_negatives'][2]}  | {history.history['val_true_negatives'][2]}")
ypred = new_model.predict(testing_ds)
ypred = np.where(ypred > 0.5, 1, 0)
ytrue = [y for x, y in testing_ds]
ytrue = np.concatenate(ytrue)
from sklearn import metrics
metrics.confusion_matrix(ytrue, ypred)<jupyter_output><empty_output><jupyter_text>New images<jupyter_code>mask_url = "https://media.vanityfair.com/photos/5f2ac1b683d239dce41f8229/master/pass/kate-middleton.jpg"
mask_path = tf.keras.utils.get_file('kate mask', origin=mask_url)

img = keras.preprocessing.image.load_img(
    mask_path, target_size=(img_height, img_width)
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch

predictions = new_model.predict(img_array)
score = tf.nn.softmax(predictions[0])

print(
    "This image most likely belongs to {} with a {:.2f} percent confidence."
    .format(class_names[np.argmax(score)], 100 * np.max(score))
)
plt.figure(figsize=(10, 10)) 
ax = plt.subplot(3, 4, 1)
plt.imshow(img)
#plt.title()
plt.grid(True)
pink_url = "https://cdn.shopify.com/s/files/1/2727/4444/products/E42A9819.jpg?v=1586730354"
pink_path = tf.keras.utils.get_file('pink', origin=pink_url)

img = keras.preprocessing.image.load_img(
    pink_path, target_size=(img_height, img_width)
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch

predictions = new_model.predict(img_array)
score = tf.nn.softmax(predictions[0])

print(
    "This image most likely belongs to {} with a {:.2f} percent confidence."
    .format(class_names[np.argmax(score)], 100 * np.max(score))
)
plt.figure(figsize=(10, 10)) 
ax = plt.subplot(3, 4, 1)
plt.imshow(img)
#plt.title()
plt.grid(True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13279312055564743, 'fraction_numerical': 0.025136431288242105, 'mean_word_length': 5.682872928176796, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 3, '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': '569624', 'license': 'MIT', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2127, 'n_words': 390, 'path': 'MaskNoMask-Daniel.ipynb', 'repo_name': 'DaniBrew4/Deep-Learning-Mask', 'size': '6047'}