Files
ml-arithmetic/Data Generator.ipynb
2018-09-10 14:47:18 -05:00

269 lines
5.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image, ImageDraw, ImageFont\n",
"from random import randint\n",
"import os\n",
"import uuid"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def make_img(top,bottom,operand,mode):\n",
" img = Image.new('1', (128,128), color = (1))\n",
" fnt = ImageFont.truetype('arial.ttf', 50)\n",
" d = ImageDraw.Draw(img)\n",
" answer = get_answer(top,bottom,operand)\n",
" my_string = str(str(top) + '\\n' + operand + str(bottom))\n",
" d.multiline_text((30,5), my_string, font=fnt, align='right')\n",
" d.line([(30,110),(115,110)], width=3)\n",
" if not os.path.exists('./data/{}/{}'.format(mode,answer)):\n",
" os.makedirs('./data/{}/{}'.format(mode,answer))\n",
" img.save('./data/{}/{}/{}.png'.format(mode,answer,uuid.uuid4().hex))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"operators = ['x','+', '-']"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def get_answer(top, bottom, operand):\n",
" if(operand == 'x'):\n",
" return top * bottom\n",
" elif(operand == '+'):\n",
" return top + bottom\n",
" else:\n",
" return top - bottom "
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1000):\n",
" int_one = randint(0,10)\n",
" int_two = randint(0,10)\n",
" if(i/1000 < .6):\n",
" mode = 'train'\n",
" elif(i/1000 >= .6 and i/1000 < .8):\n",
" mode = 'validate'\n",
" else:\n",
" mode = 'test'\n",
" if(int_one > int_two):\n",
" make_img(int_one,int_two, operators[randint(0,2)],mode)\n",
" else:\n",
" make_img(int_two,int_one, operators[randint(0,2)],mode)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"my_set = []\n",
"for i in range(11):\n",
" for j in range(11):\n",
" my_set.append(i+j)\n",
" my_set.append(i*j)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"242"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(my_set)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0,\n",
" 1,\n",
" 2,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 6,\n",
" 7,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 11,\n",
" 12,\n",
" 13,\n",
" 14,\n",
" 15,\n",
" 16,\n",
" 17,\n",
" 18,\n",
" 19,\n",
" 20,\n",
" 21,\n",
" 24,\n",
" 25,\n",
" 27,\n",
" 28,\n",
" 30,\n",
" 32,\n",
" 35,\n",
" 36,\n",
" 40,\n",
" 42,\n",
" 45,\n",
" 48,\n",
" 49,\n",
" 50,\n",
" 54,\n",
" 56,\n",
" 60,\n",
" 63,\n",
" 64,\n",
" 70,\n",
" 72,\n",
" 80,\n",
" 81,\n",
" 90,\n",
" 100}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_set = set(my_set)\n",
"my_set"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"my_list = list(my_set)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"63"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list[39]"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"img = Image.new('1', (128,128), color = (1))\n",
"fnt = ImageFont.truetype('arial.ttf', 50)\n",
"d = ImageDraw.Draw(img)\n",
"top = 10\n",
"bottom = 10\n",
"operand = 'x'\n",
"answer = get_answer(top,bottom,operand)\n",
"my_string = str(str(top) + '\\n' + operand + str(bottom))\n",
"d.multiline_text((30,5), my_string, font=fnt, align='right')\n",
"d.line([(30,110),(115,110)], width=3)\n",
"img.save('./human/test.png')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"for answer in range(100):\n",
" for mode in ['train','validate','test']:\n",
" if not os.path.exists('./data/{}/{}'.format(mode,answer)):\n",
" os.makedirs('./data/{}/{}'.format(mode,answer))"
]
},
{
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}