sajid006

Wordbreaking11

Oct 14th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Oct 10 21:55:15 2021
  4.  
  5. @author: Sajid
  6. """
  7.  
  8. from skimage.io import imread, imshow
  9. from skimage.filters import gaussian, threshold_otsu
  10. from skimage.feature import canny
  11. from skimage.transform import probabilistic_hough_line, rotate
  12.  
  13. #testing
  14. import numpy as np
  15. import os
  16. import cv2
  17. import math
  18. import matplotlib.pyplot as plt
  19.  
  20. import torch
  21. from torch import nn
  22. from torch import optim
  23. import torch.nn.functional as F
  24. from torchvision import datasets, transforms, models
  25.  
  26.  
  27.  
  28. from collections import OrderedDict
  29. from PIL import Image
  30.  
  31. import pandas as pd
  32. import seaborn as sns
  33.  
  34. import math
  35. import cv2
  36. import numpy as np
  37.  
  38.  
  39.  
  40. # define the CNN architecture
  41. class Net(nn.Module):
  42. ### TODO: choose an architecture, and complete the class
  43.  
  44. def __init__(self):
  45. super(Net, self).__init__()
  46.  
  47. # convolutional layer (sees 64x64x3 image tensor)
  48. self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
  49. # convolutional layer (sees 32x32x16 tensor)
  50. self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
  51. # convolutional layer (sees 16x16x32 tensor)
  52. self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
  53. # convolutional layer (sees 8x8x64 tensor)
  54. self.conv4 = nn.Conv2d(64, 128, 3, padding=1)
  55. self.conv5 = nn.Conv2d(128, 256, 3, padding=1)
  56.  
  57. # max pooling layer
  58. self.pool = nn.MaxPool2d(2, 2)
  59. # linear layer (256 * 2 * 2 -> 512)
  60. self.fc1 = nn.Linear(256 * 2 * 2 , 2048)
  61. # linear layer (512 -> 50)
  62. self.fc2 = nn.Linear(2048,512)
  63. # dropout layer (p=0.2)
  64. self.dropout = nn.Dropout(0.2)
  65. self.fc3 = nn.Linear(512,50)
  66. #self.softmax = nn.Softmax(dim=1)
  67.  
  68.  
  69.  
  70. def forward(self, x):
  71. # add sequence of convolutional and max pooling layers
  72. x = self.pool(F.relu(self.conv1(x)))
  73. x = self.pool(F.relu(self.conv2(x)))
  74. x = self.pool(F.relu(self.conv3(x)))
  75. x = self.pool(F.relu(self.conv4(x)))
  76. x = self.pool(F.relu(self.conv5(x)))
  77. # flatten image input
  78. x = x.view(-1, 256*2*2)
  79.  
  80. # add dropout layer
  81. x = self.dropout(x)
  82. # add 1st hidden layer, with relu activation function
  83. x = F.relu(self.fc1(x))
  84. # add dropout layer
  85. x = self.dropout(x)
  86. # add 2nd hidden layer, with relu activation function
  87. x = self.fc2(x)
  88. x = self.dropout(x)
  89. x = self.fc3(x)
  90. return x
  91. train_on_gpu = torch.cuda.is_available()
  92.  
  93.  
  94. classes=['অ','আ','ই', 'ঈ', 'উ','ঊ','ঋ','এ','ঐ', 'ও' , 'ঔ','ক', 'খ', 'গ', 'ঘ', 'ঙ', 'চ', 'ছ', 'জ', 'ঝ', 'ঞ', 'ট',
  95. 'ঠ', 'ড', 'ঢ', 'ণ', 'ত', 'থ', 'দ', 'ধ', 'ন', 'প', 'ফ', 'ব', 'ভ', 'ম', 'য', 'র', 'ল', 'শ' , 'ষ', 'স' , 'হ' ,
  96. 'ড়','ঢ়','য়','ৎ','৹', ':', '৺']
  97.  
  98. model_scratch = Net()
  99. model_scratch.load_state_dict(torch.load('model_scratch.pt' , map_location=torch.device('cpu')))
  100.  
  101. def process_image(image):
  102. ''' Scales, crops, and normalizes a PIL image for a PyTorch model
  103.  
  104. '''
  105.  
  106. #img = Image.open(image)
  107. img=image
  108. img=cv2.merge([img,img,img])
  109. img = transforms.ToPILImage()(img)
  110. transformation = transforms.Compose([transforms.Resize([64,64]),
  111. #transforms.Grayscale(num_output_channels=1),
  112. transforms.ToTensor(),
  113. transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
  114. ])
  115. return transformation(img)
  116. def predict(image_path, model):
  117.  
  118. model.to('cpu')
  119. image = process_image(image_path)
  120. image = image.unsqueeze_(0)
  121.  
  122. model.eval()
  123. with torch.no_grad():
  124. output = model.forward(image)
  125.  
  126. #probabilities = torch.exp(output)
  127.  
  128. #topk_probabilities, topk_labels = probabilities.topk(topk)
  129. #return([topk_probabilities, topk_labels]
  130. # convert output probabilities to predicted class
  131. _, preds_tensor = torch.max(output, 1)
  132. preds = np.squeeze(preds_tensor.numpy()) if not train_on_gpu else np.squeeze(preds_tensor.cpu().numpy())
  133. return(classes[preds])
  134.  
  135.  
  136.  
  137.  
  138. img = cv2.imread(r"F:\Thesis Files\Bangla OCR Dataset\Dataset\Dataset\1\Words\1_2\1_2_2_1.JPG",cv2.IMREAD_GRAYSCALE)
  139. cv2.imshow('input',img)
  140. he,we=img.shape
  141. vec=[]
  142. vag=we/10
  143. vag=math.floor(vag)
  144. for i in range(0,10):
  145. cur=(i+1)*vag
  146. currImg = img[0:he, 0:cur]
  147. vec.append(currImg)
  148. for i in range(0,10):
  149. cv2.imshow('Crop %d' % i,vec[i])
  150. print(predict(vec[i],model_scratch))
  151.  
  152. cv2.waitKey(0)
Add Comment
Please, Sign In to add comment