Compare commits
No commits in common. "b3e74b8c204ed896fa949c5d0e64ea9f0c96b632" and "cb1f94adc14e7cefc5f81305aca482a6fd9c9d05" have entirely different histories.
b3e74b8c20
...
cb1f94adc1
2 changed files with 0 additions and 383 deletions
|
@ -1,359 +0,0 @@
|
||||||
{
|
|
||||||
"cells": [
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"# Image Classification\n",
|
|
||||||
"\n",
|
|
||||||
"Simple image classification using the [CIFAR-10 dataset](https://www.cs.toronto.edu/~kriz/cifar.html).\n",
|
|
||||||
"\n",
|
|
||||||
"The CIFAR-10 dataset has 60,000 32x32 colour images in 10 classes (6,000 per class). These are split into 50,000 training images and 10,000 testing images.\n",
|
|
||||||
"\n",
|
|
||||||
"Here are the classes:\n",
|
|
||||||
"1. Airplane\n",
|
|
||||||
"2. Car\n",
|
|
||||||
"3. Bird\n",
|
|
||||||
"4. Cat\n",
|
|
||||||
"5. Deer\n",
|
|
||||||
"6. Dog\n",
|
|
||||||
"7. Frog\n",
|
|
||||||
"8. Horse\n",
|
|
||||||
"9. Ship\n",
|
|
||||||
"10. Truck"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"import matplotlib.pyplot as plt\n",
|
|
||||||
"import keras\n",
|
|
||||||
"import numpy as np\n",
|
|
||||||
"import os\n",
|
|
||||||
"from keras.src.datasets.cifar import load_batch\n",
|
|
||||||
"from keras import backend\n",
|
|
||||||
"from skimage.transform import resize\n",
|
|
||||||
"\n",
|
|
||||||
"classes = [\n",
|
|
||||||
" \"airplane\",\n",
|
|
||||||
" \"car\",\n",
|
|
||||||
" \"bird\",\n",
|
|
||||||
" \"cat\",\n",
|
|
||||||
" \"deer\",\n",
|
|
||||||
" \"dog\",\n",
|
|
||||||
" \"frog\",\n",
|
|
||||||
" \"horse\",\n",
|
|
||||||
" \"ship\",\n",
|
|
||||||
" \"truck\",\n",
|
|
||||||
"]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Load the dataset 💿"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"num_train_samples = 50000\n",
|
|
||||||
"\n",
|
|
||||||
"x_train = np.empty((num_train_samples, 3, 32, 32), dtype=\"uint8\")\n",
|
|
||||||
"y_train = np.empty((num_train_samples,), dtype=\"uint8\")\n",
|
|
||||||
"\n",
|
|
||||||
"for i in range(1, 6):\n",
|
|
||||||
" file_path = os.path.join(\"cifar-10-batches-py\", f\"data_batch_{i}\")\n",
|
|
||||||
" (\n",
|
|
||||||
" x_train[(i - 1) * 10000 : i * 10000, :, :, :],\n",
|
|
||||||
" y_train[(i - 1) * 10000 : i * 10000],\n",
|
|
||||||
" ) = load_batch(file_path)\n",
|
|
||||||
"\n",
|
|
||||||
"file_path = os.path.join(\"cifar-10-batches-py\", \"test_batch\")\n",
|
|
||||||
"x_test, y_test = load_batch(file_path)\n",
|
|
||||||
"\n",
|
|
||||||
"y_train = np.reshape(y_train, (len(y_train), 1))\n",
|
|
||||||
"y_test = np.reshape(y_test, (len(y_test), 1))\n",
|
|
||||||
"\n",
|
|
||||||
"if backend.image_data_format() == \"channels_last\":\n",
|
|
||||||
" x_train = x_train.transpose(0, 2, 3, 1)\n",
|
|
||||||
" x_test = x_test.transpose(0, 2, 3, 1)\n",
|
|
||||||
"\n",
|
|
||||||
"x_test = x_test.astype(x_train.dtype)\n",
|
|
||||||
"y_test = y_test.astype(y_train.dtype)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Exploring 🔎"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"print(x_train.shape)\n",
|
|
||||||
"print(y_train.shape)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"`x_train` is the actual images in the dataset. You can see they are 32x32 and the 3 is for red, green and blue values.\n",
|
|
||||||
"`y_train` is the category for each image, this is just a single number between 0 and 9."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"x_train[1]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"plt.imshow(x_train[1])\n",
|
|
||||||
"print(y_train[1])"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Processing 🫧\n",
|
|
||||||
"\n",
|
|
||||||
"Our neural network works with decimal numbers between 0 and 1, so we need to convert the categories into 0s and 1s. We take an array of 0s and set a 1 for the category.\n",
|
|
||||||
"\n",
|
|
||||||
"For example, the number 2 would get encoded to `[0, 0, 1, ...]`."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"y_train_one_hot = keras.src.utils.numerical_utils.to_categorical(y_train, 10)\n",
|
|
||||||
"y_test_one_hot = keras.src.utils.numerical_utils.to_categorical(y_test, 10)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# task: can you print out the one hot encoded label for the truck above?\n",
|
|
||||||
"print(y_train_one_hot[1])"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"At the moment each pixel is represented by a number from 0 to 255. We also need to convert these to be between 0 and 1."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"x_train = x_train.astype(\"float32\")\n",
|
|
||||||
"x_test = x_test.astype(\"float32\")\n",
|
|
||||||
"x_train = x_train / 255\n",
|
|
||||||
"x_test = x_test / 255"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"x_train[0]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Build and Train CNN 🔨"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"from keras.models import Sequential\n",
|
|
||||||
"from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\n",
|
|
||||||
"\n",
|
|
||||||
"model = Sequential()\n",
|
|
||||||
"model.add(\n",
|
|
||||||
" Conv2D(32, (3, 3), activation=\"relu\", padding=\"same\", input_shape=(32, 32, 3))\n",
|
|
||||||
")\n",
|
|
||||||
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
|
|
||||||
"model.add(Dropout(0.25))\n",
|
|
||||||
"model.add(Conv2D(64, (3, 3), activation=\"relu\", padding=\"same\"))\n",
|
|
||||||
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
|
|
||||||
"model.add(Dropout(0.25))\n",
|
|
||||||
"model.add(Flatten())\n",
|
|
||||||
"model.add(Dense(512, activation=\"relu\"))\n",
|
|
||||||
"model.add(Dropout(0.5))\n",
|
|
||||||
"model.add(Dense(10, activation=\"softmax\"))\n",
|
|
||||||
"\n",
|
|
||||||
"model.summary()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"hist = model.fit(\n",
|
|
||||||
" x_train, y_train_one_hot, batch_size=32, epochs=1, validation_split=0.2\n",
|
|
||||||
")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Evaluate 🧪"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"model.evaluate(x_test, y_test_one_hot)[1]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"~50% accuracy... not great"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"### What about for something it's not been trained on?\n",
|
|
||||||
"\n",
|
|
||||||
"Let's try and feed a picture of a cat to the model, and see what it thinks... As a reminder, the model hasn't been trained on pictures of cats."
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"cat = plt.imread(\"cat.jpg\")\n",
|
|
||||||
"cat_resized = resize(cat, (32, 32, 3))"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"plt.imshow(cat_resized)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"probabilities = model.predict(\n",
|
|
||||||
" np.array(\n",
|
|
||||||
" [\n",
|
|
||||||
" cat_resized,\n",
|
|
||||||
" ]\n",
|
|
||||||
" )\n",
|
|
||||||
")\n",
|
|
||||||
"probabilities"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"index = np.argsort(probabilities[0, :])\n",
|
|
||||||
"print(f\"Most likely: {classes[index[9]]}, probability={probabilities[0,index[9]]}\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Additional Challenges 🏆\n",
|
|
||||||
"\n",
|
|
||||||
"- Try adding in some more layers to the neural network, adding a second `Conv2D` layer under both of the existing ones.\n",
|
|
||||||
"- Try increasing the number of `epochs` when training.\n",
|
|
||||||
"- Save/load your model with `model.save('mymodel.h5')` and `keras.models.load_model('mymodel.h5')`."
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"metadata": {
|
|
||||||
"kernelspec": {
|
|
||||||
"display_name": "Python 3 (ipykernel)",
|
|
||||||
"language": "python",
|
|
||||||
"name": "python3"
|
|
||||||
},
|
|
||||||
"language_info": {
|
|
||||||
"codemirror_mode": {
|
|
||||||
"name": "ipython",
|
|
||||||
"version": 3
|
|
||||||
},
|
|
||||||
"file_extension": ".py",
|
|
||||||
"mimetype": "text/x-python",
|
|
||||||
"name": "python",
|
|
||||||
"nbconvert_exporter": "python",
|
|
||||||
"pygments_lexer": "ipython3",
|
|
||||||
"version": "3.10.12"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nbformat": 4,
|
|
||||||
"nbformat_minor": 4
|
|
||||||
}
|
|
24
Dockerfile
24
Dockerfile
|
@ -1,24 +0,0 @@
|
||||||
FROM docker.io/library/ubuntu:22.04
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND noninteractive
|
|
||||||
|
|
||||||
RUN apt-get update \
|
|
||||||
&& apt-get install -y --no-install-recommends apt-utils build-essential g++ curl cmake zlib1g-dev libjpeg-dev xvfb xorg-dev libboost-all-dev libsdl2-dev swig python3 python3-dev python3-future python3-pip python3-setuptools python3-wheel python3-tk libatlas-base-dev cython3 \
|
|
||||||
&& apt-get clean \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
RUN python3 -m pip install --upgrade pip \
|
|
||||||
&& python3 -m pip install jupyterlab keras==3.3.3 matplotlib==3.9.0 numpy==1.26.4 tensorflow==2.16.1 scikit-image==0.22.0 \
|
|
||||||
&& python3 -m pip install "gymnasium[box2d]==0.29.1" "stable-baselines3[extra]==2.3.2"
|
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y wget
|
|
||||||
|
|
||||||
WORKDIR /work
|
|
||||||
COPY . /work
|
|
||||||
RUN /work/download-data.sh \
|
|
||||||
&& rm /work/*_solutions.ipynb
|
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND teletype
|
|
||||||
|
|
||||||
CMD xvfb-run -s "-screen 0 1400x900x24" \
|
|
||||||
/usr/local/bin/jupyter lab --port 8888 --ip=0.0.0.0 --allow-root
|
|
Loading…
Reference in a new issue