{"cells":[{"metadata":{},"cell_type":"markdown","source":"### Pytorch EfficientNet Feature Extraction\nThis notebook demonstrate the usage of extracting features from the CT scan images by using EfficientNet(Pytorch implementation).\n\nIf you'd like to add the information of CT scan images into your Feature-Engineering process, you can follow the methodology of this notebook to obtain features from each patient’s images."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n\nimport cv2\nimport pydicom","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"! pip install efficientnet_pytorch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_img(path):\n    d = pydicom.dcmread(path)\n    # normlize and resize the image\n    return cv2.resize((d.pixel_array - d.pixel_array.min()) / (d.pixel_array.max() - d.pixel_array.min()), (512, 512))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ROOT = \"../input/osic-pulmonary-fibrosis-progression\"\nos.listdir(ROOT)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load a demo image\ndemo_img_path = os.path.join(ROOT, \"train\", \"ID00007637202177411956430\", \"1.dcm\")\ndemo_img = get_img(demo_img_path)\ndemo_img.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# get the efficientNet model\nfrom efficientnet_pytorch import EfficientNet\nmodel = EfficientNet.from_pretrained('efficientnet-b6')\nmodel.cuda()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\n# convert the image to the input tensor, the input must be a 3-channel tensor with float dtype\ndemo_img_ts = torch.from_numpy(demo_img).unsqueeze(0).unsqueeze(0).repeat(1, 3, 1, 1).to(dtype=torch.float).cuda()\ndemo_img_ts.size()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As you can see, when the input image is in shape (batch, 3, 512, 512). The feature tensor extracted by the efficientNet-b6 is in shape (batch, 2304, 16, 16). You can flatten this tensor or send it through a few more convolution layers to make it adapt to your model's input size.\n### NOTE\nAfter extraction, you might need to detach this tensor from the graph then send it into your model for fvc regression. Otherwise, you might fine-tune the weights of the efficientNet when you train your fvc regression model."},{"metadata":{"trusted":true},"cell_type":"code","source":"demo_feature = model.extract_features(demo_img_ts)\ndemo_feature.size()","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}