Code
= load_all_images_3channel(
x, y =["sunny", "cloudy", "foggy", "rainy", "snowy"], pixels=PIXELS
classes )
has processed 18039
import random
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten
from keras.optimizers import SGD, RMSprop, Adam
from keras.utils import to_categorical
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import ConfusionMatrixDisplay
from utils import (
load_all_images_3channel,
get_class_name,
predict_image_3c,
CLASSES,
)
PIXELS = 200
# settings
plt.style.use('seaborn')
plt.show()
plt.rcParams["figure.dpi"] = 100
plt.rcParams["figure.figsize"] = (8, 5)
has processed 18039
((18039, 200, 200, 3), (18039,))
# shuffle and split
x_train, x_test, y_train, y_test = train_test_split(
x, y, train_size=0.7, shuffle=True, random_state=777
)
y_train_labels = y_train.copy()
y_test_labels = y_test.copy()
# convert class vector into binary matrix
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
x_train.shape, y_train.shape
((12627, 200, 200, 3), (12627, 5))
model = Sequential([
# feature detector portion of model
Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(PIXELS, PIXELS, 3)),
MaxPool2D(pool_size=(2, 2)),
Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
MaxPool2D(pool_size=(2, 2)),
Conv2D(filters=128, kernel_size=(3, 3), activation='relu'),
MaxPool2D(pool_size=(2, 2)),
Conv2D(filters=128, kernel_size=(3, 3), activation='relu'),
MaxPool2D(pool_size=(2, 2)),
# classifier portion of model
Flatten(),
Dense(units=512, activation='relu'),
Dense(units=5, activation='sigmoid'),
])
model.summary()
# set optimizer and metrics
model.compile(
optimizer=RMSprop(learning_rate=0.001, momentum=0.3),
loss="binary_crossentropy",
metrics=["accuracy"],
)
# train model
history = model.fit(
x_train,
y_train,
batch_size=64,
epochs=20,
validation_split=0.2,
callbacks=[
EarlyStopping(patience=3, min_delta=0.0001, restore_best_weights=True),
ReduceLROnPlateau(patience=1, factor=0.1),
],
)
170/170 [==============================] - 2s 11ms/step - loss: 0.2327 - accuracy: 0.7380
170/170 [==============================] - 2s 9ms/step
missed_features = x_test[pred_values != y_test_labels]
missed_labels = pred_values[pred_values != y_test_labels]
# first 10 missed predictions
fig, axs = plt.subplots(3, 5, sharex=True, sharey=True)
fig.tight_layout(h_pad=2)
plt.figure(figsize=(4, 7), dpi=100)
for i, ax in enumerate(axs.flatten()):
ax.imshow(missed_features[i])
ax.set_title(f'Predicted: {get_class_name(missed_labels[i])}')
plt.show()
<Figure size 400x700 with 0 Axes>
Metal device set to: Apple M1
systemMemory: 16.00 GB
maxCacheSize: 5.33 GB
2022-08-23 09:59:59.161478: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-08-23 09:59:59.161583: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
prob | |
---|---|
cloudy | 90.199997 |
rainy | 3.400000 |
sunny | 3.200000 |
snowy | 0.500000 |
foggy | 0.400000 |