{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"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 cv2\nfrom tqdm import tqdm_notebook as tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom functools import reduce\nimport os\nfrom sklearn.model_selection import train_test_split\nfrom scipy.optimize import minimize\nfrom tqdm.auto import tqdm as tq\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import models\nfrom torchvision import transforms, utils\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom math import sqrt, acos, pi, sin, cos\nfrom scipy.spatial.transform import Rotation as R\nfrom sklearn.metrics import average_precision_score\nfrom multiprocessing import Pool\n\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.utils.model_zoo as model_zoo\nimport gc\nimport torch\nimport torch.nn as nn\nfrom torch.hub import load_state_dict_from_url\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom math import sqrt, acos, pi, sin, cos\nfrom scipy.spatial.transform import Rotation as R\nfrom sklearn.metrics import average_precision_score\nfrom multiprocessing import Pool\n\n\nPATH = '../input/pku-autonomous-driving/'\nos.listdir(PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(PATH + 'train.csv')\ntest = pd.read_csv(PATH + 'sample_submission.csv')\nbad_list = ['ID_1a5a10365',\n'ID_1db0533c7',\n'ID_53c3fe91a',\n'ID_408f58e9f',\n'ID_4445ae041',\n'ID_bb1d991f6',\n'ID_c44983aeb',\n'ID_f30ebe4d4']\ntrain = train.loc[~train['ImageId'].isin(bad_list)]\n# From camera.zip\ncamera_matrix = np.array([[2304.5479, 0,  1686.2379],\n                          [0, 2305.8757, 1354.9849],\n                          [0, 0, 1]], dtype=np.float32)\ncamera_matrix_inv = np.linalg.inv(camera_matrix)\n\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"s=train[train['ImageId']=='ID_037f71efa']['PredictionString']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"s.tolist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[train['ImageId']=='ID_03d83bd0e']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def imread(path, fast_mode=False):\n    img = cv2.imread(path)\n    if not fast_mode and img is not None and len(img.shape) == 3:\n        img = np.array(img[:, :, ::-1])\n    return img\n\nimg = imread(PATH + 'train_images/ID_8a6e65317' + '.jpg')\nIMG_SHAPE = img.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def str2coords(s, names=['id', 'yaw', 'pitch', 'roll', 'x', 'y', 'z']):\n    '''\n    Input:\n        s: PredictionString (e.g. from train dataframe)\n        names: array of what to extract from the string\n    Output:\n        list of dicts with keys from `names`\n    '''\n    coords = []\n    for l in np.array(s.split()).reshape([-1, 7]):\n        coords.append(dict(zip(names, l.astype('float'))))\n        if 'id' in coords[-1]:\n            coords[-1]['id'] = int(coords[-1]['id'])\n    return coords","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"points_df = pd.DataFrame()\nfor col in ['x', 'y', 'z', 'yaw', 'pitch', 'roll']:\n    arr = []\n    for ps in train['PredictionString']:\n        coords = str2coords(ps)\n        arr += [c[col] for c in coords]\n    points_df[col] = arr\n\nprint('len(points_df)', len(points_df))\npoints_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def rotate(x, angle):\n    x = x + angle\n    x = x - (x + np.pi) // (2 * np.pi) * 2 * np.pi\n    return x","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"below function is to get pixel coordinates from the (X, Y, Z) which are the coordinates of a 3D point in the world coordinate space"},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_img_coords(s):\n    '''\n    Input is a PredictionString (e.g. from train dataframe)\n    Output is two arrays:\n        xs: x coordinates in the image\n        ys: y coordinates in the image\n    '''\n    coords = str2coords(s)\n    xs = [c['x'] for c in coords]\n    ys = [c['y'] for c in coords]\n    zs = [c['z'] for c in coords]\n    P = np.array(list(zip(xs, ys, zs))).T\n    img_p = np.dot(camera_matrix, P).T\n    img_p[:, 0] /= img_p[:, 2]\n    img_p[:, 1] /= img_p[:, 2]\n    img_xs = img_p[:, 0]\n    img_ys = img_p[:, 1]\n    img_zs = img_p[:, 2] # z = Distance from the camera\n    return img_xs, img_ys\n\nplt.figure(figsize=(14,14))\nplt.imshow(imread(PATH + 'train_images/' + train['ImageId'][3474] + '.jpg'))\nplt.scatter(*get_img_coords(train['PredictionString'][3474]), color='red', s=100);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from math import sin, cos\n\n# convert euler angle to rotation matrix\ndef euler_to_Rot(yaw, pitch, roll):\n    Y = np.array([[cos(yaw), 0, sin(yaw)],\n                  [0, 1, 0],\n                  [-sin(yaw), 0, cos(yaw)]])\n    P = np.array([[1, 0, 0],\n                  [0, cos(pitch), -sin(pitch)],\n                  [0, sin(pitch), cos(pitch)]])\n    R = np.array([[cos(roll), -sin(roll), 0],\n                  [sin(roll), cos(roll), 0],\n                  [0, 0, 1]])\n    return np.dot(Y, np.dot(P, R))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_line(image, points):\n    color = (255, 0, 0)\n    cv2.line(image, tuple(points[1][:2]), tuple(points[2][:2]), color, 16)\n    cv2.line(image, tuple(points[1][:2]), tuple(points[4][:2]), color, 16)\n\n    cv2.line(image, tuple(points[1][:2]), tuple(points[5][:2]), color, 16)\n    cv2.line(image, tuple(points[2][:2]), tuple(points[3][:2]), color, 16)\n    cv2.line(image, tuple(points[2][:2]), tuple(points[6][:2]), color, 16)\n    cv2.line(image, tuple(points[3][:2]), tuple(points[4][:2]), color, 16)\n    cv2.line(image, tuple(points[3][:2]), tuple(points[7][:2]), color, 16)\n\n    cv2.line(image, tuple(points[4][:2]), tuple(points[8][:2]), color, 16)\n    cv2.line(image, tuple(points[5][:2]), tuple(points[8][:2]), color, 16)\n\n    cv2.line(image, tuple(points[5][:2]), tuple(points[6][:2]), color, 16)\n    cv2.line(image, tuple(points[6][:2]), tuple(points[7][:2]), color, 16)\n    cv2.line(image, tuple(points[7][:2]), tuple(points[8][:2]), color, 16)\n    return image\n\n\n# def draw_points(image, points):\n#     image = np.array(image)\n#     for (p_x, p_y, p_z) in points:\n#         # print(\"p_x, p_y\", p_x, p_y)\n#         cv2.circle(image, (p_x, p_y), 5, (255, 0, 0), -1)\n#     return image\n\ndef draw_points(image, points):\n    image = np.array(image)\n    #print('points for point:',points)\n    for (p_x, p_y, p_z) in points:\n        cv2.circle(image, (p_x, p_y),30, (0, 255, 0), -1)\n        if p_x > image.shape[1] or p_y > image.shape[0]:\n            print('Point', p_x, p_y, 'is out of image with shape', image.shape)\n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def draw_line(image, points):\n#     color = (255, 0, 0)\n#     print('points:',points)\n#     cv2.line(image, tuple(points[0][:2]), tuple(points[3][:2]), color, 16)\n#     cv2.line(image, tuple(points[0][:2]), tuple(points[1][:2]), color, 16)\n#     cv2.line(image, tuple(points[1][:2]), tuple(points[2][:2]), color, 16)\n#     cv2.line(image, tuple(points[2][:2]), tuple(points[3][:2]), color, 16)\n#     return image\n\n\n# def draw_points(image, points):\n#     print('points for point:',points)\n#     for (p_x, p_y, p_z) in points:\n#         cv2.circle(image, (p_x, p_y), int(1000 / p_z), (0, 255, 0), -1)\n#         if p_x > image.shape[1] or p_y > image.shape[0]:\n#             print('Point', p_x, p_y, 'is out of image with shape', image.shape)\n#     return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def visualize(img, coords):\n    x_l = 1.02\n    y_l = 0.80\n    z_l = 2.31\n    img = img.copy()\n    #print('img:',img)\n    for point in coords:\n        # Get values\n        x, y, z = point['x'], point['y'], point['z']\n        yaw, pitch, roll = -point['pitch'], -point['yaw'], -point['roll']\n#     for yaw, pitch, roll, x, y, z in zip(yaws, pitches, rolls, xs, ys, zs):\n#         yaw, pitch, roll, x, y, z = [float(x) for x in [yaw, pitch, roll, x, y, z]]\n#         # I think the pitch and yaw should be exchanged\n#         yaw, pitch, roll = -pitch, -yaw, -roll\n        Rt = np.eye(4)\n        t = np.array([x, y, z])\n        Rt[:3, 3] = t\n        Rt[:3, :3] = euler_to_Rot(yaw, pitch, roll).T\n        Rt = Rt[:3, :]\n        P = np.array([[0, 0, 0, 1],\n                      [x_l, y_l, -z_l, 1],\n                      [x_l, y_l, z_l, 1],\n                      [-x_l, y_l, z_l, 1],\n                      [-x_l, y_l, -z_l, 1],\n                      [x_l, -y_l, -z_l, 1],\n                      [x_l, -y_l, z_l, 1],\n                      [-x_l, -y_l, z_l, 1],\n                      [-x_l, -y_l, -z_l, 1]]).T\n        img_cor_points = np.dot(camera_matrix, np.dot(Rt, P))\n        img_cor_points = img_cor_points.T\n        img_cor_points[:, 0] /= img_cor_points[:, 2]\n        img_cor_points[:, 1] /= img_cor_points[:, 2]\n        # call this function before chage the dtype\n        #img_cor_2_world_cor()\n        img_cor_points = img_cor_points.astype(int)\n        #print('img_cor_points:',img_cor_points)\n        img = draw_points(img, img_cor_points[0:1])\n        img = draw_line(img, img_cor_points)\n\n#     img = Image.fromarray(img)\n#     plt.imshow(img)\n#     plt.show()\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def visualize(img, coords):\n#     # You will also need functions from the previous cells\n#     x_l = 1.02\n#     y_l = 0.80\n#     z_l = 2.31\n    \n#     img = img.copy()\n#     print('img:',img)\n#     for point in coords:\n#         # Get values\n#         x, y, z = point['x'], point['y'], point['z']\n#         yaw, pitch, roll = -point['pitch'], -point['yaw'], -point['roll']\n#         # Math\n#         Rt = np.eye(4)\n#         print('Rt:',Rt)\n#         t = np.array([x, y, z])\n#         print('t:',t)\n#         Rt[:3, 3] = t\n#         print('Rt:',Rt)\n#         Rt[:3, :3] = euler_to_Rot(yaw, pitch, roll).T\n#         print('Rt:',Rt)\n#         Rt = Rt[:3, :]\n#         print('Rt:',Rt)\n#         P = np.array([[x_l, -y_l, -z_l, 1],\n#                       [x_l, -y_l, z_l, 1],\n#                       [-x_l, -y_l, z_l, 1],\n#                       [-x_l, -y_l, -z_l, 1],\n#                       [0, 0, 0, 1]]).T\n#         print('P:',P)\n#         img_cor_points = np.dot(camera_matrix, np.dot(Rt, P))\n#         print('img_cor_points:',img_cor_points)\n#         img_cor_points = img_cor_points.T\n#         print('img_cor_points T:',img_cor_points)\n#         img_cor_points[:, 0] /= img_cor_points[:, 2]\n#         img_cor_points[:, 1] /= img_cor_points[:, 2]\n#         img_cor_points = img_cor_points.astype(int)\n#         print('img_cor_points:',img_cor_points)\n#         # Drawing\n#         # img_cor_points has 5 rows, first 4 are for corner points which are used to draw lines\n#         # and last is for center which is used to draw center points for car\n#         img = draw_line(img, img_cor_points)\n#         img = draw_points(img, img_cor_points[-1:])\n    \n#     return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_absolute_error, mean_squared_error\n\nzy_slope = LinearRegression()\nX = points_df[['z']]\ny = points_df['y']\nzy_slope.fit(X, y)\nprint('MAE without x:', mean_absolute_error(y, zy_slope.predict(X)))\n\n# Will use this model later\nxzy_slope = LinearRegression()\nX = points_df[['x', 'z']]\ny = points_df['y']\nxzy_slope.fit(X, y)\nprint('MAE with x:', mean_absolute_error(y, xzy_slope.predict(X)))\n\nprint('\\ndy/dx = {:.3f}\\ndy/dz = {:.3f}'.format(*xzy_slope.coef_))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_WIDTH = 1024\nIMG_HEIGHT = IMG_WIDTH // 16 * 5\nMODEL_SCALE = 8\n\ndef _regr_preprocess(regr_dict, flip=False):\n    if flip:\n        for k in ['x', 'pitch', 'roll']:\n            regr_dict[k] = -regr_dict[k]\n    for name in ['x', 'y', 'z']:\n        regr_dict[name] = regr_dict[name] / 100\n    regr_dict['roll'] = rotate(regr_dict['roll'], np.pi)\n    regr_dict['pitch_sin'] = sin(regr_dict['pitch'])\n    regr_dict['pitch_cos'] = cos(regr_dict['pitch'])\n    regr_dict.pop('pitch')\n    regr_dict.pop('id')\n    return regr_dict\n\ndef _regr_back(regr_dict):\n    for name in ['x', 'y', 'z']:\n        regr_dict[name] = regr_dict[name] * 100\n    regr_dict['roll'] = rotate(regr_dict['roll'], -np.pi)\n    \n    pitch_sin = regr_dict['pitch_sin'] / np.sqrt(regr_dict['pitch_sin']**2 + regr_dict['pitch_cos']**2)\n    pitch_cos = regr_dict['pitch_cos'] / np.sqrt(regr_dict['pitch_sin']**2 + regr_dict['pitch_cos']**2)\n    regr_dict['pitch'] = np.arccos(pitch_cos) * np.sign(pitch_sin)\n    return regr_dict\n\ndef preprocess_image(img, flip=False):\n    img = img[img.shape[0] // 2:]\n    bg = np.ones_like(img) * img.mean(1, keepdims=True).astype(img.dtype)\n    bg = bg[:, :img.shape[1] // 6]\n    img = np.concatenate([bg, img, bg], 1)\n    img = cv2.resize(img, (IMG_WIDTH, IMG_HEIGHT))\n    if flip:\n        img = img[:,::-1]\n    return (img / 255).astype('float32')\n\ndef get_mask_and_regr(img, labels, flip=False):\n    mask = np.zeros([IMG_HEIGHT // MODEL_SCALE, IMG_WIDTH // MODEL_SCALE], dtype='float32')\n    regr_names = ['x', 'y', 'z', 'yaw', 'pitch', 'roll']\n    regr = np.zeros([IMG_HEIGHT // MODEL_SCALE, IMG_WIDTH // MODEL_SCALE, 7], dtype='float32')\n    coords = str2coords(labels)\n    xs, ys = get_img_coords(labels)\n    for x, y, regr_dict in zip(xs, ys, coords):\n        x, y = y, x\n        x = (x - img.shape[0] // 2) * IMG_HEIGHT / (img.shape[0] // 2) / MODEL_SCALE\n        x = np.round(x).astype('int')\n        y = (y + img.shape[1] // 6) * IMG_WIDTH / (img.shape[1] * 4/3) / MODEL_SCALE\n        y = np.round(y).astype('int')\n        if x >= 0 and x < IMG_HEIGHT // MODEL_SCALE and y >= 0 and y < IMG_WIDTH // MODEL_SCALE:\n            mask[x, y] = 1\n            regr_dict = _regr_preprocess(regr_dict, flip)\n            regr[x, y] = [regr_dict[n] for n in sorted(regr_dict)]\n    if flip:\n        mask = np.array(mask[:,::-1])\n        regr = np.array(regr[:,::-1])\n    return mask, regr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# IMG_WIDTH = 1536\n# IMG_HEIGHT = 512\n# MODEL_SCALE = 8\n\n# def _regr_preprocess(regr_dict):\n#     for name in ['x', 'y', 'z']:\n#         regr_dict[name] = regr_dict[name] / 100\n#     regr_dict['roll'] = rotate(regr_dict['roll'], np.pi)\n#     regr_dict['pitch_sin'] = sin(regr_dict['pitch'])\n#     regr_dict['pitch_cos'] = cos(regr_dict['pitch'])\n#     regr_dict.pop('pitch')\n#     regr_dict.pop('id')\n#     return regr_dict\n\n# def _regr_back(regr_dict):\n#     for name in ['x', 'y', 'z']:\n#         regr_dict[name] = regr_dict[name] * 100\n#     regr_dict['roll'] = rotate(regr_dict['roll'], -np.pi)\n    \n#     pitch_sin = regr_dict['pitch_sin'] / np.sqrt(regr_dict['pitch_sin']**2 + regr_dict['pitch_cos']**2)\n#     pitch_cos = regr_dict['pitch_cos'] / np.sqrt(regr_dict['pitch_sin']**2 + regr_dict['pitch_cos']**2)\n#     regr_dict['pitch'] = np.arccos(pitch_cos) * np.sign(pitch_sin)\n#     return regr_dict\n\n# def preprocess_image(img):\n#     img = img[img.shape[0] // 2:]\n#     bg = np.ones_like(img) * img.mean(1, keepdims=True).astype(img.dtype)\n#     bg = bg[:, :img.shape[1] // 4]\n#     img = np.concatenate([bg, img, bg], 1)\n#     img = cv2.resize(img, (IMG_WIDTH, IMG_HEIGHT))\n#     return (img / 255).astype('float32')\n\n# def get_mask_and_regr(img, labels):\n#     mask = np.zeros([IMG_HEIGHT // MODEL_SCALE, IMG_WIDTH // MODEL_SCALE], dtype='float32')\n#     regr_names = ['x', 'y', 'z', 'yaw', 'pitch', 'roll']\n#     regr = np.zeros([IMG_HEIGHT // MODEL_SCALE, IMG_WIDTH // MODEL_SCALE, 7], dtype='float32')\n#     coords = str2coords(labels)\n#     #print('coords:',coords)\n#     xs, ys = get_img_coords(labels)\n#     #print(xs,ys)\n#     for x, y, regr_dict in zip(xs, ys, coords):\n#         #print(x,y,regr_dict)\n#         x, y = y, x\n#         x = (x - img.shape[0] // 2) * IMG_HEIGHT / (img.shape[0] // 2) / MODEL_SCALE\n#         x = np.round(x).astype('int')\n#         y = (y + img.shape[1] // 4) * IMG_WIDTH / (img.shape[1] * 1.5) / MODEL_SCALE\n#         y = np.round(y).astype('int')\n#         #print(x,y)\n#         #print(regr_dict)\n#         if x >= 0 and x < IMG_HEIGHT // MODEL_SCALE and y >= 0 and y < IMG_WIDTH // MODEL_SCALE:\n#             #print('m b', mask[y])\n#             mask[x, y] = 1\n#             #print('m a',mask[y])\n#             regr_dict = _regr_preprocess(regr_dict)\n#             #print('after:',regr_dict)\n#             #print('sorted:',sorted(regr_dict))\n#             regr[x, y] = [regr_dict[n] for n in sorted(regr_dict)]\n#             #print('final',regr[x,y])\n#     return mask, regr","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"wherever x,y points are there, in mask array those points are marked as one and then they are plotted in graph, thus wherever car is there it is detected in mask plot\n\nand in regression plot we can plot any one of the seven values on those x,y points where car is detected:\nsorted: ['pitch_cos', 'pitch_sin', 'roll', 'x', 'y', 'yaw', 'z']"},{"metadata":{},"cell_type":"markdown","source":"regression values are plotted colorwise\nlike whichever is positive it is colored as yellow, whichever is highly negative it is colored as dark blue"},{"metadata":{"trusted":true},"cell_type":"code","source":"img0 = imread(PATH + 'train_imagePrivs/' + train['ImageId'][0] + '.jpg')\n#print(PATH + 'train_images/' + train['ImageId'][0] + '.jpg')\nimg = preprocess_image(img0)\n\nmask, regr = get_mask_and_regr(img0, train['PredictionString'][0])\n#print('regr:',regr[:,:,-2])\n#print('img.shape', img.shape, 'std:', np.std(img))\n#print('mask.shape', mask.shape, 'std:', np.std(mask))\n#print('regr.shape', regr.shape, 'std:', np.std(regr))\n\nplt.figure(figsize=(16,16))\nplt.title('Processed image')\nplt.imshow(img)\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('Detection Mask')\nplt.imshow(mask)\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('pitch cos values')\nplt.imshow(regr[:,:,0])\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('pitch sin values')\nplt.imshow(regr[:,:,1])\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('roll')\nplt.imshow(regr[:,:,2])\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('x')\nplt.imshow(regr[:,:,3])\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('y')\nplt.imshow(regr[:,:,4])\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('Yaw values')\nplt.imshow(regr[:,:,5])\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('z')\nplt.imshow(regr[:,:,6])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DISTANCE_THRESH_CLEAR = 2\n\ndef convert_3d_to_2d(x, y, z, fx = 2304.5479, fy = 2305.8757, cx = 1686.2379, cy = 1354.9849):\n    # stolen from https://www.kaggle.com/theshockwaverider/eda-visualization-baseline\n    return x * fx / z + cx, y * fy / z + cy\n\ndef optimize_xy(r, c, x0, y0, z0, flipped=False):\n    def distance_fn(xyz):\n        x, y, z = xyz\n        xx = -x if flipped else x\n        slope_err = (xzy_slope.predict([[xx,z]])[0] - y)**2\n        x, y = convert_3d_to_2d(x, y, z)\n        y, x = x, y\n        x = (x - IMG_SHAPE[0] // 2) * IMG_HEIGHT / (IMG_SHAPE[0] // 2) / MODEL_SCALE\n        y = (y + IMG_SHAPE[1] // 6) * IMG_WIDTH / (IMG_SHAPE[1] * 4 / 3) / MODEL_SCALE\n        #print ('optimize:',max(0.2, (x-r)**2 + (y-c)**2) + max(0.4, slope_err))\n        return max(0.2, (x-r)**2 + (y-c)**2) + max(0.4, slope_err)\n    \n    res = minimize(distance_fn, [x0, y0, z0], method='Powell')\n    #print('res:',res)\n    x_new, y_new, z_new = res.x\n    #print('new parameters',x_new, y_new, z_new)\n    return x_new, y_new, z_new\n\ndef clear_duplicates(coords):\n    for c1 in coords:\n        #print('coords:',coords)\n        #print('c1:',c1)\n        xyz1 = np.array([c1['x'], c1['y'], c1['z']])\n        #print('xyz1:',xyz1)\n        for c2 in coords:\n            #print('c2:',c2)\n            xyz2 = np.array([c2['x'], c2['y'], c2['z']])\n            #print('xyz2:',xyz2)\n            distance = np.sqrt(((xyz1 - xyz2)**2).sum())\n            #print('distance:',distance)\n            if distance < DISTANCE_THRESH_CLEAR:\n                #print('c1-confidence',c1['confidence'])\n                #print('c2-confidence',c2['confidence'])\n                if c1['confidence'] < c2['confidence']:\n                    c1['confidence'] = -1\n    #print('clear_duplicates:',[c for c in coords if c['confidence'] > 0])\n    return [c for c in coords if c['confidence'] > 0]\n\ndef extract_coords(prediction, flipped=False):\n    logits = prediction[0]\n    #print('logits:',logits)\n    regr_output = prediction[1:]\n    #print('regr_output:',regr_output)\n    points = np.argwhere(logits > 0)\n    #print('points:',points)\n    #print('alt_points:',[np.nonzero(logits)])\n    col_names = sorted(['x', 'y', 'z', 'yaw', 'pitch_sin', 'pitch_cos', 'roll'])\n    #print('col_names:',col_names)\n    coords = []\n    for r, c in points:\n        #print('r:',r)\n        #print('c:',c)\n        regr_dict = dict(zip(col_names, regr_output[:, r, c]))\n        # values of all seven points we need at the point where object or car is found\n        #print('regr_output:',regr_output[:, r, c])\n        #print('regr_dict:',regr_dict)\n        coords.append(_regr_back(regr_dict))\n        #print('coords appended:',coords)\n        coords[-1]['confidence'] = 1 / (1 + np.exp(-logits[r, c]))\n        coords[-1]['x'], coords[-1]['y'], coords[-1]['z'] = \\\n                optimize_xy(r, c,\n                            coords[-1]['x'],\n                            coords[-1]['y'],\n                            coords[-1]['z'], flipped)\n        #print('coords optimized:',coords)\n    coords = clear_duplicates(coords)\n    #print('after clear:',coords)\n    return coords\n\ndef coords2str(coords, names=['yaw', 'pitch', 'roll', 'x', 'y', 'z', 'confidence']):\n    s = []\n    for c in coords:\n        for n in names:\n            s.append(str(c.get(n, 0)))\n    #print('s',s)\n    return ' '.join(s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for idx in range(100,110):\n    fig, axes = plt.subplots(1, 2, figsize=(20,20))\n    \n    for ax_i in range(2):\n        print(\"image_name:\",PATH + 'train_images/' + train['ImageId'].iloc[idx] + '.jpg')\n        img0 = imread(PATH + 'train_images/' + train['ImageId'].iloc[idx] + '.jpg')\n        if ax_i == 1:\n            img0 = img0[:,::-1]\n        img = preprocess_image(img0, ax_i==1)\n        mask, regr = get_mask_and_regr(img0, train['PredictionString'][idx], ax_i==1)\n        #print('mask:',type(mask))\n        #print('shape_mask:',mask.shape)\n        #print('mask none:',mask[None])\n        #print('shape_regr:',regr.shape)\n        regr = np.rollaxis(regr, 2, 0)\n        #print('concatenate:',np.concatenate([mask[None], regr], 0))\n        xx=np.concatenate([mask[None], regr], 0)\n        #print('shape xx:',xx.shape)\n        coords = extract_coords(np.concatenate([mask[None], regr], 0), ax_i==1)\n        \n        axes[ax_i].set_title('Flip = {}'.format(ax_i==1))\n        axes[ax_i].imshow(visualize(img0, coords))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class CarDataset(Dataset):\n    \"\"\"Car dataset.\"\"\"\n\n    def __init__(self, dataframe, root_dir, training=True, transform=None):\n        self.df = dataframe\n        self.root_dir = root_dir\n        self.transform = transform\n        self.training = training\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        if torch.is_tensor(idx):\n            idx = idx.tolist()\n        \n        # Get image name\n        idx, labels = self.df.values[idx]\n        #if idx=='ID_a59ad3974':\n        print('idx:',idx)\n        #print('index:',self.df.index)\n        #print('labels',labels)\n        img_name = self.root_dir.format(idx)\n        #print('name:',img_name)\n        \n        # Read image\n        img0 = imread(img_name, True)\n        img = preprocess_image(img0)\n        img = np.rollaxis(img, 2, 0)\n        \n        # Get mask and regression maps\n        if self.training:\n            mask, regr = get_mask_and_regr(img0, labels)\n            regr = np.rollaxis(regr, 2, 0)\n        else:\n            mask, regr = 0, 0\n        \n        return [img, mask, regr]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# img_name=file\n# img0 = imread(img_name, True)\n# img = preprocess_image(img0)\n# img = np.rollaxis(img, 2, 0)\n# mask, regr = 0, 0\n# output = model(torch.tensor(img[None]).to(device)).data.cpu().numpy()\n# coords_pred = extract_coords(output[0])\n# print(coords_pred)\n# img = imread(file)\n# fig, axes = plt.subplots(1, 1, figsize=(30,30))\n# axes.set_title('Prediction')\n# axes.imshow(visualize(img, coords_pred))\n# plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_dir = PATH + 'train_images/{}.jpg'\ntest_images_dir = PATH + 'test_images/{}.jpg'\n\ndf_train, df_dev = train_test_split(train, test_size=0.08, random_state=63)\ndf_test = test\n\n# Create dataset objects\ntrain_dataset = CarDataset(df_train, train_images_dir)\ndev_dataset = CarDataset(df_dev, train_images_dir)\ntest_dataset = CarDataset(df_test, test_images_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dataset[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img, mask, regr = train_dataset[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"regr.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# this is to change the shape of regr from (40,128,7) to (7,40,128)\nregr = np.zeros([IMG_HEIGHT // MODEL_SCALE, IMG_WIDTH // MODEL_SCALE, 7], dtype='float32')\nprint('shape before:',regr.shape)\nregr = np.rollaxis(regr, 2, 0)\nprint('shape after:',regr.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mask.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"regr.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d=np.concatenate([mask[None], regr], 0)\nd.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d[:,0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d[:,1:].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"(regr[np.nonzero(regr)])\n# regression values present for x,y points where car is detected in order: sorted: ['pitch_cos', 'pitch_sin', 'roll', 'x', 'y', 'yaw', 'z']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"[np.nonzero(regr)]\n# index of non zero values present in regression array","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img, mask, regr = train_dataset[3473]\n\nplt.figure(figsize=(16,16))\nplt.imshow(np.rollaxis(img, 0, 3))\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.imshow(mask)\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.imshow(regr[-2])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE = 4\n\n# Create data generators - they will produce batches\ntrain_loader = DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)\ndev_loader = DataLoader(dataset=dev_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)\ntest_loader = DataLoader(dataset=test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_loader)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_dataset)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install efficientnet-pytorch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from efficientnet_pytorch import EfficientNet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class double_conv(nn.Module):\n    '''(conv => BN => ReLU) * 2'''\n    def __init__(self, in_ch, out_ch):\n        super(double_conv, self).__init__()\n        self.conv = nn.Sequential(\n            nn.Conv2d(in_ch, out_ch, 3, padding=1),\n            nn.BatchNorm2d(out_ch),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(out_ch, out_ch, 3, padding=1),\n            nn.BatchNorm2d(out_ch),\n            nn.ReLU(inplace=True)\n        )\n\n    def forward(self, x):\n        x = self.conv(x)\n        return x\n\nclass up(nn.Module):\n    def __init__(self, in_ch, out_ch, bilinear=True):\n        super(up, self).__init__()\n\n        #  would be a nice idea if the upsampling could be learned too,\n        #  but my machine do not have enough memory to handle all those weights\n        if bilinear:\n            self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)\n        else:\n            self.up = nn.ConvTranspose2d(in_ch//2, in_ch//2, 2, stride=2)\n\n        self.conv = double_conv(in_ch, out_ch)\n\n    def forward(self, x1, x2=None):\n        x1 = self.up(x1)\n        \n        # input is CHW\n        diffY = x2.size()[2] - x1.size()[2]\n        diffX = x2.size()[3] - x1.size()[3]\n\n        x1 = F.pad(x1, (diffX // 2, diffX - diffX//2,\n                        diffY // 2, diffY - diffY//2))\n        \n        # for padding issues, see \n        # https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a\n        # https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd\n        \n        if x2 is not None:\n            x = torch.cat([x2, x1], dim=1)\n        else:\n            x = x1\n        x = self.conv(x)\n        return x\n\ndef get_mesh(batch_size, shape_x, shape_y):\n    mg_x, mg_y = np.meshgrid(np.linspace(0, 1, shape_y), np.linspace(0, 1, shape_x))\n    mg_x = np.tile(mg_x[None, None, :, :], [batch_size, 1, 1, 1]).astype('float32')\n    mg_y = np.tile(mg_y[None, None, :, :], [batch_size, 1, 1, 1]).astype('float32')\n    mesh = torch.cat([torch.tensor(mg_x).to(device), torch.tensor(mg_y).to(device)], 1)\n    return mesh","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class MyUNet(nn.Module):\n    '''Mixture of previous classes'''\n    def __init__(self, n_classes):\n        super(MyUNet, self).__init__()\n        self.base_model = EfficientNet.from_pretrained('efficientnet-b0')\n        \n        self.conv0 = double_conv(5, 64)\n        self.conv1 = double_conv(64, 128)\n        self.conv2 = double_conv(128, 512)\n        self.conv3 = double_conv(512, 1024)\n        \n        self.mp = nn.MaxPool2d(2)\n        \n        self.up1 = up(1282 + 1024, 512)\n        self.up2 = up(512 + 512, 256)\n        self.outc = nn.Conv2d(256, n_classes, 1)\n\n    def forward(self, x):\n        batch_size = x.shape[0]\n        mesh1 = get_mesh(batch_size, x.shape[2], x.shape[3])\n        x0 = torch.cat([x, mesh1], 1)\n        x1 = self.mp(self.conv0(x0))\n        x2 = self.mp(self.conv1(x1))\n        x3 = self.mp(self.conv2(x2))\n        x4 = self.mp(self.conv3(x3))\n        \n        x_center = x[:, :, :, IMG_WIDTH // 8: -IMG_WIDTH // 8]\n        feats = self.base_model.extract_features(x_center)\n        bg = torch.zeros([feats.shape[0], feats.shape[1], feats.shape[2], feats.shape[3] // 8]).to(device)\n        feats = torch.cat([bg, feats, bg], 3)\n        \n        # Add positional info\n        mesh2 = get_mesh(batch_size, feats.shape[2], feats.shape[3])\n        feats = torch.cat([feats, mesh2], 1)\n        \n        x = self.up1(feats, x4)\n        x = self.up2(x, x3)\n        x = self.outc(x)\n        return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Gets the GPU if there is one, otherwise the cpu\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\nn_epochs = 1\n# 8 outputs-first one is for mask and other 7 for regression array\nmodel = MyUNet(8).to(device)\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n#exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=max(n_epochs, 10) * len(train_loader) // 3, gamma=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def criterion(prediction, mask, regr, size_average=True):\n    # Binary mask loss\n    pred_mask = torch.sigmoid(prediction[:, 0])\n    #print('prediction shape:',prediction.shape)\n    #print('prediction[:, 0] shape:',prediction[:, 0].shape)\n    #print('prediction[:, 0]:',prediction[:, 0])\n    #print('pred_mask',pred_mask)\n    #print('pred_mask shape',pred_mask.shape)\n#     mask_loss = mask * (1 - pred_mask)**2 * torch.log(pred_mask + 1e-12) + (1 - mask) * pred_mask**2 * torch.log(1 - pred_mask + 1e-12)\n    mask_loss = mask * torch.log(pred_mask + 1e-12) + (1 - mask) * torch.log(1 - pred_mask + 1e-12)\n    mask_loss = -mask_loss.mean(0).sum()\n    #print('mask loss:', mask_loss)\n    \n    # Regression L1 loss\n    pred_regr = prediction[:, 1:]\n    #print('prediction[:, 1:] shape:',prediction[:, 1:].shape)\n    #print('prediction[:, 1:]:',prediction[:, 1:])\n    #print('pred_regr',pred_regr)\n    #print('pred_regr shape',pred_regr.shape)\n    regr_loss = (torch.abs(pred_regr - regr).sum(1) * mask).sum(1).sum(1) / mask.sum(1).sum(1)\n    #print('regr_loss:', regr_loss)\n    regr_loss = regr_loss.mean(0)\n    #print('regr loss mean:',regr_loss)\n    \n    # Sum\n    loss = mask_loss + regr_loss\n    #print('loss:',loss)\n    if not size_average:\n        loss *= prediction.shape[0]\n    return loss","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def train_model(epoch, history=None):\n    model.train()\n\n    for batch_idx, (img_batch, mask_batch, regr_batch) in enumerate(tqdm(train_loader)):\n        #print('batch idx:',batch_idx)\n        img_batch = img_batch.to(device)\n        #print('img_batch',img_batch)\n        #print('img_batch size',img_batch.shape)\n        mask_batch = mask_batch.to(device)\n        #print('mask_batch',mask_batch)\n        #print('mask_batch size',mask_batch.shape)\n        regr_batch = regr_batch.to(device)\n        #print('regr_batch',regr_batch)\n        #print('regr_batch shape',regr_batch.shape)\n        \n        optimizer.zero_grad()\n        output = model(img_batch)\n        #print('output:', output)\n        #print('output shape:',output.shape)\n        loss = criterion(output, mask_batch, regr_batch)\n        if history is not None:\n            history.loc[epoch + batch_idx / len(train_loader), 'train_loss'] = loss.data.cpu().numpy()\n        \n        loss.backward()\n        \n        optimizer.step()\n        exp_lr_scheduler.step()\n    \n    print('Train Epoch: {} \\tLR: {:.6f}\\tLoss: {:.6f}'.format(\n        epoch,\n        optimizer.state_dict()['param_groups'][0]['lr'],\n        loss.data))\n\ndef evaluate_model(epoch, history=None):\n    model.eval()\n    loss = 0\n    \n    with torch.no_grad():\n        for img_batch, mask_batch, regr_batch in dev_loader:\n            img_batch = img_batch.to(device)\n            mask_batch = mask_batch.to(device)\n            regr_batch = regr_batch.to(device)\n\n            output = model(img_batch)\n\n            loss += criterion(output, mask_batch, regr_batch, size_average=False).data\n    \n    loss /= len(dev_loader.dataset)\n    \n    if history is not None:\n        history.loc[epoch, 'dev_loss'] = loss.cpu().numpy()\n    \n    print('Dev loss: {:.4f}'.format(loss))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(\"../input/weights\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"m=torch.load(\"../input/weights2/weights.pth\",map_location=torch.device('cpu'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_state_dict(m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# %%time\n# import gc\n\n# history = pd.DataFrame()\n\n# for epoch in range(n_epochs):\n#     torch.cuda.empty_cache()\n#     gc.collect()\n#     train_model(epoch, history)\n#     evaluate_model(epoch, history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#torch.save(model.state_dict(), './model.pth')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# output = model(torch.tensor(img[None]).to(device))\n# print(output[0,0].shape)\n# print('output shape:',output.shape)\n# logits = output[0,0].data.cpu().numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img, mask, regr = dev_dataset[100]\n\nplt.figure(figsize=(16,16))\nplt.title('Input image')\nplt.imshow(np.rollaxis(img, 0, 3))\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('Ground truth mask')\nplt.imshow(mask)\nplt.show()\n\noutput = model(torch.tensor(img[None]).to(device))\nprint('output shape:',output.shape)\nlogits = output[0,0].data.cpu().numpy()\n\nplt.figure(figsize=(16,16))\nplt.title('Model predictions')\nplt.imshow(logits)\nplt.show()\n\nplt.figure(figsize=(16,16))\nplt.title('Model predictions thresholded')\nplt.imshow(logits > 0)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# torch.cuda.empty_cache()\n# gc.collect()\n\n# for idx in range(2):\n#     img, mask, regr = dev_dataset[idx]\n    \n#     output = model(torch.tensor(img[None]).to(device)).data.cpu().numpy()\n#     coords_pred = extract_coords(output[0])\n#     #print('coords_pred:',coords_pred)\n#     coords_true = extract_coords(np.concatenate([mask[None], regr], 0))\n#     #print('coords_true:',coords_true)\n    \n#     img = imread(train_images_dir.format(df_dev['ImageId'].iloc[idx]))\n    \n#     fig, axes = plt.subplots(1, 2, figsize=(30,30))\n#     axes[0].set_title('Ground truth')\n#     axes[0].imshow(visualize(img, coords_true))\n#     axes[1].set_title('Prediction')\n#     axes[1].imshow(visualize(img, coords_pred))\n#     plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(df_train.values[0][1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_dev[df_dev['ImageId']=='ID_037f71efa']['PredictionString'].values[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_name='../input/pku-autonomous-driving/train_images/ID_c0e303932.jpg'\n#directory = '../input/pku-autonomous-driving'\nimg2 = cv2.imread(img_name) \n#os.chdir(directory) \ncv2.imwrite('ID_c0e303932.jpg', img2) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_name='../input/pku-autonomous-driving/train_images/ID_0513c9ece.jpg'\nimg0 = imread(img_name, True)\nimg = preprocess_image(img0)\nimg = np.rollaxis(img, 2, 0)\n#mask, regr = 0, 0\nlabels=df_train[df_train['ImageId']=='ID_0513c9ece']['PredictionString'].values[0]\nmask, regr = get_mask_and_regr(img0, labels)\nregr = np.rollaxis(regr, 2, 0)\ncoords_true = extract_coords(np.concatenate([mask[None], regr], 0))\nprint('coords true:',coords_true)\nmask, regr = 0, 0\noutput = model(torch.tensor(img[None]).to(device)).data.cpu().numpy()\ncoords_pred = extract_coords(output[0])\nprint('coords pred:',coords_pred)\n#coords_pred=coords_true\nimg = imread(img_name)\n# fig, axes = plt.subplots(1, 1, figsize=(30,30))\n# axes.set_title('Prediction')\n# axes.imshow(visualize(img, coords_true))\n#plt.show()\nfig, axes = plt.subplots(1, 2, figsize=(30,30))\naxes[0].set_title('Ground truth')\naxes[0].imshow(visualize(img, coords_true))\naxes[1].set_title('Prediction')\naxes[1].imshow(visualize(img, coords_pred))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train_images_dir.format(df_train['ImageId'].iloc[idx]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"torch.cuda.empty_cache()\ngc.collect()\n\nfor idx in range(2350,2370):\n    img, mask, regr = train_dataset[idx]\n    output = model(torch.tensor(img[None]).to(device)).data.cpu().numpy()\n    coords_pred = extract_coords(output[0])\n    #print('coords_pred:',coords_pred)\n    coords_true = extract_coords(np.concatenate([mask[None], regr], 0))\n    #print('coords_true:',coords_true)\n    \n    img = imread(train_images_dir.format(df_train['ImageId'].iloc[idx]))\n    \n    fig, axes = plt.subplots(1, 2, figsize=(30,30))\n    axes[0].set_title('Ground truth')\n    axes[0].imshow(visualize(img, coords_true))\n    axes[1].set_title('Prediction')\n    axes[1].imshow(visualize(img, coords_pred))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"torch.cuda.empty_cache()\ngc.collect()\n\nfor idx in range(1,2):\n    img, mask, regr = test_dataset[idx]\n    \n    output = model(torch.tensor(img[None]).to(device)).data.cpu().numpy()\n    coords_pred = extract_coords(output[0])\n    #print('coords_pred:',coords_pred)\n    #coords_true = extract_coords(np.concatenate([mask[None], regr], 0))\n    #print('coords_true:',coords_true)\n    \n    img = imread(test_images_dir.format(df_test['ImageId'].iloc[idx]))\n    \n    #fig, axes = plt.subplots(1, 2, figsize=(30,30))\n    fig, axes = plt.subplots(1, 1, figsize=(30,30))\n    #axes[0].set_title('Ground truth')\n    #axes[0].imshow(visualize(img, coords_true))\n    axes.set_title('Prediction')\n    axes.imshow(visualize(img, coords_pred))\n    plt.show()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}