<jupyter_start><jupyter_text>Image Classifier for Dogs and Cats<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import os

plt.rcParams['figure.figsize'] = 15, 8
from keras import Input, Model
from keras.layers import Conv2D, Flatten, Dense, MaxPool2D, Dropout
from keras import optimizers, losses, metrics
from keras.preprocessing.image import ImageDataGenerator
from keras import regularizers
DATADIR = '/Volumes/Study/GitHub/data/cats-dogs/'
LOGDIR = '/Volumes/Study/GitHub/neural-nets/graphs/cats-dogs/'
train_dir = os.path.join(DATADIR + 'training/')
validation_dir = os.path.join(DATADIR + 'validation/')
test_dir = os.path.join(DATADIR + 'test/')
print(train_dir)
print(validation_dir)
print(test_dir)
ip = Input(batch_shape=(None, 150, 150, 3), name='input')

x = Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu', name='conv1')(ip)
x = MaxPool2D(pool_size=(2, 2), name='pool1')(x)

x = Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu', name='conv2')(x)
x = MaxPool2D(pool_size=(2, 2), name='pool2')(x)

x = Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='relu', name='conv3')(x)
x = MaxPool2D(pool_size=(2, 2), name='pool3')(x)

x = Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='relu', name='conv4')(x)
x = MaxPool2D(pool_size=(2, 2), name='pool4')(x)

x = Flatten(name='flatten')(x)
x = Dropout(rate=0.3, name='dropout1')(x)

x = Dense(units=128, activation='relu', name='dense1')(x)
x = Dropout(rate=0.3, name='dropout2')(x)

output = Dense(units=1, activation='sigmoid', name='output')(x)

model = Model(inputs=ip, outputs=output)
model.summary()
loss = losses.binary_crossentropy
opt = optimizers.Adam(lr=1e-5)
metric = metrics.binary_accuracy
model.compile(loss=loss, optimizer=opt, metrics=[metric])
train_datagen = ImageDataGenerator(rescale=1./255)
validation_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
                    train_dir,
                    target_size=(150, 150),
                    classes=['cats', 'dogs'],
                    batch_size=20, 
                    class_mode='binary')
validiation_generator = train_datagen.flow_from_directory(
                            validation_dir,
                            target_size=(150, 150),
                            classes=['cats', 'dogs'],
                            batch_size=20, 
                            class_mode='binary')
history = model.fit_generator(train_generator,
                             steps_per_epoch=100,
                             epochs=20,
                             validation_data=validiation_generator,
                             validation_steps=50)

model.save(os.path.join(LOGDIR + 'cat_dog_v1_1.h5'))
plt.plot(history.history['loss'], label = 'training_loss')
plt.plot(history.history['val_loss'], label = 'validation_loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training and Validation loss')
plt.legend()
plt.show()
plt.plot(history.history['binary_accuracy'], label = 'training_accuracy')
plt.plot(history.history['val_binary_accuracy'], label = 'validation_accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15008987417615338, 'fraction_numerical': 0.03025763930497304, 'mean_word_length': 3.9466666666666668, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '354141', 'license': 'MIT', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1116, 'n_words': 202, 'path': '24.cats_and_dogs_v1.ipynb', 'repo_name': 'mkumar73/neural-nets', 'size': '3338'}