{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport pydicom\nimport os\nimport zipfile\nimport matplotlib.pyplot as plt\nimport pylab\nimport png\nimport keras\nfrom tqdm import tqdm\nfrom keras.datasets import mnist\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D, Activation, LeakyReLU\nfrom matplotlib.patches import Rectangle\ndatapath = \"../input/\"\nprint(os.listdir(datapath))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"693b3947e39a718bf9d1cdc34d17cf39701181fe"},"cell_type":"code","source":"box = pd.read_csv(datapath+\"stage_1_train_labels.csv\")\npIds = box['patientId']\naux = pd.read_csv(datapath+\"stage_1_detailed_class_info.csv\")\ntrain = pd.concat([box.loc[1:20293], aux.loc[1:20293].drop(labels=['patientId'], axis=1)], axis=1)\nvalid = pd.concat([box.loc[20294:28989], aux.loc[20294:28989].drop(labels=['patientId'], axis=1)], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cac8e76cbd3b9b7c92105be8c24b41129ffe4d0b"},"cell_type":"code","source":"def visBox(pId):\n    \"\"\"\n    Method to visualize boxes around lung opacities in training set\n    Takes an entry from the parsed dict\n    \"\"\"\n    dcmdata = pydicom.read_file(datapath+'stage_1_train_images/'+pId+'.dcm')\n    dcmimg = dcmdata.pixel_array\n    boxData = train[train['patientId'] == pId]\n    plt.figure(figsize=(20,10))\n    for i in boxData.index:\n        rect = Rectangle((boxData['x'][i],boxData['y'][i]),boxData['width'][i],boxData['height'][i],fill=False,color='red')\n        plt.gca().add_patch(rect)\n    plt.axis('off')\n    plt.imshow(dcmimg, cmap=pylab.cm.binary)\n    plt.colorbar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"160428219766c961b42d341432963378b27fb063"},"cell_type":"code","source":"lstFileNames = []\ni=0\nfor filename in tqdm(os.listdir(datapath+'stage_1_train_images/')):\n    ds = pydicom.dcmread(datapath+'stage_1_train_images/'+filename)\n    shape = ds.pixel_array.shape\n    lstFileNames.append(filename.replace('.dcm',''))\n    # Convert to float to avoid overflow or underflow losses.\n    image_2d = ds.pixel_array.astype(float)\n    # Rescaling grey scale between 0-255\n    image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0\n    # Convert to uint\n    image_2d_scaled = np.uint8(image_2d_scaled)\n    # Write the PNG file\n    with open('../working/'+filename.replace('.dcm',''), 'wb+') as png_file:\n        w = png.Writer(shape[1], shape[0], greyscale=True)\n        w.write(png_file, image_2d_scaled)\n    i=i+1\n    if i==30:\n        with zipfile.ZipFile('imgs', 'w') as myzip:\n            for f in lstFileNames:   \n                myzip.write(f)\n                os.remove('../working/'+f)\n        i=0\n        lstFileNames=[]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8a77330f544570cb8a0877e8b766b017f5181c3"},"cell_type":"code","source":"trainBox = box.replace(to_replace=float('nan'),value=0)\ntrainBox['patientId'] = box['patientId'].astype(str)+'.dcm'\ntrain_datagen = keras.preprocessing.image.ImageDataGenerator(\n        rescale=1./255,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True)\ntrain_generator = train_datagen.flow_from_dataframe(\n        directory=datapath+'stage_1_train_images',\n        dataframe=trainBox,\n        x_col='patientId',\n        y_col=['x','y','width','height'], \n        has_ext=True,\n        class_mode='other',\n        target_size=(1024,1024),\n        batch_size=32)\nmodel = Sequential()\nmodel.add(Conv2D(32, (3, 3), padding='same',\n                 input_shape=(1024,1024,3)))\nmodel.add(Activation('relu'))\nmodel.add(Conv2D(32, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), padding='same'))\nmodel.add(Activation('relu'))\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))\nmodel.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer=keras.optimizers.Adam(lr=1000,decay=.99),\n              metrics=['accuracy'])\nmodel.fit_generator(train_generator, steps_per_epoch=1024/16, epochs=317)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6bbcf45aff774aa4363880c23f352a129a701c5"},"cell_type":"code","source":"trainBox = box.replace(to_replace=float('nan'),value=0)\ntrainBox['patientId'] = box['patientId'].astype(str)+'.dcm'\ntrainBox","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3880bd65179236ab844dcf21ee28a47f49e856f8","scrolled":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7282537d331cb7cd51b72c647967b383a1b1348d"},"cell_type":"code","source":"os.listdir('../working/')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}