<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model-Sizing for Keras CNN Model Zoo\n",
    "\n",
    "This is a sanity check for : https://culurciello.github.io/tech/2016/06/04/nets.html\n",
    "\n",
    "In particular, their model comparison graph :\n",
    "\n",
    "![model comparison graph](./images/presentation/ImageNet-model-comparison_726x458.png)        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os, sys\n",
    "\n",
    "targz = \"v0.5.tar.gz\"\n",
    "url = \"https://github.com/fchollet/deep-learning-models/archive/\"+targz\n",
    "models_orig_dir = 'deep-learning-models-0.5'\n",
    "models_here_dir = 'keras_deep_learning_models'\n",
    "models_dir = './models/'\n",
    "\n",
    "if not os.path.exists(models_dir):\n",
    "    os.makedirs(models_dir)\n",
    "\n",
    "if not os.path.isfile( os.path.join(models_dir, models_here_dir, 'README.md') ):\n",
    "    tarfilepath = os.path.join(models_dir, targz)\n",
    "    if not os.path.isfile(tarfilepath):\n",
    "        import urllib.request \n",
    "        urllib.request.urlretrieve(url, tarfilepath) \n",
    "    import tarfile, shutil\n",
    "    tarfile.open(tarfilepath, 'r:gz').extractall(models_dir)\n",
    "    shutil.move(os.path.join(models_dir, models_orig_dir), os.path.join(models_dir, models_here_dir))\n",
    "    if os.path.isfile( os.path.join(models_dir, models_here_dir, 'README.md') ):\n",
    "        os.unlink(tarfilepath)\n",
    "\n",
    "sys.path.append(models_dir)\n",
    "\n",
    "print(\"Keras Model Zoo model code installed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "import keras\n",
    "print(\"keras version = %s\" % (keras.__version__,))\n",
    "\n",
    "from keras_deep_learning_models.imagenet_utils import preprocess_input, decode_predictions\n",
    "\n",
    "# This call to 'decode_predictions' wiil potentially download imagenet_class_index.json (35Kb)\n",
    "decode_predictions(np.zeros( (1,1000) ), top=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras_deep_learning_models.resnet50 import ResNet50\n",
    "#from keras.preprocessing import image\n",
    "\n",
    "model = ResNet50(weights='imagenet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "! ls -lh ~/.keras/models/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```99M resnet50_weights_tf_dim_ordering_tf_kernels.h5```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "img_path = './images/cat-with-tongue_224x224.jpg'\n",
    "img = keras.preprocessing.image.load_img(img_path, target_size=(224, 224))\n",
    "\n",
    "x = keras.preprocessing.image.img_to_array(img)\n",
    "x = np.expand_dims(x, axis=0)\n",
    "x = preprocess_input(x)\n",
    "\n",
    "preds = model.predict(x)\n",
    "print('Predicted:', decode_predictions(preds, top=1))\n",
    "# prints: [[('n02123045', 'tabby', 0.76617092)]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []<commit_msg>keras.preprocessing has some import magic (highlighted)<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model-Sizing for Keras CNN Model Zoo\n",
    "\n",
    "This is a sanity check for : https://culurciello.github.io/tech/2016/06/04/nets.html\n",
    "\n",
    "In particular, their model comparison graph :\n",
    "\n",
    "![model comparison graph](./images/presentation/ImageNet-model-comparison_726x458.png)        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os, sys\n",
    "\n",
    "targz = \"v0.5.tar.gz\"\n",
    "url = \"https://github.com/fchollet/deep-learning-models/archive/\"+targz\n",
    "models_orig_dir = 'deep-learning-models-0.5'\n",
    "models_here_dir = 'keras_deep_learning_models'\n",
    "models_dir = './models/'\n",
    "\n",
    "if not os.path.exists(models_dir):\n",
    "    os.makedirs(models_dir)\n",
    "\n",
    "if not os.path.isfile( os.path.join(models_dir, models_here_dir, 'README.md') ):\n",
    "    tarfilepath = os.path.join(models_dir, targz)\n",
    "    if not os.path.isfile(tarfilepath):\n",
    "        import urllib.request \n",
    "        urllib.request.urlretrieve(url, tarfilepath) \n",
    "    import tarfile, shutil\n",
    "    tarfile.open(tarfilepath, 'r:gz').extractall(models_dir)\n",
    "    shutil.move(os.path.join(models_dir, models_orig_dir), os.path.join(models_dir, models_here_dir))\n",
    "    if os.path.isfile( os.path.join(models_dir, models_here_dir, 'README.md') ):\n",
    "        os.unlink(tarfilepath)\n",
    "\n",
    "sys.path.append(models_dir)\n",
    "\n",
    "print(\"Keras Model Zoo model code installed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "import keras\n",
    "print(\"keras version = %s\" % (keras.__version__,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from keras_deep_learning_models.imagenet_utils import preprocess_input, decode_predictions\n",
    "from keras.preprocessing import image as keras_preprocessing_image\n",
    "\n",
    "# This call to 'decode_predictions' wiil potentially download imagenet_class_index.json (35Kb)\n",
    "decode_predictions(np.zeros( (1,1000) ), top=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "img_path = './images/cat-with-tongue_224x224.jpg'\n",
    "img = keras_preprocessing_image.load_img(img_path, target_size=(224, 224))\n",
    "\n",
    "x = keras.preprocessing.image.img_to_array(img)\n",
    "x = np.expand_dims(x, axis=0)\n",
    "x = preprocess_input(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras_deep_learning_models.resnet50 import ResNet50\n",
    "\n",
    "model = ResNet50(weights='imagenet')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```99M resnet50_weights_tf_dim_ordering_tf_kernels.h5```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "preds = model.predict(x)\n",
    "print('Predicted:', decode_predictions(preds, top=1))\n",
    "# prints: [[('n02123045', 'tabby', 0.76617092)]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "! ls -lh ~/.keras/models/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
{'fraction_non_alphanumeric': 0.2006829524560021, 'fraction_numerical': 0.017861833464670344, 'mean_word_length': 2.8911599386816556, 'pattern_counts': {'":': 98, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5558374', 'n_tokens_mistral': 3054, 'n_tokens_neox': 2931, 'n_words': 471}