Last active
April 21, 2020 19:04
-
-
Save blooser/ce0c856e676117f808139792539c6443 to your computer and use it in GitHub Desktop.
Image recognition
This file contains hidden or 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": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using TensorFlow backend.\n" | |
] | |
} | |
], | |
"source": [ | |
"from keras.models import Sequential\n", | |
"from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"classifier = Sequential()\n", | |
"\n", | |
"classifier.add(Conv2D(32, (3, 3), input_shape=(64, 64, 3), activation='tanh'))\n", | |
"classifier.add(Dropout(0.2))\n", | |
"classifier.add(MaxPooling2D(pool_size=(2, 2)))\n", | |
"\n", | |
"classifier.add(Conv2D(32, (3, 3), activation='tanh'))\n", | |
"classifier.add(Dropout(0.2))\n", | |
"classifier.add(MaxPooling2D(pool_size=(2, 2)))\n", | |
"\n", | |
"classifier.add(Conv2D(32, (3, 3), activation='softmax'))\n", | |
"classifier.add(MaxPooling2D(pool_size=(2, 2)))\n", | |
"\n", | |
"classifier.add(Flatten())\n", | |
"\n", | |
"classifier.add(Dense(128, activation='relu'))\n", | |
"\n", | |
"classifier.add(Dense(1, activation='sigmoid'))\n", | |
"\n", | |
"classifier.compile('adam', 'binary_crossentropy', ['accuracy'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Found 8000 images belonging to 2 classes.\n", | |
"Found 2000 images belonging to 2 classes.\n" | |
] | |
} | |
], | |
"source": [ | |
"from keras.preprocessing.image import ImageDataGenerator\n", | |
"\n", | |
"train_dataset = ImageDataGenerator(\n", | |
" rescale=1./255,\n", | |
" shear_range=0.2,\n", | |
" zoom_range=0.2,\n", | |
" horizontal_flip=True)\n", | |
"\n", | |
"test_dataset = ImageDataGenerator(rescale=1./255)\n", | |
"\n", | |
"train_generator = train_dataset.flow_from_directory(\n", | |
" 'dataset/training_set',\n", | |
" target_size=(64, 64),\n", | |
" batch_size=32,\n", | |
" class_mode='binary')\n", | |
"\n", | |
"validation_generator = test_dataset.flow_from_directory(\n", | |
" 'dataset/test_set',\n", | |
" target_size=(64, 64),\n", | |
" batch_size=32,\n", | |
" class_mode='binary')\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Epoch 1/10\n", | |
"2000/2000 [==============================] - 714s 357ms/step - loss: 0.6025 - acc: 0.6475 - val_loss: 0.7785 - val_acc: 0.6322\n", | |
"Epoch 2/10\n", | |
"2000/2000 [==============================] - 615s 307ms/step - loss: 0.4712 - acc: 0.7700 - val_loss: 0.8033 - val_acc: 0.6570\n", | |
"Epoch 3/10\n", | |
"2000/2000 [==============================] - 615s 307ms/step - loss: 0.4042 - acc: 0.8124 - val_loss: 0.7731 - val_acc: 0.6983\n", | |
"Epoch 4/10\n", | |
"2000/2000 [==============================] - 613s 306ms/step - loss: 0.3457 - acc: 0.8470 - val_loss: 0.7777 - val_acc: 0.7336\n", | |
"Epoch 5/10\n", | |
"2000/2000 [==============================] - 616s 308ms/step - loss: 0.2978 - acc: 0.8704 - val_loss: 0.7478 - val_acc: 0.7404\n", | |
"Epoch 6/10\n", | |
"2000/2000 [==============================] - 616s 308ms/step - loss: 0.2645 - acc: 0.8880 - val_loss: 0.8064 - val_acc: 0.7311\n", | |
"Epoch 7/10\n", | |
"2000/2000 [==============================] - 616s 308ms/step - loss: 0.2279 - acc: 0.9045 - val_loss: 0.9225 - val_acc: 0.7250\n", | |
"Epoch 8/10\n", | |
"2000/2000 [==============================] - 616s 308ms/step - loss: 0.2013 - acc: 0.9180 - val_loss: 0.6940 - val_acc: 0.7716\n", | |
"Epoch 9/10\n", | |
"2000/2000 [==============================] - 616s 308ms/step - loss: 0.1778 - acc: 0.9278 - val_loss: 0.8346 - val_acc: 0.7579\n", | |
"Epoch 10/10\n", | |
"2000/2000 [==============================] - 616s 308ms/step - loss: 0.1570 - acc: 0.9368 - val_loss: 0.8978 - val_acc: 0.7518\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x7fc68dc08f60>" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"classifier.fit_generator(\n", | |
" train_generator,\n", | |
" steps_per_epoch=2000,\n", | |
" epochs=10,\n", | |
" validation_data=validation_generator,\n", | |
" validation_steps=800)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from keras.preprocessing import image\n", | |
"from keras.models import load_model\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"width, height = 64, 64\n", | |
"\n", | |
"img = image.load_img('dataset/test_predictions/shiba.jpg', target_size=(width, height))\n", | |
"dog_1 = image.img_to_array(img)\n", | |
"dog_1 = np.expand_dims(dog_1, axis=0)\n", | |
"\n", | |
"img = image.load_img('dataset/test_predictions/golden_retriever.jpg', target_size=(width, height))\n", | |
"dog_2 = image.img_to_array(img)\n", | |
"dog_2 = np.expand_dims(dog_2, axis=0)\n", | |
"\n", | |
"img = image.load_img(\"dataset/test_predictions/cat.jpg\", target_size=(width, height))\n", | |
"cat_1 = image.img_to_array(img)\n", | |
"cat_1 = np.expand_dims(cat_1, axis=0)\n", | |
"\n", | |
"img = image.load_img(\"dataset/test_predictions/cat1.jpg\", target_size=(width, height))\n", | |
"cat_2 = image.img_to_array(img)\n", | |
"cat_2 = np.expand_dims(cat_2, axis=0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'cats': 0, 'dogs': 1}" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"train_generator.class_indices" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_answer(value):\n", | |
" return \"Dog\" if value == 1 else \"Cat\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"images = np.vstack([dog_1, cat_1, dog_2, cat_2])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"predictions = classifier.predict_classes(images, batch_size=10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1],\n", | |
" [0],\n", | |
" [1],\n", | |
" [0]], dtype=int32)" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"predictions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Dog\n", | |
"Cat\n", | |
"Dog\n", | |
"Cat\n" | |
] | |
} | |
], | |
"source": [ | |
"for predict_image in predictions:\n", | |
" print(get_answer(predict_image))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"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.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment