<jupyter_start><jupyter_text>import images<jupyter_code>img1=io.imread('./Notebook/size_1.jpg')
img2=io.imread('./Notebook/size_2.jpg')
# img3=io.imread('./Notebook/size_3.jpg')

# plt.imshow(img2)<jupyter_output><empty_output><jupyter_text>convert images to  torch batch dataif you want to train 2 imgs, set **Batch_Train** = True.Set **Batch_Train** = False, only 1 img will be trained<jupyter_code>import torch
import time
import torchvision.transforms as T
# print(img1.shape)
trsfm=T.ToTensor()

Batch_Train= True
if Batch_Train is True:
    # batch img transform
    x=torch.from_numpy(np.array([img1,img2]))
    x=x.permute(0,3,1,2)
    print(x.shape)
else:
    # single img transform
    x=trsfm(img1).unsqueeze(0)
    print(x.shape)
    # plt.imshow(img1)
dtype=torch.float
USE_GPU=True

if USE_GPU and torch.cuda.is_available():
    device = torch.device('cuda')
else:
    device = torch.device('cpu')

device=torch.device('cuda')
print(device)<jupyter_output>cuda<jupyter_text>Prepare Extractor Network Extractor Network is used to get feature map from imgs<jupyter_code>from model.modules.extractor_network import FeatureNet
fNet=FeatureNet()
x=x.to(dtype=dtype,device=device)
fNet.cuda(device=device)

start = time.time()
featureMap = fNet(x)
end = time.time()
print(end- start)
print(featureMap.shape)<jupyter_output>0.8747732639312744
torch.Size([2, 512, 32, 32])<jupyter_text>Test Region Proposal Network<jupyter_code>from model.modules.region_proposal_network import RPN

rpn_net= RPN()
rpn_net.cuda()
img_size=x.shape[2:]
# a,b,c,d,e=rpn_net(featureMap,img_size)

start = time.time()
score, loc, rois, roi_indices, anchor=rpn_net(featureMap,img_size)
end = time.time()
print(end- start)
print(rois.shape)<jupyter_output>0.04589724540710449
(2000, 4)<jupyter_text>Test Head Network<jupyter_code>from model.modules.head_network import Head

head_net=Head(n_class=10)
head_net.cuda()

start = time.time()
final_loc, final_score=head_net(featureMap, rois, roi_indices)
end = time.time()
print(end- start)<jupyter_output>0.6064224243164062<jupyter_text>Test FastRCNN<jupyter_code>from model.modules.faster_rcnn import FasterRCNN

fastNet = FasterRCNN(fNet,rpn_net,head_net)
fastNet.predict(x)<jupyter_output><empty_output><jupyter_text>Test Data Mean<jupyter_code>from data.dataset import Dataset
from tqdm import tqdm_notebook as tqdm
imgs=Dataset()

a,d,b,d=imgs[1]

avg=list()
for i in tqdm(range(len(imgs))):
    img,_,_,_=imgs[i]
    img=img/255
    avg.append(img.reshape(3,-1).mean(axis=1))
    
# # print(a.shape)
# print(a.reshape(3,-1).mean(axis=1))
a=a.transpose(1,2,0)/255
plt.imshow(a)
avg_np=np.array(avg)
a=np.mean(avg_np,axis=0)
b=np.std(avg_np,axis=0)
print(a,b)
# from model.faster_rcnn import FasterRCNN as FRCNN

# fNet=FRCNN(n_class=10)
# x=x.to(dtype=dtype,device=device)
# fNet.cuda(device=device)

# bboxes, labels, scores=fNet.predict(x)
# # print(fNet)
# # y=fNet.featureNet(x)
# # score, loc, rois,roi_indices, anchor=fNet.rpn(y, [512,512])
# # a=fNet.head.RoIPool(featureMap, rois , roi_indices)
# print(l.shape,s.shape)
from model.utils.vgg_decompose import FeatureNet as features
x=x.to(dtype=dtype,device=device)
features.cuda(device=device)
featureMap=features(x)
featureMap.shape
# import torch.nn as nn
# output=[]
# t=featureMap[0]
# g=nn.AdaptiveMaxPool2d((7,7))
# m=g(t[:,1:25,2:10])
# output.append(m)

# # t=featureMap[1]
# # g=nn.AdaptiveMaxPool2d((7,7))
# # m=g(t[:,5:25,1:10])
# # output.append(m)

# e=torch.stack(output, dim=0)
# print(e.shape)
from model.utils.vgg_decompose import ClassifierNet as classifier

classifier
from model.utils.rpn import RPN
rNet=RPN()
rNet.cuda()
# rNet.eval()
img_size=img1.shape
print(img_size)
score, loc, rois,roi_indices, anchor=rNet(featureMap, img_size)
print(roi_indices.shape)
from model.utils.vgg_roi_head import VGG_RoI_Head
vNet=VGG_RoI_Head(10)
vNet.cuda()
final_loc, final_score = vNet(featureMap, rois, roi_indices)
print(final_loc.shape)
from model.utils.final_suppression import suppression

