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.

154 lines
4.0 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "id": "50680cbc",
  6. "metadata": {},
  7. "source": [
  8. "Read/load the cifar10 dataset using tf.keras.datasets\n",
  9. "- Display the first 25 images\n",
  10. "- Convert them to greyscale images by reducing the 3 colors (r,g,b) to \n",
  11. " one greyscale\n",
  12. " using the formula gray = 0.2989 * r + 0.5870 * g + 0.1140 * b\n",
  13. "- Display the first 25 images in greyscale"
  14. ]
  15. },
  16. {
  17. "cell_type": "code",
  18. "execution_count": null,
  19. "id": "2dc5ea2f",
  20. "metadata": {},
  21. "outputs": [],
  22. "source": [
  23. "import tensorflow as tf\n",
  24. "import matplotlib.pyplot as plt\n",
  25. "import numpy as np"
  26. ]
  27. },
  28. {
  29. "cell_type": "code",
  30. "execution_count": null,
  31. "id": "e846355a",
  32. "metadata": {},
  33. "outputs": [],
  34. "source": [
  35. "# Load the CIFAR-10 dataset\n",
  36. "(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()"
  37. ]
  38. },
  39. {
  40. "cell_type": "code",
  41. "execution_count": null,
  42. "id": "85a493d2",
  43. "metadata": {},
  44. "outputs": [],
  45. "source": [
  46. "# In case of special pictures\n",
  47. "selectPicture = -1 # -1 for all or number for a class starting from 0\n",
  48. "\n",
  49. "# Define a list of class names for CIFAR-10\n",
  50. "class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']"
  51. ]
  52. },
  53. {
  54. "cell_type": "code",
  55. "execution_count": null,
  56. "id": "4eacb734",
  57. "metadata": {},
  58. "outputs": [],
  59. "source": [
  60. "# Get the indices of images with a special label in the training set\n",
  61. "special_indices = np.where(y_train == selectPicture)[0]"
  62. ]
  63. },
  64. {
  65. "cell_type": "code",
  66. "execution_count": null,
  67. "id": "be2d0cd2",
  68. "metadata": {},
  69. "outputs": [],
  70. "source": [
  71. "# Display the first 25 images in the training set\n",
  72. "plt.figure(figsize=(10,10))\n",
  73. "for i in range(25):\n",
  74. " plt.subplot(5, 5, i+1)\n",
  75. " if selectPicture == -1 :\n",
  76. " plt.imshow(x_train[i]) # all images\n",
  77. " plt.title(class_names[y_train[i][0]])\n",
  78. " else : \n",
  79. " plt.imshow(x_train[special_indices[i]]) # special Picture only\n",
  80. " plt.title(class_names[selectPicture]) \n",
  81. " plt.xticks([])\n",
  82. " plt.yticks([])\n",
  83. "plt.show()"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": null,
  89. "id": "938e632d",
  90. "metadata": {},
  91. "outputs": [],
  92. "source": [
  93. "# Convert images to grayscale\n",
  94. "train_images_gray = np.dot(x_train[..., :3], [0.2989, 0.5870, 0.1140])\n",
  95. "test_images_gray = np.dot(x_test[..., :3], [0.2989, 0.5870, 0.1140])"
  96. ]
  97. },
  98. {
  99. "cell_type": "code",
  100. "execution_count": null,
  101. "id": "96333686",
  102. "metadata": {},
  103. "outputs": [],
  104. "source": [
  105. "# Normalize pixel values to [0, 1]\n",
  106. "train_images_gray = train_images_gray / 255.0\n",
  107. "test_images_gray = test_images_gray / 255.0"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": null,
  113. "id": "64d3d2ae",
  114. "metadata": {},
  115. "outputs": [],
  116. "source": [
  117. "# Display the first 25 images in the training set\n",
  118. "plt.figure(figsize=(10,10))\n",
  119. "for i in range(25):\n",
  120. " plt.subplot(5, 5, i+1)\n",
  121. " if selectPicture == -1 :\n",
  122. " plt.imshow(train_images_gray[i],cmap='gray') # all images\n",
  123. " plt.title(class_names[y_train[i][0]])\n",
  124. " else : \n",
  125. " plt.imshow(train_images_gray[special_indices[i]],cmap='gray') # special Picture only\n",
  126. " plt.title(class_names[selectPicture]) \n",
  127. " plt.xticks([])\n",
  128. " plt.yticks([])\n",
  129. "plt.show()"
  130. ]
  131. }
  132. ],
  133. "metadata": {
  134. "kernelspec": {
  135. "display_name": "Python 3 (ipykernel)",
  136. "language": "python",
  137. "name": "python3"
  138. },
  139. "language_info": {
  140. "codemirror_mode": {
  141. "name": "ipython",
  142. "version": 3
  143. },
  144. "file_extension": ".py",
  145. "mimetype": "text/x-python",
  146. "name": "python",
  147. "nbconvert_exporter": "python",
  148. "pygments_lexer": "ipython3",
  149. "version": "3.8.16"
  150. }
  151. },
  152. "nbformat": 4,
  153. "nbformat_minor": 5
  154. }