Arquivos
cuneiform-sign-detection-code/experiments/line_segmentation/precompute_line_segmentations.ipynb
T
2020-11-19 12:18:53 +01:00

302 linhas
6.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre-compute and store line segmentations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import pandas as pd\n",
"from PIL import Image\n",
"from ast import literal_eval"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# torch\n",
"import torch\n",
"import torchvision\n",
"# addons\n",
"from tqdm import tqdm"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#%pylab inline\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# for auto-reloading external modules\n",
"# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"relative_path = '../../'\n",
"# ensure that parent path is on the python path in order to have all packages available\n",
"import sys, os\n",
"parent_path = os.path.join(os.getcwd(), relative_path)\n",
"parent_path = os.path.realpath(parent_path) # os.path.abspath(...)\n",
"sys.path.insert(0, parent_path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from lib.models.trained_model_loader import get_line_net_fcn\n",
"from lib.datasets.cunei_dataset_segments import CuneiformSegments, get_segment_meta\n",
"from lib.transliteration.sign_labels import get_label_list\n",
"from lib.utils.transform_utils import UnNormalize\n",
"\n",
"from lib.detection.run_gen_line_detection import gen_line_detections"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Config Basics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# toggle generation\n",
"save_line_detections = True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# set line segmentation network\n",
"line_model_version = 'v002'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# dataset config\n",
"collections = ['test', 'train', 'saa01', 'saa05', 'saa06', 'saa08', 'saa09', 'saa10', 'saa13', 'saa16'] \n",
"#collections = ['saa01', 'saa05']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data_layer_params = dict(batch_size=[128, 16],\n",
" img_channels=1,\n",
" gray_mean=[0.5],\n",
" gray_std=[1.0], \n",
" num_classes = 2\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Config Data Augmentation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"num_classes = data_layer_params['num_classes']\n",
"num_c = data_layer_params['img_channels']\n",
"gray_mean = data_layer_params['gray_mean']\n",
"gray_std = data_layer_params['gray_std']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"re_transform = torchvision.transforms.Compose([\n",
" UnNormalize(mean=gray_mean, std=gray_std),\n",
" torchvision.transforms.ToPILImage(),\n",
" ])\n",
"re_transform_rgb = torchvision.transforms.Compose([\n",
" UnNormalize(mean=gray_mean * 3, std=gray_std * 3),\n",
" torchvision.transforms.ToPILImage(),\n",
" ])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#use_gpu = torch.cuda.is_available()\n",
"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model_fcn = get_line_net_fcn(line_model_version, device, num_classes=num_classes, num_c=num_c)\n",
"print(model_fcn)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Run experiment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": false
},
"outputs": [],
"source": [
"for saa_version in collections:\n",
" print('collection: <><>{}<><>'.format(saa_version))\n",
" \n",
" ### Get collection dataset\n",
" dataset = CuneiformSegments(collections=[saa_version], relative_path=relative_path, \n",
" only_annotated=False, only_assigned=True, preload_segments=False)\n",
" \n",
" # filter collection dataset - OPTIONAL\n",
" didx_list = range(len(dataset))\n",
" \n",
" ### Generate line detections\n",
" gen_line_detections(didx_list, dataset, saa_version, relative_path,\n",
" line_model_version, model_fcn, re_transform, device,\n",
" save_line_detections) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}