{"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 cv2\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-12T13:50:08.867104Z","iopub.execute_input":"2022-04-12T13:50:08.867667Z","iopub.status.idle":"2022-04-12T13:50:09.281108Z","shell.execute_reply.started":"2022-04-12T13:50:08.867553Z","shell.execute_reply":"2022-04-12T13:50:09.280293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_count = pd.read_csv(\"../input/iwildcam2022-fgvc9/metadata/metadata/train_sequence_counts.csv\")\ndf_count.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:50:09.282580Z","iopub.execute_input":"2022-04-12T13:50:09.282791Z","iopub.status.idle":"2022-04-12T13:50:09.320578Z","shell.execute_reply.started":"2022-04-12T13:50:09.282765Z","shell.execute_reply":"2022-04-12T13:50:09.319694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = []\nfor i in range(max(df_count[\"count\"])):\n    count.append(len(df_count[df_count[\"count\"]==i]))\nf = plt.figure(figsize=(15,8))\nf = plt.title(\"distribution of count\")\nf = plt.bar([x for x in range(len(count))],count)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:50:09.321769Z","iopub.execute_input":"2022-04-12T13:50:09.322398Z","iopub.status.idle":"2022-04-12T13:50:09.653431Z","shell.execute_reply.started":"2022-04-12T13:50:09.322354Z","shell.execute_reply":"2022-04-12T13:50:09.652701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json, codecs\n\nwith codecs.open(\"../input/iwildcam2022-fgvc9/metadata/metadata/iwildcam2022_train_annotations.json\", 'r',\n                 encoding='utf-8', errors='ignore') as f:\n    train_meta = json.load(f)\n    \nwith codecs.open(\"../input/iwildcam2022-fgvc9/metadata/metadata/iwildcam2022_test_information.json\", 'r',\n                 encoding='utf-8', errors='ignore') as f:\n    test_meta = json.load(f)\n\ntrain_meta.keys()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:50:09.655632Z","iopub.execute_input":"2022-04-12T13:50:09.656461Z","iopub.status.idle":"2022-04-12T13:50:12.233020Z","shell.execute_reply.started":"2022-04-12T13:50:09.656412Z","shell.execute_reply":"2022-04-12T13:50:12.231942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_seq = pd.DataFrame(train_meta['images'])\ndf_seq = df_seq.drop([\"seq_num_frames\",\"datetime\",\"width\",\"width\",\"height\",\"id\",\"sub_location\",\"location\"],axis=1)\ndf_seq.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:50:12.234103Z","iopub.execute_input":"2022-04-12T13:50:12.234322Z","iopub.status.idle":"2022-04-12T13:50:13.002140Z","shell.execute_reply.started":"2022-04-12T13:50:12.234294Z","shell.execute_reply":"2022-04-12T13:50:13.001317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Play a sequence**","metadata":{}},{"cell_type":"code","source":"seq_id = \"3021a08e-7d42-11eb-8fb5-0242ac1c0002\"\ndf_c = df_count[df_count[\"seq_id\"]==seq_id]\ndf_c","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:50:13.003532Z","iopub.execute_input":"2022-04-12T13:50:13.003838Z","iopub.status.idle":"2022-04-12T13:50:13.014963Z","shell.execute_reply.started":"2022-04-12T13:50:13.003797Z","shell.execute_reply":"2022-04-12T13:50:13.014031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_seq[df_seq[\"seq_id\"]==seq_id]\nfor i in range(len(df)):\n    img = cv2.imread(\"../input/iwildcam2022-fgvc9/train/train/\"+df.iloc[i][\"file_name\"])\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:50:13.016593Z","iopub.execute_input":"2022-04-12T13:50:13.016911Z","iopub.status.idle":"2022-04-12T13:50:17.204775Z","shell.execute_reply.started":"2022-04-12T13:50:13.016869Z","shell.execute_reply":"2022-04-12T13:50:17.203904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data setup**","metadata":{}},{"cell_type":"code","source":"l_count,train,test = [],[],[]\nfor i in range(len(df_seq)):\n    line = df_seq.iloc[i]\n    seq_id = line[\"seq_id\"]\n    count = df_count[df_count[\"seq_id\"]==seq_id][\"count\"]\n    if len(count) == 1:\n        train.append(line)\n        l_count.append(count.values[0])\n    else :\n        test.append(line)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:50:17.205841Z","iopub.execute_input":"2022-04-12T13:50:17.206060Z","iopub.status.idle":"2022-04-12T13:53:22.049397Z","shell.execute_reply.started":"2022-04-12T13:50:17.206033Z","shell.execute_reply":"2022-04-12T13:53:22.048455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.DataFrame(train)\ntrain[\"count\"] = l_count\n\ntest = pd.DataFrame(test)\nprint(\"long\",len(train))\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:53:22.050971Z","iopub.execute_input":"2022-04-12T13:53:22.051290Z","iopub.status.idle":"2022-04-12T13:53:28.534801Z","shell.execute_reply.started":"2022-04-12T13:53:22.051250Z","shell.execute_reply":"2022-04-12T13:53:28.533898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 16/9\n#a = 20 *32\nredim = [180,320]\nredim","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:53:28.537466Z","iopub.execute_input":"2022-04-12T13:53:28.537709Z","iopub.status.idle":"2022-04-12T13:53:28.544450Z","shell.execute_reply.started":"2022-04-12T13:53:28.537673Z","shell.execute_reply":"2022-04-12T13:53:28.543404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,y_train  = [],[]\nfor i in range(len(train)):\n    line = train.iloc[i]\n    path = line[\"file_name\"]\n    count = line[\"count\"]\n    y_train.append(count)\n    img = cv2.imread(\"../input/iwildcam2022-fgvc9/train/train/\"+path)\n    X_train.append(cv2.resize(img, [redim[1],redim[0]],interpolation = cv2.INTER_AREA))\nlen(X_train),len(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T13:53:28.548878Z","iopub.execute_input":"2022-04-12T13:53:28.549194Z","iopub.status.idle":"2022-04-12T14:03:56.757050Z","shell.execute_reply.started":"2022-04-12T13:53:28.549161Z","shell.execute_reply":"2022-04-12T14:03:56.756082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:03:56.759873Z","iopub.execute_input":"2022-04-12T14:03:56.761091Z","iopub.status.idle":"2022-04-12T14:03:56.768635Z","shell.execute_reply.started":"2022-04-12T14:03:56.761038Z","shell.execute_reply":"2022-04-12T14:03:56.768037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[50]);","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:03:56.770038Z","iopub.execute_input":"2022-04-12T14:03:56.771182Z","iopub.status.idle":"2022-04-12T14:03:57.011458Z","shell.execute_reply.started":"2022-04-12T14:03:56.771134Z","shell.execute_reply":"2022-04-12T14:03:57.009066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras as keras\nimport keras.layers as layers\n\nbackbone = tf.keras.applications.mobilenet.MobileNet(\n    input_shape=redim+[3],\n    alpha=1.0,\n    depth_multiplier=1,\n    dropout=0.001,\n    include_top=False,\n    weights='imagenet',\n    input_tensor=None,\n    pooling=None,\n)\nbackbone.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:03:57.012805Z","iopub.execute_input":"2022-04-12T14:03:57.013681Z","iopub.status.idle":"2022-04-12T14:04:05.239975Z","shell.execute_reply.started":"2022-04-12T14:03:57.013627Z","shell.execute_reply":"2022-04-12T14:04:05.238790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\n\ninp = keras.Input(shape=redim+[3])\nx = layers.BatchNormalization()(inp)\nfor layer in backbone.layers:\n    x = layer(x)\nx = layers.AveragePooling2D()(x)\nx = layers.Conv2D(512,1)(x)\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Flatten()(x)\nx = layers.Dense(256,activation=\"relu\")(x)\nx = layers.Dropout(.2)(x)\nx = layers.Dense(1)(x)\n\nmodel = Model(inp,x)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:04:05.241559Z","iopub.execute_input":"2022-04-12T14:04:05.241817Z","iopub.status.idle":"2022-04-12T14:04:05.676955Z","shell.execute_reply.started":"2022-04-12T14:04:05.241785Z","shell.execute_reply":"2022-04-12T14:04:05.676339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(keras.optimizers.Adam(),loss=keras.losses.mae,metrics=keras.metrics.MAE)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:04:05.677882Z","iopub.execute_input":"2022-04-12T14:04:05.678138Z","iopub.status.idle":"2022-04-12T14:04:05.704156Z","shell.execute_reply.started":"2022-04-12T14:04:05.678105Z","shell.execute_reply":"2022-04-12T14:04:05.703093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train)\ny_train = np.array(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:04:05.706081Z","iopub.execute_input":"2022-04-12T14:04:05.706731Z","iopub.status.idle":"2022-04-12T14:04:08.688326Z","shell.execute_reply.started":"2022-04-12T14:04:05.706686Z","shell.execute_reply":"2022-04-12T14:04:08.687217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss', patience=15, verbose=1,\n    mode='auto', restore_best_weights=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:04:08.689468Z","iopub.execute_input":"2022-04-12T14:04:08.689687Z","iopub.status.idle":"2022-04-12T14:04:08.693574Z","shell.execute_reply.started":"2022-04-12T14:04:08.689660Z","shell.execute_reply":"2022-04-12T14:04:08.693030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h = model.fit(X_train,y_train,epochs=100,validation_split=.2,callbacks=[earlystop])","metadata":{"execution":{"iopub.status.busy":"2022-04-12T14:04:08.694438Z","iopub.execute_input":"2022-04-12T14:04:08.694753Z","iopub.status.idle":"2022-04-12T14:15:23.467535Z","shell.execute_reply.started":"2022-04-12T14:04:08.694725Z","shell.execute_reply":"2022-04-12T14:15:23.466230Z"},"trusted":true},"execution_count":null,"outputs":[]}]}