{"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":"# Interesting error finding in X-ray images. Seem by visual inspection work pretty well. //Per Lindholm\n!pip install chainer","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:14:33.207923Z","iopub.execute_input":"2021-07-23T08:14:33.208376Z","iopub.status.idle":"2021-07-23T08:14:33.212189Z","shell.execute_reply.started":"2021-07-23T08:14:33.208334Z","shell.execute_reply":"2021-07-23T08:14:33.211408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import chainer\nimport chainer.functions as F\nimport chainer.links as L\nfrom chainer.backends.cuda import to_cpu,to_gpu\nimport numpy as np\nimport pylab as plt\nimport time\nimport h5py\nfrom sklearn.preprocessing import LabelBinarizer\nfrom skimage.draw import disk\nfrom skimage.transform import resize\nimport pandas as pd\nimport glob\nfrom skimage.io import imread\nimport pydicom\nfrom pydicom.filereader import dcmread","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-23T07:24:28.246094Z","iopub.execute_input":"2021-07-23T07:24:28.246453Z","iopub.status.idle":"2021-07-23T07:24:33.151174Z","shell.execute_reply.started":"2021-07-23T07:24:28.246385Z","shell.execute_reply":"2021-07-23T07:24:33.150081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install chainer","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:24:13.464715Z","iopub.execute_input":"2021-07-23T07:24:13.465093Z","iopub.status.idle":"2021-07-23T07:24:28.244717Z","shell.execute_reply.started":"2021-07-23T07:24:13.465059Z","shell.execute_reply":"2021-07-23T07:24:28.243754Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(data=pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')[['id','boxes']]).dropna()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:24:33.152739Z","iopub.execute_input":"2021-07-23T07:24:33.153042Z","iopub.status.idle":"2021-07-23T07:24:33.237692Z","shell.execute_reply.started":"2021-07-23T07:24:33.153013Z","shell.execute_reply":"2021-07-23T07:24:33.236649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = !find ../input/siim-covid19-detection/train/ -name *.dcm","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:24:33.239067Z","iopub.execute_input":"2021-07-23T07:24:33.239381Z","iopub.status.idle":"2021-07-23T07:25:00.365775Z","shell.execute_reply.started":"2021-07-23T07:24:33.239349Z","shell.execute_reply":"2021-07-23T07:25:00.364541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from time import sleep\ndataset = []\ni = 0\nwhile i<100:\n    sleep(0.01)\n    try:\n        dataset = dataset + [F.resize_images(np.expand_dims(np.expand_dims(dcmread(files[i]).pixel_array,axis=0),axis=0).astype(np.float32)/32768,(256,256)).array.reshape(256,256)]\n        print(i)\n    except:\n        i = i+1\n    i +=1\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:45:30.740085Z","iopub.execute_input":"2021-07-23T07:45:30.740466Z","iopub.status.idle":"2021-07-23T07:45:35.047534Z","shell.execute_reply.started":"2021-07-23T07:45:30.740411Z","shell.execute_reply":"2021-07-23T07:45:35.046264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dataset)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:45:37.948651Z","iopub.execute_input":"2021-07-23T07:45:37.949002Z","iopub.status.idle":"2021-07-23T07:45:37.955454Z","shell.execute_reply.started":"2021-07-23T07:45:37.948971Z","shell.execute_reply":"2021-07-23T07:45:37.954517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(dataset[30],cmap=plt.cm.gray)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:45:41.390555Z","iopub.execute_input":"2021-07-23T07:45:41.390915Z","iopub.status.idle":"2021-07-23T07:45:41.573057Z","shell.execute_reply.started":"2021-07-23T07:45:41.390886Z","shell.execute_reply":"2021-07-23T07:45:41.572254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0].max()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:30:45.400322Z","iopub.execute_input":"2021-07-23T07:30:45.400756Z","iopub.status.idle":"2021-07-23T07:30:45.407561Z","shell.execute_reply.started":"2021-07-23T07:30:45.400719Z","shell.execute_reply":"2021-07-23T07:30:45.406531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 20\nmask = []\nradius = 75\nrr = 300\nt = np.linspace(0,2*np.pi,N)\nfor i in range(N):\n    r = rr * np.cos(t[i]) + 28*28/2\n    c = rr * np.sin(t[i]) + 28*28/2\n    z = np.random.rand(784,784)\n    z[disk((r,c),radius)] = 1.0\n    mask.append(resize(z,(28,28),anti_aliasing=True))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:30:31.541133Z","iopub.execute_input":"2021-07-23T07:30:31.541522Z","iopub.status.idle":"2021-07-23T07:30:33.472298Z","shell.execute_reply.started":"2021-07-23T07:30:31.541491Z","shell.execute_reply":"2021-07-23T07:30:33.471542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np.sum(mask,axis=0))\nplt.show()\nplt.imshow(mask[1])\nplt.show()\nplt.imshow(mask[-1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:30:33.473831Z","iopub.execute_input":"2021-07-23T07:30:33.474427Z","iopub.status.idle":"2021-07-23T07:30:34.046764Z","shell.execute_reply.started":"2021-07-23T07:30:33.474379Z","shell.execute_reply":"2021-07-23T07:30:34.045776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model2(chainer.Chain):\n    def __init__(self,hidden,out):\n        super(Model2, self).__init__()\n        with self.init_scope():\n            self.l0 = L.Linear(None,28*28)\n            self.l1 = L.Linear(None,hidden)\n            self.l10 = L.Linear(None,out)\n            self.mask = np.copy(mask)\n            \n    def __call__(self, x):\n        x = F.moveaxis(F.stack([F.relu(self.l0(x))*self.mask[i].reshape(-1,28*28) for i in range(N)]),0,1)\n        x = F.relu(self.l1(x))\n        x = self.l10(x)\n        return x\n","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:30:52.060771Z","iopub.execute_input":"2021-07-23T07:30:52.061132Z","iopub.status.idle":"2021-07-23T07:30:52.070028Z","shell.execute_reply.started":"2021-07-23T07:30:52.061102Z","shell.execute_reply":"2021-07-23T07:30:52.06873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model2(300,1)\noptimizer = chainer.optimizers.SMORMS3()\noptimizer.setup(model)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:03:20.700413Z","iopub.execute_input":"2021-07-23T08:03:20.700798Z","iopub.status.idle":"2021-07-23T08:03:20.708928Z","shell.execute_reply.started":"2021-07-23T08:03:20.700765Z","shell.execute_reply":"2021-07-23T08:03:20.708209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lb = LabelBinarizer()\nlb.fit(np.arange(5))","metadata":{"execution":{"iopub.status.busy":"2021-07-23T07:30:53.070021Z","iopub.execute_input":"2021-07-23T07:30:53.070573Z","iopub.status.idle":"2021-07-23T07:30:53.078964Z","shell.execute_reply.started":"2021-07-23T07:30:53.07054Z","shell.execute_reply":"2021-07-23T07:30:53.077746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sol = []\nsoly = []","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:08:00.732546Z","iopub.execute_input":"2021-07-23T08:08:00.732957Z","iopub.status.idle":"2021-07-23T08:08:00.740349Z","shell.execute_reply.started":"2021-07-23T08:08:00.732924Z","shell.execute_reply":"2021-07-23T08:08:00.739391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predict easy predictabel vs hard to predict\n\ndef train(nn):\n    maxloss = 0\n    i = 0\n    while True:\n        if i>nn:break\n        \n        idx = np.random.randint(0,92)\n        ix = np.random.randint(64,256)\n        iy = np.random.randint(64,256)\n        \n        X = dataset[idx]\n        x = X[ix-64:ix,iy-64:iy].reshape(-1,1)\n        y = model(x)\n        \n        loss = F.mean_squared_error(y,x)\n        \n        if loss.array > 0.75*maxloss:\n            maxloss = loss.array\n            sol.append([idx,ix,iy])\n            soly.append([idx,y,x])\n        \n            \n        \n        model.cleargrads()\n        loss.backward()\n        optimizer.update()\n        \n        if i%10==0:\n            print(i,loss.item())\n        i +=1","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:08:34.307163Z","iopub.execute_input":"2021-07-23T08:08:34.307821Z","iopub.status.idle":"2021-07-23T08:08:34.317578Z","shell.execute_reply.started":"2021-07-23T08:08:34.307768Z","shell.execute_reply":"2021-07-23T08:08:34.316727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train(100)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:08:34.932916Z","iopub.execute_input":"2021-07-23T08:08:34.933551Z","iopub.status.idle":"2021-07-23T08:08:56.633269Z","shell.execute_reply.started":"2021-07-23T08:08:34.9335Z","shell.execute_reply":"2021-07-23T08:08:56.63159Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sol","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:09:00.702376Z","iopub.execute_input":"2021-07-23T08:09:00.703059Z","iopub.status.idle":"2021-07-23T08:09:00.712525Z","shell.execute_reply.started":"2021-07-23T08:09:00.703005Z","shell.execute_reply":"2021-07-23T08:09:00.711489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 2\nn,ix,iy = sol[N]\nprint(sol[N])\nplt.imshow(dataset[n],cmap=plt.cm.gray)\nplt.show()\nplt.imshow(dataset[n][ix-64:ix,iy-64:iy],cmap=plt.cm.gray)\nplt.show()\nplt.imshow(soly[N][1].reshape(64,64).array,cmap=plt.cm.gray)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:10:10.278915Z","iopub.execute_input":"2021-07-23T08:10:10.279285Z","iopub.status.idle":"2021-07-23T08:10:10.750804Z","shell.execute_reply.started":"2021-07-23T08:10:10.279254Z","shell.execute_reply":"2021-07-23T08:10:10.749632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}