{"cells":[{"metadata":{},"cell_type":"markdown","source":"Current top score on Imagenet Classifaction used intresting idea. We can improve Image accuracy by generating pseuda labels from completly unrelated data, and than training everything on larger student network. For more details read this paper: https://arxiv.org/pdf/1911.04252v2.pdf. \n\n\nHow we can generate more data? \n\n* 1) We dondloand bengali vocab (https://github.com/MinhasKamal/BengaliDictionary)\n* 2) Randomly cut the words in the middle (can be improved) \n* 3) Plot them \n* 4) You can save this images and use for training \n\n\nObviously this is just the begining, one can become more creative and improve this code. \n\nEdit: Let me know if i did not site your code. \n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from PIL import Image,ImageDraw,ImageFont\nimport numpy as np\nfrom pathlib2 import Path\nfrom functools import partial\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom scipy.ndimage import filters\nfrom tqdm import tqdm\nimport os\nimport random","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"SIZE = 224\nHEIGHT = 137\nWIDTH = 236\nPATH =  '../input/bengali-synth-data/'\n\ndf = pd.read_csv(PATH + 'BengaliWordList_439.csv').sample(frac=1).reset_index(drop=True).iloc[:, 0].dropna()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#random font that i found on internet, \n#one can add more fonts to mimic hand writing \n\nfont_list= [ImageFont.truetype(PATH + \"kalpurush.ttf\",100), \n              ImageFont.truetype(PATH + \"Atma-Light.ttf\",100), \n              ImageFont.truetype(PATH + \"Galada-Regular.ttf\",100), \n              ImageFont.truetype(PATH + \"Mina-Regular.ttf\",100)]\n\ndef DrawBengli(txt,font):\n    image = np.zeros(shape=(HEIGHT,WIDTH),dtype=np.uint8)\n    x = Image.fromarray(image)\n    draw = ImageDraw.Draw(x)\n    draw.text((10,10),txt,(255),font=random.choice(font))\n    p = np.array(x)\n    return p\n\n#lafoss kernel 128 x 128\n#https://www.kaggle.com/iafoss/image-preprocessing-128x128\ndef bbox(img):\n    rows = np.any(img, axis=1)\n    cols = np.any(img, axis=0)\n    rmin, rmax = np.where(rows)[0][[0, -1]]\n    cmin, cmax = np.where(cols)[0][[0, -1]]\n    return rmin, rmax, cmin, cmax\n\ndef crop_resize(img0, size=SIZE, pad=20):\n    ymin,ymax,xmin,xmax = bbox(img0[:,5:-5] > 80)\n    xmin = xmin - 8 if(xmin > 8) else 0\n    ymin = ymin - 5 if(ymin > 5) else 0\n    xmax = xmax + 8 if(xmax < WIDTH - 8) else WIDTH\n    ymax = ymax + 5 if(ymax < HEIGHT - 5) else HEIGHT\n    img = img0[ymin:ymax,xmin:xmax]\n    img[img < 30] = 0\n    lx, ly = xmax-xmin,ymax-ymin\n    l = max(lx,ly) + pad\n    img = np.pad(img, [((l-ly)//2,), ((l-lx)//2,)], mode='constant')\n    return Image.fromarray(img).resize((224, 224), resample=Image.BILINEAR).convert('RGB')\n\n    \ndef guas_filter(img):\n    brush = np.random.uniform(1, 4)\n    return Image.fromarray(filters.gaussian_filter(img, brush))\n\ndef get_random_part(ts, max_ln = 4):\n    #getting random parts of the word\n    len_wd = len(ts)\n    try:\n        return ts[np.random.randint(0, len_wd//2): np.random.randint(len_wd//2, len_wd)][:np.random.randint(1, max_ln)]\n    except:\n        return ts[:np.random.randint(1, 4)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def resize_one(indx, FOLDER = 'img_synt_224_thin', test=False, fonts = font_list):\n    wd_orig = df.iloc[indx]\n    wd = get_random_part(wd_orig)\n    img = crop_resize(DrawBengli(wd, fonts))\n    if test:\n        #print (wd_orig)\n        return guas_filter(img)\n    else:\n        guas_filter(img).save(f'{FOLDER}/cust_1_{indx}.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig=plt.figure(figsize=(8, 8))\ncolumns = 7\nrows = 7\nfor i in range(1, columns*rows +1):\n    img = resize_one(i + 10, test=True)\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(img)\nplt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}