Advertisement
sepi0l

dl_fashion

Apr 25th, 2024
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 7.28 KB | None | 0 0
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "code",
  5.    "execution_count": null,
  6.    "metadata": {},
  7.    "outputs": [],
  8.    "source": [
  9.     "# TensorFlow and tf.keras\n",
  10.     "import tensorflow as tf\n",
  11.     "\n",
  12.     "# Helper libraries\n",
  13.     "import numpy as np\n",
  14.     "import matplotlib.pyplot as plt\n",
  15.     "\n",
  16.     "print(tf.__version__)"
  17.    ]
  18.   },
  19.   {
  20.    "cell_type": "code",
  21.    "execution_count": null,
  22.    "metadata": {},
  23.    "outputs": [
  24.     {
  25.      "name": "stdout",
  26.      "output_type": "stream",
  27.      "text": [
  28.       "29515/29515 [==============================] - 0s 2us/step\n",
  29.       "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n",
  30.       "26421880/26421880 [==============================] - 45s 2us/step\n",
  31.       "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n",
  32.       "5148/5148 [==============================] - 0s 0s/step\n",
  33.       "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n",
  34.       "4422102/4422102 [==============================] - 6s 1us/step\n"
  35.      ]
  36.     }
  37.    ],
  38.    "source": [
  39.     "fashion_mnist = tf.keras.datasets.fashion_mnist\n",
  40.     "\n",
  41.     "(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()"
  42.    ]
  43.   },
  44.   {
  45.    "cell_type": "code",
  46.    "execution_count": null,
  47.    "metadata": {},
  48.    "outputs": [],
  49.    "source": [
  50.     "class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',\n",
  51.     "               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']"
  52.    ]
  53.   },
  54.   {
  55.    "cell_type": "code",
  56.    "execution_count": null,
  57.    "metadata": {},
  58.    "outputs": [],
  59.    "source": [
  60.     "train_images.shape"
  61.    ]
  62.   },
  63.   {
  64.    "cell_type": "code",
  65.    "execution_count": null,
  66.    "metadata": {},
  67.    "outputs": [],
  68.    "source": [
  69.     "len(train_labels)"
  70.    ]
  71.   },
  72.   {
  73.    "cell_type": "code",
  74.    "execution_count": null,
  75.    "metadata": {},
  76.    "outputs": [],
  77.    "source": [
  78.     "test_labels"
  79.    ]
  80.   },
  81.   {
  82.    "cell_type": "code",
  83.    "execution_count": null,
  84.    "metadata": {},
  85.    "outputs": [],
  86.    "source": [
  87.     "test_images.shape"
  88.    ]
  89.   },
  90.   {
  91.    "cell_type": "code",
  92.    "execution_count": null,
  93.    "metadata": {},
  94.    "outputs": [],
  95.    "source": [
  96.     "len(test_labels)"
  97.    ]
  98.   },
  99.   {
  100.    "cell_type": "code",
  101.    "execution_count": null,
  102.    "metadata": {},
  103.    "outputs": [],
  104.    "source": [
  105.     "plt.figure()\n",
  106.     "plt.imshow(train_images[0])\n",
  107.     "plt.colorbar()\n",
  108.     "plt.grid(False)\n",
  109.     "plt.show()"
  110.    ]
  111.   },
  112.   {
  113.    "cell_type": "code",
  114.    "execution_count": null,
  115.    "metadata": {},
  116.    "outputs": [],
  117.    "source": [
  118.     "train_images = train_images / 255.0\n",
  119.     "\n",
  120.     "test_images = test_images / 255.0"
  121.    ]
  122.   },
  123.   {
  124.    "cell_type": "code",
  125.    "execution_count": null,
  126.    "metadata": {},
  127.    "outputs": [],
  128.    "source": [
  129.     "\n",
  130.     "plt.figure(figsize=(10,10))\n",
  131.     "for i in range(5):\n",
  132.     "    plt.subplot(5,5,i+1)\n",
  133.     "    plt.xticks([])\n",
  134.     "    plt.yticks([])\n",
  135.     "    plt.grid(False)\n",
  136.     "    plt.imshow(train_images[i], cmap=plt.cm.binary)\n",
  137.     "    plt.xlabel(class_names[train_labels[i]])\n",
  138.     "    plt.show()\n"
  139.    ]
  140.   },
  141.   {
  142.    "cell_type": "code",
  143.    "execution_count": null,
  144.    "metadata": {},
  145.    "outputs": [],
  146.    "source": [
  147.     "import tensorflow as tf\n",
  148.     "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout"
  149.    ]
  150.   },
  151.   {
  152.    "cell_type": "code",
  153.    "execution_count": null,
  154.    "metadata": {},
  155.    "outputs": [],
  156.    "source": [
  157.     "model = tf.keras.Sequential([\n",
  158.     "    Conv2D(64, (3,3), activation='relu', input_shape=(28, 28, 1)),\n",
  159.     "    MaxPooling2D((2, 2)),\n",
  160.     "    Conv2D(128, (3,3), activation='relu', padding=\"same\"),\n",
  161.     "    MaxPooling2D((2, 2)),\n",
  162.     "    Flatten(),\n",
  163.     "    Dense(256, activation='relu'),\n",
  164.     "    Dropout(0.2),\n",
  165.     "    Dense(10, activation='softmax')\n",
  166.     "])"
  167.    ]
  168.   },
  169.   {
  170.    "cell_type": "code",
  171.    "execution_count": null,
  172.    "metadata": {},
  173.    "outputs": [],
  174.    "source": [
  175.     "model.compile(optimizer='adam',\n",
  176.     "            loss='sparse_categorical_crossentropy',\n",
  177.     "            metrics=['accuracy'])"
  178.    ]
  179.   },
  180.   {
  181.    "cell_type": "code",
  182.    "execution_count": null,
  183.    "metadata": {},
  184.    "outputs": [],
  185.    "source": [
  186.     "model.fit(train_images, train_labels, epochs=10)"
  187.    ]
  188.   },
  189.   {
  190.    "cell_type": "code",
  191.    "execution_count": null,
  192.    "metadata": {},
  193.    "outputs": [],
  194.    "source": [
  195.     "model.evaluate(test_images, test_labels)"
  196.    ]
  197.   },
  198.   {
  199.    "cell_type": "code",
  200.    "execution_count": null,
  201.    "metadata": {},
  202.    "outputs": [],
  203.    "source": [
  204.     "probability_model = tf.keras.Sequential([model,\n",
  205.     "tf.keras.layers.Softmax()])"
  206.    ]
  207.   },
  208.   {
  209.    "cell_type": "code",
  210.    "execution_count": null,
  211.    "metadata": {},
  212.    "outputs": [],
  213.    "source": [
  214.     "predictions = probability_model.predict(test_images)\n"
  215.    ]
  216.   },
  217.   {
  218.    "cell_type": "code",
  219.    "execution_count": null,
  220.    "metadata": {},
  221.    "outputs": [],
  222.    "source": [
  223.     "predictions[0]"
  224.    ]
  225.   },
  226.   {
  227.    "cell_type": "code",
  228.    "execution_count": null,
  229.    "metadata": {},
  230.    "outputs": [],
  231.    "source": [
  232.     "def plot_image(i, predictions_array, true_label, img):\n",
  233.     "    true_label, img = true_label[i], img[i]\n",
  234.     "    plt.grid(False)\n",
  235.     "    plt.xticks([])\n",
  236.     "    plt.yticks([])\n",
  237.     "    plt.imshow(img, cmap=plt.cm.binary)\n",
  238.     "    predicted_label = np.argmax(predictions_array)\n",
  239.     "    if predicted_label == true_label:\n",
  240.     "        color = 'blue'\n",
  241.     "    else:\n",
  242.     "        color = 'red'\n",
  243.     "    plt.xlabel(\"{} {:2.0f}% ({})\".format(class_names[predicted_label],\n",
  244.     "    100*np.max(predictions_array),\n",
  245.     "    class_names[true_label]),\n",
  246.     "    color=color)\n",
  247.     "\n",
  248.     "def plot_value_array(i, predictions_array, true_label):\n",
  249.     "    true_label = true_label[i]\n",
  250.     "    plt.grid(False)\n",
  251.     "    plt.xticks(range(10))\n",
  252.     "    plt.yticks([])\n",
  253.     "    thisplot = plt.bar(range(10), predictions_array, color=\"#777777\")\n",
  254.     "    plt.ylim([0, 1])\n",
  255.     "    predicted_label = np.argmax(predictions_array)\n",
  256.     "    thisplot[predicted_label].set_color('red')\n",
  257.     "    thisplot[true_label].set_color('blue')\n"
  258.    ]
  259.   },
  260.   {
  261.    "cell_type": "code",
  262.    "execution_count": null,
  263.    "metadata": {},
  264.    "outputs": [],
  265.    "source": [
  266.     "i = 3\n",
  267.     "plt.figure(figsize=(6,3))\n",
  268.     "plt.subplot(1,2,1)\n",
  269.     "plot_image(i, predictions[i], test_labels, test_images)\n",
  270.     "plt.subplot(1,2,2)\n",
  271.     "plot_value_array(i, predictions[i], test_labels)\n",
  272.     "plt.show()\n"
  273.    ]
  274.   }
  275.  ],
  276.  "metadata": {
  277.   "kernelspec": {
  278.    "display_name": "Python 3 (ipykernel)",
  279.    "language": "python",
  280.    "name": "python3"
  281.   },
  282.   "language_info": {
  283.    "codemirror_mode": {
  284.     "name": "ipython",
  285.     "version": 3
  286.    },
  287.    "file_extension": ".py",
  288.    "mimetype": "text/x-python",
  289.    "name": "python",
  290.    "nbconvert_exporter": "python",
  291.    "pygments_lexer": "ipython3",
  292.    "version": "3.11.8"
  293.   }
  294.  },
  295.  "nbformat": 4,
  296.  "nbformat_minor": 4
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement