{"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":"\n\nimport math\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Sequential\nfrom tensorflow.keras.utils import to_categorical\nimport numpy as np \nimport PIL\n\nPIL.Image.MAX_IMAGE_PIXELS = 16989571560\nnp.random.seed(0)\nmain_dir = r'../input/mayo-clinic-strip-ai/train/'\n\ndata_dir = main_dir + '\\data'\nval_ratio = 0.2\ntest_ratio = 0.4\ntrain_dir = r'../input/mayo-clinic-strip-ai/train/'\nval_dir = data_dir + '\\val'\ntest_dir = '../input/mayo-clinic-strip-ai/test/'\n\n\nmodel_name=\"mobilenet_v2\"\nbatch_size = 16\nnum_classes = 2\nepochs = 20\nstep_size=10\n\nheight = 224\nwidth=224\nchannels=3\nnum_classes=2\n\nlog_dir=r\"C:\\Users\\Amzad\\Desktop\\keras_project\\image_classification\\project\\logs\"\n\ncsv_log_dir=log_dir+\"\\csv_log\"\ntensorboard_log_dir=log_dir+\"\\tensorboard_log\"\nweights_dir=log_dir+\"\\weights\"\n\n\n\nearly_stoping=False\nglob_file1=\"C:/Users/Amzad/Desktop/keras_project/image_classification/data/test/Cat/*.*\"\nglob_file2=\"C:/Users/Amzad/Desktop/keras_project/image_classification/data/test/Dog/*.*\"\nprediction_dir=r\"C:\\Users\\Amzad\\Desktop\\keras_project\\image_classification\\prediction\"\n\nload_model_path=r'C:\\Users\\Amzad\\Desktop\\keras_project\\image_classification\\project\\logs\\weights\\weights_of_vgg19_date_2022-04-24_15-37-55.hdf5'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-19T21:22:03.333876Z","iopub.execute_input":"2022-08-19T21:22:03.334511Z","iopub.status.idle":"2022-08-19T21:22:09.175376Z","shell.execute_reply.started":"2022-08-19T21:22:03.334417Z","shell.execute_reply":"2022-08-19T21:22:09.174339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi ","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:09.177332Z","iopub.execute_input":"2022-08-19T21:22:09.178017Z","iopub.status.idle":"2022-08-19T21:22:10.219954Z","shell.execute_reply.started":"2022-08-19T21:22:09.177977Z","shell.execute_reply":"2022-08-19T21:22:10.218776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications.mobilenet_v2 import MobileNetV2\nfrom tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.applications.mobilenet import MobileNet\nfrom tensorflow.keras.applications.densenet import DenseNet121\nimport tensorflow as tf\nfrom tensorflow.keras import layers\n\n\ndef model_chg(model_name=model_name,transfer_learing=True,height=height,width=width,channels=channels,classes=num_classes):\n    if model_name==\"inception_v3\":\n        print(f'model_name :{model_name}')\n        print(f\"number of epochs :{epochs}\")\n        print(f\"batch size :{batch_size}\")\n        print(f\"image height :{height} width :{width} channels :{channels}\")\n        if transfer_learing:\n            print(\"transfer_learning : True \")\n            main_model=InceptionV3(input_shape=(height,width,channels))\n            main_model.layers.pop()\n            model=tf.keras.Sequential()\n            for layer in main_model.layers:\n                model.add(layer)\n            for layer in main_model.layers:\n                layer.trainable=False\n            model.add(layers.Dense(num_classes,activation=\"softmax\"))\n        \n        else:\n            model = InceptionV3(weights=None,input_shape=(height,width,channels),classes=classes)  \n        return model\n\n    elif model_name==\"resnet50\": \n        model = ResNet50(weights=None,input_shape=(height,width,channels),classes=classes)  \n        return model\n\n\n\n\n\n    elif model_name==\"vgg16\":\n\n        print(f'model_name :{model_name}')\n        print(f\"number of epochs :{epochs}\")\n        print(f\"batch size :{batch_size}\")\n        print(f\"image height :{height} width :{width} channels :{channels}\")\n        if transfer_learing: \n            main_model=VGG16(input_shape=(height,width,channels))\n            main_model.layers.pop()\n            model = tf.keras.Sequential()\n            for layer in main_model.layers:\n                model.add(layer)\n            for layer in main_model.layers:\n                layer.trainable=False\n            model.add(layers.Dense(num_classes, activation='softmax'))\n        else:\n            model = VGG16(weights=None ,input_shape=(height,width,channels),classes=classes)\n        return model\n\n    elif model_name==\"vgg19\":\n        print(f'model_name :{model_name}')\n        print(f\"number of epochs :{epochs}\")\n        print(f\"batch size :{batch_size}\")\n        print(f\"image height :{height} width :{width} channels :{channels}\")\n        if transfer_learing:\n            main_model=VGG19(input_shape=(height,width,channels))\n            main_model.layers.pop()\n            model=tf.keras.Sequential()\n            for layer in main_model.layers:\n                model.add(layer)\n            for layer in main_model.layers:\n                layer.trainable=False\n            model.add(layers.Dense(num_classes,activation=\"softmax\"))\n        else:\n            model = VGG19(weights=None ,input_shape=(height,width,channels),classes=classes)\n        return model\n\n\n\n    elif model_name==\"mobilenet_v2\":\n        print(f'model_name :{model_name}')\n        print(f\"number of epochs :{epochs}\")\n        print(f\"batch size :{batch_size}\")\n        print(f\"image height :{height} width :{width} channels :{channels}\")\n        if transfer_learing:\n            print(\"transfer_learning : True \")\n            main_model=MobileNet(input_shape=(height,width,channels))\n            x=main_model.layers[-6].output\n            predictions=layers.Dense(num_classes,activation=\"softmax\")(x)\n            model=tf.keras.Model(inputs=main_model.input,outputs=predictions)\n            for layer in main_model.layers[:-5]:\n                layer.trainable=False\n        else:\n            model = MobileNetV2( weights=None, input_shape=(height,width,channels),classes=classes)\n        return model\n\n\n    elif model_name==\"densenet121\":\n        model = DenseNet121( weights=None, input_shape=(height,width,channels),classes=classes)\n        return model\n    elif model_name==\"inception_resnet_v2\":\n        model = InceptionResNetV2( weights=None, input_shape=(height,width,channels),classes=classes)\n        return model","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:10.222109Z","iopub.execute_input":"2022-08-19T21:22:10.222534Z","iopub.status.idle":"2022-08-19T21:22:10.248468Z","shell.execute_reply.started":"2022-08-19T21:22:10.222493Z","shell.execute_reply":"2022-08-19T21:22:10.247434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_dir(path='../input/mayo-clinic-strip-ai/train/'):\n    dirs=[]\n    for i in os.listdir(path):\n        lists=os.path.join(path,i)\n        dirs.append(lists)\n    return dirs","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:10.251537Z","iopub.execute_input":"2022-08-19T21:22:10.251976Z","iopub.status.idle":"2022-08-19T21:22:10.261791Z","shell.execute_reply.started":"2022-08-19T21:22:10.251943Z","shell.execute_reply":"2022-08-19T21:22:10.260839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \ntest= make_dir()\n\nkey=1\nbatch_idx = test[32*key:32*(key+1)]\nprint(batch_idx)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:10.263209Z","iopub.execute_input":"2022-08-19T21:22:10.263705Z","iopub.status.idle":"2022-08-19T21:22:10.322884Z","shell.execute_reply.started":"2022-08-19T21:22:10.263671Z","shell.execute_reply":"2022-08-19T21:22:10.322027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndef read_csv(path='../input/mayo-clinic-strip-ai/train.csv'):\n    \"\"\"\n    Reads the csv file and returns the data in a pandas dataframe.\n    :param path: The path to the csv file.\n    :return: The data in the csv file.\n    \"\"\"\n    return pd.read_csv(path)    \n","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:10.324209Z","iopub.execute_input":"2022-08-19T21:22:10.324565Z","iopub.status.idle":"2022-08-19T21:22:10.331241Z","shell.execute_reply.started":"2022-08-19T21:22:10.324533Z","shell.execute_reply":"2022-08-19T21:22:10.329896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lable_list=[]\n\ncsv=read_csv()\nfor i in csv['label']:\n    lable_list.append(i)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:10.332808Z","iopub.execute_input":"2022-08-19T21:22:10.333557Z","iopub.status.idle":"2022-08-19T21:22:10.357363Z","shell.execute_reply.started":"2022-08-19T21:22:10.333524Z","shell.execute_reply":"2022-08-19T21:22:10.356531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class dataload(keras.utils.Sequence):\n    def __init__(self,batch_size,img_paths,meta_data,image_shape):\n        self.batch_size = batch_size\n        self.img_paths = img_paths\n        self.meta_data= meta_data\n        self.image_shape = image_shape\n        \n    def __len__(self):\n        return math.ceil(len(self.img_paths)/self.batch_size)\n    \n\n    def __getitem__(self, idx):\n        i = idx * self.batch_size\n        batch_idx = self.img_paths[i:i+self.batch_size] \n        lable_batch = self.meta_data[i:i+self.batch_size]\n        x = []\n        y = []\n        for i in range(self.batch_size):\n            #print(path)\n            img = keras.preprocessing.image.load_img(batch_idx[i], target_size=self.image_shape)\n            img = img.resize(self.image_shape)\n            x.append(np.array(img)//255)\n\n            if lable_batch[i]== \"CE\":\n                y.append(0)\n            else: \n                y.append(1)\n            \n        y=to_categorical(y, num_classes = 2)\n\n        \n        #return tf.convert_to_tensor(x), y\n        return np.array(x).astype('uint8'), y ","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:10.360308Z","iopub.execute_input":"2022-08-19T21:22:10.360734Z","iopub.status.idle":"2022-08-19T21:22:10.371998Z","shell.execute_reply.started":"2022-08-19T21:22:10.360709Z","shell.execute_reply":"2022-08-19T21:22:10.371064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nos.environ[\"CUDA_VISIBLE_DEVICES\"]=\"0\"\n\nmodel=model_chg(model_name=\"mobilenet_v2\",transfer_learing=False,height=height,width=width,channels=channels,classes=num_classes)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:10.373447Z","iopub.execute_input":"2022-08-19T21:22:10.373945Z","iopub.status.idle":"2022-08-19T21:22:14.293484Z","shell.execute_reply.started":"2022-08-19T21:22:10.373911Z","shell.execute_reply":"2022-08-19T21:22:14.292442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:14.297733Z","iopub.execute_input":"2022-08-19T21:22:14.298627Z","iopub.status.idle":"2022-08-19T21:22:14.323691Z","shell.execute_reply.started":"2022-08-19T21:22:14.298588Z","shell.execute_reply":"2022-08-19T21:22:14.322582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img=test[:int(len(test)*0.8)]\ntrain_lable=lable_list[:int(len(test)*0.8)]\nval_img=test[int(len(test)*0.8):]\nval_lable=lable_list[int(len(test)*0.8):]                             \n\ntrain_ds = dataload(1,train_img,train_lable,(224,224))\nval_ds= dataload(1,val_img,val_lable,(224,224))\nprint('done')\nprint(train_lable[10])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:14.32517Z","iopub.execute_input":"2022-08-19T21:22:14.325814Z","iopub.status.idle":"2022-08-19T21:22:14.334309Z","shell.execute_reply.started":"2022-08-19T21:22:14.325775Z","shell.execute_reply":"2022-08-19T21:22:14.333236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nbatch=train_ds[0]\nplt.imshow(batch[0][0])\nprint(batch[1])","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:14.335859Z","iopub.execute_input":"2022-08-19T21:22:14.336363Z","iopub.status.idle":"2022-08-19T21:22:30.443876Z","shell.execute_reply.started":"2022-08-19T21:22:14.336306Z","shell.execute_reply":"2022-08-19T21:22:30.442839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stoping=keras.callbacks.EarlyStopping(monitor=\"val_loss\",patience=5)\n\n\n\n\ncallback = [\n    early_stoping\n]\nmodel.compile(\n    optimizer=keras.optimizers.Adam(1e-3),\n    loss=\"binary_crossentropy\",\n    metrics=[\"accuracy\",keras.metrics.Precision()],\n)\n\nhistory=model.fit(\n    train_ds, epochs=epochs, callbacks=callback, validation_data=val_ds,\n)\nprint(done)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T21:22:30.445204Z","iopub.execute_input":"2022-08-19T21:22:30.445672Z"},"trusted":true},"execution_count":null,"outputs":[]}]}