[CAM]Original CAM

CAM
Author

SEOYEON CHOI

Published

August 28, 2023

https://seoyeonc.github.io/chch/cnn/feature%20extraction/big%20data%20analysis/2022/01/11/bd_9주차.html

https://seoyeonc.github.io/chch/cam/2022/01/10/bd-8주차_1.html

import

import torch 
from fastai.vision.all import *
import cv2
import numpy as np
import os
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
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
ImageFile.LOAD_TRUNCATED_IMAGES = True
from torchvision.utils import save_image
import os
import rpy2
import rpy2.robjects as ro 
from rpy2.robjects.vectors import FloatVector 
from rpy2.robjects.packages import importr
def label_func(f):
    if f[0].isupper():
        return 'cat' 
    else: 
        return 'dog' 

원본 CAM

# os.mkdir("original_pet")
for i in range(len(path.ls())) :
    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.7)
    # b = random.uniform(0, h*0.9)
    shape = [(a, b), (a+100, b+50)]
    # font = ImageFont.truetype("DejaVuSans.ttf", round(h*0.08))
    name = str(list(path.ls())[i]).split('/')[-1]
    fname = name.split('.')[-1]
    if name[0].isupper() == True :
        img1 = ImageDraw.Draw(img)  
        # img1.rectangle(shape, fill ="white", outline ="black")
        # ImageDraw.Draw(img).text((a, b), 'CAT', (0,0,0), font=font)
        img.save("original_pet/"+name)
    else: 
        img1 = ImageDraw.Draw(img)  
        # img1.rectangle(shape, fill ="black", outline ="black")
        # ImageDraw.Draw(img).text((a, b), 'DOG', (255,255,255), font=font)
        img.save("original_pet/"+name)
path_o=Path('original_pet')   #랜덤박스넣은사진
files_o=get_image_files(path_o)
dls_o=ImageDataLoaders.from_name_func(path_o,files_o,label_func,item_tfms=Resize(512)) 
lrnr_o1=cnn_learner(dls_o,resnet34,metrics=error_rate)
lrnr_o1.fine_tune(1)
net_o1=lrnr_o1.model[0]
net_o2=lrnr_o1.model[1] 
net_o2 = torch.nn.Sequential(
    torch.nn.AdaptiveAvgPool2d(output_size=1), 
    torch.nn.Flatten(),
    torch.nn.Linear(512,out_features=2,bias=False))
net_o=torch.nn.Sequential(net_o1,net_o2)
lrnr_o2=Learner(dls_o,net_o,metrics=accuracy) 
lrnr_o2.fine_tune(10) 
interp_o = ClassificationInterpretation.from_learner(lrnr_o2)
interp_o.plot_confusion_matrix()
x_o, = first(dls_o.test_dl([PILImage.create(get_image_files(path_o)[7389])]))
camimg_o = torch.einsum('ij,jkl -> ikl', net_o2[2].weight, net_o1(x).squeeze())
# 서연 수정 code
fig, (ax1,ax2) = plt.subplots(1,2) 
# 
dls_o.train.decode((x_o,))[0].squeeze().show(ax=ax1)
ax1.imshow(camimg_o[0].to("cpu").detach(),alpha=0.7,extent=(0,511,511,0),interpolation='bilinear',cmap='bone')
#
dls_r.train.decode((x_o,))[0].squeeze().show(ax=ax2)
ax2.imshow(camimg_o[1].to("cpu").detach(),alpha=0.7,extent=(0,511,511,0),interpolation='bilinear',cmap='bone')
fig.set_figwidth(8)            
fig.set_figheight(8)
fig.tight_layout()
fig, ax = plt.subplots(5,5) 
k=0 
for i in range(5):
    for j in range(5): 
        x_o, = first(dls_o.test_dl([PILImage.create(get_image_files(path_o)[k])]))
        camimg_o = torch.einsum('ij,jkl -> ikl', net_o2[2].weight, net_o1(x_o).squeeze())
        a_o,b_o = net_r(x_o).tolist()[0]
        catprob, dogprob = np.exp(a_o)/ (np.exp(a_o)+np.exp(b_o)) ,  np.exp(b_o)/ (np.exp(a_o)+np.exp(b_o)) 
        if catprob>dogprob: 
            dls_o.train.decode((x_o,))[0].squeeze().show(ax=ax[i][j])
            ax[i][j].imshow(camimg_o[0].to("cpu").detach(),alpha=0.7,extent=(0,512,512,0),interpolation='bilinear',cmap='bone')
            ax[i][j].set_title("cat(%s)" % catprob.round(5))
        else: 
            dls_o.train.decode((x_o,))[0].squeeze().show(ax=ax[i][j])
            ax[i][j].imshow(camimg_o[1].to("cpu").detach(),alpha=0.7,extent=(0,512,512,0),interpolation='bilinear',cmap='bone')
            ax[i][j].set_title("dog(%s)" % dogprob.round(5))
        k=k+1 
