{"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\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-26T03:42:48.149097Z","iopub.execute_input":"2022-02-26T03:42:48.149469Z","iopub.status.idle":"2022-02-26T03:42:48.182275Z","shell.execute_reply.started":"2022-02-26T03:42:48.14939Z","shell.execute_reply":"2022-02-26T03:42:48.181488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nfrom PIL import Image\nfrom cv2 import imread\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:42:48.183861Z","iopub.execute_input":"2022-02-26T03:42:48.184121Z","iopub.status.idle":"2022-02-26T03:42:48.382541Z","shell.execute_reply.started":"2022-02-26T03:42:48.184089Z","shell.execute_reply":"2022-02-26T03:42:48.381832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q ../input/galaxy-zoo-the-galaxy-challenge/images_training_rev1.zip\n!unzip -q ../input/galaxy-zoo-the-galaxy-challenge/training_solutions_rev1.zip","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:42:48.383594Z","iopub.execute_input":"2022-02-26T03:42:48.383899Z","iopub.status.idle":"2022-02-26T03:42:51.091755Z","shell.execute_reply.started":"2022-02-26T03:42:48.383864Z","shell.execute_reply":"2022-02-26T03:42:51.090869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = pd.read_csv('training_solutions_rev1.csv')\nfiles = os.listdir('./images_training_rev1')\n\ntrain_set.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:42:53.339886Z","iopub.execute_input":"2022-02-26T03:42:53.340276Z","iopub.status.idle":"2022-02-26T03:42:53.778995Z","shell.execute_reply.started":"2022-02-26T03:42:53.340231Z","shell.execute_reply":"2022-02-26T03:42:53.778153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(1, figsize=(9, 9))\nplt.axis('off')\nn = 0\nfor i in range(16):\n  n += 1\n  random_img = './images_training_rev1/'+random.choice(files)\n  imgs = imread(random_img)\n  plt.subplot(4, 4, n)\n  plt.axis('off')\n  plt.imshow(imgs)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.615647Z","iopub.status.idle":"2022-02-25T18:54:33.616431Z","shell.execute_reply.started":"2022-02-25T18:54:33.6161Z","shell.execute_reply":"2022-02-25T18:54:33.616133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q torchsummary\n!pip install -q livelossplot","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:43:06.620263Z","iopub.execute_input":"2022-02-26T03:43:06.620698Z","iopub.status.idle":"2022-02-26T03:43:23.139602Z","shell.execute_reply.started":"2022-02-26T03:43:06.620663Z","shell.execute_reply":"2022-02-26T03:43:23.138741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom livelossplot import PlotLosses\nfrom torchsummary import summary\nfrom tqdm import tqdm\nimport scipy.stats as stats\nimport torchvision.models as models\nimport torch.optim as optim\nimport torch.nn.functional as F\nimport torch.nn as nn\nfrom torchvision import datasets, transforms\nimport torchmetrics\nfrom sklearn.metrics import mean_squared_error\nfrom torch.utils.data import DataLoader, random_split\nfrom torch.utils.data.sampler import SubsetRandomSampler","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:43:23.143366Z","iopub.execute_input":"2022-02-26T03:43:23.143601Z","iopub.status.idle":"2022-02-26T03:43:24.112467Z","shell.execute_reply.started":"2022-02-26T03:43:23.143572Z","shell.execute_reply":"2022-02-26T03:43:24.111635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(f'Device: {device}')","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:43:24.113751Z","iopub.execute_input":"2022-02-26T03:43:24.114073Z","iopub.status.idle":"2022-02-26T03:43:24.157901Z","shell.execute_reply.started":"2022-02-26T03:43:24.114034Z","shell.execute_reply":"2022-02-26T03:43:24.157187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder = './images_training_rev1'\ntest_folder = './images_test_rev1/'","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:43:24.159794Z","iopub.execute_input":"2022-02-26T03:43:24.160296Z","iopub.status.idle":"2022-02-26T03:43:24.168301Z","shell.execute_reply.started":"2022-02-26T03:43:24.160256Z","shell.execute_reply":"2022-02-26T03:43:24.167521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GalaxyDataSet(torch.utils.data.Dataset):\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\n    def __len__(self):\n        return len(self.x)\n\n    def __getitem__(self, index):\n        label = self.y[index]\n        image = self.x[index]\n        image = Image.fromarray((image * 255).astype(np.uint8))\n        image = transforms.ToTensor()(image)\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.627823Z","iopub.status.idle":"2022-02-25T18:54:33.628522Z","shell.execute_reply.started":"2022-02-25T18:54:33.628234Z","shell.execute_reply":"2022-02-25T18:54:33.628267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ndf_train, df_test = train_test_split(train_set, test_size=.2)\ndf_train.shape, df_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.629786Z","iopub.status.idle":"2022-02-25T18:54:33.630464Z","shell.execute_reply.started":"2022-02-25T18:54:33.630168Z","shell.execute_reply":"2022-02-25T18:54:33.630198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.transform import resize\n\nORIG_SHAPE = (424,424)\nCROP_SIZE = (256,256)\nIMG_SHAPE = (64,64)\n\ndef get_image(path, x1,y1, shape, crop_size):\n    x = plt.imread(path)\n    \n    x = x[x1:x1+crop_size[0], y1:y1+crop_size[1]]\n    x = resize(x, shape)\n    return x\n    \ndef get_all_images(dataframe, shape=IMG_SHAPE, crop_size=CROP_SIZE):\n    x1 = (ORIG_SHAPE[0]-CROP_SIZE[0])//2\n    y1 = (ORIG_SHAPE[1]-CROP_SIZE[1])//2\n   \n    sel = dataframe.values\n    ids = sel[:,0].astype(int).astype(str)\n    y_batch = sel[:,1:]\n    x_batch = []\n    for i in tqdm(ids):\n        x = get_image(train_folder+'/'+i+'.jpg', x1,y1, shape=shape, crop_size=crop_size)\n        x_batch.append(x)\n    x_batch = np.array(x_batch)\n    return x_batch, y_batch\n        \nX_train, y_train = get_all_images(df_train)\nX_test, y_test = get_all_images(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:46:28.020639Z","iopub.execute_input":"2022-02-26T03:46:28.020895Z","iopub.status.idle":"2022-02-26T03:46:28.310482Z","shell.execute_reply.started":"2022-02-26T03:46:28.020866Z","shell.execute_reply":"2022-02-26T03:46:28.309727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TrainDataSet = GalaxyDataSet(X_train, y_train)\nTestDataSet = GalaxyDataSet(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.633965Z","iopub.status.idle":"2022-02-25T18:54:33.634692Z","shell.execute_reply.started":"2022-02-25T18:54:33.634381Z","shell.execute_reply":"2022-02-25T18:54:33.634413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 1\n\ntrainloader = DataLoader(TrainDataSet, batch_size = batch_size)\nvalloader = DataLoader(TestDataSet, batch_size = batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.636121Z","iopub.status.idle":"2022-02-25T18:54:33.636834Z","shell.execute_reply.started":"2022-02-25T18:54:33.636532Z","shell.execute_reply":"2022-02-25T18:54:33.636562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GalaxyModel(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.sq = nn.Sequential(\n            nn.Conv2d(3, 512, (3,3)),\n            nn.Conv2d(512, 256, (3,3)),\n            nn.ReLU(),\n            nn.MaxPool2d(2,2),\n            nn.Conv2d(256, 256, (3,3)),\n            nn.Conv2d(256, 128, (3,3)),\n            nn.ReLU(),\n            nn.MaxPool2d(2,2),\n            nn.Conv2d(128, 128, (3,3)),\n            nn.Conv2d(128, 128, (3,3)),\n            nn.ReLU(),  \n            nn.AdaptiveMaxPool2d((1,1))\n        )\n        self.d = nn.Sequential(\n            nn.Dropout(0.25),\n            nn.Linear(128, 128),\n            nn.ReLU(),\n            nn.Dropout(0.25),\n            nn.Linear(128, 128),\n            nn.ReLU(),\n            nn.Dropout(0.25),\n            nn.Linear(128, 128),\n            nn.ReLU(),\n            nn.Dropout(0.25),\n            nn.Linear(128, 37),\n            nn.Sigmoid()\n        )\n    \n    def forward(self, res):\n        res = self.sq(res)\n        res = res.reshape(res.shape[0],128)\n        res = self.d(res)\n        return res\n\nmodel = GalaxyModel().to(device)\nsummary(model, input_size = (3, 64, 64))","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:43:24.170023Z","iopub.execute_input":"2022-02-26T03:43:24.170458Z","iopub.status.idle":"2022-02-26T03:43:32.713703Z","shell.execute_reply.started":"2022-02-26T03:43:24.17042Z","shell.execute_reply":"2022-02-26T03:43:32.712996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 15\nalpha = 0.001\noptimizer = optim.Adamax(model.parameters(), lr = alpha)\ncriterion = nn.BCELoss()\nscheduler = optim.lr_scheduler.StepLR(optimizer, step_size=4, gamma=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.646503Z","iopub.status.idle":"2022-02-25T18:54:33.647238Z","shell.execute_reply.started":"2022-02-25T18:54:33.646937Z","shell.execute_reply":"2022-02-25T18:54:33.646969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def eval_loop(model, valloader, criterion, optimizer):\n    model.eval()\n    tr_loss = 0.0\n    ep = 0\n    precision = 0\n    with torch.no_grad():\n        for i, (data, label) in enumerate(tqdm(valloader)):\n            ep += 1\n            data, label = data.to(device), label.to(device)\n            pred = model(data).type(torch.float64)\n            loss = criterion(pred, label)\n            tr_loss += loss.item()\n            precision += mean_squared_error(pred.cpu().detach().numpy(), label.cpu().detach().numpy())\n    return tr_loss/len(valloader), 100*precision/ep","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.64867Z","iopub.status.idle":"2022-02-25T18:54:33.64944Z","shell.execute_reply.started":"2022-02-25T18:54:33.649128Z","shell.execute_reply":"2022-02-25T18:54:33.649161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def training_loop(model, trainloader, criterion, optimizer, scheduler):\n    model.train()\n    tr_loss = 0.0\n    ep = 0\n    precision = 0\n    for i, (data, label) in enumerate(tqdm(trainloader)):\n        ep += 1\n        data, label = data.to(device), label.to(device)\n        optimizer.zero_grad()\n        pred = model(data).type(torch.float64)\n        loss = criterion(pred, label)\n        \n        loss.backward()\n        optimizer.step()\n        tr_loss += loss.item()\n        precision += mean_squared_error(pred.cpu().detach().numpy(), label.cpu().detach().numpy())\n#     scheduler.step()\n    return tr_loss/len(trainloader), 100*precision/ep#100*correct/(correct + wrong)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.650882Z","iopub.status.idle":"2022-02-25T18:54:33.651625Z","shell.execute_reply.started":"2022-02-25T18:54:33.651302Z","shell.execute_reply":"2022-02-25T18:54:33.651346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(model,trainloader, valloader, criterion, optimizer, scheduler, epoch):\n    print('-'*5+'Training'+'-'*5)\n    liveloss = PlotLosses()\n    \n    for ep in range(epoch):\n        logs = {}\n        print(f'Training epoch: {ep+1}')\n        t_loss, t_acc = training_loop(model, trainloader,criterion, optimizer, scheduler)\n        v_loss, v_acc = eval_loop(model, valloader, criterion, optimizer)\n        print(f'Training loss:{t_loss} Training accuracy: {t_acc}')\n        print(f'Validation loss: {v_loss} Validation accuracy: {v_acc}')\n        logs['log loss'] = t_loss\n        logs['val_log loss'] = v_loss\n        liveloss.update(logs)\n        liveloss.send()\n    print('Finished Training')","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.653198Z","iopub.status.idle":"2022-02-25T18:54:33.653938Z","shell.execute_reply.started":"2022-02-25T18:54:33.653618Z","shell.execute_reply":"2022-02-25T18:54:33.65365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model(model,trainloader, valloader, criterion, optimizer, scheduler, epochs)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.655383Z","iopub.status.idle":"2022-02-25T18:54:33.656182Z","shell.execute_reply.started":"2022-02-25T18:54:33.655859Z","shell.execute_reply":"2022-02-25T18:54:33.655892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ndummy_input, _ = DataLoader(TrainDataSet[0])\ntorch.onnx.export(model,\n         dummy_input.cuda() if device == \"cuda\" else dummy_input,\n         \"galaxy_classification.onnx\",\n         export_params=True,\n         opset_version=10,\n         do_constant_folding=True,\n         input_names = ['modelInput'],\n         output_names = ['modelOutput'],\n         dynamic_axes={'modelInput' : {0 : 'batch_size'},\n                                'modelOutput' : {0 : 'batch_size'}}) \nprint('Model has been converted to ONNX')","metadata":{"execution":{"iopub.status.busy":"2022-02-25T18:54:33.657568Z","iopub.status.idle":"2022-02-25T18:54:33.658269Z","shell.execute_reply.started":"2022-02-25T18:54:33.657985Z","shell.execute_reply":"2022-02-25T18:54:33.658017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q -o ../input/galaxy-zoo-the-galaxy-challenge/images_test_rev1.zip","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:44:48.431728Z","iopub.execute_input":"2022-02-26T03:44:48.432481Z","iopub.status.idle":"2022-02-26T03:45:09.600169Z","shell.execute_reply.started":"2022-02-26T03:44:48.432437Z","shell.execute_reply":"2022-02-26T03:45:09.599198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testfiles = os.listdir(test_folder)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:45:09.602594Z","iopub.execute_input":"2022-02-26T03:45:09.603136Z","iopub.status.idle":"2022-02-26T03:45:09.65254Z","shell.execute_reply.started":"2022-02-26T03:45:09.603089Z","shell.execute_reply":"2022-02-26T03:45:09.651842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_image_generator(ids, shape=IMG_SHAPE):\n    x1 = (ORIG_SHAPE[0]-CROP_SIZE[0])//2\n    y1 = (ORIG_SHAPE[1]-CROP_SIZE[1])//2\n    x_batch = []\n    for i in ids:\n        x = get_image(test_folder+i, x1, y1, shape=IMG_SHAPE, crop_size=CROP_SIZE)\n        x_batch.append(x)\n    x_batch = np.array(x_batch)\n    return x_batch","metadata":{"execution":{"iopub.status.busy":"2022-02-26T03:46:38.230436Z","iopub.execute_input":"2022-02-26T03:46:38.230979Z","iopub.status.idle":"2022-02-26T03:46:38.23593Z","shell.execute_reply.started":"2022-02-26T03:46:38.230925Z","shell.execute_reply":"2022-02-26T03:46:38.235256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 1\nval_predictions = []\nN_val = len(testfiles)\nimport gc\nwith torch.no_grad():\n    for i in tqdm(np.arange(0, N_val, batch_size)):\n        if i+batch_size > N_val:\n            upper = N_val\n        else:\n            upper = i+batch_size\n        X = test_image_generator(testfiles[i:upper]).reshape(64,64,-1)\n        X = Image.fromarray((X * 255).astype(np.uint8))\n        X = transforms.ToTensor()(X)\n        y_pred = model(X[None, ...].to(device))\n        val_predictions.append(y_pred)\n\nval_predictions = np.array(val_predictions)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T04:18:56.138601Z","iopub.execute_input":"2022-02-26T04:18:56.138852Z","iopub.status.idle":"2022-02-26T04:18:56.162568Z","shell.execute_reply.started":"2022-02-26T04:18:56.138823Z","shell.execute_reply":"2022-02-26T04:18:56.161127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predi = []\nfor i in val_predictions:\n    predi.append(i.cpu().numpy())\npredi = np.array(predi).reshape(-1,37)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T04:24:59.570408Z","iopub.execute_input":"2022-02-26T04:24:59.57069Z","iopub.status.idle":"2022-02-26T04:25:01.212625Z","shell.execute_reply.started":"2022-02-26T04:24:59.570659Z","shell.execute_reply":"2022-02-26T04:25:01.211852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predi.shape\nY_pred = np.vstack(predi)\nids = np.array([v.split('.')[0] for v in testfiles]).reshape(len(testfiles),1)\nsubmission_df = pd.DataFrame(np.hstack((ids, Y_pred)), columns=train_set.columns)\nsubmission_df = submission_df.sort_values(by=['GalaxyID'])\nsubmission_df.to_csv('sample_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-26T04:25:48.442521Z","iopub.execute_input":"2022-02-26T04:25:48.44277Z","iopub.status.idle":"2022-02-26T04:25:53.5808Z","shell.execute_reply.started":"2022-02-26T04:25:48.442742Z","shell.execute_reply":"2022-02-26T04:25:53.58008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-26T04:26:01.994536Z","iopub.execute_input":"2022-02-26T04:26:01.994807Z","iopub.status.idle":"2022-02-26T04:26:02.020155Z","shell.execute_reply.started":"2022-02-26T04:26:01.994776Z","shell.execute_reply":"2022-02-26T04:26:02.019282Z"},"trusted":true},"execution_count":null,"outputs":[]}]}