How can I speed up processing of thresholding using TensorFlow or PyTorch?
Question:
I’m new to Python and am in the learning phase of image processing.
Since I need to deal with large datasets which is basically only on thresholding, TensorFlow or PyTorch may be more suitable through the usage of a GPU. Are there some related examples? What should I do?
Here’s my current approach using OpenCV.
import numpy as np
import cv2
import os
folderDir = "C://Users/ruler/Desktop/testseg/"
total = []
with open('readme.txt', 'w') as f:
count = 0
for allImages in os.listdir(folderDir):
if (allImages.startswith('TRAIN_SET') and allImages.endswith(".bmp")):
img = cv2.imread(os.path.join(folderDir, allImages))
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_,blackMask = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV)
_, blackgreyMask = cv2.threshold(gry, 128, 255, cv2.THRESH_BINARY_INV)
whiteMask = cv2.bitwise_not(blackgreyMask)
greyMask = cv2.bitwise_xor(blackMask, blackgreyMask)
x1 = []
y1 = []
def verticle(mask, y, x):
vertiPixel = 0
while(y < mask.shape[0]):
if (y + 1) == mask.shape[0]:
break
else:
if(mask[y + 1][x] == 255):
vertiPixel += 1
y += 1
else:
break
y1.append(vertiPixel)
return y1
def horizontal(mask, y, x):
horiPixel = 0
while(x < mask.shape[1]):
if (x + 1) == mask.shape[1]:
break
else:
if(mask[y][x + 1] == 255):
horiPixel += 1
x += 1
else:
break
x1.append(horiPixel)
return x1
def mask(mask):
for y in range (mask.shape[0]):
for x in range (mask.shape[1]):
if(mask[y][x] == 255):
verticle(mask, y, x)
horizontal(mask, y, x)
mask(blackMask)
print(np.average(y1), np.average(x1))
I tried to look for TensorFlow or PyTorch related for thresholding, but I did not really find it.
Expectation
I implemented thresholding API through TensorFlow or PyTorch to speed up image thresholding process.
Answers:
You need to understand you can implement ratios on devices for simple tasks or multiple ratios propagation, but for calculation, you can also use the TensorFlow and Keras models.
The convolution layer is what you are doing by multiple of the input image with smaller or sizes matrixes and added strides effects that can resize and remarks images from 256 x 256 to 32 x 32 with significant colors and relative position. Anyway, using program logic or implementing ratios propagation is nothing false.
Sample: Model implementation a mixing of Keras layers and function propagations.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 32, 32, 4 )),
tf.keras.layers.Normalization(mean=3., variance=2.),
tf.keras.layers.Normalization(mean=4., variance=6.),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Reshape((128, 225)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(192, activation='relu'),
tf.keras.layers.Dense(10),
])
Output: It is easy to create image recognitions using the TensorFlow Keras model.
I’m new to Python and am in the learning phase of image processing.
Since I need to deal with large datasets which is basically only on thresholding, TensorFlow or PyTorch may be more suitable through the usage of a GPU. Are there some related examples? What should I do?
Here’s my current approach using OpenCV.
import numpy as np
import cv2
import os
folderDir = "C://Users/ruler/Desktop/testseg/"
total = []
with open('readme.txt', 'w') as f:
count = 0
for allImages in os.listdir(folderDir):
if (allImages.startswith('TRAIN_SET') and allImages.endswith(".bmp")):
img = cv2.imread(os.path.join(folderDir, allImages))
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_,blackMask = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY_INV)
_, blackgreyMask = cv2.threshold(gry, 128, 255, cv2.THRESH_BINARY_INV)
whiteMask = cv2.bitwise_not(blackgreyMask)
greyMask = cv2.bitwise_xor(blackMask, blackgreyMask)
x1 = []
y1 = []
def verticle(mask, y, x):
vertiPixel = 0
while(y < mask.shape[0]):
if (y + 1) == mask.shape[0]:
break
else:
if(mask[y + 1][x] == 255):
vertiPixel += 1
y += 1
else:
break
y1.append(vertiPixel)
return y1
def horizontal(mask, y, x):
horiPixel = 0
while(x < mask.shape[1]):
if (x + 1) == mask.shape[1]:
break
else:
if(mask[y][x + 1] == 255):
horiPixel += 1
x += 1
else:
break
x1.append(horiPixel)
return x1
def mask(mask):
for y in range (mask.shape[0]):
for x in range (mask.shape[1]):
if(mask[y][x] == 255):
verticle(mask, y, x)
horizontal(mask, y, x)
mask(blackMask)
print(np.average(y1), np.average(x1))
I tried to look for TensorFlow or PyTorch related for thresholding, but I did not really find it.
Expectation
I implemented thresholding API through TensorFlow or PyTorch to speed up image thresholding process.
You need to understand you can implement ratios on devices for simple tasks or multiple ratios propagation, but for calculation, you can also use the TensorFlow and Keras models.
The convolution layer is what you are doing by multiple of the input image with smaller or sizes matrixes and added strides effects that can resize and remarks images from 256 x 256 to 32 x 32 with significant colors and relative position. Anyway, using program logic or implementing ratios propagation is nothing false.
Sample: Model implementation a mixing of Keras layers and function propagations.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 32, 32, 4 )),
tf.keras.layers.Normalization(mean=3., variance=2.),
tf.keras.layers.Normalization(mean=4., variance=6.),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Reshape((128, 225)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96, return_sequences=True, return_state=False)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(96)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(192, activation='relu'),
tf.keras.layers.Dense(10),
])
Output: It is easy to create image recognitions using the TensorFlow Keras model.