fig.set_figwidth(16)            
fig.set_figheight(16)
fig.tight_layout()
fig, ax = plt.subplots(5,5) 
k=0 
for i in range(5):
    for j in range(5): 
        x_o, = first(dls_o.test_dl([PILImage.create(get_image_files(path_o)[k])]))
        camimg_o = torch.einsum('ij,jkl -> ikl', net_o2[2].weight, net_o1(x).squeeze())
        a_o,b_o = net_o(x_o).tolist()[0]
        catprob, dogprob = np.exp(a_o)/ (np.exp(a_o)+np.exp(b_o)) ,  np.exp(b_o)/ (np.exp(a_o)+np.exp(b_o))
        if catprob>dogprob: 
            test=camimg_o[0]-torch.min(camimg_o[0])
            A1=torch.exp(-0.1*test)
            X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
            Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
            x1=x_o.squeeze().to('cpu')*Y1-torch.min(x_o.squeeze().to('cpu'))*Y1
            (x1*0.25).squeeze().show(ax=ax[i][j])
            ax[i][j].set_title("cat(%s)" % catprob.round(5))
        else: 
            test=camimg_o[1]-torch.min(camimg_o[1])
            A1=torch.exp(-0.1*test)
            X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
            Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
            x1=x_o.squeeze().to('cpu')*Y1-torch.min(x_o.squeeze().to('cpu'))*Y1
            (x1*0.25).squeeze().show(ax=ax[i][j])
            ax[i][j].set_title("dog(%s)" % dogprob.round(5))
        k=k+1 
fig.set_figwidth(16)            
fig.set_figheight(16)
fig.tight_layout()
  • .mat파일 있나 확인
for i in range(len(path_o.ls())) :
    img = PILImage.create(get_image_files(path_o)[i])
    img = img.resize([512,512], resample=None, box=None, reducing_gap=None)
    name = str(list(path_o.ls())[i]).split('/')[-1]
    fname = name.split('.')[-1]
    if fname!="jpg" : 
        print(name)
    else : pass
x_o, = first(dls_o.test_dl([PILImage.create(get_image_files(path_o)[1])]))
camimg_o = torch.einsum('ij,jkl -> ikl', net_o2[2].weight, net_o1(x).squeeze())
a_o,b_o = net_o(x_o).tolist()[0]
catprob_o, dogprob_o = np.exp(a_o)/ (np.exp(a_o)+np.exp(b_o)) ,  np.exp(b_o)/ (np.exp(a_o)+np.exp(b_o))
if catprob_o>dogprob_o: 
    test_o=camimg_o[0]-torch.min(camimg_o[0])
    A1_o=torch.exp(-0.01*test_o)
    X1_o=np.array(A1_o.to("cpu").detach(),dtype=np.float32)
    Y1_o=torch.Tensor(cv2.resize(X1_o,(512,512),interpolation=cv2.INTER_LINEAR))
    x1_o=x_o.squeeze().to('cpu')*Y1_o-torch.min(x_o.squeeze().to('cpu'))*Y1_o
    (x1_o*0.25).squeeze().show()
else: 
        test_o=camimg_o[1]-torch.min(camimg_o[1])
        A1_o=torch.exp(-0.01*test_o)
        X1_o=np.array(A1_o.to("cpu").detach(),dtype=np.float32)
        Y1_o=torch.Tensor(cv2.resize(X1_o,(512,512),interpolation=cv2.INTER_LINEAR))
        x1_o=x_o.squeeze().to('cpu')*Y1-torch.min(x_o.squeeze().to('cpu'))*Y1_o
        (x1_o*0.25).squeeze().show()
# #저장 참고
# np_arr = np.array(tensor, dtype=np.uint8)
# img = PIL.Image.fromarray(np_arr)
# img.save('path')
# name = str(list(path.ls())[1]).split('/')[-1]
# res1=(x1*0.35).squeeze()
# res1.show()
# save_image(res1, "pet3_mode1_res/"+name)
#res1.save("pet3_mode1_res/"+name)