{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"pip install keras==2.3.1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\nimport h5py\nimport cv2\nimport glob, os\nfrom matplotlib import pyplot as plt\nimport h5py\nimport numpy as np\nfrom tqdm import tqdm\nlbl=[]\nimg=np.zeros((3064,224,224))\nfor i in range(1,3065):\n    try:\n        path='/kaggle/input/brain-tumour-1/brainTumorDataPublic_1766/'\n        with h5py.File(path+str(i)+'.mat') as f:\n          images = f['cjdata']\n          resized = cv2.resize(images['image'][:,:], (224,224), interpolation = cv2.INTER_CUBIC )\n          x=np.asarray(resized)\n          x=(x-np.min(x))/(np.max(x)-np.min(x))\n          x=x.reshape((1,224,224))\n          img[i-1]=x\n          lbl.append(int(images['label'][0]))\n    except:\n        try:\n          path='/kaggle/input/brain-tumour-1/brainTumorDataPublic_22993064/'\n          with h5py.File(path+str(i)+'.mat') as f:\n              images = f['cjdata']\n              resized = cv2.resize(images['image'][:,:], (224,224), interpolation = cv2.INTER_CUBIC )\n              x=np.asarray(resized)\n              x=(x-np.min(x))/(np.max(x)-np.min(x))\n              x=x.reshape((1,224,224))\n              img[i-1]=x\n              lbl.append(int(images['label'][0]))\n        except:\n            try:\n              path='/kaggle/input/brain-tumour-1/brainTumorDataPublic_15332298/'\n              with h5py.File(path+str(i)+'.mat') as f:\n                  images = f['cjdata']\n                  resized = cv2.resize(images['image'][:,:], (224,224), interpolation = cv2.INTER_CUBIC )\n                  x=np.asarray(resized)\n                  x=(x-np.min(x))/(np.max(x)-np.min(x))\n                  x=x.reshape((1,224,224))\n                  img[i-1]=x\n                  lbl.append(int(images['label'][0]))\n            except:\n              path='/kaggle/input/brain-tumour-1/brainTumorDataPublic_7671532/'\n              with h5py.File(path+str(i)+'.mat') as f:\n                  images = f['cjdata']\n                  resized = cv2.resize(images['image'][:,:], (224,224), interpolation = cv2.INTER_CUBIC )\n                  x=np.asarray(resized)\n                  x=(x-np.min(x))/(np.max(x)-np.min(x))\n                  x=x.reshape((1,224,224))\n                  img[i-1]=x\n                  lbl.append(int(images['label'][0]))\npath='/kaggle/input/brain-tumour/cvind (2).mat'\n\nwith h5py.File(path) as f:\n      data=f['cvind']\n      idx=data[0]\nimport scipy.io\nobj_arr = {}\nobj_arr['images'] = img\nobj_arr['label'] = lbl\nobj_arr['fold']=idx\nnp.save('check.npy', obj_arr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\nimport h5py\nfrom tensorflow.keras.optimizers import *\nimport cv2\nfrom tensorflow.keras.utils import to_categorical\nfrom matplotlib import pyplot as plt\nimport glob, os\nfrom matplotlib import pyplot as plt\nimport h5py\nfrom sklearn.metrics import accuracy_score\nimport numpy as np\nfrom tqdm import tqdm\nimport time\nimport gc\nfrom tensorflow.keras.applications import *\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.models import Model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = \"check.npy\" \ndf=np.load(path,allow_pickle=True)\ndf=df.item()\ndf['images']=df['images'].astype(np.float32)\ndimension=224","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#shuffle samples\ndef unison_shuffled_copies(items):\n    p = np.random.permutation(len(items[0]))\n    return [item[p] for item in items]\n\n\n\n\n#change targets\ndef change(img):\n    resized = cv2.resize(img, (299,299), interpolation = cv2.INTER_AREA )\n    return resized\n\n\n\n\n#get train and test splits\ndef get_trn_tst(df,tst_fold):\n  dimension=224\n  idx=np.asarray(df['fold'])\n  y=np.asarray(df['label'])\n  y-=1\n  img=np.asarray(df['images'])\n  img1=[]\n  img1=np.asarray(img)\n  del([img])\n  gc.collect()\n  trn_y=np.asarray(y[(idx!=tst_fold)])\n  trn_img=np.asarray(img1[(idx!=tst_fold)])\n  tst_y=np.asarray(y[(idx==tst_fold)])\n  tst_img=img1[idx==tst_fold]\n  trn_img=np.repeat(trn_img.reshape((trn_img.shape[0],dimension,dimension,1)),3,axis=3)\n  tst_img=np.repeat(tst_img.reshape((tst_img.shape[0],dimension,dimension,1)),3,axis=3)\n  return (trn_img.copy(),trn_y.copy()),(tst_img.copy(),tst_y.copy())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod=InceptionV3(include_top=True, weights='imagenet')\nmod.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def load_model(last=True):   \n  K.clear_session() \n  mod=InceptionV3(include_top=True, weights='imagenet')\n  out_1=mod.layers[-2].output\n  out=Dense(3,activation='softmax')(out_1)\n  model=Model(inputs=mod.input,outputs=out)\n\n  if last:\n    for i in range(len(model.layers)):\n        model.layers[i].trainable = False\n  model.layers[-1].trainable=True\n  return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow import keras\nimport pandas as pd\nfrom tensorflow.keras.applications.nasnet import NASNetMobile, preprocess_input\nimport imgaug as ia\nfrom imgaug import augmenters as iaa\ndef rotate_image(image, angle):\n  image_center = tuple(np.array(image.shape[1::-1]) / 2)\n  rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)\n  result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)\n  return result\ndef Hflip( images):\n\t\tseq = iaa.Sequential([iaa.Fliplr(1.0)])\n\t\treturn seq.augment_images(images)\ndef Vflip( images):\n\t\tseq = iaa.Sequential([iaa.Flipud(1.0)])\n\t\treturn seq.augment_images(images)\ndef noise(images):\n    ls=[]\n    for i in images:\n        x = np.random.normal(loc=0, scale=0.05, size=(dimension,dimension,3))\n        ls.append(i+x)\n    return ls\ndef rotate(images):\n    ls=[]\n    for angle in range(-15,20,5):\n        for image in images:\n            ls.append(rotate_image(image,angle))\n    return ls\n\nclass DataGenerator(keras.utils.Sequence):\n  def __init__(self, images, labels, batch_size=64, image_dimensions = (96 ,96 ,3), shuffle=False, augment=False):\n    self.labels       = labels              # array of labels\n    self.images = images        # array of image paths\n    self.batch_size   = batch_size          # batch size\n    self.on_epoch_end()\n\n  def __len__(self):\n    return int(np.floor(self.labels.shape[0] / self.batch_size))\n\n  def on_epoch_end(self):\n    self.indexes = np.arange(self.labels.shape[0])\n\n  def __getitem__(self, index):\n\t\t# selects indices of data for next batch\n    indexes = self.indexes[index * self.batch_size : (index + 1) * self.batch_size]\n    # select data and load images\n    labels = self.labels.loc[indexes]\n    img = [self.images[k].astype(np.float32) for k in indexes]\n    imgH=Hflip(img)\n    imgV=Vflip(img)\n    imgR=rotate(img)\n    images=[]\n    images.extend(imgH)\n    images.extend(imgV)\n    images.extend(imgR)\n    lbl=labels.copy()\n    labels=pd.DataFrame()\n    labels=pd.concat([labels,lbl],0)\n    labels=pd.concat([labels,lbl],0)\n    labels=pd.concat([labels,lbl],0)\n    labels=pd.concat([labels,lbl],0)\n    labels=pd.concat([labels,lbl],0)\n    labels=pd.concat([labels,lbl],0)\n    labels=pd.concat([labels,lbl],0)\n    labels=pd.concat([labels,lbl],0)\n    labels=pd.concat([labels,lbl],0)\n    #images = np.array([preprocess_input(img) for img in images])\n    return np.asarray(images), labels.values.astype('uint8')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"best_accuracy_last={}\nfinal_accuracy_last={}\nhistory_last={}\nanswers_last={}\npredictions_last={}\npredictions_last_best={}\ntimes_last={}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"  def upd(dk,data): \n    if dk==0:\n        dk=data\n    else:\n        for ky in data.keys():\n            dk[ky].extend(data[ky])\n    return dk\n  index=4\n  epoch=50\n  pre_acc=0\n  best=0\n  fold='fold_'+str(index)\n  trn,tst=get_trn_tst(df,index)\n  history_last[fold]=0\n  ids=np.arange(len(tst[0]))\n\n\n  plt.imshow(trn[0][0])\n  plt.show()\n  plt.imshow(tst[0][0])\n  plt.show()\n\n\n\n  trn_x,trn_y=unison_shuffled_copies([trn[0],trn[1]])\n  tst_x,tst_y,ids=unison_shuffled_copies([tst[0],tst[1],ids])\n\n\n\n  model=load_model(last=False)\n\n\n  \n  #compiling the model\n  model.compile(optimizer=Adam(3e-4,decay=1e-3), \n                     loss='categorical_crossentropy', \n                     metrics=['accuracy'])\n  train_data = DataGenerator(trn_x,pd.get_dummies(trn_y), batch_size=4, augment=True)\n\n  \n  #fitting the model\n  #timing\n  start=time.time()\n  for i in range(epoch):\n      hist=model.fit_generator(train_data,epochs=1,steps_per_epoch=len(trn_x)//4)\n      history_last[fold]=upd(history_last[fold],hist.history)\n\n  end=time.time()\n  times_last[fold]=end-start\n\n  #getting the prediction \n  pre=model.predict(tst_x)\n  \n\n\n\n  #select the maximum position\n  predictions=np.argmax(pre,1)\n  predictions_last[fold]=predictions\n  predictions_last[fold+'_predictions']=pre\n    \n\n  \n  \n  \n  #getting the accuracy\n  new_acc=accuracy_score(predictions,tst_y)\n\n  \n\n\n  #storing the predictions\n  final_accuracy_last[fold]=new_acc\n\n\n\n\n\n\n\n\n  #storing the answers\n  answers_last[fold]=tst_y\n    \n    \n    \n    \n  #freeing memory\n  del([trn,tst,trn_x,trn_y,tst_x])\n  gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.save('best_accuracy_last_fold'+str(index)+'_1e-3_wn.npy',best_accuracy_last)\nnp.save('final_accuracy_last_fold'+str(index)+'_1e-3_wn.npy',final_accuracy_last)\nnp.save('history_last_fold'+str(index)+'_1e-3_wn.npy',history_last)\nnp.save('answers_last_fold'+str(index)+'_1e-3_wn.npy',answers_last)\nnp.save('predictions_last_fold'+str(index)+'_1e-3_wn.npy',predictions_last)\nnp.save('predictions_last_best_fold'+str(index)+'_1e-3_wn.npy',predictions_last_best)\nnp.save('times_last_fold'+str(index)+'_1e-3_wn.npy',times_last)\nnp.save('actual_predictions'+str(index)+'_1e-3_wn.npy',tst_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history_last[fold]['loss'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_acc","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}