Created
September 8, 2018 07:54
-
-
Save lasershow/618083f39816b7e5a0958da957185d93 to your computer and use it in GitHub Desktop.
sample.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import keras\n", | |
"from keras.applications.vgg16 import VGG16\n", | |
"from keras.preprocessing.image import ImageDataGenerator\n", | |
"from keras.layers import Input\n", | |
"import os\n", | |
"from os import listdir, makedirs\n", | |
"from os.path import join, exists, expanduser\n", | |
"\n", | |
"from keras import applications\n", | |
"from keras.preprocessing.image import ImageDataGenerator\n", | |
"from keras import optimizers\n", | |
"from keras.models import Sequential, Model\n", | |
"from keras.layers import Dense, GlobalAveragePooling2D, Convolution2D, MaxPooling2D\n", | |
"from keras.layers import Activation, Dropout, Flatten, Dense\n", | |
"from keras import backend as K\n", | |
"import tensorflow as tf" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Found 41322 images belonging to 81 classes.\n" | |
] | |
} | |
], | |
"source": [ | |
"image_data_generator = ImageDataGenerator(rescale=1.0/255)\n", | |
"train_data = image_data_generator.flow_from_directory(\n", | |
" './fruits-360/Training/',\n", | |
" target_size=(100, 100),\n", | |
" batch_size=32,\n", | |
" shuffle=True\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5\n", | |
"58892288/58889256 [==============================] - 20s 0us/step\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/ipykernel/__main__.py:12: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=Tensor(\"in..., outputs=Tensor(\"se...)`\n" | |
] | |
} | |
], | |
"source": [ | |
"input_tensor = Input(shape=(100, 100, 3))\n", | |
"vgg16 = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)\n", | |
"\n", | |
"# FC層を構築\n", | |
"top_model = Sequential()\n", | |
"top_model.add(Flatten(input_shape=vgg16.output_shape[1:]))\n", | |
"top_model.add(Dense(256, activation='relu'))\n", | |
"top_model.add(Dropout(0.5))\n", | |
"top_model.add(Dense(81, activation='softmax'))\n", | |
"\n", | |
"# VGG16とFCを接続\n", | |
"model = Model(input=vgg16.input, output=top_model(vgg16.output))\n", | |
"\n", | |
"# 最後のconv層の直前までの層をfreeze\n", | |
"for layer in model.layers[:15]:\n", | |
" layer.trainable = False\n", | |
"\n", | |
"# Fine-tuningのときはSGDの方がよい\n", | |
"model.compile(loss='categorical_crossentropy',\n", | |
" optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),\n", | |
" metrics=['accuracy'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Epoch 1/1\n", | |
"1207/1292 [===========================>..] - ETA: 11s - loss: 2.8044 - acc: 0.3665" | |
] | |
} | |
], | |
"source": [ | |
"history = model.fit_generator(train_data, epochs=1, verbose=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Environment (conda_tensorflow_p36)", | |
"language": "python", | |
"name": "conda_tensorflow_p36" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment