{"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":"import os\nimport pathlib\nimport ast\n\nif \"models\" in pathlib.Path.cwd().parts:\n  while \"models\" in pathlib.Path.cwd().parts:\n    os.chdir('..')\nelif not pathlib.Path('models').exists():\n  !git clone --depth 1 https://github.com/tensorflow/models\n    \n!conda install -y cudnn \n\nimport tensorflow as tf\ntf.test.gpu_device_name()\ntf.test.is_built_with_cuda()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-01-12T20:14:51.789222Z","iopub.execute_input":"2022-01-12T20:14:51.789582Z","iopub.status.idle":"2022-01-12T20:17:49.291461Z","shell.execute_reply.started":"2022-01-12T20:14:51.78949Z","shell.execute_reply":"2022-01-12T20:17:49.29038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ln -s /usr/local/cuda-11.0/targets/x86_64-linux/lib/libcusolver.so.10 /usr/local/cuda-11.0/targets/x86_64-linux/lib/libcusolver.so.11","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:17:49.294013Z","iopub.execute_input":"2022-01-12T20:17:49.294605Z","iopub.status.idle":"2022-01-12T20:17:50.038537Z","shell.execute_reply.started":"2022-01-12T20:17:49.294547Z","shell.execute_reply":"2022-01-12T20:17:50.037183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nimport torch\nprint('CUDA:',torch.version.cuda)\n\ncudnn = torch.backends.cudnn.version()\ncudnn_major = cudnn // 1000\ncudnn = cudnn % 1000\ncudnn_minor = cudnn // 100\ncudnn_patch = cudnn % 100\nprint( 'cuDNN:', '.'.join([str(cudnn_major),str(cudnn_minor),str(cudnn_patch)]) )\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:17:50.042149Z","iopub.execute_input":"2022-01-12T20:17:50.042563Z","iopub.status.idle":"2022-01-12T20:17:51.574475Z","shell.execute_reply.started":"2022-01-12T20:17:50.042466Z","shell.execute_reply":"2022-01-12T20:17:51.573466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\ncd models/research\nprotoc object_detection/protos/*.proto --python_out=.\ncp object_detection/packages/tf2/setup.py .\npython -m pip install .\nexport PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:17:51.577777Z","iopub.execute_input":"2022-01-12T20:17:51.578454Z","iopub.status.idle":"2022-01-12T20:20:04.045164Z","shell.execute_reply.started":"2022-01-12T20:17:51.578422Z","shell.execute_reply":"2022-01-12T20:20:04.043906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:04.04858Z","iopub.execute_input":"2022-01-12T20:20:04.048883Z","iopub.status.idle":"2022-01-12T20:20:04.05919Z","shell.execute_reply.started":"2022-01-12T20:20:04.048838Z","shell.execute_reply":"2022-01-12T20:20:04.058236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data imports\nDATA_PATH = '/kaggle/input/tensorflow-great-barrier-reef'\nimages_path = os.path.join(DATA_PATH,'train_images')\ndf_test = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/test.csv\")\ndf_train = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\nsample_submission = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\nexample = np.load(\"/kaggle/input/tensorflow-great-barrier-reef/example_test.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:04.060644Z","iopub.execute_input":"2022-01-12T20:20:04.062987Z","iopub.status.idle":"2022-01-12T20:20:04.49801Z","shell.execute_reply.started":"2022-01-12T20:20:04.062938Z","shell.execute_reply":"2022-01-12T20:20:04.496987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['img_path'] = os.path.join('../input/tensorflow-great-barrier-reef/train_images')+\"/video_\"+df_train.video_id.astype(str)+\"/\"+df_train.video_frame.astype(str)+\".jpg\"\ndf_train['annotations'] = df_train['annotations'].apply(lambda x: ast.literal_eval(x))\ndf_train['Number_bbox'] = df_train['annotations'].apply(lambda x:len(x)) ","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:04.499819Z","iopub.execute_input":"2022-01-12T20:20:04.500158Z","iopub.status.idle":"2022-01-12T20:20:04.984204Z","shell.execute_reply.started":"2022-01-12T20:20:04.500117Z","shell.execute_reply":"2022-01-12T20:20:04.983312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bbox_areas(annotations):\n    if not annotations:\n        return [0]\n    area_list = []\n    for annotation in annotations:\n        area_list.append(annotation['width']*annotation['height'])\n    return area_list\ndf_train[\"bbox_area\"] = df_train[\"annotations\"].apply(bbox_areas)\ndf_train[\"max_area\"] = df_train[\"bbox_area\"].apply(lambda x : max(x))\ndf_train[\"min_area\"] = df_train[\"bbox_area\"].apply(lambda x : min(x))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:04.985627Z","iopub.execute_input":"2022-01-12T20:20:04.986393Z","iopub.status.idle":"2022-01-12T20:20:05.045717Z","shell.execute_reply.started":"2022-01-12T20:20:04.986344Z","shell.execute_reply":"2022-01-12T20:20:05.044842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_viz(df_train, id):\n    image = df_train['img_path'][id]\n    img = Image.open(image)\n    \n    for box in df_train['annotations'][id]:\n        shape = [box['x'], box['y'], box['x']+box['width'], box['y']+box['height']]\n        ImageDraw.Draw(img).rectangle(shape, outline =\"red\", width=3)\n    display(img)\ndf_train.sort_values(\"max_area\", ascending=False).head()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.047211Z","iopub.execute_input":"2022-01-12T20:20:05.047672Z","iopub.status.idle":"2022-01-12T20:20:05.085614Z","shell.execute_reply.started":"2022-01-12T20:20:05.047627Z","shell.execute_reply":"2022-01-12T20:20:05.084656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = df_train.query(\"Number_bbox>0\")\nlen(train_data)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.087023Z","iopub.execute_input":"2022-01-12T20:20:05.087631Z","iopub.status.idle":"2022-01-12T20:20:05.108563Z","shell.execute_reply.started":"2022-01-12T20:20:05.087542Z","shell.execute_reply":"2022-01-12T20:20:05.107419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data.explode(\"annotations\").reset_index()\ntrain_data = train_data.drop(['index'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.110266Z","iopub.execute_input":"2022-01-12T20:20:05.11069Z","iopub.status.idle":"2022-01-12T20:20:05.140239Z","shell.execute_reply.started":"2022-01-12T20:20:05.110646Z","shell.execute_reply":"2022-01-12T20:20:05.139306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_bbox_dim(annotation):\n#     print(annotation)\n    return [annotation['x'], annotation['y'],annotation['x'] + annotation['width'],annotation['y'] + annotation['height'],  annotation['width'],annotation['height'] ]","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.141957Z","iopub.execute_input":"2022-01-12T20:20:05.14229Z","iopub.status.idle":"2022-01-12T20:20:05.149555Z","shell.execute_reply.started":"2022-01-12T20:20:05.142246Z","shell.execute_reply":"2022-01-12T20:20:05.148336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations_data = train_data.apply(lambda row: get_bbox_dim(row[\"annotations\"]), axis=1)\nannotations_df = pd.DataFrame(list(annotations_data), columns=['xmin', 'ymin', 'xmax', 'ymax', 'width', 'height'])","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.151184Z","iopub.execute_input":"2022-01-12T20:20:05.152454Z","iopub.status.idle":"2022-01-12T20:20:05.398536Z","shell.execute_reply.started":"2022-01-12T20:20:05.152394Z","shell.execute_reply":"2022-01-12T20:20:05.39761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.404453Z","iopub.execute_input":"2022-01-12T20:20:05.405225Z","iopub.status.idle":"2022-01-12T20:20:05.410542Z","shell.execute_reply.started":"2022-01-12T20:20:05.405192Z","shell.execute_reply":"2022-01-12T20:20:05.40949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.concat([annotations_df, train_data], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.412198Z","iopub.execute_input":"2022-01-12T20:20:05.412997Z","iopub.status.idle":"2022-01-12T20:20:05.424383Z","shell.execute_reply.started":"2022-01-12T20:20:05.412951Z","shell.execute_reply":"2022-01-12T20:20:05.423251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xmax = np.array(train_data[\"xmax\"].values.tolist())\nymax = np.array(train_data[\"ymax\"].values.tolist())\ntrain_data[\"xmax\"] = np.where(xmax > 1280, 1280, xmax).tolist()\ntrain_data[\"ymax\"] = np.where(ymax > 720, 720, ymax).tolist()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.425967Z","iopub.execute_input":"2022-01-12T20:20:05.426673Z","iopub.status.idle":"2022-01-12T20:20:05.453995Z","shell.execute_reply.started":"2022-01-12T20:20:05.426625Z","shell.execute_reply":"2022-01-12T20:20:05.453188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['img_path'].iloc[1].split('/')[-1]","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.455462Z","iopub.execute_input":"2022-01-12T20:20:05.455775Z","iopub.status.idle":"2022-01-12T20:20:05.463773Z","shell.execute_reply.started":"2022-01-12T20:20:05.455737Z","shell.execute_reply":"2022-01-12T20:20:05.462542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['class'] = 'starfish'\ntrain_data['filename'] = train_data['img_path'].iloc[1].split('/')[-1]","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.46573Z","iopub.execute_input":"2022-01-12T20:20:05.466445Z","iopub.status.idle":"2022-01-12T20:20:05.4748Z","shell.execute_reply.started":"2022-01-12T20:20:05.466397Z","shell.execute_reply":"2022-01-12T20:20:05.473705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_video1 = train_data[train_data['video_id']==1].reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.476733Z","iopub.execute_input":"2022-01-12T20:20:05.477502Z","iopub.status.idle":"2022-01-12T20:20:05.490764Z","shell.execute_reply.started":"2022-01-12T20:20:05.477441Z","shell.execute_reply":"2022-01-12T20:20:05.489741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"COLUMNS_TO_KEEP = ['width', 'height', 'xmin', 'ymin', 'xmax', 'ymax', 'filename','class']\ntrain_csv = train_data_video1[COLUMNS_TO_KEEP]","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.4927Z","iopub.execute_input":"2022-01-12T20:20:05.493347Z","iopub.status.idle":"2022-01-12T20:20:05.502201Z","shell.execute_reply.started":"2022-01-12T20:20:05.493304Z","shell.execute_reply":"2022-01-12T20:20:05.501242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfrom sklearn.model_selection import train_test_split\nx_train, x_val = train_test_split(train_csv, test_size = 0.2)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:05.503928Z","iopub.execute_input":"2022-01-12T20:20:05.505221Z","iopub.status.idle":"2022-01-12T20:20:06.379484Z","shell.execute_reply.started":"2022-01-12T20:20:05.505134Z","shell.execute_reply":"2022-01-12T20:20:06.378387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.to_csv('training.csv',index=False)\nx_val.to_csv('eval.csv',index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:06.381334Z","iopub.execute_input":"2022-01-12T20:20:06.381749Z","iopub.status.idle":"2022-01-12T20:20:06.417051Z","shell.execute_reply.started":"2022-01-12T20:20:06.381675Z","shell.execute_reply":"2022-01-12T20:20:06.416122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.tar.gz","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:06.418699Z","iopub.execute_input":"2022-01-12T20:20:06.419136Z","iopub.status.idle":"2022-01-12T20:20:10.009043Z","shell.execute_reply.started":"2022-01-12T20:20:06.419076Z","shell.execute_reply":"2022-01-12T20:20:10.008064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar --gunzip --extract --verbose --file=faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.tar.gz","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:10.011034Z","iopub.execute_input":"2022-01-12T20:20:10.011786Z","iopub.status.idle":"2022-01-12T20:20:13.747843Z","shell.execute_reply.started":"2022-01-12T20:20:10.011734Z","shell.execute_reply":"2022-01-12T20:20:13.746561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"open(\"generate_tfrecord.py\",\"w+\")","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:13.750521Z","iopub.execute_input":"2022-01-12T20:20:13.750874Z","iopub.status.idle":"2022-01-12T20:20:13.760341Z","shell.execute_reply.started":"2022-01-12T20:20:13.750806Z","shell.execute_reply":"2022-01-12T20:20:13.759271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pwd\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:13.762457Z","iopub.execute_input":"2022-01-12T20:20:13.763218Z","iopub.status.idle":"2022-01-12T20:20:13.771508Z","shell.execute_reply.started":"2022-01-12T20:20:13.763171Z","shell.execute_reply":"2022-01-12T20:20:13.770278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile '/kaggle/working/generate_tfrecord.py'\nfrom __future__ import division\nfrom __future__ import print_function\nfrom __future__ import absolute_import\n\nimport os\nimport io\nimport pandas as pd\nimport tensorflow as tf\n\nfrom PIL import Image\nfrom object_detection.utils import dataset_util\nfrom collections import namedtuple, OrderedDict\n\nflags = tf.compat.v1.app.flags\nflags.DEFINE_string('csv_input', '', 'Path to the CSV input')\nflags.DEFINE_string('output_path', '', 'Path to output TFRecord')\nflags.DEFINE_string('image_dir', '', 'Path to images')\nFLAGS = flags.FLAGS\n\n\n# TO-DO replace this with label map\ndef class_text_to_int(row_label):\n    if row_label == 'wheat':\n        return 1\n    else:\n        return 0\n\n\ndef split(df, group):\n    data = namedtuple('data', ['filename', 'object'])\n    gb = df.groupby(group)\n    return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]\n\n\ndef create_tf_example(group, path):\n    with tf.compat.v1.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:\n        encoded_jpg = fid.read()\n    encoded_jpg_io = io.BytesIO(encoded_jpg)\n    image = Image.open(encoded_jpg_io)\n    width, height = image.size\n\n    filename = group.filename.encode('utf8')\n    image_format = b'jpg'\n    xmins = []\n    xmaxs = []\n    ymins = []\n    ymaxs = []\n    classes_text = []\n    classes = []\n\n    for index, row in group.object.iterrows():\n        xmins.append(row['xmin'] / width)\n        xmaxs.append(row['xmax'] / width)\n        ymins.append(row['ymin'] / height)\n        ymaxs.append(row['ymax'] / height)\n        classes_text.append(row['class'].encode('utf8'))\n        classes.append(class_text_to_int(row['class']))\n\n    tf_example = tf.train.Example(features=tf.train.Features(feature={\n        'image/height': dataset_util.int64_feature(height),\n        'image/width': dataset_util.int64_feature(width),\n        'image/filename': dataset_util.bytes_feature(filename),\n        'image/source_id': dataset_util.bytes_feature(filename),\n        'image/encoded': dataset_util.bytes_feature(encoded_jpg),\n        'image/format': dataset_util.bytes_feature(image_format),\n        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),\n        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),\n        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),\n        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),\n        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),\n        'image/object/class/label': dataset_util.int64_list_feature(classes),\n    }))\n    return tf_example\n\n\ndef main(_):\n    writer = tf.io.TFRecordWriter(FLAGS.output_path)\n    path = os.path.join(FLAGS.image_dir)\n    examples = pd.read_csv(FLAGS.csv_input)\n    grouped = split(examples, 'filename')\n    for group in grouped:\n        tf_example = create_tf_example(group, path)\n        writer.write(tf_example.SerializeToString())\n\n    writer.close()\n    output_path = os.path.join(os.getcwd(), FLAGS.output_path)\n    print('Successfully created the TFRecords: {}'.format(output_path))\n\n\nif __name__ == '__main__':\n    tf.compat.v1.app.run()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:13.773668Z","iopub.execute_input":"2022-01-12T20:20:13.774367Z","iopub.status.idle":"2022-01-12T20:20:13.786636Z","shell.execute_reply.started":"2022-01-12T20:20:13.774319Z","shell.execute_reply":"2022-01-12T20:20:13.785593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"open(\"label_map.txt\",\"w+\")","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:13.788423Z","iopub.execute_input":"2022-01-12T20:20:13.789978Z","iopub.status.idle":"2022-01-12T20:20:13.802152Z","shell.execute_reply.started":"2022-01-12T20:20:13.789915Z","shell.execute_reply":"2022-01-12T20:20:13.800509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile '/kaggle/working/label_map.txt'\nitem {\n  id: 1\n  name: 'starfish'\n}","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:13.804074Z","iopub.execute_input":"2022-01-12T20:20:13.804964Z","iopub.status.idle":"2022-01-12T20:20:13.813649Z","shell.execute_reply.started":"2022-01-12T20:20:13.804917Z","shell.execute_reply":"2022-01-12T20:20:13.812467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip3 install tf-nightly","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:20:13.816973Z","iopub.execute_input":"2022-01-12T20:20:13.8178Z","iopub.status.idle":"2022-01-12T20:21:20.678152Z","shell.execute_reply.started":"2022-01-12T20:20:13.817734Z","shell.execute_reply":"2022-01-12T20:21:20.676883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python /kaggle/working/generate_tfrecord.py --csv_input=/kaggle/working/training.csv  --output_path=train.record --image_dir=../input/tensorflow-great-barrier-reef/train_images/video_0","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:20.684355Z","iopub.execute_input":"2022-01-12T20:21:20.685087Z","iopub.status.idle":"2022-01-12T20:21:31.589174Z","shell.execute_reply.started":"2022-01-12T20:21:20.685044Z","shell.execute_reply":"2022-01-12T20:21:31.588087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 /kaggle/working/generate_tfrecord.py --csv_input=eval.csv  --output_path=eval.record --image_dir=../input/tensorflow-great-barrier-reef/train_images/video_0\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:31.592558Z","iopub.execute_input":"2022-01-12T20:21:31.592924Z","iopub.status.idle":"2022-01-12T20:21:37.093452Z","shell.execute_reply.started":"2022-01-12T20:21:31.592875Z","shell.execute_reply":"2022-01-12T20:21:37.092324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nopen(\"pipeline.config\",\"w+\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:37.097645Z","iopub.execute_input":"2022-01-12T20:21:37.097949Z","iopub.status.idle":"2022-01-12T20:21:37.106442Z","shell.execute_reply.started":"2022-01-12T20:21:37.097916Z","shell.execute_reply":"2022-01-12T20:21:37.105337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile '/kaggle/working/models/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.config'\nmodel {\n  faster_rcnn {\n    num_classes: 1\n    image_resizer {\n      keep_aspect_ratio_resizer {\n        min_dimension: 640\n        max_dimension: 640\n        pad_to_max_dimension: true\n      }\n    }\n    feature_extractor {\n      type: 'faster_rcnn_resnet50_keras'\n      batch_norm_trainable: true\n    }\n    first_stage_anchor_generator {\n      grid_anchor_generator {\n        scales: [0.25, 0.5, 1.0, 2.0]\n        aspect_ratios: [0.5, 1.0, 2.0]\n        height_stride: 16\n        width_stride: 16\n      }\n    }\n    first_stage_box_predictor_conv_hyperparams {\n      op: CONV\n      regularizer {\n        l2_regularizer {\n          weight: 0.0\n        }\n      }\n      initializer {\n        truncated_normal_initializer {\n          stddev: 0.01\n        }\n      }\n    }\n    first_stage_nms_score_threshold: 0.0\n    first_stage_nms_iou_threshold: 0.7\n    first_stage_max_proposals: 300\n    first_stage_localization_loss_weight: 2.0\n    first_stage_objectness_loss_weight: 1.0\n    initial_crop_size: 14\n    maxpool_kernel_size: 2\n    maxpool_stride: 2\n    second_stage_box_predictor {\n      mask_rcnn_box_predictor {\n        use_dropout: false\n        dropout_keep_probability: 1.0\n        fc_hyperparams {\n          op: FC\n          regularizer {\n            l2_regularizer {\n              weight: 0.0\n            }\n          }\n          initializer {\n            variance_scaling_initializer {\n              factor: 1.0\n              uniform: true\n              mode: FAN_AVG\n            }\n          }\n        }\n        share_box_across_classes: true\n      }\n    }\n    second_stage_post_processing {\n      batch_non_max_suppression {\n        score_threshold: 0.0\n        iou_threshold: 0.6\n        max_detections_per_class: 100\n        max_total_detections: 300\n      }\n      score_converter: SOFTMAX\n    }\n    second_stage_localization_loss_weight: 2.0\n    second_stage_classification_loss_weight: 1.0\n    use_static_shapes: true\n    use_matmul_crop_and_resize: true\n    clip_anchors_to_image: true\n    use_static_balanced_label_sampler: true\n    use_matmul_gather_in_matcher: true\n  }\n}\n\ntrain_config: {\n  batch_size: 5\n  sync_replicas: true\n  startup_delay_steps: 0\n  replicas_to_aggregate: 8\n  num_steps: 25000\n  optimizer {\n    momentum_optimizer: {\n      learning_rate: {\n        cosine_decay_learning_rate {\n          learning_rate_base: .04\n          total_steps: 25000\n          warmup_learning_rate: .013333\n          warmup_steps: 2000\n        }\n      }\n      momentum_optimizer_value: 0.9\n    }\n    use_moving_average: false\n  }\n  fine_tune_checkpoint_version: V2\n  fine_tune_checkpoint: \"/kaggle/working/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8/checkpoint/ckpt-0\"\n  fine_tune_checkpoint_type: \"detection\"\n  data_augmentation_options {\n    random_horizontal_flip {\n    }\n  }\n\n  max_number_of_boxes: 150\n  unpad_groundtruth_tensors: false\n  use_bfloat16: true  # works only on TPUs\n}\n\ntrain_input_reader: {\n  label_map_path: \"/kaggle/working/label_map.txt\"\n  tf_record_input_reader {\n    input_path: \"/kaggle/working/train.record\"\n  }\n}\n\neval_config: {\n  metrics_set: \"coco_detection_metrics\"\n  use_moving_averages: false\n  batch_size: 1;\n}\n\neval_input_reader: {\n  label_map_path: \"/kaggle/working/label_map.txt\"\n  shuffle: false\n  num_epochs: 1\n  tf_record_input_reader {\n    input_path: \"/kaggle/working/eval.record\"\n  }\n}","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:37.108625Z","iopub.execute_input":"2022-01-12T20:21:37.109544Z","iopub.status.idle":"2022-01-12T20:21:37.123864Z","shell.execute_reply.started":"2022-01-12T20:21:37.109474Z","shell.execute_reply":"2022-01-12T20:21:37.12288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir ./tuned","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:37.126981Z","iopub.execute_input":"2022-01-12T20:21:37.127408Z","iopub.status.idle":"2022-01-12T20:21:37.906434Z","shell.execute_reply.started":"2022-01-12T20:21:37.127364Z","shell.execute_reply":"2022-01-12T20:21:37.905264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/models/research/","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:37.910169Z","iopub.execute_input":"2022-01-12T20:21:37.910946Z","iopub.status.idle":"2022-01-12T20:21:37.921569Z","shell.execute_reply.started":"2022-01-12T20:21:37.91091Z","shell.execute_reply":"2022-01-12T20:21:37.920499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!export CUDA_VISIBLE_DEVICES=0","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:37.923397Z","iopub.execute_input":"2022-01-12T20:21:37.923929Z","iopub.status.idle":"2022-01-12T20:21:38.706527Z","shell.execute_reply.started":"2022-01-12T20:21:37.923883Z","shell.execute_reply":"2022-01-12T20:21:38.705206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ['PYTHONPATH'] ","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:38.709225Z","iopub.execute_input":"2022-01-12T20:21:38.710131Z","iopub.status.idle":"2022-01-12T20:21:38.718227Z","shell.execute_reply.started":"2022-01-12T20:21:38.710076Z","shell.execute_reply":"2022-01-12T20:21:38.717216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PYTHONPATH='/kaggle/working/models'\nimport os\nos.environ['PYTHONPATH'] = \"/kaggle/working/models\"\n\nimport sys\nsys.path.append(\"/kaggle/working/models\")\n# !export PYTHONPATH=$PYTHONPATH:/kaggle/working/models","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:38.720172Z","iopub.execute_input":"2022-01-12T20:21:38.720919Z","iopub.status.idle":"2022-01-12T20:21:38.727852Z","shell.execute_reply.started":"2022-01-12T20:21:38.72087Z","shell.execute_reply":"2022-01-12T20:21:38.726703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python object_detection/model_main_tf2.py \\\n    --num_train_steps=3000 \\\n    --sample_1_of_n_eval_examples=1 \\\n    --pipeline_config_path=/kaggle/working/models/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.config \\\n    --model_dir=/kaggle/working/tuned \\\n    --alsologtostderr","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:21:38.729824Z","iopub.execute_input":"2022-01-12T20:21:38.730528Z","iopub.status.idle":"2022-01-12T20:23:05.643674Z","shell.execute_reply.started":"2022-01-12T20:21:38.730481Z","shell.execute_reply":"2022-01-12T20:23:05.642387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.client import device_lib\ndevice_lib.list_local_devices()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T20:23:05.650782Z","iopub.execute_input":"2022-01-12T20:23:05.651073Z","iopub.status.idle":"2022-01-12T20:23:08.680735Z","shell.execute_reply.started":"2022-01-12T20:23:05.651041Z","shell.execute_reply":"2022-01-12T20:23:08.679688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}