Image Classifications
Image Classifications
tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np
(X_train, y_train),(X_test,y_test) = datasets.cifar10.load_data()
X_train.shape
X_test.shape
X_test
[[ 12, 25, 6],
[ 20, 37, 7],
[ 24, 36, 15],
...,
[115, 134, 138],
[149, 168, 177],
[104, 117, 131]],
...,
plt.imshow(X_train[1])
<matplotlib.image.AxesImage at 0x7fb5ea7f0c10>
print(y_train[:5])
[[6]
[9]
[9]
[4]
[1]]
y_train = y_train.reshape(-1,)
y_train[:5]
classes =["airplane" , "automobile" , "bird" , "cat", "deer", 'dog','frog',"horse" "ship" , "truck"]
def plot_samples(X,y, index):
plt.figure(figsize=(15, 2))
plt.imshow(X[index])
plt.xlabel(classes[y[index]])
plot_samples(X_train,y_train,0)
X_train = X_train/255
X_test = X_test/255
ann_model = models.Sequential([
layers.Flatten(input_shape=(32,32,3)),
layers.Dense(3000,activation='relu'),
layers.Dense(1000,activation='relu'),
layers.Dense(10,activation='sigmoid')
])
ann_model.compile(optimizer='SGD',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
ann_model.fit(X_train, y_train, epochs=5)
Epoch 1/5
1563/1563 [==============================] - 13s 5ms/step - loss: 1.8142 - accuracy: 0.3542
Epoch 2/5
1563/1563 [==============================] - 7s 5ms/step - loss: 1.6250 - accuracy: 0.4262
Epoch 3/5
1563/1563 [==============================] - 7s 4ms/step - loss: 1.5436 - accuracy: 0.4563
Epoch 4/5
1563/1563 [==============================] - 7s 5ms/step - loss: 1.4811 - accuracy: 0.4796
Epoch 5/5
1563/1563 [==============================] - 7s 4ms/step - loss: 1.4345 - accuracy: 0.4949
<keras.callbacks.History at 0x7fb56c106580>
from sklearn.metrics import confusion_matrix, classification_report
import numpy as np
y_pred = ann_model.predict(X_test)
y_pred_classes = [np.argmax(element) for element in y_pred]
print("Classification Report:", classification_report(y_test,y_pred_classes))
cnn_model = models.Sequential([
layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(32,32,3)),
layers.MaxPool2D((2,2)),
layers.Flatten(),
layers.Dense(3000,activation='relu'),
layers.Dense(10,activation='softmax')
])
cnn_model.compile(optimizer='Adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
cnn_model.fit(X_train,y_train,epochs=5,batch_size=32)
Epoch 1/5
1563/1563 [==============================] - 18s 8ms/step - loss: 1.3959 - accuracy: 0.5071
Epoch 2/5
1563/1563 [==============================] - 12s 7ms/step - loss: 1.0267 - accuracy: 0.6390
Epoch 3/5
1563/1563 [==============================] - 12s 7ms/step - loss: 0.7931 - accuracy: 0.7206
Epoch 4/5
1563/1563 [==============================] - 12s 7ms/step - loss: 0.5650 - accuracy: 0.8042
Epoch 5/5
1563/1563 [==============================] - 12s 8ms/step - loss: 0.3639 - accuracy: 0.8758
<keras.callbacks.History at 0x7fb4eec543a0>
np.argmax(y_pred[0])
print("Classification Report:\n", classification_report(y_test,y_pred_classes))
Classification Report:
precision recall f1-score support
lt i h (X t i [9])
plt.imshow(X_train[9])
<matplotlib.image.AxesImage at 0x7fb4c82d1340>