Commit 32cefe57 authored by Jochen Buettner's avatar Jochen Buettner ✌🏼

Initial commit

parents
Predict.csv
,0,1,file,predicted_class,class_probability
0,1.0,8.5204e-12,2250_fine_protomathesis_1532_p347.jpg,Table,1.0
1,0.61010736,0.38989267,2064_Piccolomini_Sfera_1552_p186.jpg,Table,0.61010736
2,1.0,2.7924847e-15,1718_sacrobosco_sfera_1561_p152.jpg,Table,1.0
3,1.0,7.109514e-14,2066_Piccolomini_Sfera_Varisco_1561_p176.jpg,Table,1.0
4,0.9997545,0.0002455361,2103_beyer_quaestiones_1549_p177.jpg,Table,0.9997545
5,1.0,7.275933e-10,2066_Piccolomini_Sfera_Varisco_1561_p175.jpg,Table,1.0
6,1.0,9.435222e-11,2066_Piccolomini_Sfera_Varisco_1561_p191.jpg,Table,1.0
7,1.0,9.506775e-11,2066_Piccolomini_Sfera_Varisco_1561_p198.jpg,Table,1.0
8,0.960135,0.03986504,2176_blebel_libellus_1588_p92.jpg,Table,0.960135
9,0.9999815,1.845621e-05,2064_Piccolomini_Sfera_1552_p171.jpg,Table,0.9999815
10,1.0,9.596138e-10,2066_Piccolomini_Sfera_Varisco_1561_p179.jpg,Table,1.0
11,0.9999963,3.7340817e-06,2064_Piccolomini_Sfera_1552_p157.jpg,Table,0.9999963
# Table Prediction
* **predict_only.ipynb**: Notebook to predict, whether a page contains a table or not.
* **best_model.h5**: VGG16 trained for table detection
* **to_predict.csv**: list of pages for for which to make a prediction
predict\_only.ipynb reads the list of pages for which to make the predictions, predicts and writes the results to Prediction.csv. Therfore 'predict\_file_path' needs to point fo the directory where the pageimages are to be found.
\ No newline at end of file
File added
{
"cells": [
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"#predictions based on the table classifier model\n",
"\n",
"import keras\n",
"from keras.models import load_model\n",
"from keras.preprocessing.image import ImageDataGenerator\n",
"from keras.applications.vgg16 import preprocess_input\n",
"\n",
"\n",
"#the ususal suspects\n",
"import pandas as pd\n",
"import numpy as np\n",
"import os\n",
"\n",
"from PIL import ImageFile\n",
"ImageFile.LOAD_TRUNCATED_IMAGES = True #deal with truncated images"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Where things are"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"datafile = 'to_predict.csv' #contains the pages for which to make a prediction\n",
"predict_file_path = '/Users/buettner/Desktop/ML/sphaeradata/pageimages/unclassified/' #path to the pageimages"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2250_fine_protomathesis_1532_p347.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2064_Piccolomini_Sfera_1552_p186.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1718_sacrobosco_sfera_1561_p152.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2066_Piccolomini_Sfera_Varisco_1561_p176.jpg</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2103_beyer_quaestiones_1549_p177.jpg</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" id\n",
"0 2250_fine_protomathesis_1532_p347.jpg\n",
"1 2064_Piccolomini_Sfera_1552_p186.jpg\n",
"2 1718_sacrobosco_sfera_1561_p152.jpg\n",
"3 2066_Piccolomini_Sfera_Varisco_1561_p176.jpg\n",
"4 2103_beyer_quaestiones_1549_p177.jpg"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.read_csv(datafile, index_col=0)\n",
"data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load and view model"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"model_4\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"input_5 (InputLayer) (None, 224, 224, 3) 0 \n",
"_________________________________________________________________\n",
"block1_conv1 (Conv2D) (None, 224, 224, 64) 1792 \n",
"_________________________________________________________________\n",
"block1_conv2 (Conv2D) (None, 224, 224, 64) 36928 \n",
"_________________________________________________________________\n",
"block1_pool (MaxPooling2D) (None, 112, 112, 64) 0 \n",
"_________________________________________________________________\n",
"block2_conv1 (Conv2D) (None, 112, 112, 128) 73856 \n",
"_________________________________________________________________\n",
"block2_conv2 (Conv2D) (None, 112, 112, 128) 147584 \n",
"_________________________________________________________________\n",
"block2_pool (MaxPooling2D) (None, 56, 56, 128) 0 \n",
"_________________________________________________________________\n",
"block3_conv1 (Conv2D) (None, 56, 56, 256) 295168 \n",
"_________________________________________________________________\n",
"block3_conv2 (Conv2D) (None, 56, 56, 256) 590080 \n",
"_________________________________________________________________\n",
"block3_conv3 (Conv2D) (None, 56, 56, 256) 590080 \n",
"_________________________________________________________________\n",
"block3_pool (MaxPooling2D) (None, 28, 28, 256) 0 \n",
"_________________________________________________________________\n",
"block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160 \n",
"_________________________________________________________________\n",
"block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808 \n",
"_________________________________________________________________\n",
"block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808 \n",
"_________________________________________________________________\n",
"block4_pool (MaxPooling2D) (None, 14, 14, 512) 0 \n",
"_________________________________________________________________\n",
"block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808 \n",
"_________________________________________________________________\n",
"block5_pool (MaxPooling2D) (None, 7, 7, 512) 0 \n",
"_________________________________________________________________\n",
"global_average_pooling2d_5 ( (None, 512) 0 \n",
"_________________________________________________________________\n",
"dense_7 (Dense) (None, 2) 1026 \n",
"=================================================================\n",
"Total params: 14,715,714\n",
"Trainable params: 14,715,714\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"modelfile = 'best_model.h5'\n",
"model = load_model(modelfile)\n",
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Prediction (may take a while)\n",
"\n",
"Create a DataGenerator"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 12 validated image filenames.\n"
]
}
],
"source": [
"data = pd.read_csv(datafile, index_col=0)\n",
"predict_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)\n",
"\n",
"dataforpredict = predict_datagen.flow_from_dataframe(dataframe=data,\n",
" directory=predict_file_path,\n",
" x_col=\"id\",\n",
" target_size=(224,224),\n",
" color_mode='rgb',\n",
" batch_size=32,\n",
" class_mode=None,\n",
" shuffle=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"do the predictions, may take a while"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r",
"1/1 [==============================] - 2s 2s/step\n"
]
}
],
"source": [
"dataforpredict.reset()\n",
"predictions = model.predict_generator(dataforpredict, \n",
" steps=len(dataforpredict), \n",
" verbose=1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert to df "
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.000000</td>\n",
" <td>8.520400e-12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.610107</td>\n",
" <td>3.898927e-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.000000</td>\n",
" <td>2.792485e-15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.000000</td>\n",
" <td>7.109514e-14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.999754</td>\n",
" <td>2.455361e-04</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1\n",
"0 1.000000 8.520400e-12\n",
"1 0.610107 3.898927e-01\n",
"2 1.000000 2.792485e-15\n",
"3 1.000000 7.109514e-14\n",
"4 0.999754 2.455361e-04"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pdPredictions=pd.DataFrame()\n",
"pdPredictions=pd.DataFrame(predictions)\n",
"pdPredictions.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add filenames, classnames and classprobabilies to df and save"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>file</th>\n",
" <th>predicted_class</th>\n",
" <th>class_probability</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.000000</td>\n",
" <td>8.520400e-12</td>\n",
" <td>2250_fine_protomathesis_1532_p347.jpg</td>\n",
" <td>Table</td>\n",
" <td>1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.610107</td>\n",
" <td>3.898927e-01</td>\n",
" <td>2064_Piccolomini_Sfera_1552_p186.jpg</td>\n",
" <td>Table</td>\n",
" <td>0.610107</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.000000</td>\n",
" <td>2.792485e-15</td>\n",
" <td>1718_sacrobosco_sfera_1561_p152.jpg</td>\n",
" <td>Table</td>\n",
" <td>1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.000000</td>\n",
" <td>7.109514e-14</td>\n",
" <td>2066_Piccolomini_Sfera_Varisco_1561_p176.jpg</td>\n",
" <td>Table</td>\n",
" <td>1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.999754</td>\n",
" <td>2.455361e-04</td>\n",
" <td>2103_beyer_quaestiones_1549_p177.jpg</td>\n",
" <td>Table</td>\n",
" <td>0.999754</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1 file \\\n",
"0 1.000000 8.520400e-12 2250_fine_protomathesis_1532_p347.jpg \n",
"1 0.610107 3.898927e-01 2064_Piccolomini_Sfera_1552_p186.jpg \n",
"2 1.000000 2.792485e-15 1718_sacrobosco_sfera_1561_p152.jpg \n",
"3 1.000000 7.109514e-14 2066_Piccolomini_Sfera_Varisco_1561_p176.jpg \n",
"4 0.999754 2.455361e-04 2103_beyer_quaestiones_1549_p177.jpg \n",
"\n",
" predicted_class class_probability \n",
"0 Table 1.000000 \n",
"1 Table 0.610107 \n",
"2 Table 1.000000 \n",
"3 Table 1.000000 \n",
"4 Table 0.999754 "
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pdPredictions['file'] = dataforpredict.filenames\n",
"pdPredictions.columns = pdPredictions.columns.astype(str)\n",
"pdPredictions['predicted_class'], pdPredictions['class_probability'] = np.nan , np.nan\n",
"pdPredictions['predicted_class'] = np.where(pdPredictions['0']>=pdPredictions['1'], 'Table', 'NoTable')\n",
"pdPredictions['class_probability'] = np.where(pdPredictions['0']>=pdPredictions['1'], pdPredictions['0'], pdPredictions['1'])\n",
"pdPredictions.to_csv('Predictions.csv')\n",
"pdPredictions.head()"
]
},
{
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
,id
0,2250_fine_protomathesis_1532_p347.jpg
1,2064_Piccolomini_Sfera_1552_p186.jpg
2,1718_sacrobosco_sfera_1561_p152.jpg
3,2066_Piccolomini_Sfera_Varisco_1561_p176.jpg
4,2103_beyer_quaestiones_1549_p177.jpg
5,2066_Piccolomini_Sfera_Varisco_1561_p175.jpg
6,2066_Piccolomini_Sfera_Varisco_1561_p191.jpg
7,2066_Piccolomini_Sfera_Varisco_1561_p198.jpg
8,2176_blebel_libellus_1588_p92.jpg
9,2064_Piccolomini_Sfera_1552_p171.jpg
10,2066_Piccolomini_Sfera_Varisco_1561_p179.jpg
11,2064_Piccolomini_Sfera_1552_p157.jpg
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment