Aditya1929 commited on
Commit
2a1b5b7
1 Parent(s): 3ea04d8

Upload test-1 (8).ipynb

Browse files
Files changed (1) hide show
  1. test-1 (8).ipynb +1 -0
test-1 (8).ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"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\nfor dirname, _, filenames in os.walk(''):\n for filename in filenames:\n print(os.path.join(dirname, filename))\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-09-09T11:19:38.928894Z","iopub.execute_input":"2022-09-09T11:19:38.929784Z","iopub.status.idle":"2022-09-09T11:19:38.936633Z","shell.execute_reply.started":"2022-09-09T11:19:38.929738Z","shell.execute_reply":"2022-09-09T11:19:38.935332Z"},"trusted":true},"execution_count":265,"outputs":[]},{"cell_type":"code","source":"import gradio","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:38.938775Z","iopub.execute_input":"2022-09-09T11:19:38.939195Z","iopub.status.idle":"2022-09-09T11:19:38.952253Z","shell.execute_reply.started":"2022-09-09T11:19:38.939158Z","shell.execute_reply":"2022-09-09T11:19:38.950836Z"},"trusted":true},"execution_count":266,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator\n\nimport cv2\n\ntrain = '../input/polyps-dataset/AI Project - Polyps 3'\nCATEGORIES = ['Benign', 'Malignant', 'Pre-malignant']\n\ndata = []\n\ndef create_training_data(data_dir):\n for category in CATEGORIES:\n path = os.path.join(data_dir, category)\n class_num = CATEGORIES.index(category) \n for img in os.listdir(path):\n try:\n img_array = cv2.imread(os.path.join(path, img))\n new_array = cv2.resize(img_array, (150, 150))\n data.append([new_array, class_num])\n except Exception as e:\n print(e)\n \ncreate_training_data(train)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:38.954958Z","iopub.execute_input":"2022-09-09T11:19:38.956163Z","iopub.status.idle":"2022-09-09T11:19:43.043366Z","shell.execute_reply.started":"2022-09-09T11:19:38.956104Z","shell.execute_reply":"2022-09-09T11:19:43.042108Z"},"trusted":true},"execution_count":267,"outputs":[]},{"cell_type":"code","source":"X_train = []\ny_train = []","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.044761Z","iopub.execute_input":"2022-09-09T11:19:43.045141Z","iopub.status.idle":"2022-09-09T11:19:43.051343Z","shell.execute_reply.started":"2022-09-09T11:19:43.045099Z","shell.execute_reply":"2022-09-09T11:19:43.050156Z"},"trusted":true},"execution_count":268,"outputs":[]},{"cell_type":"code","source":"for features, label in data:\n X_train.append(features)\n y_train.append(label)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.054798Z","iopub.execute_input":"2022-09-09T11:19:43.055332Z","iopub.status.idle":"2022-09-09T11:19:43.064122Z","shell.execute_reply.started":"2022-09-09T11:19:43.055283Z","shell.execute_reply":"2022-09-09T11:19:43.063094Z"},"trusted":true},"execution_count":269,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train).reshape(-1, 150, 150, 3)\n\nX_train = X_train/255.0","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.065911Z","iopub.execute_input":"2022-09-09T11:19:43.066660Z","iopub.status.idle":"2022-09-09T11:19:43.171978Z","shell.execute_reply.started":"2022-09-09T11:19:43.066612Z","shell.execute_reply":"2022-09-09T11:19:43.170454Z"},"trusted":true},"execution_count":270,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n featurewise_center=False,\n samplewise_center=False,\n featurewise_std_normalization=False,\n samplewise_std_normalization=False,\n zca_whitening=False,\n rotation_range = 30,\n zoom_range = 0.1, \n width_shift_range = 0.1,\n height_shift_range = 0.1,\n horizontal_flip = True,\n vertical_flip = False)\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.173432Z","iopub.execute_input":"2022-09-09T11:19:43.173809Z","iopub.status.idle":"2022-09-09T11:19:43.257976Z","shell.execute_reply.started":"2022-09-09T11:19:43.173775Z","shell.execute_reply":"2022-09-09T11:19:43.256664Z"},"trusted":true},"execution_count":271,"outputs":[]},{"cell_type":"markdown","source":"**Creating and compiling the model**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPooling2D, BatchNormalization, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.259861Z","iopub.execute_input":"2022-09-09T11:19:43.260467Z","iopub.status.idle":"2022-09-09T11:19:43.265259Z","shell.execute_reply.started":"2022-09-09T11:19:43.260428Z","shell.execute_reply":"2022-09-09T11:19:43.264100Z"},"trusted":true},"execution_count":272,"outputs":[]},{"cell_type":"code","source":"cnn = Sequential()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.267335Z","iopub.execute_input":"2022-09-09T11:19:43.268121Z","iopub.status.idle":"2022-09-09T11:19:43.281546Z","shell.execute_reply.started":"2022-09-09T11:19:43.268052Z","shell.execute_reply":"2022-09-09T11:19:43.280454Z"},"trusted":true},"execution_count":273,"outputs":[]},{"cell_type":"code","source":"cnn.add(Conv2D(32, kernel_size=(3,3), input_shape = (150, 150, 3), padding='same', activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.283035Z","iopub.execute_input":"2022-09-09T11:19:43.283700Z","iopub.status.idle":"2022-09-09T11:19:43.305193Z","shell.execute_reply.started":"2022-09-09T11:19:43.283662Z","shell.execute_reply":"2022-09-09T11:19:43.304135Z"},"trusted":true},"execution_count":274,"outputs":[]},{"cell_type":"code","source":"cnn.add(BatchNormalization())","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.309278Z","iopub.execute_input":"2022-09-09T11:19:43.309680Z","iopub.status.idle":"2022-09-09T11:19:43.327538Z","shell.execute_reply.started":"2022-09-09T11:19:43.309645Z","shell.execute_reply":"2022-09-09T11:19:43.326303Z"},"trusted":true},"execution_count":275,"outputs":[]},{"cell_type":"code","source":"cnn.add(MaxPooling2D())","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.329165Z","iopub.execute_input":"2022-09-09T11:19:43.329534Z","iopub.status.idle":"2022-09-09T11:19:43.340325Z","shell.execute_reply.started":"2022-09-09T11:19:43.329501Z","shell.execute_reply":"2022-09-09T11:19:43.339181Z"},"trusted":true},"execution_count":276,"outputs":[]},{"cell_type":"code","source":"cnn.add(Dropout(0.5))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.341888Z","iopub.execute_input":"2022-09-09T11:19:43.342430Z","iopub.status.idle":"2022-09-09T11:19:43.354530Z","shell.execute_reply.started":"2022-09-09T11:19:43.342392Z","shell.execute_reply":"2022-09-09T11:19:43.353478Z"},"trusted":true},"execution_count":277,"outputs":[]},{"cell_type":"code","source":"cnn.add(Conv2D(32, kernel_size=(3,3), padding='same', activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.356146Z","iopub.execute_input":"2022-09-09T11:19:43.356698Z","iopub.status.idle":"2022-09-09T11:19:43.378392Z","shell.execute_reply.started":"2022-09-09T11:19:43.356632Z","shell.execute_reply":"2022-09-09T11:19:43.376999Z"},"trusted":true},"execution_count":278,"outputs":[]},{"cell_type":"code","source":"cnn.add(BatchNormalization())","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.379695Z","iopub.execute_input":"2022-09-09T11:19:43.380379Z","iopub.status.idle":"2022-09-09T11:19:43.398574Z","shell.execute_reply.started":"2022-09-09T11:19:43.380340Z","shell.execute_reply":"2022-09-09T11:19:43.396892Z"},"trusted":true},"execution_count":279,"outputs":[]},{"cell_type":"code","source":"cnn.add(MaxPooling2D())","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.400056Z","iopub.execute_input":"2022-09-09T11:19:43.400772Z","iopub.status.idle":"2022-09-09T11:19:43.409634Z","shell.execute_reply.started":"2022-09-09T11:19:43.400736Z","shell.execute_reply":"2022-09-09T11:19:43.408696Z"},"trusted":true},"execution_count":280,"outputs":[]},{"cell_type":"code","source":"cnn.add(Dropout(0.5))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.411710Z","iopub.execute_input":"2022-09-09T11:19:43.412316Z","iopub.status.idle":"2022-09-09T11:19:43.425833Z","shell.execute_reply.started":"2022-09-09T11:19:43.412278Z","shell.execute_reply":"2022-09-09T11:19:43.424556Z"},"trusted":true},"execution_count":281,"outputs":[]},{"cell_type":"code","source":"cnn.add(Conv2D(64, kernel_size=(3,3), padding='same', activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.427370Z","iopub.execute_input":"2022-09-09T11:19:43.428261Z","iopub.status.idle":"2022-09-09T11:19:43.448451Z","shell.execute_reply.started":"2022-09-09T11:19:43.428220Z","shell.execute_reply":"2022-09-09T11:19:43.447342Z"},"trusted":true},"execution_count":282,"outputs":[]},{"cell_type":"code","source":"cnn.add(BatchNormalization())","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.450234Z","iopub.execute_input":"2022-09-09T11:19:43.450935Z","iopub.status.idle":"2022-09-09T11:19:43.473777Z","shell.execute_reply.started":"2022-09-09T11:19:43.450896Z","shell.execute_reply":"2022-09-09T11:19:43.472552Z"},"trusted":true},"execution_count":283,"outputs":[]},{"cell_type":"code","source":"cnn.add(MaxPooling2D())","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.475312Z","iopub.execute_input":"2022-09-09T11:19:43.476442Z","iopub.status.idle":"2022-09-09T11:19:43.485890Z","shell.execute_reply.started":"2022-09-09T11:19:43.476405Z","shell.execute_reply":"2022-09-09T11:19:43.484580Z"},"trusted":true},"execution_count":284,"outputs":[]},{"cell_type":"code","source":"cnn.add(Dropout(0.5))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.487606Z","iopub.execute_input":"2022-09-09T11:19:43.487951Z","iopub.status.idle":"2022-09-09T11:19:43.499533Z","shell.execute_reply.started":"2022-09-09T11:19:43.487917Z","shell.execute_reply":"2022-09-09T11:19:43.498441Z"},"trusted":true},"execution_count":285,"outputs":[]},{"cell_type":"code","source":"cnn.add(Dropout(0.5))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.500695Z","iopub.execute_input":"2022-09-09T11:19:43.501642Z","iopub.status.idle":"2022-09-09T11:19:43.514623Z","shell.execute_reply.started":"2022-09-09T11:19:43.501602Z","shell.execute_reply":"2022-09-09T11:19:43.513286Z"},"trusted":true},"execution_count":286,"outputs":[]},{"cell_type":"code","source":"cnn.add(Flatten())","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.516484Z","iopub.execute_input":"2022-09-09T11:19:43.517912Z","iopub.status.idle":"2022-09-09T11:19:43.533196Z","shell.execute_reply.started":"2022-09-09T11:19:43.517857Z","shell.execute_reply":"2022-09-09T11:19:43.532012Z"},"trusted":true},"execution_count":287,"outputs":[]},{"cell_type":"code","source":"cnn.add(Dense(128, activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.534999Z","iopub.execute_input":"2022-09-09T11:19:43.536161Z","iopub.status.idle":"2022-09-09T11:19:43.563316Z","shell.execute_reply.started":"2022-09-09T11:19:43.536113Z","shell.execute_reply":"2022-09-09T11:19:43.562226Z"},"trusted":true},"execution_count":288,"outputs":[]},{"cell_type":"code","source":"cnn.add(Dense(3, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.565973Z","iopub.execute_input":"2022-09-09T11:19:43.566483Z","iopub.status.idle":"2022-09-09T11:19:43.582657Z","shell.execute_reply.started":"2022-09-09T11:19:43.566436Z","shell.execute_reply":"2022-09-09T11:19:43.581695Z"},"trusted":true},"execution_count":289,"outputs":[]},{"cell_type":"code","source":"cnn.compile(optimizer = 'adam', loss='sparse_categorical_crossentropy', metrics=[('accuracy')])","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.584474Z","iopub.execute_input":"2022-09-09T11:19:43.585123Z","iopub.status.idle":"2022-09-09T11:19:43.595399Z","shell.execute_reply.started":"2022-09-09T11:19:43.585057Z","shell.execute_reply":"2022-09-09T11:19:43.594438Z"},"trusted":true},"execution_count":290,"outputs":[]},{"cell_type":"code","source":"cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.597013Z","iopub.execute_input":"2022-09-09T11:19:43.597789Z","iopub.status.idle":"2022-09-09T11:19:43.608036Z","shell.execute_reply.started":"2022-09-09T11:19:43.597750Z","shell.execute_reply":"2022-09-09T11:19:43.606952Z"},"trusted":true},"execution_count":291,"outputs":[]},{"cell_type":"code","source":"learning = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', patience = 2, verbose = 1, factor = 0.3, min_lr = 0.000001)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.609203Z","iopub.execute_input":"2022-09-09T11:19:43.609662Z","iopub.status.idle":"2022-09-09T11:19:43.623349Z","shell.execute_reply.started":"2022-09-09T11:19:43.609626Z","shell.execute_reply":"2022-09-09T11:19:43.622203Z"},"trusted":true},"execution_count":292,"outputs":[]},{"cell_type":"code","source":"cnn.fit(datagen.flow(X_train, y_train, batch_size = 32), epochs = 5, callbacks = learning)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:19:43.628454Z","iopub.execute_input":"2022-09-09T11:19:43.629617Z","iopub.status.idle":"2022-09-09T11:20:10.090437Z","shell.execute_reply.started":"2022-09-09T11:19:43.629560Z","shell.execute_reply":"2022-09-09T11:20:10.089480Z"},"trusted":true},"execution_count":293,"outputs":[]},{"cell_type":"code","source":"def predict(img): \n img = np.array(img)/ 255\n img = img.reshape(-1, 150, 150, 3)\n benign = cnn.predict([0])\n mal = cnn.predict([1])\n mal2 = cnn.predict([2])\n \n if benign>mal and benign>mal2:\n imgClass = 'Benign'\n if mal>benign and mal>mal2:\n imgClass = 'Malignant'\n if mal2>benign and mal2>mal:\n imgClass = 'Pre-malignant' \n \n \n return imgClass","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:20:10.092374Z","iopub.execute_input":"2022-09-09T11:20:10.093160Z","iopub.status.idle":"2022-09-09T11:20:10.101469Z","shell.execute_reply.started":"2022-09-09T11:20:10.093111Z","shell.execute_reply":"2022-09-09T11:20:10.100349Z"},"trusted":true},"execution_count":294,"outputs":[]},{"cell_type":"code","source":"img = gradio.inputs.Image(shape=(150, 150))\nlabel = gradio.outputs.Label(num_top_classes=3)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:20:10.103294Z","iopub.execute_input":"2022-09-09T11:20:10.104040Z","iopub.status.idle":"2022-09-09T11:20:10.117647Z","shell.execute_reply.started":"2022-09-09T11:20:10.103994Z","shell.execute_reply":"2022-09-09T11:20:10.116798Z"},"trusted":true},"execution_count":295,"outputs":[]},{"cell_type":"code","source":"interface = gradio.Interface(fn = predict, title = 'Polyp Segmentation', inputs = img, outputs = label, interpretation = \"default\")\ninterface.launch(debug = True, share = True)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T11:20:10.119014Z","iopub.execute_input":"2022-09-09T11:20:10.119633Z"},"trusted":true},"execution_count":null,"outputs":[]}]}