{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":104884,"sourceType":"datasetVersion","datasetId":54339}],"dockerImageVersionId":30096,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport glob\nimport zipfile\nimport seaborn as sb\nimport numpy as np # linear algebra\nimport pandas as pd # data processing \nfrom PIL import Image\nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.callbacks import EarlyStopping,ModelCheckpoint\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nfrom keras.models import Model,Sequential\nfrom keras.layers import Conv2D, GlobalAveragePooling2D, Dropout, Flatten, Dense, Activation,GlobalMaxPooling2D\nfrom tensorflow.keras.applications.resnet50 import ResNet50,preprocess_input\nfrom sklearn.metrics import confusion_matrix,classification_report\nfrom keras.optimizers import Adam\n\n\nfor dirname, _, filenames in os.walk('/kaggle/input/dogs-vs-cats'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-11T03:09:07.922928Z","iopub.execute_input":"2024-04-11T03:09:07.923674Z","iopub.status.idle":"2024-04-11T03:09:14.842905Z","shell.execute_reply.started":"2024-04-11T03:09:07.923544Z","shell.execute_reply":"2024-04-11T03:09:14.841592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Unzip the files \nzip_file = glob.glob('/kaggle/input/dogs-vs-cats/test1*.zip')  #return any files with .zip extension\nfor file in zip_file:\n    #extract file into a temp folder\n    with zipfile.ZipFile(file,\"r\") as zip_ref:\n        zip_ref.extractall(\"datasets\")\n        ","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:14.844756Z","iopub.execute_input":"2024-04-11T03:09:14.845197Z","iopub.status.idle":"2024-04-11T03:09:21.839965Z","shell.execute_reply.started":"2024-04-11T03:09:14.845152Z","shell.execute_reply":"2024-04-11T03:09:21.838952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"  def prepare_data(dir_path):\n    filenames = os.listdir(dir_path)\n    labels = []\n    for filename in filenames:\n        label = filename.split('.')[0]\n        if label == 'dog':\n            labels.append(1)\n        else:\n            labels.append(0)\n    return filenames,labels\n\n#prepare data \ntrain_filenames,train_labels = prepare_data('datasets/train') \ntest_filenames,test_labels = prepare_data('datasets/test1') \n\ndf_train = pd.DataFrame({'filename':train_filenames,'class': train_labels})\ndf_test = pd.DataFrame({'filename': test_filenames,'class': test_labels})","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:21.842301Z","iopub.execute_input":"2024-04-11T03:09:21.84265Z","iopub.status.idle":"2024-04-11T03:09:22.331038Z","shell.execute_reply.started":"2024-04-11T03:09:21.842618Z","shell.execute_reply":"2024-04-11T03:09:22.327987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skin_df= pd.read_csv('/kaggle/input/skin-cancer-mnist-ham10000/HAM10000_metadata.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.332733Z","iopub.status.idle":"2024-04-11T03:09:22.333706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = {os.path.splitext(os.path.basename(x))}","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.335212Z","iopub.status.idle":"2024-04-11T03:09:22.33614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.337564Z","iopub.status.idle":"2024-04-11T03:09:22.338421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.340355Z","iopub.status.idle":"2024-04-11T03:09:22.341321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape, df_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.342733Z","iopub.status.idle":"2024-04-11T03:09:22.343658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sb.countplot(x='class',data=df_train).set_title(\"Data Distribution\")","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.344996Z","iopub.status.idle":"2024-04-11T03:09:22.345943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we can see that our dataset is prettey balanced. we will try to reduce the samples\n# to 2000 for each class since we will use a pretrained model\ncats = df_train[df_train['class']==0][:3000]\ndogs = df_train[df_train['class']==1][:3000]\n#concat the two dataframes\ndf = pd.concat([cats,dogs])\n#shuffle\ndf = df.sample(frac=1)\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.347237Z","iopub.status.idle":"2024-04-11T03:09:22.34819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DIR_PATH = 'datasets/train/'\n#Let's show some image from the dataframe\nfig, axis = plt.subplots(3, 3, figsize=(20, 20))\nfor i, ax in enumerate(axis.flat):\n    img = load_img(DIR_PATH+df['filename'].iloc[i]) \n    ax.imshow(img)\n    ax.axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.349537Z","iopub.status.idle":"2024-04-11T03:09:22.350415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#set some parameters \nimage_shape = (224,224)\nepochs = 5\nbatch_size = 32\n# to avoid type errors we cast df columns to string opjects\ndf['filename'] = df['filename'].astype('str')\ndf['class'] = df['class'].astype('str')\n#split data ibto train and validation set\ntrain, validation = train_test_split(df, test_size=0.2)\n#reset indexes \ntrain = train.reset_index()\nvalidation = validation.reset_index()\n\n#data augmenetation \ntrain_gen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    width_shift_range=0.1,\n    height_shift_range=0.1)\n\n#train generator \ntrain_generator = train_gen.flow_from_dataframe(\n    train, \n    directory = DIR_PATH,\n    x_col='filename',\n    y_col=\"class\",\n    class_mode=\"categorical\",\n    target_size=image_shape,\n    batch_size=batch_size)\n#validation generator \nvalidation_gen = ImageDataGenerator(\n    rescale=1./255)\n\nval_generator = validation_gen.flow_from_dataframe(\n    validation,  \n    directory = DIR_PATH,\n    x_col='filename',\n    y_col=\"class\",\n    class_mode=\"categorical\",\n    target_size=image_shape,\n    batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.351815Z","iopub.status.idle":"2024-04-11T03:09:22.352693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_image= train.sample(n=1).reset_index(drop=True)\nsample_generator = train_gen.flow_from_dataframe(\n    sample_image,\n    directory = DIR_PATH,\n    x_col='filename',\n    y_col='class')\nplt.figure(figsize=(12, 12))\nfor i in range(0, 9):\n    plt.subplot(3, 3, i+1)\n    for X_batch, Y_batch in sample_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.353986Z","iopub.status.idle":"2024-04-11T03:09:22.354854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#build the Resnet model \nresnet = ResNet50(weights='imagenet',\n                      input_shape= (224,224,3),\n                      include_top= False)  \n\n\n#show the base model summary \nresnet.summary()\n#show how manay layers in the Resnet Network\nlayers = resnet.layers\nprint(f'Number of Layers: {len(layers)} ')","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.356141Z","iopub.status.idle":"2024-04-11T03:09:22.357078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of samples for each set \nsize_train = train.shape[0]\nsize_val = validation.shape[0]\n# early stopping \ncallbacks = EarlyStopping(patience = 3, monitor='val_acc')\n                        \n# let's train our Model \ninputs = resnet.input\n# add an average pooling layer\nx = resnet.output\nx = GlobalAveragePooling2D()(x)\n#first dense layer\nx = Dense(512, activation='relu')(x)\n#dropout \nx = Dropout(0.5)(x)\n# output layer\noutputs = Dense(2, activation ='softmax')(x)\n# this is the model we will train\nmodel = Model(inputs=inputs, outputs=outputs)\n\n# freeze all convolutional Resnet layers\nfor layer in layers:\n    layer.trainable = False\n# compile the model \nmodel.compile(optimizer=Adam(lr=0.001),\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n# train the model on the new data for a few epochs\nhistory = model.fit(\n    train_generator,\n    epochs= epochs,\n    validation_data = val_generator,\n    validation_steps = size_val//batch_size,\n    steps_per_epoch = size_train//batch_size,\n    callbacks = [callbacks])","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.358408Z","iopub.status.idle":"2024-04-11T03:09:22.359388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training loss vs validation loss\nsb.set_style('whitegrid')\nplt.figure()\nfig,(ax1, ax2)=plt.subplots(1,2,figsize=(19,8))\nax1.plot(history.history['loss'])\nax1.plot(history.history['val_loss'])\nax1.legend(['Training','Validation'])\nax1.set_title('Loss')\nax1.set_xlabel('#epochs')\n## plot training accuracy vs validation accuracy \nax2.plot(history.history['accuracy'])\nax2.plot(history.history['val_accuracy'])\nax2.legend(['Training','Validation'])\nax2.set_title('Acurracy')\nax2.set_xlabel('#epochs')","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.36078Z","iopub.status.idle":"2024-04-11T03:09:22.361837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Evaluate\nloss, acc = model.evaluate_generator(val_generator)\nprint(\"Validation Accuracy = %f \\nValidation Loss = %f \" % (acc, loss))","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.363225Z","iopub.status.idle":"2024-04-11T03:09:22.364097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#save the weights of the model to hdf5 file \nmodel.save_weights(\"resnet_model.h5\")\nprint(\"Model Saved.\")","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.3654Z","iopub.status.idle":"2024-04-11T03:09:22.366317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = validation['class'].astype('int32')\npredict =  model.predict_generator(val_generator)\npredictions = np.where(predict > 0.5, 1,0)\npredictions = np.argmax(predictions, axis=1)\n#show the confusion matrix \nconf_matrix = confusion_matrix(labels, predictions) \n# plot the confusion matrix\nfig,ax = plt.subplots(figsize=(8, 8))\nsb.heatmap(conf_matrix, annot=True, linewidths=0.01,cmap=\"Greens\",linecolor=\"gray\", fmt= '.1f',ax=ax)\nplt.xlabel(\"Predicted Class\")\nplt.ylabel(\"True Class\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T03:09:22.367661Z","iopub.status.idle":"2024-04-11T03:09:22.368585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To be CONTINUED...","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}