bboxes, labels, scores=suppression(rois,roi_indices,final_loc, final_score, 10, img_size, img_number=2, high_thresh=True)
# import fina
print(bboxes[1].shape)
print(bboxes[0].shape)
import matplotlib.patches as patches

fig,ax = plt.subplots(1,figsize=(15,15))

# Display the image
ax.imshow(img2)
n=50
bbox=bboxes[0]
for i in range(n,n+5):
    xy=bbox[i][0:2]
    w=bbox[i][2]-bbox[i][0]
    h=bbox[i][3]-bbox[i][1]
    # Create a Rectangle patch
    rect = patches.Rectangle(xy,w,h,linewidth=1,edgecolor='r',facecolor='none')
    # Add the patch to the Axes
    ax.add_patch(rect)

plt.show()
print(bboxes[0].shape)
# type(rois)
# rois=torch.from_numpy(rois)
# rois.reshape(-1,1,4).shape
# loc.cpu().data.numpy()
# type(rois)
from model.faster_rcnn import FasterRCNN as FRCNN

fNet=FRCNN()
x=x.to(dtype=dtype,device=device)
# fNet.cuda(device=device)

# l,s=fNet(x)
# print(fNet)
# y=fNet.featureNet(x)
# score, loc, rois,roi_indices, anchor=fNet.rpn(y, [512,512])
# a=fNet.head.RoIPool(featureMap, rois , roi_indices)
from model.utils.roi_pooling import RoIPool
roiPool=RoIPool()
output=roiPool(featureMap,rois, roi_indices)

# from model.utils.vgg_decompose import classifier

# print(output.shape)
# classifier.cuda()
# output.to(device=device)
# output=output.view(output.size(0),-1)
# out=classifier(output)

# print(out.shape)

# from model.utils.vgg_roi_head import VGG_RoI_Head
# RoIHead=VGG_RoI_Head(n_class=10)
# RoIHead.cuda()
# output.to(device=device)
# final_loc, final_score= RoIHead(featureMap, rois, roi_indices)
print(final_loc.shape)
print(final_score[0])
print(output.shape)
import matplotlib.patches as patches

fig,ax = plt.subplots(1,figsize=(15,15))


# Display the image
ax.imshow(img1)

# rois=anchor
print(rois.shape)
n=100
for i in range(n,n+3):
    xy=rois[i][0:2]
    w=rois[i][2]-rois[i][0]
    h=rois[i][3]-rois[i][1]
    # Create a Rectangle patch
    rect = patches.Rectangle(xy,w,h,linewidth=1,edgecolor='r',facecolor='none')
    # Add the patch to the Axes
    ax.add_patch(rect)

plt.show()



from trainer import FasterRCNNTrainer
from model.faster_rcnn import FasterRCNN

faster_rcnn=FasterRCNN()
faster_rcnn.cuda()
trainer = FasterRCNNTrainer(faster_rcnn)
import torch
import torch.nn as nn

model=nn.Sequential(nn.Conv2d(3,10,3))
torch.optim.SGD(model.parameters(),0.1)
from data.dataset import Dataset
imgDataset=Dataset()
img, bbox, label, scale = imgDataset[100]
print(img.shape)
print(bbox.shape)
print(label.shape)
print(scale)

print('bbox',bbox)
print('label', label)

imgs=img[None,:,:,:]
bboxs=bbox[None,:,:]
labels=label[None,:]
scales=scale

device=torch.device('cuda')
dtype=torch.float
imgs=torch.from_numpy(imgs).to(dtype=dtype, device=device)
bboxs=torch.from_numpy(bboxs).to(dtype=dtype, device=device)
labels=torch.from_numpy(labels).to(dtype=dtype, device=device)

for i in range(1000):
    img, bbox, label, scale = imgDataset[i]
    imgs=img[None,:,:,:]
    bboxs=bbox[None,:,:]
    labels=label[None,:]
    scales=scale

    device=torch.device('cuda')
    dtype=torch.float
    imgs=torch.from_numpy(imgs).to(dtype=dtype, device=device)
    bboxs=torch.from_numpy(bboxs).to(dtype=dtype, device=device)
    labels=torch.from_numpy(labels).to(dtype=dtype, device=device)
    loss=trainer.train_step(imgs, bboxs, labels, scales)
    print(loss.total_loss.cpu().detach().numpy())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1542011673679924, 'fraction_numerical': 0.027826795167639473, 'mean_word_length': 6.57245632065776, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '116914', 'license': 'MIT', 'n_tokens_mistral': 3180, 'n_tokens_neox': 3104, 'n_words': 476, 'path': 'Unit_test.ipynb', 'repo_name': 'xufangda/Faster_RCNN_without_pain', 'size': '7367'}