This commit is contained in:
soumya ranjan 2019-05-28 11:06:19 +05:30
Родитель c266a144b3
Коммит 201448e661
5 изменённых файлов: 810 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,262 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"from pathlib import Path\n",
"from PIL import Image\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"cwd = Path.cwd()\n",
"from collections import defaultdict\n",
"from random import random\n",
"\n",
"def folder2coco(folder, map_file, pct=0.2):\n",
" data_dir = cwd/'data'\n",
" \n",
" # Create class_id to species mapping\n",
" mapper = pd.read_csv(data_dir/map_file)\n",
" id2species = {idx: species for idx, species in zip(mapper['class_idx'].values, mapper['original_class'].values)}\n",
" \n",
" # Define the coco format\n",
" train = defaultdict(list)\n",
" valid = defaultdict(list)\n",
" info = {\n",
" 'description': 'The 2019 Snake Species Identification Challenge',\n",
" 'url': 'https://www.aicrowd.com/challenges/snake-species-identification-challenge',\n",
" 'version': 1.0,\n",
" 'date_created': '2019-05-10'\n",
" }\n",
" train['info'] = info\n",
" valid['info'] = info\n",
"\n",
" counter = 0\n",
" for idx, species_dir in enumerate((data_dir/folder).iterdir()):\n",
" train['categories'].append({'id': idx, 'name': id2species[int(species_dir.stem.split('-')[-1])]})\n",
" valid['categories'].append({'id': idx, 'name': id2species[int(species_dir.stem.split('-')[-1])]})\n",
" for image_path in species_dir.iterdir():\n",
" try:\n",
" coco = train if random() > pct else valid\n",
" (w, h) = Image.open(image_path).size\n",
" coco['images'].append({'id': counter, 'file_name': f'/{folder}/{species_dir.name}/{image_path.name}', 'width': w, 'height': h})\n",
" coco['annotations'].append({'id': counter, 'image_id': counter, 'category_id': idx})\n",
" counter += 1\n",
" except OSError as e:\n",
" pass\n",
" \n",
" return train, valid"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"train, valid = folder2coco('train', 'class_id_mapping.csv')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"json.dump(train, (cwd/'data'/'train.json').open('wt', encoding='utf-8'))\n",
"json.dump(valid, (cwd/'data'/'valid.json').open('wt', encoding='utf-8'))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(45, 45)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(train['categories']), len(valid['categories'])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(66054, 16363)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(train['images']), len(valid['images'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(66054, 16363)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(train['annotations']), len(valid['annotations'])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['info', 'categories', 'images', 'annotations'])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train.keys()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'id': 0, 'name': 'pantherophis_vulpinus'},\n",
" {'id': 1, 'name': 'nerodia_erythrogaster'}]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train['categories'][:2]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'id': 0, 'image_id': 0, 'category_id': 0},\n",
" {'id': 3, 'image_id': 3, 'category_id': 0}]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train['annotations'][:2]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'id': 0,\n",
" 'file_name': '/train/class-543/ca34c7358cec2385f4f47ecd3a6a160e.jpg',\n",
" 'width': 2000,\n",
" 'height': 1500},\n",
" {'id': 3,\n",
" 'file_name': '/train/class-543/49cdb6ea29d4b007cb32b840eaca7061.jpg',\n",
" 'width': 1500,\n",
" 'height': 2000}]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train['images'][:2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -0,0 +1,191 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import pandas as pd\n",
"from pathlib import Path\n",
"from PIL import Image"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"cwd = Path.cwd()\n",
"from collections import defaultdict\n",
"\n",
"def test2coco(folder):\n",
" data_dir = cwd/'data'\n",
" \n",
" # Define the coco format\n",
" test = defaultdict(list)\n",
" test['info'] = {\n",
" 'description': 'The 2019 Snake Species Identification Challenge',\n",
" 'url': 'https://www.aicrowd.com/challenges/snake-species-identification-challenge',\n",
" 'version': 1.0,\n",
" 'date_created': '2019-05-27'\n",
" }\n",
" counter = 0\n",
" for image_path in (data_dir/folder).iterdir():\n",
" try:\n",
" (w, h) = Image.open(image_path).size\n",
" test['images'].append({'id': counter, 'file_name': f'/{folder}/{image_path.name}', 'width': w, 'height': h})\n",
" counter += 1\n",
" except OSError as e:\n",
" pass\n",
" \n",
" return test"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"class_id_mapping.csv sample_submission_file.csv train.json\r\n",
"round1\t\t train\t\t\t valid.json\r\n"
]
}
],
"source": [
"!ls data/"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"test = test2coco('round1')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"json.dump(test, (cwd/'data'/'round1.json').open('wt', encoding='utf-8'))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['info', 'images'])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test.keys()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'id': 0,\n",
" 'file_name': '/round1/3927a45206e4fe7c7bfea9d9e14a52db.jpg',\n",
" 'width': 375,\n",
" 'height': 500},\n",
" {'id': 1,\n",
" 'file_name': '/round1/eda8b5404a77be4997242fc9d634de12.jpg',\n",
" 'width': 375,\n",
" 'height': 500},\n",
" {'id': 2,\n",
" 'file_name': '/round1/b775c934273f4eb008b261e1009b80c7.jpg',\n",
" 'width': 1500,\n",
" 'height': 2000},\n",
" {'id': 3,\n",
" 'file_name': '/round1/433fd2459cc82e05c2f8d5e755d37d28.jpg',\n",
" 'width': 800,\n",
" 'height': 599},\n",
" {'id': 4,\n",
" 'file_name': '/round1/da08f02b6e2265737d78685768501ec6.jpg',\n",
" 'width': 375,\n",
" 'height': 500},\n",
" {'id': 5,\n",
" 'file_name': '/round1/2b5b0818c289068d25e3682cfeeda0f8.jpg',\n",
" 'width': 1200,\n",
" 'height': 2000},\n",
" {'id': 6,\n",
" 'file_name': '/round1/3b6c1796ccae7617aea30331e2a52138.jpg',\n",
" 'width': 375,\n",
" 'height': 500},\n",
" {'id': 7,\n",
" 'file_name': '/round1/6a5b3e573c1e86cddea1c55d46f21bcd.jpg',\n",
" 'width': 2000,\n",
" 'height': 1329},\n",
" {'id': 8,\n",
" 'file_name': '/round1/76bf7d58ecba7fe1cd2bd57acecaf07f.jpg',\n",
" 'width': 500,\n",
" 'height': 375},\n",
" {'id': 9,\n",
" 'file_name': '/round1/2b3c9be576d0fbe913a2eac590d6fb84.jpg',\n",
" 'width': 1125,\n",
" 'height': 2000}]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test['images'][:10]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Просмотреть файл

@ -0,0 +1 @@
/media/srm/ssd2/datasets/snakes