Hey-Barrels
Baseline for Hey Barrels Challenge
A getting started code for the Hey Barrels challenge.
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
In [2]:
API_KEY = "" # Please enter your API Key [https://www.aicrowd.com/participants/me]
%aicrowd login --api-key $API_KEY
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
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]:
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)
Out[9]:
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
Content
Comments
You must login before you can post a comment.