<jupyter_start><jupyter_text>Set Data Path<jupyter_code>from pathlib import Path

base_dir = Path("data")
train_dir = base_dir/Path("train")
validation_dir = base_dir/Path("validation")
test_dir = base_dir/Path("test")<jupyter_output><empty_output><jupyter_text>Image Transform Function<jupyter_code>from torchvision import transforms

transform = transforms.Compose([ 
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=(.5, .5, .5), std=(.5, .5, .5))
])<jupyter_output><empty_output><jupyter_text>Load Training Data (x: features, y: labels)<jupyter_code>import torch
from PIL import Image

x, y = [], []
for file_name in train_dir.glob("*.jpg"):
    bounding_box_file = file_name.with_suffix('.txt')
    
    with open(bounding_box_file) as file:
        lines = file.readlines()
        if(len(lines) > 1):
            continue
        else:
            line = lines[0].strip('\n')
            (classes, cen_x, cen_y, box_w, box_h) = list(map(float, line.split(' ')))
            torch_data = torch.FloatTensor([cen_x, cen_y, box_w, box_h])
            y.append(torch_data)
            
    img = Image.open(str(file_name)).convert('RGB')
    img = transform(img)
    x.append(img)<jupyter_output><empty_output><jupyter_text>Put Training Data into Torch Loader<jupyter_code>import torch.utils.data as Data

tensor_x = torch.stack(x)
tensor_y = torch.stack(y)
torch_dataset = Data.TensorDataset(tensor_x, tensor_y)
loader = Data.DataLoader(dataset=torch_dataset, batch_size=32, shuffle=True, num_workers=2)<jupyter_output><empty_output><jupyter_text>Load Pretrained RestNet18 Model<jupyter_code>import torchvision
from torch import nn

model = torchvision.models.resnet18(pretrained=True)
fc_in_size = model.fc.in_features
model.fc = nn.Linear(fc_in_size, 4)<jupyter_output><empty_output><jupyter_text>Parameters<jupyter_code>EPOCH = 10
LR = 1e-3<jupyter_output><empty_output><jupyter_text>Loss Function & Optimizer<jupyter_code>loss_func = nn.SmoothL1Loss()
opt = torch.optim.Adam(model.parameters(), lr=LR)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>for epoch in range(EPOCH):
    for step, (batch_x, batch_y) in enumerate(loader):
        batch_x = batch_x
        batch_y = batch_y
        output = model(batch_x)
        loss = loss_func(output, batch_y)
        opt.zero_grad()
        loss.backward()
        opt.step()
        
        if(step % 5 == 0):
            print("Epoch {} | Step {} | Loss {}".format(epoch, step, loss))<jupyter_output>Epoch 0 | Step 0 | Loss 0.3441098928451538
Epoch 0 | Step 5 | Loss 0.17193390429019928
Epoch 0 | Step 10 | Loss 0.10625818371772766
Epoch 1 | Step 0 | Loss 0.0646333172917366
Epoch 1 | Step 5 | Loss 0.03650979697704315
Epoch 1 | Step 10 | Loss 0.017900140956044197<jupyter_text>Show some of the Prediction<jupyter_code>%matplotlib inline
import cv2
from matplotlib import pyplot as plt
import numpy as np

model = model.cpu()
for batch_x, batch_y in loader:
    predict = model(batch_x)
    for x, pred, y in zip(batch_x, predict, batch_y):
        (pos_x, pos_y, box_w, box_h) = pred
        pos_x *= 224
        pos_y *= 224
        box_w *= 224
        box_h *= 224
        
        image = transforms.ToPILImage()(x)
        img = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
        img = cv2.rectangle(img, (pos_x - box_w/2, pos_y - box_h/2), (pos_x + box_w/2, pos_y + box_h/2), (255, 0, 0), 3)
        
        plt.imshow(img)
        plt.show()
    break<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12340062553312482, 'fraction_numerical': 0.05032698322433893, 'mean_word_length': 3.819178082191781, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 37, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '5638', 'license': 'MIT', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1297, 'n_words': 258, 'path': 'Single_Object_Detection_CPU.ipynb', 'repo_name': 'PM25/Object_Recognition_DL', 'size': '3517'}