Capstone Code
Capstone Code
%matplotlib inline
[50]: training_im_list_ronaldo = []
[51]: training_im_list_musk = []
[52]: training_im_list_oprah = []
1
[53]: test_im_list_oprah = []
[54]: test_im_list_musk = []
[55]: test_im_list_ronaldo = []
[ ]:
[58]: random.shuffle(test_im_list_combined)
random.shuffle(training_im_list_combined)
[59]: # This function should take in an RGB image and return a new, standardized␣
↪version
def standardize_input(image):
image_crop = np.copy(image)
row_crop = 7
col_crop = 8
2
image_crop = image[row_crop:-row_crop, col_crop:-col_crop]
## TODO: Resize image and pre-process so that all "standard" images are the␣
↪same size
[61]: X_test = []
y_test = []
X_train = []
y_train = []
for im in test_im_list_combined:
standardized = standardize_input(im[0])
# for loop to standardize all images
X_test.append(standardized)
# append all standardized images into x test
y_test.append(im[1])
for im in training_im_list_combined:
standardized = standardize_input(im[0])
# for loop to standardize all images
X_train.append(standardized)
# append all standardized images into x train
y_train.append(im[1])
# n_samples = 1187
# dim1 = 32
# dim2 = 32
# dim3 = 3
# reshaping data so it works with KNN since KNN only takes 2 dimensional␣
↪datasets
# this also allows us to multiply them, converting the 4 dim dtaset into a 2␣
↪dim dataset
3
## KNN AREA ##
n_samples, dim1, dim2, dim3 = X_train.shape
X_train_knn = X_train.reshape(n_samples, dim1 * dim2 * dim3)
## TF Multilayer NN ##
X_train_tf = tf.convert_to_tensor(X_train)
y_train_tf = np.array(y_train)
[63]: X_train.shape
[64]: X_train_knn.shape
# changed from 4d to 2d so I can input into KNN algorithm
[66]: model.compile(optimizer='adam',
loss=tf.keras.losses.
↪SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
Epoch 1/50
1/1 [==============================] - 1s 549ms/step - loss: 278.4537 -
accuracy: 0.0000e+00
Epoch 2/50
1/1 [==============================] - 0s 11ms/step - loss: 136.8713 - accuracy:
0.4286
Epoch 3/50
1/1 [==============================] - 0s 11ms/step - loss: 873.1324 - accuracy:
0.3810
Epoch 4/50
1/1 [==============================] - 0s 9ms/step - loss: 633.8701 - accuracy:
0.6190
Epoch 5/50
4
1/1 [==============================] - 0s 9ms/step - loss: 947.1122 - accuracy:
0.3333
Epoch 6/50
1/1 [==============================] - 0s 9ms/step - loss: 461.8854 - accuracy:
0.3333
Epoch 7/50
1/1 [==============================] - 0s 9ms/step - loss: 93.0297 - accuracy:
0.6667
Epoch 8/50
1/1 [==============================] - 0s 41ms/step - loss: 117.3218 - accuracy:
0.7143
Epoch 9/50
1/1 [==============================] - 0s 9ms/step - loss: 351.9328 - accuracy:
0.4762
Epoch 10/50
1/1 [==============================] - 0s 9ms/step - loss: 395.5494 - accuracy:
0.5238
Epoch 11/50
1/1 [==============================] - 0s 9ms/step - loss: 298.4268 - accuracy:
0.5714
Epoch 12/50
1/1 [==============================] - 0s 9ms/step - loss: 268.7654 - accuracy:
0.6667
Epoch 13/50
1/1 [==============================] - 0s 9ms/step - loss: 232.7682 - accuracy:
0.6667
Epoch 14/50
1/1 [==============================] - 0s 8ms/step - loss: 132.1676 - accuracy:
0.7143
Epoch 15/50
1/1 [==============================] - 0s 10ms/step - loss: 24.2420 - accuracy:
0.8571
Epoch 16/50
1/1 [==============================] - 0s 9ms/step - loss: 14.2222 - accuracy:
0.9524
Epoch 17/50
1/1 [==============================] - 0s 9ms/step - loss: 45.7453 - accuracy:
0.8571
Epoch 18/50
1/1 [==============================] - 0s 8ms/step - loss: 80.2530 - accuracy:
0.7619
Epoch 19/50
1/1 [==============================] - 0s 7ms/step - loss: 77.2863 - accuracy:
0.8095
Epoch 20/50
1/1 [==============================] - 0s 9ms/step - loss: 56.7919 - accuracy:
0.9048
Epoch 21/50
5
1/1 [==============================] - 0s 15ms/step - loss: 42.7763 - accuracy:
0.9524
Epoch 22/50
1/1 [==============================] - 0s 28ms/step - loss: 61.6861 - accuracy:
0.8571
Epoch 23/50
1/1 [==============================] - 0s 9ms/step - loss: 83.4838 - accuracy:
0.7143
Epoch 24/50
1/1 [==============================] - 0s 9ms/step - loss: 46.2261 - accuracy:
0.8571
Epoch 25/50
1/1 [==============================] - 0s 9ms/step - loss: 22.9200 - accuracy:
0.9524
Epoch 26/50
1/1 [==============================] - 0s 8ms/step - loss: 19.5285 - accuracy:
0.9524
Epoch 27/50
1/1 [==============================] - 0s 8ms/step - loss: 14.8376 - accuracy:
0.9524
Epoch 28/50
1/1 [==============================] - 0s 38ms/step - loss: 8.9528 - accuracy:
0.9524
Epoch 29/50
1/1 [==============================] - 0s 9ms/step - loss: 1.9692 - accuracy:
0.9524
Epoch 30/50
1/1 [==============================] - 0s 9ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 31/50
1/1 [==============================] - 0s 8ms/step - loss: 1.9414 - accuracy:
0.9524
Epoch 32/50
1/1 [==============================] - 0s 8ms/step - loss: 3.1183 - accuracy:
0.9524
Epoch 33/50
1/1 [==============================] - 0s 9ms/step - loss: 4.9481 - accuracy:
0.9524
Epoch 34/50
1/1 [==============================] - 0s 8ms/step - loss: 4.3794 - accuracy:
0.9524
Epoch 35/50
1/1 [==============================] - 0s 39ms/step - loss: 1.5810 - accuracy:
0.9524
Epoch 36/50
1/1 [==============================] - 0s 9ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 37/50
6
1/1 [==============================] - 0s 9ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 38/50
1/1 [==============================] - 0s 9ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 39/50
1/1 [==============================] - 0s 10ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 40/50
1/1 [==============================] - 0s 10ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 41/50
1/1 [==============================] - 0s 38ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 42/50
1/1 [==============================] - 0s 9ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 43/50
1/1 [==============================] - 0s 8ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 44/50
1/1 [==============================] - 0s 8ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 45/50
1/1 [==============================] - 0s 8ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 46/50
1/1 [==============================] - 0s 8ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 47/50
1/1 [==============================] - 0s 8ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 48/50
1/1 [==============================] - 0s 40ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 49/50
1/1 [==============================] - 0s 8ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
Epoch 50/50
1/1 [==============================] - 0s 8ms/step - loss: 0.0000e+00 -
accuracy: 1.0000
[20]: print(training_history.history['loss'])
7
45.745330810546875, 80.25296020507812, 77.28632354736328, 56.791893005371094,
42.776309967041016, 61.68613815307617, 83.48382568359375, 46.22612762451172,
22.9200496673584, 19.528507232666016, 14.837599754333496, 8.952764511108398,
1.969249963760376, 0.0, 1.94140625, 3.1183383464813232, 4.948056221008301,
4.379383087158203, 1.5809617042541504, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[24]: predictions[0]
[24]: array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
[25]: np.argmax(predictions[0])
[25]: 0
[26]: y_test[0]
[26]: 0
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
8
color = 'red'
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
[29]: i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], y_test, X_test)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i], y_test)
plt.show()
[30]: # Plot the first X test images, their predicted labels, and the true labels.
# Color correct predictions in blue and incorrect predictions in red.
9
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions[i], y_test, X_test)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions[i], y_test)
plt.tight_layout()
plt.show()
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[30], line 9
7 for i in range(num_images):
8 plt.subplot(num_rows, 2*num_cols, 2*i+1)
----> 9 plot_image(i, predictions[i], y_test, X_test)
10 plt.subplot(num_rows, 2*num_cols, 2*i+2)
11 plot_value_array(i, predictions[i], y_test)
10
[31]: epoch = len(training_history.history.get('loss',[]))
11
12