-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a6a16ac
Showing
5 changed files
with
13,217 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
train* | ||
test* | ||
*.h5 | ||
*.zip | ||
*.png | ||
pred.csv | ||
.ipy* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"import shutil" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"train_filenames = os.listdir('train')\n", | ||
"train_cat = filter(lambda x:x[:3] == 'cat', train_filenames)\n", | ||
"train_dog = filter(lambda x:x[:3] == 'dog', train_filenames)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def rmrf_mkdir(dirname):\n", | ||
" if os.path.exists(dirname):\n", | ||
" shutil.rmtree(dirname)\n", | ||
" os.mkdir(dirname)\n", | ||
"\n", | ||
"rmrf_mkdir('train2')\n", | ||
"os.mkdir('train2/cat')\n", | ||
"os.mkdir('train2/dog')\n", | ||
"\n", | ||
"rmrf_mkdir('test2')\n", | ||
"os.symlink('../test/', 'test2/test')\n", | ||
"\n", | ||
"for filename in train_cat:\n", | ||
" os.symlink('../../train/'+filename, 'train2/cat/'+filename)\n", | ||
"\n", | ||
"for filename in train_dog:\n", | ||
" os.symlink('../../train/'+filename, 'train2/dog/'+filename)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 2", | ||
"language": "python", | ||
"name": "python2" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Using TensorFlow backend.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from keras.models import *\n", | ||
"from keras.layers import *\n", | ||
"from keras.applications import *\n", | ||
"from keras.preprocessing.image import *\n", | ||
"\n", | ||
"import h5py" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def write_gap(MODEL, image_size, lambda_func=None):\n", | ||
" width = image_size[0]\n", | ||
" height = image_size[1]\n", | ||
" input_tensor = Input((height, width, 3))\n", | ||
" x = input_tensor\n", | ||
" if lambda_func:\n", | ||
" x = Lambda(lambda_func)(x)\n", | ||
" base_model = MODEL(input_tensor=x, weights='imagenet', include_top=False)\n", | ||
" model = Model(base_model.input, GlobalAveragePooling2D()(base_model.output))\n", | ||
"\n", | ||
" gen = ImageDataGenerator()\n", | ||
" train_generator = gen.flow_from_directory(\"train2\", image_size, shuffle=False, \n", | ||
" batch_size=16)\n", | ||
" test_generator = gen.flow_from_directory(\"test2\", image_size, shuffle=False, \n", | ||
" batch_size=16, class_mode=None)\n", | ||
"\n", | ||
" train = model.predict_generator(train_generator, train_generator.nb_sample)\n", | ||
" test = model.predict_generator(test_generator, test_generator.nb_sample)\n", | ||
" with h5py.File(\"gap_%s.h5\"%MODEL.func_name) as h:\n", | ||
" h.create_dataset(\"train\", data=train)\n", | ||
" h.create_dataset(\"test\", data=test)\n", | ||
" h.create_dataset(\"label\", data=train_generator.classes)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true, | ||
"scrolled": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Found 25000 images belonging to 2 classes.\n", | ||
"Found 12500 images belonging to 1 classes.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"write_gap(ResNet50, (224, 224))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Found 25000 images belonging to 2 classes.\n", | ||
"Found 12500 images belonging to 1 classes.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"write_gap(Xception, (299, 299), xception.preprocess_input)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Found 25000 images belonging to 2 classes.\n", | ||
"Found 12500 images belonging to 1 classes.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"write_gap(InceptionV3, (299, 299), inception_v3.preprocess_input)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Found 25000 images belonging to 2 classes.\n", | ||
"Found 12500 images belonging to 1 classes.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"write_gap(VGG16, (224, 224))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": { | ||
"collapsed": false, | ||
"deletable": true, | ||
"editable": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Found 25000 images belonging to 2 classes.\n", | ||
"Found 12500 images belonging to 1 classes.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"write_gap(VGG19, (224, 224))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 2", | ||
"language": "python", | ||
"name": "python2" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.