{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":30201,"databundleVersionId":2750748,"sourceType":"competition"}],"dockerImageVersionId":30140,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport random\nimport os\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import classification_report, log_loss, accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm","metadata":{"papermill":{"duration":7.037063,"end_time":"2021-05-14T07:53:28.134219","exception":false,"start_time":"2021-05-14T07:53:21.097156","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:07:30.163998Z","iopub.execute_input":"2024-12-06T15:07:30.164296Z","iopub.status.idle":"2024-12-06T15:07:35.936634Z","shell.execute_reply.started":"2024-12-06T15:07:30.164212Z","shell.execute_reply":"2024-12-06T15:07:35.936088Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_train_val_images'\ntest_dir = '../input/sartorius-cell-instance-segmentation/LIVECell_dataset_2021/images/livecell_test_images'","metadata":{"papermill":{"duration":0.027151,"end_time":"2021-05-14T07:53:28.180068","exception":false,"start_time":"2021-05-14T07:53:28.152917","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:07:35.937921Z","iopub.execute_input":"2024-12-06T15:07:35.938163Z","iopub.status.idle":"2024-12-06T15:07:35.941657Z","shell.execute_reply.started":"2024-12-06T15:07:35.938137Z","shell.execute_reply":"2024-12-06T15:07:35.941011Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Name=[]\nfor file in os.listdir(train_dir):\n    Name+=[file]\nprint(Name)\nprint(len(Name))","metadata":{"papermill":{"duration":0.047389,"end_time":"2021-05-14T07:53:28.24493","exception":false,"start_time":"2021-05-14T07:53:28.197541","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:07:35.942588Z","iopub.execute_input":"2024-12-06T15:07:35.942779Z","iopub.status.idle":"2024-12-06T15:07:35.984418Z","shell.execute_reply.started":"2024-12-06T15:07:35.942755Z","shell.execute_reply":"2024-12-06T15:07:35.983609Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"N=list(range(len(Name)))\nnormal_mapping=dict(zip(Name,N)) \nreverse_mapping=dict(zip(N,Name)) ","metadata":{"papermill":{"duration":0.02811,"end_time":"2021-05-14T07:53:28.291373","exception":false,"start_time":"2021-05-14T07:53:28.263263","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:07:35.985735Z","iopub.execute_input":"2024-12-06T15:07:35.985938Z","iopub.status.idle":"2024-12-06T15:07:35.98983Z","shell.execute_reply.started":"2024-12-06T15:07:35.985913Z","shell.execute_reply":"2024-12-06T15:07:35.989168Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainset=[]\ntrainname=[]\nfor name in tqdm(Name):\n    path=os.path.join(train_dir,name)\n    for im in os.listdir(path):\n        image=load_img(os.path.join(path,im), grayscale=False, color_mode='rgb', target_size=(150,150))\n        image=img_to_array(image)\n        image=image/255.0\n        trainset+=[image]\n        trainname+=[name]","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","papermill":{"duration":53.625106,"end_time":"2021-05-14T07:54:21.934401","exception":false,"start_time":"2021-05-14T07:53:28.309295","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:07:35.99091Z","iopub.execute_input":"2024-12-06T15:07:35.99117Z","iopub.status.idle":"2024-12-06T15:08:52.769439Z","shell.execute_reply.started":"2024-12-06T15:07:35.991137Z","shell.execute_reply":"2024-12-06T15:08:52.76875Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"testset=[]\ntestname=[]\nfor name in tqdm(Name):\n    path=os.path.join(test_dir,name)\n    for im in os.listdir(path):\n        image=load_img(os.path.join(path,im), grayscale=False, color_mode='rgb', target_size=(150,150))\n        image=img_to_array(image)\n        image=image/255.0\n        testset+=[image]\n        testname+=[name]","metadata":{"execution":{"iopub.status.busy":"2024-12-06T15:08:52.770578Z","iopub.execute_input":"2024-12-06T15:08:52.771106Z","iopub.status.idle":"2024-12-06T15:09:23.469179Z","shell.execute_reply.started":"2024-12-06T15:08:52.771064Z","shell.execute_reply":"2024-12-06T15:09:23.46849Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n=len(trainset)\nN=list(range(n))\nrandom.seed(2021)\nrandom.shuffle(N)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T15:09:23.470299Z","iopub.execute_input":"2024-12-06T15:09:23.470545Z","iopub.status.idle":"2024-12-06T15:09:23.47737Z","shell.execute_reply.started":"2024-12-06T15:09:23.47051Z","shell.execute_reply":"2024-12-06T15:09:23.476603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axs = plt.subplots(3,3,figsize=(15,15))\nfor i in range(9):\n    r=i//3\n    c=i%3\n    img1=trainset[N[i]]\n    name=trainname[N[i]]\n    ax=axs[r][c].axis(\"off\")\n    ax=axs[r][c].set_title(name)\n    ax=axs[r][c].imshow(img1)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-06T15:09:23.478489Z","iopub.execute_input":"2024-12-06T15:09:23.478743Z","iopub.status.idle":"2024-12-06T15:09:24.405399Z","shell.execute_reply.started":"2024-12-06T15:09:23.478709Z","shell.execute_reply":"2024-12-06T15:09:24.404682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainX=np.array(trainset)\ntestX=np.array(testset)","metadata":{"papermill":{"duration":0.054332,"end_time":"2021-05-14T07:54:22.032223","exception":false,"start_time":"2021-05-14T07:54:21.977891","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:24.406396Z","iopub.execute_input":"2024-12-06T15:09:24.406632Z","iopub.status.idle":"2024-12-06T15:09:24.869416Z","shell.execute_reply.started":"2024-12-06T15:09:24.406605Z","shell.execute_reply":"2024-12-06T15:09:24.868834Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainY0=pd.Series(trainname).map(normal_mapping)\ntestY0=pd.Series(testname).map(normal_mapping)","metadata":{"execution":{"iopub.status.busy":"2024-12-06T15:09:24.871374Z","iopub.execute_input":"2024-12-06T15:09:24.871596Z","iopub.status.idle":"2024-12-06T15:09:24.885251Z","shell.execute_reply.started":"2024-12-06T15:09:24.871565Z","shell.execute_reply":"2024-12-06T15:09:24.884625Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainY1=to_categorical(trainY0)\ntrainY=np.array(trainY1)","metadata":{"papermill":{"duration":0.054953,"end_time":"2021-05-14T07:54:22.130465","exception":false,"start_time":"2021-05-14T07:54:22.075512","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:24.88617Z","iopub.execute_input":"2024-12-06T15:09:24.886365Z","iopub.status.idle":"2024-12-06T15:09:24.895511Z","shell.execute_reply.started":"2024-12-06T15:09:24.886342Z","shell.execute_reply":"2024-12-06T15:09:24.895008Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainx,testx,trainy,testy=train_test_split(trainX,trainY,test_size=0.2,random_state=44)","metadata":{"papermill":{"duration":0.080595,"end_time":"2021-05-14T07:54:22.381291","exception":false,"start_time":"2021-05-14T07:54:22.300696","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:24.896355Z","iopub.execute_input":"2024-12-06T15:09:24.896536Z","iopub.status.idle":"2024-12-06T15:09:25.228352Z","shell.execute_reply.started":"2024-12-06T15:09:24.896511Z","shell.execute_reply":"2024-12-06T15:09:25.227769Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(trainx.shape)\nprint(testx.shape)\nprint(trainy.shape)\nprint(testy.shape)","metadata":{"papermill":{"duration":0.057085,"end_time":"2021-05-14T07:54:22.483015","exception":false,"start_time":"2021-05-14T07:54:22.42593","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:25.22944Z","iopub.execute_input":"2024-12-06T15:09:25.229704Z","iopub.status.idle":"2024-12-06T15:09:25.236112Z","shell.execute_reply.started":"2024-12-06T15:09:25.229671Z","shell.execute_reply":"2024-12-06T15:09:25.235323Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(horizontal_flip=True,vertical_flip=True,rotation_range=20,zoom_range=0.5,\n                        width_shift_range=0.2,height_shift_range=0.2,shear_range=0.1,fill_mode=\"nearest\")","metadata":{"papermill":{"duration":0.053275,"end_time":"2021-05-14T07:54:22.581817","exception":false,"start_time":"2021-05-14T07:54:22.528542","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:25.237004Z","iopub.execute_input":"2024-12-06T15:09:25.237188Z","iopub.status.idle":"2024-12-06T15:09:25.249036Z","shell.execute_reply.started":"2024-12-06T15:09:25.237168Z","shell.execute_reply":"2024-12-06T15:09:25.248356Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pretrained_model3 = tf.keras.applications.DenseNet201(input_shape=(150,150,3),include_top=False,weights='imagenet',pooling='avg')\npretrained_model3.trainable = False","metadata":{"papermill":{"duration":6.210104,"end_time":"2021-05-14T07:54:28.836468","exception":false,"start_time":"2021-05-14T07:54:22.626364","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:25.249924Z","iopub.execute_input":"2024-12-06T15:09:25.250136Z","iopub.status.idle":"2024-12-06T15:09:32.11581Z","shell.execute_reply.started":"2024-12-06T15:09:25.250113Z","shell.execute_reply":"2024-12-06T15:09:32.115149Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs3 = pretrained_model3.input\nx3 = tf.keras.layers.Dense(128, activation='relu')(pretrained_model3.output)\noutputs3 = tf.keras.layers.Dense(len(Name), activation='softmax')(x3)\nmodel = tf.keras.Model(inputs=inputs3, outputs=outputs3)","metadata":{"papermill":{"duration":0.131823,"end_time":"2021-05-14T07:54:29.017695","exception":false,"start_time":"2021-05-14T07:54:28.885872","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:32.116926Z","iopub.execute_input":"2024-12-06T15:09:32.117207Z","iopub.status.idle":"2024-12-06T15:09:32.36528Z","shell.execute_reply.started":"2024-12-06T15:09:32.117174Z","shell.execute_reply":"2024-12-06T15:09:32.364678Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"papermill":{"duration":0.086122,"end_time":"2021-05-14T07:54:29.153177","exception":false,"start_time":"2021-05-14T07:54:29.067055","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:32.366196Z","iopub.execute_input":"2024-12-06T15:09:32.366364Z","iopub.status.idle":"2024-12-06T15:09:32.385125Z","shell.execute_reply.started":"2024-12-06T15:09:32.366344Z","shell.execute_reply":"2024-12-06T15:09:32.384364Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"his=model.fit(datagen.flow(trainx,trainy,batch_size=32),validation_data=(testx,testy),epochs=50)","metadata":{"papermill":{"duration":889.435014,"end_time":"2021-05-14T08:09:18.637498","exception":false,"start_time":"2021-05-14T07:54:29.202484","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:09:32.386171Z","iopub.execute_input":"2024-12-06T15:09:32.386494Z","iopub.status.idle":"2024-12-06T15:24:02.476201Z","shell.execute_reply.started":"2024-12-06T15:09:32.386457Z","shell.execute_reply":"2024-12-06T15:24:02.475443Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=model.predict(testx)\npred=np.argmax(y_pred,axis=1)\nground = np.argmax(testy,axis=1)\nprint(classification_report(ground,pred))","metadata":{"papermill":{"duration":8.868209,"end_time":"2021-05-14T08:09:28.841115","exception":false,"start_time":"2021-05-14T08:09:19.972906","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:24:02.484947Z","iopub.execute_input":"2024-12-06T15:24:02.485216Z","iopub.status.idle":"2024-12-06T15:24:06.702733Z","shell.execute_reply.started":"2024-12-06T15:24:02.485187Z","shell.execute_reply":"2024-12-06T15:24:06.701918Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"get_acc = his.history['accuracy']\nvalue_acc = his.history['val_accuracy']\nget_loss = his.history['loss']\nvalidation_loss = his.history['val_loss']\n\nepochs = range(len(get_acc))\nplt.plot(epochs, get_acc, 'r', label='Accuracy of Training data')\nplt.plot(epochs, value_acc, 'b', label='Accuracy of Validation data')\nplt.title('Training vs validation accuracy')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":1.51516,"end_time":"2021-05-14T08:09:31.647806","exception":false,"start_time":"2021-05-14T08:09:30.132646","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:24:06.703698Z","iopub.execute_input":"2024-12-06T15:24:06.703893Z","iopub.status.idle":"2024-12-06T15:24:06.932378Z","shell.execute_reply.started":"2024-12-06T15:24:06.70387Z","shell.execute_reply":"2024-12-06T15:24:06.931659Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"epochs = range(len(get_loss))\nplt.plot(epochs, get_loss, 'r', label='Loss of Training data')\nplt.plot(epochs, validation_loss, 'b', label='Loss of Validation data')\nplt.title('Training vs validation loss')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":1.457879,"end_time":"2021-05-14T08:09:34.409275","exception":false,"start_time":"2021-05-14T08:09:32.951396","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:24:06.933527Z","iopub.execute_input":"2024-12-06T15:24:06.934066Z","iopub.status.idle":"2024-12-06T15:24:07.152788Z","shell.execute_reply.started":"2024-12-06T15:24:06.934025Z","shell.execute_reply":"2024-12-06T15:24:07.152195Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred2=model.predict(testX)","metadata":{"papermill":{"duration":5.736929,"end_time":"2021-05-14T08:09:53.376946","exception":false,"start_time":"2021-05-14T08:09:47.640017","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:24:07.153764Z","iopub.execute_input":"2024-12-06T15:24:07.153957Z","iopub.status.idle":"2024-12-06T15:24:12.903153Z","shell.execute_reply.started":"2024-12-06T15:24:07.153933Z","shell.execute_reply":"2024-12-06T15:24:12.902523Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PRED=[]\nfor item in pred2:\n    value2=np.argmax(item)\n    PRED+=[value2]\nprint(PRED[0:10])\n\nANS=list(testY0)\nprint(ANS[0:10])","metadata":{"papermill":{"duration":1.309919,"end_time":"2021-05-14T08:09:55.990265","exception":false,"start_time":"2021-05-14T08:09:54.680346","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:24:12.904368Z","iopub.execute_input":"2024-12-06T15:24:12.90457Z","iopub.status.idle":"2024-12-06T15:24:12.914096Z","shell.execute_reply.started":"2024-12-06T15:24:12.904545Z","shell.execute_reply":"2024-12-06T15:24:12.913399Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy=accuracy_score(ANS,PRED)\nprint(accuracy)","metadata":{"papermill":{"duration":1.393392,"end_time":"2021-05-14T08:09:58.682758","exception":false,"start_time":"2021-05-14T08:09:57.289366","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-12-06T15:24:12.91492Z","iopub.execute_input":"2024-12-06T15:24:12.915153Z","iopub.status.idle":"2024-12-06T15:24:12.926012Z","shell.execute_reply.started":"2024-12-06T15:24:12.915115Z","shell.execute_reply":"2024-12-06T15:24:12.925324Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":1.30393,"end_time":"2021-05-14T08:10:01.287258","exception":false,"start_time":"2021-05-14T08:09:59.983328","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null}]}