{"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 tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, Activation, Dropout, Flatten, Dense","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-31T12:33:48.401688Z","iopub.execute_input":"2021-10-31T12:33:48.403061Z","iopub.status.idle":"2021-10-31T12:33:55.385349Z","shell.execute_reply.started":"2021-10-31T12:33:48.402912Z","shell.execute_reply":"2021-10-31T12:33:55.384394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:55.386885Z","iopub.execute_input":"2021-10-31T12:33:55.38742Z","iopub.status.idle":"2021-10-31T12:33:55.392559Z","shell.execute_reply.started":"2021-10-31T12:33:55.387377Z","shell.execute_reply":"2021-10-31T12:33:55.391514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"../input/intel-mobileodt-cervical-cancer-screening/train/train\"\ntest_dir = \"../input/intel-mobileodt-cervical-cancer-screening/test\"\n\nimg_width, img_height = 150, 150\n\ninput_shape = (img_width, img_height,3)\n\nepochs = 20\n\nbatch_size = 2\n\nnb_train_samples = 1481\n\nnb_test_samples = 512","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:55.393774Z","iopub.execute_input":"2021-10-31T12:33:55.394047Z","iopub.status.idle":"2021-10-31T12:33:55.414935Z","shell.execute_reply.started":"2021-10-31T12:33:55.394003Z","shell.execute_reply":"2021-10-31T12:33:55.414196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(64, (3, 3), kernel_initializer='random_normal', input_shape = input_shape))\n#model.add(Lambda(lambda image: tf.image.resize(image,(img_width, img_height))))\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(32, (3, 3), kernel_initializer='random_normal'))\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3), kernel_initializer='random_normal'))\nmodel.add(Activation('relu'))\nmodel.add(MaxPool2D(pool_size=(2, 2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, kernel_initializer='random_normal'))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(3, kernel_initializer='random_normal'))\nmodel.add(Activation('softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:55.416244Z","iopub.execute_input":"2021-10-31T12:33:55.417192Z","iopub.status.idle":"2021-10-31T12:33:55.642737Z","shell.execute_reply.started":"2021-10-31T12:33:55.41714Z","shell.execute_reply":"2021-10-31T12:33:55.641706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:55.645161Z","iopub.execute_input":"2021-10-31T12:33:55.645463Z","iopub.status.idle":"2021-10-31T12:33:55.66141Z","shell.execute_reply.started":"2021-10-31T12:33:55.645429Z","shell.execute_reply":"2021-10-31T12:33:55.660605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:55.662907Z","iopub.execute_input":"2021-10-31T12:33:55.663121Z","iopub.status.idle":"2021-10-31T12:33:55.674726Z","shell.execute_reply.started":"2021-10-31T12:33:55.663093Z","shell.execute_reply":"2021-10-31T12:33:55.673461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1. / 255)","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:55.676288Z","iopub.execute_input":"2021-10-31T12:33:55.676542Z","iopub.status.idle":"2021-10-31T12:33:55.683895Z","shell.execute_reply.started":"2021-10-31T12:33:55.676513Z","shell.execute_reply":"2021-10-31T12:33:55.682663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_generator = datagen.flow_from_directory(\n    train_dir,\n    target_size = (img_width, img_height),\n    batch_size=batch_size,\n    class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:55.685641Z","iopub.execute_input":"2021-10-31T12:33:55.685952Z","iopub.status.idle":"2021-10-31T12:33:56.016152Z","shell.execute_reply.started":"2021-10-31T12:33:55.685911Z","shell.execute_reply":"2021-10-31T12:33:56.015179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = datagen.flow_from_directory(\n    test_dir,\n    target_size = (img_width, img_height),\n    batch_size=batch_size,\n    class_mode='categorical', classes = ['test'])","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:56.017165Z","iopub.execute_input":"2021-10-31T12:33:56.017536Z","iopub.status.idle":"2021-10-31T12:33:56.226826Z","shell.execute_reply.started":"2021-10-31T12:33:56.0175Z","shell.execute_reply":"2021-10-31T12:33:56.22568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"histoty = model.fit(\n    train_generator,\n    steps_per_epoch=nb_train_samples // batch_size,epochs=epochs)","metadata":{"execution":{"iopub.status.busy":"2021-10-31T12:33:56.228485Z","iopub.execute_input":"2021-10-31T12:33:56.228824Z","iopub.status.idle":"2021-10-31T14:40:26.132897Z","shell.execute_reply.started":"2021-10-31T12:33:56.228781Z","shell.execute_reply":"2021-10-31T14:40:26.127195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-10-31T14:40:26.143307Z","iopub.execute_input":"2021-10-31T14:40:26.145109Z","iopub.status.idle":"2021-10-31T14:43:20.955115Z","shell.execute_reply.started":"2021-10-31T14:40:26.144835Z","shell.execute_reply":"2021-10-31T14:43:20.954083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\nmodel.save('cervical_cancer_model.h5')\n\n","metadata":{"execution":{"iopub.status.busy":"2021-10-31T14:44:18.496219Z","iopub.execute_input":"2021-10-31T14:44:18.496531Z","iopub.status.idle":"2021-10-31T14:44:18.64725Z","shell.execute_reply.started":"2021-10-31T14:44:18.496499Z","shell.execute_reply":"2021-10-31T14:44:18.646005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}