{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":21154,"databundleVersionId":1243559,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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 torch\nimport torch.backends.cudnn as cudnn\nimport torchvision\n\nimport tensorflow as tf\n\nimport os\nimport time","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-25T08:17:24.839059Z","iopub.execute_input":"2023-11-25T08:17:24.839711Z","iopub.status.idle":"2023-11-25T08:17:41.889363Z","shell.execute_reply.started":"2023-11-25T08:17:24.839664Z","shell.execute_reply":"2023-11-25T08:17:41.888154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install GPUtil\n\n#from GPUtil import showUtilization as gpu_usage\n#gpu_usage() \n\nos.environ[\"CUDA_VISIBLE_DEVICES\"]=\"0,1\"","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:17:41.891248Z","iopub.execute_input":"2023-11-25T08:17:41.891872Z","iopub.status.idle":"2023-11-25T08:17:41.899824Z","shell.execute_reply.started":"2023-11-25T08:17:41.891841Z","shell.execute_reply":"2023-11-25T08:17:41.898585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\ntrain_files = glob.glob('/kaggle/input/tpu-getting-started/*/train/*.tfrec')\nval_files = glob.glob('/kaggle/input/tpu-getting-started/*/val/*.tfrec')\ntest_files = tf.io.gfile.glob('/kaggle/input/tpu-getting-started/*/test/*.tfrec')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:17:41.901337Z","iopub.execute_input":"2023-11-25T08:17:41.901672Z","iopub.status.idle":"2023-11-25T08:17:42.191606Z","shell.execute_reply.started":"2023-11-25T08:17:41.901642Z","shell.execute_reply":"2023-11-25T08:17:42.190510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASSES = ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'wild geranium', 'tiger lily',\n           'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle', 'snapdragon', \"colt's foot\", 'king protea',\n           'spear thistle', 'yellow iris', 'globe-flower', 'purple coneflower', 'peruvian lily', 'balloon flower',\n           'giant white arum lily', 'fire lily', 'pincushion flower', 'fritillary', 'red ginger', 'grape hyacinth',\n           'corn poppy', 'prince of wales feathers', 'stemless gentian', 'artichoke', 'sweet william',\n           'carnation', 'garden phlox', 'love in the mist', 'cosmos', 'alpine sea holly', 'ruby-lipped cattleya',\n           'cape flower', 'great masterwort', 'siam tulip', 'lenten rose', 'barberton daisy', 'daffodil', 'sword lily',\n           'poinsettia', 'bolero deep blue', 'wallflower', 'marigold', 'buttercup', 'daisy', 'common dandelion',\n           'petunia', 'wild pansy', 'primula', 'sunflower', 'lilac hibiscus', 'bishop of llandaff', 'gaura', 'geranium',\n           'orange dahlia', 'pink-yellow dahlia', 'cautleya spicata', 'japanese anemone', 'black-eyed susan',\n           'silverbush', 'californian poppy', 'osteospermum', 'spring crocus', 'iris', 'windflower', 'tree poppy',\n           'gazania', 'azalea', 'water lily', 'rose', 'thorn apple', 'morning glory', 'passion flower', 'lotus',\n           'toad lily', 'anthurium', 'frangipani', 'clematis', 'hibiscus', 'columbine', 'desert-rose', 'tree mallow',\n           'magnolia', 'cyclamen ', 'watercress', 'canna lily', 'hippeastrum ', 'bee balm', 'pink quill', 'foxglove',\n           'bougainvillea', 'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower', 'trumpet creeper',\n           'blackberry lily', 'common tulip', 'wild rose']","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:17:42.193849Z","iopub.execute_input":"2023-11-25T08:17:42.194219Z","iopub.status.idle":"2023-11-25T08:17:42.202346Z","shell.execute_reply.started":"2023-11-25T08:17:42.194162Z","shell.execute_reply":"2023-11-25T08:17:42.201238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_feature_description = {\n    'class': tf.io.FixedLenFeature([], tf.int64),\n    'id': tf.io.FixedLenFeature([], tf.string),\n    'image': tf.io.FixedLenFeature([], tf.string),\n}\n\ntest_feature_description = {\n    'id': tf.io.FixedLenFeature([], tf.string),\n    'image': tf.io.FixedLenFeature([], tf.string),\n}\n\ndef _parse_image_function(example_proto):\n    return tf.io.parse_single_example(example_proto, train_feature_description)\n\ndef _parse_image_function2(example_proto):\n    return tf.io.parse_single_example(example_proto, test_feature_description)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:17:42.203927Z","iopub.execute_input":"2023-11-25T08:17:42.204380Z","iopub.status.idle":"2023-11-25T08:17:42.217489Z","shell.execute_reply.started":"2023-11-25T08:17:42.204341Z","shell.execute_reply":"2023-11-25T08:17:42.216444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids = []\ntrain_class = []\ntrain_images = []\nfor i in train_files:\n    train_image_dataset = tf.data.TFRecordDataset(i)\n    train_image_dataset = train_image_dataset.map(_parse_image_function)\n    ids = [str(id_features['id'].numpy())[2:-1] for id_features in train_image_dataset] # [2:-1] is done to remove b' from 1st and 'from last in train id names\n    train_ids = train_ids + ids\n    classes = [int(class_features['class'].numpy()) for class_features in train_image_dataset]\n    train_class = train_class + classes\n    images = [image_features['image'].numpy() for image_features in train_image_dataset]\n    train_images = train_images + images\n    \nval_ids = []\nval_class = []\nval_images = []\nfor i in val_files:\n    val_image_dataset = tf.data.TFRecordDataset(i)\n    val_image_dataset = val_image_dataset.map(_parse_image_function)\n    ids = [str(id_features['id'].numpy())[2:-1] for id_features in val_image_dataset]\n    val_ids = val_ids + ids\n    classes = [int(class_features['class'].numpy()) for class_features in val_image_dataset]\n    val_class = val_class + classes\n    images = [image_features['image'].numpy() for image_features in val_image_dataset]\n    val_images = val_images + images\n    ","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:17:42.218893Z","iopub.execute_input":"2023-11-25T08:17:42.219258Z","iopub.status.idle":"2023-11-25T08:19:47.501562Z","shell.execute_reply.started":"2023-11-25T08:17:42.219221Z","shell.execute_reply":"2023-11-25T08:19:47.500448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = []\ntest_images = []\nfor i in test_files:\n    test_image_dataset = tf.data.TFRecordDataset(i)\n    test_image_dataset = test_image_dataset.map(_parse_image_function2)\n    ids = [str(id_features['id'].numpy())[2:-1] for id_features in test_image_dataset]\n    test_ids = test_ids + ids\n    images = [image_features['image'].numpy() for image_features in test_image_dataset]\n    test_images = test_images + images\ntest_class = [0]*len(test_ids)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:19:47.503386Z","iopub.execute_input":"2023-11-25T08:19:47.504053Z","iopub.status.idle":"2023-11-25T08:20:35.605248Z","shell.execute_reply.started":"2023-11-25T08:19:47.504011Z","shell.execute_reply":"2023-11-25T08:20:35.604153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import IPython.display as display\ndisplay.display(display.Image(data=train_images[211]))","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:20:35.606884Z","iopub.execute_input":"2023-11-25T08:20:35.607309Z","iopub.status.idle":"2023-11-25T08:20:35.616254Z","shell.execute_reply.started":"2023-11-25T08:20:35.607274Z","shell.execute_reply":"2023-11-25T08:20:35.615083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport cv2\nimport albumentations\nimport torch\nimport numpy as np\nimport io\nfrom torch.utils.data import Dataset\n\nclass FlowerDataset(Dataset):\n    def __init__(self,id,classes,image,img_height,img_width,mean,std,is_valid):\n        self.id = id\n        self.classes = classes\n        self.image = image\n        self.is_valid = is_valid\n        if self.is_valid == 1:\n            self.aug = albumentations.Compose([\n                albumentations.Resize(img_height,img_width, always_apply=True),\n                albumentations.Normalize(mean,std,always_apply=True),\n                albumentations.ShiftScaleRotate(shift_limit = 0.0625, scale_limit = 0.1, rotate_limit = 5, p=0.9)\n            ])\n    def __len__(self):\n        return len(self.id)\n    \n    def getId(self,index):\n        return self.id[index]\n    \n    def __getitem__(self,index):\n        img = np.array(Image.open(io.BytesIO(self.image[index])))\n        img = cv2.resize(img, dsize=(128,128), interpolation=cv2.INTER_CUBIC)\n        img = self.aug(image=img)['image']\n        img = np.transpose(img,(2,0,1)).astype(np.float32)\n        \n        return torch.tensor(img, dtype=torch.float), int(self.classes[index])\n        ","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:20:35.617681Z","iopub.execute_input":"2023-11-25T08:20:35.618518Z","iopub.status.idle":"2023-11-25T08:20:37.269149Z","shell.execute_reply.started":"2023-11-25T08:20:35.618477Z","shell.execute_reply":"2023-11-25T08:20:37.268066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = FlowerDataset(id=train_ids, classes=train_class, image=train_images, img_height=128, img_width=128, mean=(0.485,0.456,0.406), std=(0.229,0.224,0.225),is_valid=1)\nval_dataset = FlowerDataset(id=val_ids, classes=val_class, image=val_images, img_height=128, img_width=128, mean=(0.485,0.456,0.406), std=(0.229,0.224,0.225),is_valid=1)\ntest_dataset = FlowerDataset(id=test_ids, classes=[0]*len(test_ids), image=test_images, img_height=128, img_width=128, mean=(0.485,0.456,0.406), std=(0.229,0.224,0.225),is_valid=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:20:37.271987Z","iopub.execute_input":"2023-11-25T08:20:37.272600Z","iopub.status.idle":"2023-11-25T08:20:37.280670Z","shell.execute_reply.started":"2023-11-25T08:20:37.272567Z","shell.execute_reply":"2023-11-25T08:20:37.279334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nidx = 219\nimg = train_dataset[idx][0]\nprint(train_dataset[idx][1])\nnpimg = img.numpy()\nplt.imshow(np.transpose(npimg,(1,2,0)))","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:20:37.281923Z","iopub.execute_input":"2023-11-25T08:20:37.282271Z","iopub.status.idle":"2023-11-25T08:20:37.762633Z","shell.execute_reply.started":"2023-11-25T08:20:37.282242Z","shell.execute_reply":"2023-11-25T08:20:37.761308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(CLASSES)\nuse_cuda = torch.cuda.is_available()\n#use_cuda = False\nbatch_size = 16\ntrainloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=2, drop_last=True)\nvalloader = torch.utils.data.DataLoader(val_dataset, batch_size=16, shuffle=False, num_workers=2, drop_last=True)\n\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:20:37.764159Z","iopub.execute_input":"2023-11-25T08:20:37.764557Z","iopub.status.idle":"2023-11-25T08:20:37.770617Z","shell.execute_reply.started":"2023-11-25T08:20:37.764510Z","shell.execute_reply":"2023-11-25T08:20:37.769268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\n\ndef conv3x3(in_planes, out_planes, stride=1):\n    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=True)\n\ndef cfg(depth):\n    depth_lst = [18, 34, 50, 101, 152]\n    assert (depth in depth_lst), \"Error : Resnet depth should be either 18, 34, 50, 101, 152\"\n    cf_dict = {\n        '18': (BasicBlock, [2,2,2,2]),\n        '34': (BasicBlock, [3,4,6,3]),\n        '50': (Bottleneck, [3,4,6,3]),\n        '101':(Bottleneck, [3,4,23,3]),\n        '152':(Bottleneck, [3,8,36,3]),\n    }\n\n    return cf_dict[str(depth)]\n\nclass BasicBlock(nn.Module):\n    expansion = 1\n\n    def __init__(self, in_planes, planes, stride=1):\n        super(BasicBlock, self).__init__()\n        self.conv1 = conv3x3(in_planes, planes, stride)\n        self.bn1 = nn.BatchNorm2d(planes)\n        self.conv2 = conv3x3(planes, planes)\n        self.bn2 = nn.BatchNorm2d(planes)\n\n        self.shortcut = nn.Sequential()\n        if stride != 1 or in_planes != self.expansion * planes:\n            self.shortcut = nn.Sequential(\n                nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=True),\n                nn.BatchNorm2d(self.expansion*planes)\n            )\n\n    def forward(self, x):\n        out = F.relu(self.bn1(self.conv1(x)))\n        out = self.bn2(self.conv2(out))\n        out += self.shortcut(x)\n        out = F.relu(out)\n\n        return out\n\nclass Bottleneck(nn.Module):\n    expansion = 4\n\n    def __init__(self, in_planes, planes, stride=1):\n        super(Bottleneck, self).__init__()\n        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=True)\n        self.bn1 = nn.BatchNorm2d(planes)\n        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=True)\n        self.bn2 = nn.BatchNorm2d(planes)\n        self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=True)\n        self.bn3 = nn.BatchNorm2d(self.expansion*planes)\n\n        self.shortcut = nn.Sequential()\n        if stride != 1 or in_planes != self.expansion*planes:\n            self.shortcut = nn.Sequential(\n                nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=True),\n                nn.BatchNorm2d(self.expansion*planes)\n            )\n\n    def forward(self, x):\n        out = F.relu(self.bn1(self.conv1(x)))\n        out = F.relu(self.bn2(self.conv2(out)))\n        out = self.bn3(self.conv3(out))\n        out += self.shortcut(x)\n        out = F.relu(out)\n\n        return out\n\nclass ResNet(nn.Module):\n    def __init__(self, depth, num_classes):\n        super(ResNet, self).__init__()\n        self.in_planes = 16\n\n        block, num_blocks = cfg(depth)\n\n        self.conv1 = conv3x3(3,16)\n        self.bn1 = nn.BatchNorm2d(16)\n        self.layer1 = self._make_layer(block, 16, num_blocks[0], stride=1)\n        self.layer2 = self._make_layer(block, 32, num_blocks[1], stride=2)\n        self.layer3 = self._make_layer(block, 64, num_blocks[2], stride=2)\n        self.linear = nn.Linear(64*block.expansion, num_classes)\n\n    def _make_layer(self, block, planes, num_blocks, stride):\n        strides = [stride] + [1]*(num_blocks-1)\n        layers = []\n\n        for stride in strides:\n            layers.append(block(self.in_planes, planes, stride))\n            self.in_planes = planes * block.expansion\n\n        return nn.Sequential(*layers)\n\n    def forward(self, x):\n        out = F.relu(self.bn1(self.conv1(x)))\n        out = self.layer1(out)\n        out = self.layer2(out)\n        out = self.layer3(out)\n        out = F.avg_pool2d(out, 8)\n        out = out.view(out.size(0), -1)\n        out = self.linear(out)\n\n        return out","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:20:37.772151Z","iopub.execute_input":"2023-11-25T08:20:37.772557Z","iopub.status.idle":"2023-11-25T08:20:37.796214Z","shell.execute_reply.started":"2023-11-25T08:20:37.772516Z","shell.execute_reply":"2023-11-25T08:20:37.794867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#net = Resnet(50,num_classes)\n#net_save = Resnet(50,num_classes)\n\nnet=torchvision.models.resnet50(weights=torchvision.models.ResNet50_Weights.DEFAULT)\nnet.fc = torch.nn.Linear(net.fc.in_features,num_classes)\n\nnet_save=torchvision.models.resnet50()\nnet_save.fc = torch.nn.Linear(net_save.fc.in_features,num_classes)\n\nlr=0.0025\nnum_epochs=1\n\ncriterion = torch.nn.CrossEntropyLoss()\n\noptimizer = torch.optim.SGD(\n        net.parameters(),\n        lr=lr,\n        momentum=0.9,\n        weight_decay=5e-4,\n        nesterov=True,\n)\nscheduler = torch.optim.lr_scheduler.MultiStepLR(\n    optimizer,\n    milestones=[60, 120, 160],\n    gamma=0.2,\n)\nif use_cuda:\n        net.cuda()\n        net = torch.nn.DataParallel(net, device_ids=[0,1])\n        cudnn.benchmark = True\n        net_save.cuda()\n        criterion.cuda()","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:20:37.798176Z","iopub.execute_input":"2023-11-25T08:20:37.799218Z","iopub.status.idle":"2023-11-25T08:20:41.908900Z","shell.execute_reply.started":"2023-11-25T08:20:37.799134Z","shell.execute_reply":"2023-11-25T08:20:41.907564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_acc=0\n\ndef train(epoch):\n    net.train()\n    net.training = True\n    train_loss = 0\n    correct = 0\n    total = 0\n\n    print(\"\\n=> Training Epoch #%d, LR=%.4f\" % (epoch, optimizer.param_groups[0][\"lr\"]))\n    for batch_idx, (inputs, targets) in enumerate(trainloader):\n        if use_cuda:\n            inputs, targets = inputs.cuda(), targets.cuda()  # GPU settings\n        optimizer.zero_grad()\n\n        outputs = net(inputs)  # Forward Propagation\n        loss = criterion(outputs, targets)  # Loss\n        loss.backward()  # Backward Propagation\n        optimizer.step()  # Optimizer update\n        train_loss += loss.item()\n        _, predicted = torch.max(outputs.data, 1)\n        total += targets.size(0)\n        correct += predicted.eq(targets.data).cpu().sum()\n\n        if batch_idx % 300 == 0:\n            print(\"| Epoch [%3d/%3d] Iter[%3d/%3d]\\t\\tLoss: %.4f Acc@1: %.3f%% \"\n                % (\n                    epoch,\n                    num_epochs,\n                    batch_idx + 1,\n                    (len(train_dataset) // batch_size) + 1,\n                    loss.item(),\n                    100.0 * correct / total,\n                )\n            )\n\n\ndef test(epoch):\n    net.eval()\n    net.training = False\n    test_loss = 0\n    correct = 0\n    total = 0\n    global best_acc\n    with torch.no_grad():\n        for _, (inputs, targets) in enumerate(valloader):\n            if use_cuda:\n                inputs, targets = inputs.cuda(), targets.cuda()\n            outputs = net(inputs)\n\n            loss = criterion(outputs, targets)\n\n            test_loss += loss.item()\n            _, predicted = torch.max(outputs.data, 1)\n            total += targets.size(0)\n            correct += predicted.eq(targets.data).cpu().sum()\n\n        # Save checkpoint when best model\n        acc = 100.0 * correct / total\n        print(\"\\n| Validation Epoch #%d\\t\\t\\tLoss: %.4f Acc@1: %.2f%%\" % (epoch, loss.item(), acc))\n\n        if acc > best_acc:\n            print(\"| Saving Best model...\\t\\t\\tTop1 = %.2f%%\" % (acc))\n            best_acc = acc\n            net_save.load_state_dict(net.state_dict(),strict=True)\n        return best_acc\n\n\nelapsed_time = 0\nfor epoch in range(0, num_epochs):\n    start_time = time.time()\n\n    train(epoch)\n    best_acc = test(epoch)\n    scheduler.step()\n\n    epoch_time = time.time() - start_time\n    elapsed_time += epoch_time","metadata":{"execution":{"iopub.status.busy":"2023-11-25T08:20:41.910749Z","iopub.execute_input":"2023-11-25T08:20:41.911261Z","iopub.status.idle":"2023-11-25T09:53:05.707679Z","shell.execute_reply.started":"2023-11-25T08:20:41.911218Z","shell.execute_reply":"2023-11-25T09:53:05.706279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net_save.eval()\ntest_ids = []\npredictions = []\nfor i in range(len(test_dataset)):\n    ids = test_dataset.getId(i)\n    if not ids in test_ids:\n        img, _ = test_dataset.__getitem__(i)\n        out = torch.nn.functional.softmax(net_save(img[None,:]),dim=1)\n        _,predicted = torch.max(out,dim=1)\n        test_ids.append(ids)\n        predictions.append(predicted.item())\n       \nnp.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')","metadata":{"execution":{"iopub.status.busy":"2023-11-25T09:53:05.710651Z","iopub.execute_input":"2023-11-25T09:53:05.711183Z","iopub.status.idle":"2023-11-25T09:59:57.097451Z","shell.execute_reply.started":"2023-11-25T09:53:05.711128Z","shell.execute_reply":"2023-11-25T09:59:57.094894Z"},"trusted":true},"execution_count":null,"outputs":[]}]}