{"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 pandas as pd\nimport keras\nfrom keras.applications import densenet\nfrom keras.preprocessing import image\n\nfrom keras.applications.densenet import preprocess_input, decode_predictions\nfrom keras.models import Model,Sequential\nimport numpy as np\nfrom keras.layers import Input,Dense,BatchNormalization,Dropout,Activation, GlobalAveragePooling2D,Conv2D,MaxPooling2D,Flatten\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import regularizers, optimizers\nfrom keras.optimizers import Adam\nimport os\nfrom keras.callbacks import ModelCheckpoint\nimport json\nfrom keras import regularizers\nfrom sklearn.model_selection import train_test_split\nfrom matplotlib import pyplot\nimport matplotlib.pyplot as plt\nimport datetime","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-20T20:24:38.284538Z","iopub.execute_input":"2021-06-20T20:24:38.284856Z","iopub.status.idle":"2021-06-20T20:24:43.804804Z","shell.execute_reply.started":"2021-06-20T20:24:38.284828Z","shell.execute_reply":"2021-06-20T20:24:43.804026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dirImages = '../input/histopathologic-cancer-detection/train'","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:24:56.114511Z","iopub.execute_input":"2021-06-20T20:24:56.114827Z","iopub.status.idle":"2021-06-20T20:24:56.118682Z","shell.execute_reply.started":"2021-06-20T20:24:56.114797Z","shell.execute_reply":"2021-06-20T20:24:56.117663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/histopathologic-cancer-detection/train_labels.csv\", dtype=str)\ndf","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:24:58.80008Z","iopub.execute_input":"2021-06-20T20:24:58.800548Z","iopub.status.idle":"2021-06-20T20:24:59.422812Z","shell.execute_reply.started":"2021-06-20T20:24:58.800503Z","shell.execute_reply":"2021-06-20T20:24:59.421957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".tif\"\n\ndf[\"id\"]=df[\"id\"].apply(append_ext)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:03.421197Z","iopub.execute_input":"2021-06-20T20:25:03.421521Z","iopub.status.idle":"2021-06-20T20:25:03.497632Z","shell.execute_reply.started":"2021-06-20T20:25:03.421491Z","shell.execute_reply":"2021-06-20T20:25:03.496791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:07.740632Z","iopub.execute_input":"2021-06-20T20:25:07.740957Z","iopub.status.idle":"2021-06-20T20:25:07.753622Z","shell.execute_reply.started":"2021-06-20T20:25:07.740928Z","shell.execute_reply":"2021-06-20T20:25:07.752826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df[['id']]\ny = df[['label']]","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:12.550111Z","iopub.execute_input":"2021-06-20T20:25:12.550439Z","iopub.status.idle":"2021-06-20T20:25:12.560234Z","shell.execute_reply.started":"2021-06-20T20:25:12.55041Z","shell.execute_reply":"2021-06-20T20:25:12.559319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_training_validation,x_testing,y_training_validation,y_testing = train_test_split(X,y,test_size=22003,stratify=y)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:17.95508Z","iopub.execute_input":"2021-06-20T20:25:17.955409Z","iopub.status.idle":"2021-06-20T20:25:19.299524Z","shell.execute_reply.started":"2021-06-20T20:25:17.955379Z","shell.execute_reply":"2021-06-20T20:25:19.298673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_training,x_validation,y_training,y_validation = train_test_split(x_training_validation,y_training_validation,test_size=22003,stratify=y_training_validation)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:21.812426Z","iopub.execute_input":"2021-06-20T20:25:21.812738Z","iopub.status.idle":"2021-06-20T20:25:22.988353Z","shell.execute_reply.started":"2021-06-20T20:25:21.812709Z","shell.execute_reply":"2021-06-20T20:25:22.987502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x_training_all,x_training,y_training_all,y_training = train_test_split(x_training,y_training,test_size=80000,stratify=y_training)","metadata":{"execution":{"iopub.status.busy":"2021-06-19T00:49:29.798997Z","iopub.execute_input":"2021-06-19T00:49:29.799312Z","iopub.status.idle":"2021-06-19T00:49:31.080951Z","shell.execute_reply.started":"2021-06-19T00:49:29.799281Z","shell.execute_reply":"2021-06-19T00:49:31.0801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_training.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-20T15:48:55.365734Z","iopub.execute_input":"2021-06-20T15:48:55.366047Z","iopub.status.idle":"2021-06-20T15:48:55.37093Z","shell.execute_reply.started":"2021-06-20T15:48:55.366019Z","shell.execute_reply":"2021-06-20T15:48:55.370132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_validation.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-20T15:48:58.563547Z","iopub.execute_input":"2021-06-20T15:48:58.563873Z","iopub.status.idle":"2021-06-20T15:48:58.572058Z","shell.execute_reply.started":"2021-06-20T15:48:58.563844Z","shell.execute_reply":"2021-06-20T15:48:58.57122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_testing.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-20T15:49:00.87726Z","iopub.execute_input":"2021-06-20T15:49:00.877598Z","iopub.status.idle":"2021-06-20T15:49:00.885122Z","shell.execute_reply.started":"2021-06-20T15:49:00.877567Z","shell.execute_reply":"2021-06-20T15:49:00.884201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_training = pd.concat([x_training,y_training], axis=1)\ndf_training","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:29.458144Z","iopub.execute_input":"2021-06-20T20:25:29.458472Z","iopub.status.idle":"2021-06-20T20:25:29.47864Z","shell.execute_reply.started":"2021-06-20T20:25:29.458444Z","shell.execute_reply":"2021-06-20T20:25:29.477865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_validation = pd.concat([x_validation,y_validation], axis=1)\ndf_validation","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:34.421858Z","iopub.execute_input":"2021-06-20T20:25:34.422197Z","iopub.status.idle":"2021-06-20T20:25:34.438637Z","shell.execute_reply.started":"2021-06-20T20:25:34.422167Z","shell.execute_reply":"2021-06-20T20:25:34.437855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_testing = pd.concat([x_testing,y_testing], axis=1)\ndf_testing","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:38.581091Z","iopub.execute_input":"2021-06-20T20:25:38.581411Z","iopub.status.idle":"2021-06-20T20:25:38.596081Z","shell.execute_reply.started":"2021-06-20T20:25:38.581383Z","shell.execute_reply":"2021-06-20T20:25:38.595206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen =  ImageDataGenerator()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:42.851652Z","iopub.execute_input":"2021-06-20T20:25:42.852095Z","iopub.status.idle":"2021-06-20T20:25:42.85885Z","shell.execute_reply.started":"2021-06-20T20:25:42.852053Z","shell.execute_reply":"2021-06-20T20:25:42.858032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\ntrain_generator=datagen.flow_from_dataframe(\ndataframe=df_training,\ndirectory=dirImages,\nx_col=\"id\",\ny_col=\"label\",\nsubset=\"training\",\nbatch_size=32,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=(96,96))\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:25:47.451929Z","iopub.execute_input":"2021-06-20T20:25:47.452261Z","iopub.status.idle":"2021-06-20T20:29:51.846607Z","shell.execute_reply.started":"2021-06-20T20:25:47.452231Z","shell.execute_reply":"2021-06-20T20:29:51.845741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# generate samples and plot\nfor i in range(9):\n\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# generate batch of images\n\tbatch = train_generator.next()\n\t# convert to unsigned integers for viewing\n\timage = batch[0].astype('uint8')\n\t# plot raw pixel data\n\tpyplot.imshow(image[0])\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:37:26.162787Z","iopub.execute_input":"2021-06-20T20:37:26.163146Z","iopub.status.idle":"2021-06-20T20:37:29.111834Z","shell.execute_reply.started":"2021-06-20T20:37:26.163113Z","shell.execute_reply":"2021-06-20T20:37:29.108084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:47:40.584572Z","iopub.execute_input":"2021-06-20T08:47:40.584926Z","iopub.status.idle":"2021-06-20T08:47:40.589023Z","shell.execute_reply.started":"2021-06-20T08:47:40.584889Z","shell.execute_reply":"2021-06-20T08:47:40.588163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\nvalid_generator=datagen.flow_from_dataframe(\ndataframe=df_validation,\ndirectory=dirImages,\nx_col=\"id\",\ny_col=\"label\",\nsubset=\"training\",\nbatch_size=32,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=(96,96))\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:37:36.06687Z","iopub.execute_input":"2021-06-20T20:37:36.067213Z","iopub.status.idle":"2021-06-20T20:38:09.407495Z","shell.execute_reply.started":"2021-06-20T20:37:36.067182Z","shell.execute_reply":"2021-06-20T20:38:09.406616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\ntest_generator=datagen.flow_from_dataframe(\ndataframe=df_testing,\ndirectory=dirImages,\nx_col=\"id\",\ny_col=\"label\",\nsubset=\"training\",\nbatch_size=6,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=(96,96))\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:43:51.430255Z","iopub.execute_input":"2021-06-20T20:43:51.430581Z","iopub.status.idle":"2021-06-20T20:44:25.743698Z","shell.execute_reply.started":"2021-06-20T20:43:51.430551Z","shell.execute_reply":"2021-06-20T20:44:25.742919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rawCNNModel(input_shape):\n    model = Sequential()\n    model.add(Conv2D(32, (3, 3), padding='same',\n                     input_shape=input_shape))\n    model.add(Activation('relu'))\n    model.add(Conv2D(32, (3, 3)))\n    model.add(Activation('relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n    model.add(Conv2D(64, (3, 3), padding='same'))\n    model.add(Activation('relu'))\n    model.add(Conv2D(64, (3, 3)))\n    model.add(Activation('relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.25))\n    model.add(Flatten())\n    model.add(Dense(512))\n    model.add(Activation('relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(2, activation='softmax'))\n    #model.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),loss=\"categorical_crossentropy\",metrics=[\"accuracy\"])\n    model.compile(loss=keras.losses.binary_crossentropy,\n              optimizer=keras.optimizers.Adam(0.001), \n              metrics=['accuracy'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-06-20T11:04:38.92075Z","iopub.execute_input":"2021-06-20T11:04:38.921081Z","iopub.status.idle":"2021-06-20T11:04:38.93232Z","shell.execute_reply.started":"2021-06-20T11:04:38.921049Z","shell.execute_reply":"2021-06-20T11:04:38.931524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rawModel1 = rawCNNModel(train_generator[0][0].shape[1:])","metadata":{"execution":{"iopub.status.busy":"2021-06-20T11:04:49.221385Z","iopub.execute_input":"2021-06-20T11:04:49.221746Z","iopub.status.idle":"2021-06-20T11:04:51.432055Z","shell.execute_reply.started":"2021-06-20T11:04:49.221709Z","shell.execute_reply":"2021-06-20T11:04:51.431107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rawModel1.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T11:04:54.762166Z","iopub.execute_input":"2021-06-20T11:04:54.762503Z","iopub.status.idle":"2021-06-20T11:04:54.77801Z","shell.execute_reply.started":"2021-06-20T11:04:54.762466Z","shell.execute_reply":"2021-06-20T11:04:54.777233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\nhistory = rawModel1.fit(train_generator,epochs=50,validation_data=valid_generator)\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T11:05:00.386315Z","iopub.execute_input":"2021-06-20T11:05:00.386702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import DenseNet121\nmodel_d=DenseNet121(weights='imagenet',include_top=False, input_shape=train_generator[0][0].shape[1:]) \nx=model_d.output\n\nx= GlobalAveragePooling2D()(x)\nx= BatchNormalization()(x)\nx= Dropout(0.5)(x)\nx= Dense(1024,activation='relu')(x) \nx= Dense(512,activation='relu')(x) \nx= BatchNormalization()(x)\nx= Dropout(0.5)(x)\n\npreds=Dense(2,activation='softmax')(x) #FC-layer\nmodel=Model(inputs=model_d.input,outputs=preds)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:45:37.691397Z","iopub.execute_input":"2021-06-20T20:45:37.691726Z","iopub.status.idle":"2021-06-20T20:45:42.906943Z","shell.execute_reply.started":"2021-06-20T20:45:37.691696Z","shell.execute_reply":"2021-06-20T20:45:42.906126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam' , loss = 'categorical_crossentropy' , metrics=['accuracy'])\nprint(datetime.datetime.now())\nhistory = model.fit(train_generator,epochs=50,validation_data=valid_generator)\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T20:46:23.734794Z","iopub.execute_input":"2021-06-20T20:46:23.735134Z","iopub.status.idle":"2021-06-21T03:24:33.619521Z","shell.execute_reply.started":"2021-06-20T20:46:23.735103Z","shell.execute_reply":"2021-06-21T03:24:33.618409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-21T03:43:34.654783Z","iopub.execute_input":"2021-06-21T03:43:34.655129Z","iopub.status.idle":"2021-06-21T03:43:34.796436Z","shell.execute_reply.started":"2021-06-21T03:43:34.655099Z","shell.execute_reply":"2021-06-21T03:43:34.795496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-21T03:46:31.451468Z","iopub.execute_input":"2021-06-21T03:46:31.451796Z","iopub.status.idle":"2021-06-21T03:46:31.582387Z","shell.execute_reply.started":"2021-06-21T03:46:31.451766Z","shell.execute_reply":"2021-06-21T03:46:31.581425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate_generator(test_generator)\nprint('Accuracy on Validation Set',score[1])","metadata":{"execution":{"iopub.status.busy":"2021-06-21T03:27:37.62391Z","iopub.execute_input":"2021-06-21T03:27:37.624288Z","iopub.status.idle":"2021-06-21T03:29:51.880479Z","shell.execute_reply.started":"2021-06-21T03:27:37.624255Z","shell.execute_reply":"2021-06-21T03:29:51.87962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"score = rawModel1.evaluate_generator(test_generator)\nprint('Accuracy on Validation Set',score[1])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def AlexNet(input_shape):\n    \n    X_input = Input(input_shape)\n    \n    X = Conv2D(96,(11,11),strides = 4,name=\"conv0\")(X_input)\n    X = BatchNormalization(axis = 3 , name = \"bn0\")(X)\n    X = Activation('relu')(X)\n    \n    X = MaxPooling2D((3,3),strides = 2,name = 'max0')(X)\n    \n    X = Conv2D(256,(5,5),padding = 'same' , name = 'conv1')(X)\n    X = BatchNormalization(axis = 3 ,name='bn1')(X)\n    X = Activation('relu')(X)\n    \n    X = MaxPooling2D((3,3),strides = 2,name = 'max1')(X)\n    \n    X = Conv2D(384, (3,3) , padding = 'same' , name='conv2')(X)\n    X = BatchNormalization(axis = 3, name = 'bn2')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(384, (3,3) , padding = 'same' , name='conv3')(X)\n    X = BatchNormalization(axis = 3, name = 'bn3')(X)\n    X = Activation('relu')(X)\n    \n    X = Conv2D(256, (3,3) , padding = 'same' , name='conv4')(X)\n    X = BatchNormalization(axis = 3, name = 'bn4')(X)\n    X = Activation('relu')(X)\n    \n    X = MaxPooling2D((3,3),strides = 2,name = 'max2')(X)\n    \n    X = Flatten()(X)\n    \n    X = Dense(4096, activation = 'relu', name = \"fc0\")(X)\n    \n    X = Dense(4096, activation = 'relu', name = 'fc1')(X) \n    \n    X = Dense(2,activation='softmax',name = 'fc2')(X)\n    \n    model = Model(inputs = X_input, outputs = X, name='AlexNet')\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:50:55.364824Z","iopub.execute_input":"2021-06-20T08:50:55.36517Z","iopub.status.idle":"2021-06-20T08:50:55.376761Z","shell.execute_reply.started":"2021-06-20T08:50:55.365139Z","shell.execute_reply":"2021-06-20T08:50:55.375951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexModel = AlexNet(train_generator[0][0].shape[1:])","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:51:00.287725Z","iopub.execute_input":"2021-06-20T08:51:00.288072Z","iopub.status.idle":"2021-06-20T08:51:00.443045Z","shell.execute_reply.started":"2021-06-20T08:51:00.28804Z","shell.execute_reply":"2021-06-20T08:51:00.442192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexModel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:51:04.48799Z","iopub.execute_input":"2021-06-20T08:51:04.488334Z","iopub.status.idle":"2021-06-20T08:51:04.505476Z","shell.execute_reply.started":"2021-06-20T08:51:04.488304Z","shell.execute_reply":"2021-06-20T08:51:04.504701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexModel.compile(optimizer = 'adam' , loss = 'categorical_crossentropy' , metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:51:09.209066Z","iopub.execute_input":"2021-06-20T08:51:09.209382Z","iopub.status.idle":"2021-06-20T08:51:09.221291Z","shell.execute_reply.started":"2021-06-20T08:51:09.209351Z","shell.execute_reply":"2021-06-20T08:51:09.220489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\nhistory = alexModel.fit(train_generator,epochs=50,validation_data=valid_generator)\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-19T03:21:51.288739Z","iopub.execute_input":"2021-06-19T03:21:51.289047Z","iopub.status.idle":"2021-06-19T09:02:19.64822Z","shell.execute_reply.started":"2021-06-19T03:21:51.28902Z","shell.execute_reply":"2021-06-19T09:02:19.647385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = pd.read_csv(\"https://gitlab.com/koharudin.mail07/tugas-akhir-praktikum-pola/-/raw/main/file.csv\")\nhistory","metadata":{"execution":{"iopub.status.busy":"2021-06-20T04:47:54.124778Z","iopub.execute_input":"2021-06-20T04:47:54.125137Z","iopub.status.idle":"2021-06-20T04:47:54.449882Z","shell.execute_reply.started":"2021-06-20T04:47:54.12508Z","shell.execute_reply":"2021-06-20T04:47:54.449127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history['accuracy'])\nplt.plot(history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T04:47:58.071319Z","iopub.execute_input":"2021-06-20T04:47:58.071636Z","iopub.status.idle":"2021-06-20T04:47:58.229757Z","shell.execute_reply.started":"2021-06-20T04:47:58.071607Z","shell.execute_reply":"2021-06-20T04:47:58.228797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# summarize history for loss\nplt.plot(history['loss'])\nplt.plot(history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T05:20:52.996339Z","iopub.execute_input":"2021-06-20T05:20:52.996674Z","iopub.status.idle":"2021-06-20T05:20:53.299867Z","shell.execute_reply.started":"2021-06-20T05:20:52.996642Z","shell.execute_reply":"2021-06-20T05:20:53.298916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = alexModel.evaluate_generator(test_generator)\nprint('Accuracy on Validation Set',score[1])\n","metadata":{"execution":{"iopub.status.busy":"2021-06-19T09:07:06.194072Z","iopub.execute_input":"2021-06-19T09:07:06.19439Z","iopub.status.idle":"2021-06-19T09:08:48.803396Z","shell.execute_reply.started":"2021-06-19T09:07:06.194361Z","shell.execute_reply":"2021-06-19T09:08:48.802621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alexModel.save(\"alexnet_hispathological.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-06-19T09:10:51.947379Z","iopub.execute_input":"2021-06-19T09:10:51.947815Z","iopub.status.idle":"2021-06-19T09:10:52.528162Z","shell.execute_reply.started":"2021-06-19T09:10:51.947777Z","shell.execute_reply":"2021-06-19T09:10:52.527323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**SVM**","metadata":{}},{"cell_type":"code","source":"pretainedModel = keras.models.load_model('../input/tugaspola/alexnet_hispathological.h5')","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:51:40.749582Z","iopub.execute_input":"2021-06-20T08:51:40.749918Z","iopub.status.idle":"2021-06-20T08:51:45.030557Z","shell.execute_reply.started":"2021-06-20T08:51:40.749865Z","shell.execute_reply":"2021-06-20T08:51:45.029708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretainedModel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:51:46.728762Z","iopub.execute_input":"2021-06-20T08:51:46.729124Z","iopub.status.idle":"2021-06-20T08:51:46.747506Z","shell.execute_reply.started":"2021-06-20T08:51:46.729093Z","shell.execute_reply":"2021-06-20T08:51:46.746762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_feat = Model(inputs=pretainedModel.input,outputs=pretainedModel.get_layer('fc2').output)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:51:52.152322Z","iopub.execute_input":"2021-06-20T08:51:52.152653Z","iopub.status.idle":"2021-06-20T08:51:52.1621Z","shell.execute_reply.started":"2021-06-20T08:51:52.15262Z","shell.execute_reply":"2021-06-20T08:51:52.161134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\ndef saveObj (filename,obj):\n    with open(filename,'wb') as f:\n        pickle.dump(obj, f)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:51:54.83312Z","iopub.execute_input":"2021-06-20T08:51:54.833447Z","iopub.status.idle":"2021-06-20T08:51:54.839793Z","shell.execute_reply.started":"2021-06-20T08:51:54.833417Z","shell.execute_reply":"2021-06-20T08:51:54.8389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\nfeat_train = model_feat.predict(train_generator)\nsaveObj(\"./obj_feat_train.pickle\",feat_train)\nprint(feat_train.shape)\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T08:52:10.126353Z","iopub.execute_input":"2021-06-20T08:52:10.126678Z","iopub.status.idle":"2021-06-20T09:07:41.794239Z","shell.execute_reply.started":"2021-06-20T08:52:10.126646Z","shell.execute_reply":"2021-06-20T09:07:41.793294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_train","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:09:47.234507Z","iopub.execute_input":"2021-06-20T09:09:47.234854Z","iopub.status.idle":"2021-06-20T09:09:47.241786Z","shell.execute_reply.started":"2021-06-20T09:09:47.234818Z","shell.execute_reply":"2021-06-20T09:09:47.240852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\nfrom sklearn.svm import SVC\n\nsvm = SVC(kernel='rbf')\n\nsvm.fit(feat_train,y_training)\nsaveObj(\"./obj_svm_train.pickle\",svm)\nprint('fitting done !!!')\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:10:08.36046Z","iopub.execute_input":"2021-06-20T09:10:08.360799Z","iopub.status.idle":"2021-06-20T09:32:55.416266Z","shell.execute_reply.started":"2021-06-20T09:10:08.360767Z","shell.execute_reply":"2021-06-20T09:32:55.415362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm.score(feat_train,y_training)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:36:40.141083Z","iopub.execute_input":"2021-06-20T09:36:40.141414Z","iopub.status.idle":"2021-06-20T09:56:47.695627Z","shell.execute_reply.started":"2021-06-20T09:36:40.141383Z","shell.execute_reply":"2021-06-20T09:56:47.694776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\nfeat_test = model_feat.predict(test_generator)\nsaveObj(\"./obj_feat_test.pickle\",feat_train)\nprint(feat_test.shape)\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T09:58:05.952146Z","iopub.execute_input":"2021-06-20T09:58:05.952474Z","iopub.status.idle":"2021-06-20T10:00:21.067767Z","shell.execute_reply.started":"2021-06-20T09:58:05.952443Z","shell.execute_reply":"2021-06-20T10:00:21.066955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm.score(feat_test,y_testing)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T10:00:28.526689Z","iopub.execute_input":"2021-06-20T10:00:28.527085Z","iopub.status.idle":"2021-06-20T10:02:59.099453Z","shell.execute_reply.started":"2021-06-20T10:00:28.527051Z","shell.execute_reply":"2021-06-20T10:02:59.098474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(datetime.datetime.now())\nfeat_val = model_feat.predict(valid_generator)\nsaveObj(\"./obj_feat_val.pickle\",feat_train)\nprint(feat_val.shape)\nprint(datetime.datetime.now())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T10:03:04.515962Z","iopub.execute_input":"2021-06-20T10:03:04.516287Z","iopub.status.idle":"2021-06-20T10:05:17.411628Z","shell.execute_reply.started":"2021-06-20T10:03:04.516257Z","shell.execute_reply":"2021-06-20T10:05:17.410839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm.score(feat_val,y_validation)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T10:11:57.061317Z","iopub.execute_input":"2021-06-20T10:11:57.061631Z","iopub.status.idle":"2021-06-20T10:14:28.743608Z","shell.execute_reply.started":"2021-06-20T10:11:57.061604Z","shell.execute_reply":"2021-06-20T10:14:28.742631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nmodel_file = \"./model.pickle\"\ntrain_model_name=\"123\"\nwith open(model_file,'wb') as f:\n    pickle.dump(train_model_name, f)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T02:32:47.811573Z","iopub.execute_input":"2021-06-20T02:32:47.81191Z","iopub.status.idle":"2021-06-20T02:32:47.818308Z","shell.execute_reply.started":"2021-06-20T02:32:47.81188Z","shell.execute_reply":"2021-06-20T02:32:47.817095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nmodel_file = \"./model.pickle\"\nwith open(model_file, 'rb') as fp:\n     banana = pickle.load(fp)\nbanana","metadata":{"execution":{"iopub.status.busy":"2021-06-20T02:33:32.601655Z","iopub.execute_input":"2021-06-20T02:33:32.602051Z","iopub.status.idle":"2021-06-20T02:33:32.609903Z","shell.execute_reply.started":"2021-06-20T02:33:32.602018Z","shell.execute_reply":"2021-06-20T02:33:32.608423Z"},"trusted":true},"execution_count":null,"outputs":[]}]}