-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_cifar10_processing.py .py
84 lines (67 loc) · 2.62 KB
/
example_cifar10_processing.py .py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Example LBP transform on Cifar10 dataset
# Akgun, Devrim. "A PyTorch Operations Based Approach for Computing Local Binary Patterns." U. Porto Journal of Engineering 7.4 (2021): 61-69.
https://journalengineering.fe.up.pt/index.php/upjeng/article/view/2183-6493_007-004_0005/567
"""
from lib.lbplib import lbp_py,lbp_pt
import numpy as np
import time
import matplotlib.pyplot as plt
import torch
import torchvision
#Compute LBP using Python operations
def py_extract_lbp_rgb(x_train):
[N,Rows,Cols,Channels]=x_train.shape
x_train_lbp=np.zeros(shape=(N,Rows,Cols,Channels),dtype='uint8')
for i in range(N):
x_train_lbp[i,:,:,0]=lbp_py(x_train[i,:,:,0])
x_train_lbp[i,:,:,1]=lbp_py(x_train[i,:,:,1])
x_train_lbp[i,:,:,2]=lbp_py(x_train[i,:,:,2])
return x_train_lbp
#Compute LBP using PyTorch operations
def torch_extract_lbp_rgb(x_train):
[N,Rows,Cols,Channels]=x_train.shape
x_train_lbp=np.zeros(shape=(N,Rows,Cols,Channels),dtype='uint8')
x_train_lbp[:,:,:,0]=lbp_pt(x_train[:,:,:,0]).numpy()
x_train_lbp[:,:,:,1]=lbp_pt(x_train[:,:,:,1]).numpy()
x_train_lbp[:,:,:,2]=lbp_pt(x_train[:,:,:,2]).numpy()
return x_train_lbp
#Use Cifar10 for the example
trainset = torchvision.datasets.CIFAR10(root='./data', download=True)
#use first 200 images for the test
x_train=trainset.data[:200,:,:,:]
# 1- Extract lbp features for the selected images using PyTorch ---------------
start_time = time.time()
# process all images using PyTorch
x_train_pt =torch.from_numpy(x_train)
x_train_lbp_pt = torch_extract_lbp_rgb(x_train_pt)
elapsed_pt = time.time() - start_time
print('PyTorch elapsed_time=',elapsed_pt)
# 2- Extract lbp features for the selected images using Python-----------------
start_time = time.time()
# process all images using Python
x_train_lbp_py = py_extract_lbp_rgb(x_train)
elapsed_py = time.time() - start_time
print('Python elapsed_time=',elapsed_py)
# Check error
print('error=',np.sum(x_train_lbp_py-x_train_lbp_pt))
# Example images---------------------------------------------------------------
# Input images
plt.figure(1)
figs, axes = plt.subplots(4, 6)
for i in range(4):
for j in range(6):
axes[i, j].imshow(x_train[i*6+j,:,:,:])
axes[i, j].set_xticks([])
axes[i, j].set_yticks([])
# LBP transformed images
plt.figure(2)
figs, axes = plt.subplots(4, 6)
for i in range(4):
for j in range(6):
axes[i, j].imshow(x_train_lbp_pt[i*6+j,:,:,:])
#axes[i, j].imshow(x_train_lbp_py[i*6+j,:,:,:])
axes[i, j].set_xticks([])
axes[i, j].set_yticks([])