File size: 5,508 Bytes
f52233f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Image Classification for Cat vs Dog Dataset (Fixed Version)
# Run in Google Colab with GPU

## 1. Setup Environment
!nvidia-smi
!pip install tensorboard-plugin-profile

## 2. Import Libraries
import tensorflow as tf
from tensorflow.keras import layers, models, callbacks
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt
import datetime
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
import os
import zipfile
from google.colab import files
from shutil import move
from pathlib import Path

print("TensorFlow version:", tf.__version__)

## 3. Upload and Reorganize Your Dataset
# Upload your zip file
uploaded = files.upload()
zip_filename = list(uploaded.keys())[0]

# Extract the zip file
with zipfile.ZipFile(zip_filename, 'r') as zip_ref:
    zip_ref.extractall('extracted_dataset')

# Verify extraction
!ls extracted_dataset

# Your dataset has images directly in custom_dataset/train/ (not in cat/dog subfolders)
# We need to reorganize them into proper class folders
def organize_dataset(input_dir, output_dir):
    # Create class directories
    os.makedirs(os.path.join(output_dir, 'cat'), exist_ok=True)
    os.makedirs(os.path.join(output_dir, 'dog'), exist_ok=True)
    
    # Move cat images
    for file in Path(input_dir).glob('cat.*.jpg'):
        move(str(file), os.path.join(output_dir, 'cat', file.name))
    
    # Move dog images
    for file in Path(input_dir).glob('dog.*.jpg'):
        move(str(file), os.path.join(output_dir, 'dog', file.name))

# Reorganize the dataset
input_path = 'extracted_dataset/custom_dataset/train'
output_path = 'organized_dataset/train'
organize_dataset(input_path, output_path)

# Verify the new structure
!ls organized_dataset/train
!ls organized_dataset/train/cat | head -5
!ls organized_dataset/train/dog | head -5

## 4. Create Data Generators
# Parameters
IMG_SIZE = (150, 150)
BATCH_SIZE = 32

# Data generators with augmentation
train_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    validation_split=0.2  # 20% for validation
)

# Training generator
train_generator = train_datagen.flow_from_directory(
    'organized_dataset/train',
    target_size=IMG_SIZE,
    batch_size=BATCH_SIZE,
    class_mode='binary',
    subset='training',
    shuffle=True
)

# Validation generator
validation_generator = train_datagen.flow_from_directory(
    'organized_dataset/train',
    target_size=IMG_SIZE,
    batch_size=BATCH_SIZE,
    class_mode='binary',
    subset='validation',
    shuffle=True
)

# Get class names
class_names = list(train_generator.class_indices.keys())
print("\nDetected classes:", class_names)
print("Number of training samples:", train_generator.samples)
print("Number of validation samples:", validation_generator.samples)

# Visualize samples
plt.figure(figsize=(12, 9))
for i in range(9):
    img, label = next(train_generator)
    plt.subplot(3, 3, i+1)
    plt.imshow(img[i])
    plt.title(class_names[int(label[i])])
    plt.axis('off')
plt.suptitle("Sample Training Images")
plt.show()

## 5. Build Model
def build_model(input_shape):
    model = models.Sequential([
        layers.Conv2D(32, (3,3), activation='relu', input_shape=input_shape),
        layers.MaxPooling2D((2,2)),
        
        layers.Conv2D(64, (3,3), activation='relu'),
        layers.MaxPooling2D((2,2)),
        
        layers.Conv2D(128, (3,3), activation='relu'),
        layers.MaxPooling2D((2,2)),
        
        layers.Flatten(),
        layers.Dense(512, activation='relu'),
        layers.Dropout(0.5),
        layers.Dense(1, activation='sigmoid')  # Binary output
    ])
    
    model.compile(
        optimizer='adam',
        loss='binary_crossentropy',
        metrics=['accuracy']
    )
    return model

model = build_model(input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3))
model.summary()

## 6. Train Model
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

callbacks = [
    callbacks.EarlyStopping(patience=5, restore_best_weights=True),
    callbacks.ModelCheckpoint('best_model.h5', save_best_only=True),
    callbacks.TensorBoard(log_dir=log_dir),
    callbacks.ReduceLROnPlateau(factor=0.1, patience=3)
]

history = model.fit(
    train_generator,
    steps_per_epoch=train_generator.samples // BATCH_SIZE,
    epochs=30,
    validation_data=validation_generator,
    validation_steps=validation_generator.samples // BATCH_SIZE,
    callbacks=callbacks
)

## 7. Evaluate Model
# Plot training history
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train')
plt.plot(history.history['val_accuracy'], label='Validation')
plt.title('Accuracy')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train')
plt.plot(history.history['val_loss'], label='Validation')
plt.title('Loss')
plt.legend()
plt.show()

## 8. Save Model
model.save('cat_dog_classifier.h5')

# Convert to TFLite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('cat_dog.tflite', 'wb') as f:
    f.write(tflite_model)

print("\nModel saved in HDF5 and TFLite formats")