Loading
Feedback

Hey-Barrels

Baseline for Hey Barrels Challenge

A getting started code for the Hey Barrels challenge.

By ashivani


In [1]:
from pathlib import Path

import tensorflow as tf
import cv2 as cv
import pandas as pd
import numpy as np

!pip install git+https://gitlab.aicrowd.com/aicrowd/aicrowd-cli.git >/dev/null
%load_ext aicrowd.magic
  Running command git clone -q https://gitlab.aicrowd.com/aicrowd/aicrowd-cli.git /tmp/pip-req-build-u91h4e3d
In [2]:
API_KEY = "" # Please enter your API Key [https://www.aicrowd.com/participants/me]
%aicrowd login --api-key $API_KEY
API Key valid
Saved API Key successfully!
In [3]:
%aicrowd dataset list -c hey-barrels
%aicrowd dataset download -c hey-barrels -j 3

!unzip train.zip >/dev/null
!unzip test.zip >/dev/null
              Datasets for challenge #750                                       
┌───┬────────────────────────┬─────────────┬───────────┐                        
│ # │ Title                  │ Description │      Size │                        
├───┼────────────────────────┼─────────────┼───────────┤                        
│ 0 │ example_submission.csv │ -           │   7.45 KB │                        
│ 1 │ test.zip               │ -           │ 485.96 MB │                        
│ 2 │ train.zip              │ -           │ 494.61 MB │                        
└───┴────────────────────────┴─────────────┴───────────┘                        


In [4]:
TRAIN_DATA_DIR = "train/images"
TRAIN_LABELS = "train/meta-data.csv"
TEST_DATA_DIR = "test"
In [5]:
TRAIN_DATA = {}
for im in Path(TRAIN_DATA_DIR).iterdir():
    # scale down image
    TRAIN_DATA[im.name] = cv.resize(cv.imread(str(im), 0), (150, 200)).flatten()
In [6]:
X = np.array(list(TRAIN_DATA.values()))
In [7]:
Y = pd.read_csv(TRAIN_LABELS, index_col="filename").reindex(TRAIN_DATA.keys())
Y.head()
Out[7]:
barrels_count pigs_count
filename
0130.png 10 10
0078.png 7 8
0465.png 12 8
0436.png 11 5
0175.png 9 9
In [8]:
model = tf.keras.Sequential([
    tf.keras.Input(shape=(150*200)),
    tf.keras.layers.Dense(8192),
    tf.keras.layers.Dense(512),
    tf.keras.layers.Dense(2),
])
model.compile(loss="mean_squared_error", optimizer=tf.keras.optimizers.Adam(learning_rate=0.001))
In [9]:
model.fit(x=X, y=Y.values, epochs=5)
Epoch 1/5
16/16 [==============================] - 20s 1s/step - loss: 6367730860.3076
Epoch 2/5
16/16 [==============================] - 19s 1s/step - loss: 665434505.4118
Epoch 3/5
16/16 [==============================] - 19s 1s/step - loss: 64884318.1176
Epoch 4/5
16/16 [==============================] - 19s 1s/step - loss: 18244301.8824
Epoch 5/5
16/16 [==============================] - 19s 1s/step - loss: 5015027.9412
Out[9]:
<tensorflow.python.keras.callbacks.History at 0x7f85c15b7b90>
In [10]:
TEST_DATA = {}
for im in Path(TEST_DATA_DIR).iterdir():
    # scale down image
    TEST_DATA[im.name] = cv.resize(cv.imread(str(im), 0), (150, 200)).flatten()

X_test = np.array(list(TEST_DATA.values()))
In [11]:
y_test = model.predict(X_test)
# we cant have -ve sheeps and barrels...
y_test[y_test < 0] = 0
# ...or a fractional number of them
y_test = y_test.astype("int")
In [16]:
submission = pd.DataFrame(zip(TEST_DATA.keys(), y_test[:,0], y_test[:,1]), columns=["filename", "barrels_count", "pigs_count"]).sort_values("filename")
submission.to_csv("submission.csv", index=False)
In [19]:
%aicrowd submission create -c hey-barrels -f submission.csv
                                       ╭─────────────────────────╮                                       
                                       │ Successfully submitted! │                                       
                                       ╰─────────────────────────╯                                       
                                             Important links                                             
┌──────────────────┬────────────────────────────────────────────────────────────────────────────────────┐
│  This submission │ https://www.aicrowd.com/challenges/ml-battleground/submissions/123272              │
│                  │                                                                                    │
│  All submissions │ https://www.aicrowd.com/challenges/ml-battleground/submissions?my_submissions=true │
│                  │                                                                                    │
│      Leaderboard │ https://www.aicrowd.com/challenges/ml-battleground/leaderboards                    │
│                  │                                                                                    │
│ Discussion forum │ https://discourse.aicrowd.com/c/ml-battleground                                    │
│                  │                                                                                    │
│   Challenge page │ https://www.aicrowd.com/challenges/ml-battleground                                 │
└──────────────────┴────────────────────────────────────────────────────────────────────────────────────┘
{'submission_id': 123272, 'created_at': '2021-02-25T12:36:25.684Z'}
↕️  Read More

Liked by  

Comments

You must login before you can post a comment.