Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from tensorflow import keras | |
from keras import layers | |
inputs = keras.Input(shape=(256, 256, 3)) | |
x = layers.Rescaling(1./255)(inputs) | |
x = layers.Conv2D(filters=32, kernel_size=3, activation=keras.activations.relu)(x) | |
x = layers.MaxPooling2D(pool_size=2)(x) | |
x = layers.Conv2D(filters=64, kernel_size=3, activation=keras.activations.relu)(x) | |
x = layers.MaxPooling2D(pool_size=2)(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.keras.utils import image_dataset_from_directory | |
test_dataset = image_dataset_from_directory( | |
new_base_dir / "test", | |
image_size=(256, 256), | |
batch_size=32 | |
) | |
train_dataset = image_dataset_from_directory( | |
new_base_dir / "train", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
callbacks = [ | |
keras.callbacks.ModelCheckpoint( | |
filepath=f"{hotDogDir}hotdog_classifier_v1.keras", | |
monitor="val_loss", | |
save_best_only=True | |
) | |
] | |
history = model.fit( | |
train_dataset, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test_model = keras.models.load_model(f'{hotDogDir}hotdog_classifier_v1.keras') | |
test_loss, test_acc = test_model.evaluate(test_dataset) | |
print(f"Test Acc: {test_acc:.3f}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_augmentation = keras.Sequential( | |
[ | |
layers.RandomRotation(0.3), | |
layers.RandomZoom(0.2), | |
layers.RandomFlip("horizontal"), | |
] | |
) | |
plt.figure(figsize=(10, 10)) | |
for images, _ in test_dataset.take(1): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inputs = keras.Input(shape=(256, 256, 3)) | |
x = data_augmentation(inputs) | |
x = layers.Rescaling(1./255)(x) | |
x = layers.Conv2D(filters=32, kernel_size=3, activation=keras.activations.relu)(x) | |
x = layers.MaxPooling2D(pool_size=2)(x) | |
x = layers.Conv2D(filters=64, kernel_size=3, activation=keras.activations.relu)(x) | |
x = layers.MaxPooling2D(pool_size=2)(x) | |
x = layers.Conv2D(filters=128, kernel_size=3, activation=keras.activations.relu)(x) | |
x = layers.MaxPooling2D(pool_size=2)(x) | |
x = layers.Conv2D(filters=256, kernel_size=3, activation=keras.activations.relu)(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
conv_base = keras.applications.vgg16.VGG16( | |
weights="imagenet", | |
include_top=False, | |
input_shape=(256, 256, 3) | |
) | |
conv_base.summary() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def get_features_and_labels(dataset): | |
all_features = [] | |
all_labels = [] | |
for images, labels in dataset: | |
preprocessed_images = keras.applications.vgg16.preprocess_input(images) | |
features = conv_base.predict(preprocessed_images) | |
all_features.append(features) | |
all_labels.append(labels) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inputs = keras.Input(shape=(8, 8, 512)) | |
x = layers.Flatten()(inputs) | |
x = layers.Dense(256)(x) | |
x = layers.Dropout(0.5)(x) | |
outputs = layers.Dense(1, activation= keras.activations.sigmoid)(x) | |
model = keras.Model(inputs, outputs) | |
model.compile(optimizer=keras.optimizers.RMSprop(), | |
loss=keras.losses.BinaryCrossentropy(), | |
metrics=["accuracy"]) |
OlderNewer