{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Dog Breed Identification\n\nIn this competition, given an image of a dog we are asked to predict a probability for each of the different 120 breeds of the dogs.\n\nSeveral experts online argue that VAEs are the most powerful neural networks that we have.","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)\n#this might be a problem the docstring says that tensorflow is only supported through 2.1, unless it's tensorflow-gpu \n#I am going to try first, then troubleshoot if there's a problem","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:03.881936Z","iopub.execute_input":"2022-06-29T19:37:03.882398Z","iopub.status.idle":"2022-06-29T19:37:08.834611Z","shell.execute_reply.started":"2022-06-29T19:37:03.882365Z","shell.execute_reply":"2022-06-29T19:37:08.83377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install livelossplot","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-06-29T19:37:08.836128Z","iopub.execute_input":"2022-06-29T19:37:08.836869Z","iopub.status.idle":"2022-06-29T19:37:08.840796Z","shell.execute_reply.started":"2022-06-29T19:37:08.83683Z","shell.execute_reply":"2022-06-29T19:37:08.839765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\n\nimport cv2\nimport PIL\nimport os\nimport pathlib\nimport shutil\nfrom IPython.display import Image, display\n\n# Plotly for the interactive viewer (see last section)\nimport plotly.graph_objs as go\nimport plotly.graph_objects as go\nfrom tensorflow.keras.models import Model\nfrom sklearn.metrics import cohen_kappa_score\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential, Model,load_model\nfrom tensorflow.keras.applications.vgg16 import VGG16,preprocess_input\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.xception import Xception, preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array\nfrom tensorflow.keras import Input\nfrom tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler \nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten,BatchNormalization,Activation\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import Adam, SGD, RMSprop\nfrom tensorflow.keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers.experimental import preprocessing\n\nimport gc\nimport skimage.io\n#from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:41.249467Z","iopub.execute_input":"2022-06-29T19:38:41.24993Z","iopub.status.idle":"2022-06-29T19:38:41.261071Z","shell.execute_reply.started":"2022-06-29T19:38:41.249894Z","shell.execute_reply":"2022-06-29T19:38:41.260408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_src = '../input/dog-breed-identification/train'\n# test_src = '../input/dog-breed-identification/test'\n\n# train_labels = pd.read_csv('../input/dog-breed-identification/labels.csv', index_col = 'id')\n# submission=pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.24815Z","iopub.status.idle":"2022-06-29T19:37:10.24846Z","shell.execute_reply.started":"2022-06-29T19:37:10.248285Z","shell.execute_reply":"2022-06-29T19:37:10.248304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_size = len(os.listdir(train_src))\n# test_size = len(os.listdir(test_src))\n\n# print(train_size,test_size)\n# print(train_labels.shape)\n# print(submission.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.251937Z","iopub.status.idle":"2022-06-29T19:37:10.252778Z","shell.execute_reply.started":"2022-06-29T19:37:10.252543Z","shell.execute_reply":"2022-06-29T19:37:10.252567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# target, dog_breeds = pd.factorize(train_labels['breed'], sort = True)\n# train_labels['target'] = target\n\n# print(dog_breeds)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.254028Z","iopub.status.idle":"2022-06-29T19:37:10.254649Z","shell.execute_reply.started":"2022-06-29T19:37:10.254395Z","shell.execute_reply":"2022-06-29T19:37:10.254433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display(train_labels.head())\n# display(submission.head())","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.255931Z","iopub.status.idle":"2022-06-29T19:37:10.256415Z","shell.execute_reply.started":"2022-06-29T19:37:10.256169Z","shell.execute_reply":"2022-06-29T19:37:10.256193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_labels['breed'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:03.872114Z","iopub.status.idle":"2022-06-29T19:37:03.872831Z","shell.execute_reply.started":"2022-06-29T19:37:03.87254Z","shell.execute_reply":"2022-06-29T19:37:03.87258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(15, 10))\n# train_labels['breed'].value_counts().plot(kind='bar')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.257911Z","iopub.status.idle":"2022-06-29T19:37:10.258746Z","shell.execute_reply.started":"2022-06-29T19:37:10.258516Z","shell.execute_reply":"2022-06-29T19:37:10.25854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare the Train and Test Data Directories","metadata":{}},{"cell_type":"code","source":"cd /kaggle/input/dog-breed-identification/","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:49.499791Z","iopub.execute_input":"2022-06-29T19:38:49.500264Z","iopub.status.idle":"2022-06-29T19:38:49.507216Z","shell.execute_reply.started":"2022-06-29T19:38:49.500228Z","shell.execute_reply":"2022-06-29T19:38:49.506371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# try:\n#     data_dir = pathlib.Path('/kaggle/input/dog-breed-identification/')\n# except FileExistsError:\n#     pass\n","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.261638Z","iopub.status.idle":"2022-06-29T19:37:10.262496Z","shell.execute_reply.started":"2022-06-29T19:37:10.262238Z","shell.execute_reply":"2022-06-29T19:37:10.262262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# try:\n#      data_dir.mkdir()\n# except FileExistsError:\n#      pass\n# #I don't know what's up thoug","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.263589Z","iopub.status.idle":"2022-06-29T19:37:10.264562Z","shell.execute_reply.started":"2022-06-29T19:37:10.264281Z","shell.execute_reply":"2022-06-29T19:37:10.264308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dir = data_dir / 'train'\n# test_dir = data_dir / 'test'\n\n# train_dir.mkdir()\n# test_dir.mkdir()","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.265611Z","iopub.status.idle":"2022-06-29T19:37:10.266482Z","shell.execute_reply.started":"2022-06-29T19:37:10.266221Z","shell.execute_reply":"2022-06-29T19:37:10.266245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ## Create a directory for each dog breed classes\n# try:\n#     for breed in dog_breeds:\n#         breed_dir = train_dir / breed\n#         breed_dir.mkdir()\n# except OSError:\n#         pass","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.267518Z","iopub.status.idle":"2022-06-29T19:37:10.268371Z","shell.execute_reply.started":"2022-06-29T19:37:10.268132Z","shell.execute_reply":"2022-06-29T19:37:10.268155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for root, dirs, files in os.walk(train_src):\n#     for file in files:\n#         imgName = file.split('.')[0]\n#         src_file = os.path.join(train_src, file)\n#         destination = os.path.join(train_dir, train_labels.loc[imgName, 'breed'])\n#         shutil.copy2(src_file, destination)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-06-29T19:37:10.269441Z","iopub.status.idle":"2022-06-29T19:37:10.270245Z","shell.execute_reply.started":"2022-06-29T19:37:10.270022Z","shell.execute_reply":"2022-06-29T19:37:10.270045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shutil.copytree(test_src, test_dir / 'images')","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.271277Z","iopub.status.idle":"2022-06-29T19:37:10.27215Z","shell.execute_reply.started":"2022-06-29T19:37:10.271924Z","shell.execute_reply":"2022-06-29T19:37:10.271947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cd /kaggle/input/dog-breed-identification/","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:37:10.27323Z","iopub.status.idle":"2022-06-29T19:37:10.274233Z","shell.execute_reply.started":"2022-06-29T19:37:10.273967Z","shell.execute_reply":"2022-06-29T19:37:10.273994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom IPython import display\n\nimport glob\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport PIL\nimport tensorflow as tf\nimport time","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:53.293928Z","iopub.execute_input":"2022-06-29T19:38:53.294624Z","iopub.status.idle":"2022-06-29T19:38:53.299108Z","shell.execute_reply.started":"2022-06-29T19:38:53.294588Z","shell.execute_reply":"2022-06-29T19:38:53.298237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import imageio\nimport tensorflow_probability as tfp","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:53.791099Z","iopub.execute_input":"2022-06-29T19:38:53.791524Z","iopub.status.idle":"2022-06-29T19:38:54.510548Z","shell.execute_reply.started":"2022-06-29T19:38:53.79149Z","shell.execute_reply":"2022-06-29T19:38:54.509733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image, display","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:54.512064Z","iopub.execute_input":"2022-06-29T19:38:54.512295Z","iopub.status.idle":"2022-06-29T19:38:54.519268Z","shell.execute_reply.started":"2022-06-29T19:38:54.512263Z","shell.execute_reply":"2022-06-29T19:38:54.518537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:54.582959Z","iopub.execute_input":"2022-06-29T19:38:54.58329Z","iopub.status.idle":"2022-06-29T19:38:54.58769Z","shell.execute_reply.started":"2022-06-29T19:38:54.583258Z","shell.execute_reply":"2022-06-29T19:38:54.586457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split ","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:55.138952Z","iopub.execute_input":"2022-06-29T19:38:55.139206Z","iopub.status.idle":"2022-06-29T19:38:55.144028Z","shell.execute_reply.started":"2022-06-29T19:38:55.139179Z","shell.execute_reply":"2022-06-29T19:38:55.143099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_images_path = \"../input/dog-breed-identification/train/\"\n# test_images_path = \"../input/dog-breed-identification/test/\"","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:56.031269Z","iopub.execute_input":"2022-06-29T19:38:56.031923Z","iopub.status.idle":"2022-06-29T19:38:56.035218Z","shell.execute_reply.started":"2022-06-29T19:38:56.031884Z","shell.execute_reply":"2022-06-29T19:38:56.034478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Image(train_images_path + '000bec180eb18c7604dcecc8fe0dba07.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:56.860773Z","iopub.execute_input":"2022-06-29T19:38:56.861037Z","iopub.status.idle":"2022-06-29T19:38:56.864873Z","shell.execute_reply.started":"2022-06-29T19:38:56.86101Z","shell.execute_reply":"2022-06-29T19:38:56.864048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I accidentally deleted two very important looking lines of code","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:58.03041Z","iopub.execute_input":"2022-06-29T19:38:58.030893Z","iopub.status.idle":"2022-06-29T19:38:58.034321Z","shell.execute_reply.started":"2022-06-29T19:38:58.030858Z","shell.execute_reply":"2022-06-29T19:38:58.033402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def is_equal_images(target_dir, target_df): \n#     \"\"\"\n#     This function will check if target_dir images are equal to image list in target_df\n#     \"\"\"\n#     len_target_dir = len(os.listdir(target_dir))\n#     len_target_df = len(target_df)\n#     if len_target_dir == len_target_df: \n#         print(f\"Both are having same no of images:{len_target_dir}\")\n#     else: \n#         print(f\"Target dir having {len_target_dir} images while Target DF having {len_target_df}\")\n        \n# is_equal_images(target_dir = train_images_path, target_df = labels)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:58.745913Z","iopub.execute_input":"2022-06-29T19:38:58.746631Z","iopub.status.idle":"2022-06-29T19:38:58.75011Z","shell.execute_reply.started":"2022-06-29T19:38:58.746598Z","shell.execute_reply":"2022-06-29T19:38:58.74929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# labels['breed'].value_counts().plot.bar(figsize=(20,10))\n# print(f\"Average Images per breed:{int(labels['breed'].value_counts().sum()/len(labels['breed'].unique()))}\")\n# print(f\"Total no of breeds:{len(labels['breed'].unique())}\")","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:59.119094Z","iopub.execute_input":"2022-06-29T19:38:59.119582Z","iopub.status.idle":"2022-06-29T19:38:59.124721Z","shell.execute_reply.started":"2022-06-29T19:38:59.119549Z","shell.execute_reply":"2022-06-29T19:38:59.12253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class_names = labels['breed'].unique()\n# class_names[:10]","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:59.443066Z","iopub.execute_input":"2022-06-29T19:38:59.443348Z","iopub.status.idle":"2022-06-29T19:38:59.447567Z","shell.execute_reply.started":"2022-06-29T19:38:59.443317Z","shell.execute_reply":"2022-06-29T19:38:59.446684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create an array of train images \n# filenames = [train_images_path + fname + '.jpg' for fname in labels['id']]\n# filenames[:10]","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:59.738937Z","iopub.execute_input":"2022-06-29T19:38:59.739333Z","iopub.status.idle":"2022-06-29T19:38:59.743597Z","shell.execute_reply.started":"2022-06-29T19:38:59.739295Z","shell.execute_reply":"2022-06-29T19:38:59.742244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# target_labels = [breed for breed in labels['breed']]\n# target_labels[:10]","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:38:59.975523Z","iopub.execute_input":"2022-06-29T19:38:59.975734Z","iopub.status.idle":"2022-06-29T19:38:59.983765Z","shell.execute_reply.started":"2022-06-29T19:38:59.97571Z","shell.execute_reply":"2022-06-29T19:38:59.982921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# target_labels_encoded = [label == np.array(class_names) for label in target_labels]\n# target_labels_encoded[:2]","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:00.292448Z","iopub.execute_input":"2022-06-29T19:39:00.292902Z","iopub.status.idle":"2022-06-29T19:39:00.296554Z","shell.execute_reply.started":"2022-06-29T19:39:00.292871Z","shell.execute_reply":"2022-06-29T19:39:00.295631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(target_labels[0])\n# target_labels[0] == class_names","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:00.535749Z","iopub.execute_input":"2022-06-29T19:39:00.536073Z","iopub.status.idle":"2022-06-29T19:39:00.539593Z","shell.execute_reply.started":"2022-06-29T19:39:00.536044Z","shell.execute_reply":"2022-06-29T19:39:00.538682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(target_labels[0]) # original label\n# print(np.where(class_names == target_labels[0])[0][0]) # index where label occurs\n# print(target_labels_encoded[0].argmax()) # index where label occurs in boolean array\n# print(target_labels_encoded[0].astype(int)) # there will be a 1 where the sample label occurs","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:00.827457Z","iopub.execute_input":"2022-06-29T19:39:00.827887Z","iopub.status.idle":"2022-06-29T19:39:00.831874Z","shell.execute_reply.started":"2022-06-29T19:39:00.827858Z","shell.execute_reply":"2022-06-29T19:39:00.830895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import tensorflow.compat.v1 as tf\n# tf.Session()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:43:26.185815Z","iopub.execute_input":"2022-06-29T19:43:26.186526Z","iopub.status.idle":"2022-06-29T19:43:26.190136Z","shell.execute_reply.started":"2022-06-29T19:43:26.186491Z","shell.execute_reply":"2022-06-29T19:43:26.189477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Experiement with small data 1000 images \n# NUM_IMAGES = 2000\n\n# #Split data into training & validation \n# X_train, X_val, Y_train, Y_val = train_test_split(filenames, target_labels_encoded, test_size=0.2, random_state=42)\n\n# len(X_train), len(X_val), len(Y_train), len(Y_val)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:01.320986Z","iopub.execute_input":"2022-06-29T19:39:01.321224Z","iopub.status.idle":"2022-06-29T19:39:01.325919Z","shell.execute_reply.started":"2022-06-29T19:39:01.321199Z","shell.execute_reply":"2022-06-29T19:39:01.324259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SIZE = [224, 224]\nBATCH_SIZE = 32\nSEED = 42","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:01.576256Z","iopub.execute_input":"2022-06-29T19:39:01.576524Z","iopub.status.idle":"2022-06-29T19:39:01.581187Z","shell.execute_reply.started":"2022-06-29T19:39:01.576497Z","shell.execute_reply":"2022-06-29T19:39:01.580438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# IMAGE_SIZE = 224\n\n# # Creating a function to preprocess the images\n# def preprocess_images(image_path):\n    \n#     # Read in the image\n#     image = tf.io.read_file(image_path)\n    \n#     # Turn the image into numerical tensors\n#     image = tf.image.decode_jpeg(image, channels=3)\n    \n#     # Convert the color channel values from 0-225 to 0-1\n#     image = tf.image.convert_image_dtype(image, tf.float32)\n    \n#     # Resize the image\n#     image = tf.image.resize(image, size=[IMAGE_SIZE, IMAGE_SIZE])\n    \n#     return image","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:02.188706Z","iopub.execute_input":"2022-06-29T19:39:02.189301Z","iopub.status.idle":"2022-06-29T19:39:02.193745Z","shell.execute_reply.started":"2022-06-29T19:39:02.189266Z","shell.execute_reply":"2022-06-29T19:39:02.192611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def preprocess_images(images):\n#     images = np.array(images)\n#     images = np.reshape(images,(images.shape[0], 10, 10, 1)) / 255.\n#     return np.where(images > .5, 1.0, 0.0).astype('float32')\n\n# train_images = preprocess_images(X_train)\n# test_images = preprocess_images(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:02.803794Z","iopub.execute_input":"2022-06-29T19:39:02.804526Z","iopub.status.idle":"2022-06-29T19:39:02.80769Z","shell.execute_reply.started":"2022-06-29T19:39:02.804494Z","shell.execute_reply":"2022-06-29T19:39:02.806854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_generator = ImageDataGenerator(rescale= 1./255, validation_split=0.2, rotation_range=20,\n                                    zoom_range=0.1, width_shift_range=0.2, height_shift_range=0.2,\n                                    shear_range=0.1, horizontal_flip=True, fill_mode=\"nearest\")","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:02.971653Z","iopub.execute_input":"2022-06-29T19:39:02.971985Z","iopub.status.idle":"2022-06-29T19:39:02.976787Z","shell.execute_reply.started":"2022-06-29T19:39:02.971957Z","shell.execute_reply":"2022-06-29T19:39:02.976033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:20.670045Z","iopub.execute_input":"2022-06-29T19:39:20.670798Z","iopub.status.idle":"2022-06-29T19:39:20.715871Z","shell.execute_reply.started":"2022-06-29T19:39:20.670752Z","shell.execute_reply":"2022-06-29T19:39:20.715132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['id'] = labels['id'].apply(lambda x: x + '.jpg')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:39:22.769354Z","iopub.execute_input":"2022-06-29T19:39:22.769835Z","iopub.status.idle":"2022-06-29T19:39:22.790623Z","shell.execute_reply.started":"2022-06-29T19:39:22.769803Z","shell.execute_reply":"2022-06-29T19:39:22.789613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['breed'].value_counts().plot.bar(figsize=(16, 8))","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:40:11.092555Z","iopub.execute_input":"2022-06-29T19:40:11.093075Z","iopub.status.idle":"2022-06-29T19:40:13.329655Z","shell.execute_reply.started":"2022-06-29T19:40:11.093041Z","shell.execute_reply":"2022-06-29T19:40:13.328891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = data_generator.flow_from_dataframe(labels, directory='../input/dog-breed-identification/train', x_col='id', y_col='breed', target_size=SIZE, class_mode='categorical', batch_size=BATCH_SIZE, shuffle=True, seed=SEED, subset='training', validate_filenames=False)\nval_generator = data_generator.flow_from_dataframe(labels, directory='../input/dog-breed-identification/train', x_col='id', y_col='breed', target_size=SIZE, class_mode='categorical', batch_size=BATCH_SIZE, shuffle=True, seed=SEED, subset='validation', validate_filenames=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:46:37.98998Z","iopub.execute_input":"2022-06-29T19:46:37.990238Z","iopub.status.idle":"2022-06-29T19:46:38.055332Z","shell.execute_reply.started":"2022-06-29T19:46:37.99021Z","shell.execute_reply":"2022-06-29T19:46:38.054414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, label = next(train_generator)\n\nfig = plt.figure(figsize=(15, 10))\n\nfor i in range(12):\n    fig.add_subplot(3, 4, i+1)\n    plt.imshow(img[i])\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:48:06.751104Z","iopub.execute_input":"2022-06-29T19:48:06.751974Z","iopub.status.idle":"2022-06-29T19:48:06.781993Z","shell.execute_reply.started":"2022-06-29T19:48:06.751925Z","shell.execute_reply":"2022-06-29T19:48:06.780863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = 60000\nbatch_size = 32\ntest_size = 10000","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:40:00.977064Z","iopub.execute_input":"2022-06-29T17:40:00.977652Z","iopub.status.idle":"2022-06-29T17:40:00.984371Z","shell.execute_reply.started":"2022-06-29T17:40:00.977617Z","shell.execute_reply":"2022-06-29T17:40:00.983596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = (tf.data.Dataset.from_tensor_slices(X_train)\n                 .shuffle(train_size).batch(batch_size))\ntest_dataset = (tf.data.Dataset.from_tensor_slices(X_val)\n                .shuffle(test_size).batch(batch_size))","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:41:39.43607Z","iopub.execute_input":"2022-06-29T17:41:39.436598Z","iopub.status.idle":"2022-06-29T17:41:39.470904Z","shell.execute_reply.started":"2022-06-29T17:41:39.436552Z","shell.execute_reply":"2022-06-29T17:41:39.469946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CVAE(tf.keras.models.Model):\n  \"\"\"Convolutional variational autoencoder.\"\"\"\n\n  def __init__(self, latent_dim):\n    super(CVAE, self).__init__()\n    self.latent_dim = latent_dim\n    self.encoder = tf.keras.Sequential(\n        [\n            tf.keras.layers.InputLayer(input_shape=(28, 28, 1)),\n            tf.keras.layers.Conv2D(\n                filters=32, kernel_size=3, strides=(2, 2), activation='relu'),\n            tf.keras.layers.Conv2D(\n                filters=64, kernel_size=3, strides=(2, 2), activation='relu'),\n            tf.keras.layers.Flatten(),\n            # No activation\n            tf.keras.layers.Dense(latent_dim + latent_dim),\n        ]\n    )\n\n    self.decoder = tf.keras.Sequential(\n        [\n            tf.keras.layers.InputLayer(input_shape=(latent_dim,)),\n            tf.keras.layers.Dense(units=7*7*32, activation=tf.nn.relu),\n            tf.keras.layers.Reshape(target_shape=(7, 7, 32)),\n            tf.keras.layers.Conv2DTranspose(\n                filters=64, kernel_size=3, strides=2, padding='same',\n                activation='relu'),\n            tf.keras.layers.Conv2DTranspose(\n                filters=32, kernel_size=3, strides=2, padding='same',\n                activation='relu'),\n            # No activation\n            tf.keras.layers.Conv2DTranspose(\n                filters=1, kernel_size=3, strides=1, padding='same'),\n        ]\n    )\n\n  @tf.function\n  def sample(self, eps=None):\n    if eps is None:\n      eps = tf.random.normal(shape=(100, self.latent_dim))\n    return self.decode(eps, apply_sigmoid=True)\n\n  def encode(self, x):\n    mean, logvar = tf.split(self.encoder(x), num_or_size_splits=2, axis=1)\n    return mean, logvar\n\n  def reparameterize(self, mean, logvar):\n    eps = tf.random.normal(shape=mean.shape)\n    return eps * tf.exp(logvar * .5) + mean\n\n  def decode(self, z, apply_sigmoid=False):\n    logits = self.decoder(z)\n    if apply_sigmoid:\n      probs = tf.sigmoid(logits)\n      return probs\n    return logits","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:48:44.432504Z","iopub.execute_input":"2022-06-29T19:48:44.432772Z","iopub.status.idle":"2022-06-29T19:48:44.449041Z","shell.execute_reply.started":"2022-06-29T19:48:44.432745Z","shell.execute_reply":"2022-06-29T19:48:44.448367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = CVAE(latent_dim=latent_dim)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:50:47.551632Z","iopub.execute_input":"2022-06-29T19:50:47.552394Z","iopub.status.idle":"2022-06-29T19:50:47.647695Z","shell.execute_reply.started":"2022-06-29T19:50:47.552353Z","shell.execute_reply":"2022-06-29T19:50:47.646965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.Adam(1e-4)\n\n\ndef log_normal_pdf(sample, mean, logvar, raxis=1):\n  log2pi = tf.math.log(2. * np.pi)\n  return tf.reduce_sum(\n      -.5 * ((sample - mean) ** 2. * tf.exp(-logvar) + logvar + log2pi),\n      axis=raxis)\n\n\ndef compute_loss(model, x):\n  mean, logvar = model.encode(x)\n  z = model.reparameterize(mean, logvar)\n  x_logit = model.decode(z)\n  cross_ent = tf.nn.sigmoid_cross_entropy_with_logits(logits=x_logit, labels=x)\n  logpx_z = -tf.reduce_sum(cross_ent, axis=[1, 2, 3])\n  logpz = log_normal_pdf(z, 0., 0.)\n  logqz_x = log_normal_pdf(z, mean, logvar)\n  return -tf.reduce_mean(logpx_z + logpz - logqz_x)\n\n\n@tf.function\ndef train_step(model, x, optimizer):\n  \"\"\"Executes one training step and returns the loss.\n\n  This function computes the loss and gradients, and uses the latter to\n  update the model's parameters.\n  \"\"\"\nwith tf.GradientTape() as tape:\n    loss = compute_loss(model, x)\n    gradients = tape.gradient(loss, model.trainable_variables)\n    optimizer.apply_gradients(zip(gradients, model.trainable_variables))","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:50:51.502009Z","iopub.execute_input":"2022-06-29T19:50:51.502628Z","iopub.status.idle":"2022-06-29T19:50:51.534524Z","shell.execute_reply.started":"2022-06-29T19:50:51.502591Z","shell.execute_reply":"2022-06-29T19:50:51.533569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\n# set the dimensionality of the latent space to a plane for visualization later\nlatent_dim = 2\nnum_examples_to_generate = 16\n\n# keeping the random vector constant for generation (prediction) so\n# it will be easier to see the improvement.\nrandom_vector_for_generation = tf.random.normal(\n    shape=[num_examples_to_generate, latent_dim])\nmodel = CVAE(latent_dim)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:48:55.235748Z","iopub.execute_input":"2022-06-29T19:48:55.236653Z","iopub.status.idle":"2022-06-29T19:48:55.659507Z","shell.execute_reply.started":"2022-06-29T19:48:55.236605Z","shell.execute_reply":"2022-06-29T19:48:55.658802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mse_loss(y_true, y_pred):\n    r_loss = K.mean(K.square(y_true - y_pred), axis = [1,2,3])\n    return 1000 * r_loss\n \ndef kl_loss(mean, log_var):\n    kl_loss =  -0.5 * K.sum(1 + log_var - K.square(mean) - K.exp(log_var), axis = 1)\n    return kl_loss\n \ndef vae_loss(y_true, y_pred, mean, var):\n    r_loss = mse_loss(y_true, y_pred)\n    kl_loss = kl_loss(mean, log_var)\n    return  r_loss + kl_loss\n\n","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:57:29.293283Z","iopub.execute_input":"2022-06-29T19:57:29.293969Z","iopub.status.idle":"2022-06-29T19:57:29.300906Z","shell.execute_reply.started":"2022-06-29T19:57:29.293935Z","shell.execute_reply":"2022-06-29T19:57:29.300196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.compile(optimizer, vae_loss)\nmodel.compile(optimizer, loss = 'categorical crossentropy')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history1 = model.fit(train_generator, epochs=5, validation_data=val_generator)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T19:57:43.814826Z","iopub.execute_input":"2022-06-29T19:57:43.81513Z","iopub.status.idle":"2022-06-29T19:57:43.85902Z","shell.execute_reply.started":"2022-06-29T19:57:43.815101Z","shell.execute_reply":"2022-06-29T19:57:43.857014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_and_save_images(model, epoch, test_sample):\n    mean, logvar = model.encode(test_sample)\n    z = model.reparameterize(mean, logvar)\n    predictions = model.sample(z)\n    fig = plt.figure(figsize=(4, 4))\n\n    for i in range(predictions.shape[0]):\n        plt.subplot(4, 4, i + 1)\n        plt.imshow(predictions[i, :, :, 0], cmap='gray')\n        plt.axis('off')\n\n  # tight_layout minimizes the overlap between 2 sub-plots\n    plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:41:44.722894Z","iopub.execute_input":"2022-06-29T17:41:44.723369Z","iopub.status.idle":"2022-06-29T17:41:44.730085Z","shell.execute_reply.started":"2022-06-29T17:41:44.723331Z","shell.execute_reply":"2022-06-29T17:41:44.729235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pick a sample of the test set for generating output images\nassert batch_size >= num_examples_to_generate\nfor test_batch in test_dataset.take(1):\n    test_sample = test_batch[0:num_examples_to_generate, :, :, :]","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:41:46.524381Z","iopub.execute_input":"2022-06-29T17:41:46.525085Z","iopub.status.idle":"2022-06-29T17:41:46.62117Z","shell.execute_reply.started":"2022-06-29T17:41:46.525037Z","shell.execute_reply":"2022-06-29T17:41:46.619659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generate_and_save_images(model, 0, test_sample)\n\nfor epoch in range(1, epochs + 1):\n    start_time = time.time()\n    for train_x in train_dataset:\n        train_step(model, train_x, optimizer)\n    end_time = time.time()\n\n    loss = tf.keras.metrics.Mean()\n    for test_x in test_dataset:\n        loss(compute_loss(model, test_x))\n    elbo = -loss.result()\n    display.clear_output(wait=False)\n    print('Epoch: {}, Test set ELBO: {}, time elapse for current epoch: {}'\n        .format(epoch, elbo, end_time - start_time))\n    generate_and_save_images(model, epoch, test_sample)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:37:59.900913Z","iopub.status.idle":"2022-06-29T17:37:59.901524Z","shell.execute_reply.started":"2022-06-29T17:37:59.9013Z","shell.execute_reply":"2022-06-29T17:37:59.901322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_image(epoch_no):\n    return PIL.Image.open('image_at_epoch_{:04d}.png'.format(epoch_no))","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:37:59.90266Z","iopub.status.idle":"2022-06-29T17:37:59.903291Z","shell.execute_reply.started":"2022-06-29T17:37:59.903055Z","shell.execute_reply":"2022-06-29T17:37:59.903078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(display_image(epoch))\nplt.axis('off')  # Display images","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:37:59.904422Z","iopub.status.idle":"2022-06-29T17:37:59.905047Z","shell.execute_reply.started":"2022-06-29T17:37:59.904819Z","shell.execute_reply":"2022-06-29T17:37:59.904842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"anim_file = 'cvae.gif'\n\nwith imageio.get_writer(anim_file, mode='I') as writer:\n    filenames = glob.glob('image*.png')\n    filenames = sorted(filenames)\n    for filename in filenames:\n        image = imageio.imread(filename)\n        writer.append_data(image)\n    image = imageio.imread(filename)\n    writer.append_data(image)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:37:59.906277Z","iopub.status.idle":"2022-06-29T17:37:59.90692Z","shell.execute_reply.started":"2022-06-29T17:37:59.906667Z","shell.execute_reply":"2022-06-29T17:37:59.906704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_docs.vis.embed as embed\nembed.embed_file(anim_file)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:37:59.908107Z","iopub.status.idle":"2022-06-29T17:37:59.908733Z","shell.execute_reply.started":"2022-06-29T17:37:59.908478Z","shell.execute_reply":"2022-06-29T17:37:59.908501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_latent_images(model, n, digit_size=28):\n    \"\"\"Plots n x n digit images decoded from the latent space.\"\"\"\n\n    norm = tfp.distributions.Normal(0, 1)\n    grid_x = norm.quantile(np.linspace(0.05, 0.95, n))\n    grid_y = norm.quantile(np.linspace(0.05, 0.95, n))\n    image_width = digit_size*n\n    image_height = image_width\n    image = np.zeros((image_height, image_width))\n\n    for i, yi in enumerate(grid_x):\n        for j, xi in enumerate(grid_y):\n            z = np.array([[xi, yi]])\n            x_decoded = model.sample(z)\n            digit = tf.reshape(x_decoded[0], (digit_size, digit_size))\n            image[i * digit_size: (i + 1) * digit_size,\n                j * digit_size: (j + 1) * digit_size] = digit.numpy()\n\nplt.figure(figsize=(10, 10))\nplt.imshow(image, cmap='Greys_r')\nplt.axis('Off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-29T17:37:59.909919Z","iopub.status.idle":"2022-06-29T17:37:59.910542Z","shell.execute_reply.started":"2022-06-29T17:37:59.910312Z","shell.execute_reply":"2022-06-29T17:37:59.910334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Notebook Notes**","metadata":{}},{"cell_type":"markdown","source":"Normally, notebooks look like the following at the top:","metadata":{}},{"cell_type":"code","source":"#import numpy as np          # linear algebra\n#import pandas as pd        # data processing, CSV file I/O (e.g. pd.read_csv)\n#from random import randint\n#from tqdm import tqdm      #tqdm is a library in Python which is used for creating Progress Meters or Progress Bars. \n#Look at this: https://www.analyticsvidhya.com/blog/2021/05/how-to-use-progress-bars-in-python/\n#import cv2\n#import matplotlib.pyplot as plt\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\n#from subprocess import check_output\n#print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","metadata":{"execution":{"iopub.status.busy":"2022-06-29T15:18:21.730557Z","iopub.execute_input":"2022-06-29T15:18:21.73084Z","iopub.status.idle":"2022-06-29T15:18:21.734983Z","shell.execute_reply.started":"2022-06-29T15:18:21.730812Z","shell.execute_reply":"2022-06-29T15:18:21.734228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"My last attempt was loaded with ImportError /t s and FileNotFound /t s \nI just started a new notebook and it was able to find the directories and the packages were up-to-date","metadata":{"execution":{"iopub.status.busy":"2022-06-29T15:19:52.999013Z","iopub.execute_input":"2022-06-29T15:19:52.999742Z","iopub.status.idle":"2022-06-29T15:19:53.005538Z","shell.execute_reply.started":"2022-06-29T15:19:52.999694Z","shell.execute_reply":"2022-06-29T15:19:53.004387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}