Upload model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Copy of convolutional_neural_network.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1jcU6XSixUKLWzQxMdwEdP6MUoac4HvAq
|
| 8 |
+
|
| 9 |
+
# Convolutional Neural Network
|
| 10 |
+
|
| 11 |
+
### Importing the libraries
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import tensorflow as tf
|
| 15 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
| 16 |
+
from tensorflow.keras.models import load_model
|
| 17 |
+
|
| 18 |
+
"""## Part 1 - Data Preprocessing
|
| 19 |
+
|
| 20 |
+
### Preprocessing the Training set
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
train_datagen=ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)
|
| 24 |
+
training_set=train_datagen.flow_from_directory(r'dataset\training_set',target_size=(64,64),batch_size=32,class_mode='binary')
|
| 25 |
+
|
| 26 |
+
"""### Preprocessing the Test set"""
|
| 27 |
+
|
| 28 |
+
test_datagen=ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)
|
| 29 |
+
test_set=test_datagen.flow_from_directory(r'dataset\test_set',target_size=(64,64),batch_size=32,class_mode='binary')
|
| 30 |
+
|
| 31 |
+
"""## Part 2 - Building the CNN
|
| 32 |
+
|
| 33 |
+
### Initialising the CNN
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
cnn=tf.keras.models.Sequential()
|
| 37 |
+
|
| 38 |
+
"""### Step 1 - Convolution"""
|
| 39 |
+
|
| 40 |
+
cnn.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu',input_shape=[64,64,3]))
|
| 41 |
+
|
| 42 |
+
"""### Step 2 - Pooling"""
|
| 43 |
+
|
| 44 |
+
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
|
| 45 |
+
|
| 46 |
+
"""### Adding a second convolutional layer"""
|
| 47 |
+
|
| 48 |
+
cnn.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu'))
|
| 49 |
+
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
|
| 50 |
+
|
| 51 |
+
"""### Step 3 - Flattening"""
|
| 52 |
+
|
| 53 |
+
cnn.add(tf.keras.layers.Flatten())
|
| 54 |
+
|
| 55 |
+
"""### Step 4 - Full Connection"""
|
| 56 |
+
|
| 57 |
+
cnn.add(tf.keras.layers.Dense(units=128,activation='relu'))
|
| 58 |
+
|
| 59 |
+
"""### Step 5 - Output Layer"""
|
| 60 |
+
|
| 61 |
+
cnn.add(tf.keras.layers.Dense(units=1,activation='sigmoid'))
|
| 62 |
+
|
| 63 |
+
"""## Part 3 - Training the CNN
|
| 64 |
+
|
| 65 |
+
### Compiling the CNN
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
cnn.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])
|
| 69 |
+
|
| 70 |
+
"""### Training the CNN on the Training set and evaluating it on the Test set"""
|
| 71 |
+
|
| 72 |
+
cnn.fit(x=training_set,validation_data=test_set,epochs=25)
|
| 73 |
+
|
| 74 |
+
"""## Part 4 - Making a single prediction"""
|
| 75 |
+
cnn.save('cnn_model.h5')
|
| 76 |
+
import numpy as np
|
| 77 |
+
from keras.preprocessing import image
|
| 78 |
+
test_image=image.load_img(r'dataset\single_prediction\cat_or_dog_1.jpg',target_size=(64,64))
|
| 79 |
+
test_image=image.img_to_array(test_image)
|
| 80 |
+
test_image=np.expand_dims(test_image,axis=0)
|
| 81 |
+
res=cnn.predict(test_image)
|
| 82 |
+
training_set.class_indices
|
| 83 |
+
if res[0][0]==1:
|
| 84 |
+
prediction='dog'
|
| 85 |
+
else:
|
| 86 |
+
prediction='cat'
|
| 87 |
+
|
| 88 |
+
print(prediction)
|
| 89 |
+
cnn.summary()
|