<jupyter_start><jupyter_text><jupyter_code>file_id = '1U9pYoh1sW245xFmm0FgMdhaWUD4Q1mc5'


# Download dataset
!gdown https://drive.google.com/uc?id={file_id}

# Unzip the downloaded file
!unzip -q human-or-animated.zip

file_id = '1-qPuicxaXSLpENG2RAA74apCNYZ48oXs'


# Download dataset
!gdown https://drive.google.com/uc?id={file_id}

# Unzip the downloaded file
!unzip -q validation-human-or-animated.zip
import os
import zipfile
# Directory with our training human pictures
train_human_dir = os.path.join('/content/human-or-animated/human')

# Directory with our training animated pictures
train_animated_dir = os.path.join('/content/human-or-animated/animated')

# Directory with our training human pictures
validation_human_dir = os.path.join('/content/validation-human-or-animated/human')

# Directory with our training animated pictures
validation_animated_dir = os.path.join('/content/validation-human-or-animated/animated')

train_human_names = os.listdir(train_human_dir)
print(train_human_names[:10])

train_animated_names = os.listdir(train_animated_dir)
print(train_animated_names[:10])

validation_human_hames = os.listdir(validation_human_dir)
print(validation_human_hames[:10])

validation_animated_names = os.listdir(validation_animated_dir)
print(validation_animated_names[:10])
print('total training human images:', len(os.listdir(train_human_dir)))
print('total training animated images:', len(os.listdir(train_animated_dir)))
print('total validation human images:', len(os.listdir(validation_human_dir)))
print('total validation animated images:', len(os.listdir(validation_animated_dir)))
ACCURACY_THRESHOLD = 0.98
class myCallback(tf.keras.callbacks.Callback): 
    def on_epoch_end(self, epoch, logs={}): 
        if(logs.get('acc') > ACCURACY_THRESHOLD):   
          print("\nReached 98 accuracy, so stopping training!!" )   
          self.model.stop_training = True

callbacks = myCallback()
import tensorflow as tf

model = tf.keras.models.Sequential([
    # This is the first convolution
    tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(300, 300, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # The second convolution
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The third convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The fourth convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The fifth convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(),
    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.summary()
from tensorflow.keras.optimizers import RMSprop

model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(learning_rate=0.001),
              metrics=['acc'])

from tensorflow.keras.preprocessing.image import ImageDataGenerator


train_datagen = ImageDataGenerator(rescale=1/255)
validation_datagen = ImageDataGenerator(rescale=1/255)

train_generator = train_datagen.flow_from_directory(
        '/content/human-or-animated/',  
        target_size=(300, 300),  
        batch_size=128,
        class_mode='binary')
validation_generator = validation_datagen.flow_from_directory(
        '/content/validation-human-or-animated/',  
        target_size=(300, 300),  
        batch_size=5,
        
        class_mode='binary')

history = model.fit_generator(
      train_generator,
      steps_per_epoch=8,  
      epochs=8,
      verbose=1,
      validation_data = validation_generator,
      validation_steps=8,
      callbacks=[callbacks])
import numpy as np
from google.colab import files
from keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys():
  path = '/content/' + fn
  img = image.load_img(path, target_size=(300, 300))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=10)
  print(classes[0])
  if classes[0]>0.5:
    print(fn + " is a human")
  else:
    print(fn + " is a animated")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12554811908608354, 'fraction_numerical': 0.029540733902607894, 'mean_word_length': 4.528061224489796, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '277837', 'license': 'MIT', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1483, 'n_words': 265, 'path': 'UpdatedHuman_or_aniated.ipynb', 'repo_name': 'Alpha-beta-LPU/Face-Detection-Models', 'size': '4333'}