-
Notifications
You must be signed in to change notification settings - Fork 2
/
Visualisation.py
147 lines (121 loc) · 3.83 KB
/
Visualisation.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 8 22:10:18 2020
---------------------------------------------------------------------
-- Author: Vigneashwara Pandiyan
---------------------------------------------------------------------
Utils file for visualization/ Plots
"""
#%%
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation
import matplotlib.pyplot as plt
from matplotlib.pyplot import specgram
import seaborn as sns
from scipy.stats import norm
import joypy
import pandas as pd
from matplotlib import cm
from scipy import signal
import pywt
import matplotlib.patches as mpatches
import os
from PIL import Image
import torchvision.transforms as transforms
import torchvision
import torch
from torchsummary import summary
from torchvision import datasets
#%%
datadir = 'Bronze dataset/'
traindir = datadir + 'Train/'
testdir = datadir + 'Test/'
#%%
data_transform = transforms.Compose([
torchvision.transforms.Resize((224,224)),
transforms.RandomVerticalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
trainset = datasets.ImageFolder(root=traindir,
transform=data_transform)
trainloader = torch.utils.data.DataLoader(trainset,
batch_size=4, shuffle=True,
num_workers=0)
testset = datasets.ImageFolder(root=testdir,
transform=data_transform)
testloader = torch.utils.data.DataLoader(testset,
batch_size=4, shuffle=False,
num_workers=0)
#%%
classes = ('Balling', 'Keyhole', 'LoF', 'Nopores')
#%%
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
img=np.transpose(npimg, (1, 2, 0))
return img
#%%
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
# show images
fig, ax = plt.subplots(figsize=(12,5))
img=imshow(torchvision.utils.make_grid(images))
img=np.flip(img, 0)
plt.imshow(img)
plt.savefig('Class Images.png',bbox_inches='tight',dpi=800)
plt.show()
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
#%%
categories = []
img_categories = []
n_train = []
n_valid = []
n_test = []
hs = []
ws = []
# Iterate through each category
for d in os.listdir(traindir):
categories.append(d)
# Number of each image
train_imgs = os.listdir(traindir + d)
#valid_imgs = os.listdir(validdir + d)
test_imgs = os.listdir(testdir + d)
n_train.append(len(train_imgs))
# n_valid.append(len(valid_imgs))
n_test.append(len(test_imgs))
# Find stats for train images
for i in train_imgs:
img_categories.append(d)
img = Image.open(traindir + d + '/' + i)
img_array = np.array(img)
# Shape
hs.append(img_array.shape[0])
ws.append(img_array.shape[1])
# Dataframe of categories
cat_df = pd.DataFrame({'category': categories,
'n_train': n_train,
'n_test': n_test}).\
sort_values('category')
# Dataframe of training images
image_df = pd.DataFrame({
'category': img_categories,
'height': hs,
'width': ws
})
cat_df.sort_values('n_train', ascending=False, inplace=True)
cat_df.head()
cat_df.tail()
fig, ax = plt.subplots(figsize=(12,5))
cat_df.set_index('category')['n_train'].plot.bar(
color=plt.cm.Paired(np.arange(len(cat_df))))
plt.xticks(rotation=25,fontsize= 20)
plt.ylabel('Total count',fontsize= 20)
plt.title('Training Images by Category',fontsize= 20)
plt.savefig('Training Images.png',bbox_inches='tight',dpi=800)
img_dsc = image_df.groupby('category').describe()
img_dsc.head()