<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
from tensorflow import keras
import tensorflow_datasets as tfds
import os
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.layers import Input, Dense, GlobalAveragePooling2D, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras import Sequential
(ds_train, ds_validation, ds_test), ds_info = tfds.load(
    "patch_camelyon",
    split = ["train", "validation", "test"], 
    shuffle_files = True,
    as_supervised = True,
    with_info = True,

print(ds_info)
fig = tfds.show_examples(ds_train, ds_info, rows = 4, cols = 4)
def normalize_img(image, label):
  # this will normalize the images
  return tf.cast(image, tf.float16)/255.0, label
def augment(image_label, seed):
  image, label = image_label
  image, label = normalize_img(image, label)
  # I ended up not performing data augmentation as it decreased the accuracy of my model.
  return image, label
AUTOTUNE = tf.data.experimental.AUTOTUNE
# When choosing the number of parallel calls for next step, TensorFlow can decide the best value to optimize productivity. 
BATCH_SIZE = 16
counter = tf.data.experimental.Counter()
ds_train = tf.data.Dataset.zip((ds_train, (counter, counter)))
ds_train = ds_train.map(augment, num_parallel_calls = AUTOTUNE)
ds_train = ds_train.cache()
# This will allow some of the data to be kept in memory so it will be loaded more quickly next time
ds_train = ds_train.shuffle(ds_info.splits["train"].num_examples)
# Shuffles data randomly
ds_train = ds_train.batch(BATCH_SIZE)
ds_train = ds_train.prefetch(AUTOTUNE)
# Helps increase efficiency, will prepare some examples while the GPU is running
ds_validation = ds_validation.map(normalize_img, num_parallel_calls = AUTOTUNE)
ds_validation = ds_validation.batch(32)
ds_validation = ds_validation.prefetch(AUTOTUNE)
ds_test = ds_test.map(normalize_img, num_parallel_calls = AUTOTUNE)
ds_test = ds_test.batch(32)
ds_test = ds_test.prefetch(AUTOTUNE)
def create_model(image_size = 96):
  inception = InceptionV3(weights = 'imagenet',
                          include_top = False,
                          input_shape = (image_size, image_size, 3))
  
  for layer in inception.layers:
    layer.trainable = False

  model = Sequential()
  model.add(inception)
  model.add(GlobalAveragePooling2D())

  input = Input(shape=(image_size, image_size, 3))

  encoded = model(input)

  dense1 = Dense(256, activation='relu')(encoded)
  batch_normalization1 = BatchNormalization()(dense1)
  drop_layer = tf.keras.layers.Dropout(0.7)(batch_normalization1)
  dense2 = Dense(128, activation='relu')(drop_layer)
  batch_normalization2 = BatchNormalization()(dense2)
  drop_layer2 = tf.keras.layers.Dropout(0.7)(batch_normalization2)

  output = Dense(1, activation='sigmoid')(drop_layer2)
  model = Model(inputs=[input], outputs=output)

  return model
model = create_model()
model.summary()
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
              loss='binary_crossentropy',
              metrics=['accuracy'])
callbacks = [tf.keras.callbacks.EarlyStopping(monitor="val_loss" , patience=5 , verbose=1)]
model.fit(ds_train,
          batch_size = 16, 
          epochs = 20,
          verbose=1,
          validation_data = (ds_validation),
          callbacks = callbacks
          )
model.evaluate(ds_test)<jupyter_output>1024/1024 [==============================] - 31s 30ms/step - loss: 0.4280 - accuracy: 0.8017
{'fraction_non_alphanumeric': 0.10835471913316225, 'fraction_numerical': 0.021956087824351298, 'mean_word_length': 4.658064516129032, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '322840', 'license': 'MIT', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1132, 'n_words': 267, 'path': 'Detecting_Cancer_Metastases_on_Gigapixel_Pathology_Images.ipynb', 'repo_name': 'madisonpage050/PatchCamelyonModel', 'size': '3507'}