{ "cells": [ { "cell_type": "code", "execution_count": 7, "id": "572dc7fb", "metadata": {}, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "from matplotlib.image import imread\n", "import pandas as pd\n", "from collections import Counter\n", "import json\n", "import os\n", "import re\n", "import tempfile\n", "import numpy as np\n", "from os.path import exists\n", "from imblearn.under_sampling import RandomUnderSampler\n", "from PIL import ImageFile\n", "import sklearn as sk\n", "from sklearn.model_selection import train_test_split, StratifiedShuffleSplit\n", "import tensorflow as tf\n", "import tensorflow.keras\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten, Activation\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.optimizers import Adam\n", "# custom modules\n", "import image_faults\n", "\n", "ImageFile.LOAD_TRUNCATED_IMAGES = True" ] }, { "cell_type": "code", "execution_count": 8, "id": "8d94196d", "metadata": {}, "outputs": [], "source": [ "def add_regularization(model, regularizer=tf.keras.regularizers.l2(0.0001)):\n", "\n", " if not isinstance(regularizer, tf.keras.regularizers.Regularizer):\n", " print(\"Regularizer must be a subclass of tf.keras.regularizers.Regularizer\")\n", " return model\n", "\n", " for layer in model.layers:\n", " for attr in ['kernel_regularizer']:\n", " if hasattr(layer, attr):\n", " setattr(layer, attr, regularizer)\n", "\n", " # When we change the layers attributes, the change only happens in the model config file\n", " model_json = model.to_json()\n", "\n", " # Save the weights before reloading the model.\n", " tmp_weights_path = os.path.join(tempfile.gettempdir(), 'tmp_weights.h5')\n", " model.save_weights(tmp_weights_path)\n", "\n", " # load the model from the config\n", " model = tf.keras.models.model_from_json(model_json)\n", " \n", " # Reload the model weights\n", " model.load_weights(tmp_weights_path, by_name=True)\n", " return model" ] }, { "cell_type": "code", "execution_count": 9, "id": "a5c72863", "metadata": {}, "outputs": [], "source": [ "# image_faults.faulty_images() # removes faulty images\n", "df = pd.read_csv('expanded_class.csv', index_col=[0], low_memory=False)" ] }, { "cell_type": "code", "execution_count": 11, "id": "1057a442", "metadata": { "scrolled": true }, "outputs": [ { "ename": "AttributeError", "evalue": "'NoneType' object has no attribute 'group'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdict_pics\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mdict_pics\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdict_pics_jup\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0mblah\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSeries\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPictureURL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdrop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlabels\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'PictureURL'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mdict_pics_jup\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'temp_pics_source_list.txt'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mtemp_pics_source_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mdict_pics\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mtarget_dir\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msep\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mr'[^/]+(?=/\\$_|.jpg)'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mIGNORECASE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'.jpg'\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtemp_pics_source_list\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"{source:target} dictionary created @ \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mtarget_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdict_pics\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'temp_pics_source_list.txt'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mtemp_pics_source_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mdict_pics\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mtarget_dir\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msep\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mr'[^/]+(?=/\\$_|.jpg)'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mIGNORECASE\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'.jpg'\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtemp_pics_source_list\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"{source:target} dictionary created @ \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mtarget_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdict_pics\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'group'" ] } ], "source": [ "def dict_pics_jup(): # \n", " target_dir = os.getcwd() + os.sep + \"training_images\"\n", " with open('temp_pics_source_list.txt') as f:\n", " temp_pics_source_list = json.load(f)\n", " dict_pics = {k:target_dir + os.sep + re.search(r'[^/]+(?=/\\$_|.jpg)', k, re.IGNORECASE).group() + '.jpg' for k in temp_pics_source_list}\n", " print(\"{source:target} dictionary created @ \" + target_dir)\n", " return dict_pics\n", "\n", "dict_pics = dict_pics_jup()\n", "blah = pd.Series(df.PictureURL)\n", "df = df.drop(labels=['PictureURL'], axis=1)\n", "blah = blah.apply(lambda x: dict_pics[x])\n", "df = pd.concat([blah, df],axis=1)\n", "df = df.groupby('PrimaryCategoryID').filter(lambda x: len(x)>25) # removes cat outliers\n", "# removes non-existent image paths" ] }, { "cell_type": "code", "execution_count": 5, "id": "7a6146e6", "metadata": {}, "outputs": [], "source": [ "df['PrimaryCategoryID'] = df['PrimaryCategoryID'].astype(str) # pandas thinks ids are ints\n", "\n", "df=df.sample(frac=1)" ] }, { "cell_type": "code", "execution_count": 6, "id": "114cc3c0", "metadata": {}, "outputs": [], "source": [ "undersample = RandomUnderSampler(sampling_strategy='auto')\n", "train, y_under = undersample.fit_resample(df, df['PrimaryCategoryID'])\n", "#print(Counter(train['PrimaryCategoryID']))" ] }, { "cell_type": "code", "execution_count": 7, "id": "506aa5cf", "metadata": {}, "outputs": [], "source": [ "train, test = train_test_split(train, test_size=0.1, random_state=42)\n", "# stratify=train['PrimaryCategoryID']\n", "# train['PrimaryCategoryID'].value_counts()" ] }, { "cell_type": "code", "execution_count": 8, "id": "4d72eb90", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 5110 validated image filenames belonging to 13 classes.\n", "Found 1277 validated image filenames belonging to 13 classes.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.8/dist-packages/keras_preprocessing/image/dataframe_iterator.py:279: UserWarning: Found 1 invalid image filename(s) in x_col=\"PictureURL\". These filename(s) will be ignored.\n", " warnings.warn(\n" ] } ], "source": [ "datagen = ImageDataGenerator(rescale=1./255., \n", " validation_split=.2,\n", " #samplewise_std_normalization=True,\n", " #horizontal_flip= True,\n", " #vertical_flip= True,\n", " #width_shift_range= 0.2,\n", " #height_shift_range= 0.2,\n", " #rotation_range= 90,\n", " preprocessing_function=tf.keras.applications.xception.preprocess_input)\n", "train_generator=datagen.flow_from_dataframe(\n", " dataframe=train[:len(train)],\n", " directory='./training_images',\n", " x_col='PictureURL',\n", " y_col='PrimaryCategoryID',\n", " batch_size=64,\n", " seed=42,\n", " shuffle=True,\n", " target_size=(299,299),\n", " subset='training'\n", " )\n", "validation_generator=datagen.flow_from_dataframe(\n", " dataframe=train[:len(train)], # is using train right?\n", " directory='./training_images',\n", " x_col='PictureURL',\n", " y_col='PrimaryCategoryID',\n", " batch_size=64,\n", " seed=42,\n", " shuffle=True,\n", " target_size=(299,299),\n", " subset='validation'\n", " )" ] }, { "cell_type": "code", "execution_count": 9, "id": "7b70f37f", "metadata": {}, "outputs": [], "source": [ "imgs, labels = next(train_generator)" ] }, { "cell_type": "code", "execution_count": 10, "id": "1ed54bf5", "metadata": {}, "outputs": [], "source": [ "def plotImages(images_arr):\n", " fig, axes = plt.subplots(1, 10, figsize=(20,20))\n", " axes = axes.flatten()\n", " for img, ax in zip( images_arr, axes):\n", " ax.imshow(img)\n", " ax.axis('off')\n", " plt.tight_layout()\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": 11, "id": "85934565", "metadata": {}, "outputs": [], "source": [ "#plotImages(imgs)\n", "#print(labels)" ] }, { "cell_type": "code", "execution_count": 12, "id": "6322bcad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "physical_devices = tf.config.list_physical_devices('GPU')\n", "print(len(physical_devices))\n", "tf.config.experimental.set_memory_growth(physical_devices[0], True)" ] }, { "cell_type": "code", "execution_count": 13, "id": "07fd25c6", "metadata": {}, "outputs": [], "source": [ "# see https://www.kaggle.com/dmitrypukhov/cnn-with-imagedatagenerator-flow-from-dataframe for train/test/val split \n", "# example\n", "\n", "# may need to either create a test dataset from the original dataset or just download a new one" ] }, { "cell_type": "code", "execution_count": 14, "id": "b31af79e", "metadata": {}, "outputs": [], "source": [ "base_model = tf.keras.applications.xception.Xception(include_top=False, weights='imagenet', pooling='avg')\n", "#base_model.summary()" ] }, { "cell_type": "code", "execution_count": 15, "id": "fe06f2bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model: \"model\"\n", "__________________________________________________________________________________________________\n", " Layer (type) Output Shape Param # Connected to \n", "==================================================================================================\n", " input_1 (InputLayer) [(None, None, None, 0 [] \n", " 3)] \n", " \n", " block1_conv1 (Conv2D) (None, None, None, 864 ['input_1[0][0]'] \n", " 32) \n", " \n", " block1_conv1_bn (BatchNormaliz (None, None, None, 128 ['block1_conv1[0][0]'] \n", " ation) 32) \n", " \n", " block1_conv1_act (Activation) (None, None, None, 0 ['block1_conv1_bn[0][0]'] \n", " 32) \n", " \n", " block1_conv2 (Conv2D) (None, None, None, 18432 ['block1_conv1_act[0][0]'] \n", " 64) \n", " \n", " block1_conv2_bn (BatchNormaliz (None, None, None, 256 ['block1_conv2[0][0]'] \n", " ation) 64) \n", " \n", " block1_conv2_act (Activation) (None, None, None, 0 ['block1_conv2_bn[0][0]'] \n", " 64) \n", " \n", " block2_sepconv1 (SeparableConv (None, None, None, 8768 ['block1_conv2_act[0][0]'] \n", " 2D) 128) \n", " \n", " block2_sepconv1_bn (BatchNorma (None, None, None, 512 ['block2_sepconv1[0][0]'] \n", " lization) 128) \n", " \n", " block2_sepconv2_act (Activatio (None, None, None, 0 ['block2_sepconv1_bn[0][0]'] \n", " n) 128) \n", " \n", " block2_sepconv2 (SeparableConv (None, None, None, 17536 ['block2_sepconv2_act[0][0]'] \n", " 2D) 128) \n", " \n", " block2_sepconv2_bn (BatchNorma (None, None, None, 512 ['block2_sepconv2[0][0]'] \n", " lization) 128) \n", " \n", " conv2d (Conv2D) (None, None, None, 8192 ['block1_conv2_act[0][0]'] \n", " 128) \n", " \n", " block2_pool (MaxPooling2D) (None, None, None, 0 ['block2_sepconv2_bn[0][0]'] \n", " 128) \n", " \n", " batch_normalization (BatchNorm (None, None, None, 512 ['conv2d[0][0]'] \n", " alization) 128) \n", " \n", " add (Add) (None, None, None, 0 ['block2_pool[0][0]', \n", " 128) 'batch_normalization[0][0]'] \n", " \n", " block3_sepconv1_act (Activatio (None, None, None, 0 ['add[0][0]'] \n", " n) 128) \n", " \n", " block3_sepconv1 (SeparableConv (None, None, None, 33920 ['block3_sepconv1_act[0][0]'] \n", " 2D) 256) \n", " \n", " block3_sepconv1_bn (BatchNorma (None, None, None, 1024 ['block3_sepconv1[0][0]'] \n", " lization) 256) \n", " \n", " block3_sepconv2_act (Activatio (None, None, None, 0 ['block3_sepconv1_bn[0][0]'] \n", " n) 256) \n", " \n", " block3_sepconv2 (SeparableConv (None, None, None, 67840 ['block3_sepconv2_act[0][0]'] \n", " 2D) 256) \n", " \n", " block3_sepconv2_bn (BatchNorma (None, None, None, 1024 ['block3_sepconv2[0][0]'] \n", " lization) 256) \n", " \n", " conv2d_1 (Conv2D) (None, None, None, 32768 ['add[0][0]'] \n", " 256) \n", " \n", " block3_pool (MaxPooling2D) (None, None, None, 0 ['block3_sepconv2_bn[0][0]'] \n", " 256) \n", " \n", " batch_normalization_1 (BatchNo (None, None, None, 1024 ['conv2d_1[0][0]'] \n", " rmalization) 256) \n", " \n", " add_1 (Add) (None, None, None, 0 ['block3_pool[0][0]', \n", " 256) 'batch_normalization_1[0][0]'] \n", " \n", " block4_sepconv1_act (Activatio (None, None, None, 0 ['add_1[0][0]'] \n", " n) 256) \n", " \n", " block4_sepconv1 (SeparableConv (None, None, None, 188672 ['block4_sepconv1_act[0][0]'] \n", " 2D) 728) \n", " \n", " block4_sepconv1_bn (BatchNorma (None, None, None, 2912 ['block4_sepconv1[0][0]'] \n", " lization) 728) \n", " \n", " block4_sepconv2_act (Activatio (None, None, None, 0 ['block4_sepconv1_bn[0][0]'] \n", " n) 728) \n", " \n", " block4_sepconv2 (SeparableConv (None, None, None, 536536 ['block4_sepconv2_act[0][0]'] \n", " 2D) 728) \n", " \n", " block4_sepconv2_bn (BatchNorma (None, None, None, 2912 ['block4_sepconv2[0][0]'] \n", " lization) 728) \n", " \n", " conv2d_2 (Conv2D) (None, None, None, 186368 ['add_1[0][0]'] \n", " 728) \n", " \n", " block4_pool (MaxPooling2D) (None, None, None, 0 ['block4_sepconv2_bn[0][0]'] \n", " 728) \n", " \n", " batch_normalization_2 (BatchNo (None, None, None, 2912 ['conv2d_2[0][0]'] \n", " rmalization) 728) \n", " \n", " add_2 (Add) (None, None, None, 0 ['block4_pool[0][0]', \n", " 728) 'batch_normalization_2[0][0]'] \n", " \n", " block5_sepconv1_act (Activatio (None, None, None, 0 ['add_2[0][0]'] \n", " n) 728) \n", " \n", " block5_sepconv1 (SeparableConv (None, None, None, 536536 ['block5_sepconv1_act[0][0]'] \n", " 2D) 728) \n", " \n", " block5_sepconv1_bn (BatchNorma (None, None, None, 2912 ['block5_sepconv1[0][0]'] \n", " lization) 728) \n", " \n", " block5_sepconv2_act (Activatio (None, None, None, 0 ['block5_sepconv1_bn[0][0]'] \n", " n) 728) \n", " \n", " block5_sepconv2 (SeparableConv (None, None, None, 536536 ['block5_sepconv2_act[0][0]'] \n", " 2D) 728) \n", " \n", " block5_sepconv2_bn (BatchNorma (None, None, None, 2912 ['block5_sepconv2[0][0]'] \n", " lization) 728) \n", " \n", " block5_sepconv3_act (Activatio (None, None, None, 0 ['block5_sepconv2_bn[0][0]'] \n", " n) 728) \n", " \n", " block5_sepconv3 (SeparableConv (None, None, None, 536536 ['block5_sepconv3_act[0][0]'] \n", " 2D) 728) \n", " \n", " block5_sepconv3_bn (BatchNorma (None, None, None, 2912 ['block5_sepconv3[0][0]'] \n", " lization) 728) \n", " \n", " add_3 (Add) (None, None, None, 0 ['block5_sepconv3_bn[0][0]', \n", " 728) 'add_2[0][0]'] \n", " \n", " block6_sepconv1_act (Activatio (None, None, None, 0 ['add_3[0][0]'] \n", " n) 728) \n", " \n", " block6_sepconv1 (SeparableConv (None, None, None, 536536 ['block6_sepconv1_act[0][0]'] \n", " 2D) 728) \n", " \n", " block6_sepconv1_bn (BatchNorma (None, None, None, 2912 ['block6_sepconv1[0][0]'] \n", " lization) 728) \n", " \n", " block6_sepconv2_act (Activatio (None, None, None, 0 ['block6_sepconv1_bn[0][0]'] \n", " n) 728) \n", " \n", " block6_sepconv2 (SeparableConv (None, None, None, 536536 ['block6_sepconv2_act[0][0]'] \n", " 2D) 728) \n", " \n", " block6_sepconv2_bn (BatchNorma (None, None, None, 2912 ['block6_sepconv2[0][0]'] \n", " lization) 728) \n", " \n", " block6_sepconv3_act (Activatio (None, None, None, 0 ['block6_sepconv2_bn[0][0]'] \n", " n) 728) \n", " \n", " block6_sepconv3 (SeparableConv (None, None, None, 536536 ['block6_sepconv3_act[0][0]'] \n", " 2D) 728) \n", " \n", " block6_sepconv3_bn (BatchNorma (None, None, None, 2912 ['block6_sepconv3[0][0]'] \n", " lization) 728) \n", " \n", " add_4 (Add) (None, None, None, 0 ['block6_sepconv3_bn[0][0]', \n", " 728) 'add_3[0][0]'] \n", " \n", " block7_sepconv1_act (Activatio (None, None, None, 0 ['add_4[0][0]'] \n", " n) 728) \n", " \n", " block7_sepconv1 (SeparableConv (None, None, None, 536536 ['block7_sepconv1_act[0][0]'] \n", " 2D) 728) \n", " \n", " block7_sepconv1_bn (BatchNorma (None, None, None, 2912 ['block7_sepconv1[0][0]'] \n", " lization) 728) \n", " \n", " block7_sepconv2_act (Activatio (None, None, None, 0 ['block7_sepconv1_bn[0][0]'] \n", " n) 728) \n", " \n", " block7_sepconv2 (SeparableConv (None, None, None, 536536 ['block7_sepconv2_act[0][0]'] \n", " 2D) 728) \n", " \n", " block7_sepconv2_bn (BatchNorma (None, None, None, 2912 ['block7_sepconv2[0][0]'] \n", " lization) 728) \n", " \n", " block7_sepconv3_act (Activatio (None, None, None, 0 ['block7_sepconv2_bn[0][0]'] \n", " n) 728) \n", " \n", " block7_sepconv3 (SeparableConv (None, None, None, 536536 ['block7_sepconv3_act[0][0]'] \n", " 2D) 728) \n", " \n", " block7_sepconv3_bn (BatchNorma (None, None, None, 2912 ['block7_sepconv3[0][0]'] \n", " lization) 728) \n", " \n", " add_5 (Add) (None, None, None, 0 ['block7_sepconv3_bn[0][0]', \n", " 728) 'add_4[0][0]'] \n", " \n", " block8_sepconv1_act (Activatio (None, None, None, 0 ['add_5[0][0]'] \n", " n) 728) \n", " \n", " block8_sepconv1 (SeparableConv (None, None, None, 536536 ['block8_sepconv1_act[0][0]'] \n", " 2D) 728) \n", " \n", " block8_sepconv1_bn (BatchNorma (None, None, None, 2912 ['block8_sepconv1[0][0]'] \n", " lization) 728) \n", " \n", " block8_sepconv2_act (Activatio (None, None, None, 0 ['block8_sepconv1_bn[0][0]'] \n", " n) 728) \n", " \n", " block8_sepconv2 (SeparableConv (None, None, None, 536536 ['block8_sepconv2_act[0][0]'] \n", " 2D) 728) \n", " \n", " block8_sepconv2_bn (BatchNorma (None, None, None, 2912 ['block8_sepconv2[0][0]'] \n", " lization) 728) \n", " \n", " block8_sepconv3_act (Activatio (None, None, None, 0 ['block8_sepconv2_bn[0][0]'] \n", " n) 728) \n", " \n", " block8_sepconv3 (SeparableConv (None, None, None, 536536 ['block8_sepconv3_act[0][0]'] \n", " 2D) 728) \n", " \n", " block8_sepconv3_bn (BatchNorma (None, None, None, 2912 ['block8_sepconv3[0][0]'] \n", " lization) 728) \n", " \n", " add_6 (Add) (None, None, None, 0 ['block8_sepconv3_bn[0][0]', \n", " 728) 'add_5[0][0]'] \n", " \n", " block9_sepconv1_act (Activatio (None, None, None, 0 ['add_6[0][0]'] \n", " n) 728) \n", " \n", " block9_sepconv1 (SeparableConv (None, None, None, 536536 ['block9_sepconv1_act[0][0]'] \n", " 2D) 728) \n", " \n", " block9_sepconv1_bn (BatchNorma (None, None, None, 2912 ['block9_sepconv1[0][0]'] \n", " lization) 728) \n", " \n", " block9_sepconv2_act (Activatio (None, None, None, 0 ['block9_sepconv1_bn[0][0]'] \n", " n) 728) \n", " \n", " block9_sepconv2 (SeparableConv (None, None, None, 536536 ['block9_sepconv2_act[0][0]'] \n", " 2D) 728) \n", " \n", " block9_sepconv2_bn (BatchNorma (None, None, None, 2912 ['block9_sepconv2[0][0]'] \n", " lization) 728) \n", " \n", " block9_sepconv3_act (Activatio (None, None, None, 0 ['block9_sepconv2_bn[0][0]'] \n", " n) 728) \n", " \n", " block9_sepconv3 (SeparableConv (None, None, None, 536536 ['block9_sepconv3_act[0][0]'] \n", " 2D) 728) \n", " \n", " block9_sepconv3_bn (BatchNorma (None, None, None, 2912 ['block9_sepconv3[0][0]'] \n", " lization) 728) \n", " \n", " add_7 (Add) (None, None, None, 0 ['block9_sepconv3_bn[0][0]', \n", " 728) 'add_6[0][0]'] \n", " \n", " block10_sepconv1_act (Activati (None, None, None, 0 ['add_7[0][0]'] \n", " on) 728) \n", " \n", " block10_sepconv1 (SeparableCon (None, None, None, 536536 ['block10_sepconv1_act[0][0]'] \n", " v2D) 728) \n", " \n", " block10_sepconv1_bn (BatchNorm (None, None, None, 2912 ['block10_sepconv1[0][0]'] \n", " alization) 728) \n", " \n", " block10_sepconv2_act (Activati (None, None, None, 0 ['block10_sepconv1_bn[0][0]'] \n", " on) 728) \n", " \n", " block10_sepconv2 (SeparableCon (None, None, None, 536536 ['block10_sepconv2_act[0][0]'] \n", " v2D) 728) \n", " \n", " block10_sepconv2_bn (BatchNorm (None, None, None, 2912 ['block10_sepconv2[0][0]'] \n", " alization) 728) \n", " \n", " block10_sepconv3_act (Activati (None, None, None, 0 ['block10_sepconv2_bn[0][0]'] \n", " on) 728) \n", " \n", " block10_sepconv3 (SeparableCon (None, None, None, 536536 ['block10_sepconv3_act[0][0]'] \n", " v2D) 728) \n", " \n", " block10_sepconv3_bn (BatchNorm (None, None, None, 2912 ['block10_sepconv3[0][0]'] \n", " alization) 728) \n", " \n", " add_8 (Add) (None, None, None, 0 ['block10_sepconv3_bn[0][0]', \n", " 728) 'add_7[0][0]'] \n", " \n", " block11_sepconv1_act (Activati (None, None, None, 0 ['add_8[0][0]'] \n", " on) 728) \n", " \n", " block11_sepconv1 (SeparableCon (None, None, None, 536536 ['block11_sepconv1_act[0][0]'] \n", " v2D) 728) \n", " \n", " block11_sepconv1_bn (BatchNorm (None, None, None, 2912 ['block11_sepconv1[0][0]'] \n", " alization) 728) \n", " \n", " block11_sepconv2_act (Activati (None, None, None, 0 ['block11_sepconv1_bn[0][0]'] \n", " on) 728) \n", " \n", " block11_sepconv2 (SeparableCon (None, None, None, 536536 ['block11_sepconv2_act[0][0]'] \n", " v2D) 728) \n", " \n", " block11_sepconv2_bn (BatchNorm (None, None, None, 2912 ['block11_sepconv2[0][0]'] \n", " alization) 728) \n", " \n", " block11_sepconv3_act (Activati (None, None, None, 0 ['block11_sepconv2_bn[0][0]'] \n", " on) 728) \n", " \n", " block11_sepconv3 (SeparableCon (None, None, None, 536536 ['block11_sepconv3_act[0][0]'] \n", " v2D) 728) \n", " \n", " block11_sepconv3_bn (BatchNorm (None, None, None, 2912 ['block11_sepconv3[0][0]'] \n", " alization) 728) \n", " \n", " add_9 (Add) (None, None, None, 0 ['block11_sepconv3_bn[0][0]', \n", " 728) 'add_8[0][0]'] \n", " \n", " block12_sepconv1_act (Activati (None, None, None, 0 ['add_9[0][0]'] \n", " on) 728) \n", " \n", " block12_sepconv1 (SeparableCon (None, None, None, 536536 ['block12_sepconv1_act[0][0]'] \n", " v2D) 728) \n", " \n", " block12_sepconv1_bn (BatchNorm (None, None, None, 2912 ['block12_sepconv1[0][0]'] \n", " alization) 728) \n", " \n", " block12_sepconv2_act (Activati (None, None, None, 0 ['block12_sepconv1_bn[0][0]'] \n", " on) 728) \n", " \n", " block12_sepconv2 (SeparableCon (None, None, None, 536536 ['block12_sepconv2_act[0][0]'] \n", " v2D) 728) \n", " \n", " block12_sepconv2_bn (BatchNorm (None, None, None, 2912 ['block12_sepconv2[0][0]'] \n", " alization) 728) \n", " \n", " block12_sepconv3_act (Activati (None, None, None, 0 ['block12_sepconv2_bn[0][0]'] \n", " on) 728) \n", " \n", " block12_sepconv3 (SeparableCon (None, None, None, 536536 ['block12_sepconv3_act[0][0]'] \n", " v2D) 728) \n", " \n", " block12_sepconv3_bn (BatchNorm (None, None, None, 2912 ['block12_sepconv3[0][0]'] \n", " alization) 728) \n", " \n", " add_10 (Add) (None, None, None, 0 ['block12_sepconv3_bn[0][0]', \n", " 728) 'add_9[0][0]'] \n", " \n", " block13_sepconv1_act (Activati (None, None, None, 0 ['add_10[0][0]'] \n", " on) 728) \n", " \n", " block13_sepconv1 (SeparableCon (None, None, None, 536536 ['block13_sepconv1_act[0][0]'] \n", " v2D) 728) \n", " \n", " block13_sepconv1_bn (BatchNorm (None, None, None, 2912 ['block13_sepconv1[0][0]'] \n", " alization) 728) \n", " \n", " block13_sepconv2_act (Activati (None, None, None, 0 ['block13_sepconv1_bn[0][0]'] \n", " on) 728) \n", " \n", " block13_sepconv2 (SeparableCon (None, None, None, 752024 ['block13_sepconv2_act[0][0]'] \n", " v2D) 1024) \n", " \n", " block13_sepconv2_bn (BatchNorm (None, None, None, 4096 ['block13_sepconv2[0][0]'] \n", " alization) 1024) \n", " \n", " conv2d_3 (Conv2D) (None, None, None, 745472 ['add_10[0][0]'] \n", " 1024) \n", " \n", " block13_pool (MaxPooling2D) (None, None, None, 0 ['block13_sepconv2_bn[0][0]'] \n", " 1024) \n", " \n", " batch_normalization_3 (BatchNo (None, None, None, 4096 ['conv2d_3[0][0]'] \n", " rmalization) 1024) \n", " \n", " add_11 (Add) (None, None, None, 0 ['block13_pool[0][0]', \n", " 1024) 'batch_normalization_3[0][0]'] \n", " \n", " block14_sepconv1 (SeparableCon (None, None, None, 1582080 ['add_11[0][0]'] \n", " v2D) 1536) \n", " \n", " block14_sepconv1_bn (BatchNorm (None, None, None, 6144 ['block14_sepconv1[0][0]'] \n", " alization) 1536) \n", " \n", " block14_sepconv1_act (Activati (None, None, None, 0 ['block14_sepconv1_bn[0][0]'] \n", " on) 1536) \n", " \n", " block14_sepconv2 (SeparableCon (None, None, None, 3159552 ['block14_sepconv1_act[0][0]'] \n", " v2D) 2048) \n", " \n", " block14_sepconv2_bn (BatchNorm (None, None, None, 8192 ['block14_sepconv2[0][0]'] \n", " alization) 2048) \n", " \n", " block14_sepconv2_act (Activati (None, None, None, 0 ['block14_sepconv2_bn[0][0]'] \n", " on) 2048) \n", " \n", " global_average_pooling2d (Glob (None, 2048) 0 ['block14_sepconv2_act[0][0]'] \n", " alAveragePooling2D) \n", " \n", " dense (Dense) (None, 13) 26637 ['global_average_pooling2d[0][0]'\n", " ] \n", " \n", "==================================================================================================\n", "Total params: 20,888,117\n", "Trainable params: 20,833,589\n", "Non-trainable params: 54,528\n", "__________________________________________________________________________________________________\n" ] } ], "source": [ "for layer in base_model.layers:\n", " layer.trainable = True\n", " \n", "output = Dense(13, activation='softmax')(base_model.output)\n", "model = tf.keras.Model(base_model.input, output)\n", "#model = add_regularization(model)\n", "model.summary()\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "ea620129", "metadata": {}, "outputs": [], "source": [ "#model.add(Dropout(.5))\n", "#model.add(Dense(64, activation='softmax'))\n", "# model.add(Dropout(.25))\n", "#model = add_regularization(model)\n" ] }, { "cell_type": "code", "execution_count": 17, "id": "fd5d1246", "metadata": {}, "outputs": [], "source": [ "model.compile(optimizer=Adam(learning_rate=.0001), loss='categorical_crossentropy',\n", " metrics=['accuracy'])\n", "# sparse_categorical_crossentropy" ] }, { "cell_type": "code", "execution_count": 18, "id": "9cd2ba27", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/30\n", "80/80 [==============================] - 78s 913ms/step - loss: 1.8419 - accuracy: 0.4153 - val_loss: 2.6144 - val_accuracy: 0.0720\n", "Epoch 2/30\n", "80/80 [==============================] - 69s 862ms/step - loss: 0.9255 - accuracy: 0.7121 - val_loss: 2.5804 - val_accuracy: 0.1026\n", "Epoch 3/30\n", "80/80 [==============================] - 72s 900ms/step - loss: 0.4003 - accuracy: 0.9092 - val_loss: 2.4443 - val_accuracy: 0.2310\n", "Epoch 4/30\n", "80/80 [==============================] - 71s 888ms/step - loss: 0.1224 - accuracy: 0.9847 - val_loss: 2.1299 - val_accuracy: 0.3782\n", "Epoch 5/30\n", "80/80 [==============================] - 75s 935ms/step - loss: 0.0371 - accuracy: 0.9975 - val_loss: 1.7368 - val_accuracy: 0.4973\n", "Epoch 6/30\n", "80/80 [==============================] - 73s 900ms/step - loss: 0.0167 - accuracy: 0.9992 - val_loss: 1.6747 - val_accuracy: 0.5341\n", "Epoch 7/30\n", "80/80 [==============================] - 72s 897ms/step - loss: 0.0097 - accuracy: 0.9998 - val_loss: 1.6494 - val_accuracy: 0.5442\n", "Epoch 8/30\n", "80/80 [==============================] - 74s 915ms/step - loss: 0.0062 - accuracy: 0.9998 - val_loss: 1.6659 - val_accuracy: 0.5568\n", "Epoch 9/30\n", "80/80 [==============================] - 74s 917ms/step - loss: 0.0044 - accuracy: 1.0000 - val_loss: 1.7088 - val_accuracy: 0.5615\n", "Epoch 10/30\n", "80/80 [==============================] - 70s 868ms/step - loss: 0.0035 - accuracy: 1.0000 - val_loss: 1.7540 - val_accuracy: 0.5583\n", "Epoch 11/30\n", "80/80 [==============================] - 70s 864ms/step - loss: 0.0027 - accuracy: 0.9998 - val_loss: 1.7894 - val_accuracy: 0.5552\n", "Epoch 12/30\n", "80/80 [==============================] - 69s 858ms/step - loss: 0.0020 - accuracy: 1.0000 - val_loss: 1.8126 - val_accuracy: 0.5536\n", "Epoch 13/30\n", "80/80 [==============================] - 69s 857ms/step - loss: 0.0019 - accuracy: 1.0000 - val_loss: 1.8496 - val_accuracy: 0.5544\n", "Epoch 14/30\n", "80/80 [==============================] - 69s 859ms/step - loss: 0.0015 - accuracy: 1.0000 - val_loss: 1.8646 - val_accuracy: 0.5544\n", "Epoch 15/30\n", "30/80 [==========>...................] - ETA: 36s - loss: 0.0011 - accuracy: 1.0000" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m model.fit(x=train_generator,\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0msteps_per_epoch\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_generator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mvalidation_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mvalidation_generator\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mvalidation_steps\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalidation_generator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mepochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py\u001b[0m in \u001b[0;36merror_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 63\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 64\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 65\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pylint: disable=broad-except\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_process_traceback_frames\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__traceback__\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/engine/training.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[1;32m 1219\u001b[0m \u001b[0mlogs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtmp_logs\u001b[0m \u001b[0;31m# No error, now safe to assign to logs.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1220\u001b[0m \u001b[0mend_step\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstep\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mdata_handler\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstep_increment\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1221\u001b[0;31m \u001b[0mcallbacks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_train_batch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mend_step\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1222\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstop_training\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1223\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/callbacks.py\u001b[0m in \u001b[0;36mon_train_batch_end\u001b[0;34m(self, batch, logs)\u001b[0m\n\u001b[1;32m 434\u001b[0m \"\"\"\n\u001b[1;32m 435\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_should_call_train_batch_hooks\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 436\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_batch_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mModeKeys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTRAIN\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'end'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 437\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 438\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mon_test_batch_begin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/callbacks.py\u001b[0m in \u001b[0;36m_call_batch_hook\u001b[0;34m(self, mode, hook, batch, logs)\u001b[0m\n\u001b[1;32m 293\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_batch_begin_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 294\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'end'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 295\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_batch_end_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 296\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 297\u001b[0m raise ValueError(\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/callbacks.py\u001b[0m in \u001b[0;36m_call_batch_end_hook\u001b[0;34m(self, mode, batch, logs)\u001b[0m\n\u001b[1;32m 314\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_batch_times\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch_time\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 315\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 316\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_batch_hook_helper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhook_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 317\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 318\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_batch_times\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_num_batches_for_timing_check\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/callbacks.py\u001b[0m in \u001b[0;36m_call_batch_hook_helper\u001b[0;34m(self, hook_name, batch, logs)\u001b[0m\n\u001b[1;32m 352\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mcallback\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcallbacks\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 353\u001b[0m \u001b[0mhook\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcallback\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhook_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 354\u001b[0;31m \u001b[0mhook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 355\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 356\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_check_timing\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/callbacks.py\u001b[0m in \u001b[0;36mon_train_batch_end\u001b[0;34m(self, batch, logs)\u001b[0m\n\u001b[1;32m 1030\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1031\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mon_train_batch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1032\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_batch_update_progbar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1033\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1034\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mon_test_batch_end\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlogs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/callbacks.py\u001b[0m in \u001b[0;36m_batch_update_progbar\u001b[0;34m(self, batch, logs)\u001b[0m\n\u001b[1;32m 1102\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mverbose\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1103\u001b[0m \u001b[0;31m# Only block async when verbose = 1.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1104\u001b[0;31m \u001b[0mlogs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf_utils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msync_to_numpy_or_python_type\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlogs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1105\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprogbar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseen\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlogs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfinalize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1106\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/utils/tf_utils.py\u001b[0m in \u001b[0;36msync_to_numpy_or_python_type\u001b[0;34m(tensors)\u001b[0m\n\u001b[1;32m 552\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mt\u001b[0m \u001b[0;31m# Don't turn ragged or sparse tensors to NumPy.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 553\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 554\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnest\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmap_structure\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_to_single_numpy_or_python_type\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtensors\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 555\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 556\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/tensorflow/python/util/nest.py\u001b[0m in \u001b[0;36mmap_structure\u001b[0;34m(func, *structure, **kwargs)\u001b[0m\n\u001b[1;32m 867\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 868\u001b[0m return pack_sequence_as(\n\u001b[0;32m--> 869\u001b[0;31m \u001b[0mstructure\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mentries\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 870\u001b[0m expand_composites=expand_composites)\n\u001b[1;32m 871\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/tensorflow/python/util/nest.py\u001b[0m in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 867\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 868\u001b[0m return pack_sequence_as(\n\u001b[0;32m--> 869\u001b[0;31m \u001b[0mstructure\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mentries\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 870\u001b[0m expand_composites=expand_composites)\n\u001b[1;32m 871\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/keras/utils/tf_utils.py\u001b[0m in \u001b[0;36m_to_single_numpy_or_python_type\u001b[0;34m(t)\u001b[0m\n\u001b[1;32m 548\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_to_single_numpy_or_python_type\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 549\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 550\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 552\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mt\u001b[0m \u001b[0;31m# Don't turn ragged or sparse tensors to NumPy.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py\u001b[0m in \u001b[0;36mnumpy\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1147\u001b[0m \"\"\"\n\u001b[1;32m 1148\u001b[0m \u001b[0;31m# TODO(slebedev): Consider avoiding a copy for non-CPU or remote tensors.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1149\u001b[0;31m \u001b[0mmaybe_arr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_numpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# pylint: disable=protected-access\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1150\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmaybe_arr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmaybe_arr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mmaybe_arr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1151\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py\u001b[0m in \u001b[0;36m_numpy\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1113\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_numpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1114\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1115\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_numpy_internal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1116\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_NotOkStatusException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pylint: disable=protected-access\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1117\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_status_to_exception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;31m# pylint: disable=protected-access\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "model.fit(x=train_generator,\n", " steps_per_epoch=len(train_generator),\n", " validation_data=validation_generator,\n", " validation_steps=len(validation_generator),\n", " epochs=30,\n", " verbose=1)" ] }, { "cell_type": "code", "execution_count": null, "id": "63f791af", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }