<jupyter_start><jupyter_text>Classification of Horses vs HumansData downloaded from https://storage.googleapis.com/laurencemoroney-blog.appspot.com/horse-or-human.zip containing around 1280 CGI generated images of horses and humans in different poses.<jupyter_code>#Importing the necessary libraries
import os
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import Model
from os import getcwd
#Loading the pretrained inception model

INCEPTION_WEIGHTS = r"C:\Users\ku.kulshrestha\Downloads\inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5"

# Import the inception model  
from tensorflow.keras.applications.inception_v3 import InceptionV3

#Instance of the inception model from the local pre-trained weights
local_weights_file = INCEPTION_WEIGHTS

pre_trained_model = InceptionV3(input_shape=(150,150,3),
                               weights=None,
                               include_top=False)

pre_trained_model.load_weights(local_weights_file)

# Making all the layers in the pre-trained model non-trainable
for layer in pre_trained_model.layers:
    layer.trainable=False

pre_trained_model.summary()
#Using the outpt of mixed7 layer
last_layer = pre_trained_model.get_layer('mixed7')
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output
#Callback definition for stoping training at 97% accuracy 

class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('accuracy')>0.97):
      print("\nReached 97.0% accuracy so cancelling training!")
      self.model.stop_training = True

# Addition of final trainable layers

from tensorflow.keras.optimizers import RMSprop

x = layers.Flatten()(last_output)
x = layers.Dense(1024, activation='relu')(x)
x = layers.Dropout(0.2)(x)                  
x = layers.Dense  (1, activation='sigmoid')(x)           

model = Model(pre_trained_model.input, x) 

model.compile(optimizer = RMSprop(lr=0.0001), 
              loss = 'binary_crossentropy', 
              metrics = ['accuracy'])

model.summary()
#Unziping the data

path_horse_or_human = r"C:\Users\ku.kulshrestha\Documents\CourseraTensorflow\C2CNN\horse-or-human.zip"
path_validation_horse_or_human = r"C:\Users\ku.kulshrestha\Documents\CourseraTensorflow\C2CNN\validation-horse-or-human.zip"
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import os
import zipfile
import shutil

local_zip = path_horse_or_human
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall(r'C:\Users\ku.kulshrestha\Documents\github repos\mini-projects\data\horse-or-human-training')
zip_ref.close()

local_zip = path_validation_horse_or_human
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall(r'C:\Users\ku.kulshrestha\Documents\github repos\mini-projects\data\horse-or-human-validation')
zip_ref.close()
# Define our example directories and files
train_dir = r'C:\Users\ku.kulshrestha\Documents\github repos\mini-projects\data\horse-or-human-training'
validation_dir = r'C:\Users\ku.kulshrestha\Documents\github repos\mini-projects\data\horse-or-human-validation'

train_horses_dir = os.path.join(train_dir,'horses')
train_humans_dir = os.path.join(train_dir,'humans')
validation_horses_dir = os.path.join(validation_dir,'horses')
validation_humans_dir = os.path.join(validation_dir,'humans')

train_horses_fnames = os.listdir(train_horses_dir)
train_humans_fnames = os.listdir(train_humans_dir)
validation_horses_fnames = os.listdir(validation_horses_dir)
validation_humans_fnames = os.listdir(validation_humans_dir)

print(len(train_horses_fnames))
print(len(train_humans_fnames))
print(len(validation_horses_fnames))
print(len(validation_humans_fnames))

#Setting up training and validation ImageDataGenerators

train_datagen = ImageDataGenerator(rescale=1./255,
                                  horizontal_flip=True,
                                  shear_range=0.2,
                                  height_shift_range=0.2,
                                  width_shift_range=0.2,
                                  rotation_range=40,
                                  zoom_range=0.2,
                                  fill_mode='nearest')

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(directory=train_dir,
                                                   batch_size=20,
                                                   class_mode='binary',
                                                   target_size=(150,150))     

validation_generator =  test_datagen.flow_from_directory(directory=validation_dir,
                                                        batch_size=20,
                                                        class_mode='binary',
                                                        target_size=(150,150))
#Training the model

callbacks = myCallback()
history = model.fit_generator(train_generator, 
                             epochs=10,
                             validation_data=validation_generator,
                              steps_per_epoch=50,
                              validation_steps=50,
                              verbose=1,
                             callbacks=[callbacks])
#Plotting training and validation loss and accuracy

%matplotlib inline
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()


plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09195202503041891, 'fraction_numerical': 0.014253432991482705, 'mean_word_length': 3.0041753653444676, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '292538', 'license': 'MIT', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1699, 'n_words': 302, 'path': '2-horse-vs-humans-classifier/Horses-vs-Humans-using-Inception.ipynb', 'repo_name': 'kushkul/mini-projects', 'size': '5753'}