{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30840,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n* Veri setine ulaşmak için [tıklayınız.](https://www.kaggle.com/c/aptos2019-blindness-detection)","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:10:16.277006Z","iopub.execute_input":"2025-02-03T17:10:16.277305Z","iopub.status.idle":"2025-02-03T17:10:16.579455Z","shell.execute_reply.started":"2025-02-03T17:10:16.277257Z","shell.execute_reply":"2025-02-03T17:10:16.578805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = '/kaggle/input/aptos2019-blindness-detection/'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:10:18.100732Z","iopub.execute_input":"2025-02-03T17:10:18.101158Z","iopub.status.idle":"2025-02-03T17:10:18.104972Z","shell.execute_reply.started":"2025-02-03T17:10:18.101131Z","shell.execute_reply":"2025-02-03T17:10:18.104130Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Pandas'ın [.csv okuma modülünü](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html) kullanarak, sınıflandırma verilerini jupyter notebook'a aktarıyoruz.","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(path + 'train.csv', sep = ',')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:10:20.482256Z","iopub.execute_input":"2025-02-03T17:10:20.482702Z","iopub.status.idle":"2025-02-03T17:10:20.507640Z","shell.execute_reply.started":"2025-02-03T17:10:20.482671Z","shell.execute_reply":"2025-02-03T17:10:20.506533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:10:21.553954Z","iopub.execute_input":"2025-02-03T17:10:21.554387Z","iopub.status.idle":"2025-02-03T17:10:21.575951Z","shell.execute_reply.started":"2025-02-03T17:10:21.554340Z","shell.execute_reply":"2025-02-03T17:10:21.575078Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['diagnosis'].hist()\ndf['diagnosis'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T14:08:14.714876Z","iopub.execute_input":"2025-02-03T14:08:14.715226Z","iopub.status.idle":"2025-02-03T14:08:15.096262Z","shell.execute_reply.started":"2025-02-03T14:08:14.715195Z","shell.execute_reply":"2025-02-03T14:08:15.095255Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Retinopati görüntülerini aktarmak için, **os.listdir()** kullanarak *''train_img''* klasöründe bulunan dosyaların isimlerini *''files''* ismine atadım.\n* '000c1434d8d7.png'\n* '001639a390f0.png'\n* '0024cdab0c1e.png'\n*  ...","metadata":{}},{"cell_type":"code","source":"import os\nfiles = os.listdir(path + 'train_images')\n# files","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:10:25.594756Z","iopub.execute_input":"2025-02-03T17:10:25.595066Z","iopub.status.idle":"2025-02-03T17:10:25.767165Z","shell.execute_reply.started":"2025-02-03T17:10:25.595039Z","shell.execute_reply":"2025-02-03T17:10:25.766539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(files)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:10:27.675028Z","iopub.execute_input":"2025-02-03T17:10:27.675384Z","iopub.status.idle":"2025-02-03T17:10:27.681214Z","shell.execute_reply.started":"2025-02-03T17:10:27.675350Z","shell.execute_reply":"2025-02-03T17:10:27.680008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:10:28.529949Z","iopub.execute_input":"2025-02-03T17:10:28.530302Z","iopub.status.idle":"2025-02-03T17:10:28.830964Z","shell.execute_reply.started":"2025-02-03T17:10:28.530247Z","shell.execute_reply":"2025-02-03T17:10:28.830164Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"OpenCV'nin [görüntü okuma modülü](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html)nü kullanarak **files**'ın içindeki görüntü isimleri üzerinden bir for döngüsü döndürerek görüntüleri okuduk. Ardından bunları (400,400,3) boyutunda yeniden şekillendirdik. OpenCV görüntüleri BGR şeklinde okuduğu için bunları RGB renk koduna çevirdik. Ardından döngüde dönen her görüntüyü *img_list* ismindeki listeye ekledik.","metadata":{}},{"cell_type":"code","source":"img_list = []\n\nfor i in files[0:20]:\n    image = cv2.imread(path + 'train_images/'+i)\n    image = cv2.resize(image,(400,400))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    img_list.append(image)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:17.225264Z","iopub.execute_input":"2025-02-03T17:11:17.225574Z","iopub.status.idle":"2025-02-03T17:11:18.963261Z","shell.execute_reply.started":"2025-02-03T17:11:17.225551Z","shell.execute_reply":"2025-02-03T17:11:18.962358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(img_list[2])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:20.514033Z","iopub.execute_input":"2025-02-03T17:11:20.514394Z","iopub.status.idle":"2025-02-03T17:11:20.893142Z","shell.execute_reply.started":"2025-02-03T17:11:20.514354Z","shell.execute_reply":"2025-02-03T17:11:20.892250Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(img_list[4])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:21.354332Z","iopub.execute_input":"2025-02-03T17:11:21.354636Z","iopub.status.idle":"2025-02-03T17:11:21.626449Z","shell.execute_reply.started":"2025-02-03T17:11:21.354601Z","shell.execute_reply":"2025-02-03T17:11:21.625605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kopya = img_list[4].copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:25.386381Z","iopub.execute_input":"2025-02-03T17:11:25.386728Z","iopub.status.idle":"2025-02-03T17:11:25.390733Z","shell.execute_reply.started":"2025-02-03T17:11:25.386698Z","shell.execute_reply":"2025-02-03T17:11:25.389850Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Threshold uygulayabilmek için görüntümüzü siyah-beyaz renklere çevirdik.","metadata":{}},{"cell_type":"code","source":"kopya = cv2.cvtColor(kopya, cv2.COLOR_RGB2GRAY)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:27.870876Z","iopub.execute_input":"2025-02-03T17:11:27.871204Z","iopub.status.idle":"2025-02-03T17:11:27.875999Z","shell.execute_reply.started":"2025-02-03T17:11:27.871172Z","shell.execute_reply":"2025-02-03T17:11:27.875157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(kopya, cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:28.352490Z","iopub.execute_input":"2025-02-03T17:11:28.352793Z","iopub.status.idle":"2025-02-03T17:11:28.587086Z","shell.execute_reply.started":"2025-02-03T17:11:28.352759Z","shell.execute_reply":"2025-02-03T17:11:28.586319Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Görüntü siyah beyaz olduğu için RGB kanallarını kaybederek (400,400) boyutuna indirgendi.","metadata":{}},{"cell_type":"code","source":"kopya.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:31.468447Z","iopub.execute_input":"2025-02-03T17:11:31.468743Z","iopub.status.idle":"2025-02-03T17:11:31.473999Z","shell.execute_reply.started":"2025-02-03T17:11:31.468720Z","shell.execute_reply":"2025-02-03T17:11:31.473177Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Threshold'un daha başarılı uygulanabilmesi için bir miktar Blur uyguluyoruz.","metadata":{}},{"cell_type":"code","source":"blur = cv2.GaussianBlur(kopya,(5,5),0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:32.260301Z","iopub.execute_input":"2025-02-03T17:11:32.260589Z","iopub.status.idle":"2025-02-03T17:11:32.270124Z","shell.execute_reply.started":"2025-02-03T17:11:32.260567Z","shell.execute_reply":"2025-02-03T17:11:32.269362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(blur,cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:32.826521Z","iopub.execute_input":"2025-02-03T17:11:32.826797Z","iopub.status.idle":"2025-02-03T17:11:33.039600Z","shell.execute_reply.started":"2025-02-03T17:11:32.826775Z","shell.execute_reply":"2025-02-03T17:11:33.038786Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Burada görüntü üzerinde  **10 değerinin üzerinde bulduğu tüm değerleri 255 değerine eşitledik**","metadata":{}},{"cell_type":"code","source":"thresh = cv2.threshold(blur,10,255, cv2.THRESH_BINARY)[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:35.878988Z","iopub.execute_input":"2025-02-03T17:11:35.879431Z","iopub.status.idle":"2025-02-03T17:11:35.888007Z","shell.execute_reply.started":"2025-02-03T17:11:35.879392Z","shell.execute_reply":"2025-02-03T17:11:35.886413Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(thresh, cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:36.227069Z","iopub.execute_input":"2025-02-03T17:11:36.227431Z","iopub.status.idle":"2025-02-03T17:11:36.426105Z","shell.execute_reply.started":"2025-02-03T17:11:36.227400Z","shell.execute_reply":"2025-02-03T17:11:36.425167Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Kırpma işlemini yapmak için elimizdeki yeni görüntünün **kenar koordinatlarını(kontur)** buluyoruz.","metadata":{}},{"cell_type":"code","source":"kontur = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:38.821544Z","iopub.execute_input":"2025-02-03T17:11:38.821846Z","iopub.status.idle":"2025-02-03T17:11:38.828311Z","shell.execute_reply.started":"2025-02-03T17:11:38.821822Z","shell.execute_reply":"2025-02-03T17:11:38.827300Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" kontur = kontur[0][0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:39.193965Z","iopub.execute_input":"2025-02-03T17:11:39.194322Z","iopub.status.idle":"2025-02-03T17:11:39.198008Z","shell.execute_reply.started":"2025-02-03T17:11:39.194261Z","shell.execute_reply":"2025-02-03T17:11:39.197122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kontur.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:39.601945Z","iopub.execute_input":"2025-02-03T17:11:39.602230Z","iopub.status.idle":"2025-02-03T17:11:39.607851Z","shell.execute_reply.started":"2025-02-03T17:11:39.602208Z","shell.execute_reply":"2025-02-03T17:11:39.606888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kontur = kontur[:,0,:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:40.032551Z","iopub.execute_input":"2025-02-03T17:11:40.032790Z","iopub.status.idle":"2025-02-03T17:11:40.036162Z","shell.execute_reply.started":"2025-02-03T17:11:40.032770Z","shell.execute_reply":"2025-02-03T17:11:40.035442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kontur.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:40.420058Z","iopub.execute_input":"2025-02-03T17:11:40.420402Z","iopub.status.idle":"2025-02-03T17:11:40.425410Z","shell.execute_reply.started":"2025-02-03T17:11:40.420370Z","shell.execute_reply":"2025-02-03T17:11:40.424656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kontur[:,0].argmax()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:41.560392Z","iopub.execute_input":"2025-02-03T17:11:41.560718Z","iopub.status.idle":"2025-02-03T17:11:41.566451Z","shell.execute_reply.started":"2025-02-03T17:11:41.560690Z","shell.execute_reply":"2025-02-03T17:11:41.565564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kontur[335]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:42.454401Z","iopub.execute_input":"2025-02-03T17:11:42.454691Z","iopub.status.idle":"2025-02-03T17:11:42.460142Z","shell.execute_reply.started":"2025-02-03T17:11:42.454668Z","shell.execute_reply":"2025-02-03T17:11:42.459459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kontur[:,0].argmin()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:42.904757Z","iopub.execute_input":"2025-02-03T17:11:42.905032Z","iopub.status.idle":"2025-02-03T17:11:42.910100Z","shell.execute_reply.started":"2025-02-03T17:11:42.905009Z","shell.execute_reply":"2025-02-03T17:11:42.909380Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kontur[111]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:43.954995Z","iopub.execute_input":"2025-02-03T17:11:43.955351Z","iopub.status.idle":"2025-02-03T17:11:43.960622Z","shell.execute_reply.started":"2025-02-03T17:11:43.955319Z","shell.execute_reply":"2025-02-03T17:11:43.959759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sol = tuple(kontur[kontur[:,0].argmin()])\nsağ = tuple(kontur[kontur[:,0].argmax()])\nüst = tuple(kontur[kontur[:,1].argmin()])\nalt = tuple(kontur[kontur[:,1].argmax()])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:44.405206Z","iopub.execute_input":"2025-02-03T17:11:44.405480Z","iopub.status.idle":"2025-02-03T17:11:44.410053Z","shell.execute_reply.started":"2025-02-03T17:11:44.405457Z","shell.execute_reply":"2025-02-03T17:11:44.409329Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Görüntümüzün **4 uç noktasına ait koordinatları**nı elde ettik.","metadata":{}},{"cell_type":"code","source":"sol, sağ, üst, alt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:46.926260Z","iopub.execute_input":"2025-02-03T17:11:46.926576Z","iopub.status.idle":"2025-02-03T17:11:46.931595Z","shell.execute_reply.started":"2025-02-03T17:11:46.926550Z","shell.execute_reply":"2025-02-03T17:11:46.930801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x1 = sol[0]\ny1 = üst[1]\nx2 = sağ[0]\ny2 = alt[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:48.061036Z","iopub.execute_input":"2025-02-03T17:11:48.061379Z","iopub.status.idle":"2025-02-03T17:11:48.065262Z","shell.execute_reply.started":"2025-02-03T17:11:48.061345Z","shell.execute_reply":"2025-02-03T17:11:48.064478Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x1, y1, x2, y2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:48.404390Z","iopub.execute_input":"2025-02-03T17:11:48.404617Z","iopub.status.idle":"2025-02-03T17:11:48.409359Z","shell.execute_reply.started":"2025-02-03T17:11:48.404599Z","shell.execute_reply":"2025-02-03T17:11:48.408568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"orijinal = img_list[4].copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:51.411846Z","iopub.execute_input":"2025-02-03T17:11:51.412130Z","iopub.status.idle":"2025-02-03T17:11:51.416050Z","shell.execute_reply.started":"2025-02-03T17:11:51.412107Z","shell.execute_reply":"2025-02-03T17:11:51.415174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(orijinal)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:51.739977Z","iopub.execute_input":"2025-02-03T17:11:51.740304Z","iopub.status.idle":"2025-02-03T17:11:52.061188Z","shell.execute_reply.started":"2025-02-03T17:11:51.740248Z","shell.execute_reply":"2025-02-03T17:11:52.060372Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"crop_ilk = orijinal[y1:y2 , x1:x2]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:53.057865Z","iopub.execute_input":"2025-02-03T17:11:53.058163Z","iopub.status.idle":"2025-02-03T17:11:53.061691Z","shell.execute_reply.started":"2025-02-03T17:11:53.058139Z","shell.execute_reply":"2025-02-03T17:11:53.060966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(crop_ilk)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:53.517809Z","iopub.execute_input":"2025-02-03T17:11:53.518078Z","iopub.status.idle":"2025-02-03T17:11:53.786065Z","shell.execute_reply.started":"2025-02-03T17:11:53.518057Z","shell.execute_reply":"2025-02-03T17:11:53.785225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"crop_ilk.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:55.776082Z","iopub.execute_input":"2025-02-03T17:11:55.776396Z","iopub.status.idle":"2025-02-03T17:11:55.781181Z","shell.execute_reply.started":"2025-02-03T17:11:55.776372Z","shell.execute_reply":"2025-02-03T17:11:55.780500Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Kıprma işlemini yaptıktan sonra görüntü boyut kayması yaşadığı için tekrardan (400,400) boyutuna eşitliyoruz.","metadata":{}},{"cell_type":"code","source":"crop_ilk = cv2.resize(crop_ilk,(400,400))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:57.224885Z","iopub.execute_input":"2025-02-03T17:11:57.225170Z","iopub.status.idle":"2025-02-03T17:11:57.229742Z","shell.execute_reply.started":"2025-02-03T17:11:57.225148Z","shell.execute_reply":"2025-02-03T17:11:57.228702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(crop_ilk)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:11:57.842052Z","iopub.execute_input":"2025-02-03T17:11:57.842346Z","iopub.status.idle":"2025-02-03T17:11:58.104112Z","shell.execute_reply.started":"2025-02-03T17:11:57.842322Z","shell.execute_reply":"2025-02-03T17:11:58.103369Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Kenarlardaki veriler gereksiz olduğu için bir eşik belirleyip, bir miktar daha görüntüleri kırpıyoruz.","metadata":{}},{"cell_type":"code","source":"x = int(x2-x1)*4//100\ny = int(y2-y1)*5//100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:00.665642Z","iopub.execute_input":"2025-02-03T17:12:00.665931Z","iopub.status.idle":"2025-02-03T17:12:00.669730Z","shell.execute_reply.started":"2025-02-03T17:12:00.665908Z","shell.execute_reply":"2025-02-03T17:12:00.668919Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x,y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:01.146079Z","iopub.execute_input":"2025-02-03T17:12:01.146442Z","iopub.status.idle":"2025-02-03T17:12:01.151474Z","shell.execute_reply.started":"2025-02-03T17:12:01.146410Z","shell.execute_reply":"2025-02-03T17:12:01.150694Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"crop_son = orijinal[y1+y : y2-y , x1+x : x2-x]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:04.397809Z","iopub.execute_input":"2025-02-03T17:12:04.398095Z","iopub.status.idle":"2025-02-03T17:12:04.402157Z","shell.execute_reply.started":"2025-02-03T17:12:04.398073Z","shell.execute_reply":"2025-02-03T17:12:04.401389Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(crop_son)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:04.751975Z","iopub.execute_input":"2025-02-03T17:12:04.752241Z","iopub.status.idle":"2025-02-03T17:12:05.011993Z","shell.execute_reply.started":"2025-02-03T17:12:04.752216Z","shell.execute_reply":"2025-02-03T17:12:05.011174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"crop_son = cv2.resize(crop_son,(400,400))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:06.041991Z","iopub.execute_input":"2025-02-03T17:12:06.042311Z","iopub.status.idle":"2025-02-03T17:12:06.047058Z","shell.execute_reply.started":"2025-02-03T17:12:06.042256Z","shell.execute_reply":"2025-02-03T17:12:06.046185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(crop_son)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:06.419043Z","iopub.execute_input":"2025-02-03T17:12:06.419292Z","iopub.status.idle":"2025-02-03T17:12:06.702206Z","shell.execute_reply.started":"2025-02-03T17:12:06.419248Z","shell.execute_reply":"2025-02-03T17:12:06.701469Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## CLAHE - Kontrast Limitli Adaptif Histogram Eşitleme","metadata":{}},{"cell_type":"markdown","source":"CLAHE uygulamak için renk kanalını RGB'den LAB'a çeviriyoruz.","metadata":{}},{"cell_type":"code","source":"lab = cv2.cvtColor(crop_son, cv2.COLOR_RGB2LAB)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:10.340223Z","iopub.execute_input":"2025-02-03T17:12:10.340574Z","iopub.status.idle":"2025-02-03T17:12:10.485646Z","shell.execute_reply.started":"2025-02-03T17:12:10.340546Z","shell.execute_reply":"2025-02-03T17:12:10.484315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lab.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:10.752319Z","iopub.execute_input":"2025-02-03T17:12:10.752608Z","iopub.status.idle":"2025-02-03T17:12:10.757424Z","shell.execute_reply.started":"2025-02-03T17:12:10.752584Z","shell.execute_reply":"2025-02-03T17:12:10.756611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"l,a,b = cv2.split(lab)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:11.173060Z","iopub.execute_input":"2025-02-03T17:12:11.173331Z","iopub.status.idle":"2025-02-03T17:12:11.178249Z","shell.execute_reply.started":"2025-02-03T17:12:11.173309Z","shell.execute_reply":"2025-02-03T17:12:11.177455Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"LAB'daki ''l'' görüntünün siyah-beyaz parlaklık değerini içeriyor. CLAHE işlemini sadece bu katmana uygulayacağız. ","metadata":{}},{"cell_type":"code","source":"plt.imshow(l, cmap='gray')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:12.526181Z","iopub.execute_input":"2025-02-03T17:12:12.526531Z","iopub.status.idle":"2025-02-03T17:12:12.760933Z","shell.execute_reply.started":"2025-02-03T17:12:12.526503Z","shell.execute_reply":"2025-02-03T17:12:12.760118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"l.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:12.990622Z","iopub.execute_input":"2025-02-03T17:12:12.990897Z","iopub.status.idle":"2025-02-03T17:12:12.996024Z","shell.execute_reply.started":"2025-02-03T17:12:12.990876Z","shell.execute_reply":"2025-02-03T17:12:12.995195Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"düz = l.flatten()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:14.384911Z","iopub.execute_input":"2025-02-03T17:12:14.385198Z","iopub.status.idle":"2025-02-03T17:12:14.389169Z","shell.execute_reply.started":"2025-02-03T17:12:14.385175Z","shell.execute_reply":"2025-02-03T17:12:14.388357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"düz.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:14.818668Z","iopub.execute_input":"2025-02-03T17:12:14.818945Z","iopub.status.idle":"2025-02-03T17:12:14.823859Z","shell.execute_reply.started":"2025-02-03T17:12:14.818925Z","shell.execute_reply":"2025-02-03T17:12:14.822958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(düz,25,[0,256], color = 'r')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:16.198527Z","iopub.execute_input":"2025-02-03T17:12:16.198816Z","iopub.status.idle":"2025-02-03T17:12:16.353714Z","shell.execute_reply.started":"2025-02-03T17:12:16.198793Z","shell.execute_reply":"2025-02-03T17:12:16.352893Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"clahe = cv2.createCLAHE(clipLimit=7.0,tileGridSize=((8,8)))\ncl = clahe.apply(l)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:17.415923Z","iopub.execute_input":"2025-02-03T17:12:17.416209Z","iopub.status.idle":"2025-02-03T17:12:17.425209Z","shell.execute_reply.started":"2025-02-03T17:12:17.416188Z","shell.execute_reply":"2025-02-03T17:12:17.424344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(cl.flatten(),25,[0,256], color = 'r')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:18.692464Z","iopub.execute_input":"2025-02-03T17:12:18.692794Z","iopub.status.idle":"2025-02-03T17:12:18.851687Z","shell.execute_reply.started":"2025-02-03T17:12:18.692772Z","shell.execute_reply":"2025-02-03T17:12:18.850840Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## CLAHE uygulanmış hali","metadata":{}},{"cell_type":"code","source":"plt.imshow(cl)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:20.474097Z","iopub.execute_input":"2025-02-03T17:12:20.474538Z","iopub.status.idle":"2025-02-03T17:12:20.746473Z","shell.execute_reply.started":"2025-02-03T17:12:20.474502Z","shell.execute_reply":"2025-02-03T17:12:20.745606Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## CLAHE uygulanmamış hali\n","metadata":{}},{"cell_type":"code","source":"plt.imshow(l)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:25.532779Z","iopub.execute_input":"2025-02-03T17:12:25.533070Z","iopub.status.idle":"2025-02-03T17:12:25.778574Z","shell.execute_reply.started":"2025-02-03T17:12:25.533048Z","shell.execute_reply":"2025-02-03T17:12:25.777739Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"CLAHE işlemi uyguladığımız katmanı, diğer katmanlarla birleştirip tekrardan görüntümüzü RGB yapıyoruz.","metadata":{}},{"cell_type":"code","source":"limg = cv2.merge((cl,a,b))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:26.883243Z","iopub.execute_input":"2025-02-03T17:12:26.883578Z","iopub.status.idle":"2025-02-03T17:12:26.889585Z","shell.execute_reply.started":"2025-02-03T17:12:26.883552Z","shell.execute_reply":"2025-02-03T17:12:26.888719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"son = cv2.cvtColor(limg, cv2.COLOR_LAB2RGB)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:28.429013Z","iopub.execute_input":"2025-02-03T17:12:28.429341Z","iopub.status.idle":"2025-02-03T17:12:28.434190Z","shell.execute_reply.started":"2025-02-03T17:12:28.429310Z","shell.execute_reply":"2025-02-03T17:12:28.433525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(son)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:30.104815Z","iopub.execute_input":"2025-02-03T17:12:30.105097Z","iopub.status.idle":"2025-02-03T17:12:30.393500Z","shell.execute_reply.started":"2025-02-03T17:12:30.105074Z","shell.execute_reply":"2025-02-03T17:12:30.392583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(crop_son)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:33.347125Z","iopub.execute_input":"2025-02-03T17:12:33.347481Z","iopub.status.idle":"2025-02-03T17:12:33.619175Z","shell.execute_reply.started":"2025-02-03T17:12:33.347450Z","shell.execute_reply":"2025-02-03T17:12:33.618352Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Median Blur","metadata":{}},{"cell_type":"code","source":"med_son = cv2.medianBlur(son, 3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:34.900806Z","iopub.execute_input":"2025-02-03T17:12:34.901094Z","iopub.status.idle":"2025-02-03T17:12:34.908542Z","shell.execute_reply.started":"2025-02-03T17:12:34.901072Z","shell.execute_reply":"2025-02-03T17:12:34.907758Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(med_son)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:36.582572Z","iopub.execute_input":"2025-02-03T17:12:36.582877Z","iopub.status.idle":"2025-02-03T17:12:36.866618Z","shell.execute_reply.started":"2025-02-03T17:12:36.582852Z","shell.execute_reply":"2025-02-03T17:12:36.865739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"arka_plan = cv2.medianBlur(son, 37)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:37.640772Z","iopub.execute_input":"2025-02-03T17:12:37.641054Z","iopub.status.idle":"2025-02-03T17:12:37.659524Z","shell.execute_reply.started":"2025-02-03T17:12:37.641033Z","shell.execute_reply":"2025-02-03T17:12:37.658865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(arka_plan)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:39.510806Z","iopub.execute_input":"2025-02-03T17:12:39.511118Z","iopub.status.idle":"2025-02-03T17:12:39.765560Z","shell.execute_reply.started":"2025-02-03T17:12:39.511091Z","shell.execute_reply":"2025-02-03T17:12:39.764677Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Anevrizma Görünürülüğünü Arttırma","metadata":{}},{"cell_type":"code","source":"maske = cv2.addWeighted(med_son,1,arka_plan,-1,255)\nplt.imshow(maske)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:43.533807Z","iopub.execute_input":"2025-02-03T17:12:43.534135Z","iopub.status.idle":"2025-02-03T17:12:43.782873Z","shell.execute_reply.started":"2025-02-03T17:12:43.534105Z","shell.execute_reply":"2025-02-03T17:12:43.781977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"son_img = cv2.bitwise_and(maske,med_son)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:44.848496Z","iopub.execute_input":"2025-02-03T17:12:44.848787Z","iopub.status.idle":"2025-02-03T17:12:44.855102Z","shell.execute_reply.started":"2025-02-03T17:12:44.848764Z","shell.execute_reply":"2025-02-03T17:12:44.854340Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# işlemden sonraki hali","metadata":{}},{"cell_type":"code","source":"plt.imshow(son_img)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:48.196610Z","iopub.execute_input":"2025-02-03T17:12:48.196893Z","iopub.status.idle":"2025-02-03T17:12:48.467414Z","shell.execute_reply.started":"2025-02-03T17:12:48.196871Z","shell.execute_reply":"2025-02-03T17:12:48.466464Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# işlemden önceki hali","metadata":{}},{"cell_type":"code","source":"plt.imshow(med_son)","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:12:55.833686Z","iopub.execute_input":"2025-02-03T17:12:55.833979Z","iopub.status.idle":"2025-02-03T17:12:56.099101Z","shell.execute_reply.started":"2025-02-03T17:12:55.833956Z","shell.execute_reply":"2025-02-03T17:12:56.098307Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Yaptığımız tüm işlemleri tek bir for döngüsünde toplayıp, elimizdeki bütün görüntüleri bu döngüyle img_list ismindeki listeye kaydediyoruz.","metadata":{}},{"cell_type":"code","source":"img_list = []\n\nfrom tqdm import tqdm_notebook as tqdm\n\nfor i in tqdm(files):\n    image = cv2.imread(path + 'train_images/'+i)\n    image = cv2.resize(image,(400,400))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    kopya = image.copy()\n    kopya = cv2.cvtColor(kopya, cv2.COLOR_RGB2GRAY)\n    blur = cv2.GaussianBlur(kopya,(5,5),0)\n    thresh = cv2.threshold(blur,10,255, cv2.THRESH_BINARY)[1]\n    kontur = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    kontur = kontur[0][0]\n    kontur = kontur[:,0,:]\n    x1 = tuple(kontur[kontur[:,0].argmin()])[0]\n    y1 = tuple(kontur[kontur[:,1].argmin()])[1]\n    x2 = tuple(kontur[kontur[:,0].argmax()])[0]\n    y2 = tuple(kontur[kontur[:,1].argmax()])[1]\n    x = int(x2-x1)*4//50\n    y = int(y2-y1)*5//50\n    kopya2 = image.copy()\n    if x2-x1 >100 and y2-y1> 100:\n        kopya2 = kopya2[y1+y : y2-y , x1+x : x2-x]\n        kopya2 = cv2.resize(kopya2,(400,400))\n    lab = cv2.cvtColor(kopya2, cv2.COLOR_RGB2LAB)\n    l,a,b = cv2.split(lab)\n    clahe = cv2.createCLAHE(clipLimit=5.0,tileGridSize=((8,8)))\n    cl = clahe.apply(l)\n    limg = cv2.merge((cl,a,b))\n    son = cv2.cvtColor(limg, cv2.COLOR_LAB2RGB)\n    med_son = cv2.medianBlur(son, 3)\n    arka_plan = cv2.medianBlur(son, 37)\n    maske = cv2.addWeighted(med_son,1,arka_plan,-1,255)\n    son_img = cv2.bitwise_and(maske,med_son)\n    img_list.append(son_img)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:17:56.840975Z","iopub.execute_input":"2025-02-03T17:17:56.841345Z","iopub.status.idle":"2025-02-03T17:25:56.274552Z","shell.execute_reply.started":"2025-02-03T17:17:56.841310Z","shell.execute_reply":"2025-02-03T17:25:56.273576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(img_list[2])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:41:50.375057Z","iopub.execute_input":"2025-02-03T17:41:50.375440Z","iopub.status.idle":"2025-02-03T17:41:50.674241Z","shell.execute_reply.started":"2025-02-03T17:41:50.375410Z","shell.execute_reply":"2025-02-03T17:41:50.673354Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = plt.figure(figsize=(20,12))\n\nfor i in range(12):\n    img = img_list[i]\n    fig.add_subplot(3,4,i+1)\n    plt.imshow(img)\n\nplt.tight_layout()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T17:41:58.700764Z","iopub.execute_input":"2025-02-03T17:41:58.701051Z","iopub.status.idle":"2025-02-03T17:42:01.865751Z","shell.execute_reply.started":"2025-02-03T17:41:58.701028Z","shell.execute_reply":"2025-02-03T17:42:01.864391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df['diagnosis']","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### One Hot Encoding","metadata":{}},{"cell_type":"code","source":"y_train = pd.get_dummies(df['diagnosis']).values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:08.629490Z","iopub.execute_input":"2025-02-03T18:02:08.629809Z","iopub.status.idle":"2025-02-03T18:02:08.635044Z","shell.execute_reply.started":"2025-02-03T18:02:08.629784Z","shell.execute_reply":"2025-02-03T18:02:08.634117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# y_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:09.165342Z","iopub.execute_input":"2025-02-03T18:02:09.165587Z","iopub.status.idle":"2025-02-03T18:02:09.168872Z","shell.execute_reply.started":"2025-02-03T18:02:09.165568Z","shell.execute_reply":"2025-02-03T18:02:09.167953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['diagnosis'][1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:09.599814Z","iopub.execute_input":"2025-02-03T18:02:09.600096Z","iopub.status.idle":"2025-02-03T18:02:09.605152Z","shell.execute_reply.started":"2025-02-03T18:02:09.600075Z","shell.execute_reply":"2025-02-03T18:02:09.604470Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:09.991906Z","iopub.execute_input":"2025-02-03T18:02:09.992135Z","iopub.status.idle":"2025-02-03T18:02:09.997035Z","shell.execute_reply.started":"2025-02-03T18:02:09.992116Z","shell.execute_reply":"2025-02-03T18:02:09.996328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_son = np.ones(y_train.shape, dtype='uint8')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:10.386061Z","iopub.execute_input":"2025-02-03T18:02:10.386353Z","iopub.status.idle":"2025-02-03T18:02:10.389917Z","shell.execute_reply.started":"2025-02-03T18:02:10.386326Z","shell.execute_reply":"2025-02-03T18:02:10.388999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# y_train_son","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:10.771505Z","iopub.execute_input":"2025-02-03T18:02:10.771784Z","iopub.status.idle":"2025-02-03T18:02:10.775144Z","shell.execute_reply.started":"2025-02-03T18:02:10.771760Z","shell.execute_reply":"2025-02-03T18:02:10.774352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_son[:,4] = y_train[:,4]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:11.364143Z","iopub.execute_input":"2025-02-03T18:02:11.364447Z","iopub.status.idle":"2025-02-03T18:02:11.368209Z","shell.execute_reply.started":"2025-02-03T18:02:11.364420Z","shell.execute_reply":"2025-02-03T18:02:11.367326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# y_train_son","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:11.737986Z","iopub.execute_input":"2025-02-03T18:02:11.738214Z","iopub.status.idle":"2025-02-03T18:02:11.741696Z","shell.execute_reply.started":"2025-02-03T18:02:11.738194Z","shell.execute_reply":"2025-02-03T18:02:11.740808Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:13.690017Z","iopub.execute_input":"2025-02-03T18:02:13.690375Z","iopub.status.idle":"2025-02-03T18:02:13.693884Z","shell.execute_reply.started":"2025-02-03T18:02:13.690344Z","shell.execute_reply":"2025-02-03T18:02:13.693071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.logical_or(0,0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:14.086614Z","iopub.execute_input":"2025-02-03T18:02:14.086879Z","iopub.status.idle":"2025-02-03T18:02:14.091995Z","shell.execute_reply.started":"2025-02-03T18:02:14.086856Z","shell.execute_reply":"2025-02-03T18:02:14.091135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.logical_or(1,0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:14.471455Z","iopub.execute_input":"2025-02-03T18:02:14.471713Z","iopub.status.idle":"2025-02-03T18:02:14.476344Z","shell.execute_reply.started":"2025-02-03T18:02:14.471689Z","shell.execute_reply":"2025-02-03T18:02:14.475568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.logical_or(0,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:14.883577Z","iopub.execute_input":"2025-02-03T18:02:14.883828Z","iopub.status.idle":"2025-02-03T18:02:14.888705Z","shell.execute_reply.started":"2025-02-03T18:02:14.883806Z","shell.execute_reply":"2025-02-03T18:02:14.887983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.logical_or(1,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:15.234972Z","iopub.execute_input":"2025-02-03T18:02:15.235204Z","iopub.status.idle":"2025-02-03T18:02:15.239723Z","shell.execute_reply.started":"2025-02-03T18:02:15.235183Z","shell.execute_reply":"2025-02-03T18:02:15.238984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.logical_and(0,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:15.606133Z","iopub.execute_input":"2025-02-03T18:02:15.606426Z","iopub.status.idle":"2025-02-03T18:02:15.611345Z","shell.execute_reply.started":"2025-02-03T18:02:15.606402Z","shell.execute_reply":"2025-02-03T18:02:15.610598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.logical_and(1,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:17.316030Z","iopub.execute_input":"2025-02-03T18:02:17.316341Z","iopub.status.idle":"2025-02-03T18:02:17.322106Z","shell.execute_reply.started":"2025-02-03T18:02:17.316315Z","shell.execute_reply":"2025-02-03T18:02:17.321182Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(3,-1,-1):\n    y_train_son[:,i] = np.logical_or(y_train[:,i], y_train_son[:,i+1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:17.661152Z","iopub.execute_input":"2025-02-03T18:02:17.661472Z","iopub.status.idle":"2025-02-03T18:02:17.666047Z","shell.execute_reply.started":"2025-02-03T18:02:17.661443Z","shell.execute_reply":"2025-02-03T18:02:17.665045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# y_train_son","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:19.184154Z","iopub.execute_input":"2025-02-03T18:02:19.184518Z","iopub.status.idle":"2025-02-03T18:02:19.187847Z","shell.execute_reply.started":"2025-02-03T18:02:19.184487Z","shell.execute_reply":"2025-02-03T18:02:19.186968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train = np.array(img_list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:19.981603Z","iopub.execute_input":"2025-02-03T18:02:19.981886Z","iopub.status.idle":"2025-02-03T18:02:20.501932Z","shell.execute_reply.started":"2025-02-03T18:02:19.981862Z","shell.execute_reply":"2025-02-03T18:02:20.500894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:02:20.503007Z","iopub.execute_input":"2025-02-03T18:02:20.503363Z","iopub.status.idle":"2025-02-03T18:02:20.508898Z","shell.execute_reply.started":"2025-02-03T18:02:20.503330Z","shell.execute_reply":"2025-02-03T18:02:20.508033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_son.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T20:06:35.419428Z","iopub.execute_input":"2025-02-03T20:06:35.419743Z","iopub.status.idle":"2025-02-03T20:06:35.425070Z","shell.execute_reply.started":"2025-02-03T20:06:35.419718Z","shell.execute_reply":"2025-02-03T20:06:35.424202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_val , y_train, y_val = train_test_split(x_train,\n                                                   y_train_son,\n                                                   test_size=0.15,\n                                                   random_state=2019,\n                                                   shuffle=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T20:06:42.307197Z","iopub.execute_input":"2025-02-03T20:06:42.307567Z","iopub.status.idle":"2025-02-03T20:06:42.331588Z","shell.execute_reply.started":"2025-02-03T20:06:42.307538Z","shell.execute_reply":"2025-02-03T20:06:42.330318Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape, x_val.shape , y_train.shape, y_val.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:03:14.416089Z","iopub.execute_input":"2025-02-03T18:03:14.416750Z","iopub.status.idle":"2025-02-03T18:03:14.423024Z","shell.execute_reply.started":"2025-02-03T18:03:14.416706Z","shell.execute_reply":"2025-02-03T18:03:14.422083Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Veri büyütme (Data Augmentation)","metadata":{}},{"cell_type":"markdown","source":"<img src=https://raw.githubusercontent.com/brktzlk/Diyabetik_Retinopati_Teshisi/master/İmages/ret%20sık.png width=1300>","metadata":{}},{"cell_type":"markdown","source":"> 1. random rotation \n> 2. horizontal flips\n> 3. vertical flips\n> 4. horizontal shifts\n> 5. vertical shifts\n> 6. shear\n\nRetinopati teşhisi modellerinde sık kullanılır.","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(horizontal_flip=True,vertical_flip=True)\ndata_generator = datagen.flow(x_train,y_train,batch_size=2,seed=2020)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T19:26:50.536659Z","iopub.execute_input":"2025-02-03T19:26:50.536949Z","iopub.status.idle":"2025-02-03T19:26:57.456162Z","shell.execute_reply.started":"2025-02-03T19:26:50.536926Z","shell.execute_reply":"2025-02-03T19:26:57.455482Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Transfer Learning  - EfficientNet\n","metadata":{}},{"cell_type":"markdown","source":"<img src=https://miro.medium.com/max/700/1*jm5MEylOA8abyAi51CcSLA.png width=1300>","metadata":{}},{"cell_type":"code","source":"!pip install efficientnet","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T18:12:27.888069Z","iopub.execute_input":"2025-02-03T18:12:27.888414Z","iopub.status.idle":"2025-02-03T18:12:32.813148Z","shell.execute_reply.started":"2025-02-03T18:12:27.888381Z","shell.execute_reply":"2025-02-03T18:12:32.812000Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow.keras.applications import EfficientNetB5\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T19:56:24.784421Z","iopub.execute_input":"2025-02-03T19:56:24.784752Z","iopub.status.idle":"2025-02-03T19:56:24.788617Z","shell.execute_reply.started":"2025-02-03T19:56:24.784727Z","shell.execute_reply":"2025-02-03T19:56:24.787795Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## include_top \nEğer bu seçeneği **''True''** yaparsanız; model daha önce hangi verilerle eğitildiyse yine o verilere uygun tahmin etme yeteneğine sahip olacaktır. Önreğin daha önce eğitimde uçak,araba,ev gördüyse siz kendi verilerinizle eğitim yapsanız da retinopati görselini gördüğünde uçak mı araba mı olduğunu anlamaya çalışacaktır. Ayrıca Kendi verilerinizle eğitim yapabilmek için önceden eğitilen görüntülerin boyutuna çevirmelisiniz. ImageNet 32x32 görüntülere sahip. Bizim elimizdeki 400x400 görüntüleri 32x32 boyutuna indirgemeliyiz eğitime sokabilmek için. Fakat bunu yapsak bile işimize yaramayacak. Çünkü model, include_top=''True'' yapıldığında ev,araba,uçak vb. görüntülerden başka görüntüyü sınıflandırma yapamaz.","metadata":{}},{"cell_type":"code","source":"örnek_model2 = EfficientNetB5(include_top=False)\n# örnek_model2.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T19:56:26.781611Z","iopub.execute_input":"2025-02-03T19:56:26.781910Z","iopub.status.idle":"2025-02-03T19:56:29.157288Z","shell.execute_reply.started":"2025-02-03T19:56:26.781887Z","shell.execute_reply":"2025-02-03T19:56:29.156360Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Bu seçeneği **''False''** yaptığımızda modelin sonundaki tahmin yapan kısmı kaldırıyor. Bu sayede kendi verilerimize uygun tahmin katmanı oluşturabiliyoruz.","metadata":{}},{"cell_type":"code","source":"img_shape = (400,400,3)\nEfficientNetB5 = EfficientNetB5(weights = 'imagenet',\n            include_top = False,\n           input_shape = img_shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T19:56:31.281886Z","iopub.execute_input":"2025-02-03T19:56:31.282188Z","iopub.status.idle":"2025-02-03T19:56:33.643889Z","shell.execute_reply.started":"2025-02-03T19:56:31.282165Z","shell.execute_reply":"2025-02-03T19:56:33.643118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for layer in EfficientNetB5.layers[:-3]:   #  EfficientNetB5 modeline ait olan son 3 nörondan öncesini öğrenemez\n    layer.trainable = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T19:56:36.096033Z","iopub.execute_input":"2025-02-03T19:56:36.096390Z","iopub.status.idle":"2025-02-03T19:56:36.107953Z","shell.execute_reply.started":"2025-02-03T19:56:36.096358Z","shell.execute_reply":"2025-02-03T19:56:36.107073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential \nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dropout,Dense\n\n\nmodel = Sequential()\nmodel.add(EfficientNetB5)\nmodel.add(GlobalAveragePooling2D())   # diğer modellerde 'flatten' bu modelimizde GlobalAveragePooling2D\nmodel.add(Dropout(0.5))\nmodel.add(Dense(5,activation = 'sigmoid'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T19:57:53.367082Z","iopub.execute_input":"2025-02-03T19:57:53.367413Z","iopub.status.idle":"2025-02-03T19:57:53.396615Z","shell.execute_reply.started":"2025-02-03T19:57:53.367387Z","shell.execute_reply":"2025-02-03T19:57:53.395953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfrom tensorflow.keras.optimizers import Adam\n\nmodel.compile(loss='binary_crossentropy',\n             optimizer=Adam(learning_rate=5e-5),\n             metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T19:57:55.695434Z","iopub.execute_input":"2025-02-03T19:57:55.695730Z","iopub.status.idle":"2025-02-03T19:57:55.707227Z","shell.execute_reply.started":"2025-02-03T19:57:55.695706Z","shell.execute_reply":"2025-02-03T19:57:55.706356Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau\n\nlr = ReduceLROnPlateau(monitor = 'val_loss',\n                      patience = 3,\n                      verbose = 1,\n                      mode='auto',\n                      factor=0.25,\n                      min_lr=0.000001)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-03T19:57:58.062062Z","iopub.execute_input":"2025-02-03T19:57:58.062381Z","iopub.status.idle":"2025-02-03T19:57:58.066568Z","shell.execute_reply.started":"2025-02-03T19:57:58.062354Z","shell.execute_reply":"2025-02-03T19:57:58.065766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(data_generator,\n                    steps_per_epoch = 1000, #aslında 3100 / 2 (train / batch) \n                    epochs = 3,\n                    validation_data = (x_val,y_val),\n                    callbacks = [lr])","metadata":{"trusted":true,"_kg_hide-output":true},"outputs":[],"execution_count":null}]}