-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdogs_vs_cats.py
114 lines (87 loc) · 3.25 KB
/
dogs_vs_cats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
"""Dogs vs Cats.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1juJVmflhtvDCV63lI_3ZMltBtw-wphZJ
"""
from google.colab import drive
drive.mount('/content/drive')
!cp /content/drive/My\ Drive/Colab\ Notebooks/Colab\ Data/kaggle.json ~/.kaggle/kaggle.json
!kaggle config set -n path -v/content
!chmod 600 /root/.kaggle/kaggle.json
!kaggle competitions download -c dogs-vs-cats-redux-kernels-edition
base_dir = '/content/competitions/dogs-vs-cats-redux-kernels-edition'
!unzip ./train.zip
import os
import cv2
import numpy as np
from tqdm import tqdm_notebook as tqdm
import tensorflow as tf
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout,Flatten, Dense
from tensorflow.keras.models import model_from_json
data_dir = os.path.join(base_dir,'train')
# Preparing labels
Y = []
for f in os.listdir(data_dir):
if(f.split('.')[0] == 'dog'):
Y.append(1)
else:
Y.append(0)
Y = np.array(Y)
# Preparing X
X = []
IMG_SIZE = 64
for f in tqdm(os.listdir(data_dir)):
img_file = os.path.join(data_dir,f)
img = cv2.imread(img_file)
img = cv2.resize(img,(IMG_SIZE, IMG_SIZE))
img = img/255
X.append(img)
X = np.array(X)
print(X.shape)
# Splitting dataset
xtrain, xtest, ytrain, ytest = train_test_split(X,Y,test_size=0.25,random_state=1)
clf = Sequential()
clf.add(layers.Conv2D(filters=32,kernel_size=(3,3),strides=(1,1),padding='same',activation='relu',input_shape=(IMG_SIZE,IMG_SIZE,3)))
clf.add(layers.MaxPool2D(pool_size=(2,2),strides=(2,2),padding='same'))
clf.add(layers.Conv2D(filters=64,kernel_size=(3,3),strides=(1,1),padding='same',activation='relu'))
clf.add(layers.MaxPool2D(pool_size=(2,2),strides=(2,2),padding='same'))
clf.add(layers.Conv2D(filters=128,kernel_size=(3,3),strides=(1,1),padding='same',activation='relu'))
clf.add(layers.MaxPool2D(pool_size=(2,2),strides=(2,2),padding='same'))
clf.add(layers.Flatten())
clf.add(layers.Dropout(0.4))
clf.add(layers.Dense(128,activation='relu'))
clf.add(layers.Dense(1,activation='sigmoid'))
clf.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
print(clf.summary())
# Training our classifier
clf.fit(xtrain,ytrain,epochs=50,batch_size=128,validation_data=(xtest,ytest),shuffle=True)
loss, accuracy = clf.evaluate(xtest,ytest)
print("Accuracy on validation data : ", accuracy)
#store_dir = '/content/drive/My\ Drive/Colab\ Notebooks/Colab\ Data'
!cd /content/drive/My\ Drive/Colab\ Notebooks/Colab\ Data
clf.save_weights('dogs_1.h5')
clf_json = clf.to_json()
with open("dogsmodel_1.json","w") as jsonfile:
jsonfile.write(clf_json)
# Testing on given test data.
!cd /content/competitions/dogs-vs-cats-redux-kernels-edition
!unzip test.zip
testY = []
testX = []
data_dir_test = os.path.join(base_dir,'train')
for f in tqdm(os.listdir(data_dir)):
if(f.split('.')[0] == 'dog'):
testY.append(1)
else:
testY.append(0)
img_file = os.path.join(data_dir_test,f)
img = cv2.imread(img_file)
img = cv2.resize(img,(IMG_SIZE, IMG_SIZE))
img = img/255
testX.append(img)
testX = np.array(testX)
testY = np.array(testY)
print("Accuracy on testing data : ",clf.evaluate(testX,testY)[1])