Skip to content

Commit 27860e9

Browse files
authored
Add files via upload
1 parent 806f064 commit 27860e9

7 files changed

+428
-22
lines changed

Example LBP transform on Cifar10 dataset.ipynb

Lines changed: 244 additions & 0 deletions
Large diffs are not rendered by default.

Example LBP transform using PyTorch.ipynb

Lines changed: 158 additions & 0 deletions
Large diffs are not rendered by default.

example_cifar10_processing.py .py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
"""
4-
Example lbp application on Cifar10
5-
4+
Example LBP transform on Cifar10 dataset
5+
# Akgun, Devrim. "A PyTorch Operations Based Approach for Computing Local Binary Patterns." U. Porto Journal of Engineering 7.4 (2021): 61-69.
66
https://journalengineering.fe.up.pt/index.php/upjeng/article/view/2183-6493_007-004_0005/567
77
"""
88
from lib.lbplib import lbp_py,lbp_pt
@@ -12,6 +12,7 @@
1212
import torch
1313
import torchvision
1414

15+
#Compute LBP using Python operations
1516
def py_extract_lbp_rgb(x_train):
1617
[N,Rows,Cols,Channels]=x_train.shape
1718
x_train_lbp=np.zeros(shape=(N,Rows,Cols,Channels),dtype='uint8')
@@ -22,7 +23,7 @@ def py_extract_lbp_rgb(x_train):
2223
return x_train_lbp
2324

2425

25-
26+
#Compute LBP using PyTorch operations
2627
def torch_extract_lbp_rgb(x_train):
2728
[N,Rows,Cols,Channels]=x_train.shape
2829
x_train_lbp=np.zeros(shape=(N,Rows,Cols,Channels),dtype='uint8')
@@ -31,31 +32,27 @@ def torch_extract_lbp_rgb(x_train):
3132
x_train_lbp[:,:,:,2]=lbp_pt(x_train[:,:,:,2]).numpy()
3233
return x_train_lbp
3334

35+
#Use Cifar10 for the example
36+
trainset = torchvision.datasets.CIFAR10(root='./data', download=True)
3437

35-
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
36-
download=True)
3738
#use first 200 images for the test
3839
x_train=trainset.data[:200,:,:,:]
3940

40-
# Process complete dataset
41-
42-
# Extract lbp features for Cifar10 dataset using PyTorch
41+
# 1- Extract lbp features for the selected images using PyTorch ---------------
4342
start_time = time.time()
4443

45-
#process all images in the Cifar10 --------------------------------------------
46-
47-
44+
# process all images using PyTorch
4845
x_train_pt =torch.from_numpy(x_train)
4946

5047
x_train_lbp_pt = torch_extract_lbp_rgb(x_train_pt)
5148

5249
elapsed_pt = time.time() - start_time
5350
print('PyTorch elapsed_time=',elapsed_pt)
5451

55-
# Extract lbp features for Cifar10 dataset ------------------------------------
52+
# 2- Extract lbp features for the selected images using Python-----------------
5653
start_time = time.time()
5754

58-
#process all images in the Cifar10
55+
# process all images using Python
5956
x_train_lbp_py = py_extract_lbp_rgb(x_train)
6057

6158
elapsed_py = time.time() - start_time
@@ -67,7 +64,7 @@ def torch_extract_lbp_rgb(x_train):
6764

6865

6966
# Example images---------------------------------------------------------------
70-
#input images
67+
# Input images
7168
plt.figure(1)
7269
figs, axes = plt.subplots(4, 6)
7370
for i in range(4):
@@ -76,7 +73,7 @@ def torch_extract_lbp_rgb(x_train):
7673
axes[i, j].set_xticks([])
7774
axes[i, j].set_yticks([])
7875

79-
# LBP images
76+
# LBP transformed images
8077
plt.figure(2)
8178
figs, axes = plt.subplots(4, 6)
8279
for i in range(4):

example_image_rgb.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
Test file for rgb and grascale outputs
66
https://journalengineering.fe.up.pt/index.php/upjeng/article/view/2183-6493_007-004_0005/567
77
"""
8-
from lib.lbplib import lbp_py,lbp_pt
8+
from lib.lbplib import lbp_pt
99
from PIL import Image
1010
import matplotlib.pyplot as plt
11-
import time
1211
import numpy as np
1312
import torch
1413

example_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@
4444

4545
print('python:')
4646
print(ypy)
47-
print('TensorFlow:')
47+
print('PyTorch:')
4848
print(ypt[0,:,:])

pytorch_lbp.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#import time
99
#os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
1010
print("torch.cuda.is_available():",torch.cuda.is_available())
11-
#------------------------------------------------------------------------------
11+
12+
13+
#- Python implementation of LBP transform -------------------------------------
1214
def lbp_python(Im):
1315

1416
sat=len(Im)
@@ -29,10 +31,10 @@ def lbp_python(Im):
2931
( I[i-1,j-1]>= I[i,j] )*128;
3032

3133
return L
32-
#------------------------------------------------------------------------------
3334

3435

35-
def tc_lbp(x):
36+
#- PyTorch implementation of LBP transform ------------------------------------
37+
def lbp_torch(x):
3638
#pad image for 3x3 mask size
3739
x = F.pad(input=x, pad = [1, 1, 1, 1], mode='constant')
3840
b=x.shape
@@ -91,7 +93,7 @@ def tc_lbp(x):
9193
print(imgs)
9294

9395
# Compute using pytorch
94-
y1=tc_lbp(torch.from_numpy(imgs.reshape(1,7,7)))
96+
y1=lbp_torch(torch.from_numpy(imgs.reshape(1,7,7)))
9597
# Compute using python
9698
y2=lbp_python(imgs)
9799

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
matplotlib==3.6.0
2+
numpy==1.23.3
3+
Pillow==9.2.0
4+
Pillow==10.0.1
5+
torch==1.12.0+cu113
6+
torchvision==0.13.0+cu113

0 commit comments

Comments
 (0)