{"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":"# The objective of the notebook is to show how use different loss functions in binary classification in Tensorflow.\n \n    \n# Here I have used \n\n# * Cross_entropy\n# * Huber\n# * KLDivergence\n\nspecial mention to this notebook https://www.kaggle.com/code/rohitgadhwar/image-classification-using-cnn-transfer-learning ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nimport os\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as img\nimport seaborn as sns\nfrom tensorflow.keras.models import Model\nseed = 42\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-11T18:06:01.489905Z","iopub.execute_input":"2022-10-11T18:06:01.490372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load and Extract data","metadata":{}},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test1', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR_PATH = './train'\nfile_names = os.listdir(TRAIN_DIR_PATH)\nprint('There are {} number of images in directory.'.format(len(file_names)))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_dataframe(file_names):\n    files, labels = list(), list()\n    for file in file_names:\n        files.append(file)\n        labels.append(file[:3])\n    df = pd.DataFrame({'filename':files, 'label':labels})\n    return df\n\ndf = to_dataframe(file_names)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analyze data","metadata":{}},{"cell_type":"markdown","source":"Looking at a few random photos in the directory, you can see that the photos are color and have different shapes and sizes.","metadata":{}},{"cell_type":"markdown","source":"# Split data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_set, valid_set = train_test_split(df, test_size=0.2,random_state= seed)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.shape, valid_set.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image data Generator and Data Augmentation","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nWIDTH, HEIGHT = 256, 256\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2022-10-09T10:26:56.428047Z","iopub.execute_input":"2022-10-09T10:26:56.428467Z","iopub.status.idle":"2022-10-09T10:26:56.437178Z","shell.execute_reply.started":"2022-10-09T10:26:56.428433Z","shell.execute_reply":"2022-10-09T10:26:56.434167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagenerator = ImageDataGenerator(rotation_range=15, \n                                  rescale=1./255, \n                                  shear_range=0.1,\n                                  zoom_range=0.2,\n                                  horizontal_flip=True,\n                                  width_shift_range=0.1,\n                                  height_shift_range=0.1)\n\ntraining_data = train_datagenerator.flow_from_dataframe(dataframe=train_set, \n                                                        directory='./train',\n                                                       x_col='filename',\n                                                       y_col='label',\n                                                       target_size=(WIDTH, HEIGHT),\n                                                       class_mode='categorical',\n                                                       batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T10:26:56.438747Z","iopub.execute_input":"2022-10-09T10:26:56.439230Z","iopub.status.idle":"2022-10-09T10:26:56.623633Z","shell.execute_reply.started":"2022-10-09T10:26:56.439195Z","shell.execute_reply":"2022-10-09T10:26:56.622656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_datagenerator = ImageDataGenerator(rescale=1./255)\nvalidation_data = valid_datagenerator.flow_from_dataframe(dataframe=valid_set,\n                                                         directory='./train',\n                                                         x_col='filename',\n                                                         y_col='label',\n                                                         target_size=(WIDTH,HEIGHT),\n                                                         class_mode='categorical',\n                                                         batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T10:26:56.627000Z","iopub.execute_input":"2022-10-09T10:26:56.627308Z","iopub.status.idle":"2022-10-09T10:26:56.680600Z","shell.execute_reply.started":"2022-10-09T10:26:56.627281Z","shell.execute_reply":"2022-10-09T10:26:56.679510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(IMG_SIZE = 256, output = 2 , train = False , activation = \"softmax\" ):\n\n\n    IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)  # IMG_SIZE = 256\n    \n    model = tf.keras.applications.xception.Xception(\n       input_shape=IMG_SHAPE,\n        include_top=False,\n        weights='imagenet'\n        )\n\n    x = tf.keras.layers.Conv2D(128, (3, 3), activation='relu')(model.output)\n    x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x)\n    x = tf.keras.layers.Flatten()(x)\n    x = tf.keras.layers.Dense(100, activation='relu')(x)\n    x = tf.keras.layers.Dense(output, activation= activation)(x)\n    \n    if(train == False):\n\n        model.trainable = False\n\n    model = Model(inputs=model.input, outputs=x)\n\n    my_model = tf.keras.models.clone_model(model)\n    return my_model","metadata":{"execution":{"iopub.status.busy":"2022-10-09T10:26:56.682362Z","iopub.execute_input":"2022-10-09T10:26:56.682789Z","iopub.status.idle":"2022-10-09T10:26:56.693023Z","shell.execute_reply.started":"2022-10-09T10:26:56.682752Z","shell.execute_reply":"2022-10-09T10:26:56.691963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n    \nlosses = {\"Cross_entropy\" : tf.keras.losses.CategoricalCrossentropy(),\n          \"Huber\" : tf.keras.losses.Huber(),\n          \"KLDivergence\" : tf.keras.losses.KLDivergence()}\n    \n    # Compile the model\nfor name , loss in losses.items():\n    print()\n    print(\"_________________________\")\n    print()\n    print(name)\n    print()\n    print(\"_________________________\")\n    print()\n    \n    model = create_model()\n    \n    model.compile(loss= loss, \n                    optimizer= tf.keras.optimizers.Adam(learning_rate=0.0001, beta_1=0.9, beta_2=0.999, decay=0.0001),\n                    metrics=['accuracy'])\n\n\n    \n    # Fit data to model\n    history = model.fit(training_data,\n                        epochs=5,\n                        validation_data=validation_data,\n                        validation_steps= 100,      #validation_steps=valid_set.shape[0]//batch_size,\n                        steps_per_epoch= 100,  )    #steps_per_epoch=train_set.shape[0]//batch_size,\n                                                    \n    \n    \n    loss, acc = model.evaluate(validation_data, steps=len(validation_data), verbose=1)\n    print(\"acc = \" , acc )\n    print(\"loss = \" ,  loss)\n    \n\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-10-09T10:52:40.511821Z","iopub.execute_input":"2022-10-09T10:52:40.512221Z","iopub.status.idle":"2022-10-09T11:13:41.928816Z","shell.execute_reply.started":"2022-10-09T10:52:40.512187Z","shell.execute_reply":"2022-10-09T11:13:41.927707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The purpose of the notebook is not to get high accuracy, rather the objective is to explore different loss functions. ","metadata":{}}]}