<jupyter_start><jupyter_text>Functions<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

def open_img(img_fname: str) -> np.array:
    image = Image.open(img_fname)
    return np.array(image)

def show_img(image: np.array) -> None:
    fig = plt.figure(figsize=(14, 8))
    plt.imshow(image)
    plt.axis('off')
    plt.show()
    
def depth_to_heatmap(depht_arr: np.array, reverse: bool = True) -> np.array:
    colormap = plt.get_cmap('inferno')
    depht_arr = depht_arr / depht_arr.max()
    
    if reverse:
        mask = np.ma.masked_where(depht_arr==0, depht_arr)
        depht_arr = 1 - depht_arr
        depht_arr[mask.mask] = 0

    heatmap = colormap(depht_arr)
    return heatmap<jupyter_output><empty_output><jupyter_text>Evo data  Monodepth2<jupyter_code>import sys
from pathlib import Path

repo_dir = Path().resolve() / "networks/monodepth2"
sys.path.insert(0, repo_dir.as_posix())<jupyter_output><empty_output><jupyter_text>Options<jupyter_code>from dataclasses import dataclass
from typing import Tuple

@dataclass
class Options:
    # PATHS
    data_path: str
    log_dir: str = (repo_dir / "tmp").as_posix()
    
    # TRAINING options
    model_name: str = (repo_dir / "mdp").as_posix()
    split: str = "eigen_zhou"
    num_layers: int = 18
    dataset: str = "kitti"
    png: bool = False
    height: int = 192
    width: int = 640
    disparity_smoothness: float = 1e-3
    scales: Tuple = (0, 1, 2, 3, 4)
    min_depth: float = 0.1
    max_depth: float = 100.0
    use_stereo: bool = False
    frame_ids: Tuple[int] = (-1, 0, 1)

    # OPTIMIZATION options
    batch_size: int = 1 # 12
    learning_rate: float = 1e4
    num_epochs: int = 20
    scheduler_step_size: int = 15
    
    # SYSTEM options
    no_cuda: bool = False
    num_workers: int = 12
        
    # ABLATION options
    weights_init: str = "pretrained"
        
     # LOADING options
    load_weights_folder: str = repo_dir / "models/mono_640x192"
    models_to_load: Tuple[str] = ("encoder", "depth")
    
data_path = Path().resolve() / "datasets/bags_historical"     
data_path = Path("/data/dvkorshunov/datasets/kitti")
opt = Options(data_path=data_path.as_posix())
opt<jupyter_output><empty_output><jupyter_text>Dataset  Dataloader<jupyter_code>from torch.utils.data import DataLoader
from datasets import KITTIRAWDataset
from utils import readlines


train_filenames = readlines(repo_dir /f"splits/{opt.split}/train_files.txt")
opt.png = True
img_ext = ".png" if opt.png else ".jpg"

train_dataset = KITTIRAWDataset(
    data_path=opt.data_path, 
    filenames=train_filenames, 
    height=opt.height, 
    width=opt.width,
    frame_idxs=opt.frame_ids, 
    num_scales=4, 
    img_ext=img_ext,
    is_train=True, 
)

train_loader = DataLoader(
    dataset=train_dataset, 
    batch_size=opt.batch_size, 
    shuffle=True,
    num_workers=opt.num_workers, 
    pin_memory=True, 
    drop_last=True
)
data = train_dataset[15000]
depth_data = data["depth_gt"].numpy().squeeze()
pre_img = data[('color', -1, 0)].numpy().transpose((1, 2, 0)) 

import matplotlib.pyplot as plt

arr = [pre_img, depth_data]
for img in arr:
    plt.figure(figsize=(16, 10))
    plt.imshow(img)
    plt.show()<jupyter_output><empty_output><jupyter_text>Network<jupyter_code>import torch
import numpy as np
# import torch.nn.functional as F
import torch.optim as optim
# from torch.utils.data import DataLoader
# from tensorboardX import SummaryWriter


import networks


device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
models = {}
parameters_to_train = []

models["encoder"] = networks.ResnetEncoder(opt.num_layers, opt.weights_init == "pretrained")
models["encoder"].to(device)
parameters_to_train += list(models["encoder"].parameters())

models["depth"] = networks.DepthDecoder(models["encoder"].num_ch_enc, opt.scales)
models["depth"].to(device)
parameters_to_train += list(models["depth"].parameters())
import os

def load_model(opt, models):
    """Load model(s) from disk
    """
    opt.load_weights_folder = os.path.expanduser(opt.load_weights_folder)

    assert os.path.isdir(opt.load_weights_folder), \
        "Cannot find folder {}".format(opt.load_weights_folder)
    print("loading model from folder {}".format(opt.load_weights_folder))

    for n in opt.models_to_load:
        print("Loading {} weights...".format(n))
        path = os.path.join(opt.load_weights_folder, "{}.pth".format(n))
        model_dict = models[n].state_dict()
        pretrained_dict = torch.load(path)
        pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
        model_dict.update(pretrained_dict)
        models[n].load_state_dict(model_dict)
model_optimizer = optim.Adam(parameters_to_train, opt.learning_rate)
model_lr_scheduler = optim.lr_scheduler.StepLR(
    optimizer=model_optimizer, 
    step_size=opt.scheduler_step_size, 
    gamma=0.1
)

if opt.load_weights_folder is not None:
    load_model(opt, models)

print("Training model named:\n  ", opt.model_name)
print("Models and tensorboard events files are saved to:\n  ", opt.log_dir)
print("Training is using:\n  ", device)<jupyter_output>loading model from folder /home/dvkorshunov/mono_depth/monocular_depth_estimation/networks/monodepth2/models/mono_640x192
Loading encoder weights...
Loading depth weights...
Training model named:
   /home/dvkorshunov/mono_depth/monocular_depth_estimation/networks/monodepth2/mdp
Models and tensorboard events files are saved to:
   /home/dvkorshunov/mono_depth/monocular_depth_estimation/networks/monodepth2/tmp
Training is using:
   cuda<jupyter_text>Train<jupyter_code>iterator = iter(train_loader)
for batch_idxin in range(len(train_loader)):
    try:
        inputs = next(iterator)
    except (FileNotFoundError, TypeError) as err:
        continue
    img = inputs[("color_aug", 0, 0)].to(device)
    features = models["encoder"](img)
    outputs = models["depth"](features)
    break

features = models["encoder"](data[("color_aug", 0, 0)].unsqueeze(0).to(device))
outputs = models["depth"](features)
depth_tmp = outputs[('disp', 0)].squeeze().cpu().detach().numpy()
depth_tmp.max()
plt.figure(figsize=(14,8))
plt.imshow(depth_tmp)
plt.show()
dataюлу
img = data[("color_aug", 0, 0)].numpy().transpose((1,2,0))
plt.figure(figsize=(14,8))
plt.imshow(img)
plt.show()
img = data["depth_gt"].numpy().transpose((1,2,0)).squeeze()
heatmap = depth_to_heatmap(img)
plt.figure(figsize=(14,8))
plt.imshow(heatmap)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12831926323867998, 'fraction_numerical': 0.015195702225633155, 'mean_word_length': 4.306188925081433, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 27, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '290244', 'license': 'MIT', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2332, 'n_words': 449, 'path': 'monodepth2_train.ipynb', 'repo_name': 'korshunovdv/monocular_depth_estimation', 'size': '6515'}