machinelearning

Machine learning code
git clone git://git.laack.co/machinelearning.git
Log | Files | Refs

CNNWithKeras.ipynb (2456B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "code",
      5    "execution_count": 30,
      6    "metadata": {},
      7    "outputs": [],
      8    "source": [
      9     "from sklearn.datasets import load_sample_images\n",
     10     "import tensorflow as tf\n",
     11     "import keras\n",
     12     "\n",
     13     "images = load_sample_images()['images']\n",
     14     "images = tf.keras.layers.CenterCrop(height=70, width=120)(images)\n",
     15     "images = keras.layers.Rescaling(scale=1/255)(images) "
     16    ]
     17   },
     18   {
     19    "cell_type": "code",
     20    "execution_count": 31,
     21    "metadata": {},
     22    "outputs": [
     23     {
     24      "data": {
     25       "text/plain": [
     26        "TensorShape([2, 70, 120, 3])"
     27       ]
     28      },
     29      "execution_count": 31,
     30      "metadata": {},
     31      "output_type": "execute_result"
     32     }
     33    ],
     34    "source": [
     35     "images.shape"
     36    ]
     37   },
     38   {
     39    "cell_type": "code",
     40    "execution_count": 32,
     41    "metadata": {},
     42    "outputs": [
     43     {
     44      "data": {
     45       "text/plain": [
     46        "TensorShape([2, 70, 120, 32])"
     47       ]
     48      },
     49      "execution_count": 32,
     50      "metadata": {},
     51      "output_type": "execute_result"
     52     }
     53    ],
     54    "source": [
     55     "conv = keras.layers.Conv2D(filters=32, kernel_size=7, padding='same')\n",
     56     "fmaps = conv(images)\n",
     57     "fmaps.shape"
     58    ]
     59   },
     60   {
     61    "cell_type": "code",
     62    "execution_count": 33,
     63    "metadata": {},
     64    "outputs": [
     65     {
     66      "data": {
     67       "text/plain": [
     68        "(7, 7, 3, 32)"
     69       ]
     70      },
     71      "execution_count": 33,
     72      "metadata": {},
     73      "output_type": "execute_result"
     74     }
     75    ],
     76    "source": [
     77     "kernels, biases = conv.get_weights()\n",
     78     "kernels.shape"
     79    ]
     80   },
     81   {
     82    "cell_type": "code",
     83    "execution_count": 34,
     84    "metadata": {},
     85    "outputs": [
     86     {
     87      "data": {
     88       "text/plain": [
     89        "(32,)"
     90       ]
     91      },
     92      "execution_count": 34,
     93      "metadata": {},
     94      "output_type": "execute_result"
     95     }
     96    ],
     97    "source": [
     98     "biases.shape"
     99    ]
    100   },
    101   {
    102    "cell_type": "code",
    103    "execution_count": 35,
    104    "metadata": {},
    105    "outputs": [],
    106    "source": [
    107     "pool = keras.layers.MaxPool2D(pool_size=2)"
    108    ]
    109   }
    110  ],
    111  "metadata": {
    112   "kernelspec": {
    113    "display_name": ".venv",
    114    "language": "python",
    115    "name": "python3"
    116   },
    117   "language_info": {
    118    "codemirror_mode": {
    119     "name": "ipython",
    120     "version": 3
    121    },
    122    "file_extension": ".py",
    123    "mimetype": "text/x-python",
    124    "name": "python",
    125    "nbconvert_exporter": "python",
    126    "pygments_lexer": "ipython3",
    127    "version": "3.11.2"
    128   }
    129  },
    130  "nbformat": 4,
    131  "nbformat_minor": 2
    132 }