{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport cv2\nsns.set(color_codes=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datadir = \"/kaggle/input/siim-isic-melanoma-classification/\"\ndf_train = pd.read_csv(datadir + \"train.csv\")\ndf_test = pd.read_csv(datadir + \"test.csv\")\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = ['benign', 'malignant']\n\nsns.catplot(x=\"sex\", y=\"age_approx\", hue=\"benign_malignant\", kind=\"bar\", data=df_train);\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\n\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['image_name'].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_t_0 = df_train[df_train['target']==0].sample(3500)\ndf_t_0['target'] = \"b\"\ndf_t_1 = df_train[df_train['target']==1]\ndf_t_1['target'] = \"m\"\ndf_train = pd.concat([df_t_0, df_t_1])\ndf_train = train.reset_index()\ndel df_t_0\ndel df_t_1\nprint(len(df_train))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['image_name'] = df_train['image_name'].apply(lambda x: os.path.join(datadir, \"jpeg/\" + \"train/\" + x + \".jpg\"))\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_arr = cv2.imread(df_train['image_name'][0])\ndef resize(arr):\n    arr = cv2.resize(arr, (256,256))\n    arr = cv2.cvtColor(arr, cv2.COLOR_BGR2RGB)\n    return arr\nplt.imshow(resize(img_arr))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(df_train['image_name'], df_train['target'], test_size = 0.2, random_state = 1)\nX_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = pd.concat([X_train, y_train], axis = 1)\nX_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_val = pd.concat([X_val, y_val], axis = 1)\nX_val.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_image_generator = ImageDataGenerator(rescale=1./255, \n                                           rotation_range=10,\n                                           width_shift_range=0.2,\n                                           height_shift_range=0.2,\n                                           shear_range=0.2,\n                                           zoom_range=0.2,\n                                           horizontal_flip=True,\n                                           vertical_flip=True)\nvalidation_image_generator = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 128\nimg_size = 256\ntrain_data_gen = train_image_generator.flow_from_dataframe(X_train,\n                                                           x_col = \"image_name\",\n                                                           y_col = \"target\",\n                                                           batch_size=batch_size,\n                                                           shuffle=True,\n                                                           target_size=(img_size, img_size),\n                                                           class_mode='raw')\n\nval_data_gen = validation_image_generator.flow_from_dataframe(X_val,\n                                                           x_col = \"image_name\",\n                                                           y_col = \"target\",\n                                                           batch_size=batch_size,\n                                                           shuffle=True,\n                                                           target_size=(img_size, img_size),\n                                                           class_mode='raw')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.layers import Input, Activation, BatchNormalization\nfrom tensorflow.keras import Model\n\nX_input = Input((img_size,img_size, 3))\nX = Conv2D(16, (3, 3), padding = \"same\", name='conv1')(X_input)\nX = BatchNormalization()(X)\nX = Activation(\"relu\")(X)\nX = MaxPooling2D((2,2), padding=\"valid\")(X)\n\nX = Conv2D(32, (3, 3), padding = \"same\", name='conv2')(X)\nX = BatchNormalization()(X)\nX = Activation(\"relu\")(X)\nX = MaxPooling2D((2,2), padding=\"valid\")(X)\n\nX = Conv2D(80, (3, 3), padding = \"same\", name='conv3')(X)\nX = BatchNormalization()(X)\nX = Activation(\"relu\")(X)\nX = MaxPooling2D((2,2), padding=\"valid\")(X)\n\nX = Conv2D(256, (3, 3), padding = \"same\", name='conv4')(X)\nX = BatchNormalization()(X)\nX = Activation(\"relu\")(X)\nX = MaxPooling2D((2,2), padding=\"valid\")(X)\n\nX = Conv2D(512, (3, 3), padding = \"same\", name='conv5')(X)\nX = BatchNormalization()(X)\nX = Activation(\"relu\")(X)\nX = MaxPooling2D((2,2), padding=\"valid\")(X)\n\nX = Conv2D(1024, (3, 3), padding = \"same\", name='conv6')(X)\nX = BatchNormalization()(X)\nX = Activation(\"relu\")(X)\nX = MaxPooling2D((2,2), padding=\"valid\")(X)\n\nX = Flatten()(X)\n\nX = Dense(4096)(X)\nX = Activation(\"relu\")(X)\nX = Dense(2048)(X)\nX = Activation(\"relu\")(X)\nX_out = Dense(1, activation=\"sigmoid\")(X)\n\nmodel = Model(inputs=X_input, outputs=X_out, name='nenet')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"total_train = X_train.shape[0]\ntotal_val = X_val.shape[0]\nepochs = 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(\n    train_data_gen,\n    steps_per_epoch=total_train // batch_size,\n    epochs=epochs,\n    validation_data=val_data_gen,\n    validation_steps=total_val // batch_size\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test = pd.read_csv(datadir + \"test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test['image_name'] = df_test['image_name'].apply(lambda x: os.path.join(datadir, \"jpeg/\" + \"test/\" + x + \".jpg\"))\ndf_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_image_generator = ImageDataGenerator(rescale=1./255)\n\ntest_data_gen = validation_image_generator.flow_from_dataframe(df_test,\n                                                           x_col = \"image_name\",\n                                                           y_col = None,\n                                                           batch_size=batch_size,\n                                                           shuffle=False,\n                                                           target_size=(img_size, img_size),\n                                                           class_mode=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model.predict(test_data_gen, steps=df_test.shape[0]//batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"answer = np.array(predict)\nprint(answer.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(len(answer)):\n    answer[i]=answer[i][0]\nanswer = list(answer)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test1 = pd.read_csv(datadir + \"test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_answer = pd.DataFrame(answer, columns=['target'])\ndf_image = df_test1['image_name']\n\ndf_final = pd.concat([df_image, df_answer], axis = 1)\ndf_final.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.to_csv('submission.csv', header=True, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#model.save('nenet')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}