<jupyter_start><jupyter_text>Classification: CATS vs DOGS     jpeg -> CNN Keras -> [0,1]--- Import Tensorflow with Keras and Packages<jupyter_code>import tensorflow as tf
from time import time
from tensorflow.python.keras.callbacks import TensorBoard
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Activation, Dropout, Flatten
from keras.utils import to_categorical
from keras import backend as K
from keras.preprocessing.image import array_to_img, img_to_array, load_img, ImageDataGenerator
from PIL import Image
from matplotlib import pyplot as plt
%matplotlib inline 
import os<jupyter_output><empty_output><jupyter_text>--- FOLDERS and CATEGORIES: - Folders<jupyter_code>datadir = 'dogVScat'
cat1 = 'dogs'
cat2 = 'cats'<jupyter_output><empty_output><jupyter_text>- Training set<jupyter_code>target = os.chdir(datadir + "/train/" + cat1)
cat_1 = os.listdir()
target = os.chdir("../" + cat2)
cat_2 = os.listdir()
print("CAT1:",len(cat_1),"CAT2:",len(cat_2), "TOTAL:",len(cat_1)+len(cat_2))<jupyter_output>CAT1: 5501 CAT2: 6054 TOTAL: 11555<jupyter_text>- Validation set<jupyter_code>target = os.chdir("../../validation/" + cat1)
cat_1_val = os.listdir()
target = os.chdir("../" + cat2)
cat_2_val = os.listdir()
print("CAT1:",len(cat_1_val),"CAT2:",len(cat_2_val), "TOTAL:",len(cat_1_val)+len(cat_2_val))<jupyter_output>CAT1: 1302 CAT2: 1500 TOTAL: 2802<jupyter_text>- Return to Home<jupyter_code>os.chdir("../../../")<jupyter_output><empty_output><jupyter_text>--- HYPER PARAMETERS: - Dimensions- Epochs- Batch<jupyter_code>###
img_width, img_height = 300, 300

data_dir = 'dogVScat'
train_data_dir = data_dir + '/train'
validation_data_dir = data_dir + '/validation'
test_data_dir = data_dir + '/test'

nb_train_samples = len(cat_1)+len(cat_2)
nb_validation_samples = len(cat_1_val)+len(cat_2_val)
epochs = 5                                                 # 50
batch_size = 32                                            # 16<jupyter_output><empty_output><jupyter_text>--- CNN MODEL: - Layers Architecture<jupyter_code>if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

print(model.summary())
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>- Data Generators<jupyter_code>train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)<jupyter_output><empty_output><jupyter_text>- Train VS Validation SET<jupyter_code>train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')<jupyter_output>Found 2802 images belonging to 2 classes.<jupyter_text>- Visualize on Tensorboard<jupyter_code>tensorboard = TensorBoard(log_dir="logs/{}".format(time()))<jupyter_output><empty_output><jupyter_text>- Model Training<jupyter_code>history = model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size, callbacks=[tensorboard])<jupyter_output>Epoch 1/5
361/361 [==============================] - 553s 2s/step - loss: 0.5096 - acc: 0.7584 - val_loss: 0.5269 - val_acc: 0.7845
Epoch 2/5
361/361 [==============================] - 564s 2s/step - loss: 0.5045 - acc: 0.7630 - val_loss: 0.4669 - val_acc: 0.7888
Epoch 3/5
361/361 [==============================] - 585s 2s/step - loss: 0.4921 - acc: 0.7645 - val_loss: 0.4461 - val_acc: 0.8036
Epoch 4/5
361/361 [==============================] - 544s 2s/step - loss: 0.4856 - acc: 0.7759 - val_loss: 0.4608 - val_acc: 0.7957
Epoch 5/5
361/361 [==============================] - 559s 2s/step - loss: 0.4821 - acc: 0.7832 - val_loss: 0.5391 - val_acc: 0.7996<jupyter_text>- Visualize Learning Rate<jupyter_code>plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])

plt.title('Model Accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['cnn:train', 'cnn:test'], loc='upper left')
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])

plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['cnn:train', 'cnn:test'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>- Save<jupyter_code>model.save_weights('keras_weights.h5')
model.save('keras_model.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17421668554171385, 'fraction_numerical': 0.053227633069082674, 'mean_word_length': 5.758928571428571, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '578439', 'license': 'MIT', 'n_tokens_mistral': 2196, 'n_tokens_neox': 1982, 'n_words': 335, 'path': '5_CNN_Keras_TF.ipynb', 'repo_name': 'e-petrachi/DeepLearningNotebooks', 'size': '5298'}