{"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":{"papermill":{"duration":0.033958,"end_time":"2021-11-12T10:16:40.254509","exception":false,"start_time":"2021-11-12T10:16:40.220551","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:09.494702Z","iopub.execute_input":"2021-11-13T04:54:09.495132Z","iopub.status.idle":"2021-11-13T04:54:09.515598Z","shell.execute_reply.started":"2021-11-13T04:54:09.495025Z","shell.execute_reply":"2021-11-13T04:54:09.514986Z"},"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 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 cv2\nimport timm","metadata":{"papermill":{"duration":8.598239,"end_time":"2021-11-12T10:16:48.873651","exception":false,"start_time":"2021-11-12T10:16:40.275412","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:09.518447Z","iopub.execute_input":"2021-11-13T04:54:09.518831Z","iopub.status.idle":"2021-11-13T04:54:17.838796Z","shell.execute_reply.started":"2021-11-13T04:54:09.518790Z","shell.execute_reply":"2021-11-13T04:54:17.837982Z"},"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":{"papermill":{"duration":0.026552,"end_time":"2021-11-12T10:16:48.918692","exception":false,"start_time":"2021-11-12T10:16:48.892140","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:17.841180Z","iopub.execute_input":"2021-11-13T04:54:17.841783Z","iopub.status.idle":"2021-11-13T04:54:17.848814Z","shell.execute_reply.started":"2021-11-13T04:54:17.841737Z","shell.execute_reply":"2021-11-13T04:54:17.847992Z"},"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":{"papermill":{"duration":0.028291,"end_time":"2021-11-12T10:16:48.965152","exception":false,"start_time":"2021-11-12T10:16:48.936861","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:17.850184Z","iopub.execute_input":"2021-11-13T04:54:17.850480Z","iopub.status.idle":"2021-11-13T04:54:17.861357Z","shell.execute_reply.started":"2021-11-13T04:54:17.850443Z","shell.execute_reply":"2021-11-13T04:54:17.860708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/cs194-26-proj5/train.csv')","metadata":{"papermill":{"duration":0.165811,"end_time":"2021-11-12T10:16:49.149040","exception":false,"start_time":"2021-11-12T10:16:48.983229","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:17.864233Z","iopub.execute_input":"2021-11-13T04:54:17.864519Z","iopub.status.idle":"2021-11-13T04:54:18.036247Z","shell.execute_reply.started":"2021-11-13T04:54:17.864483Z","shell.execute_reply":"2021-11-13T04:54:18.035516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rotate_augmentation(image, x, y, angle):\n    h, w = image.shape[:2]\n    M = cv2.getRotationMatrix2D((w//2,h//2), angle, 1.0)\n    angle_rad = -angle*np.pi/180\n    rotated_image = cv2.warpAffine(image, M, (w,h), flags=cv2.INTER_CUBIC)\n    x = x-w//2\n    y = y-h//2\n    rotated_x = x*np.cos(angle_rad)-y*np.sin(angle_rad)+w//2\n    rotated_y = x*np.sin(angle_rad)+y*np.cos(angle_rad)+h//2\n    \n    return rotated_image, rotated_x, rotated_y","metadata":{"papermill":{"duration":0.02684,"end_time":"2021-11-12T10:16:49.194249","exception":false,"start_time":"2021-11-12T10:16:49.167409","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:18.037522Z","iopub.execute_input":"2021-11-13T04:54:18.037859Z","iopub.status.idle":"2021-11-13T04:54:18.045837Z","shell.execute_reply.started":"2021-11-13T04:54:18.037820Z","shell.execute_reply":"2021-11-13T04:54:18.044156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def alter_brightness(image, x, y):\n    if random.random() < 0.5:\n        altered_image = np.clip(image*(random.randrange(50,100,1)/100), 0.0, 1.0)  \n    else:\n        altered_image = np.clip(image*(random.randrange(100,200,1)/100), 0.0, 1.0)    \n    return altered_image, x, y","metadata":{"papermill":{"duration":0.025504,"end_time":"2021-11-12T10:16:49.237638","exception":false,"start_time":"2021-11-12T10:16:49.212134","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:18.047295Z","iopub.execute_input":"2021-11-13T04:54:18.047530Z","iopub.status.idle":"2021-11-13T04:54:18.055633Z","shell.execute_reply.started":"2021-11-13T04:54:18.047498Z","shell.execute_reply":"2021-11-13T04:54:18.054978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TrainDataset(Dataset):\n    def __init__(self,data, transform=True):\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, landmark):\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        h,w=im.shape[:2]\n        \n        \n        # out of bounds points at left\n        if min(landmark[:,0]-left)<5:\n            left = max(int(left+min(landmark[:,0]-left)-70),0)\n\n        # out of bounds points at right\n        if sum((landmark[:,0]-left+5) > width):\n            width = min(int(max(landmark[:,0]-left)+70),w-left)\n\n        # out of bounds points at top\n        if min(landmark[:,1]-top)<5:\n            top = max(int(top+min(landmark[:,1]-top)-70),0)\n\n        # out of bounds points at the bottom\n        if sum((landmark[:,1]-top+5) > height):\n            height = min(int(max(landmark[:,1]-top)+70),h-top)\n        \n        \n        final_landmarks = []\n        for pair in landmark:\n            pair_0 = pair[0] - left\n            pair_1 = pair[1] - top\n            final_landmarks.append([pair_0, pair_1])\n        final_landmarks = np.array(final_landmarks).astype('float32')\n        return im[top:top+height, left:left+width], final_landmarks, 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        tmp_landmarks = np.array(json.loads(row.landmarks))\n        bbox = np.array(json.loads(row.bbox))\n        im, landmarks, left, top,width, height = self.crop_box(i, ori_image, bbox, tmp_landmarks)\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).squeeze().numpy()\n            #scale points\n            landmarks = landmarks * [224/w, 224/h]\n#             landmarks = torch.from_numpy(landmarks)\n            \n            #aug\n            image = im\n            x=landmarks[:,0]\n            y=landmarks[:,1]\n            if random.random() < 0.6:\n                # select random angle between -45 and 45\n                angle = random.randrange(-45, 45, 1) \n                temp_image, temp_x, temp_y = rotate_augmentation(image, x, y, angle)\n                h,w=temp_image.shape\n                if sum(temp_x > w)==0 and sum(temp_x < 0)==0 and sum(temp_y > h)==0 and sum(temp_y < 0)==0:\n                    image, x, y = temp_image, temp_x, temp_y\n            if random.random() < 0.5:\n                image, x, y =  alter_brightness(image, x, y)\n            landmarks = np.column_stack((x,y))\n            landmarks = torch.from_numpy(landmarks)\n            image = torch.from_numpy(image)\n            image = torch.unsqueeze(image, 0)\n            \n        output = {'image': image, 'pts': landmarks, 'scaled_pts': (landmarks/224).flatten(),\\\n                  'h': h, 'w': w, 'box': bbox, 'left':left, 'top':top, 'width':width, 'height':height}\n        return output\n#         return im,landmarks,h,w,bbox","metadata":{"papermill":{"duration":0.043018,"end_time":"2021-11-12T10:16:49.298460","exception":false,"start_time":"2021-11-12T10:16:49.255442","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:18.056552Z","iopub.execute_input":"2021-11-13T04:54:18.056723Z","iopub.status.idle":"2021-11-13T04:54:18.080598Z","shell.execute_reply.started":"2021-11-13T04:54:18.056701Z","shell.execute_reply":"2021-11-13T04:54:18.079832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfull_train_dataset = TrainDataset(train, transform=True)\nfull_train_dataloader = DataLoader(full_train_dataset)","metadata":{"papermill":{"duration":0.027083,"end_time":"2021-11-12T10:16:49.343992","exception":false,"start_time":"2021-11-12T10:16:49.316909","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:18.082208Z","iopub.execute_input":"2021-11-13T04:54:18.082697Z","iopub.status.idle":"2021-11-13T04:54:18.095626Z","shell.execute_reply.started":"2021-11-13T04:54:18.082659Z","shell.execute_reply":"2021-11-13T04:54:18.094853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i_batch, sample_batched in enumerate(full_train_dataloader):\n    plt.figure()\n    plt.imshow(sample_batched['image'][0][0], cmap='gray')\n    plt.scatter(sample_batched['pts'][0][:,0], sample_batched['pts'][0][:,1], s=10, marker='.', c='r')\n    plt.show()\n    count += 1\n    if count > 3:\n        break","metadata":{"papermill":{"duration":1.062029,"end_time":"2021-11-12T10:16:50.424515","exception":false,"start_time":"2021-11-12T10:16:49.362486","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:18.096739Z","iopub.execute_input":"2021-11-13T04:54:18.097154Z","iopub.status.idle":"2021-11-13T04:54:19.189727Z","shell.execute_reply.started":"2021-11-13T04:54:18.097118Z","shell.execute_reply":"2021-11-13T04:54:19.187448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_fn(train_loader, model, optimizer, criterion, device):\n    model = model.train()\n    t = time.time()\n    losses = AverageMeter()\n    pbar = tqdm(enumerate(train_loader), total=len(train_loader))\n    for i_batch, sample_batched in pbar:\n        im = sample_batched['image']\n        batch_size = im.size(0)\n        im = im.to(device, dtype=torch.float)\n        pts = sample_batched['scaled_pts']\n        pts = pts.to(device, dtype=torch.float)\n        optimizer.zero_grad()\n        output = model(im)\n        loss = criterion(output, pts)\n        loss.backward()\n        optimizer.step()\n        losses.update(loss.item(), batch_size)\n    return losses.avg","metadata":{"papermill":{"duration":0.035204,"end_time":"2021-11-12T10:16:50.485272","exception":false,"start_time":"2021-11-12T10:16:50.450068","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:19.195073Z","iopub.execute_input":"2021-11-13T04:54:19.195575Z","iopub.status.idle":"2021-11-13T04:54:19.211752Z","shell.execute_reply.started":"2021-11-13T04:54:19.195532Z","shell.execute_reply":"2021-11-13T04:54:19.210769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def valid_fn(vdataloader, model, criterion):\n    model.eval()\n    t = time.time()\n    losses = AverageMeter()\n    pbar = tqdm(enumerate(vdataloader), total=len(vdataloader))\n    for i_batch, sample_batched in pbar:\n        im = sample_batched['image']\n        batch_size = im.size(0)\n        im = im.to(device, dtype=torch.float)\n        pts = sample_batched['scaled_pts']\n        pts = pts.to(device, dtype=torch.float)\n        with torch.no_grad():\n            output = net(im)\n        loss = criterion(output, pts)\n        losses.update(loss.item(), batch_size)\n    return losses.avg","metadata":{"papermill":{"duration":0.035788,"end_time":"2021-11-12T10:16:50.546986","exception":false,"start_time":"2021-11-12T10:16:50.511198","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:19.216330Z","iopub.execute_input":"2021-11-13T04:54:19.216710Z","iopub.status.idle":"2021-11-13T04:54:19.242500Z","shell.execute_reply.started":"2021-11-13T04:54:19.216552Z","shell.execute_reply":"2021-11-13T04:54:19.241484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folds = train.copy()\nFold = KFold(n_splits=5, shuffle=True, random_state=42)\nfor n, (train_index, val_index) in enumerate(Fold.split(folds, folds['landmarks'])):\n    folds.loc[val_index, 'fold'] = int(n)\nfolds['fold'] = folds['fold'].astype(int)\nprint(folds.shape)","metadata":{"papermill":{"duration":0.04161,"end_time":"2021-11-12T10:16:50.614546","exception":false,"start_time":"2021-11-12T10:16:50.572936","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:19.244677Z","iopub.execute_input":"2021-11-13T04:54:19.245275Z","iopub.status.idle":"2021-11-13T04:54:19.266280Z","shell.execute_reply.started":"2021-11-13T04:54:19.245239Z","shell.execute_reply":"2021-11-13T04:54:19.265006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_loaders(fold_num,df=folds):\n    train_df = df[df.fold != fold_num].reset_index(drop=True)\n    valid_df = df[df.fold == fold_num].reset_index(drop=True)\n    \n    train_dataset = TrainDataset(train_df)\n    valid_dataset = TrainDataset(valid_df)\n\n    train_loader = DataLoader(train_dataset, batch_size=16, \n                              num_workers=2, shuffle=True, pin_memory=True, drop_last=True)\n    valid_loader = DataLoader(valid_dataset, batch_size=32, \n                              num_workers=2, shuffle=False, pin_memory=True)\n    print('train size:',train_df.shape[0])\n    print('valid size:',valid_df.shape[0])\n    \n    return train_loader, valid_loader","metadata":{"papermill":{"duration":0.032777,"end_time":"2021-11-12T10:16:50.671809","exception":false,"start_time":"2021-11-12T10:16:50.639032","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:19.271683Z","iopub.execute_input":"2021-11-13T04:54:19.271884Z","iopub.status.idle":"2021-11-13T04:54:19.279903Z","shell.execute_reply.started":"2021-11-13T04:54:19.271860Z","shell.execute_reply":"2021-11-13T04:54:19.279061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resnet50 = models.resnet50()\n# resnet50.conv1 = torch.nn.Conv2d(1,64, kernel_size=(7,7),stride=(2,2),padding=(3,3),bias=False)\n# resnet50.fc = torch.nn.Linear(512 * resnet50.layer1[0].expansion,68*2)\n# device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n# print(device)","metadata":{"papermill":{"duration":0.030206,"end_time":"2021-11-12T10:16:50.726373","exception":false,"start_time":"2021-11-12T10:16:50.696167","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:19.281111Z","iopub.execute_input":"2021-11-13T04:54:19.281358Z","iopub.status.idle":"2021-11-13T04:54:19.296156Z","shell.execute_reply.started":"2021-11-13T04:54:19.281324Z","shell.execute_reply":"2021-11-13T04:54:19.295249Z"},"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":{"papermill":{"duration":0.032351,"end_time":"2021-11-12T10:16:50.783177","exception":false,"start_time":"2021-11-12T10:16:50.750826","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:19.297542Z","iopub.execute_input":"2021-11-13T04:54:19.297838Z","iopub.status.idle":"2021-11-13T04:54:19.308209Z","shell.execute_reply.started":"2021-11-13T04:54:19.297797Z","shell.execute_reply":"2021-11-13T04:54:19.307304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fold = 3\ntrain_loader, valid_loader = prepare_loaders(fold_num=fold)\nseed=42\nset_seed(seed)\nepochs=30\nnet = effnet('tf_efficientnet_b4_ns')\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\nnet = net.to(device, dtype=torch.float)\n# Define loss_fn\ncriterion = nn.MSELoss()\nlearning_rate = 1e-4\noptimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)\nstart = time.time()\n\nlog_df = pd.DataFrame(columns=(['EPOCH']+['TRAIN_LOSS']+['VALID_LOSS']))\nbest_loss = np.inf\nfor epoch in range(epochs):\n    train_loss = train_fn(train_loader, net, optimizer, criterion, device)\n    valid_loss = valid_fn(valid_loader, net, criterion)\n    log_row = {'EPOCH': epoch, \n               'TRAIN_LOSS': train_loss,\n               'VALID_LOSS': valid_loss,\n              }\n    print('train_loss: ',train_loss)\n    print('valid_loss: ',valid_loss)\n    log_df = log_df.append(pd.DataFrame(log_row, index=[0]), sort=False)\n    if valid_loss < best_loss:\n        logger.info(f'epoch{epoch} save best model... {valid_loss}')\n        best_loss = valid_loss\n        torch.save(net.state_dict(), f\"fold{fold}_epoch{epoch}_seed{seed}.pth\")\nend = time.time()\nprint(\"time= \" + str(end-start))","metadata":{"papermill":{"duration":7301.217699,"end_time":"2021-11-12T12:18:32.025902","exception":false,"start_time":"2021-11-12T10:16:50.808203","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:54:19.311102Z","iopub.execute_input":"2021-11-13T04:54:19.312148Z","iopub.status.idle":"2021-11-13T04:55:03.010052Z","shell.execute_reply.started":"2021-11-13T04:54:19.312083Z","shell.execute_reply":"2021-11-13T04:55:03.008847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(log_df.EPOCH, log_df.TRAIN_LOSS, label=\"Train\")\nplt.plot(log_df.EPOCH, log_df.VALID_LOSS, label=\"Validation\")\nplt.legend(loc=\"upper right\")\nplt.savefig('loss_graph.jpeg')","metadata":{"papermill":{"duration":3.31044,"end_time":"2021-11-12T12:18:38.240940","exception":false,"start_time":"2021-11-12T12:18:34.930500","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:55:03.011500Z","iopub.status.idle":"2021-11-13T04:55:03.012085Z","shell.execute_reply.started":"2021-11-13T04:55:03.011824Z","shell.execute_reply":"2021-11-13T04:55:03.011850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#overfit 1 img \n# df=train.iloc[:2,:]\n# test_dataset = TrainDataset(df)\n\n# test_loader = DataLoader(test_dataset, batch_size=1, \n#                           num_workers=2, shuffle=True, pin_memory=True, drop_last=True)","metadata":{"papermill":{"duration":3.314733,"end_time":"2021-11-12T12:18:44.847688","exception":false,"start_time":"2021-11-12T12:18:41.532955","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:55:03.013280Z","iopub.status.idle":"2021-11-13T04:55:03.013826Z","shell.execute_reply.started":"2021-11-13T04:55:03.013589Z","shell.execute_reply":"2021-11-13T04:55:03.013616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# epochs=2\n# net = resnet18\n# net = net.to(device, dtype=torch.float)\n# # Define loss_fn\n# criterion = nn.MSELoss()\n# learning_rate = 1e-4\n# optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)\n# start = time.time()\n\n# for epoch in range(epochs):\n#     train_loss = train_fn(test_loader, net, optimizer, criterion, device)\n#     print(train_loss)\n#     torch.save(net.state_dict(), f\"fold{fold}_epoch{epoch}.pth\")\n# end = time.time()\n# print(\"time= \" + str(end-start))","metadata":{"papermill":{"duration":3.201107,"end_time":"2021-11-12T12:18:51.009764","exception":false,"start_time":"2021-11-12T12:18:47.808657","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:55:03.014909Z","iopub.status.idle":"2021-11-13T04:55:03.015481Z","shell.execute_reply.started":"2021-11-13T04:55:03.015243Z","shell.execute_reply":"2021-11-13T04:55:03.015267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resnet18.load_state_dict(torch.load('./fold0_epoch293.pth'))\n# net = resnet18\n# net = net.to(device, dtype=torch.float)","metadata":{"papermill":{"duration":2.931986,"end_time":"2021-11-12T12:18:56.802811","exception":false,"start_time":"2021-11-12T12:18:53.870825","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:55:03.016576Z","iopub.status.idle":"2021-11-13T04:55:03.017130Z","shell.execute_reply.started":"2021-11-13T04:55:03.016881Z","shell.execute_reply":"2021-11-13T04:55:03.016908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# im_list=[]\n# x_l=[]\n# y_l=[]\n\n# with torch.no_grad():\n#     pbar = tqdm(enumerate(test_loader), total=len(test_loader))\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#         im_list.append(sample_batched['ori_image'])\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#             result.append(output)","metadata":{"papermill":{"duration":3.22772,"end_time":"2021-11-12T12:19:02.928474","exception":false,"start_time":"2021-11-12T12:18:59.700754","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:55:03.018227Z","iopub.status.idle":"2021-11-13T04:55:03.018780Z","shell.execute_reply.started":"2021-11-13T04:55:03.018540Z","shell.execute_reply":"2021-11-13T04:55:03.018567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(im_list[1].cpu().squeeze(),cmap='gray')\n# plt.scatter(x_l[1],y_l[1])","metadata":{"papermill":{"duration":2.90234,"end_time":"2021-11-12T12:19:08.735320","exception":false,"start_time":"2021-11-12T12:19:05.832980","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:55:03.019875Z","iopub.status.idle":"2021-11-13T04:55:03.020447Z","shell.execute_reply.started":"2021-11-13T04:55:03.020204Z","shell.execute_reply":"2021-11-13T04:55:03.020240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import glob\n\n# files = glob.glob('./*')\n# for f in files:\n#     os.remove(f)","metadata":{"papermill":{"duration":2.952357,"end_time":"2021-11-12T12:19:14.911329","exception":false,"start_time":"2021-11-12T12:19:11.958972","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-13T04:55:03.021521Z","iopub.status.idle":"2021-11-13T04:55:03.022080Z","shell.execute_reply.started":"2021-11-13T04:55:03.021824Z","shell.execute_reply":"2021-11-13T04:55:03.021850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":3.103992,"end_time":"2021-11-12T12:19:21.204129","exception":false,"start_time":"2021-11-12T12:19:18.100137","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":2.928522,"end_time":"2021-11-12T12:19:27.237745","exception":false,"start_time":"2021-11-12T12:19:24.309223","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}