MH0386’s Jupyter Notebooks
  • Home
  1. Notebooks
  2. Deep Learning
  3. Inception Network
  • Notebooks
    • Deep Learning
      • Create Autoencoder Model
      • Inception Network
      • Word2vec
    • Fine Tuning
      • Data
      • Data
    • Machine Learning
      • Iris Classification from Scratch
      • Using Chain Code
      • Loading Data
      • Imports
      • Model
      • Naive Bayes from Built-in
    • Pre Processing
      • Text Processing
    • Rag
      • Try Docling
  1. Notebooks
  2. Deep Learning
  3. Inception Network

  • Show All Code
  • Hide All Code

Kaggle

Code
from os import listdir

from kagglehub import dataset_download
from keras import Model
from keras.src.applications.inception_v3 import InceptionV3
from keras.src.layers import Dense, GlobalAveragePooling2D
from keras.src.utils import load_img, to_categorical
from matplotlib import pyplot
from numpy import argmax, asarray, zeros
from PIL import Image
from seaborn import heatmap
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import train_test_split
Code
dataset_path = dataset_download("abhinavnayak/catsvdogs-transformed")
print("Path to dataset files:", dataset_path)
Code
data_path: str = f"{dataset_path}/train_transformed/"
print(data_path)
Code
list_of_images = listdir(data_path)
number_of_images: int = len(list_of_images)
print(number_of_images)
Code
i = 1
pyplot.figure(figsize=(10, 10))
for image in list_of_images:
    if i > 5:
        break
    img = load_img(data_path + image)
    pyplot.subplot(1, 5, i)
    pyplot.imshow(img)
    pyplot.title(str(img.size))
    pyplot.axis("off")
    i += 1

pyplot.show()
Code
images = zeros((number_of_images, 224, 224, 3))
labels = zeros((number_of_images, 1))
Code
for image_number, image_path in enumerate(list_of_images):
    img = Image.open(data_path + image_path)
    img = asarray(img)
    img = img / img.max()
    images[image_number] = img
    labels[image_number] = 0 if "cat" in image_path else 1
Code
print(labels)
Code
labels = to_categorical(labels)
Code
print(labels)
Code
images.shape
Code
pyplot.imshow(images[0])
Code
x_train, x_test, y_train, y_test = train_test_split(
    images, labels, test_size=0.2, random_state=42
)
Code
x_train.shape, x_test.shape, y_train.shape, y_test.shape
Code
x_train, x_test = x_train / 255.0, x_test / 255.0
Code
model = InceptionV3(
    include_top=False,
    weights="imagenet",
    input_shape=(224, 224, 3),
)
Code
# Freeze the pre-trained layers
for layer in model.layers:
    layer.trainable = False
Code
# Add layers using calls:
x = model.output  # Get the output of the InceptionV3 base
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation="relu")(x)
predictions = Dense(2, activation="softmax")(x)
Code
# Create a new Functional model with these layers:
model = Model(inputs=model.input, outputs=predictions)
Code
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
Code
history = model.fit(x_train, y_train, epochs=50)
Code
pyplot.plot(history.history["accuracy"])
pyplot.plot(history.history["loss"])
pyplot.title("Model Accuracy")
pyplot.ylabel("Accuracy")
pyplot.xlabel("Epoch")
pyplot.show()
Code
model.evaluate(x_test, y_test)
Code
y_predict = model.predict(x_test)
Code
y_predict = argmax(y_predict, axis=1)
y_test_argmax = argmax(y_test, axis=1)
Code
cm = confusion_matrix(y_test_argmax, y_predict)
heatmap(cm, annot=True, fmt="d")
Code
cr = classification_report(y_test_argmax, y_predict)
print(cr)
Back to top
Create Autoencoder Model
Word2vec

Mohamed Hisham Abdelzaher

 
  • Edit this page
  • View source
  • Report an issue