{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**Important: This notebook will only work with fastai-0.7.x. Do not try to run any fastai-1.x code from this path in the repository because it will load fastai-0.7.x**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction to our first task: 'Dogs vs Cats'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "PATH = \"data/dogscats/\"\n", "sz=224\n", "batch_size=64" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n", "/home/jhoward/anaconda3/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6\n", " return f(*args, **kwds)\n" ] } ], "source": [ "import numpy as np\n", "from keras.preprocessing.image import ImageDataGenerator\n", "from keras.preprocessing import image\n", "from keras.layers import Dropout, Flatten, Dense\n", "from keras.applications import ResNet50\n", "from keras.models import Model, Sequential\n", "from keras.layers import Dense, GlobalAveragePooling2D\n", "from keras import backend as K\n", "from keras.applications.resnet50 import preprocess_input" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "train_data_dir = f'{PATH}train'\n", "validation_data_dir = f'{PATH}valid'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 23000 images belonging to 2 classes.\n", "Found 2000 images belonging to 2 classes.\n" ] } ], "source": [ "train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,\n", " shear_range=0.2, zoom_range=0.2, horizontal_flip=True)\n", "\n", "test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\n", "\n", "train_generator = train_datagen.flow_from_directory(train_data_dir,\n", " target_size=(sz, sz),\n", " batch_size=batch_size, class_mode='binary')\n", "\n", "validation_generator = test_datagen.flow_from_directory(validation_data_dir,\n", " shuffle=False,\n", " target_size=(sz, sz),\n", " batch_size=batch_size, class_mode='binary')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "base_model = ResNet50(weights='imagenet', include_top=False)\n", "x = base_model.output\n", "x = GlobalAveragePooling2D()(x)\n", "x = Dense(1024, activation='relu')(x)\n", "predictions = Dense(1, activation='sigmoid')(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = Model(inputs=base_model.input, outputs=predictions)\n", "for layer in base_model.layers: layer.trainable = False\n", "model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/3\n", "359/359 [==============================] - 81s 226ms/step - loss: 0.1780 - acc: 0.9486 - val_loss: 0.0805 - val_acc: 0.9743\n", "Epoch 2/3\n", "359/359 [==============================] - 77s 215ms/step - loss: 0.0928 - acc: 0.9697 - val_loss: 0.0592 - val_acc: 0.9814\n", "Epoch 3/3\n", "359/359 [==============================] - 74s 207ms/step - loss: 0.0746 - acc: 0.9730 - val_loss: 0.0617 - val_acc: 0.9814\n", "CPU times: user 15min 53s, sys: 36.4 s, total: 16min 29s\n", "Wall time: 3min 52s\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "model.fit_generator(train_generator, train_generator.n // batch_size, epochs=3, workers=4,\n", " validation_data=validation_generator, validation_steps=validation_generator.n // batch_size)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "split_at = 140\n", "for layer in model.layers[:split_at]: layer.trainable = False\n", "for layer in model.layers[split_at:]: layer.trainable = True\n", "model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/1\n", "359/359 [==============================] - 217s 603ms/step - loss: 0.0762 - acc: 0.9741 - val_loss: 7.9436 - val_acc: 0.5055\n", "CPU times: user 4min 47s, sys: 14.1 s, total: 5min 1s\n", "Wall time: 3min 38s\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "model.fit_generator(train_generator, train_generator.n // batch_size, epochs=1, workers=3,\n", " validation_data=validation_generator, validation_steps=validation_generator.n // batch_size)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }