191 lines
4.8 KiB
Plaintext
191 lines
4.8 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Importing libraries\n",
|
||
|
"from keras.models import Sequential\n",
|
||
|
"from keras.layers import Conv2D\n",
|
||
|
"from keras.layers import MaxPooling2D\n",
|
||
|
"from keras.layers import Flatten\n",
|
||
|
"from keras.layers import Dense"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Initialising the CNN\n",
|
||
|
"classifier = Sequential()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Step 1 - Convolution\n",
|
||
|
"classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Step 2 - Pooling\n",
|
||
|
"classifier.add(MaxPooling2D(pool_size = (2, 2)))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Adding a second convolutional layer\n",
|
||
|
"classifier.add(Conv2D(32, (3, 3), activation = 'relu'))\n",
|
||
|
"classifier.add(MaxPooling2D(pool_size = (2, 2)))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Step 3 - Flattening\n",
|
||
|
"classifier.add(Flatten())"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Step 4 - Full connection\n",
|
||
|
"classifier.add(Dense(units = 128, activation = 'relu'))\n",
|
||
|
"classifier.add(Dense(units = 1, activation = 'sigmoid'))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Compiling the CNN\n",
|
||
|
"classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Partie 2 - CNN"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from keras.preprocessing.image import ImageDataGenerator"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"train_datagen = ImageDataGenerator(rescale = 1./255,\n",
|
||
|
" shear_range = 0.2,\n",
|
||
|
" zoom_range = 0.2,\n",
|
||
|
" horizontal_flip = True)\n",
|
||
|
"\n",
|
||
|
"test_datagen = ImageDataGenerator(rescale = 1./255)\n",
|
||
|
"\n",
|
||
|
"training_set = train_datagen.flow_from_directory('dataset/training_set',\n",
|
||
|
" target_size = (64, 64),\n",
|
||
|
" batch_size = 32,\n",
|
||
|
" class_mode = 'binary')\n",
|
||
|
"\n",
|
||
|
"test_set = test_datagen.flow_from_directory('dataset/test_set',\n",
|
||
|
" target_size = (64, 64),\n",
|
||
|
" batch_size = 32,\n",
|
||
|
" class_mode = 'binary')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"classifier.fit_generator(training_set,\n",
|
||
|
" steps_per_epoch = 8000,\n",
|
||
|
" epochs = 25,\n",
|
||
|
" validation_data = test_set,\n",
|
||
|
" validation_steps = 2000)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Partie 3 - Prédictions"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import numpy as np\n",
|
||
|
"from keras.preprocessing import image\n",
|
||
|
"test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))\n",
|
||
|
"test_image = image.img_to_array(test_image)\n",
|
||
|
"test_image = np.expand_dims(test_image, axis = 0)\n",
|
||
|
"result = classifier.predict(test_image)\n",
|
||
|
"training_set.class_indices\n",
|
||
|
"if result[0][0] == 1:\n",
|
||
|
" prediction = 'dog'\n",
|
||
|
"else:\n",
|
||
|
" prediction = 'cat'"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"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.10"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 4
|
||
|
}
|