{"cells":[{"metadata":{},"cell_type":"markdown","source":"This code in written with the help of PyTorch Image Segmentation Demo:\n\nhttps://learnopencv.com/pytorch-for-beginners-semantic-segmentation-using-torchvision/"},{"metadata":{},"cell_type":"markdown","source":"# Steps\n\n1. reading CSV file\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\ntrain_csv_path = '../input/siim-isic-melanoma-classification/train.csv'\njpeg_dir = '../input/siim-isic-melanoma-classification/jpeg/train'\ntrain_df = pd.read_csv(train_csv_path)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"2. Reading Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\n\nrow = train_df.iloc[5]\nprint(row)\nimg = Image.open(f\"{jpeg_dir}/{row[0]}.jpg\")\nplt.imshow(img); plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"3. Load NN and declare transformations"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Models and transformations\nimport torchvision.transforms as T\nfrom torchvision import models\nimport torch\nimport torch.nn as nn\n\ntrf = T.Compose([T.Resize(512),\n                 T.CenterCrop(512),\n                 T.ToTensor(), \n                 T.Normalize(mean = [0.485, 0.456, 0.406], \n                             std = [0.229, 0.224, 0.225])])\n\nclass NullNet(nn.Module): \n    def __init__(self):\n        super(NullNet, self).__init__()\n    def forward(self, x):\n        return x\n    \n\ninp = trf(img).unsqueeze(0)    \nmodel_ft = models.resnet18(pretrained=True)\nmodel_ft.fc = NullNet()\n\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nmodel_ft = model_ft.to(device)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"4. Apply NN to single Image and observe"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Pass the input through the net\ninp = inp.to(device)\nout = model_ft(inp)\n\nprint (\"Input Shape:\",inp.shape)\nprint (\"Feature Shape:\",out.shape)\nprint (\"Length of Train Data:\", len(train_df))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"5. Saving Features to a CSV file"},{"metadata":{"trusted":true},"cell_type":"code","source":"import csv\nimport time\nimport numpy as np\n\nsince = time.time()\niter1=0\nfile = open('train_features.csv', 'a+', newline ='')\n# writing the data into the file \nwith file:     \n    write = csv.writer(file) \n    \n    for row in train_df.iloc:\n        img = Image.open(f\"{jpeg_dir}/{row[0]}.jpg\")\n        inp = trf(img).unsqueeze(0)\n        inp = inp.to(device)\n        out = model_ft(inp)\n\n        output_format = np.concatenate((  row ,(out[0,:].cpu().detach().numpy() )),axis=0)\n        write.writerows([output_format]) \n\n        iter1 = iter1 + 1\n\n        if iter1 % 2000 == 1999:\n            time_elapsed = time.time() - since\n            print('Time from start {:.0f}m {:.0f}s'.format(\n            time_elapsed // 60, time_elapsed % 60))\n            print('Percentage complete: {:4f}'.format(100*iter1/(len(train_df))))\n            #break # delete this break to save features from the entire dataset","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}