{"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":"package_paths = [\n    '../input/pytorch-image-models/pytorch-image-models-master', #'../input/efficientnet-pytorch-07/efficientnet_pytorch-0.7.0'\n]\nimport sys; \n\nfor pth in package_paths:\n    sys.path.append(pth)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:00:13.717916Z","iopub.execute_input":"2021-11-13T05:00:13.718630Z","iopub.status.idle":"2021-11-13T05:00:13.744019Z","shell.execute_reply.started":"2021-11-13T05:00:13.718518Z","shell.execute_reply":"2021-11-13T05:00:13.743362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rgb2gray(rgb):\n    return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:59:52.165204Z","iopub.execute_input":"2021-11-13T05:59:52.165880Z","iopub.status.idle":"2021-11-13T05:59:52.172236Z","shell.execute_reply.started":"2021-11-13T05:59:52.165846Z","shell.execute_reply":"2021-11-13T05:59:52.171223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport os\nimport pandas as pd\nimport skimage as sk\nfrom skimage import io as skio, transform\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms, utils\nimport time\nimport copy\nimport random\nimport xml.etree.ElementTree as ET \nimport torchvision.models as models\nfrom PIL import Image # to read images\nfrom PIL import Image, ImageOps\nfrom torchvision.transforms import ToTensor, ToPILImage\nimport json\nimport time\nfrom tqdm import tqdm\ntqdm.pandas()\nfrom sklearn.model_selection import KFold\nimport os\nimport timm\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-11-13T06:01:43.347158Z","iopub.execute_input":"2021-11-13T06:01:43.347690Z","iopub.status.idle":"2021-11-13T06:01:43.546763Z","shell.execute_reply.started":"2021-11-13T06:01:43.347652Z","shell.execute_reply":"2021-11-13T06:01:43.546026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed = 42):\n    '''Sets the seed of the entire notebook so results are the same every time we run.\n    This is for REPRODUCIBILITY.'''\n    np.random.seed(seed)\n    random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    # When running on the CuDNN backend, two further options must be set\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    # Set a fixed value for the hash seed\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    print('> SEEDING DONE')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:00:21.728414Z","iopub.execute_input":"2021-11-13T05:00:21.728621Z","iopub.status.idle":"2021-11-13T05:00:21.736862Z","shell.execute_reply.started":"2021-11-13T05:00:21.728594Z","shell.execute_reply":"2021-11-13T05:00:21.736088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AverageMeter(object):\n    \"\"\"Computes and stores the average and current value\"\"\"\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.val = 0\n        self.avg = 0\n        self.sum = 0\n        self.count = 0\n\n    def update(self, val, n=1):\n        self.val = val\n        self.sum += val * n\n        self.count += n\n        self.avg = self.sum / self.count\n        \ndef get_logger(filename='log'):\n    from logging import getLogger, INFO, StreamHandler, FileHandler, Formatter\n    logger = getLogger(__name__)\n    logger.setLevel(INFO)\n    handler1 = StreamHandler()\n    handler1.setFormatter(Formatter(\"%(message)s\"))\n    handler2 = FileHandler(filename=f\"{filename}.log\")\n    handler2.setFormatter(Formatter(\"%(message)s\"))\n    logger.addHandler(handler1)\n    logger.addHandler(handler2)\n    return logger\n\nlogger = get_logger()","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:00:21.739072Z","iopub.execute_input":"2021-11-13T05:00:21.739293Z","iopub.status.idle":"2021-11-13T05:00:21.751219Z","shell.execute_reply.started":"2021-11-13T05:00:21.739267Z","shell.execute_reply":"2021-11-13T05:00:21.750481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self,data, transform=True, test=False):\n        self.data = data\n        self.transform = transform\n    def __len__(self):\n        return len(self.data)\n    def crop_box(self, index, im, box):\n        left = int(max(box[0], 0))\n        top = int(max(box[1], 0))\n        width = int(box[2]*1.4)\n        height = int(box[3]*1.4)\n        \n        return im[top:top+height, left:left+width], left, top, width, height \n    \n    def __getitem__(self, i):\n        row = self.data.iloc[i]\n        ori_image = Image.open(row.img_file_path)\n        ori_image = ImageOps.grayscale(ori_image)\n        ori_image = ToTensor()(ori_image).squeeze()\n        bbox = np.array(json.loads(row.bbox))\n        im, left, top,width, height = self.crop_box(i, ori_image, bbox)\n        h, w = im.shape[:2]\n        if self.transform:\n            #resize\n            im = torch.unsqueeze(im, dim=0)\n            im = transforms.Resize((224,224))(im)\n            \n        output = {'image': im,'h': h, 'w': w, 'box': bbox, 'left':left, 'top':top, 'width':width, 'height':height}\n        return output","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:00:21.752509Z","iopub.execute_input":"2021-11-13T05:00:21.752782Z","iopub.status.idle":"2021-11-13T05:00:21.764119Z","shell.execute_reply.started":"2021-11-13T05:00:21.752745Z","shell.execute_reply":"2021-11-13T05:00:21.763288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def countList(lst1, lst2):\n    return np.array([sub[item] for item in range(len(lst2))\n                      for sub in [lst1, lst2]])","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:00:21.765361Z","iopub.execute_input":"2021-11-13T05:00:21.766118Z","iopub.status.idle":"2021-11-13T05:00:21.774891Z","shell.execute_reply.started":"2021-11-13T05:00:21.766080Z","shell.execute_reply":"2021-11-13T05:00:21.773473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class effnet(nn.Module):\n    def __init__(self, model_arch, pretrained=True):\n        super().__init__()\n        self.model = timm.create_model(model_arch, pretrained=pretrained)\n        n_features = self.model.classifier.in_features\n        self.model.conv_stem = nn.Conv2d(1, 48, kernel_size=(3, 3), stride=(2, 2), bias=False)\n        self.model.classifier = nn.Linear(n_features, 68*2)\n\n    def forward(self, x):\n        x = self.model(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:00:21.776414Z","iopub.execute_input":"2021-11-13T05:00:21.776812Z","iopub.status.idle":"2021-11-13T05:00:21.786659Z","shell.execute_reply.started":"2021-11-13T05:00:21.776774Z","shell.execute_reply":"2021-11-13T05:00:21.785873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\n\ndf_list = []\nweights = os.listdir('../input/effnet-weights')\nfor weight in weights:\n    net = effnet('tf_efficientnet_b4_ns')\n    net.load_state_dict(torch.load('../input/effnet-weights/'+weight))\n    net = net.to(device, dtype=torch.float)\n    test = pd.read_csv('../input/cs194-26-proj5/test.csv')\n    test_dataset = TestDataset(test)\n    test_dataloader = DataLoader(test_dataset)\n    x_l=[]\n    y_l=[]\n\n    with torch.no_grad():\n        pbar = tqdm(enumerate(test_dataloader), total=len(test_dataloader))\n        for i_batch, sample_batched in pbar:\n            im = sample_batched['image']\n            h = sample_batched['h']\n            w = sample_batched['w']\n            box = sample_batched['box']\n            width = sample_batched['width'].item()\n            height = sample_batched['height'].item()\n            left = sample_batched['left'].item()\n            top = sample_batched['top'].item()\n            batch_size = im.size(0)\n            im = im.to(device, dtype=torch.float)\n            with torch.no_grad():\n                output = net(im)\n                output = output.cpu().numpy()\n                x = output[0][0::2]*width+left\n                y = output[0][1::2]*height+top\n                x_l.append(x)\n                y_l.append(y)\n    x_l = np.array(x_l)\n    y_l = np.array(y_l)\n    big_list = []\n    for i in range(len(x_l)):\n        big_list.append(countList(x_l[i], y_l[i]))\n    big_list = np.array(big_list)\n    final_output = big_list.flatten()\n    ids = np.arange(len(test)*68*2)\n    df = pd.DataFrame({'Id':ids, 'Predicted':final_output})\n    df_list.append(df)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T06:06:09.066136Z","iopub.execute_input":"2021-11-13T06:06:09.066945Z","iopub.status.idle":"2021-11-13T06:06:12.501597Z","shell.execute_reply.started":"2021-11-13T06:06:09.066898Z","shell.execute_reply":"2021-11-13T06:06:12.500623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final = df_list[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:01:48.587914Z","iopub.execute_input":"2021-11-13T05:01:48.588326Z","iopub.status.idle":"2021-11-13T05:01:48.593102Z","shell.execute_reply.started":"2021-11-13T05:01:48.588271Z","shell.execute_reply":"2021-11-13T05:01:48.592367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final['Id']=np.arange(len(test)*68*2)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:01:48.596453Z","iopub.execute_input":"2021-11-13T05:01:48.597333Z","iopub.status.idle":"2021-11-13T05:01:48.606297Z","shell.execute_reply.started":"2021-11-13T05:01:48.597282Z","shell.execute_reply":"2021-11-13T05:01:48.605499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final.to_csv('1fold_tf_efficientnet_b4_ns.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:01:48.607652Z","iopub.execute_input":"2021-11-13T05:01:48.608162Z","iopub.status.idle":"2021-11-13T05:01:48.988707Z","shell.execute_reply.started":"2021-11-13T05:01:48.608127Z","shell.execute_reply":"2021-11-13T05:01:48.987873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im_list = []\nfor i in test.iloc[:5].img_file_path.values:\n    im_list.append(skio.imread(i))","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:45:24.476151Z","iopub.execute_input":"2021-11-13T05:45:24.476802Z","iopub.status.idle":"2021-11-13T05:45:24.556428Z","shell.execute_reply.started":"2021-11-13T05:45:24.476766Z","shell.execute_reply":"2021-11-13T05:45:24.555600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(im_list[0],cmap='gray')\nplt.scatter(x_l[0],y_l[0])\nplt.savefig('testpic1.jpeg')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:47:32.644203Z","iopub.execute_input":"2021-11-13T05:47:32.645138Z","iopub.status.idle":"2021-11-13T05:47:33.180671Z","shell.execute_reply.started":"2021-11-13T05:47:32.645096Z","shell.execute_reply":"2021-11-13T05:47:33.180003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(im_list[1],cmap='gray')\nplt.scatter(x_l[1],y_l[1])\nplt.savefig('testpic2.jpeg')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:47:34.247211Z","iopub.execute_input":"2021-11-13T05:47:34.247759Z","iopub.status.idle":"2021-11-13T05:47:34.735626Z","shell.execute_reply.started":"2021-11-13T05:47:34.247724Z","shell.execute_reply":"2021-11-13T05:47:34.734937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(im_list[2],cmap='gray')\nplt.scatter(x_l[2],y_l[2])\nplt.savefig('testpic3.jpeg')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:47:36.513883Z","iopub.execute_input":"2021-11-13T05:47:36.514686Z","iopub.status.idle":"2021-11-13T05:47:36.807824Z","shell.execute_reply.started":"2021-11-13T05:47:36.514628Z","shell.execute_reply":"2021-11-13T05:47:36.807142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(im_list[3],cmap='gray')\nplt.scatter(x_l[3],y_l[3])\nplt.savefig('testpic4.jpeg')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:48:01.926870Z","iopub.execute_input":"2021-11-13T05:48:01.927128Z","iopub.status.idle":"2021-11-13T05:48:02.354402Z","shell.execute_reply.started":"2021-11-13T05:48:01.927099Z","shell.execute_reply":"2021-11-13T05:48:02.353704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(im_list[4],cmap='gray')\nplt.scatter(x_l[4],y_l[4])\nplt.savefig('testpic5.jpeg')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:48:18.714892Z","iopub.execute_input":"2021-11-13T05:48:18.715159Z","iopub.status.idle":"2021-11-13T05:48:19.233089Z","shell.execute_reply.started":"2021-11-13T05:48:18.715130Z","shell.execute_reply":"2021-11-13T05:48:19.232441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#elvis\nx_l=[]\ny_l=[]\nep = skio.imread('../input/headshots/elvis.jpg')\nep = rgb2gray(ep)\nleft = 97\ntop = 334\nwidth = 536\nheight = 536\nep_224 = cv2.resize(ep[top:top+height,left:left+width],(224,224))\nwith torch.no_grad():\n    im = torch.from_numpy(ep_224).to(device, dtype=torch.float)\n    im = im.unsqueeze(dim=0)\n    im = im.unsqueeze(dim=0)\n    ep_output = net(im)\n    ep_output = ep_output.cpu().numpy()\n    x = ep_output[0][0::2]*width+left\n    y = ep_output[0][1::2]*height+top\n    x_l.append(x)\n    y_l.append(y)\n    \nplt.imshow(ep,cmap='gray')\nplt.scatter(x_l[0],y_l[0])\nplt.savefig('ep_test.jpeg')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T06:53:16.075466Z","iopub.execute_input":"2021-11-13T06:53:16.075914Z","iopub.status.idle":"2021-11-13T06:53:16.502023Z","shell.execute_reply.started":"2021-11-13T06:53:16.075878Z","shell.execute_reply":"2021-11-13T06:53:16.501264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#monroe\nx_l=[]\ny_l=[]\nmm = skio.imread('../input/headshots/monroe.jpg')\nmm = rgb2gray(mm)\nleft = 82\ntop = 234\nwidth = 250\nheight = 248\nmm_224 = cv2.resize(mm[top:top+height,left:left+width],(224,224))\nwith torch.no_grad():\n    im = torch.from_numpy(mm_224).to(device, dtype=torch.float)\n    im = im.unsqueeze(dim=0)\n    im = im.unsqueeze(dim=0)\n    mm_output = net(im)\n    mm_output = mm_output.cpu().numpy()\n    x = mm_output[0][0::2]*width+left\n    y = mm_output[0][1::2]*height+top\n    x_l.append(x)\n    y_l.append(y)\n    \nplt.imshow(mm,cmap='gray')\nplt.scatter(x_l[0],y_l[0])\nplt.savefig('mm_test.jpeg')","metadata":{"execution":{"iopub.status.busy":"2021-11-13T06:55:15.858156Z","iopub.execute_input":"2021-11-13T06:55:15.858912Z","iopub.status.idle":"2021-11-13T06:55:16.196050Z","shell.execute_reply.started":"2021-11-13T06:55:15.858866Z","shell.execute_reply":"2021-11-13T06:55:16.195369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#beckham\nx_l=[]\ny_l=[]\ndb = skio.imread('../input/headshots/beckham.jpg')\ndb = rgb2gray(db)\nleft = 311\ntop = 244\nwidth = 260\nheight = 261\ndb_224 = cv2.resize(db[top:top+height,left:left+width],(224,224))\nwith torch.no_grad():\n    im = torch.from_numpy(db_224).to(device, dtype=torch.float)\n    im = im.unsqueeze(dim=0)\n    im = im.unsqueeze(dim=0)\n    db_output = net(im)\n    db_output = db_output.cpu().numpy()\n    x = db_output[0][0::2]*width+left\n    y = db_output[0][1::2]*height+top\n    x_l.append(x)\n    y_l.append(y)\n    \nplt.imshow(db,cmap='gray')\nplt.scatter(x_l[0],y_l[0])\nplt.savefig('db_test.jpeg')\n","metadata":{"execution":{"iopub.status.busy":"2021-11-13T06:57:27.067056Z","iopub.execute_input":"2021-11-13T06:57:27.067422Z","iopub.status.idle":"2021-11-13T06:57:27.522778Z","shell.execute_reply.started":"2021-11-13T06:57:27.067383Z","shell.execute_reply":"2021-11-13T06:57:27.522072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"net","metadata":{"execution":{"iopub.status.busy":"2021-11-13T05:04:53.141504Z","iopub.execute_input":"2021-11-13T05:04:53.142391Z","iopub.status.idle":"2021-11-13T05:04:53.154298Z","shell.execute_reply.started":"2021-11-13T05:04:53.142347Z","shell.execute_reply":"2021-11-13T05:04:53.153362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}