<jupyter_start><jupyter_text><jupyter_code>import keras, datetime
from keras.layers import Input, Dense, Conv2D
from keras.models import Model
from keras.callbacks import TensorBoard, ModelCheckpoint, ReduceLROnPlateau
# from keras.applications import mobilenetv2
import numpy as np


# !pip uninstall tensorflow
# !pip install tensorflow-gpu

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth, files
from oauth2client.client import GoogleCredentials
from mpl_toolkits.mplot3d import Axes3D
from sklearn.preprocessing import StandardScaler
from keras_preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt 
import matplotlib.image as mpimg

import numpy as np 
import pandas as pd 

from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, GlobalAveragePooling2D
import keras
from math import ceil
from keras.applications.mobilenet_v2 import MobileNetV2
from keras.applications.resnet50 import ResNet50
from keras.applications.inception_v3 import InceptionV3
# from keras.applications.inception_v3 import preprocess_input
# from keras.applications.resnet50 import preprocess_input
from keras.applications.xception import preprocess_input
import os 
import tensorflow as tf
from tensorflow.python.client import device_lib
from keras.callbacks import EarlyStopping, ModelCheckpoint
# from cyclicLR import CyclicLR
from keras.optimizers import Adam, SGD, RMSprop
import datetime
from keras.applications import xception
import cv2
# print(device_lib.list_local_devices())
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# import tensorflow.compat.v1 as tf
# tf.disable_v2_behavior()
from google.colab.patches import cv2_imshow

img_size = 224

mode = 'lmks'

output_size = 18

start_time = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')

data_00 = np.load('lmks_CAT_00.npy', allow_pickle=True)
data_01 = np.load('lmks_CAT_01.npy', allow_pickle=True)
data_02 = np.load('lmks_CAT_02.npy', allow_pickle=True)
data_03 = np.load('lmks_CAT_03.npy', allow_pickle=True)
data_04 = np.load('lmks_CAT_04.npy', allow_pickle=True)
data_05 = np.load('lmks_CAT_05.npy', allow_pickle=True)
data_06 = np.load('lmks_CAT_06.npy', allow_pickle=True)

print("Loaded data")

x_train = np.concatenate((data_00.item().get('imgs'), data_01.item().get('imgs'), data_02.item().get('imgs'), data_03.item().get('imgs'), data_04.item().get('imgs'), data_05.item().get('imgs')), axis=0)
y_train = np.concatenate((data_00.item().get(mode), data_01.item().get(mode), data_02.item().get(mode), data_03.item().get(mode), data_04.item().get(mode), data_05.item().get(mode)), axis=0)

print("Merged data")


x_test = np.array(data_06.item().get('imgs'))
y_test = np.array(data_06.item().get(mode))

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.

x_train = np.reshape(x_train, (-1, img_size, img_size, 3))
x_test = np.reshape(x_test, (-1, img_size, img_size, 3))

y_train = np.reshape(y_train, (-1, output_size))
y_test = np.reshape(y_test, (-1, output_size))

print("Here goes the NN")


input_shape = (224, 224, 3)
base_model = xception.Xception(input_shape=input_shape, weights='imagenet', include_top=False)

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(18)(x)

model = Model(inputs=base_model.input, outputs=predictions)

start_time = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
print("ALMOST THERE")

# model.summary()
# early_stopping = EarlyStopping(monitor='val_loss', patience=5)
# training
model.compile(optimizer=keras.optimizers.Adam(), loss='mse')


model.fit(x_train, y_train, epochs=50, batch_size=32, shuffle=True,
  validation_data=(x_test, y_test), verbose=1,
  callbacks=[
    # TensorBoard(log_dir='logs/%s' % (start_time)),
    ModelCheckpoint('./models/%s.h5' % (start_time), monitor='val_loss', verbose=1, save_best_only=True, mode='auto'),
    ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, verbose=1, mode='auto')
  ]
)
model.save('model_cats.h5')
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)


uploaded = drive.CreateFile({'title': 'model_cats.h5'})
uploaded.SetContentFile('model_cats.h5')
uploaded.Upload()
# uploaded.Download()
print('Uploaded file with ID {}'.format(uploaded.get('id')))<jupyter_output>Uploaded file with ID 1m9qT8ySfH77GyIzEzurVPFcE1JMkNpI-
{'fraction_non_alphanumeric': 0.13314385658067338, 'fraction_numerical': 0.030826410144293836, 'mean_word_length': 6.92894280762565, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '341369', 'license': 'MIT', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1675, 'n_words': 330, 'path': 'CatFeatures.ipynb', 'repo_name': 'RomulusGwelt/deep-learning-kbtu-2019', 'size': '4574'}