Skip to content

Instantly share code, notes, and snippets.

from snippets.loader import *
from PIL import Image
import xml.etree.ElementTree as ET
from torchvision import transforms
device = 'cuda'
voc_labels = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable',
'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor')
label_map = {k: v + 1 for v, k in enumerate(voc_labels)}
label_map['background'] = 0
@sizhky
sizhky / voc.py
Created May 13, 2020 09:54
Load VOC data
import sys; sys.path.append('/home/yyr/data/VOCdevkit/')
from load_data import VOCDataset, get_items, aug_trn
from snippets.loader import *
_2007_root = Path("/home/yyr/data/VOCdevkit/VOC2007")
_2012_root = Path("/home/yyr/data/VOCdevkit/VOC2012")
train_items = get_items(_2007_root, 'train') + get_items(_2012_root, 'train')
val_items = get_items(_2007_root, 'val') + get_items(_2012_root, 'val')
logger.info(f'\n{len(train_items)} training images\n{len(val_items)} validation images')
x = VOCDataset(train_items, tfms=aug_trn)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sizhky
sizhky / load-voc-colab.py
Last active May 24, 2020 13:33
load voc into colab from kaggle
project_url = 'https://github.com/sizhky/rcnn'
import os
if not os.path.exists('faster-rcnn'):
!git clone --quiet {project_url} rcnn
!pip install -q --upgrade imgaug fire torch_snippets
from google.colab import files
files.upload() # upload kaggle.json
!mkdir -p ~/.kaggle
!mv kaggle.json ~/.kaggle/
!ls ~/.kaggle
df = pd.read_csv('../validation-annotations-bbox.csv')
df['LabelName'] = df['LabelName'].map(lambda x: code2label[x])
unique_classes = df['LabelName'].value_counts()
unique_classes = unique_classes[unique_classes < 501]
df = df[df['LabelName'].map(lambda x: x in unique_classes)]
print(df.ImageID.nunique())
!mkdir -p open-images-mini
from tqdm import tqdm
for f in tqdm(df.ImageID.unique()):
class OpenImages(Dataset):
def __init__(self, image_folder, df):
self.root = image_folder
self.df = df
self.unique_images = df['ImageID'].unique()
def __len__(self): return len(self.unique_images)
def __getitem__(self, ix):
image_id = self.unique_images[ix]
image_path = f'{self.root}/{image_id}.jpg'
image = cv2.imread(image_path, 1)[...,::-1] # conver BGR to RGB
@sizhky
sizhky / jupyter theme.sh
Last active August 6, 2022 13:41
jupyter theme
jt -t grade3 -fs 95 -tfs 11 -nfs 115 -cellw 95% -T
jt -t onedork -fs 95 -tfs 11 -nfs 115 -cellw 88% -T -N
import torch, math
import torch.nn as nn
import torch.nn.functional as F
class RetinaNet(nn.Module):
num_anchors = 9
def __init__(self, num_classes):
super(RetinaNet, self).__init__()
self.fpn = FPN50()
self.num_classes = num_classes
@sizhky
sizhky / set_background.py
Created June 14, 2020 09:30
set background in jupyter cells
from IPython.core.magic import register_cell_magic
from IPython.display import HTML, display
def set_background(color):
script = (
"var cell = this.closest('.jp-CodeCell');"
"var editor = cell.querySelector('.jp-Editor');"
"editor.style.background='{}';"
"this.parentNode.removeChild(this)"
).format(color)
@sizhky
sizhky / youtube speed
Created July 4, 2020 15:19
Youtube speed over 9000
document.getElementsByTagName("video")[0].playbackRate = 9001