Skip to content
Snippets Groups Projects
Commit b8d3066d authored by Patrick Scheidegger's avatar Patrick Scheidegger
Browse files

.ipynb_checkpoints folder removed

parent d41fe38f
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:1e23f8e1 tags:
# AI Applications Graded Miniproject 1: Deep Learning, multiclass image classification
%% Cell type:markdown id:d2161a24 tags:
Team: Scheidegger, Patrick & Grob, Nils-Robin
%% Cell type:markdown id:4d9cfc22 tags:
## Dataset
We chose following opensource [dataset](https://www.kaggle.com/datasets/alessiocorrado99/animals10) from alessiocorrado99.
The dataset contains 10 classes of animals, namely dogs, cats, sheeps, elefants, horses, butterflies, chickens, and squirrels.
There are approx. 25'000 images in the dataset.
%% Cell type:code id:4f9c1d76 tags:
``` python
!pip install tensorflow
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
```
%% Output
Collecting tensorflow
Downloading tensorflow-2.11.0-cp310-cp310-win_amd64.whl (1.9 kB)
Collecting tensorflow-intel==2.11.0
Downloading tensorflow_intel-2.11.0-cp310-cp310-win_amd64.whl (266.3 MB)
------------------------------------- 266.3/266.3 MB 10.6 MB/s eta 0:00:00
Collecting tensorflow-io-gcs-filesystem>=0.23.1
Downloading tensorflow_io_gcs_filesystem-0.31.0-cp310-cp310-win_amd64.whl (1.5 MB)
---------------------------------------- 1.5/1.5 MB 47.6 MB/s eta 0:00:00
Collecting tensorflow-estimator<2.12,>=2.11.0
Downloading tensorflow_estimator-2.11.0-py2.py3-none-any.whl (439 kB)
------------------------------------- 439.2/439.2 kB 28.6 MB/s eta 0:00:00
Collecting protobuf<3.20,>=3.9.2
Downloading protobuf-3.19.6-cp310-cp310-win_amd64.whl (895 kB)
------------------------------------- 895.7/895.7 kB 55.3 MB/s eta 0:00:00
Requirement already satisfied: packaging in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (21.3)
Collecting opt-einsum>=2.3.2
Downloading opt_einsum-3.3.0-py3-none-any.whl (65 kB)
---------------------------------------- 65.5/65.5 kB ? eta 0:00:00
Requirement already satisfied: setuptools in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (63.4.1)
Collecting tensorboard<2.12,>=2.11
Downloading tensorboard-2.11.2-py3-none-any.whl (6.0 MB)
---------------------------------------- 6.0/6.0 MB 34.8 MB/s eta 0:00:00
Collecting flatbuffers>=2.0
Downloading flatbuffers-23.1.21-py2.py3-none-any.whl (26 kB)
Collecting libclang>=13.0.0
Downloading libclang-15.0.6.1-py2.py3-none-win_amd64.whl (23.2 MB)
--------------------------------------- 23.2/23.2 MB 26.2 MB/s eta 0:00:00
Collecting h5py>=2.9.0
Downloading h5py-3.8.0-cp310-cp310-win_amd64.whl (2.6 MB)
---------------------------------------- 2.6/2.6 MB 33.6 MB/s eta 0:00:00
Collecting termcolor>=1.1.0
Downloading termcolor-2.2.0-py3-none-any.whl (6.6 kB)
Requirement already satisfied: typing-extensions>=3.6.6 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (4.3.0)
Collecting absl-py>=1.0.0
Downloading absl_py-1.4.0-py3-none-any.whl (126 kB)
-------------------------------------- 126.5/126.5 kB 7.3 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.20 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.23.3)
Requirement already satisfied: six>=1.12.0 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.16.0)
Collecting astunparse>=1.6.0
Downloading astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Collecting gast<=0.4.0,>=0.2.1
Downloading gast-0.4.0-py3-none-any.whl (9.8 kB)
Collecting google-pasta>=0.1.1
Downloading google_pasta-0.2.0-py3-none-any.whl (57 kB)
---------------------------------------- 57.5/57.5 kB ? eta 0:00:00
Collecting keras<2.12,>=2.11.0
Downloading keras-2.11.0-py2.py3-none-any.whl (1.7 MB)
---------------------------------------- 1.7/1.7 MB 54.0 MB/s eta 0:00:00
Collecting grpcio<2.0,>=1.24.3
Downloading grpcio-1.51.3-cp310-cp310-win_amd64.whl (3.7 MB)
---------------------------------------- 3.7/3.7 MB 39.6 MB/s eta 0:00:00
Collecting wrapt>=1.11.0
Downloading wrapt-1.15.0-cp310-cp310-win_amd64.whl (36 kB)
Requirement already satisfied: wheel<1.0,>=0.23.0 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from astunparse>=1.6.0->tensorflow-intel==2.11.0->tensorflow) (0.37.1)
Collecting google-auth<3,>=1.6.3
Downloading google_auth-2.16.1-py2.py3-none-any.whl (177 kB)
------------------------------------- 177.2/177.2 kB 10.4 MB/s eta 0:00:00
Requirement already satisfied: requests<3,>=2.21.0 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.28.1)
Collecting tensorboard-data-server<0.7.0,>=0.6.0
Downloading tensorboard_data_server-0.6.1-py3-none-any.whl (2.4 kB)
Collecting markdown>=2.6.8
Downloading Markdown-3.4.1-py3-none-any.whl (93 kB)
---------------------------------------- 93.3/93.3 kB 5.2 MB/s eta 0:00:00
Collecting tensorboard-plugin-wit>=1.6.0
Downloading tensorboard_plugin_wit-1.8.1-py3-none-any.whl (781 kB)
------------------------------------- 781.3/781.3 kB 51.4 MB/s eta 0:00:00
Collecting google-auth-oauthlib<0.5,>=0.4.1
Downloading google_auth_oauthlib-0.4.6-py2.py3-none-any.whl (18 kB)
Collecting werkzeug>=1.0.1
Downloading Werkzeug-2.2.3-py3-none-any.whl (233 kB)
------------------------------------- 233.6/233.6 kB 14.0 MB/s eta 0:00:00
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from packaging->tensorflow-intel==2.11.0->tensorflow) (3.0.9)
Collecting rsa<5,>=3.1.4
Using cached rsa-4.9-py3-none-any.whl (34 kB)
Collecting pyasn1-modules>=0.2.1
Using cached pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
Collecting cachetools<6.0,>=2.0.0
Downloading cachetools-5.3.0-py3-none-any.whl (9.3 kB)
Collecting requests-oauthlib>=0.7.0
Downloading requests_oauthlib-1.3.1-py2.py3-none-any.whl (23 kB)
Requirement already satisfied: charset-normalizer<3,>=2 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.26.11)
Requirement already satisfied: idna<4,>=2.5 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2022.9.24)
Requirement already satisfied: MarkupSafe>=2.1.1 in c:\users\pat\miniconda3\envs\aifo\lib\site-packages (from werkzeug>=1.0.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.1.1)
Collecting pyasn1<0.5.0,>=0.4.6
Using cached pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
Collecting oauthlib>=3.0.0
Downloading oauthlib-3.2.2-py3-none-any.whl (151 kB)
---------------------------------------- 151.7/151.7 kB ? eta 0:00:00
Installing collected packages: tensorboard-plugin-wit, pyasn1, libclang, flatbuffers, wrapt, werkzeug, termcolor, tensorflow-io-gcs-filesystem, tensorflow-estimator, tensorboard-data-server, rsa, pyasn1-modules, protobuf, opt-einsum, oauthlib, markdown, keras, h5py, grpcio, google-pasta, gast, cachetools, astunparse, absl-py, requests-oauthlib, google-auth, google-auth-oauthlib, tensorboard, tensorflow-intel, tensorflow
Successfully installed absl-py-1.4.0 astunparse-1.6.3 cachetools-5.3.0 flatbuffers-23.1.21 gast-0.4.0 google-auth-2.16.1 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.51.3 h5py-3.8.0 keras-2.11.0 libclang-15.0.6.1 markdown-3.4.1 oauthlib-3.2.2 opt-einsum-3.3.0 protobuf-3.19.6 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-oauthlib-1.3.1 rsa-4.9 tensorboard-2.11.2 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.1 tensorflow-2.11.0 tensorflow-estimator-2.11.0 tensorflow-intel-2.11.0 tensorflow-io-gcs-filesystem-0.31.0 termcolor-2.2.0 werkzeug-2.2.3 wrapt-1.15.0
C:\Users\Pat\miniconda3\envs\AIFO\lib\site-packages\scipy\__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.23.3
warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
%% Cell type:code id:3b128ab4 tags:
``` python
```
%% Cell type:code id:9237f546 tags:
``` python
!pip install mplcyberpunk
!pip install split-folders
import cv2
import matplotlib.pyplot as plt
import pathlib, os, random, mplcyberpunk, math, splitfolders
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras import Sequential
```
%% Output
Collecting mplcyberpunk
Downloading mplcyberpunk-0.6.0-py3-none-any.whl (6.0 kB)
Requirement already satisfied: matplotlib in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from mplcyberpunk) (3.5.2)
Requirement already satisfied: packaging>=20.0 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from matplotlib->mplcyberpunk) (21.3)
Requirement already satisfied: fonttools>=4.22.0 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from matplotlib->mplcyberpunk) (4.25.0)
Requirement already satisfied: python-dateutil>=2.7 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from matplotlib->mplcyberpunk) (2.8.2)
Requirement already satisfied: pillow>=6.2.0 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from matplotlib->mplcyberpunk) (9.2.0)
Requirement already satisfied: numpy>=1.17 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from matplotlib->mplcyberpunk) (1.23.3)
Requirement already satisfied: cycler>=0.10 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from matplotlib->mplcyberpunk) (0.11.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from matplotlib->mplcyberpunk) (1.4.2)
Requirement already satisfied: pyparsing>=2.2.1 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from matplotlib->mplcyberpunk) (3.0.9)
Requirement already satisfied: six>=1.5 in /Users/nilsrobingrob/miniconda3/envs/AIFO/lib/python3.9/site-packages (from python-dateutil>=2.7->matplotlib->mplcyberpunk) (1.16.0)
Installing collected packages: mplcyberpunk
Successfully installed mplcyberpunk-0.6.0
Collecting split-folders
Downloading split_folders-0.5.1-py3-none-any.whl (8.4 kB)
Installing collected packages: split-folders
Successfully installed split-folders-0.5.1
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Input In [6], in <cell line: 4>()
1 get_ipython().system('pip install mplcyberpunk')
2 get_ipython().system('pip install split-folders')
----> 4 import cv2
5 import matplotlib.pyplot as plt
6 import pathlib, os, random, mplcyberpunk, math, splitfolders
ModuleNotFoundError: No module named 'cv2'
%% Cell type:code id:6c8ea912 tags:
``` python
epochs = 30
batch_size = 32
img_height, img_width = 400, 400
input_shape = (img_height, img_width, 3)
def create_data_sparse(data_bs):
data_bs = pathlib.Path(data_bs)
splitfolders.ratio(data_bs, output='Imgs/', seed=1234, ratio=(0.7, 0.15, 0.15), group_prefix=None)
data_gen = ImageDataGenerator(rescale=1.0 / 255)
train_ds = data_gen.flow_from_directory('Imgs/train/', target_size=(img_height, img_width),
class_mode='sparse', batch_size=batch_size, subset='training')
val_ds = data_gen.flow_from_directory('Imgs/val/', target_size=(img_height, img_width),
class_mode='sparse', batch_size=batch_size, shuffle=False)
return train_ds, val_ds
train_ds, val_ds = create_data_sparse('/kaggle/input/animals10/raw-img/')
```
%% Output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [7], in <cell line: 18>()
13 val_ds = data_gen.flow_from_directory('Imgs/val/', target_size=(img_height, img_width),
14 class_mode='sparse', batch_size=batch_size, shuffle=False)
16 return train_ds, val_ds
---> 18 train_ds, val_ds = create_data_sparse('/kaggle/input/animals10/raw-img/')
Input In [7], in create_data_sparse(data_bs)
7 def create_data_sparse(data_bs):
----> 8 data_bs = pathlib.Path(data_bs)
9 splitfolders.ratio(data_bs, output='Imgs/', seed=1234, ratio=(0.7, 0.15, 0.15), group_prefix=None)
10 data_gen = ImageDataGenerator(rescale=1.0 / 255)
NameError: name 'pathlib' is not defined
%% Cell type:code id:b4710bff tags:
``` python
class BaseModel(tf.keras.Model):
def __init__(self):
super(BaseModel, self).__init__()
self.C1 = Conv2D(64, (3 * 3), padding='same', input_shape=input_shape)
self.B1 = BatchNormalization()
self.A1 = Activation('relu')
self.P1 = MaxPooling2D(2, padding='same')
self.C2 = Conv2D(32, (3 * 3), padding='same')
self.B2 = BatchNormalization()
self.A2 = Activation('relu')
self.P2 = MaxPooling2D(2, padding='same')
self.Dr1 = Dropout(0.2)
self.C3 = Conv2D(32, (3 * 3), padding='same')
self.B3 = BatchNormalization()
self.A3 = Activation('relu')
self.P3 = MaxPooling2D(2, padding='same')
self.Dr2 = Dropout(0.3)
self.F1 = Flatten()
self.D1 = Dense(512, activation='relu')
self.B4 = BatchNormalization()
self.D2 = Dense(256, activation='relu')
self.D3 = Dense(256, activation='relu')
self.D4 = Dense(10, activation='softmax')
def call(self, x):
x = self.C1(x)
x = self.B1(x)
x = self.A1(x)
x = self.P1(x)
x = self.C2(x)
x = self.B2(x)
x = self.A2(x)
x = self.P2(x)
x = self.Dr1(x)
x = self.C3(x)
x = self.B3(x)
x = self.A3(x)
x = self.P3(x)
x = self.Dr2(x)
x = self.F1(x)
x = self.D1(x)
x = self.B4(x)
x = self.D2(x)
x = self.D3(x)
y = self.D4(x)
return y
def __repr__(self):
return 'Huang_Model'
net = BaseModel()
net.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
checkpoint_save_path = './BaseModel.ckpt'
if os.path.exists(checkpoint_save_path + '.index'):
net.load_weights(checkpoint_save_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path, save_weights_only=True, save_best_only=True)
history = net.fit(train_ds, epochs=epochs, batch_size=batch_size, validation_data=val_ds, callbacks=[cp_callback])
net.summary()
file = open('./BaseModelWeights.txt', 'w')
for v in net.trainable_variables:
file.write(str(v.name) + '\n')
file.write(str(v.shape) + '\n')
file.write(str(v.numpy()) + '\n')
file.close()
```
%% Output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [8], in <cell line: 1>()
----> 1 class BaseModel(tf.keras.Model):
2 def __init__(self):
3 super(BaseModel, self).__init__()
NameError: name 'tf' is not defined
%% Cell type:code id:80eb7f9d tags:
``` python
acc = history.history['sparse_categorical_accuracy']
val_acc = history.history['val_sparse_categorical_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.figure(figsize=(12, 6))
plt.style.use('cyberpunk')
plt.subplot(1, 2, 1)
plt.plot(acc, label='Training Acc')
plt.plot(val_acc, label='Validation Acc')
plt.title('Training And Validation Acc')
plt.legend()
mplcyberpunk.add_glow_effects()
plt.subplot(1, 2, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training And Validation Loss')
plt.legend()
mplcyberpunk.add_glow_effects()
plt.show()
```
%% Output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 acc = history.history['sparse_categorical_accuracy']
2 val_acc = history.history['val_sparse_categorical_accuracy']
3 loss = history.history['loss']
NameError: name 'history' is not defined
%% Cell type:code id:a19d17ff tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment