{"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":"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":"2021-10-30T14:20:11.852223Z","iopub.execute_input":"2021-10-30T14:20:11.852572Z","iopub.status.idle":"2021-10-30T14:20:11.860336Z","shell.execute_reply.started":"2021-10-30T14:20:11.852512Z","shell.execute_reply":"2021-10-30T14:20:11.85913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:20:11.862376Z","iopub.execute_input":"2021-10-30T14:20:11.862622Z","iopub.status.idle":"2021-10-30T14:20:11.875242Z","shell.execute_reply.started":"2021-10-30T14:20:11.862594Z","shell.execute_reply":"2021-10-30T14:20:11.874505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:20:11.87643Z","iopub.execute_input":"2021-10-30T14:20:11.876785Z","iopub.status.idle":"2021-10-30T14:20:11.897563Z","shell.execute_reply.started":"2021-10-30T14:20:11.876754Z","shell.execute_reply":"2021-10-30T14:20:11.896284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:20:11.899908Z","iopub.execute_input":"2021-10-30T14:20:11.900983Z","iopub.status.idle":"2021-10-30T14:20:11.906655Z","shell.execute_reply.started":"2021-10-30T14:20:11.900929Z","shell.execute_reply":"2021-10-30T14:20:11.90553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:20:11.908139Z","iopub.execute_input":"2021-10-30T14:20:11.908584Z","iopub.status.idle":"2021-10-30T14:20:51.197148Z","shell.execute_reply.started":"2021-10-30T14:20:11.90855Z","shell.execute_reply":"2021-10-30T14:20:51.195768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:20:51.1987Z","iopub.execute_input":"2021-10-30T14:20:51.198954Z","iopub.status.idle":"2021-10-30T14:21:07.211839Z","shell.execute_reply.started":"2021-10-30T14:20:51.198925Z","shell.execute_reply":"2021-10-30T14:21:07.210864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n=len(trainset)\nN=list(range(n))\nrandom.seed(2021)\nrandom.shuffle(N)","metadata":{},"execution_count":null,"outputs":[]},{"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":{},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:07.213096Z","iopub.execute_input":"2021-10-30T14:21:07.213365Z","iopub.status.idle":"2021-10-30T14:21:07.283182Z","shell.execute_reply.started":"2021-10-30T14:21:07.213332Z","shell.execute_reply":"2021-10-30T14:21:07.281985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainY0=pd.Series(trainname).map(normal_mapping)\ntestY0=pd.Series(testname).map(normal_mapping)","metadata":{"execution":{"iopub.status.busy":"2021-10-30T14:21:07.28456Z","iopub.execute_input":"2021-10-30T14:21:07.284833Z","iopub.status.idle":"2021-10-30T14:21:07.294785Z","shell.execute_reply.started":"2021-10-30T14:21:07.2848Z","shell.execute_reply":"2021-10-30T14:21:07.293182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:07.296408Z","iopub.execute_input":"2021-10-30T14:21:07.296664Z","iopub.status.idle":"2021-10-30T14:21:07.309102Z","shell.execute_reply.started":"2021-10-30T14:21:07.296637Z","shell.execute_reply":"2021-10-30T14:21:07.308296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:07.312251Z","iopub.execute_input":"2021-10-30T14:21:07.312811Z","iopub.status.idle":"2021-10-30T14:21:07.353573Z","shell.execute_reply.started":"2021-10-30T14:21:07.312764Z","shell.execute_reply":"2021-10-30T14:21:07.352811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:07.354986Z","iopub.execute_input":"2021-10-30T14:21:07.355267Z","iopub.status.idle":"2021-10-30T14:21:07.361928Z","shell.execute_reply.started":"2021-10-30T14:21:07.355233Z","shell.execute_reply":"2021-10-30T14:21:07.360723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:07.363891Z","iopub.execute_input":"2021-10-30T14:21:07.364371Z","iopub.status.idle":"2021-10-30T14:21:07.376725Z","shell.execute_reply.started":"2021-10-30T14:21:07.364335Z","shell.execute_reply":"2021-10-30T14:21:07.375807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:07.378072Z","iopub.execute_input":"2021-10-30T14:21:07.37891Z","iopub.status.idle":"2021-10-30T14:21:12.876292Z","shell.execute_reply.started":"2021-10-30T14:21:07.378862Z","shell.execute_reply":"2021-10-30T14:21:12.87528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:12.877805Z","iopub.execute_input":"2021-10-30T14:21:12.878142Z","iopub.status.idle":"2021-10-30T14:21:12.956023Z","shell.execute_reply.started":"2021-10-30T14:21:12.878098Z","shell.execute_reply":"2021-10-30T14:21:12.954914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:12.957332Z","iopub.execute_input":"2021-10-30T14:21:12.957829Z","iopub.status.idle":"2021-10-30T14:21:12.986157Z","shell.execute_reply.started":"2021-10-30T14:21:12.95778Z","shell.execute_reply":"2021-10-30T14:21:12.984846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:21:12.987744Z","iopub.execute_input":"2021-10-30T14:21:12.988001Z","iopub.status.idle":"2021-10-30T14:22:22.266551Z","shell.execute_reply.started":"2021-10-30T14:21:12.987972Z","shell.execute_reply":"2021-10-30T14:22:22.265585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:22:22.268176Z","iopub.execute_input":"2021-10-30T14:22:22.268745Z","iopub.status.idle":"2021-10-30T14:22:31.270262Z","shell.execute_reply.started":"2021-10-30T14:22:22.268695Z","shell.execute_reply":"2021-10-30T14:22:31.269557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:22:31.271426Z","iopub.execute_input":"2021-10-30T14:22:31.272017Z","iopub.status.idle":"2021-10-30T14:22:31.478457Z","shell.execute_reply.started":"2021-10-30T14:22:31.271982Z","shell.execute_reply":"2021-10-30T14:22:31.477582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:22:31.480064Z","iopub.execute_input":"2021-10-30T14:22:31.480324Z","iopub.status.idle":"2021-10-30T14:22:31.715772Z","shell.execute_reply.started":"2021-10-30T14:22:31.480294Z","shell.execute_reply":"2021-10-30T14:22:31.714654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:22:31.717486Z","iopub.execute_input":"2021-10-30T14:22:31.718099Z","iopub.status.idle":"2021-10-30T14:22:45.42968Z","shell.execute_reply.started":"2021-10-30T14:22:31.718047Z","shell.execute_reply":"2021-10-30T14:22:45.42868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:25:49.069177Z","iopub.execute_input":"2021-10-30T14:25:49.069836Z","iopub.status.idle":"2021-10-30T14:25:49.085951Z","shell.execute_reply.started":"2021-10-30T14:25:49.069779Z","shell.execute_reply":"2021-10-30T14:25:49.084775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2021-10-30T14:22:45.438602Z","iopub.execute_input":"2021-10-30T14:22:45.438925Z","iopub.status.idle":"2021-10-30T14:22:45.456456Z","shell.execute_reply.started":"2021-10-30T14:22:45.438879Z","shell.execute_reply":"2021-10-30T14:22:45.455536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"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":[]},"execution_count":null,"outputs":[]}]}