{"cells":[{"metadata":{"id":"NVtFOHsdy8x_","colab_type":"text"},"cell_type":"markdown","source":"Hi everyone! This is my first notebook on Kaggle, I hope you'll find usefull.\n\nSources:"},{"metadata":{"id":"HeJW3lG44_OH","colab_type":"code","cellView":"both","outputId":"a354eed6-640a-4270-aece-29f6e3afae2f","colab":{"base_uri":"https://localhost:8080/","height":80},"trusted":true},"cell_type":"code","source":"#@title Import des librairies\nimport os\nimport glob\nimport shutil\nfrom zipfile import ZipFile \nimport glob\nimport json\nimport json\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport cv2\nfrom math import sin, cos\nimport seaborn as sns\nfrom PIL import Image, ImageOps\nimport os\nimport imp\nimport tensorflow as tf\nimport numpy as np\nimport random\nimport math\nfrom collections import OrderedDict\n\nfrom keras import models\nfrom keras.layers import Input\nfrom keras.layers import Convolution2D\nfrom keras.layers import BatchNormalization\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Flatten\nfrom keras import backend as K\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping","execution_count":null,"outputs":[]},{"metadata":{"id":"UyT0tIRoAWUq","colab_type":"code","cellView":"form","colab":{},"trusted":true},"cell_type":"code","source":"#@title Création des fonctions\n#@markdown On crée des fonctions utiles\n\n# Création d'un dossier\ndef create_repertory(rep):\n    \"\"\"\n    fonction de création de dossier\n    \"\"\"\n    try:\n        os.mkdir(rep)\n    except:\n        print('Le dossier est existant')\n\n# Affichage d'une image et son histogramme\ndef print_image(img):\n    \"\"\"\n    fonction d'affichage de l'image\n    \"\"\"\n    # On affiche l'image\n    plt.figure(figsize=(20, 5))\n    plt.subplot(1, 2, 1)\n    plt.imshow(img)\n    # On affiche l'histogramme\n    plt.subplot(1, 2, 2)\n    plt.hist(img.flatten(), bins=range(256))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"gij4ldvV0crs","colab_type":"code","cellView":"both","colab":{},"trusted":true},"cell_type":"code","source":"#@title Lister les dossiers\n#@markdown On liste les dossiers\n\n\ntrain_img = []\ntest_img = []\ntrain_masks = []\ntest_masks = []\ncar_models_json = []\n\n# On liste le train\nfiles = glob.glob('/kaggle/input/pku-autonomous-driving/train_images/' + \"/*.jpg\")\nfor file in files:\n    train_img.append(file)\n\nfiles = glob.glob('/kaggle/input/pku-autonomous-driving/train_masks/' + \"/*.jpg\")\nfor file in files:\n    train_masks.append(file)\n# On liste le test\nfiles = glob.glob('/kaggle/input/pku-autonomous-driving/test_images/' + \"/*.jpg\")\nfor file in files:\n    test_img.append(file)\n\nfiles = glob.glob('/kaggle/input/pku-autonomous-driving/test_masks/' + \"/*.jpg\")\nfor file in files:\n    test_masks.append(file)\n\n# On liste les json\nfiles = glob.glob('/kaggle/input/pku-autonomous-driving/car_models_json/' + \"/*.json\")\nfor file in files:\n    car_models_json.append(file)","execution_count":null,"outputs":[]},{"metadata":{"id":"djbBrCCdOLX2","colab_type":"code","cellView":"form","outputId":"dd85bd74-e430-4e6c-8304-98385e132bc3","colab":{"base_uri":"https://localhost:8080/","height":204},"trusted":true},"cell_type":"code","source":"#@title Creation des DataFrames\n#@markdown On créé des dataframes\n\n# Train_img\ntrain_img_df = pd.DataFrame(train_img, columns= ['train_img'])\ntrain_img_df['ImageId'] = train_img_df['train_img'].str.split('/kaggle/input/pku-autonomous-driving/train_images/', expand=True)[1].str.split('.jpg', expand=True)[0]\n\n# Train_masks\ntrain_masks_df = pd.DataFrame(train_masks, columns= ['train_masks'])\ntrain_masks_df['ImageId'] = train_masks_df['train_masks'].str.split('/kaggle/input/pku-autonomous-driving/train_masks/', expand=True)[1].str.split('.jpg', expand=True)[0]\n\n# Test_img\ntest_img_df = pd.DataFrame(test_img, columns= ['test_img'])\ntest_img_df['ImageId'] = test_img_df['test_img'].str.split('/kaggle/input/pku-autonomous-driving/test_images/', expand=True)[1].str.split('.jpg', expand=True)[0]\n\n# Test_masks\ntest_masks_df = pd.DataFrame(test_masks, columns= ['test_masks'])\ntest_masks_df['ImageId'] = test_masks_df['test_masks'].str.split('/kaggle/input/pku-autonomous-driving/test_masks/', expand=True)[1].str.split('.jpg', expand=True)[0]\n\n# json\ncar_models_json_df = pd.DataFrame(car_models_json, columns= ['car_models_json'])\ncar_models_json_df['ImageId'] = car_models_json_df['car_models_json'].str.split('/kaggle/input/pku-autonomous-driving/car_models_json/', expand=True)[1].str.split('.json', expand=True)[0]\n\n# Import train.csv\ntrain_csv = pd.read_csv(\"/kaggle/input/pku-autonomous-driving/train.csv\")\n\n# Concat train\ntrain = pd.merge(train_csv, train_img_df, on='ImageId', how='outer')\ntrain = pd.merge(train, train_masks_df, on='ImageId', how='outer')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"hNf57AsOtNmx","colab_type":"text"},"cell_type":"markdown","source":"[](http://)# Exploration"},{"metadata":{"id":"zs0pj4eOtQXY","colab_type":"code","cellView":"form","outputId":"e407a508-7917-48c3-90cc-63d0665b4b3f","colab":{"base_uri":"https://localhost:8080/","height":357},"trusted":true},"cell_type":"code","source":"#@title Création du DataFrame train\nimages = []\nmodel_type = []\nyaw = []\npitche = []\nroll = []\nx = []\ny = []\nz = []\n\nfor i in list(range(0,train.shape[0])):\n    pred_string = train.PredictionString.iloc[i]\n    items = pred_string.split(' ')\n    model_types, yaws, pitches, rolls, xs, ys, zs = [items[i::7] for i in range(7)]\n    model_type.append(model_types)\n    yaw.append(yaws)\n    pitche.append(pitches)\n    roll.append(rolls)\n    x.append(xs)\n    y.append(ys)\n    z.append(zs)\n    images.append(train.loc[i, 'ImageId'])\nliste1 = pd.DataFrame([images, model_type, yaw, pitche, roll, x, y, z],\n                     index=['ImageId', 'model_type', 'yaw', 'pitche', 'roll', 'x', 'y', 'z']).T\nliste1['nb_car'] = [len(liste1['model_type'][i]) for i in range(liste1.shape[0])]\nliste1.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"PhBytXxE3GAW","colab_type":"code","cellView":"form","outputId":"070d032d-2376-4b04-82c3-80f0a2ae7f01","colab":{"base_uri":"https://localhost:8080/","height":350},"trusted":true},"cell_type":"code","source":"#@title Distribution du nombre de voitures par image\nplt.figure(figsize=(20,5))\nsns.countplot(liste1['nb_car'])\nplt.title('Number of cars')\nplt.xlabel('Number of cars')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"djsqhlZHBr37","colab_type":"code","cellView":"form","outputId":"ce517c23-c8f9-430c-e6a4-ba640820a1b7","colab":{"base_uri":"https://localhost:8080/","height":367},"trusted":true},"cell_type":"code","source":"#@title Distribution des types de voitures\ncar_model = []\nfor i in range(liste1.shape[0]):\n    for j in range(len(liste1['model_type'][i])):\n        car_model.append(liste1['model_type'][i][j])\ntest = pd.DataFrame(car_model)\ntest[1] = 1\ntest = test.groupby(by=0, as_index=True).sum()\ntest['model_type'] = test.index\ntest['nb_car'] = test[1]\ntest = test.sort_values(by=['model_type'])\nprint('There is ', test[1].sum(), 'cars')\n\n# Distribution des type de voitures\nplt.figure(figsize=(20,5))\nsns.barplot(x='model_type', y='nb_car', data=test, palette=\"deep\")\nplt.title('car model type')\nplt.xlabel('model type')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"KXsZ7SpiA2kU","colab_type":"code","cellView":"form","outputId":"bdfbb50a-f4f0-4d21-e0dc-73d4f7d61e88","colab":{"base_uri":"https://localhost:8080/","height":350},"trusted":true},"cell_type":"code","source":"#@title Distribution des yaw\nyaw_list = []\nfor i in range(liste1.shape[0]):\n    for j in range(len(liste1['yaw'][i])):\n        yaw_list.append(liste1['yaw'][i][j])\ntest = pd.DataFrame(yaw_list)\ntest[0] = test[0].astype('float32')\nplt.figure(figsize=(20,5))\nplt.title('yaw distribution')\nsns.distplot(test[0], bins=500);\nplt.xlabel('yaw')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"YnJ2OHdWLJ4i","colab_type":"code","cellView":"form","outputId":"ad3b70ff-8bb1-4d72-9ce8-50ab7952b7be","colab":{"base_uri":"https://localhost:8080/","height":350},"trusted":true},"cell_type":"code","source":"#@title Distribution des pitches\npitche_list = []\nfor i in range(liste1.shape[0]):\n    for j in range(len(liste1['pitche'][i])):\n        pitche_list.append(liste1['pitche'][i][j])\ntest = pd.DataFrame(pitche_list)\ntest[0] = test[0].astype('float32')\nplt.figure(figsize=(20,5))\nplt.title('pitche distribution')\nsns.distplot(test[0], bins=500);\nplt.xlabel('pitche')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"9WBJoIneLJkd","colab_type":"code","cellView":"form","outputId":"3959982c-1795-4a61-e3e8-3a0e51d2a6fb","colab":{"base_uri":"https://localhost:8080/","height":350},"trusted":true},"cell_type":"code","source":"#@title Distribution des roll\nroll_list = []\nfor i in range(liste1.shape[0]):\n    for j in range(len(liste1['roll'][i])):\n        roll_list.append(liste1['roll'][i][j])\ntest = pd.DataFrame(roll_list)\ntest[0] = test[0].astype('float32')\nplt.figure(figsize=(20,5))\nplt.title('roll distribution')\nsns.distplot(test[0], bins=500);\nplt.xlabel('roll')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"RYZsdnVMM4WK","colab_type":"code","cellView":"form","outputId":"e136101f-11a1-4101-ad57-a840b8778a5e","colab":{"base_uri":"https://localhost:8080/","height":350},"trusted":true},"cell_type":"code","source":"#@title Distribution des x\nx_list = []\nfor i in range(liste1.shape[0]):\n    for j in range(len(liste1['x'][i])):\n        x_list.append(liste1['x'][i][j])\ntest = pd.DataFrame(x_list)\ntest[0] = test[0].astype('float32')\nplt.figure(figsize=(20,5))\nplt.title('x distribution')\nsns.distplot(test[0], bins=500);\nplt.xlabel('x')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"jArQqykJM34Y","colab_type":"code","cellView":"form","outputId":"a82d516e-fac1-4818-d7cc-2b448ac26e07","colab":{"base_uri":"https://localhost:8080/","height":350},"trusted":true},"cell_type":"code","source":"#@title Distribution des y\ny_list = []\nfor i in range(liste1.shape[0]):\n    for j in range(len(liste1['y'][i])):\n        y_list.append(liste1['y'][i][j])\ntest = pd.DataFrame(y_list)\ntest[0] = test[0].astype('float32')\nplt.figure(figsize=(20,5))\nplt.title('y distribution')\nsns.distplot(test[0], bins=500);\nplt.xlabel('y')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"ImUFBfJJLJOh","colab_type":"code","cellView":"form","outputId":"95524f72-603c-4c4a-d518-3ac8e16d61c0","colab":{"base_uri":"https://localhost:8080/","height":350},"trusted":true},"cell_type":"code","source":"#@title Distribution des z\nz_list = []\nfor i in range(liste1.shape[0]):\n    for j in range(len(liste1['z'][i])):\n        z_list.append(liste1['z'][i][j])\ntest = pd.DataFrame(z_list)\ntest[0] = test[0].astype('float32')\nplt.figure(figsize=(20,5))\nplt.title('z distribution')\nsns.distplot(test[0], bins=500);\nplt.xlabel('z')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"ljkgsPtsOdJY","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":1000},"outputId":"4b635942-b29d-4528-d2a6-4d706f96f818","trusted":true},"cell_type":"code","source":"def unpack(group):\n    row = group.iloc[0]\n    result = []\n    data = row['PredictionString']\n    while data:\n        data = data.split(maxsplit=7)\n        result.append(OrderedDict((\n            ('image_id', row['ImageId']),\n            ('model_type', int(data[0])),\n            ('yaw', float(data[1])), \n            ('pitch', float(data[2])), \n            ('roll', float(data[3])), \n            ('x', float(data[4])), \n            ('y', float(data[5])),\n            ('z', float(data[6]))\n        )))\n        data = data[7] if len(data) == 8 else ''\n    return pd.DataFrame(result)\n\ntrain_df = train[['ImageId', 'PredictionString']]\nunpacked_train_df = train_df.groupby('ImageId', group_keys=False).apply(unpack).reset_index(drop=True)\n\nsns.set()\nsns.pairplot(unpacked_train_df)","execution_count":null,"outputs":[]},{"metadata":{"id":"U8avr4k1OT-t","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":344},"outputId":"41f4441b-b10a-4deb-fe61-a7b32e70e9a0","trusted":true},"cell_type":"code","source":"# calculate the correlation matrix\ncorr = unpacked_train_df.corr()\n\n# plot the heatmap\nplt.title('Correlation y and z')\nsns.heatmap(corr,\n            xticklabels=corr.columns,\n            yticklabels=corr.columns)","execution_count":null,"outputs":[]},{"metadata":{"id":"qo7PQcekXGpc","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":318},"outputId":"ef3440cc-c371-4131-ef96-29453cd5420e","trusted":true},"cell_type":"code","source":"plt.title('Correlation y and z')\nsns.regplot(unpacked_train_df['y'], unpacked_train_df['z'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"There is a correlation between y and z"},{"metadata":{"id":"WVuyC7GR28mL","colab_type":"code","cellView":"form","outputId":"686865b3-6153-4f95-e3af-0b04a4b13f19","colab":{"base_uri":"https://localhost:8080/","height":575},"trusted":true},"cell_type":"code","source":"#@title On importe un fichier json\nwith open(car_models_json[0]) as json_file:\n    data = json.load(json_file)\n    vertices = np.array(data['vertices'])\n    triangles = np.array(data['faces']) - 1\n    plt.figure(figsize=(20,10))\n    ax = plt.axes(projection='3d')\n    ax.set_title('car_type: '+data['car_type'])\n    ax.set_xlim([-4, 4])\n    ax.set_ylim([-4, 4])\n    ax.set_zlim([0, 3])\n    ax.plot_trisurf(vertices[:,0],\n                    vertices[:,2],\n                    triangles,\n                    -vertices[:,1], shade=True, color='blue')","execution_count":null,"outputs":[]},{"metadata":{"id":"cl3L7tZARYCU","colab_type":"code","cellView":"form","outputId":"a1b55171-202c-4bd6-e2bb-ccc600444dd0","colab":{"base_uri":"https://localhost:8080/","height":1000},"trusted":true},"cell_type":"code","source":"#@title Augmented reality\n# Load an image\nimg_name = train['ImageId'][50]\nimg = cv2.imread(f'/kaggle/input/pku-autonomous-driving/train_images/{img_name}.jpg',cv2.COLOR_BGR2RGB)[:,:,::-1]\nimg2 = cv2.imread(f'/kaggle/input/pku-autonomous-driving/train_masks/{img_name}.jpg',cv2.COLOR_BGR2RGB)[:,:,::-1]\n\n# Prepare data\npred_string = train[train.ImageId == img_name].PredictionString.iloc[0]\nitems = pred_string.split(' ')\nmodel_types, yaws, pitches, rolls, xs, ys, zs = [items[i::7] for i in range(7)]\nliste = pd.DataFrame([model_types, yaws, pitches, rolls, xs, ys, zs],\n                            index=['model_types', 'yaws', 'pitches', 'rolls', 'xs', 'ys', 'zs'])\niterations = []\nfor i in list(liste.columns):\n    iterations.append(liste[i])\n\n# k is camera instrinsic matrix\nk = np.array([[2304.5479, 0,  1686.2379],\n           [0, 2305.8757, 1354.9849],\n           [0, 0, 1]], dtype=np.float32)\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))\n\ndef draw_obj(image, vertices, triangles):\n    for t in triangles:\n        coord = np.array([vertices[t[0]][:2], vertices[t[1]][:2], vertices[t[2]][:2]], dtype=np.int32)\n        # cv2.fillConvexPoly(image, coord, (0,0,255))\n        cv2.polylines(image, np.int32([coord]), 1, (0,0,255))\n\n\n\n\n\noverlay = np.zeros_like(img)\nfor model_type, yaw, pitch, roll, x, y, z in iterations:\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    #We open the model\n    with open(car_models_json[int(model_type)]) as json_file:\n        data = json.load(json_file)\n        vertices = np.array(data['vertices'])\n        vertices[:, 1] = -vertices[:, 1]\n        triangles = np.array(data['faces']) - 1\n\n    P = np.ones((vertices.shape[0],vertices.shape[1]+1))\n    P[:, :-1] = vertices\n    P = P.T\n    img_cor_points = np.dot(k, 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    draw_obj(overlay, img_cor_points, triangles)\n\n# Print image\nalpha = .5\nimg = np.array(img)\ncv2.addWeighted(overlay, alpha, img, 1 - alpha, 0, img)\nplt.figure(figsize=(20,20))\nprint_image(img)\nliste\n\n# Print mask\nalpha = .5\nimg2 = np.array(img2)\ncv2.addWeighted(overlay, alpha, img2, 1 - alpha, 0, img2)\nplt.figure(figsize=(20,20))\nprint_image(img2)\niterations","execution_count":null,"outputs":[]},{"metadata":{"id":"JXdd5rtZVhT3","colab_type":"code","cellView":"form","outputId":"ddf3022b-816e-406d-c03c-0de22ca3fdb3","colab":{"base_uri":"https://localhost:8080/","height":458},"trusted":true},"cell_type":"code","source":"#@title Center plot\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\n\ndef 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\n\ndef str2coords(s):\n    pred_string = s\n    items = pred_string.split(' ')\n    model_types, yaws, pitches, rolls, xs, ys, zs = [items[i::7] for i in range(7)]\n    liste = pd.DataFrame([ model_types, yaws, pitches, rolls, xs, ys, zs],\n                        index=['model_type', 'yaw', 'pitche', 'roll', 'x', 'y', 'z']).T\n    coords = []\n    for i in range(liste.shape[0]):\n        coords.append({'id': float(liste['model_type'][i]),\n                    'yaw': float(liste['yaw'][i]),\n                    'pitch': float(liste['pitche'][i]),\n                    'roll': float(liste['roll'][i]),\n                    'x': float(liste['x'][i]),\n                    'y': float(liste['y'][i]),\n                    'z': float(liste['z'][i]),\n                    })\n    return coords\n\ndef 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 = [float(c['x']) for c in coords]\n    ys = [float(c['y']) for c in coords]\n    zs = [float(c['z']) for c in coords]\n    position = []\n    for i in range(len(xs)):\n        position.append([xs[i], ys[i], zs[i]])\n    P = np.array(position).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\nfrom 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))\n\ndef draw_line(image, points):\n    color = (255, 0, 0)\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\ndef draw_points(image, 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\n\n\ndef 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    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        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([[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        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        img_cor_points = img_cor_points.astype(int)\n        # Drawing\n        img = draw_line(img, img_cor_points)\n        img = draw_points(img, img_cor_points[-1:])\n    \n    return img\n\nidx = 128\n\n\nfig, axes = plt.subplots(1, 2, figsize=(20,20))\nimg = imread('/kaggle/input/pku-autonomous-driving/train_images/' + train['ImageId'].iloc[idx] + '.jpg')\naxes[0].imshow(img)\nimg_vis = visualize(img, str2coords(train['PredictionString'].iloc[idx]))\naxes[1].imshow(img_vis)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Modelisation"},{"metadata":{"trusted":true},"cell_type":"code","source":"# separation train test val\n\nnb_pic = int(len(list(train['train_img'].index))*0.2)\nliste_index_train = random.sample(list(train['train_img'].index), nb_pic)\na = int(len(liste_index_train)*0.2)\nliste_index_test = random.sample(liste_index_train, a)\nfor j in liste_index_test:\n    del liste_index_train[liste_index_train.index(j)]\nliste_index_val = random.sample(liste_index_train, a)\nfor j in liste_index_val:\n    del liste_index_train[liste_index_val.index(j)]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We try to determine the right number of car"},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = train['train_img'][liste_index_train]\nx_test = train['train_img'][liste_index_test]\nx_val = train['train_img'][liste_index_val]\ny_train = liste1['nb_car'][liste_index_train]/44\ny_test = liste1['nb_car'][liste_index_test]/44\ny_val = liste1['nb_car'][liste_index_val]/44\n\n# Pour l'entrainnement\nnew_train = pd.DataFrame({\"x\":x_train})\nnew_train = new_train.join(y_train)\n\nnew_test = pd.DataFrame({\"x\":x_test})\nnew_test = new_test.join(y_test)\n\n# Pour la validation\nnew_val = pd.DataFrame({\"x\":x_val})\nnew_val = new_val.join(y_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def score(y_true, y_pred):\n    if not K.is_tensor(y_pred):\n        y_pred = K.constant(y_pred)\n    y_true = K.cast(y_true, y_pred.dtype)\n    return K.sum(K.abs(y_true/y_true)) / K.sum(K.abs(y_pred/y_true))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def sum_absolute_error(y_true, y_pred):\n    if not K.is_tensor(y_pred):\n        y_pred = K.constant(y_pred)\n    y_true = K.cast(y_true, y_pred.dtype)\n    return K.sum(K.abs(y_pred - y_true), axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers.merge import add, concatenate\n# parametres\nimg_size = 512\nnb_conv = 7\nnb_dense = 4\nunits = 512\ndropout = 0.5\n# construction du modèle\ninput_shape = (1692, 1355, 3)\n\nx = Input(shape=input_shape)\nl = x\nl = Convolution2D(filters=2, kernel_size=[1, 1], strides=1, activation=\"relu\")(l)\nl = BatchNormalization()(l)\nl = MaxPooling2D()(l)\n\nfor i in list(range(nb_conv)): \n    l = Convolution2D(filters=2*(2**(i//2+1)), kernel_size=[3, 3], strides=1, activation=\"relu\")(l)\n    l = BatchNormalization()(l)\n    l = MaxPooling2D(pool_size=(2, 2), strides=2)(l)\n# Couche Flattening\nl = Flatten()(l)\n\n\nl = Dense(units=1024, activation=\"relu\")(l)\nl = Dropout(dropout)(l)\n\n\n\nl = Dense(units=1, activation=\"linear\")(l)\n\nfirst_model = models.Model(x, l)\nfirst_model.compile(loss=sum_absolute_error, optimizer='adam', metrics=[score])\n\n\nfirst_model.summary()\n\n\n\ntrain_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntraining_set = train_datagen.flow_from_dataframe(new_train,\n                                                 directory=None,\n                                                 x_col='x',\n                                                 y_col='nb_car',\n                                                 weight_col=None,\n                                                 target_size=(1692, 1355),\n                                                 color_mode='rgb',\n                                                 classes=None,\n                                                 class_mode='raw',\n                                                 batch_size=32,\n                                                 shuffle=True,\n                                                 seed=None,\n                                                 save_to_dir=None,\n                                                 save_prefix='',\n                                                 save_format='jpg',\n                                                 subset=None,\n                                                 interpolation='nearest', \n                                                 validate_filenames=True)\n\ntest_set = test_datagen.flow_from_dataframe(new_test,\n                                            directory=None,\n                                            x_col='x',\n                                            y_col='nb_car',\n                                            weight_col=None,\n                                            target_size=(1692, 1355),\n                                            color_mode='rgb',\n                                            classes=None,\n                                            class_mode='raw',\n                                            batch_size=32,\n                                            shuffle=True,\n                                            seed=None,\n                                            save_to_dir=None,\n                                            save_prefix='',\n                                            save_format='jpg',\n                                            subset=None,\n                                            interpolation='nearest', \n                                            validate_filenames=True)\n\nval_set = test_datagen.flow_from_dataframe(new_val,\n                                            directory=None,\n                                            x_col='x',\n                                            y_col='nb_car',\n                                            weight_col=None,\n                                            target_size=(1692, 1355),\n                                            color_mode='rgb',\n                                            classes=None,\n                                            class_mode='raw',\n                                            batch_size=32,\n                                            shuffle=True,\n                                            seed=None,\n                                            save_to_dir=None,\n                                            save_prefix='',\n                                            save_format='jpg',\n                                            subset=None,\n                                            interpolation='nearest', \n                                            validate_filenames=True)\n\ncheckpoint = ModelCheckpoint(\"nb_car.h5\", monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=True, mode='auto', period=1)\n\n\n\nHistory = first_model.fit_generator(training_set,\n                                    steps_per_epoch=int(math.ceil(len(training_set.filenames)/32)), # len du jeux d'entrainement / batch\n                                    epochs=20,\n                                    validation_data=test_set,\n                                    validation_steps=int(math.ceil(len(test_set.filenames)/32)), # len du jeux de test / batch\n                                    callbacks = [checkpoint],\n                                    workers=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(30,10))\ny_hat = []\nfor i in list(range(len(val_set.filepaths))):\n    test_image = Image.open(val_set.filepaths[i])\n    test_image_full = np.array(test_image)\n    test_image = test_image.resize((1692, 1355), resample=0)\n    test_image = np.array(test_image)/255\n    test_image = np.expand_dims(test_image, axis=0)\n    result = first_model.predict(test_image)\n    y_hat.append(int(result[0]*44))\n\n    \nplt.plot(y_hat)\nplt.plot(list(liste1['nb_car'][liste_index_val]))\necart = np.mean(np.abs(np.array(y_hat) - liste1['nb_car'][liste_index_val]))\nprint('mean absolute error: ', ecart)","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"Projet08.ipynb","provenance":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}