<jupyter_start><jupyter_text>Load module<jupyter_code>import sys
sys.path.append('../')

from ssd_data import datasets
from ssd_data import transforms, target_transforms, augmentations, utils

from ssd.models.ssd300 import SSD300
from ssd.train import *

#from torchvision import transforms > not import!!
from torch.utils.data import DataLoader
from torch.optim.sgd import SGD
from torch.optim.adam import Adam<jupyter_output><empty_output><jupyter_text>Load COCO Dataset<jupyter_code>augmentation = augmentations.AugmentationOriginal()
#augmentation = None
transform = transforms.Compose(
    [transforms.Resize((300, 300)),
     transforms.ToTensor(),
     transforms.Normalize(rgb_means=(0.485, 0.456, 0.406), rgb_stds=(0.229, 0.224, 0.225))]
)
target_transform = target_transforms.Compose(
    [target_transforms.Corners2Centroids(),
     target_transforms.OneHot(class_nums=datasets.COCO_class_nums, add_background=True),
     target_transforms.ToTensor()]
)
train_dataset = datasets.COCO2014_TrainValDataset(ignore=target_transforms.Ignore(iscrowd=True), transform=transform, target_transform=target_transform, augmentation=augmentation)

train_loader = DataLoader(train_dataset,
                          batch_size=32,
                          shuffle=True,
                          collate_fn=utils.batch_ind_fn,
                          num_workers=4,
                          pin_memory=True)
