<jupyter_start><jupyter_text>"""

author: <NAME>,

index: s12623,

email: <EMAIL>

To run module:

import module into Google Colaboratory notebook and run.

This module recognize type of animal according to given image of animal.

Keras model is build as classification type and contains two types of classification neural network architecture.

""" **First model**<jupyter_code>%tensorflow_version 2.x
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense, Dropout, Conv2D, MaxPooling2D
from keras.utils import to_categorical
import matplotlib.pyplot as plt
import random
print('Tensorflow version: ', tf.__version__)
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()
print('before')
print('X_train size: ', len(X_train))
print('y_train size: ', len(y_train))
print()
print('still work:')
i = 0 
while not i == len(y_train):

  if y_train[i][0] in {0, 1, 8, 9}:
    X_train = np.delete(X_train, i, 0)
    y_train = np.delete(y_train, i, 0)
    i = i - 1
  i = i + 1

  if (i % (len(y_train) // 100)) == 0:
    print('{:.0%}'.format(i / len(y_train)), end=' ')
  if (i % (len(y_train) // 10)) == 0:
    print()

print()
print('done.')
print()
print('actual:')
print('X_train size: ', len(X_train))
print('y_train size: ', len(y_train))
print('before')
print('X_test size: ', len(X_test))
print('y_test size: ', len(y_test))
print()
print('still work:')
i = 0 
while not i == len(y_test):

  if y_test[i][0] in {0, 1, 8, 9}:
    X_test = np.delete(X_test, i, 0)
    y_test = np.delete(y_test, i, 0)
    i = i - 1
  i = i + 1

  if (i % (len(y_test) // 100)) == 0:
    print('{:.0%}'.format(i / len(y_test)), end=' ')
  if (i % (len(y_test) // 10)) == 0:
    print()

print()
print('done.')
print()
print('actual:')
print('X_test size: ', len(X_test))
print('y_test size: ', len(y_test))
idx = 0
for y in y_train:
  if y[0] == 2:
    y_train[idx][0] = 0
  if y[0] == 3:
    y_train[idx][0] = 1
  if y[0] == 4:
    y_train[idx][0] = 2
  if y[0] == 5:
    y_train[idx][0] = 3
  if y[0] == 6:
    y_train[idx][0] = 4
  if y[0] == 7:
    y_train[idx][0] = 5
  idx = idx + 1

idx = 0
for y in y_test:
  if y[0] == 2:
    y_test[idx][0] = 0
  if y[0] == 3:
    y_test[idx][0] = 1
  if y[0] == 4:
    y_test[idx][0] = 2
  if y[0] == 5:
    y_test[idx][0] = 3
  if y[0] == 6:
    y_test[idx][0] = 4
  if y[0] == 7:
    y_test[idx][0] = 5
  idx = idx + 1

y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test) 

print(y_train_cat.shape, y_test_cat.shape)

model = Sequential()
model.add(Conv2D(filters=32, 
                 kernel_size=(3, 3),
                 activation='relu',
                 input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=64, 
                 kernel_size=(3, 3),
                 activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=128, 
                 kernel_size=(3, 3),
                 activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(6, activation='softmax'))

model.summary()

model.compile(
            optimizer='adam', 
            loss='categorical_crossentropy',
            metrics=['categorical_accuracy'])
model.fit(X_train, y_train_cat, epochs=20, validation_data=(X_test, y_test_cat))
f, axarr = plt.subplots()
f.set_size_inches(16, 5)

animals_cifar10_map = {
    0: 'bird',
    1: 'cat',
    2: 'deer',
    3: 'dog',
    4: 'frog',
    5: 'horse'
}

i = random.randrange(0, len(X_test))
print(animals_cifar10_map[y_test[i][0]])
img = X_test[i]
axarr.imshow(img)

img_rgb = X_test[i].reshape(1,32,32,3) 
propability = model.predict(img_rgb)
max_prop_idx = np.argmax(propability)
predicted_animal = animals_cifar10_map[max_prop_idx]
print('prediction of animal: ', predicted_animal)<jupyter_output>dog
prediction of animal:  dog<jupyter_text>**Second model**<jupyter_code>model = Sequential()
model.add(Conv2D(filters=32, 
                 kernel_size=(2, 2),
                 activation='relu',
                 input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=64, 
                 kernel_size=(2, 2),
                 activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(filters=128, 
                 kernel_size=(2, 2),
                 activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(2048, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(6, activation='softmax'))

model.summary()

model.compile(
            optimizer='adam', 
            loss='categorical_crossentropy',
            metrics=['categorical_accuracy'])
model.fit(X_train, y_train_cat, epochs=20, validation_data=(X_test, y_test_cat))
f, axarr = plt.subplots()
f.set_size_inches(16, 5)

animals_cifar10_map = {
    0: 'bird',
    1: 'cat',
    2: 'deer',
    3: 'dog',
    4: 'frog',
    5: 'horse'
}

i = random.randrange(0, len(X_test))
print(animals_cifar10_map[y_test[i][0]])
img = X_test[i]
axarr.imshow(img)

img_rgb = X_test[i].reshape(1,32,32,3) 
propability = model.predict(img_rgb)
max_prop_idx = np.argmax(propability)
predicted_animal = animals_cifar10_map[max_prop_idx]
print('prediction of animal: ', predicted_animal)<jupyter_output>bird
prediction of animal:  bird
{'fraction_non_alphanumeric': 0.16608816383200278, 'fraction_numerical': 0.040437348143005904, 'mean_word_length': 3.2250733137829912, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '240904', 'license': 'MIT', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2304, 'n_words': 459, 'path': 'Neural_Networks/classification/2_neural_net_NAI_CIFAR10_animals.ipynb', 'repo_name': 'dccstcc/NAI_PJATK_2020', 'size': '5778'}