2022-03-30 08:59:31 +00:00
{
2022-03-30 09:48:21 +00:00
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import PIL.Image\n",
"import glob\n",
"import os\n",
"\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
2022-03-30 08:59:31 +00:00
{
2022-03-30 09:48:21 +00:00
"name": "stdout",
"output_type": "stream",
"text": [
"/tmp/deepl/dataset\n",
"['octane', 'werewolf', 'breakout', 'aftershock']\n"
]
}
],
"source": [
"IMAGE_SIZE = (400, 150, 3)\n",
"RESIZED_SIZE = (100, 50, 3)\n",
"RESIZED_SIZE_PIL = (RESIZED_SIZE[1], RESIZED_SIZE[0], RESIZED_SIZE[2])\n",
"DATASET_PATH = \"./dataset/\"\n",
"DATASET_PATH = os.path.abspath(DATASET_PATH)\n",
"CLASSES = glob.glob(f\"{DATASET_PATH}/*\")\n",
"CLASSES = list(map(lambda f: f.split(\"/\")[-1], CLASSES))\n",
"\n",
"print(DATASET_PATH)\n",
"print(CLASSES)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def load_data():\n",
" # Récupération des fichiers\n",
" files = glob.glob(f\"{DATASET_PATH}/**/*.jpg\", recursive = True)\n",
"\n",
" # Initialise les structures de données\n",
" x = np.zeros((len(files), *RESIZED_SIZE_PIL))\n",
" y = np.zeros((len(files), 1))\n",
"\n",
" # print(f\"x.shape = {x.shape}\")\n",
"\n",
" for i, path in enumerate(files):\n",
" # Lecture de l'image\n",
" img = PIL.Image.open(path)\n",
"\n",
" # print(f\"img.size = {img.size}\")\n",
"\n",
" # Redimensionnement de l'image\n",
" img = img.resize(RESIZED_SIZE[:-1], PIL.Image.ANTIALIAS)\n",
"\n",
" # print(f\"img.size = {img.size}\")\n",
"\n",
" test = np.asarray(img)\n",
"\n",
" # print(f\"test.shape = {test.shape}\")\n",
"\n",
" # Remplissage de la variable x\n",
" x[i] = test\n",
"\n",
" # On récupère l'index dans le path\n",
" class_label = path.split(\"/\")[-2]\n",
"\n",
" # On récupère le numéro de la classe à partir du string\n",
" class_label = CLASSES.index(class_label)\n",
" \n",
" # Remplissage de la variable y\n",
" y[i] = class_label\n",
"\n",
" return x, y"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"x, y = load_data()\n",
"x = x / 255"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAxoAAAGoCAYAAADB3ZMFAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOz9adBtS5rfhf2ezFxr7eEdznjHunWrqqu61WoNLTFIYGEwxhiHBQ5/AXkIQrIDmQ84PAgDJgIbh40sY4QjHA5CSBFyOAIsKWwctiwIkBqhsCYaQWvoQd2qoe+tuuMZ3vMOe1hDZj7+kJlrrf2ec25Xdd861VLvp7vueffea16ZTz7//zOJqnKUoxzlKEc5ylGOcpSjHOUon6eYH/QFHOUoRznKUY5ylKMc5ShH+TtPjkDjKEc5ylGOcpSjHOUoRznK5y5HoHGUoxzlKEc5ylGOcpSjHOVzlyPQOMpRjnKUoxzlKEc5ylGO8rnLEWgc5ShHOcpRjnKUoxzlKEf53OUINI5ylKMc5ShHOcpRjnKUo3zucgQav8pERP53IvJERD55Bed6T0T+kc/5mP+qiPw7n+cxj3KUoxzlKEc5ylGO8refHIHGryIRkS8Cvw/49ar6hoj8bhH5Cz/o6zrKUY5ylBeJiHxJRFRE3A/6Wo5ylKP8nS1HffO3pxyBxq8u+SLwVFUffR4HO07GoxzlKEc5ylGOcpSj/KDkCDR+ACIi/5KIfFNEbkTk50Tkv51DmP4M8JaIb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DXbPkH/kd/yX+3E9/i857mqpGBXw28sxcyWZwWz4bEewcWCR0iRGTdIiYRBJkFWEqgzEGZ9PvlTNJOVubdI2rcNayWi2IRIbYE4P
"text/plain": [
"<Figure size 864x432 with 9 Axes>"
2022-03-30 08:59:31 +00:00
]
2022-03-30 09:48:21 +00:00
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# Randomisation des indices et affichage de 9 images alétoires de la base d'apprentissage\n",
"indices = np.arange(x.shape[0])\n",
"np.random.shuffle(indices)\n",
"\n",
"plt.figure(figsize=(12, 6))\n",
"\n",
"for i in range(0, 3*3):\n",
" plt.subplot(3, 3, i+1)\n",
" plt.title(CLASSES[int(y[indices[i]])])\n",
" plt.imshow(x[indices[i]])\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import InputLayer, Dense, Flatten, Conv2D, MaxPooling2D\n",
"from tensorflow.keras import optimizers"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\n",
" conv2d (Conv2D) (None, 49, 99, 32) 416 \n",
" \n",
" max_pooling2d (MaxPooling2D (None, 24, 49, 32) 0 \n",
" ) \n",
" \n",
" conv2d_1 (Conv2D) (None, 23, 48, 64) 8256 \n",
" \n",
" max_pooling2d_1 (MaxPooling (None, 11, 24, 64) 0 \n",
" 2D) \n",
" \n",
" flatten (Flatten) (None, 16896) 0 \n",
" \n",
" dense (Dense) (None, 50) 844850 \n",
" \n",
" dense_1 (Dense) (None, 4) 204 \n",
" \n",
"=================================================================\n",
"Total params: 853,726\n",
"Trainable params: 853,726\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
2022-03-30 08:59:31 +00:00
},
{
2022-03-30 09:48:21 +00:00
"name": "stderr",
"output_type": "stream",
"text": [
"2022-03-30 11:46:58.052420: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n",
"To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2022-03-30 11:46:58.671001: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1538 MB memory: -> device: 0, name: Quadro K620, pci bus id: 0000:03:00.0, compute capability: 5.0\n"
]
2022-03-30 08:59:31 +00:00
},
{
2022-03-30 09:48:21 +00:00
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/5\n"
]
2022-03-30 08:59:31 +00:00
},
{
2022-03-30 09:48:21 +00:00
"name": "stderr",
"output_type": "stream",
"text": [
"2022-03-30 11:47:00.113976: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8100\n",
"2022-03-30 11:47:00.378978: W tensorflow/stream_executor/gpu/asm_compiler.cc:111] *** WARNING *** You are using ptxas 10.1.243, which is older than 11.1. ptxas before 11.1 is known to miscompile XLA code, leading to incorrect results or invalid-address errors.\n",
"\n",
"You may not need to update to CUDA 11.1; cherry-picking the ptxas binary is often sufficient.\n"
]
2022-03-30 08:59:31 +00:00
},
{
2022-03-30 09:48:21 +00:00
"name": "stdout",
"output_type": "stream",
"text": [
"320/320 [==============================] - 6s 15ms/step - loss: 1.2562 - accuracy: 0.4588 - val_loss: 2.1222 - val_accuracy: 0.0000e+00\n",
"Epoch 2/5\n",
"320/320 [==============================] - 5s 15ms/step - loss: 1.0819 - accuracy: 0.8850 - val_loss: 2.3470 - val_accuracy: 0.0000e+00\n",
"Epoch 3/5\n",
"320/320 [==============================] - 5s 15ms/step - loss: 0.9304 - accuracy: 0.9375 - val_loss: 2.3578 - val_accuracy: 0.0000e+00\n",
"Epoch 4/5\n",
"320/320 [==============================] - 5s 15ms/step - loss: 0.7820 - accuracy: 0.9375 - val_loss: 2.1716 - val_accuracy: 0.0000e+00\n",
"Epoch 5/5\n",
"320/320 [==============================] - 5s 15ms/step - loss: 0.6454 - accuracy: 0.9375 - val_loss: 2.0182 - val_accuracy: 0.0000e+00\n"
]
2022-03-30 08:59:31 +00:00
}
2022-03-30 09:48:21 +00:00
],
"source": [
"model = Sequential()\n",
"\n",
"model.add(InputLayer(input_shape=RESIZED_SIZE_PIL))\n",
"\n",
"model.add(Conv2D(32, 2, activation=\"relu\"))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"\n",
"model.add(Conv2D(64, 2, activation=\"relu\"))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"\n",
"model.add(Flatten())\n",
"\n",
"model.add(Dense(50, activation=\"relu\"))\n",
"\n",
"model.add(Dense(4, activation=\"softmax\"))\n",
"\n",
"model.summary()\n",
"\n",
"adam = optimizers.Adam(learning_rate=3e-6)\n",
"model.compile(optimizer=adam, loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n",
"history = model.fit(x, y, validation_split=0.2, epochs=5, batch_size=10)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".env",
"language": "python",
"name": ".env"
},
"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.8.10"
2022-03-30 08:59:31 +00:00
},
2022-03-30 09:48:21 +00:00
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
2022-03-30 08:59:31 +00:00
}