print('{} training images'.format(len(train_dataset)))<jupyter_output>loading annotations into memory...
Done (t=5.74s)
creating index...
index created!
loading annotations into memory...
Done (t=2.56s)
creating index...
index created!
122218 training images<jupyter_text>Define model<jupyter_code>model = SSD300(class_labels=train_dataset.class_labels, batch_norm=False).cuda()
model.load_vgg_weights()
print(model)<jupyter_output>SSD300(
  (codec): Codec(
    (encoder): Encoder()
    (decoder): Decoder()
  )
  (defaultBox): DBoxSSDOriginal()
  (predictor): Predictor()
  (inferenceBox): InferenceBox()
  (feature_layers): ModuleDict(
    (convRL1_1): ConvRelu(
      (conv): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (relu): ReLU(inplace=True)
    )
    (convRL1_2): ConvRelu(
      (conv): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (relu): ReLU(inplace=True)
    )
    (pool1): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
    (convRL2_1): ConvRelu(
      (conv): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (relu): ReLU(inplace=True)
    )
    (convRL2_2): ConvRelu(
      (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (relu): ReLU(inplace=True)
    )
    (pool2): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
    (convRL3[...]<jupyter_text>Set matplotlib for Jupyter notebook<jupyter_code>%matplotlib notebook<jupyter_output><empty_output><jupyter_text>Train COCO<jupyter_code>optimizer = SGD(model.parameters(), lr=1e-3, momentum=0.9, weight_decay=5e-4) # late and not converge.
#optimizer = Adam(model.parameters(), lr=1e-3, weight_decay=5e-4)
iter_sheduler = SSDIterMultiStepLR(optimizer, milestones=(160000, 200000), gamma=0.1, verbose=True)

save_manager = SaveManager(modelname='ssd300-COCO', interval=10000, max_checkpoints=15, plot_yrange=(0, 8))
log_manager = LogManager(interval=500, save_manager=save_manager, loss_interval=500, live_graph=LiveGraph((0, 8)))
trainer = TrainLogger(model, loss_func=SSDLoss(), optimizer=optimizer, scheduler=iter_sheduler, log_manager=log_manager)

#trainer.train(70, train_loader)
trainer.train(240000, train_loader)<jupyter_output><empty_output><jupyter_text>Train VOC<jupyter_code>import sys
sys.path.append('../')

from ssd_data import datasets
from ssd_data import transforms, target_transforms, augmentations, utils

from ssd.models.ssd300 import SSD300
from ssd.train import *

#from torchvision import transforms > not import!!
from torch.utils.data import DataLoader
from torch.optim.sgd import SGD
from torch.optim.adam import Adam

%matplotlib notebook

augmentation = augmentations.AugmentationOriginal()
#augmentation = None
transform = transforms.Compose(
    [transforms.Resize((300, 300)),
     transforms.ToTensor(),
     transforms.Normalize(rgb_means=(0.485, 0.456, 0.406), rgb_stds=(0.229, 0.224, 0.225))]
)
target_transform = target_transforms.Compose(
    [target_transforms.Corners2Centroids(),
     target_transforms.OneHot(class_nums=datasets.VOC_class_nums, add_background=True),
     target_transforms.ToTensor()]
)
train_dataset = datasets.Compose(datasets=(datasets.VOC2007_TrainValDataset, datasets.VOC2012_TrainValDataset),
                                 ignore=target_transforms.Ignore(difficult=True), transform=transform, target_transform=target_transform, augmentation=augmentation)

train_loader = DataLoader(train_dataset,
                          batch_size=32,
                          shuffle=True,
                          collate_fn=utils.batch_ind_fn,
                          num_workers=4,
                          pin_memory=True)
print('{} training images'.format(len(train_dataset)))
model = SSD300(class_labels=train_dataset.class_labels, batch_norm=False).cuda()
model.load_for_finetune('../weights/ssd300-voc2007+12+coco/ssd300-COCO_i-240000.pth')
print(model)
optimizer = SGD(model.parameters(), lr=1e-3, momentum=0.9, weight_decay=5e-4) # late and not converge.
#optimizer = Adam(model.parameters(), lr=1e-3, weight_decay=5e-4)
iter_sheduler = SSDIterMultiStepLR(optimizer, milestones=(10000, 20000), gamma=0.1, verbose=True)

save_manager = SaveManager(modelname='ssd300-voc2007+2012+coco', interval=5000, max_checkpoints=15, plot_yrange=(0, 8))
log_manager = LogManager(interval=10, save_manager=save_manager, loss_interval=10, live_graph=LiveGraph((0, 8)))
trainer = TrainLogger(model, loss_func=SSDLoss(), optimizer=optimizer, scheduler=iter_sheduler, log_manager=log_manager)

#trainer.train(70, train_loader)
trainer.train(30000, train_loader)
import sys
sys.path.append('../')

from ssd_data import datasets
from ssd_data import transforms, target_transforms, augmentations, utils

from ssd.models.ssd300 import SSD300
from ssd.train import *

#from torchvision import transforms > not import!!
from torch.utils.data import DataLoader

transform = transforms.Compose(
    [transforms.Resize((300, 300)),
     transforms.ToTensor(),
     transforms.Normalize(rgb_means=(0.485, 0.456, 0.406), rgb_stds=(0.229, 0.224, 0.225))]
)
target_transform = target_transforms.Compose(
    [target_transforms.Corners2Centroids(),
     target_transforms.OneHot(class_nums=datasets.VOC_class_nums, add_background=True),
     target_transforms.ToTensor()]
)


test_dataset = datasets.VOC2007_TestDataset(transform=transform, target_transform=target_transform)
test_loader = DataLoader(test_dataset,
                          batch_size=32,
                          shuffle=False,
                          collate_fn=utils.batch_ind_fn,
                          num_workers=4,
                          pin_memory=True)

model = SSD300(class_labels=test_dataset.class_labels, batch_norm=False).cuda()
model.load_weights('../weights/ssd300-voc2007+12+coco/ssd300-voc2007+2012+coco_i-30000.pth')
model.eval()

evaluator = VOC2007Evaluator(test_loader)
ap = evaluator(model)
print(ap)<jupyter_output>inferring...	155/155:	100%
{'aeroplane': 0.786150178389392, 'bicycle': 0.8399774174945714, 'bird': 0.7507116905281198, 'boat': 0.6923998209602293, 'bottle': 0.5230408289991064, 'bus': 0.8480870902485015, 'car': 0.8288337582101216, 'cat': 0.8820340782319882, 'chair': 0.5942341368577568, 'cow': 0.8343326949977034, 'diningtable': 0.6750599362303004, 'dog': 0.8530757775519262, 'horse': 0.892378109832055, 'motorbike': 0.8595394209308723, 'person': 0.8028232927218402, 'pottedplant': 0.564724227389145, 'sheep': 0.7689508449648664, 'sofa': 0.8006025781988818, 'train': 0.856105506403826, 'tvmonitor': 0.7112252683634754, 'mAP': 0.7682143328752341}
{'fraction_non_alphanumeric': 0.12821149311158, 'fraction_numerical': 0.09259029415415167, 'mean_word_length': 4.951255539143279, 'pattern_counts': {'":': 0, '<': 22, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '299785', 'license': 'MIT', 'n_tokens_mistral': 3312, 'n_tokens_neox': 2816, 'n_words': 470, 'path': 'demo/training300-voc2007+2012+COCO.ipynb', 'repo_name': 'star-baba/res50_sa_ssd', 'size': '8057'}