{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n'''\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n'''\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-04T15:08:43.62146Z","iopub.execute_input":"2022-03-04T15:08:43.621967Z","iopub.status.idle":"2022-03-04T15:08:43.654813Z","shell.execute_reply.started":"2022-03-04T15:08:43.621859Z","shell.execute_reply":"2022-03-04T15:08:43.653616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tqdm import tqdm\nimport cv2\nimport random\n\nfrom sklearn.utils import shuffle\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, accuracy_score, precision_score, recall_score\nfrom imblearn.over_sampling import RandomOverSampler\n#from sklearn.metrics import accuracy_score\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, Dropout, Dense, Flatten, BatchNormalization\nfrom tensorflow.keras.optimizers import SGD, Adam\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:43.656688Z","iopub.execute_input":"2022-03-04T15:08:43.656913Z","iopub.status.idle":"2022-03-04T15:08:51.995955Z","shell.execute_reply.started":"2022-03-04T15:08:43.656888Z","shell.execute_reply":"2022-03-04T15:08:51.994988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input')","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:51.997389Z","iopub.execute_input":"2022-03-04T15:08:51.997624Z","iopub.status.idle":"2022-03-04T15:08:52.008993Z","shell.execute_reply.started":"2022-03-04T15:08:51.997598Z","shell.execute_reply":"2022-03-04T15:08:52.006546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = os.path.join(\"../input\", 'siim-isic-melanoma-classification')\nprint(path)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.013288Z","iopub.execute_input":"2022-03-04T15:08:52.014374Z","iopub.status.idle":"2022-03-04T15:08:52.031652Z","shell.execute_reply.started":"2022-03-04T15:08:52.014308Z","shell.execute_reply":"2022-03-04T15:08:52.030304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.033081Z","iopub.execute_input":"2022-03-04T15:08:52.033775Z","iopub.status.idle":"2022-03-04T15:08:52.045938Z","shell.execute_reply.started":"2022-03-04T15:08:52.033721Z","shell.execute_reply":"2022-03-04T15:08:52.044794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/siim-isic-melanoma-classification/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.047541Z","iopub.execute_input":"2022-03-04T15:08:52.047814Z","iopub.status.idle":"2022-03-04T15:08:52.167066Z","shell.execute_reply.started":"2022-03-04T15:08:52.047773Z","shell.execute_reply":"2022-03-04T15:08:52.166072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/siim-isic-melanoma-classification/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.168607Z","iopub.execute_input":"2022-03-04T15:08:52.168961Z","iopub.status.idle":"2022-03-04T15:08:52.210219Z","shell.execute_reply.started":"2022-03-04T15:08:52.168914Z","shell.execute_reply":"2022-03-04T15:08:52.209152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.index","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.211734Z","iopub.execute_input":"2022-03-04T15:08:52.212011Z","iopub.status.idle":"2022-03-04T15:08:52.219493Z","shell.execute_reply.started":"2022-03-04T15:08:52.211969Z","shell.execute_reply":"2022-03-04T15:08:52.218523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.loc[train['image_name'] == 'ISIC_0068279'].index)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.22122Z","iopub.execute_input":"2022-03-04T15:08:52.221526Z","iopub.status.idle":"2022-03-04T15:08:52.247382Z","shell.execute_reply.started":"2022-03-04T15:08:52.22148Z","shell.execute_reply":"2022-03-04T15:08:52.246451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr =[] \narr.append(train['target'][train.loc[train['image_name'] == 'ISIC_0068279'].index])\nprint(arr[0] +1)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.251697Z","iopub.execute_input":"2022-03-04T15:08:52.252257Z","iopub.status.idle":"2022-03-04T15:08:52.268081Z","shell.execute_reply.started":"2022-03-04T15:08:52.252218Z","shell.execute_reply":"2022-03-04T15:08:52.26712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['image_name'][0]","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.269927Z","iopub.execute_input":"2022-03-04T15:08:52.270503Z","iopub.status.idle":"2022-03-04T15:08:52.284084Z","shell.execute_reply.started":"2022-03-04T15:08:52.270449Z","shell.execute_reply":"2022-03-04T15:08:52.282783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train['image_name'])","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.28581Z","iopub.execute_input":"2022-03-04T15:08:52.286356Z","iopub.status.idle":"2022-03-04T15:08:52.297699Z","shell.execute_reply.started":"2022-03-04T15:08:52.286309Z","shell.execute_reply":"2022-03-04T15:08:52.296499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/siim-isic-melanoma-classification/\"\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.299581Z","iopub.execute_input":"2022-03-04T15:08:52.300036Z","iopub.status.idle":"2022-03-04T15:08:52.312732Z","shell.execute_reply.started":"2022-03-04T15:08:52.299946Z","shell.execute_reply":"2022-03-04T15:08:52.311281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread(os.path.join(path, 'jpeg', 'train', train['image_name'][0] + '.jpg'))\nimg = cv2.resize(img, (32,32))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:52.314516Z","iopub.execute_input":"2022-03-04T15:08:52.315566Z","iopub.status.idle":"2022-03-04T15:08:53.100209Z","shell.execute_reply.started":"2022-03-04T15:08:52.315513Z","shell.execute_reply":"2022-03-04T15:08:53.099471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:53.101424Z","iopub.execute_input":"2022-03-04T15:08:53.102069Z","iopub.status.idle":"2022-03-04T15:08:53.10826Z","shell.execute_reply.started":"2022-03-04T15:08:53.102037Z","shell.execute_reply":"2022-03-04T15:08:53.107504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_arrays (path) :\n    images_data = []\n    labels_data = []\n    train = pd.read_csv(os.path.join(path, 'train.csv'))\n    for k in tqdm(range(len(train['image_name']))) :\n        try :\n            img = plt.imread(os.path.join(path, 'jpeg', 'train', train['image_name'][k] + '.jpg'))\n            img = cv2.resize(img, (32,32))\n            images_data.append(img)\n            labels_data.append(train['target'][k])\n        except :\n            continue\n    print(\"finished : \", len(images_data))\n    return images_data, labels_data","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:53.109449Z","iopub.execute_input":"2022-03-04T15:08:53.110175Z","iopub.status.idle":"2022-03-04T15:08:53.119953Z","shell.execute_reply.started":"2022-03-04T15:08:53.110117Z","shell.execute_reply":"2022-03-04T15:08:53.119172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_data, labels_data = prepare_arrays(path)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T15:08:53.121082Z","iopub.execute_input":"2022-03-04T15:08:53.121743Z","iopub.status.idle":"2022-03-04T17:50:13.543944Z","shell.execute_reply.started":"2022-03-04T15:08:53.121712Z","shell.execute_reply":"2022-03-04T17:50:13.538703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(images_data[30000])\nprint(labels_data[30000])","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:13.5518Z","iopub.execute_input":"2022-03-04T17:50:13.555556Z","iopub.status.idle":"2022-03-04T17:50:13.842592Z","shell.execute_reply.started":"2022-03-04T17:50:13.555442Z","shell.execute_reply":"2022-03-04T17:50:13.841469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images_data)\n#print(labels_data[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:13.844311Z","iopub.execute_input":"2022-03-04T17:50:13.845104Z","iopub.status.idle":"2022-03-04T17:50:13.852957Z","shell.execute_reply.started":"2022-03-04T17:50:13.845051Z","shell.execute_reply":"2022-03-04T17:50:13.851704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = images_data.copy()\nlabels = labels_data.copy()","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:13.854877Z","iopub.execute_input":"2022-03-04T17:50:13.855151Z","iopub.status.idle":"2022-03-04T17:50:13.867743Z","shell.execute_reply.started":"2022-03-04T17:50:13.855108Z","shell.execute_reply":"2022-03-04T17:50:13.866829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_samples (images, labels) :\n    real_labels = [\"benign\", \"malignant\"]\n    fig = plt.figure(figsize = (20,25))\n    rand = random.randint(0, len(images))\n\n    for i in range(20): \n        fig.add_subplot(5,4, i+1)\n        plt.title(real_labels[int(labels[rand])])\n        plt.imshow(images[rand])\n        rand = random.randint(0, len(images))","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:13.869838Z","iopub.execute_input":"2022-03-04T17:50:13.870119Z","iopub.status.idle":"2022-03-04T17:50:13.88102Z","shell.execute_reply.started":"2022-03-04T17:50:13.870088Z","shell.execute_reply":"2022-03-04T17:50:13.879877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_samples(images, labels)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:13.882765Z","iopub.execute_input":"2022-03-04T17:50:13.882997Z","iopub.status.idle":"2022-03-04T17:50:17.718823Z","shell.execute_reply.started":"2022-03-04T17:50:13.882968Z","shell.execute_reply":"2022-03-04T17:50:17.717241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# processing the data for the model","metadata":{}},{"cell_type":"code","source":"def resize_array(images, size) :\n    for i in range(len(images)) :\n        images[i] = cv2.resize(images[i], size)\n    return images","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:17.720511Z","iopub.execute_input":"2022-03-04T17:50:17.721138Z","iopub.status.idle":"2022-03-04T17:50:17.726644Z","shell.execute_reply.started":"2022-03-04T17:50:17.721104Z","shell.execute_reply":"2022-03-04T17:50:17.725347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder()\ndef preprocess_data(images, labels) :\n    labels = le.fit_transform(labels)\n    labels = to_categorical(labels, 2)\n    images, labels = shuffle(images, labels, random_state = 32)\n    x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size = 0.25, random_state = 32)\n    x_train = np.array(x_train) \n    x_test = np.array(x_test) \n    y_train = np.array(y_train) \n    y_test = np.array(y_test)\n    return x_train, x_test, y_train, y_test","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:17.727995Z","iopub.execute_input":"2022-03-04T17:50:17.728225Z","iopub.status.idle":"2022-03-04T17:50:17.741097Z","shell.execute_reply.started":"2022-03-04T17:50:17.728197Z","shell.execute_reply":"2022-03-04T17:50:17.7402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = preprocess_data(images, labels)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:17.742554Z","iopub.execute_input":"2022-03-04T17:50:17.743177Z","iopub.status.idle":"2022-03-04T17:50:18.159138Z","shell.execute_reply.started":"2022-03-04T17:50:17.743135Z","shell.execute_reply":"2022-03-04T17:50:18.158384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# modeling","metadata":{}},{"cell_type":"code","source":"def build_model () :\n    model = Sequential()\n    model.add(Conv2D(filters = 128, kernel_size = (4,4), input_shape = (32, 32, 3), activation = 'relu'))\n    model.add(MaxPool2D(pool_size = (4,4)))\n    model.add(Conv2D(filters = 64, kernel_size = (2,2), activation = 'relu'))\n    model.add(MaxPool2D(pool_size = (2,2)))\n    model.add(BatchNormalization())\n    #model.add(GlobalAveragePooling2D())\n\n    model.add(Flatten())\n    model.add(Dense(128, activation = 'relu'))\n    model.add(Dropout(0.1))\n    model.add(Dense(2, activation = 'sigmoid')) # sigmoid is better for binary classification\n\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:18.160522Z","iopub.execute_input":"2022-03-04T17:50:18.161991Z","iopub.status.idle":"2022-03-04T17:50:18.170982Z","shell.execute_reply.started":"2022-03-04T17:50:18.161937Z","shell.execute_reply":"2022-03-04T17:50:18.170116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model()","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:18.172441Z","iopub.execute_input":"2022-03-04T17:50:18.173595Z","iopub.status.idle":"2022-03-04T17:50:18.498225Z","shell.execute_reply.started":"2022-03-04T17:50:18.173517Z","shell.execute_reply":"2022-03-04T17:50:18.497359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(model, epochs_num) :\n    model.compile(loss = 'binary_crossentropy', optimizer = 'Adam', metrics = ['accuracy'])\n    early_stop = EarlyStopping(monitor = 'val_loss', patience = 3)\n    hist = model.fit(x_train, y_train, epochs = epochs_num, validation_data = (x_test, y_test))#, callbacks = early_stop)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:18.505859Z","iopub.execute_input":"2022-03-04T17:50:18.506168Z","iopub.status.idle":"2022-03-04T17:50:18.513309Z","shell.execute_reply.started":"2022-03-04T17:50:18.506134Z","shell.execute_reply":"2022-03-04T17:50:18.512316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_scores (model) :\n    scores = pd.DataFrame(model.history.history)\n    titles = [\"accuaracy vs val_accuracy\", \"loss vs val_loss\"]\n    fig, axes = plt.subplots(nrows = 1, ncols = 2, figsize = (24,6))\n    #fig, ax = plt.figure(figsize = (20,8))\n\n    for i in range(2): \n        fig.add_subplot(1,2, i+1)\n        axes[i].set_title(titles[i])\n        if i==0 : plt.plot(scores[['accuracy','val_accuracy']])#, color = 'green')\n        if i==1 : plt.plot(scores[['loss','val_loss']])#, color = 'green')\n        #scores[['accuracy', 'val_accuracy']].plot(ax = axes[i])","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:18.555326Z","iopub.execute_input":"2022-03-04T17:50:18.555643Z","iopub.status.idle":"2022-03-04T17:50:18.566361Z","shell.execute_reply.started":"2022-03-04T17:50:18.555591Z","shell.execute_reply":"2022-03-04T17:50:18.56527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# modeling image augmentation","metadata":{}},{"cell_type":"code","source":"#be calm and watch wa3i\ndef image_augmentation(x_train, x_test, y_train, y_test) :\n    image_generator = ImageDataGenerator(\n        samplewise_center = True,\n        samplewise_std_normalization = True,\n        rotation_range = 25, \n        zoom_range = 0.1,\n        horizontal_flip = True#, fill_mode = \"nearst\"\n    )\n    train_generator = image_generator.flow(x_train, y_train, batch_size = 32)\n    test_generator = image_generator.flow(x_test, y_test, batch_size = 32)\n    return train_generator, test_generator","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:18.568485Z","iopub.execute_input":"2022-03-04T17:50:18.568806Z","iopub.status.idle":"2022-03-04T17:50:18.583113Z","shell.execute_reply.started":"2022-03-04T17:50:18.568764Z","shell.execute_reply":"2022-03-04T17:50:18.582195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator, test_generator = image_augmentation(x_train, x_test, y_train, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:18.584284Z","iopub.execute_input":"2022-03-04T17:50:18.584597Z","iopub.status.idle":"2022-03-04T17:50:19.070567Z","shell.execute_reply.started":"2022-03-04T17:50:18.584556Z","shell.execute_reply":"2022-03-04T17:50:19.069628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:19.071848Z","iopub.execute_input":"2022-03-04T17:50:19.072119Z","iopub.status.idle":"2022-03-04T17:50:19.079121Z","shell.execute_reply.started":"2022-03-04T17:50:19.072087Z","shell.execute_reply":"2022-03-04T17:50:19.077999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelA =  build_model()","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:19.080745Z","iopub.execute_input":"2022-03-04T17:50:19.080975Z","iopub.status.idle":"2022-03-04T17:50:19.161018Z","shell.execute_reply.started":"2022-03-04T17:50:19.08095Z","shell.execute_reply":"2022-03-04T17:50:19.15993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def trainA(model, epochs_num) :\n    model.compile(loss = 'binary_crossentropy', optimizer = 'Adam', metrics = ['accuracy'])\n    early_stop = EarlyStopping(monitor = 'val_loss', patience = 3)\n    hist = model.fit(train_generator, epochs = epochs_num, validation_data = (test_generator))#, callbacks = early_stop)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:19.162887Z","iopub.execute_input":"2022-03-04T17:50:19.163108Z","iopub.status.idle":"2022-03-04T17:50:19.169442Z","shell.execute_reply.started":"2022-03-04T17:50:19.163081Z","shell.execute_reply":"2022-03-04T17:50:19.168455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# oversamping our data","metadata":{}},{"cell_type":"code","source":"images_arr = np.array(images)\nprint(images_arr.shape)\nreshaped_images = images_arr.reshape(images_arr.shape[0], -1)\nprint(reshaped_images.shape)\n\noversample = RandomOverSampler()\noversample_images_non_normal, oversample_labels = oversample.fit_resample(reshaped_images, labels)\n\noversample_images = oversample_images_non_normal.reshape(-1, 32,32,3)\nprint(oversample_images.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:19.170962Z","iopub.execute_input":"2022-03-04T17:50:19.171706Z","iopub.status.idle":"2022-03-04T17:50:19.863863Z","shell.execute_reply.started":"2022-03-04T17:50:19.171658Z","shell.execute_reply":"2022-03-04T17:50:19.862767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = 0\nfor i in oversample_labels :\n    if i == 1:\n        c += 1\n        \nprint(c)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:19.865273Z","iopub.execute_input":"2022-03-04T17:50:19.865549Z","iopub.status.idle":"2022-03-04T17:50:19.881003Z","shell.execute_reply.started":"2022-03-04T17:50:19.865518Z","shell.execute_reply":"2022-03-04T17:50:19.879769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(oversample_images[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:19.882888Z","iopub.execute_input":"2022-03-04T17:50:19.883198Z","iopub.status.idle":"2022-03-04T17:50:20.115365Z","shell.execute_reply.started":"2022-03-04T17:50:19.883155Z","shell.execute_reply":"2022-03-04T17:50:20.114297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# New models","metadata":{}},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = preprocess_data(oversample_images, oversample_labels)\nmodel = build_model()\ntrain(model, 12)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T17:50:20.116848Z","iopub.execute_input":"2022-03-04T17:50:20.117098Z","iopub.status.idle":"2022-03-04T18:00:54.744585Z","shell.execute_reply.started":"2022-03-04T17:50:20.117066Z","shell.execute_reply":"2022-03-04T18:00:54.743251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_scores(model) ","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:00:54.747493Z","iopub.execute_input":"2022-03-04T18:00:54.748576Z","iopub.status.idle":"2022-03-04T18:00:55.328093Z","shell.execute_reply.started":"2022-03-04T18:00:54.74852Z","shell.execute_reply":"2022-03-04T18:00:55.327251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.argmax(model.predict(x_test), axis = -1)\ny_testc = np.argmax(y_test, axis = -1)\n\nprint(classification_report(y_testc, pred))","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:00:55.329557Z","iopub.execute_input":"2022-03-04T18:00:55.32979Z","iopub.status.idle":"2022-03-04T18:00:59.140552Z","shell.execute_reply.started":"2022-03-04T18:00:55.329761Z","shell.execute_reply":"2022-03-04T18:00:59.139829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('Normal_skin_cancer_model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:00:59.141759Z","iopub.execute_input":"2022-03-04T18:00:59.142377Z","iopub.status.idle":"2022-03-04T18:00:59.212266Z","shell.execute_reply.started":"2022-03-04T18:00:59.142343Z","shell.execute_reply":"2022-03-04T18:00:59.211301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# oversampling with augmentation","metadata":{}},{"cell_type":"code","source":"train_generator, test_generator = image_augmentation(x_train, x_test, y_train, y_test)\nmodelA = build_model()\ntrainA(modelA, 24)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:00:59.213567Z","iopub.execute_input":"2022-03-04T18:00:59.213797Z","iopub.status.idle":"2022-03-04T18:35:10.5217Z","shell.execute_reply.started":"2022-03-04T18:00:59.213768Z","shell.execute_reply":"2022-03-04T18:35:10.520214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_scores(modelA) ","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:35:10.52415Z","iopub.execute_input":"2022-03-04T18:35:10.524551Z","iopub.status.idle":"2022-03-04T18:35:11.19103Z","shell.execute_reply.started":"2022-03-04T18:35:10.524501Z","shell.execute_reply":"2022-03-04T18:35:11.187159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelA.evaluate(train_generator)\nmodelA.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:35:11.192512Z","iopub.execute_input":"2022-03-04T18:35:11.192934Z","iopub.status.idle":"2022-03-04T18:36:48.094926Z","shell.execute_reply.started":"2022-03-04T18:35:11.192898Z","shell.execute_reply":"2022-03-04T18:36:48.093942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predA = np.argmax(modelA.predict(x_test), axis = -1)\ny_testc = np.argmax(y_test, axis = -1)\n\n\nprint(len(x_test))\nprint(len(y_test))\nprint(accuracy_score(y_testc, predA))\nprint(classification_report(y_testc, predA))","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:37:16.85501Z","iopub.execute_input":"2022-03-04T18:37:16.855321Z","iopub.status.idle":"2022-03-04T18:37:21.277774Z","shell.execute_reply.started":"2022-03-04T18:37:16.855286Z","shell.execute_reply":"2022-03-04T18:37:21.276504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelA.save('Augmented_skin_cancer_model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:51.970898Z","iopub.execute_input":"2022-03-04T18:36:51.971162Z","iopub.status.idle":"2022-03-04T18:36:52.010847Z","shell.execute_reply.started":"2022-03-04T18:36:51.971128Z","shell.execute_reply":"2022-03-04T18:36:52.010004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# evaluation","metadata":{}},{"cell_type":"code","source":"def prepare_eval_array (path) :\n    eval_array = []\n    train = pd.read_csv(os.path.join(path, 'train.csv'))\n    for k in tqdm(range(10)) :\n        rand = random.randint(0, len(test['image_name']))\n        try :\n            img = plt.imread(os.path.join(path, 'jpeg', 'test', test['image_name'][rand] + '.jpg'))\n            img = cv2.resize(img, (32,32))\n            eval_array.append(img)\n        except :\n            continue\n    print(\"finished : \", len(eval_array))\n    return eval_array","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:52.012336Z","iopub.execute_input":"2022-03-04T18:36:52.012769Z","iopub.status.idle":"2022-03-04T18:36:52.020971Z","shell.execute_reply.started":"2022-03-04T18:36:52.012736Z","shell.execute_reply":"2022-03-04T18:36:52.020007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_array = prepare_eval_array(path)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:52.022526Z","iopub.execute_input":"2022-03-04T18:36:52.022818Z","iopub.status.idle":"2022-03-04T18:36:54.472346Z","shell.execute_reply.started":"2022-03-04T18:36:52.022787Z","shell.execute_reply":"2022-03-04T18:36:54.471063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_predictions (model) :\n    fig = plt.figure(figsize = (20,8))\n    real_labels = [\"benign\", \"malignant\"]\n\n    for i in range(10) :\n        img = eval_array[i]\n        img = cv2.resize(img, (64,64))\n        img = img.reshape(1,64,64,3)\n        val = np.argmax(model.predict(img))\n        fig.add_subplot(2,5, i+1)\n        plt.title(real_labels[val])\n        plt.imshow(eval_array[i])","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:54.474268Z","iopub.execute_input":"2022-03-04T18:36:54.47465Z","iopub.status.idle":"2022-03-04T18:36:54.484861Z","shell.execute_reply.started":"2022-03-04T18:36:54.474606Z","shell.execute_reply":"2022-03-04T18:36:54.483433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_predictions(model)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:54.486608Z","iopub.execute_input":"2022-03-04T18:36:54.486876Z","iopub.status.idle":"2022-03-04T18:36:55.122998Z","shell.execute_reply.started":"2022-03-04T18:36:54.486844Z","shell.execute_reply":"2022-03-04T18:36:55.121387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_predictions(modelA)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:55.124275Z","iopub.status.idle":"2022-03-04T18:36:55.125037Z","shell.execute_reply.started":"2022-03-04T18:36:55.124801Z","shell.execute_reply":"2022-03-04T18:36:55.124827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# images from website","metadata":{}},{"cell_type":"code","source":"os.listdir(\"../input\")","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:55.126605Z","iopub.status.idle":"2022-03-04T18:36:55.127012Z","shell.execute_reply.started":"2022-03-04T18:36:55.126816Z","shell.execute_reply":"2022-03-04T18:36:55.126842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_labels = [\"benign\", \"malignant\"]\nimg = plt.imread(\"../input/evaluation-images/binign_skin1.jpg\")\nimg1 = img.copy()\nimg1 = cv2.resize(img1, (32,32))\nimg1 = img1.reshape(1,32,32,3)\nval = np.argmax(model.predict(img1))\nplt.title(real_labels[val])\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:55.129219Z","iopub.status.idle":"2022-03-04T18:36:55.130082Z","shell.execute_reply.started":"2022-03-04T18:36:55.129858Z","shell.execute_reply":"2022-03-04T18:36:55.129887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"../input/melanoma-image\")","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:55.131833Z","iopub.status.idle":"2022-03-04T18:36:55.132592Z","shell.execute_reply.started":"2022-03-04T18:36:55.132357Z","shell.execute_reply":"2022-03-04T18:36:55.132381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_labels = [\"benign\", \"malignant\"]\nimg = plt.imread(\"../input/melanoma-image/melanoma-14.jpg\")\nimg1 = img.copy()\nimg1 = cv2.resize(img1, (32,32))\nimg1 = img1.reshape(1,32,32,3)\nval = np.argmax(model.predict(img1))\nplt.title(real_labels[val])\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:55.134239Z","iopub.status.idle":"2022-03-04T18:36:55.134599Z","shell.execute_reply.started":"2022-03-04T18:36:55.134422Z","shell.execute_reply":"2022-03-04T18:36:55.134443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_labels = [\"benign\", \"malignant\"]\nimg = plt.imread(\"../input/melanoma-image/melanoma-14.jpg\")\nimg1 = img.copy()\nimg1 = cv2.resize(img1, (32,32))\nimg1 = img1.reshape(1,32,32,3)\nval = np.argmax(modelA.predict(img1))\nplt.title(real_labels[val])\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-03-04T18:36:55.136064Z","iopub.status.idle":"2022-03-04T18:36:55.136433Z","shell.execute_reply.started":"2022-03-04T18:36:55.136232Z","shell.execute_reply":"2022-03-04T18:36:55.136254Z"},"trusted":true},"execution_count":null,"outputs":[]}]}