Machine Learning Kurs im Rahmen der Studierendentage im SS 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

157 lines
4.3 KiB

{
"cells": [
{
"cell_type": "markdown",
"id": "50680cbc",
"metadata": {},
"source": [
"Read/load the cifar10 dataset using tf.keras.datasets\n",
"- Display the first 25 images\n",
"- Convert them to greyscale images by reducing the 3 colors (r,g,b) to \n",
" one greyscale\n",
" using the formula gray = 0.2989 * r + 0.5870 * g + 0.1140 * b\n",
"- Display the first 25 images in greyscale"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2dc5ea2f",
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e846355a",
"metadata": {},
"outputs": [],
"source": [
"# Load the CIFAR-10 dataset\n",
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a493d2",
"metadata": {},
"outputs": [],
"source": [
"# In case of special pictures\n",
"selectPicture = -1 # -1 for all or number for a class starting from 0\n",
"\n",
"# Define a list of class names for CIFAR-10\n",
"class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4eacb734",
"metadata": {},
"outputs": [],
"source": [
"# Get the indices of images with a special label in the training set\n",
"special_indices = np.where(y_train == selectPicture)[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be2d0cd2",
"metadata": {},
"outputs": [],
"source": [
"# Display the first 25 images in the training set\n",
"plt.figure(figsize=(10,10))\n",
"for i in range(25):\n",
" plt.subplot(5, 5, i+1)\n",
" if selectPicture == -1 :\n",
" plt.imshow(x_train[i]) # all images\n",
" plt.title(class_names[y_train[i][0]])\n",
" else : \n",
" plt.imshow(x_train[special_indices[i]]) # special Picture only\n",
" plt.title(class_names[selectPicture]) \n",
" plt.xticks([])\n",
" plt.yticks([])\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "938e632d",
"metadata": {},
"outputs": [],
"source": [
"# Convert images to grayscale\n",
"# The ... notation is used to select all dimensions of the array up to the last one\n",
"# The vector [0.2989, 0.5870, 0.1140] converts a color image to grayscale by taking \n",
"# a weighted average of the color channels.\n",
"train_images_gray = np.dot(x_train[..., :3], [0.2989, 0.5870, 0.1140])\n",
"test_images_gray = np.dot(x_test[..., :3], [0.2989, 0.5870, 0.1140])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96333686",
"metadata": {},
"outputs": [],
"source": [
"# Normalize pixel values to [0, 1]\n",
"train_images_gray = train_images_gray / 255.0\n",
"test_images_gray = test_images_gray / 255.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64d3d2ae",
"metadata": {},
"outputs": [],
"source": [
"# Display the first 25 images in the training set\n",
"plt.figure(figsize=(10,10))\n",
"for i in range(25):\n",
" plt.subplot(5, 5, i+1)\n",
" if selectPicture == -1 :\n",
" plt.imshow(train_images_gray[i],cmap='gray') # all images\n",
" plt.title(class_names[y_train[i][0]])\n",
" else : \n",
" plt.imshow(train_images_gray[special_indices[i]],cmap='gray') # special Picture only\n",
" plt.title(class_names[selectPicture]) \n",
" plt.xticks([])\n",
" plt.yticks([])\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}