{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport cv2\nimport keras\nimport tensorflow as tf\n\nfrom keras_preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras import regularizers, optimizers\nfrom sklearn.utils import class_weight\nimport os\nfrom PIL import Image\nimport shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-12T09:42:33.220616Z","iopub.execute_input":"2022-12-12T09:42:33.221105Z","iopub.status.idle":"2022-12-12T09:42:40.890260Z","shell.execute_reply.started":"2022-12-12T09:42:33.221004Z","shell.execute_reply":"2022-12-12T09:42:40.889182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Understanding train data\ntrain_df=pd.read_csv(r\"/kaggle/input/siim-isic-melanoma-classification/train.csv\")\ntest_df=pd.read_csv(r\"/kaggle/input/siim-isic-melanoma-classification/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:40.894067Z","iopub.execute_input":"2022-12-12T09:42:40.895186Z","iopub.status.idle":"2022-12-12T09:42:41.016539Z","shell.execute_reply.started":"2022-12-12T09:42:40.895146Z","shell.execute_reply":"2022-12-12T09:42:41.015592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def zoom_at(img, zoom=1, angle=0, coord=None):\n    \n    cy, cx = [ i/2 for i in img.shape[:-1] ] if coord is None else coord[::-1]\n    \n    rot_mat = cv2.getRotationMatrix2D((cx,cy), angle, zoom)\n    result = cv2.warpAffine(img, rot_mat, img.shape[1::-1], flags=cv2.INTER_LINEAR)\n    \n    return result","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:41.017669Z","iopub.execute_input":"2022-12-12T09:42:41.018423Z","iopub.status.idle":"2022-12-12T09:42:41.024765Z","shell.execute_reply.started":"2022-12-12T09:42:41.018388Z","shell.execute_reply":"2022-12-12T09:42:41.023828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/working/two_fifty_six/melanoma\n\n# /kaggle/working/two_fifty_six/non_melanoma","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:41.026083Z","iopub.execute_input":"2022-12-12T09:42:41.026483Z","iopub.status.idle":"2022-12-12T09:42:41.041178Z","shell.execute_reply.started":"2022-12-12T09:42:41.026442Z","shell.execute_reply":"2022-12-12T09:42:41.039746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_preprocessing(path):\n\n    animage=cv2.imread(path)\n    animage = cv2.resize(animage, (600,600))\n    # animage=animage.astype(np.uint8)\n    animage = cv2.cvtColor(animage, cv2.COLOR_BGR2RGB)\n\n    kernel = np.ones((2,2), np.uint8)\n    # animage = cv2.dilate(animage, kernel, iterations=1)\n    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(256,256))\n    animage[:, :, 0] = clahe.apply(animage[:, :, 0])\n    # animage = cv2.medianBlur(animage, 5)\n    animage=cv2.addWeighted (animage,4, cv2.GaussianBlur( animage , (0,0) , 256/10) ,-4 ,128)\n    # animage = cv2.dilate(animage, kernel, iterations=1)\n\n    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(256,256))\n    animage[:, :, 0] = clahe.apply(animage[:, :, 0])\n    animage=zoom_at(animage, zoom=1.1, angle=0, coord=None)\n#     animage = tf.convert_to_tensor(animage, dtype=tf.float32)\n    \n    return animage","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:41.044297Z","iopub.execute_input":"2022-12-12T09:42:41.044731Z","iopub.status.idle":"2022-12-12T09:42:41.057060Z","shell.execute_reply.started":"2022-12-12T09:42:41.044694Z","shell.execute_reply":"2022-12-12T09:42:41.055927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path=r'/kaggle/input/siim-isic-melanoma-classification/jpeg/train'\ntrain_nonmel=train_df[train_df['target']==0]\ntrain_mel=train_df[train_df['target']==1]\n","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:41.058293Z","iopub.execute_input":"2022-12-12T09:42:41.058941Z","iopub.status.idle":"2022-12-12T09:42:41.088539Z","shell.execute_reply.started":"2022-12-12T09:42:41.058899Z","shell.execute_reply":"2022-12-12T09:42:41.087283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# im = Image.fromarray(A)\n# im.save(\"your_file.jpeg\")","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:41.089919Z","iopub.execute_input":"2022-12-12T09:42:41.090268Z","iopub.status.idle":"2022-12-12T09:42:41.095265Z","shell.execute_reply.started":"2022-12-12T09:42:41.090236Z","shell.execute_reply":"2022-12-12T09:42:41.093932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('/kaggle/working/six_hundred/melanoma')","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:41.096558Z","iopub.execute_input":"2022-12-12T09:42:41.097542Z","iopub.status.idle":"2022-12-12T09:42:41.107062Z","shell.execute_reply.started":"2022-12-12T09:42:41.097506Z","shell.execute_reply":"2022-12-12T09:42:41.106019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# os.rmdir('/kaggle/working/two_fifty_six')\n# shutil.rmtree('/kaggle/working/two_fifty_six')","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:41.108188Z","iopub.execute_input":"2022-12-12T09:42:41.108773Z","iopub.status.idle":"2022-12-12T09:42:41.120211Z","shell.execute_reply.started":"2022-12-12T09:42:41.108742Z","shell.execute_reply":"2022-12-12T09:42:41.118938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in train_mel.index:\n    image_name=train_mel.loc[i,'image_name']+'.jpg'\n    image_path=os.path.join(train_images_path,image_name)\n    animage=image_preprocessing(image_path)\n    im = Image.fromarray(animage)\n    save_path=r'/kaggle/working/six_hundred/melanoma'\n    im.save(os.path.join(save_path,image_name))","metadata":{"execution":{"iopub.status.busy":"2022-12-12T09:42:41.122007Z","iopub.execute_input":"2022-12-12T09:42:41.122701Z","iopub.status.idle":"2022-12-12T09:45:43.902983Z","shell.execute_reply.started":"2022-12-12T09:42:41.122646Z","shell.execute_reply":"2022-12-12T09:45:43.901793Z"},"trusted":true},"execution_count":null,"outputs":[]}]}