from pytorch_grad_cam import GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad, EigenGradCAM, LayerCAM
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
https://docs.fast.ai/data.external.html
Import
import torch
from fastai.vision.all import *
from fastai.vision import *
import cv2
import numpy as np
import os
'CUDA_LAUNCH_BLOCKING'] = "1"
os.environ["CUDA_VISIBLE_DEVICES"] = "0" os.environ[
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import ImageDraw
from PIL import ImageFont
from PIL import ImageFile
from PIL import Image
= True ImageFile.LOAD_TRUNCATED_IMAGES
from torchvision import *
import os
import rpy2
import rpy2.robjects as ro
from rpy2.robjects.vectors import FloatVector
from rpy2.robjects.packages import importr
Data
= untar_data(URLs.CALTECH_101);path path
Path('/home/csy/.fastai/data/caltech_101')
path.ls()
(#102) [Path('/home/csy/.fastai/data/caltech_101/gerenuk'),Path('/home/csy/.fastai/data/caltech_101/ceiling_fan'),Path('/home/csy/.fastai/data/caltech_101/electric_guitar'),Path('/home/csy/.fastai/data/caltech_101/schooner'),Path('/home/csy/.fastai/data/caltech_101/sunflower'),Path('/home/csy/.fastai/data/caltech_101/brain'),Path('/home/csy/.fastai/data/caltech_101/dolphin'),Path('/home/csy/.fastai/data/caltech_101/nautilus'),Path('/home/csy/.fastai/data/caltech_101/panda'),Path('/home/csy/.fastai/data/caltech_101/chair')...]
= ImageDataLoaders.from_folder(path, valid_pct=0.2, seed=42, item_tfms=Resize(512),
dls =[*aug_transforms(), Normalize.from_stats(*imagenet_stats)]) batch_tfms
dls.show_batch()
=cnn_learner(dls,resnet34,metrics=error_rate)
lrnr1) lrnr.fine_tune(
/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/fastai/vision/learner.py:288: UserWarning: `cnn_learner` has been renamed to `vision_learner` -- please update your code
warn("`cnn_learner` has been renamed to `vision_learner` -- please update your code")
/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
warnings.warn(
/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet34_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet34_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
epoch | train_loss | valid_loss | error_rate | time |
---|---|---|---|---|
0 | 2.786667 | 1.086679 | 0.280635 | 00:46 |
epoch | train_loss | valid_loss | error_rate | time |
---|---|---|---|---|
0 | 0.953599 | 0.546696 | 0.141685 | 00:59 |
=lrnr.model[0]
net1=lrnr.model[1] net2
net2
Sequential(
(0): AdaptiveConcatPool2d(
(ap): AdaptiveAvgPool2d(output_size=1)
(mp): AdaptiveMaxPool2d(output_size=1)
)
(1): fastai.layers.Flatten(full=False)
(2): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): Dropout(p=0.25, inplace=False)
(4): Linear(in_features=1024, out_features=512, bias=False)
(5): ReLU(inplace=True)
(6): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(7): Dropout(p=0.5, inplace=False)
(8): Linear(in_features=512, out_features=102, bias=False)
)
= torch.nn.Sequential(
net2 =1),
torch.nn.AdaptiveAvgPool2d(output_size
torch.nn.Flatten(),512,out_features=102,bias=False)) torch.nn.Linear(
=torch.nn.Sequential(net1,net2) net
=Learner(dls,net,metrics=accuracy) lrnr2
5) lrnr2.fine_tune(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 2.200760 | 3.150540 | 0.371444 | 00:59 |
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 1.145681 | 1.464551 | 0.653173 | 00:59 |
1 | 1.119536 | 1.774413 | 0.589716 | 00:58 |
2 | 0.751984 | 0.828347 | 0.771335 | 00:59 |
3 | 0.415494 | 0.496882 | 0.862144 | 00:59 |
4 | 0.232705 | 0.418902 | 0.882385 | 00:59 |
Step
= first(dls.test_dl([PILImage.create(get_image_files(path)[20])]))
x, 0].squeeze().show() dls.train.decode((x,))[
= x.to('cpu') x
= torch.einsum('ij,jkl -> ikl', net2[2].weight, net1(x).squeeze()) camimg
= importr('EbayesThresh').ebayesthresh
ebayesthresh
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg[0].detach().reshape(-1))**2)))
power_threshed= np.where(power_threshed>80,torch.tensor(camimg[0].detach().reshape(-1)).cpu(),0)
ybar_threshed = torch.tensor(ybar_threshed.reshape(16,16))
ybar_threshed
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg[1].detach().reshape(-1))**2)))
power_threshed2= np.where(power_threshed2>80,torch.tensor(camimg[1].detach().reshape(-1)).cpu(),0)
ybar_threshed2 = torch.tensor(ybar_threshed2.reshape(16,16)) ybar_threshed2
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed=np.array(ebayesthresh(FloatVector(torch.tensor(camimg[0].detach().reshape(-1))**2)))
<ipython-input-870-11d406c6ed63>:4: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed = np.where(power_threshed>80,torch.tensor(camimg[0].detach().reshape(-1)).cpu(),0)
<ipython-input-870-11d406c6ed63>:7: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed2=np.array(ebayesthresh(FloatVector(torch.tensor(camimg[1].detach().reshape(-1))**2)))
<ipython-input-870-11d406c6ed63>:8: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed2 = np.where(power_threshed2>80,torch.tensor(camimg[1].detach().reshape(-1)).cpu(),0)
= plt.subplots(1,3)
fig, (ax1,ax2,ax3) #
0].squeeze().show(ax=ax1)
dls.train.decode((x,))["Input image")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))["cpu").detach(),alpha=0.5,extent=(0,48,48,0),interpolation='bilinear',cmap='cool')
ax2.imshow((ybar_threshed).to(# ax2.set_title("CAT PART")
#
0].squeeze().show(ax=ax3)
dls.train.decode((x,))["cpu").detach(),alpha=0.5,extent=(0,48,48,0),interpolation='bilinear',cmap='cool')
ax3.imshow((ybar_threshed2).to(# ax3.set_title("DOG PART")
#
12)
fig.set_figwidth(12)
fig.set_figheight( fig.tight_layout()
- 판단 근거가 강할 수록 파란색 -> 보라색
mode 1
# test=camimg_o[0]-torch.min(camimg_o[0])
=torch.exp(-0.05*(ybar_threshed))
A1= 1 - A1 A2
= plt.subplots(1,2)
fig, (ax1,ax2) #
0].squeeze().show(ax=ax1)
dls.train.decode((x,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(A2.data.to("MODE1 WEIGHTT")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(A1.data.to("MODE1 RES WEIGHT")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
# mode 1 res
=np.array(A1.to("cpu").detach(),dtype=np.float32)
X1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
Y1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu'))*Y1
x1
# mode 1
=np.array(A2.to("cpu").detach(),dtype=np.float32)
X12=torch.Tensor(cv2.resize(X12,(512,512),interpolation=cv2.INTER_LINEAR))
Y12=x.squeeze().to('cpu')*Y12-torch.min(x.squeeze().to('cpu'))*Y12 x12
-
1st CAM 분리
= plt.subplots(1,1)
fig, (ax1) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))["ORIGINAL")
ax1.set_title(4)
fig.set_figwidth(4)
fig.set_figheight(
fig.tight_layout()#
= plt.subplots(1,2)
fig, (ax1, ax2) -x12*0.3).squeeze().show(ax=ax1) #MODE1
(*0.1).squeeze().show(ax=ax2) #MODE1_res
(x1"MODE1")
ax1.set_title("MODE1 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
= x1.reshape(1,3,512,512) x1
'cpu')
net1.to('cpu') net2.to(
Sequential(
(0): AdaptiveAvgPool2d(output_size=1)
(1): Flatten(start_dim=1, end_dim=-1)
(2): Linear(in_features=512, out_features=102, bias=False)
)
= torch.einsum('ij,jkl -> ikl', net2[2].weight, net1(x1).squeeze()) camimg1
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg1[0].detach().reshape(-1))**2)))
power_threshed3= np.where(power_threshed3>10,torch.tensor(camimg1[0].detach().reshape(-1)),0)
ybar_threshed3 = torch.tensor(ybar_threshed3.reshape(16,16))
ybar_threshed3
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg1[1].detach().reshape(-1))**2)))
power_threshed4= np.where(power_threshed4>10,torch.tensor(camimg1[1].detach().reshape(-1)),0)
ybar_threshed4 = torch.tensor(ybar_threshed4.reshape(16,16)) ybar_threshed4
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed3=np.array(ebayesthresh(FloatVector(torch.tensor(camimg1[0].detach().reshape(-1))**2)))
<ipython-input-900-292f842a7fbc>:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed3 = np.where(power_threshed3>10,torch.tensor(camimg1[0].detach().reshape(-1)),0)
<ipython-input-900-292f842a7fbc>:5: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed4=np.array(ebayesthresh(FloatVector(torch.tensor(camimg1[1].detach().reshape(-1))**2)))
<ipython-input-900-292f842a7fbc>:6: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed4 = np.where(power_threshed4>10,torch.tensor(camimg1[1].detach().reshape(-1)),0)
-
mode1 res
= plt.subplots(1,2)
fig, (ax1,ax2) #
*0.25).squeeze().show(ax=ax1)
(x1=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed3,alpha"CAT PART")
ax1.set_title(#
*0.25).squeeze().show(ax=ax2)
(x1=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed4,alpha"DOG PART")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
-
첫번째 CAM 결과와 비교
= plt.subplots(1,2)
fig, (ax1,ax2) #
0].squeeze().show(ax=ax1)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed,alpha"1ST CAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed3,alpha"2ND CAM")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
-
2nd CAM 분리
# test1=camimg1[0]-torch.min(camimg1[0])
= torch.exp(-0.05*(ybar_threshed3))
A3 = 1 - A3 A4
= plt.subplots(1,2)
fig, (ax1,ax2) #
=ax2)
x1.squeeze().show(ax0].squeeze().show(ax=ax1)
dls.train.decode((x1,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(A3.data.to("MODE2 RES WEIGHT")
ax1.set_title(#
=ax2)
x1.squeeze().show(ax0].squeeze().show(ax=ax2)
dls.train.decode((x1,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(A4.data.to("MODE2 WEIGHT")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
=np.array(A3.to("cpu").detach(),dtype=np.float32)
X2=torch.Tensor(cv2.resize(X2,(512,512),interpolation=cv2.INTER_LINEAR))
Y2=(x1)*Y2-torch.min((x1)*Y2)
x2
=np.array(A4.to("cpu").detach(),dtype=np.float32)
X22=torch.Tensor(cv2.resize(X22,(512,512),interpolation=cv2.INTER_LINEAR))
Y22=(x1)*Y22-torch.min((x1)*Y22) x22
= plt.subplots(1,1)
fig, (ax1) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))["ORIGINAL")
ax1.set_title(4)
fig.set_figwidth(4)
fig.set_figheight(
fig.tight_layout()#
= plt.subplots(1,2)
fig, (ax1, ax2) -x12*0.3).squeeze().show(ax=ax1) #MODE1
(*0.1).squeeze().show(ax=ax2) #MODE1_res
(x1"MODE1")
ax1.set_title("MODE1 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()#
= plt.subplots(1,2)
fig, (ax1, ax2) -x22+1.7).squeeze().show(ax=ax1) #MODE2
(*0.1).squeeze().show(ax=ax2) #MODE2_res
(x2"MODE2")
ax1.set_title("MODE2 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
= x2.reshape(1,3,512,512) x2
'cpu')
net1.to('cpu') net2.to(
Sequential(
(0): AdaptiveAvgPool2d(output_size=1)
(1): Flatten(start_dim=1, end_dim=-1)
(2): Linear(in_features=512, out_features=102, bias=False)
)
= torch.einsum('ij,jkl -> ikl', net2[2].weight, net1(x2).squeeze()) camimg2
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg2[0].detach().reshape(-1))**2)))
power_threshed5= np.where(power_threshed5>40,torch.tensor(camimg2[0].detach().reshape(-1)),0)
ybar_threshed5 = torch.tensor(ybar_threshed5.reshape(16,16))
ybar_threshed5
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg2[1].detach().reshape(-1))**2)))
power_threshed6= np.where(power_threshed6>40,torch.tensor(camimg2[1].detach().reshape(-1)),0)
ybar_threshed6 = torch.tensor(ybar_threshed6.reshape(16,16)) ybar_threshed6
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed5=np.array(ebayesthresh(FloatVector(torch.tensor(camimg2[0].detach().reshape(-1))**2)))
<ipython-input-919-ba6e482cf5c4>:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed5 = np.where(power_threshed5>40,torch.tensor(camimg2[0].detach().reshape(-1)),0)
<ipython-input-919-ba6e482cf5c4>:5: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed6=np.array(ebayesthresh(FloatVector(torch.tensor(camimg2[1].detach().reshape(-1))**2)))
<ipython-input-919-ba6e482cf5c4>:6: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed6 = np.where(power_threshed6>40,torch.tensor(camimg2[1].detach().reshape(-1)),0)
-
mode2 res 에 CAM 결과 올리기
= plt.subplots(1,2)
fig, (ax1, ax2) #
=ax1)
x2.squeeze().show(ax=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed5,alpha"CAT PART")
ax1.set_title(#
=ax2)
x2.squeeze().show(ax=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed6,alpha"DOG PART")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
= plt.subplots(1,3)
fig, (ax1,ax2,ax3) #
0].squeeze().show(ax=ax1)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed,alpha"1ST CAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed3,alpha"2ND CAM")
ax2.set_title(#
0].squeeze().show(ax=ax3)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax3.imshow(ybar_threshed5,alpha"3RD CAM")
ax3.set_title(12)
fig.set_figwidth(12)
fig.set_figheight( fig.tight_layout()
mode 3 만들기
# test2=camimg2[0]-torch.min(camimg2[0])
= torch.exp(-0.05*(ybar_threshed5)) A5
= 1 - A5 A6
= plt.subplots(1,2)
fig, (ax1, ax2) #
=ax1)
x2.squeeze().show(ax=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed5,alpha"CAT PART")
ax1.set_title(#
=ax2)
x2.squeeze().show(ax=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed6,alpha"DOG PART")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
#mode 3 res
=np.array(A5.to("cpu").detach(),dtype=np.float32)
X3=torch.Tensor(cv2.resize(X3,(512,512),interpolation=cv2.INTER_LINEAR))
Y3=x2*Y3-torch.min(x2*Y3)
x3# mode 3
=np.array(A6.to("cpu").detach(),dtype=np.float32)
X32=torch.Tensor(cv2.resize(X32,(512,512),interpolation=cv2.INTER_LINEAR))
Y32=x2*Y32-torch.min(x2*Y32) x32
import pickle
= plt.subplots(1,1)
fig, (ax1) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))["ORIGINAL")
ax1.set_title(4)
fig.set_figwidth(4)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_hcam_cat_plt1.pkl', 'wb') as file:
file)
pickle.dump(fig, #
= plt.subplots(1,2)
fig, (ax1, ax2) -x12*0.3).squeeze().show(ax=ax1) #MODE1
(*0.1).squeeze().show(ax=ax2) #MODE1_res
(x1"MODE1")
ax1.set_title("MODE1 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()with open('CALTECH_101_hcam_cat_plt2.pkl', 'wb') as file:
file)
pickle.dump(fig,
#
= plt.subplots(1,2)
fig, (ax1, ax2) -x22+1.7).squeeze().show(ax=ax1) #MODE2
(*0.1).squeeze().show(ax=ax2) #MODE2_res
(x2"MODE2")
ax1.set_title("MODE2 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()with open('CALTECH_101_hcam_cat_plt3.pkl', 'wb') as file:
file)
pickle.dump(fig,
#
= plt.subplots(1,2)
fig, (ax1, ax2) -x32+2).squeeze().show(ax=ax1) #MODE3
(*0.1).squeeze().show(ax=ax2) #MODE3_res
(x3"MODE3")
ax1.set_title("MODE3 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_hcam_cat_plt4.pkl', 'wb') as file:
file) pickle.dump(fig,
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
with open('CALTECH_101_hcam_cat_plt1.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt plt.show(hcam_cat_plt)
with open('CALTECH_101_hcam_cat_plt2.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt plt.show(hcam_cat_plt)
with open('CALTECH_101_hcam_cat_plt3.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt plt.show(hcam_cat_plt)
with open('CALTECH_101_hcam_cat_plt4.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt plt.show(hcam_cat_plt)
= plt.subplots(1,1)
fig, (ax1) *-0.3).squeeze().show(ax=ax1) #MODE1
(x12"MODE1")
ax1.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_hcam_cat_plt_mode1.pkl', 'wb') as file:
file) pickle.dump(fig,
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
with open('CALTECH_101_hcam_cat_plt_mode1.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt_mode1 plt.show(hcam_cat_plt_mode1)
= plt.subplots(1,1)
fig, (ax1) -x12*0.3 + -x22+1.7).squeeze().show(ax=ax1) #MODE1+MODE2
("MODE1+MODE2")
ax1.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_hcam_cat_plt_mode2.pkl', 'wb') as file:
file) pickle.dump(fig,
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
with open('CALTECH_101_hcam_cat_plt_mode2.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt_mode2 plt.show(hcam_cat_plt_mode2)
= plt.subplots(1,1)
fig, (ax1) *-0.3 + -x22+1.7 + -x32+2).squeeze().show(ax=ax1) #MODE1+MODE2+MODE3
(x12"MODE1+MODE2+MODE3")
ax1.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_hcam_cat_plt_mode3.pkl', 'wb') as file:
file) pickle.dump(fig,
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
with open('CALTECH_101_hcam_cat_plt_mode3.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt_mode3 plt.show(hcam_cat_plt_mode3)
Other Methods
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = GradCAM(model=model, target_layers=target_layer) gradcam
/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
warnings.warn(
/home/csy/anaconda3/envs/temp_csy/lib/python3.8/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet34_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet34_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = HiResCAM(model=model, target_layers=target_layer) hirescam
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = ScoreCAM(model=model, target_layers=target_layer) scorecam
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = GradCAMPlusPlus(model=model, target_layers=target_layer) gradcamplusplus
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = AblationCAM(model=model, target_layers=target_layer) ablationcam
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = XGradCAM(model=model, target_layers=target_layer) xgradcam
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = EigenCAM(model=model, target_layers=target_layer) eigencam
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = FullGrad(model=model, target_layers=target_layer) fullgrad
Warning: target_layers is ignored in FullGrad. All bias layers will be used instead
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = EigenGradCAM(model=model, target_layers=target_layer) eigengradcam
= resnet34(pretrained=True)
model
= [model.layer4[-1]]
target_layer = LayerCAM(model=model, target_layers=target_layer) layercam
시도
= first(dls.test_dl([PILImage.create(get_image_files(path)[20])]))
x, = x.to('cpu')
x 0].squeeze().show() dls.train.decode((x,))[
= gradcam(input_tensor=x,targets=None) cam_gradcam
= hirescam(input_tensor=x,targets=None) cam_hirescam
= scorecam(input_tensor=x,targets=None) cam_scorecam
100%|██████████| 32/32 [00:23<00:00, 1.38it/s]
= gradcamplusplus(input_tensor=x,targets=None) cam_gradcamplusplus
= ablationcam(input_tensor=x,targets=None) cam_ablationcam
100%|██████████| 16/16 [00:26<00:00, 1.63s/it]
= xgradcam(input_tensor=x,targets=None) cam_xgradcam
= eigencam(input_tensor=x,targets=None) cam_eigencam
= fullgrad(input_tensor=x,targets=None) cam_fullgrad
= eigengradcam(input_tensor=x,targets=None) cam_eigengradcam
= layercam(input_tensor=x,targets=None) cam_layercam
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_gradcam.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART GradCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_gradcam.squeeze(),alpha"DISEASE PART GradCAM")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_hirescam.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART HiResCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_hirescam.squeeze(),alpha"DISEASE PART HiResCAM")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_scorecam.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART ScoreCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_scorecam.squeeze(),alpha"DISEASE PART ScoreCAM")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_gradcamplusplus.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART GradCAMPlusPlus")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_gradcamplusplus.squeeze(),alpha"DISEASE PART GradCAMPlusPlus")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_ablationcam.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART AblationCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_ablationcam.squeeze(),alpha"DISEASE PART AblationCAM")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_xgradcam.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART XGradCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_xgradcam.squeeze(),alpha"DISEASE PART XGradCAM")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_eigencam.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART EigenCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_eigencam.squeeze(),alpha"DISEASE PART EigenCAM")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_fullgrad.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART FullGrad")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_fullgrad.squeeze(),alpha"DISEASE PART FullGrad")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= plt.subplots(1,2)
fig, (ax1,ax2) 0].squeeze().show(ax=ax1)
dls.train.decode((x,))[-cam_layercam.squeeze(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow("NORMAL PART LayerCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(cam_layercam.squeeze(),alpha"DISEASE PART LayerCAM")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
fig, ((ax1,ax2,ax3,ax4),
(ax5,ax6,ax7,ax8),= plt.subplots(3,4)
(ax9,ax10,ax11,ax12)) 'CALTECH 101 Dataset')
plt.title(#
0].squeeze().show(ax=ax1)
dls.train.decode((x,))[=0.7)
ax1.imshow(cam_gradcam.squeeze(), alpha"GradCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x,))[=0.7)
ax2.imshow(cam_hirescam.squeeze(), alpha"HiResCAM")
ax2.set_title(#
0].squeeze().show(ax=ax3)
dls.train.decode((x,))[=0.7)
ax3.imshow(cam_scorecam.squeeze(), alpha"ScoreCAM")
ax3.set_title(#
0].squeeze().show(ax=ax4)
dls.train.decode((x,))[=0.7)
ax4.imshow(cam_gradcamplusplus.squeeze(), alpha"GradCAMPlusPlus")
ax4.set_title(#
0].squeeze().show(ax=ax5)
dls.train.decode((x,))[=0.7)
ax5.imshow(cam_ablationcam.squeeze(), alpha"AblationCAM")
ax5.set_title(#
0].squeeze().show(ax=ax6)
dls.train.decode((x,))[=0.7)
ax6.imshow(cam_xgradcam.squeeze(), alpha"XGradCAM")
ax6.set_title(#
0].squeeze().show(ax=ax7)
dls.train.decode((x,))[=0.7)
ax7.imshow(cam_eigencam.squeeze(), alpha"EigenCAM")
ax7.set_title(#
0].squeeze().show(ax=ax8)
dls.train.decode((x,))[=0.7)
ax8.imshow(cam_fullgrad.squeeze(), alpha"FullGrad")
ax8.set_title(#
0].squeeze().show(ax=ax9)
dls.train.decode((x,))[=0.7)
ax9.imshow(cam_eigengradcam.squeeze(), alpha"EigenGradCAM")
ax9.set_title(#
0].squeeze().show(ax=ax10)
dls.train.decode((x,))[=0.7)
ax10.imshow(cam_layercam.squeeze(), alpha"LayerCAM")
ax10.set_title(
0].squeeze()*0).show(ax=ax11)
(dls.train.decode((x,))[0].squeeze()*0).show(ax=ax12)
(dls.train.decode((x,))[#
20)
fig.set_figwidth(20)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_fig_randombox_plt.pkl', 'wb') as file:
file) pickle.dump(fig,
with open('CALTECH_101_fig_randombox_plt.pkl', 'rb') as file:
= pickle.load(file)
fig_randombox_plt fig_randombox_plt.show()
randombox Image download
list(path.ls())
[Path('/home/csy/.fastai/data/caltech_101/gerenuk'),
Path('/home/csy/.fastai/data/caltech_101/ceiling_fan'),
Path('/home/csy/.fastai/data/caltech_101/electric_guitar'),
Path('/home/csy/.fastai/data/caltech_101/schooner'),
Path('/home/csy/.fastai/data/caltech_101/sunflower'),
Path('/home/csy/.fastai/data/caltech_101/brain'),
Path('/home/csy/.fastai/data/caltech_101/dolphin'),
Path('/home/csy/.fastai/data/caltech_101/nautilus'),
Path('/home/csy/.fastai/data/caltech_101/panda'),
Path('/home/csy/.fastai/data/caltech_101/chair'),
Path('/home/csy/.fastai/data/caltech_101/trilobite'),
Path('/home/csy/.fastai/data/caltech_101/stapler'),
Path('/home/csy/.fastai/data/caltech_101/beaver'),
Path('/home/csy/.fastai/data/caltech_101/snoopy'),
Path('/home/csy/.fastai/data/caltech_101/windsor_chair'),
Path('/home/csy/.fastai/data/caltech_101/ant'),
Path('/home/csy/.fastai/data/caltech_101/cougar_face'),
Path('/home/csy/.fastai/data/caltech_101/octopus'),
Path('/home/csy/.fastai/data/caltech_101/menorah'),
Path('/home/csy/.fastai/data/caltech_101/inline_skate'),
Path('/home/csy/.fastai/data/caltech_101/dollar_bill'),
Path('/home/csy/.fastai/data/caltech_101/chandelier'),
Path('/home/csy/.fastai/data/caltech_101/tick'),
Path('/home/csy/.fastai/data/caltech_101/garfield'),
Path('/home/csy/.fastai/data/caltech_101/crab'),
Path('/home/csy/.fastai/data/caltech_101/Faces'),
Path('/home/csy/.fastai/data/caltech_101/flamingo_head'),
Path('/home/csy/.fastai/data/caltech_101/car_side'),
Path('/home/csy/.fastai/data/caltech_101/saxophone'),
Path('/home/csy/.fastai/data/caltech_101/crocodile_head'),
Path('/home/csy/.fastai/data/caltech_101/cup'),
Path('/home/csy/.fastai/data/caltech_101/water_lilly'),
Path('/home/csy/.fastai/data/caltech_101/starfish'),
Path('/home/csy/.fastai/data/caltech_101/Motorbikes'),
Path('/home/csy/.fastai/data/caltech_101/dragonfly'),
Path('/home/csy/.fastai/data/caltech_101/barrel'),
Path('/home/csy/.fastai/data/caltech_101/laptop'),
Path('/home/csy/.fastai/data/caltech_101/flamingo'),
Path('/home/csy/.fastai/data/caltech_101/wild_cat'),
Path('/home/csy/.fastai/data/caltech_101/scorpion'),
Path('/home/csy/.fastai/data/caltech_101/helicopter'),
Path('/home/csy/.fastai/data/caltech_101/emu'),
Path('/home/csy/.fastai/data/caltech_101/pyramid'),
Path('/home/csy/.fastai/data/caltech_101/BACKGROUND_Google'),
Path('/home/csy/.fastai/data/caltech_101/rooster'),
Path('/home/csy/.fastai/data/caltech_101/binocular'),
Path('/home/csy/.fastai/data/caltech_101/llama'),
Path('/home/csy/.fastai/data/caltech_101/pizza'),
Path('/home/csy/.fastai/data/caltech_101/wrench'),
Path('/home/csy/.fastai/data/caltech_101/cellphone'),
Path('/home/csy/.fastai/data/caltech_101/strawberry'),
Path('/home/csy/.fastai/data/caltech_101/scissors'),
Path('/home/csy/.fastai/data/caltech_101/crayfish'),
Path('/home/csy/.fastai/data/caltech_101/soccer_ball'),
Path('/home/csy/.fastai/data/caltech_101/wheelchair'),
Path('/home/csy/.fastai/data/caltech_101/grand_piano'),
Path('/home/csy/.fastai/data/caltech_101/sea_horse'),
Path('/home/csy/.fastai/data/caltech_101/lobster'),
Path('/home/csy/.fastai/data/caltech_101/crocodile'),
Path('/home/csy/.fastai/data/caltech_101/pigeon'),
Path('/home/csy/.fastai/data/caltech_101/yin_yang'),
Path('/home/csy/.fastai/data/caltech_101/umbrella'),
Path('/home/csy/.fastai/data/caltech_101/lamp'),
Path('/home/csy/.fastai/data/caltech_101/airplanes'),
Path('/home/csy/.fastai/data/caltech_101/stop_sign'),
Path('/home/csy/.fastai/data/caltech_101/kangaroo'),
Path('/home/csy/.fastai/data/caltech_101/Faces_easy'),
Path('/home/csy/.fastai/data/caltech_101/lotus'),
Path('/home/csy/.fastai/data/caltech_101/accordion'),
Path('/home/csy/.fastai/data/caltech_101/cannon'),
Path('/home/csy/.fastai/data/caltech_101/watch'),
Path('/home/csy/.fastai/data/caltech_101/mayfly'),
Path('/home/csy/.fastai/data/caltech_101/bass'),
Path('/home/csy/.fastai/data/caltech_101/stegosaurus'),
Path('/home/csy/.fastai/data/caltech_101/brontosaurus'),
Path('/home/csy/.fastai/data/caltech_101/euphonium'),
Path('/home/csy/.fastai/data/caltech_101/elephant'),
Path('/home/csy/.fastai/data/caltech_101/gramophone'),
Path('/home/csy/.fastai/data/caltech_101/butterfly'),
Path('/home/csy/.fastai/data/caltech_101/buddha'),
Path('/home/csy/.fastai/data/caltech_101/okapi'),
Path('/home/csy/.fastai/data/caltech_101/Leopards'),
Path('/home/csy/.fastai/data/caltech_101/ibis'),
Path('/home/csy/.fastai/data/caltech_101/ewer'),
Path('/home/csy/.fastai/data/caltech_101/pagoda'),
Path('/home/csy/.fastai/data/caltech_101/anchor'),
Path('/home/csy/.fastai/data/caltech_101/camera'),
Path('/home/csy/.fastai/data/caltech_101/dalmatian'),
Path('/home/csy/.fastai/data/caltech_101/metronome'),
Path('/home/csy/.fastai/data/caltech_101/ferry'),
Path('/home/csy/.fastai/data/caltech_101/headphone'),
Path('/home/csy/.fastai/data/caltech_101/cougar_body'),
Path('/home/csy/.fastai/data/caltech_101/hawksbill'),
Path('/home/csy/.fastai/data/caltech_101/minaret'),
Path('/home/csy/.fastai/data/caltech_101/mandolin'),
Path('/home/csy/.fastai/data/caltech_101/bonsai'),
Path('/home/csy/.fastai/data/caltech_101/rhino'),
Path('/home/csy/.fastai/data/caltech_101/ketch'),
Path('/home/csy/.fastai/data/caltech_101/revolver'),
Path('/home/csy/.fastai/data/caltech_101/joshua_tree'),
Path('/home/csy/.fastai/data/caltech_101/platypus'),
Path('/home/csy/.fastai/data/caltech_101/hedgehog')]
len(list(path.ls()))
5394
= PILImage.create(get_image_files(path)[20]) temp_img
= temp_img.resize([512,512], resample=None, box=None, reducing_gap=None) temp_img
= (temp_img.shape[0], temp_img.shape[1]) (temp_w, temp_h)
= random.uniform(0, temp_w*0.8)
temp_a = random.uniform(0, temp_h*0.3) temp_b
= [(temp_a, temp_b), (temp_a+200, temp_b+60)]
temp_shape = ImageFont.truetype("DejaVuSans.ttf", round(temp_h*0.04))
temp_font = (str(list(path.ls())[20])+'-'+str(20)).split('/')[-1] + '.jpg'
temp_name = temp_name.split('.')[0].split('-')[0] temp_fname
temp_name
'dollar_bill-20.jpg'
temp_fname
'dollar_bill'
= ImageDraw.Draw(temp_img)
temp_img1 ="white", outline ="black")
temp_img1.rectangle(temp_shape, fill +5, temp_b+15), temp_fname, (0,0,0), font=temp_font)
ImageDraw.Draw(temp_img).text((temp_a
plt.imshow(temp_img)"caltech_101_random/"+temp_name) temp_img.save(
9143] get_image_files(path)[
Path('/home/csy/.fastai/data/caltech_101/hedgehog/image_0017.jpg')
len(path.ls())
102
# for i in range(len(str(list(path.ls())))-1) :
# img = PILImage.create(get_image_files(path)[i])
# img = img.resize([512,512], resample=None, box=None, reducing_gap=None)
# (w, h) = (img.shape[0], img.shape[1])
# a = random.uniform(0, w*0.8)
# b = random.uniform(0, h*0.3)
# shape = [(a, b), (a+200, b+60)]
# font = ImageFont.truetype("DejaVuSans.ttf", round(h*0.04))
# name = (os.path.dirname(get_image_files(path)[i]).split('/')[-1]+'-'+str(i)).split('/')[-1] + '.jpg'
# fname = name.split('.')[0].split('-')[0]
# img1 = ImageDraw.Draw(img)
# img1.rectangle(shape, fill ="white", outline ="black")
# ImageDraw.Draw(img).text((a+5, b+15), fname, (0,0,0), font=font)
# img.save("caltech_101_random/"+name)
randombox
=Path('caltech_101_random') #랜덤박스넣은사진 path_r
path_r.ls()
(#5394) [Path('caltech_101_random/Motorbikes-2934.jpg'),Path('caltech_101_random/beaver-782.jpg'),Path('caltech_101_random/Motorbikes-2975.jpg'),Path('caltech_101_random/beaver-796.jpg'),Path('caltech_101_random/BACKGROUND_Google-3983.jpg'),Path('caltech_101_random/Motorbikes-2599.jpg'),Path('caltech_101_random/laptop-3308.jpg'),Path('caltech_101_random/airplanes-5324.jpg'),Path('caltech_101_random/airplanes-5329.jpg'),Path('caltech_101_random/flamingo_head-1941.jpg')...]
=get_image_files(path_r) files_r
def label_func(f):
return f[0].split('-')[0]
=ImageDataLoaders.from_name_func(path_r,files,label_func,item_tfms=Resize(512)) dls_r
(2) 학습
=cnn_learner(dls_r,resnet34,metrics=error_rate)
lrnr_r1) lrnr_r.fine_tune(
epoch | train_loss | valid_loss | error_rate | time |
---|---|---|---|---|
0 | 2.194400 | 0.814425 | 0.234694 | 00:30 |
epoch | train_loss | valid_loss | error_rate | time |
---|---|---|---|---|
0 | 0.633236 | 0.386290 | 0.114100 | 00:36 |
=lrnr_r.model[0]
net1_r=lrnr_r.model[1] net2_r
net2_r
Sequential(
(0): AdaptiveConcatPool2d(
(ap): AdaptiveAvgPool2d(output_size=1)
(mp): AdaptiveMaxPool2d(output_size=1)
)
(1): fastai.layers.Flatten(full=False)
(2): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): Dropout(p=0.25, inplace=False)
(4): Linear(in_features=1024, out_features=512, bias=False)
(5): ReLU(inplace=True)
(6): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(7): Dropout(p=0.5, inplace=False)
(8): Linear(in_features=512, out_features=23, bias=False)
)
= torch.nn.Sequential(
net2_r =1),
torch.nn.AdaptiveAvgPool2d(output_size
torch.nn.Flatten(),512,out_features=23,bias=False)) torch.nn.Linear(
=torch.nn.Sequential(net1_r,net2_r) net_r
=Learner(dls_r,net_r,metrics=accuracy) lrnr2_r
5) lrnr2_r.fine_tune(
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.990693 | 7.214967 | 0.175325 | 00:36 |
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.191813 | 0.626072 | 0.789425 | 00:36 |
1 | 0.138365 | 0.371003 | 0.892393 | 00:36 |
2 | 0.059181 | 0.026200 | 0.996289 | 00:36 |
3 | 0.019740 | 0.011075 | 0.997217 | 00:36 |
4 | 0.006568 | 0.010148 | 0.997217 | 00:36 |
Step by step
= first(dls.test_dl([PILImage.create(get_image_files(path_r)[532])]))
x_r, 0].squeeze().show() dls.train.decode((x_r,))[
= torch.einsum('ij,jkl -> ikl', net2_r[2].weight, net1_r(x_r).squeeze()) camimg_r
= importr('EbayesThresh').ebayesthresh
ebayesthresh
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg_r[0].detach().reshape(-1))**2)))
power_threshed= np.where(power_threshed>20,torch.tensor(camimg_r[0].detach().reshape(-1)).cpu(),0)
ybar_threshed = torch.tensor(ybar_threshed.reshape(16,16))
ybar_threshed
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg_r[1].detach().reshape(-1))**2)))
power_threshed2= np.where(power_threshed2>20,torch.tensor(camimg_r[1].detach().reshape(-1)).cpu(),0)
ybar_threshed2 = torch.tensor(ybar_threshed2.reshape(16,16)) ybar_threshed2
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed=np.array(ebayesthresh(FloatVector(torch.tensor(camimg_r[0].detach().reshape(-1))**2)))
<ipython-input-695-1fb834e23ce9>:4: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed = np.where(power_threshed>20,torch.tensor(camimg_r[0].detach().reshape(-1)).cpu(),0)
<ipython-input-695-1fb834e23ce9>:7: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed2=np.array(ebayesthresh(FloatVector(torch.tensor(camimg_r[1].detach().reshape(-1))**2)))
<ipython-input-695-1fb834e23ce9>:8: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed2 = np.where(power_threshed2>20,torch.tensor(camimg_r[1].detach().reshape(-1)).cpu(),0)
= plt.subplots(1,3)
fig, (ax1,ax2,ax3) #
0].squeeze().show(ax=ax1)
dls.train.decode((x_r,))["Input image")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x_r,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow((ybar_threshed).to(# ax2.set_title("CAT PART")
#
0].squeeze().show(ax=ax3)
dls.train.decode((x_r,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax3.imshow((ybar_threshed2).to(# ax3.set_title("DOG PART")
#
12)
fig.set_figwidth(12)
fig.set_figheight( fig.tight_layout()
- 판단 근거가 강할 수록 파란색 -> 보라색
mode 1
# test=camimg_o[0]-torch.min(camimg_o[0])
=torch.exp(-0.05*(ybar_threshed))
A1= 1 - A1 A2
= plt.subplots(1,2)
fig, (ax1,ax2) #
0].squeeze().show(ax=ax1)
dls.train.decode((x_r,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(A2.data.to("MODE1 WEIGHTT")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x_r,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(A1.data.to("MODE1 RES WEIGHT")
ax2.set_title(#
8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
# mode 1 res
=np.array(A1.to("cpu").detach(),dtype=np.float32)
X1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
Y1=x_r.squeeze().to('cpu')*Y1-torch.min(x_r.squeeze().to('cpu'))*Y1
x1
# mode 1
=np.array(A2.to("cpu").detach(),dtype=np.float32)
X12=torch.Tensor(cv2.resize(X12,(512,512),interpolation=cv2.INTER_LINEAR))
Y12=x_r.squeeze().to('cpu')*Y12-torch.min(x_r.squeeze().to('cpu'))*Y12 x12
-
1st CAM 분리
= plt.subplots(1,1)
fig, (ax1) 0].squeeze().show(ax=ax1)
dls.train.decode((x_r,))["ORIGINAL")
ax1.set_title(4)
fig.set_figwidth(4)
fig.set_figheight(
fig.tight_layout()#
= plt.subplots(1,2)
fig, (ax1, ax2) *-1.5).squeeze().show(ax=ax1) #MODE1
(x12*0.5).squeeze().show(ax=ax2) #MODE1_res
(x1"MODE1")
ax1.set_title("MODE1 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
= x1.reshape(1,3,512,512) x1
'cpu')
net1_r.to('cpu') net2_r.to(
Sequential(
(0): AdaptiveAvgPool2d(output_size=1)
(1): Flatten(start_dim=1, end_dim=-1)
(2): Linear(in_features=512, out_features=23, bias=False)
)
= torch.einsum('ij,jkl -> ikl', net2_r[2].weight, net1_r(x1).squeeze()) camimg1_r
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg1_r[0].detach().reshape(-1))**2)))
power_threshed3= np.where(power_threshed3>20,torch.tensor(camimg1_r[0].detach().reshape(-1)),0)
ybar_threshed3 = torch.tensor(ybar_threshed3.reshape(16,16))
ybar_threshed3
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg1_r[1].detach().reshape(-1))**2)))
power_threshed4= np.where(power_threshed4>20,torch.tensor(camimg1_r[1].detach().reshape(-1)),0)
ybar_threshed4 = torch.tensor(ybar_threshed4.reshape(16,16)) ybar_threshed4
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed3=np.array(ebayesthresh(FloatVector(torch.tensor(camimg1_r[0].detach().reshape(-1))**2)))
<ipython-input-724-c83bfb4d70dd>:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed3 = np.where(power_threshed3>20,torch.tensor(camimg1_r[0].detach().reshape(-1)),0)
<ipython-input-724-c83bfb4d70dd>:5: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed4=np.array(ebayesthresh(FloatVector(torch.tensor(camimg1_r[1].detach().reshape(-1))**2)))
<ipython-input-724-c83bfb4d70dd>:6: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed4 = np.where(power_threshed4>20,torch.tensor(camimg1_r[1].detach().reshape(-1)),0)
-
mode1 res
= plt.subplots(1,2)
fig, (ax1,ax2) #
*0.25).squeeze().show(ax=ax1)
(x1=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed3,alpha"CAT PART")
ax1.set_title(#
*0.25).squeeze().show(ax=ax2)
(x1=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed4,alpha"DOG PART")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
-
첫번째 CAM 결과와 비교
= plt.subplots(1,2)
fig, (ax1,ax2) #
0].squeeze().show(ax=ax1)
dls.train.decode((x_r,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed,alpha"1ST CAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x_r,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed3,alpha"2ND CAM")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
-
2nd CAM 분리
# test1=camimg1[0]-torch.min(camimg1[0])
= torch.exp(-0.05*(ybar_threshed3))
A3 = 1 - A3 A4
= plt.subplots(1,2)
fig, (ax1,ax2) #
=ax2)
x1.squeeze().show(ax0].squeeze().show(ax=ax1)
dls.train.decode((x1,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(A3.data.to("MODE2 RES WEIGHT")
ax1.set_title(#
=ax2)
x1.squeeze().show(ax0].squeeze().show(ax=ax2)
dls.train.decode((x1,))["cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(A4.data.to("MODE2 WEIGHT")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
=np.array(A3.to("cpu").detach(),dtype=np.float32)
X2=torch.Tensor(cv2.resize(X2,(512,512),interpolation=cv2.INTER_LINEAR))
Y2=(x1)*Y2-torch.min((x1)*Y2)
x2
=np.array(A4.to("cpu").detach(),dtype=np.float32)
X22=torch.Tensor(cv2.resize(X22,(512,512),interpolation=cv2.INTER_LINEAR))
Y22=(x1)*Y22-torch.min((x1)*Y22) x22
= plt.subplots(1,1)
fig, (ax1) 0].squeeze().show(ax=ax1)
dls.train.decode((x_r,))["ORIGINAL")
ax1.set_title(4)
fig.set_figwidth(4)
fig.set_figheight(
fig.tight_layout()#
= plt.subplots(1,2)
fig, (ax1, ax2) *-1.5).squeeze().show(ax=ax1) #MODE1
(x12*0.5).squeeze().show(ax=ax2) #MODE1_res
(x1"MODE1")
ax1.set_title("MODE1 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()#
= plt.subplots(1,2)
fig, (ax1, ax2) -x22+1).squeeze().show(ax=ax1) #MODE2
(*0.5).squeeze().show(ax=ax2) #MODE2_res
(x2"MODE2")
ax1.set_title("MODE2 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
= x2.reshape(1,3,512,512) x2
'cpu')
net1_r.to('cpu') net2_r.to(
Sequential(
(0): AdaptiveAvgPool2d(output_size=1)
(1): Flatten(start_dim=1, end_dim=-1)
(2): Linear(in_features=512, out_features=23, bias=False)
)
= torch.einsum('ij,jkl -> ikl', net2_r[2].weight, net1_r(x2).squeeze()) camimg2_r
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg2_r[0].detach().reshape(-1))**2)))
power_threshed5= np.where(power_threshed5>10,torch.tensor(camimg2_r[0].detach().reshape(-1)),0)
ybar_threshed5 = torch.tensor(ybar_threshed5.reshape(16,16))
ybar_threshed5
=np.array(ebayesthresh(FloatVector(torch.tensor(camimg2_r[1].detach().reshape(-1))**2)))
power_threshed6= np.where(power_threshed6>10,torch.tensor(camimg2_r[1].detach().reshape(-1)),0)
ybar_threshed6 = torch.tensor(ybar_threshed6.reshape(16,16)) ybar_threshed6
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed5=np.array(ebayesthresh(FloatVector(torch.tensor(camimg2_r[0].detach().reshape(-1))**2)))
<ipython-input-746-f3a70d6c47ba>:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed5 = np.where(power_threshed5>10,torch.tensor(camimg2_r[0].detach().reshape(-1)),0)
<ipython-input-746-f3a70d6c47ba>:5: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
power_threshed6=np.array(ebayesthresh(FloatVector(torch.tensor(camimg2_r[1].detach().reshape(-1))**2)))
<ipython-input-746-f3a70d6c47ba>:6: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
ybar_threshed6 = np.where(power_threshed6>10,torch.tensor(camimg2_r[1].detach().reshape(-1)),0)
-
mode2 res 에 CAM 결과 올리기
= plt.subplots(1,2)
fig, (ax1, ax2) #
=ax1)
x2.squeeze().show(ax=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed5,alpha"CAT PART")
ax1.set_title(#
=ax2)
x2.squeeze().show(ax=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed6,alpha"DOG PART")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
= plt.subplots(1,3)
fig, (ax1,ax2,ax3) #
0].squeeze().show(ax=ax1)
dls.train.decode((x_r,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed,alpha"1ST CAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x_r,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed3,alpha"2ND CAM")
ax2.set_title(#
0].squeeze().show(ax=ax3)
dls.train.decode((x_r,))[=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax3.imshow(ybar_threshed5,alpha"3RD CAM")
ax3.set_title(12)
fig.set_figwidth(12)
fig.set_figheight( fig.tight_layout()
mode 3 만들기
# test2=camimg2[0]-torch.min(camimg2[0])
= torch.exp(-0.05*(ybar_threshed5)) A5
= 1 - A5 A6
= plt.subplots(1,2)
fig, (ax1, ax2) #
=ax1)
x2.squeeze().show(ax=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax1.imshow(ybar_threshed5,alpha"CAT PART")
ax1.set_title(#
=ax2)
x2.squeeze().show(ax=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.imshow(ybar_threshed6,alpha"DOG PART")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight( fig.tight_layout()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
#mode 3 res
=np.array(A5.to("cpu").detach(),dtype=np.float32)
X3=torch.Tensor(cv2.resize(X3,(512,512),interpolation=cv2.INTER_LINEAR))
Y3=x2*Y3-torch.min(x2*Y3)
x3# mode 3
=np.array(A6.to("cpu").detach(),dtype=np.float32)
X32=torch.Tensor(cv2.resize(X32,(512,512),interpolation=cv2.INTER_LINEAR))
Y32=x2*Y32-torch.min(x2*Y32) x32
= plt.subplots(1,1)
fig, (ax1) 0].squeeze().show(ax=ax1)
dls.train.decode((x_r,))["ORIGINAL")
ax1.set_title(4)
fig.set_figwidth(4)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_random_hcam_cat_plt1.pkl', 'wb') as file:
file)
pickle.dump(fig, #
= plt.subplots(1,2)
fig, (ax1, ax2) *-1.5).squeeze().show(ax=ax1) #MODE1
(x12*0.5).squeeze().show(ax=ax2) #MODE1_res
(x1"MODE1")
ax1.set_title("MODE1 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()with open('CALTECH_101_random_hcam_cat_plt2.pkl', 'wb') as file:
file)
pickle.dump(fig,
#
= plt.subplots(1,2)
fig, (ax1, ax2) -x22+1).squeeze().show(ax=ax1) #MODE2
(*0.5).squeeze().show(ax=ax2) #MODE2_res
(x2"MODE2")
ax1.set_title("MODE2 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()with open('CALTECH_101_random_hcam_cat_plt3.pkl', 'wb') as file:
file)
pickle.dump(fig,
#
= plt.subplots(1,2)
fig, (ax1, ax2) -x32+1).squeeze().show(ax=ax1) #MODE3
(*0.5).squeeze().show(ax=ax2) #MODE3_res
(x3"MODE3")
ax1.set_title("MODE3 RES")
ax2.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_random_hcam_cat_plt4.pkl', 'wb') as file:
file) pickle.dump(fig,
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
with open('CALTECH_101_random_hcam_cat_plt1.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt plt.show(hcam_cat_plt)
with open('CALTECH_101_random_hcam_cat_plt2.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt plt.show(hcam_cat_plt)
with open('CALTECH_101_random_hcam_cat_plt3.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt plt.show(hcam_cat_plt)
with open('CALTECH_101_random_hcam_cat_plt4.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt plt.show(hcam_cat_plt)
= plt.subplots(1,1)
fig, (ax1) *-1.5).squeeze().show(ax=ax1) #MODE1
(x12"MODE1")
ax1.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_random_hcam_cat_plt_mode1.pkl', 'wb') as file:
file) pickle.dump(fig,
with open('CALTECH_101_random_hcam_cat_plt_mode1.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt_mode1 plt.show(hcam_cat_plt_mode1)
= plt.subplots(1,1)
fig, (ax1) *-1.5 + -x22+1).squeeze().show(ax=ax1) #MODE1+MODE2
(x12"MODE1+MODE2")
ax1.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_random_hcam_cat_plt_mode2.pkl', 'wb') as file:
file) pickle.dump(fig,
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
with open('CALTECH_101_random_hcam_cat_plt_mode2.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt_mode2 plt.show(hcam_cat_plt_mode2)
= plt.subplots(1,1)
fig, (ax1) *-1.5 + -x22+1 + -x32+1).squeeze().show(ax=ax1) #MODE1+MODE2+MODE3
(x12"MODE1+MODE2+MODE3")
ax1.set_title(8)
fig.set_figwidth(8)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_random_hcam_cat_plt_mode3.pkl', 'wb') as file:
file) pickle.dump(fig,
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
with open('CALTECH_101_random_hcam_cat_plt_mode3.pkl', 'rb') as file:
= pickle.load(file)
hcam_cat_plt_mode3 plt.show(hcam_cat_plt_mode3)
Randombox Other Methods
= x_r.to('cpu') x_r
GradCAM
= GradCAM(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) gradcam_randombox
= gradcam_randombox(input_tensor=x_r,targets=None) cam_gradcam_randombox
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_gradcam_randombox.squeeze(), alpha
HiResCAM
= HiResCAM(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) hirescam_randombox
= hirescam_randombox(input_tensor=x_r,targets=None) cam_hirescam_randombox
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_hirescam_randombox.squeeze(), alpha
ScoreCAM
= ScoreCAM(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) scorecam_randombox
= scorecam_randombox(input_tensor=x_r,targets=None) cam_scorecam_randombox
100%|██████████| 32/32 [00:24<00:00, 1.30it/s]
100%|██████████| 32/32 [00:24<00:00, 1.30it/s]
100%|██████████| 32/32 [00:24<00:00, 1.31it/s]
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_scorecam_randombox.squeeze(), alpha
GradCAMPlusPlus
= GradCAMPlusPlus(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) gradcamplusplus_randombox
= gradcamplusplus_randombox(input_tensor=x_r,targets=None) cam_gradcamplusplus_randombox
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_gradcamplusplus_randombox.squeeze(), alpha
AblationCAM
= AblationCAM(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) ablationcam_randombox
= ablationcam_randombox(input_tensor=x_r,targets=None) cam_ablationcam_randombox
100%|██████████| 16/16 [00:26<00:00, 1.64s/it]
100%|██████████| 16/16 [00:25<00:00, 1.58s/it]
100%|██████████| 16/16 [00:25<00:00, 1.57s/it]
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_ablationcam_randombox.squeeze(), alpha
XGradCAM
= XGradCAM(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) xgradcam_randombox
= xgradcam_randombox(input_tensor=x_r,targets=None) cam_xgradcam_randombox
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_xgradcam_randombox.squeeze(), alpha
EigenCAM
= EigenCAM(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) eigencam_randombox
= eigencam_randombox(input_tensor=x_r,targets=None) cam_eigencam_randombox
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_eigencam_randombox.squeeze(), alpha
FullGrad
= FullGrad(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) fullgrad_randombox
Warning: target_layers is ignored in FullGrad. All bias layers will be used instead
= fullgrad_randombox(input_tensor=x_r,targets=None) cam_fullgrad_randombox
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_fullgrad_randombox.squeeze(), alpha
EigenGradCAM
= EigenGradCAM(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) eigengradcam_randombox
= eigengradcam_randombox(input_tensor=x_r,targets=None) cam_eigengradcam_randombox
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_eigengradcam_randombox.squeeze(), alpha
LayerCAM
= LayerCAM(model=lrnr_r.model.to('cpu'), target_layers=lrnr_r.model[0][-1]) layercam_randombox
= layercam_randombox(input_tensor=x_r,targets=None) cam_layercam_randombox
0].squeeze().show()
dls.train.decode((x_r,))[=0.7) plt.imshow(cam_layercam_randombox.squeeze(), alpha
Figure_Randombox
fig, ((ax1,ax2,ax3,ax4),
(ax5,ax6,ax7,ax8),= plt.subplots(3,4)
(ax9,ax10,ax11,ax12)) 'Randombox')
plt.title(#
0].squeeze().show(ax=ax1)
dls.train.decode((x_r,))[=0.7)
ax1.imshow(cam_gradcam_randombox.squeeze(), alpha"GradCAM")
ax1.set_title(#
0].squeeze().show(ax=ax2)
dls.train.decode((x_r,))[=0.7)
ax2.imshow(cam_hirescam_randombox.squeeze(), alpha"HiResCAM")
ax2.set_title(#
0].squeeze().show(ax=ax3)
dls.train.decode((x_r,))[=0.7)
ax3.imshow(cam_scorecam_randombox.squeeze(), alpha"ScoreCAM")
ax3.set_title(#
0].squeeze().show(ax=ax4)
dls.train.decode((x_r,))[=0.7)
ax4.imshow(cam_gradcamplusplus_randombox.squeeze(), alpha"GradCAMPlusPlus")
ax4.set_title(#
0].squeeze().show(ax=ax5)
dls.train.decode((x_r,))[=0.7)
ax5.imshow(cam_ablationcam_randombox.squeeze(), alpha"AblationCAM")
ax5.set_title(#
0].squeeze().show(ax=ax6)
dls.train.decode((x_r,))[=0.7)
ax6.imshow(cam_xgradcam_randombox.squeeze(), alpha"XGradCAM")
ax6.set_title(#
0].squeeze().show(ax=ax7)
dls.train.decode((x_r,))[=0.7)
ax7.imshow(cam_eigencam_randombox.squeeze(), alpha"EigenCAM")
ax7.set_title(#
0].squeeze().show(ax=ax8)
dls.train.decode((x_r,))[=0.7)
ax8.imshow(cam_fullgrad_randombox.squeeze(), alpha"FullGrad")
ax8.set_title(#
0].squeeze().show(ax=ax9)
dls.train.decode((x_r,))[=0.7)
ax9.imshow(cam_eigengradcam_randombox.squeeze(), alpha"EigenGradCAM")
ax9.set_title(#
0].squeeze().show(ax=ax10)
dls.train.decode((x_r,))[=0.7)
ax10.imshow(cam_layercam_randombox.squeeze(), alpha"LayerCAM")
ax10.set_title(#
0].squeeze()*0).show(ax=ax11)
(dls.train.decode((x_r,))[0].squeeze()*0).show(ax=ax12)
(dls.train.decode((x_r,))[#
20)
fig.set_figwidth(20)
fig.set_figheight(
fig.tight_layout()
with open('CALTECH_101_random_fig_randombox_plt.pkl', 'wb') as file:
file) pickle.dump(fig,
with open('CALTECH_101_random_fig_randombox_plt.pkl', 'rb') as file:
= pickle.load(file)
fig_randombox_plt fig_randombox_plt.show()