<jupyter_start><jupyter_text>https://www.kaggle.com/aryaprince/getting-started-with-object-detection-with-pytorch/?  EDA<jupyter_code>import torch
from torch.utils.data import Dataset, DataLoader
import torchvision
import torchvision.transforms as transforms

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

from PIL import Image
import cv2
import os
import re
data_path = '../../data/wheat_detection/'

df_train = pd.read_csv(data_path + 'train.csv')
df_sub = pd.read_csv(data_path + 'sample_submission.csv')
df_train.head()
df_train = df_train.drop(columns=['width', 'height', 'source'])
df_train.head()
len(df_train['image_id'].unique())
aa = df_train['bbox'].iloc[0]
aa
bbox_list = df_train['bbox']
x1, y1, x2, y2 = [], [], [], []
for b in bbox_list:
    bbox = re.findall('([0-9]+[.]?[0-9]*)', b)
    x1.append(float(bbox[0]))
    y1.append(float(bbox[1]))
    x2.append(float(bbox[0]) + float(bbox[2]))
    y2.append(float(bbox[1]) + float(bbox[3]))

df_train['x1'] = x1
df_train['y1'] = y1
df_train['x2'] = x2
df_train['y2'] = y2

df_train.head()
image_ids = df_train['image_id'].unique()
valid_ids = image_ids[-665:]
train_ids = image_ids[:-665]

df_valid = df_train[df_train['image_id'].isin(valid_ids)]
df_train = df_train[df_train['image_id'].isin(train_ids)]
df_valid.head()
df_train.head()
import glob
image_list = glob.glob(data_path + 'train/*.jpg')
sample = Image.open(image_list[0])
plt.imshow(sample)
plt.axis('off')<jupyter_output><empty_output><jupyter_text>Dataset<jupyter_code>class wheatdataset(Dataset):
    
    def __init__(self, dataframe, image_dir, transform=None):
        self.dataframe = dataframe
        self.image_dir = image_dir
        self.transform = transform
        
        self.image_ids = self.dataframe['image_id'].unique()
    
    def __len__(self):
        return len(self.image_ids)
    
    def __getitem__(self, idx):
        image_id = self.image_ids[idx]
        
        img = Image.open(self.image_dir + image_id + '.jpg')
        img = np.array(img) / 255.
      
    
        img = cv2.imread(self.image_dir + image_id + '.jpg')
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)
        img /= 255.0
        
        if self.transform:
            img = self.transform(img)
            
        
        boxes = self.dataframe.loc[self.dataframe['image_id'] == image_id, ['x1','y1','x2','y2']]
        boxes = np.array(boxes)
        boxes = torch.from_numpy(boxes).type(torch.float32)
        
        labels = torch.ones((boxes.shape[0])).type(torch.int64)
        
        targets = {}
        targets['boxes'] = boxes
        targets['labels'] = labels
        
        return img.type(torch.FloatTensor), targets
    
def collate_fn(batch):
    return tuple(zip(*batch))
TRAIN_DIR = '../../data/wheat_detection/train/'
TEST_DIR = '../../data/wheat_detection/test/'

transform = transforms.Compose([
    transforms.ToTensor()
])

train_data = wheatdataset(df_train, TRAIN_DIR, transform=transform)
print(len(train_data))
trainloader = DataLoader(train_data, batch_size=1, collate_fn=collate_fn)
iter(trainloader).next()
device = 'cuda' if torch.cuda.is_available else 'cpu'

images, targets = iter(trainloader).next()
images = list(image for image in images)
targets = [{k: v for k, v in t.items()} for t in targets]
boxes = targets[0]['boxes'].cpu().numpy()
sample = images[0].permute(1,2,0).numpy()

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

for box in boxes:
    cv2.rectangle(sample,
                  (box[0], box[1]),
                  (box[2], box[3]),
                  (255, 0, 0),
                  6)
    
ax.set_axis_off()
plt.imshow(sample)<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).<jupyter_text>Model<jupyter_code>from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection import fasterrcnn_resnet50_fpn
model = fasterrcnn_resnet50_fpn(pretrained=False)

num_classes = 2 # background & wheat

in_features = model.roi_heads.box_predictor.cls_score.in_features


model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
model<jupyter_output><empty_output><jupyter_text>Sample<jupyter_code>device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = model.to(device)
#model.load_state_dict(torch.load('../../data/wheat_detection/fasterrcnn_resnet50_fpn_best.pth'))
images, targets = iter(trainloader).next()
images = [img.type(torch.FloatTensor).to(device) for img in images]
targets = [{k:v.to(device) for k,v in t.items()} for t in targets]
model.eval()
with torch.no_grad():
    outputs = model(images)
    
outputs
boxes = outputs[0]['boxes'].cpu().numpy()
sample = images[0].permute(1,2,0).cpu().numpy()


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

for box in boxes:
    cv2.rectangle(sample,
                  (int(box[0]), int(box[1])),
                  (int(box[2]), int(box[3])),
                  (255, 0, 0),
                  6)
    
ax.set_axis_off()
plt.imshow(sample)<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).<jupyter_text>Train<jupyter_code>print(df_train)
print(df_valid)
TRAIN_DIR = '../../data/wheat_detection/train/'
TEST_DIR = '../../data/wheat_detection/test/'

transform = transforms.Compose([
    transforms.ToTensor()
])

def collate_fn(batch):
    return tuple(zip(*batch))

train_data = wheatdataset(df_train, TRAIN_DIR, transform=transform)
valid_data = wheatdataset(df_valid, TRAIN_DIR, transform=transform)

train_loader = DataLoader(train_data, batch_size = 1, shuffle=True, collate_fn=collate_fn)
valid_data = DataLoader(valid_data, batch_size = 4, shuffle=False, collate_fn=collate_fn, num_workers=2)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
import torch.optim as optim

model.to(device)
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
images
train_loss_list = []
for e in range(1):
    model.train()
    train_loss = 0
    for i, (images, targets) in enumerate(train_loader):
        
        images = [img.type(torch.FloatTensor).to(device) for img in images]
        targets = [{k:v.to(device) for k, v in t.items()} for t in targets]
        
        optimizer.zero_grad()
        loss_dict = model.to(device)(images, targets)
        
        loss = sum(loss for loss in loss_dict.values())
        train_loss += loss.item()
        loss.backward()
        optimizer.step()
        if i % 40 == 0:
            print('Epoch : [%d/%d] ----- Loss : %0.3f' %(i+1, len(train_loader), loss))
            
    print('Epoch : [%d/%d] ------ Loss : %0.3f' %(i+1, len(train_loader), loss))
    train_loss_list.append(train_loss)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.151698670605613, 'fraction_numerical': 0.01979320531757755, 'mean_word_length': 3.6061224489795918, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '300054', 'license': 'MIT', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2419, 'n_words': 459, 'path': 'Global_Wheat_Detection/practice1.ipynb', 'repo_name': 'gjustin40/Kaggle', 'size': '6770'}