-
Notifications
You must be signed in to change notification settings - Fork 0
/
Course_2_Week_1_Project_2.py
118 lines (91 loc) · 4.2 KB
/
Course_2_Week_1_Project_2.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
115
116
117
118
"""
This is is a part of the DeepLearning.AI TensorFlow Developer Professional Certificate offered on Coursera.
All copyrights belong to them. I am sharing this work here to showcase the projects I have worked on
Course: Introduction to TensorFlow for Artificial Intelligence, Machine Learning, and Deep Learning
Week 1: Exploring a Larger Dataset
Aim: Binary classification using the Cats vs Dogs Full Dataset
"""
import os
import pathlib
import zipfile
import shutil
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import tensorflow as tf
import numpy as np
import random
from shutil import copyfile
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from os import path, getcwd, chdir
local_zip = f"{getcwd()}/Dataset/cats-and-dogs.zip"
#shutil.rmtree("/tmp")
zip_ref=zipfile.ZipFile(local_zip, "r")
zip_ref.extractall("/Dataset/cats-and-dogs")
zip_ref.close()
print(len(os.listdir("/Dataset/cats-and-dogs/PetImages/Cat")))
print(len(os.listdir("/Dataset/cats-and-dogs/PetImages/Dog")))
try:
os.mkdir("/Dataset/cats_v_dogs")
os.mkdir("/Dataset/cats_v_dogs/training")
os.mkdir("/Dataset/cats_v_dogs/testing")
os.mkdir("/Dataset/cats_v_dogs/training/cats")
os.mkdir("/Dataset/cats_v_dogs/training/dogs")
os.mkdir("/Dataset/cats_v_dogs/testing/cats")
os.mkdir("/Dataset/cats_v_dogs/testing/dogs")
except OSError:
pass
def split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):
files = []
for filename in os.listdir(SOURCE):
file = SOURCE + filename
if os.path.getsize(file) > 0:
files.append(filename)
else:
print(filename + "is zero length, so ignoring!")
training_length = int(len(files)*SPLIT_SIZE)
testing_length = int(len(files) - training_length)
shuffled_set = random.sample(files, len(files))
training_set = shuffled_set[0:training_length]
testing_set = shuffled_set[training_length:]
for filename in training_set:
this_file = SOURCE + filename
destination = TRAINING + filename
copyfile(this_file, destination)
for filename in testing_set:
this_file = SOURCE + filename
destination = TESTING + filename
copyfile(this_file, destination)
CAT_SOURCE_DIR = "/Dataset/PetImages/Cat/"
TRAINING_CATS_DIR = "/Dataset/cats_v_dogs/training/cats/"
TESTING_CATS_DIR = "/Dataset/cats_v_dogs/testing/cats/"
DOG_SOURCE_DIR = "/Dataset/PetImages/Dog/"
TRAINING_DOGS_DIR = "/Dataset/cats_v_dogs/training/dogs/"
TESTING_DOGS_DIR = "/Dataset/cats_v_dogs/testing/dogs/"
split_size = 0.9
split_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, TESTING_CATS_DIR, split_size)
split_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, TESTING_DOGS_DIR, split_size)
print(len(os.listdir("/Dataset/cats_v_dogs/training/cats")))
print(len(os.listdir("/Dataset/cats_v_dogs/training/dogs")))
print(len(os.listdir("/Dataset/cats_v_dogs/testing/cats")))
print(len(os.listdir("/Dataset/cats_v_dogs/testing/cats")))
model = tf.keras.models.Sequential([
# YOUR CODE HERE
tf.keras.layers.Conv2D(32, (3,3), activation = 'relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3,3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3,3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation = 'relu'),
tf.keras.layers.Dense(1, activation = 'sigmoid')
])
model.compile(optimizer=keras.optimizers.RMSprop(lr=0.001), loss='binary_crossentropy', metrics=['acc'])
training_dir = "/Dataset/cats_v_dogs/training"
validation_dir = "/Dataset/cats_v_dogs/testing"
train_datagen = ImageDataGenerator(rescale=1/255)
validation_datagen = ImageDataGenerator(rescale=1/255)
train_generator = train_datagen.flow_from_directory(training_dir, batch_size = 16, class_mode="binary", target_size=(150,150))
validation_generator = train_datagen.flow_from_directory(validation_dir, batch_size = 16, class_mode="binary", target_size=(150,150))
model.fit(train_generator, validation_data=validation_generator, epochs= 30)