<jupyter_start><jupyter_text>Fully Convolutional Network - Semantic Segmentation![image.png](imgs/2.png) ![image.png](imgs/3.png)<jupyter_code>import os
import os.path as osp
import pytz
import torch

import warnings
warnings.filterwarnings('ignore')

configurations = {
    # same configuration as original work
    # https://github.com/shelhamer/fcn.berkeleyvision.org
    1: dict(
        max_iteration=100000,
        lr=1.0e-10,
        momentum=0.99,
        weight_decay=0.0005,
        interval_validate=4000,
    )
}
from types import SimpleNamespace
opts = SimpleNamespace()
opts.cfg = configurations[1]
opts.resume = ''
print(opts.cfg)
from utils import get_log_dir
opts.out = get_log_dir('resnet101', 1, opts.cfg)
print(opts.out)
gpu = 1
os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu)
cuda = torch.cuda.is_available()
print('Cuda: {}'.format(cuda))
opts.cuda = 'cuda' if cuda else 'cpu'
opts.mode = 'train'
opts.backbone = 'resnet'
opts.fcn = '101'<jupyter_output><empty_output><jupyter_text>PascalVOC Dataset - Downloaded on _`root`_ variable<jupyter_code>root = './data/Pascal_VOC'
print(root)
from data_loader import Pascal_Data
kwargs = {'num_workers': 4} if cuda else {}
train_loader = torch.utils.data.DataLoader(
        Pascal_Data(root, image_set='train', backbone=opts.backbone),
        batch_size=1, shuffle=True, **kwargs)
val_loader = torch.utils.data.DataLoader(
        Pascal_Data(root, image_set='val', backbone=opts.backbone),
        batch_size=1, shuffle=False, **kwargs)
data_loader = [train_loader, val_loader]
%matplotlib inline
import matplotlib.pyplot as plt
for data, target in train_loader: break
print(data.shape)
print(target.shape)
data.min()
data_show, label_show = train_loader.dataset.untransform(data[0].cpu().clone(), target[0].cpu().clone())

plt.imshow(data_show)
plt.show()

def imshow_label(label_show):
    import matplotlib
    import numpy as np
    cmap = plt.cm.jet
    # extract all colors from the .jet map
    cmaplist = [cmap(i) for i in range(cmap.N)]
    cmaplist[0] = (0.0,0.0,0.0,1.0)
    cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)
    # define the bins and normalize
    bounds = np.arange(0,len(train_loader.dataset.class_names))
    norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
    plt.imshow(label_show, cmap=cmap, norm=norm)
    cbar = plt.colorbar(ticks=bounds)
    cbar.ax.set_yticklabels(train_loader.dataset.class_names)
    plt.show()    
    
imshow_label(label_show)<jupyter_output><empty_output><jupyter_text>FCN - Model<jupyter_code>import numpy as np
import torch.nn as nn
from torchvision.models.segmentation import fcn_resnet101

class FCN(nn.Module):
    def __init__(self, n_class=21):

        super(FCN, self).__init__()
        self.fcn = fcn_resnet101(pretrained=False, num_classes=21)
        # Uses bilinear interpolation for upsampling
        # https://github.com/pytorch/vision/blob/master/torchvision/models/segmentation/_utils.py

    def forward(self, x, debug=False):
        return self.fcn(x)['out']          
model = FCN(n_class=21)
model.to(opts.cuda)
iter_loader=iter(train_loader)
data, target = next(iter_loader)
data = data.to(opts.cuda)
with torch.no_grad():
    output = model(data)
print('input: ', data.shape)
print('output: ', output.data.shape)
data, target = next(iter_loader)
data = data.to(opts.cuda)
with torch.no_grad():
    print('input: ', data.shape)
    output = model.fcn.backbone(data)['out']
    print('backbone: ', output.data.shape)
    output = model.fcn.classifier(output)
    print('output: ', output.data.shape)
data, target = next(iter_loader)
data = data.to(opts.cuda)
with torch.no_grad():
    print('input: ', data.shape)
    output = model.fcn.backbone(data)['out']
    print('backbone: ', output.data.shape)
    output = model.fcn.classifier(output)
    print('output: ', output.data.shape)
%matplotlib inline
from trainer import Trainer
trainer = Trainer(data_loader, opts)
print(opts.cfg.get('interval_validate', len(train_loader))) #Validate every 4000 iterations
print(opts.out)
start_epoch = 0
start_iteration = 0
if opts.resume:
    start_epoch = checkpoint['epoch']
    start_iteration = checkpoint['iteration']
trainer.epoch = start_epoch
trainer.iteration = start_iteration
trainer.Train()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13308428671491945, 'fraction_numerical': 0.015643240719122112, 'mean_word_length': 4.5708712613784135, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '586999', 'license': 'MIT', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1522, 'n_words': 292, 'path': 'resnet101_train.ipynb', 'repo_name': 'affromero/pytorch-fcn', 'size': '4283'}