├── .gitignore ├── 020_TensorIntro └── Tensors.py ├── 030_ModelingIntroduction ├── 00_LinRegFromScratch_end.py ├── 00_LinRegFromScratch_start.py ├── 10_LinReg_ModelClass_end.py ├── 10_LinReg_ModelClass_start.py ├── 20_LinReg_Batches_end.py ├── 20_LinReg_Batches_start.py ├── 30_LinReg_DatasetDataloader_end.py ├── 30_LinReg_DatasetDataloader_start.py ├── 40_LinReg_ModelSavingLoading_end.py ├── 40_LinReg_ModelSavingLoading_start.py ├── 50_LinReg_HyperparameterTuning_end.py ├── 50_LinReg_HyperparameterTuning_start.py └── model_state_dict.pth ├── 060_CNN_ImageClassification ├── ImagePreprocessing_end.py ├── ImagePreprocessing_start.py ├── LayerCalculations_end.py ├── LayerCalculations_start.py └── kiki.jpg ├── 100_SemanticSegmentation ├── data │ ├── Tile 1 │ │ ├── images │ │ │ ├── image_part_001.jpg │ │ │ ├── image_part_002.jpg │ │ │ ├── image_part_003.jpg │ │ │ ├── image_part_004.jpg │ │ │ ├── image_part_005.jpg │ │ │ ├── image_part_006.jpg │ │ │ ├── image_part_007.jpg │ │ │ ├── image_part_008.jpg │ │ │ └── image_part_009.jpg │ │ └── masks │ │ │ ├── image_part_001.png │ │ │ ├── image_part_002.png │ │ │ ├── image_part_003.png │ │ │ ├── image_part_004.png │ │ │ ├── image_part_005.png │ │ │ ├── image_part_006.png │ │ │ ├── image_part_007.png │ │ │ ├── image_part_008.png │ │ │ └── image_part_009.png │ ├── Tile 2 │ │ ├── images │ │ │ ├── image_part_001.jpg │ │ │ ├── image_part_002.jpg │ │ │ ├── image_part_003.jpg │ │ │ ├── image_part_004.jpg │ │ │ ├── image_part_005.jpg │ │ │ ├── image_part_006.jpg │ │ │ ├── image_part_007.jpg │ │ │ ├── image_part_008.jpg │ │ │ └── image_part_009.jpg │ │ └── masks │ │ │ ├── image_part_001.png │ │ │ ├── image_part_002.png │ │ │ ├── image_part_003.png │ │ │ ├── image_part_004.png │ │ │ ├── image_part_005.png │ │ │ ├── image_part_006.png │ │ │ ├── image_part_007.png │ │ │ ├── image_part_008.png │ │ │ └── image_part_009.png │ ├── Tile 3 │ │ ├── images │ │ │ ├── image_part_001.jpg │ │ │ ├── image_part_002.jpg │ │ │ ├── image_part_003.jpg │ │ │ ├── image_part_004.jpg │ │ │ ├── image_part_005.jpg │ │ │ ├── image_part_006.jpg │ │ │ ├── image_part_007.jpg │ │ │ ├── image_part_008.jpg │ │ │ └── image_part_009.jpg │ │ └── masks │ │ │ ├── image_part_001.png │ │ │ ├── image_part_002.png │ │ │ ├── image_part_003.png │ │ │ ├── image_part_004.png │ │ │ ├── image_part_005.png │ │ │ ├── image_part_006.png │ │ │ ├── image_part_007.png │ │ │ ├── image_part_008.png │ │ │ └── image_part_009.png │ ├── Tile 4 │ │ ├── images │ │ │ ├── image_part_001.jpg │ │ │ ├── image_part_002.jpg │ │ │ ├── image_part_003.jpg │ │ │ ├── image_part_004.jpg │ │ │ ├── image_part_005.jpg │ │ │ ├── image_part_006.jpg │ │ │ ├── image_part_007.jpg │ │ │ ├── image_part_008.jpg │ │ │ └── image_part_009.jpg │ │ └── masks │ │ │ ├── image_part_001.png │ │ │ ├── image_part_002.png │ │ │ ├── image_part_003.png │ │ │ ├── image_part_004.png │ │ │ ├── image_part_005.png │ │ │ ├── image_part_006.png │ │ │ ├── image_part_007.png │ │ │ ├── image_part_008.png │ │ │ └── image_part_009.png │ ├── Tile 5 │ │ ├── images │ │ │ ├── image_part_001.jpg │ │ │ ├── image_part_002.jpg │ │ │ ├── image_part_003.jpg │ │ │ ├── image_part_004.jpg │ │ │ ├── image_part_005.jpg │ │ │ ├── image_part_006.jpg │ │ │ ├── image_part_007.jpg │ │ │ ├── image_part_008.jpg │ │ │ └── image_part_009.jpg │ │ └── masks │ │ │ ├── image_part_001.png │ │ │ ├── image_part_002.png │ │ │ ├── image_part_003.png │ │ │ ├── image_part_004.png │ │ │ ├── image_part_005.png │ │ │ ├── image_part_006.png │ │ │ ├── image_part_007.png │ │ │ ├── image_part_008.png │ │ │ └── image_part_009.png │ ├── Tile 6 │ │ ├── images │ │ │ ├── image_part_001.jpg │ │ │ ├── image_part_002.jpg │ │ │ ├── image_part_003.jpg │ │ │ ├── image_part_004.jpg │ │ │ ├── image_part_005.jpg │ │ │ ├── image_part_006.jpg │ │ │ ├── image_part_007.jpg │ │ │ ├── image_part_008.jpg │ │ │ └── image_part_009.jpg │ │ └── masks │ │ │ ├── image_part_001.png │ │ │ ├── image_part_002.png │ │ │ ├── image_part_003.png │ │ │ ├── image_part_004.png │ │ │ ├── image_part_005.png │ │ │ ├── image_part_006.png │ │ │ ├── image_part_007.png │ │ │ ├── image_part_008.png │ │ │ └── image_part_009.png │ ├── Tile 7 │ │ ├── images │ │ │ ├── image_part_001.jpg │ │ │ ├── image_part_002.jpg │ │ │ ├── image_part_003.jpg │ │ │ ├── image_part_004.jpg │ │ │ ├── image_part_005.jpg │ │ │ ├── image_part_006.jpg │ │ │ ├── image_part_007.jpg │ │ │ ├── image_part_008.jpg │ │ │ └── image_part_009.jpg │ │ └── masks │ │ │ ├── image_part_001.png │ │ │ ├── image_part_002.png │ │ │ ├── image_part_003.png │ │ │ ├── image_part_004.png │ │ │ ├── image_part_005.png │ │ │ ├── image_part_006.png │ │ │ ├── image_part_007.png │ │ │ ├── image_part_008.png │ │ │ └── image_part_009.png │ ├── Tile 8 │ │ ├── images │ │ │ ├── image_part_001.jpg │ │ │ ├── image_part_002.jpg │ │ │ ├── image_part_003.jpg │ │ │ ├── image_part_004.jpg │ │ │ ├── image_part_005.jpg │ │ │ ├── image_part_006.jpg │ │ │ ├── image_part_007.jpg │ │ │ ├── image_part_008.jpg │ │ │ └── image_part_009.jpg │ │ └── masks │ │ │ ├── image_part_001.png │ │ │ ├── image_part_002.png │ │ │ ├── image_part_003.png │ │ │ ├── image_part_004.png │ │ │ ├── image_part_005.png │ │ │ ├── image_part_006.png │ │ │ ├── image_part_007.png │ │ │ ├── image_part_008.png │ │ │ └── image_part_009.png │ └── classes.json ├── dataprep_final.py ├── model_inference_final.py ├── model_inference_start.py ├── modeling_final.py ├── modeling_start.py └── sem_seg_dataset.py ├── 10_SemanticSegmentation_101.pdf ├── 15_CourseStructure.pdf ├── 20_Architectures.pdf ├── 30_Upsampling.pdf ├── 40_LossFunctions.pdf ├── 50_SemanticSegmentation_Lab.pdf ├── 60_Evaluation Metrics.pdf ├── 60_SemanticSegmentation_DataPrep.pdf ├── Model HyperparameterTuning.pdf └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | 132 | train/ 133 | val/ 134 | test/ 135 | *.pth -------------------------------------------------------------------------------- /020_TensorIntro/Tensors.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import torch 3 | import seaborn as sns 4 | import numpy as np 5 | 6 | #%% create a tensor 7 | x = torch.tensor(5.5) 8 | 9 | # %% simple calculations 10 | y = x + 10 11 | print(y) 12 | 13 | # %% automatic gradient calculation 14 | print(x.requires_grad) # check if requires_grad is true, false if not directly specified 15 | 16 | x.requires_grad_() # set requires grad to true, default True 17 | 18 | #%% or set the flag directly during creation 19 | x = torch.tensor(2.0, requires_grad=True) 20 | print(x.requires_grad) 21 | #%% function for showing automatic gradient calculation 22 | def y_function(val): 23 | return (val-3) * (val-6) * (val-4) 24 | 25 | x_range = np.linspace(0, 10, 101) 26 | x_range 27 | y_range = [y_function(i) for i in x_range] 28 | sns.lineplot(x = x_range, y = y_range) 29 | 30 | # %% define y as function of x 31 | y = (x-3) * (x-6) * (x-4) 32 | print(y) 33 | # %% 34 | 35 | # %% x -> y 36 | # create a tensor with gradients enabled 37 | x = torch.tensor(1.0, requires_grad=True) 38 | # create second tensor depending on first tensor 39 | y = (x-3) * (x-6) * (x-4) 40 | # calculate gradients 41 | y.backward() 42 | # show gradient of first tensor 43 | print(x.grad) 44 | # %% x -> y -> z 45 | x = torch.tensor(1.0, requires_grad=True) 46 | y = x**3 47 | z = 5*y - 4 48 | 49 | # %% 50 | z.backward() 51 | print(x.grad) # should be equal 5*3x**2 52 | # %% more complex network 53 | x11 = torch.tensor(2.0, requires_grad=True) 54 | x21 = torch.tensor(3.0, requires_grad=True) 55 | x12 = 5 * x11 - 3 * x21 56 | x22 = 2 * x11**2 + 2 * x21 57 | y = 4 * x12 + 3 * x22 58 | y.backward() 59 | print(x11.grad) 60 | print(x21.grad) 61 | # %% 62 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/00_LinRegFromScratch_end.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import pandas as pd 4 | import torch 5 | import torch.nn as nn 6 | import seaborn as sns 7 | 8 | #%% data import 9 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 10 | cars = pd.read_csv(cars_file) 11 | cars.head() 12 | 13 | #%% visualise the model 14 | sns.scatterplot(x='wt', y='mpg', data=cars) 15 | sns.regplot(x='wt', y='mpg', data=cars) 16 | 17 | #%% convert data to tensor 18 | X_list = cars.wt.values 19 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 20 | y_list = cars.mpg.values.tolist() 21 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 22 | X = torch.from_numpy(X_np) 23 | y = torch.tensor(y_list) 24 | 25 | 26 | #%% training 27 | w = torch.rand(1, requires_grad=True, dtype=torch.float64) 28 | b = torch.rand(1, requires_grad=True, dtype=torch.float64) 29 | 30 | num_epochs = 100 31 | learning_rate = 1e-3 32 | for epoch in range(num_epochs): 33 | for i in range(len(X)): 34 | # x, y = torch.tensor(X_list[i]), torch.tensor(y_list[i]) 35 | # forward pass 36 | y_predict = X[i] * w + b 37 | # calculate loss 38 | loss_tensor = torch.pow(y_predict - y[i], 2) 39 | # backward pass 40 | loss_tensor.backward() 41 | # extract losses 42 | loss_value = loss_tensor.data[0] 43 | # update weights and biases 44 | with torch.no_grad(): 45 | w -= w.grad * learning_rate 46 | b -= b.grad * learning_rate 47 | w.grad.zero_() 48 | b.grad.zero_() 49 | print(loss_value) 50 | 51 | #%% check results 52 | print(f"Weight: {w.item()}, Bias: {b.item()}") 53 | # %% 54 | y_pred = (torch.tensor(X_list)*w+b).detach().numpy() 55 | # %% 56 | sns.scatterplot(x=X_list, y=y_list) 57 | sns.lineplot(x=X_list, y=y_pred, color='red') 58 | # %% (Statistical) Linear Regression 59 | from sklearn.linear_model import LinearRegression 60 | reg = LinearRegression().fit(X_np, y_list) 61 | print(f"Slope: {reg.coef_}, Bias: {reg.intercept_}") 62 | 63 | 64 | # %% create graph visualisation 65 | # make sure GraphViz is installed (https://graphviz.org/download/) 66 | # if not computer restarted, append directly to PATH variable 67 | import os 68 | from torchviz import make_dot 69 | os.environ['PATH'] += os.pathsep + 'C:/Program Files (x86)/Graphviz/bin' 70 | make_dot(loss_tensor) 71 | # %% 72 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/00_LinRegFromScratch_start.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import pandas as pd 4 | import torch 5 | import torch.nn as nn 6 | import seaborn as sns 7 | 8 | #%% data import 9 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 10 | cars = pd.read_csv(cars_file) 11 | cars.head() 12 | 13 | #%% visualise the model 14 | sns.scatterplot(x='wt', y='mpg', data=cars) 15 | sns.regplot(x='wt', y='mpg', data=cars) 16 | 17 | #%% convert data to tensor 18 | 19 | 20 | #%% training 21 | 22 | #%% check results 23 | # %% 24 | 25 | # %% (Statistical) Linear Regression 26 | 27 | 28 | # %% create graph visualisation 29 | # make sure GraphViz is installed (https://graphviz.org/download/) 30 | # if not computer restarted, append directly to PATH variable 31 | # import os 32 | # from torchviz import make_dot 33 | # os.environ['PATH'] += os.pathsep + 'C:/Program Files (x86)/Graphviz/bin' 34 | # make_dot(loss_tensor) 35 | # %% 36 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/10_LinReg_ModelClass_end.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import pandas as pd 4 | import torch 5 | import torch.nn as nn 6 | import seaborn as sns 7 | 8 | #%% data import 9 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 10 | cars = pd.read_csv(cars_file) 11 | cars.head() 12 | 13 | #%% visualise the model 14 | sns.scatterplot(x='wt', y='mpg', data=cars) 15 | sns.regplot(x='wt', y='mpg', data=cars) 16 | 17 | #%% convert data to tensor 18 | X_list = cars.wt.values 19 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 20 | y_list = cars.mpg.values 21 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 22 | X = torch.from_numpy(X_np) 23 | y_true = torch.from_numpy(y_np) 24 | 25 | #%% 26 | class LinearRegressionTorch(nn.Module): 27 | def __init__(self, input_size, output_size): 28 | super(LinearRegressionTorch, self).__init__() 29 | self.linear = nn.Linear(input_size, output_size) 30 | 31 | def forward(self, x): 32 | return self.linear(x) 33 | 34 | input_dim = 1 35 | output_dim = 1 36 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 37 | 38 | 39 | # %% Mean Squared Error 40 | loss_fun = nn.MSELoss() 41 | 42 | #%% Optimizer 43 | LR = 0.02 44 | # test different values of too large 0.1 and too small 0.001 45 | # best 0.02 46 | optimizer = torch.optim.SGD(model.parameters(), lr=LR) 47 | 48 | #%% perform training 49 | losses, slope, bias = [], [], [] 50 | NUM_EPOCHS = 1000 51 | for epoch in range(NUM_EPOCHS): 52 | 53 | # set gradients to zero 54 | optimizer.zero_grad() 55 | 56 | # forward pass 57 | y_pred = model(X) 58 | 59 | # calculate loss 60 | loss = loss_fun(y_pred, y_true) 61 | loss.backward() 62 | 63 | # update parameters 64 | optimizer.step() 65 | 66 | # get parameters 67 | for name, param in model.named_parameters(): 68 | if param.requires_grad: 69 | if name == 'linear.weight': 70 | slope.append(param.data.numpy()[0][0]) 71 | if name == 'linear.bias': 72 | bias.append(param.data.numpy()[0]) 73 | 74 | 75 | # store loss 76 | losses.append(float(loss.data)) 77 | # print loss 78 | if (epoch % 100 == 0): 79 | print(f"Epoch {epoch}, Loss: {loss.data}") 80 | 81 | # %% visualise model training 82 | sns.scatterplot(x=range(NUM_EPOCHS), y=losses) 83 | 84 | #%% visualise the bias development 85 | sns.lineplot(x=range(NUM_EPOCHS), y=bias) 86 | #%% visualise the slope development 87 | sns.lineplot(x=range(NUM_EPOCHS), y=slope) 88 | 89 | # %% check the result 90 | y_pred = model(X).data.numpy().reshape(-1) 91 | sns.scatterplot(x=X_list, y=y_list) 92 | sns.lineplot(x=X_list, y=y_pred, color='red') 93 | # %% 94 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/10_LinReg_ModelClass_start.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import pandas as pd 4 | import torch 5 | import torch.nn as nn 6 | import seaborn as sns 7 | 8 | #%% data import 9 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 10 | cars = pd.read_csv(cars_file) 11 | cars.head() 12 | 13 | #%% visualise the model 14 | sns.scatterplot(x='wt', y='mpg', data=cars) 15 | sns.regplot(x='wt', y='mpg', data=cars) 16 | 17 | #%% convert data to tensor 18 | X_list = cars.wt.values 19 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 20 | y_list = cars.mpg.values 21 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 22 | X = torch.from_numpy(X_np) 23 | y_true = torch.from_numpy(y_np) 24 | 25 | #%% model class 26 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/20_LinReg_Batches_end.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import graphlib 3 | import numpy as np 4 | import pandas as pd 5 | import torch 6 | import torch.nn as nn 7 | import seaborn as sns 8 | 9 | #%% data import 10 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 11 | cars = pd.read_csv(cars_file) 12 | cars.head() 13 | 14 | #%% visualise the model 15 | sns.scatterplot(x='wt', y='mpg', data=cars) 16 | sns.regplot(x='wt', y='mpg', data=cars) 17 | 18 | #%% convert data to tensor 19 | X_list = cars.wt.values 20 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 21 | y_list = cars.mpg.values 22 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 23 | X = torch.from_numpy(X_np) 24 | y_true = torch.from_numpy(y_np) 25 | 26 | #%% 27 | class LinearRegressionTorch(nn.Module): 28 | def __init__(self, input_size, output_size): 29 | super(LinearRegressionTorch, self).__init__() 30 | self.linear = nn.Linear(input_size, output_size) 31 | 32 | def forward(self, x): 33 | return self.linear(x) 34 | 35 | input_dim = 1 36 | output_dim = 1 37 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 38 | model.train() 39 | 40 | # %% Mean Squared Error 41 | loss_fun = nn.MSELoss() 42 | 43 | #%% Optimizer 44 | learning_rate = 0.02 45 | # test different values of too large 0.1 and too small 0.001 46 | # best 0.02 47 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 48 | 49 | #%% perform training 50 | losses = [] 51 | slope, bias = [], [] 52 | NUM_EPOCHS = 1000 53 | BATCH_SIZE = 2 54 | for epoch in range(NUM_EPOCHS): 55 | for i in range(0, X.shape[0], BATCH_SIZE): 56 | # optimization 57 | optimizer.zero_grad() 58 | 59 | # forward pass 60 | y_pred = model(X[i:i+BATCH_SIZE]) 61 | 62 | # compute loss 63 | loss = loss_fun(y_pred, y_true[i:i+BATCH_SIZE]) 64 | losses.append(loss.item()) 65 | 66 | # backprop 67 | loss.backward() 68 | 69 | # update weights 70 | optimizer.step() 71 | 72 | # get parameters 73 | for name, param in model.named_parameters(): 74 | if param.requires_grad: 75 | if name == 'linear.weight': 76 | slope.append(param.data.numpy()[0][0]) 77 | if name == 'linear.bias': 78 | bias.append(param.data.numpy()[0]) 79 | 80 | 81 | # store loss 82 | losses.append(float(loss.data)) 83 | # print loss 84 | if (epoch % 100 == 0): 85 | print(f"Epoch {epoch}, Loss: {loss.data}") 86 | 87 | 88 | 89 | # %% visualise model training 90 | sns.scatterplot(x=range(len(losses)), y=losses) 91 | 92 | #%% visualise the bias development 93 | sns.lineplot(x=range(NUM_EPOCHS), y=bias) 94 | #%% visualise the slope development 95 | sns.lineplot(x=range(NUM_EPOCHS), y=slope) 96 | 97 | 98 | 99 | # %% check the result 100 | model.eval() 101 | y_pred = [i[0] for i in model(X).data.numpy()] 102 | y = [i[0] for i in y_true.data.numpy()] 103 | sns.scatterplot(x=X_list, y=y) 104 | sns.lineplot(x=X_list, y=y_pred, color='red') 105 | # %% 106 | import hiddenlayer as hl 107 | graph = hl.build_graph(model, X) 108 | # %% 109 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/20_LinReg_Batches_start.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import graphlib 3 | import numpy as np 4 | import pandas as pd 5 | import torch 6 | import torch.nn as nn 7 | import seaborn as sns 8 | 9 | #%% data import 10 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 11 | cars = pd.read_csv(cars_file) 12 | cars.head() 13 | 14 | #%% visualise the model 15 | sns.scatterplot(x='wt', y='mpg', data=cars) 16 | sns.regplot(x='wt', y='mpg', data=cars) 17 | 18 | #%% convert data to tensor 19 | X_list = cars.wt.values 20 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 21 | y_list = cars.mpg.values 22 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 23 | X = torch.from_numpy(X_np) 24 | y_true = torch.from_numpy(y_np) 25 | 26 | #%% 27 | class LinearRegressionTorch(nn.Module): 28 | def __init__(self, input_size, output_size): 29 | super(LinearRegressionTorch, self).__init__() 30 | self.linear = nn.Linear(input_size, output_size) 31 | 32 | def forward(self, x): 33 | return self.linear(x) 34 | 35 | input_dim = 1 36 | output_dim = 1 37 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 38 | model.train() 39 | 40 | # %% Mean Squared Error 41 | loss_fun = nn.MSELoss() 42 | 43 | #%% Optimizer 44 | learning_rate = 0.02 45 | # test different values of too large 0.1 and too small 0.001 46 | # best 0.02 47 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 48 | 49 | #%% perform training 50 | losses = [] 51 | slope, bias = [], [] 52 | NUM_EPOCHS = 1000 53 | BATCH_SIZE = 2 54 | for epoch in range(NUM_EPOCHS): 55 | 56 | # set gradients to zero 57 | optimizer.zero_grad() 58 | 59 | # forward pass 60 | y_pred = model(X) 61 | 62 | # calculate loss 63 | loss = loss_fun(y_pred, y_true) 64 | loss.backward() 65 | 66 | # update parameters 67 | optimizer.step() 68 | 69 | # get parameters 70 | for name, param in model.named_parameters(): 71 | if param.requires_grad: 72 | if name == 'linear.weight': 73 | slope.append(param.data.numpy()[0][0]) 74 | if name == 'linear.bias': 75 | bias.append(param.data.numpy()[0]) 76 | 77 | 78 | # store loss 79 | losses.append(float(loss.data)) 80 | # print loss 81 | if (epoch % 100 == 0): 82 | print(f"Epoch {epoch}, Loss: {loss.data}") 83 | 84 | 85 | # %% visualise model training 86 | sns.scatterplot(x=range(len(losses)), y=losses) 87 | 88 | #%% visualise the bias development 89 | sns.lineplot(x=range(NUM_EPOCHS), y=bias) 90 | #%% visualise the slope development 91 | sns.lineplot(x=range(NUM_EPOCHS), y=slope) 92 | 93 | 94 | 95 | # %% check the result 96 | model.eval() 97 | y_pred = [i[0] for i in model(X).data.numpy()] 98 | y = [i[0] for i in y_true.data.numpy()] 99 | sns.scatterplot(x=X_list, y=y) 100 | sns.lineplot(x=X_list, y=y_pred, color='red') 101 | # %% 102 | import hiddenlayer as hl 103 | graph = hl.build_graph(model, X) 104 | # %% 105 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/30_LinReg_DatasetDataloader_end.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import graphlib 3 | import numpy as np 4 | import pandas as pd 5 | import torch 6 | import torch.nn as nn 7 | from torch.utils.data import Dataset, DataLoader 8 | import seaborn as sns 9 | 10 | #%% data import 11 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 12 | cars = pd.read_csv(cars_file) 13 | cars.head() 14 | 15 | #%% visualise the model 16 | sns.scatterplot(x='wt', y='mpg', data=cars) 17 | sns.regplot(x='wt', y='mpg', data=cars) 18 | 19 | #%% convert data to tensor 20 | X_list = cars.wt.values 21 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 22 | y_list = cars.mpg.values 23 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 24 | X = torch.from_numpy(X_np) 25 | y_true = torch.from_numpy(y_np) 26 | 27 | #%% Dataset and Dataloader 28 | class LinearRegressionDataset(Dataset): 29 | def __init__(self, X, y): 30 | self.X = X 31 | self.y = y 32 | 33 | def __len__(self): 34 | return len(self.X) 35 | 36 | def __getitem__(self, idx): 37 | return self.X[idx], self.y[idx] 38 | 39 | train_loader = DataLoader(dataset = LinearRegressionDataset(X_np, y_np), batch_size=2) 40 | 41 | 42 | #%% 43 | class LinearRegressionTorch(nn.Module): 44 | def __init__(self, input_size, output_size): 45 | super(LinearRegressionTorch, self).__init__() 46 | self.linear = nn.Linear(input_size, output_size) 47 | 48 | def forward(self, x): 49 | return self.linear(x) 50 | 51 | input_dim = 1 52 | output_dim = 1 53 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 54 | model.train() 55 | 56 | # %% Mean Squared Error 57 | loss_fun = nn.MSELoss() 58 | 59 | #%% Optimizer 60 | learning_rate = 0.02 61 | # test different values of too large 0.1 and too small 0.001 62 | # best 0.02 63 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 64 | 65 | # check trainloader return 66 | # for i, data in enumerate(train_loader): 67 | # print(data) 68 | 69 | #%% perform training 70 | losses = [] 71 | slope, bias = [], [] 72 | number_epochs = 1000 73 | for epoch in range(number_epochs): 74 | for j, (X, y) in enumerate(train_loader): 75 | # optimization 76 | optimizer.zero_grad() 77 | 78 | # forward pass 79 | y_pred = model(X) 80 | 81 | # compute loss 82 | loss = loss_fun(y_pred, y) 83 | losses.append(loss.item()) 84 | 85 | # backprop 86 | loss.backward() 87 | 88 | # update weights 89 | optimizer.step() 90 | 91 | # get parameters 92 | for name, param in model.named_parameters(): 93 | if param.requires_grad: 94 | if name == 'linear.weight': 95 | slope.append(param.data.numpy()[0][0]) 96 | if name == 'linear.bias': 97 | bias.append(param.data.numpy()[0]) 98 | 99 | 100 | # store loss 101 | losses.append(float(loss.data)) 102 | # print loss 103 | if (epoch % 100 == 0): 104 | print(f"Epoch {epoch}, Loss: {loss.data}") 105 | 106 | 107 | 108 | # %% visualise model training 109 | sns.scatterplot(x=range(len(losses)), y=losses) 110 | 111 | #%% visualise the bias development 112 | sns.lineplot(x=range(number_epochs), y=bias) 113 | #%% visualise the slope development 114 | sns.lineplot(x=range(number_epochs), y=slope) 115 | 116 | 117 | 118 | # %% check the result 119 | model.eval() 120 | y_pred = [i[0] for i in model(X).data.numpy()] 121 | y = [i[0] for i in y_true.data.numpy()] 122 | sns.scatterplot(x=X_list, y=y) 123 | sns.lineplot(x=X_list, y=y_pred, color='red') 124 | # %% 125 | import hiddenlayer as hl 126 | graph = hl.build_graph(model, X) 127 | # %% 128 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/30_LinReg_DatasetDataloader_start.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import graphlib 3 | import numpy as np 4 | import pandas as pd 5 | import torch 6 | import torch.nn as nn 7 | import seaborn as sns 8 | 9 | #%% data import 10 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 11 | cars = pd.read_csv(cars_file) 12 | cars.head() 13 | 14 | #%% visualise the model 15 | sns.scatterplot(x='wt', y='mpg', data=cars) 16 | sns.regplot(x='wt', y='mpg', data=cars) 17 | 18 | #%% convert data to tensor 19 | X_list = cars.wt.values 20 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 21 | y_list = cars.mpg.values 22 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 23 | X = torch.from_numpy(X_np) 24 | y_true = torch.from_numpy(y_np) 25 | 26 | #%% 27 | class LinearRegressionTorch(nn.Module): 28 | def __init__(self, input_size, output_size): 29 | super(LinearRegressionTorch, self).__init__() 30 | self.linear = nn.Linear(input_size, output_size) 31 | 32 | def forward(self, x): 33 | return self.linear(x) 34 | 35 | input_dim = 1 36 | output_dim = 1 37 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 38 | model.train() 39 | 40 | # %% Mean Squared Error 41 | loss_fun = nn.MSELoss() 42 | 43 | #%% Optimizer 44 | learning_rate = 0.02 45 | # test different values of too large 0.1 and too small 0.001 46 | # best 0.02 47 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 48 | 49 | #%% perform training 50 | losses = [] 51 | slope, bias = [], [] 52 | NUM_EPOCHS = 1000 53 | BATCH_SIZE = 2 54 | for epoch in range(NUM_EPOCHS): 55 | for i in range(0, X.shape[0], BATCH_SIZE): 56 | # optimization 57 | optimizer.zero_grad() 58 | 59 | # forward pass 60 | y_pred = model(X[i:i+BATCH_SIZE]) 61 | 62 | # compute loss 63 | loss = loss_fun(y_pred, y_true[i:i+BATCH_SIZE]) 64 | losses.append(loss.item()) 65 | 66 | # backprop 67 | loss.backward() 68 | 69 | # update weights 70 | optimizer.step() 71 | 72 | # get parameters 73 | for name, param in model.named_parameters(): 74 | if param.requires_grad: 75 | if name == 'linear.weight': 76 | slope.append(param.data.numpy()[0][0]) 77 | if name == 'linear.bias': 78 | bias.append(param.data.numpy()[0]) 79 | 80 | 81 | # store loss 82 | losses.append(float(loss.data)) 83 | # print loss 84 | if (epoch % 100 == 0): 85 | print(f"Epoch {epoch}, Loss: {loss.data}") 86 | 87 | 88 | 89 | # %% visualise model training 90 | sns.scatterplot(x=range(len(losses)), y=losses) 91 | 92 | #%% visualise the bias development 93 | sns.lineplot(x=range(NUM_EPOCHS), y=bias) 94 | #%% visualise the slope development 95 | sns.lineplot(x=range(NUM_EPOCHS), y=slope) 96 | 97 | 98 | 99 | # %% check the result 100 | model.eval() 101 | y_pred = [i[0] for i in model(X).data.numpy()] 102 | y = [i[0] for i in y_true.data.numpy()] 103 | sns.scatterplot(x=X_list, y=y) 104 | sns.lineplot(x=X_list, y=y_pred, color='red') 105 | # %% 106 | import hiddenlayer as hl 107 | graph = hl.build_graph(model, X) 108 | # %% 109 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/40_LinReg_ModelSavingLoading_end.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import graphlib 3 | import numpy as np 4 | import pandas as pd 5 | import torch 6 | import torch.nn as nn 7 | from torch.utils.data import Dataset, DataLoader 8 | import seaborn as sns 9 | 10 | #%% data import 11 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 12 | cars = pd.read_csv(cars_file) 13 | cars.head() 14 | 15 | #%% visualise the model 16 | sns.scatterplot(x='wt', y='mpg', data=cars) 17 | sns.regplot(x='wt', y='mpg', data=cars) 18 | 19 | #%% convert data to tensor 20 | X_list = cars.wt.values 21 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 22 | y_list = cars.mpg.values 23 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 24 | X = torch.from_numpy(X_np) 25 | y_true = torch.from_numpy(y_np) 26 | 27 | #%% Dataset and Dataloader 28 | class LinearRegressionDataset(Dataset): 29 | def __init__(self, X, y): 30 | self.X = X 31 | self.y = y 32 | 33 | def __len__(self): 34 | return len(self.X) 35 | 36 | def __getitem__(self, idx): 37 | return self.X[idx], self.y[idx] 38 | 39 | train_loader = DataLoader(dataset = LinearRegressionDataset(X_np, y_np), batch_size=2) 40 | 41 | 42 | #%% Model 43 | class LinearRegressionTorch(nn.Module): 44 | def __init__(self, input_size, output_size): 45 | super(LinearRegressionTorch, self).__init__() 46 | self.linear = nn.Linear(input_size, output_size) 47 | 48 | def forward(self, x): 49 | return self.linear(x) 50 | 51 | input_dim = 1 52 | output_dim = 1 53 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 54 | model.train() 55 | 56 | # %% Mean Squared Error 57 | loss_fun = nn.MSELoss() 58 | 59 | #%% Optimizer 60 | learning_rate = 0.02 61 | # test different values of too large 0.1 and too small 0.001 62 | # best 0.02 63 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 64 | 65 | #%% perform training 66 | losses = [] 67 | slope, bias = [], [] 68 | number_epochs = 1000 69 | for epoch in range(number_epochs): 70 | for j, data in enumerate(train_loader): 71 | # optimization 72 | optimizer.zero_grad() 73 | 74 | # forward pass 75 | y_hat = model(data[0]) 76 | 77 | # compute loss 78 | loss = loss_fun(y_hat, data[1]) 79 | losses.append(loss.item()) 80 | 81 | # backprop 82 | loss.backward() 83 | 84 | # update weights 85 | optimizer.step() 86 | 87 | # get parameters 88 | for name, param in model.named_parameters(): 89 | if param.requires_grad: 90 | if name == 'linear.weight': 91 | slope.append(param.data.numpy()[0][0]) 92 | if name == 'linear.bias': 93 | bias.append(param.data.numpy()[0]) 94 | 95 | 96 | # store loss 97 | losses.append(float(loss.data)) 98 | # print loss 99 | if (epoch % 100 == 0): 100 | print(f"Epoch {epoch}, Loss: {loss.data}") 101 | 102 | # %% model state dict 103 | model.state_dict() 104 | # %% save model state dict 105 | torch.save(model.state_dict(), 'model_state_dict.pth') 106 | 107 | # %% load a model 108 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 109 | # model.state_dict() # randomly initialized 110 | model.load_state_dict(torch.load('model_state_dict.pth')) 111 | model.state_dict() 112 | # %% 113 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/40_LinReg_ModelSavingLoading_start.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import pandas as pd 4 | import torch 5 | import torch.nn as nn 6 | from torch.utils.data import Dataset, DataLoader 7 | import seaborn as sns 8 | 9 | #%% data import 10 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 11 | cars = pd.read_csv(cars_file) 12 | cars.head() 13 | 14 | #%% visualise the model 15 | sns.scatterplot(x='wt', y='mpg', data=cars) 16 | sns.regplot(x='wt', y='mpg', data=cars) 17 | 18 | #%% convert data to tensor 19 | X_list = cars.wt.values 20 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 21 | y_list = cars.mpg.values 22 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 23 | X = torch.from_numpy(X_np) 24 | y_true = torch.from_numpy(y_np) 25 | 26 | #%% Dataset and Dataloader 27 | class LinearRegressionDataset(Dataset): 28 | def __init__(self, X, y): 29 | self.X = X 30 | self.y = y 31 | 32 | def __len__(self): 33 | return len(self.X) 34 | 35 | def __getitem__(self, idx): 36 | return self.X[idx], self.y[idx] 37 | 38 | train_loader = DataLoader(dataset = LinearRegressionDataset(X_np, y_np), batch_size=2) 39 | 40 | 41 | #%% Model 42 | class LinearRegressionTorch(nn.Module): 43 | def __init__(self, input_size, output_size): 44 | super(LinearRegressionTorch, self).__init__() 45 | self.linear = nn.Linear(input_size, output_size) 46 | 47 | def forward(self, x): 48 | return self.linear(x) 49 | 50 | input_dim = 1 51 | output_dim = 1 52 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 53 | model.train() 54 | 55 | # %% Mean Squared Error 56 | loss_fun = nn.MSELoss() 57 | 58 | #%% Optimizer 59 | learning_rate = 0.02 60 | # test different values of too large 0.1 and too small 0.001 61 | # best 0.02 62 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 63 | 64 | #%% perform training 65 | losses = [] 66 | slope, bias = [], [] 67 | number_epochs = 1000 68 | for epoch in range(number_epochs): 69 | for j, data in enumerate(train_loader): 70 | # optimization 71 | optimizer.zero_grad() 72 | 73 | # forward pass 74 | y_hat = model(data[0]) 75 | 76 | # compute loss 77 | loss = loss_fun(y_hat, data[1]) 78 | losses.append(loss.item()) 79 | 80 | # backprop 81 | loss.backward() 82 | 83 | # update weights 84 | optimizer.step() 85 | 86 | # get parameters 87 | for name, param in model.named_parameters(): 88 | if param.requires_grad: 89 | if name == 'linear.weight': 90 | slope.append(param.data.numpy()[0][0]) 91 | if name == 'linear.bias': 92 | bias.append(param.data.numpy()[0]) 93 | 94 | 95 | # store loss 96 | losses.append(float(loss.data)) 97 | # print loss 98 | if (epoch % 100 == 0): 99 | print(f"Epoch {epoch}, Loss: {loss.data}") 100 | 101 | # %% model state dict 102 | 103 | # %% save model state dict 104 | 105 | # %% load a model 106 | 107 | 108 | # %% 109 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/50_LinReg_HyperparameterTuning_end.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import pandas as pd 4 | import torch 5 | import torch.nn as nn 6 | from torch.utils.data import Dataset, DataLoader 7 | import seaborn as sns 8 | from skorch import NeuralNetRegressor 9 | from sklearn.model_selection import GridSearchCV 10 | #%% data import 11 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 12 | cars = pd.read_csv(cars_file) 13 | cars.head() 14 | 15 | #%% visualise the model 16 | sns.scatterplot(x='wt', y='mpg', data=cars) 17 | sns.regplot(x='wt', y='mpg', data=cars) 18 | 19 | #%% convert data to tensor 20 | X_list = cars.wt.values 21 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 22 | y_list = cars.mpg.values 23 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 24 | X = torch.from_numpy(X_np) 25 | y_true = torch.from_numpy(y_np) 26 | 27 | #%% Dataset and Dataloader 28 | class LinearRegressionDataset(Dataset): 29 | def __init__(self, X, y): 30 | self.X = X 31 | self.y = y 32 | 33 | def __len__(self): 34 | return len(self.X) 35 | 36 | def __getitem__(self, idx): 37 | return self.X[idx], self.y[idx] 38 | 39 | train_loader = DataLoader(dataset = LinearRegressionDataset(X_np, y_np), batch_size=2) 40 | 41 | 42 | #%% 43 | class LinearRegressionTorch(nn.Module): 44 | def __init__(self, input_size=1, output_size=1): 45 | super(LinearRegressionTorch, self).__init__() 46 | self.linear = nn.Linear(input_size, output_size) 47 | 48 | def forward(self, x): 49 | return self.linear(x) 50 | 51 | input_dim = 1 52 | output_dim = 1 53 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 54 | model.train() 55 | 56 | # %% Mean Squared Error 57 | loss_fun = nn.MSELoss() 58 | 59 | #%% Optimizer 60 | learning_rate = 0.02 61 | # test different values of too large 0.1 and too small 0.001 62 | # best 0.02 63 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 64 | 65 | 66 | #%% 67 | net = NeuralNetRegressor( 68 | LinearRegressionTorch, 69 | max_epochs=10, 70 | lr=0.1, 71 | # Shuffle training data on each epoch 72 | iterator_train__shuffle=True, 73 | ) 74 | net.set_params(train_split=False, verbose=0) 75 | params = { 76 | 'lr': [0.02, 0.05, 0.08], 77 | 'max_epochs': [10, 200, 500], 78 | } 79 | gs = GridSearchCV(net, params, refit=False, cv=3, scoring='r2', verbose=2) 80 | 81 | gs.fit(X, y_true) 82 | print(f"best score: {gs.best_score_:.3f}, best params: {gs.best_params_}") 83 | 84 | 85 | # %% 86 | -------------------------------------------------------------------------------- /030_ModelingIntroduction/50_LinReg_HyperparameterTuning_start.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import pandas as pd 4 | import torch 5 | import torch.nn as nn 6 | from torch.utils.data import Dataset, DataLoader 7 | import seaborn as sns 8 | 9 | #%% data import 10 | cars_file = 'https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv' 11 | cars = pd.read_csv(cars_file) 12 | cars.head() 13 | 14 | #%% visualise the model 15 | sns.scatterplot(x='wt', y='mpg', data=cars) 16 | sns.regplot(x='wt', y='mpg', data=cars) 17 | 18 | #%% convert data to tensor 19 | X_list = cars.wt.values 20 | X_np = np.array(X_list, dtype=np.float32).reshape(-1,1) 21 | y_list = cars.mpg.values 22 | y_np = np.array(y_list, dtype=np.float32).reshape(-1,1) 23 | X = torch.from_numpy(X_np) 24 | y_true = torch.from_numpy(y_np) 25 | 26 | #%% Dataset and Dataloader 27 | class LinearRegressionDataset(Dataset): 28 | def __init__(self, X, y): 29 | self.X = X 30 | self.y = y 31 | 32 | def __len__(self): 33 | return len(self.X) 34 | 35 | def __getitem__(self, idx): 36 | return self.X[idx], self.y[idx] 37 | 38 | train_loader = DataLoader(dataset = LinearRegressionDataset(X_np, y_np), batch_size=2) 39 | 40 | 41 | #%% 42 | class LinearRegressionTorch(nn.Module): 43 | def __init__(self, input_size=1, output_size=1): 44 | super(LinearRegressionTorch, self).__init__() 45 | self.linear = nn.Linear(input_size, output_size) 46 | 47 | def forward(self, x): 48 | return self.linear(x) 49 | 50 | input_dim = 1 51 | output_dim = 1 52 | model = LinearRegressionTorch(input_size=input_dim, output_size=output_dim) 53 | model.train() 54 | 55 | # %% Mean Squared Error 56 | loss_fun = nn.MSELoss() 57 | 58 | #%% Optimizer 59 | learning_rate = 0.02 60 | # test different values of too large 0.1 and too small 0.001 61 | # best 0.02 62 | optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) 63 | 64 | 65 | #%% -------------------------------------------------------------------------------- /030_ModelingIntroduction/model_state_dict.pth: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/030_ModelingIntroduction/model_state_dict.pth -------------------------------------------------------------------------------- /060_CNN_ImageClassification/ImagePreprocessing_end.py: -------------------------------------------------------------------------------- 1 | #%% 2 | import torch 3 | from torchvision import transforms 4 | from PIL import Image 5 | import matplotlib.pyplot as plt 6 | import numpy as np 7 | # %% import image 8 | img = Image.open('kiki.jpg') 9 | img 10 | 11 | # %% compose a series of steps 12 | preprocess_steps = transforms.Compose([ 13 | transforms.Resize(300), # better (300, 300) 14 | transforms.RandomRotation(50), 15 | transforms.CenterCrop(500), 16 | transforms.Grayscale(), 17 | transforms.RandomVerticalFlip(), 18 | transforms.ToTensor(), 19 | transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), # ImageNet values 20 | ]) 21 | x = preprocess_steps(img) 22 | x 23 | 24 | # %% get the mean and std of given image 25 | x.mean([1, 2]), x.std([1, 2]) -------------------------------------------------------------------------------- /060_CNN_ImageClassification/ImagePreprocessing_start.py: -------------------------------------------------------------------------------- 1 | #%% 2 | import torch 3 | from torchvision import transforms 4 | from PIL import Image 5 | import matplotlib.pyplot as plt 6 | import numpy as np 7 | # %% import image 8 | img = Image.open('kiki.jpg') 9 | img 10 | 11 | # %% compose a series of steps 12 | -------------------------------------------------------------------------------- /060_CNN_ImageClassification/LayerCalculations_end.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | from typing import OrderedDict 3 | import torch 4 | import torch.nn as nn 5 | 6 | #%% sample input data of certain shape 7 | input = torch.rand((1, 3, 32, 32)) # BS, C, H, W 8 | # %% 9 | model = nn.Sequential(OrderedDict([ 10 | ('conv1', nn.Conv2d(3, 8, 3)), # out: (BS, 8, 30, 30) 11 | ('relu1', nn.ReLU()), 12 | ('pool', nn.MaxPool2d(2, 2)), # out: (BS, 8, 15, 15) 13 | ('conv2', nn.Conv2d(8, 16, 3)), # out: (BS, 16, 13, 13) 14 | ('relu2', nn.ReLU()), 15 | ('pool2', nn.MaxPool2d(2, 2)), # out: (BS, 16, 6, 6) 16 | ('flatten', nn.Flatten()), # shape: (3, 16*6*6) 17 | ('fc1', nn.Linear(16 * 6 * 6, 127)), 18 | ('relu3', nn.ReLU()), 19 | ('fc2', nn.Linear(128, 64)), 20 | ('relu4', nn.ReLU()), 21 | ('fc3', nn.Linear(64, 1)), 22 | ('sigmoid', nn.Sigmoid()) 23 | ])) 24 | 25 | # %% test the model setup 26 | 27 | model(input).shape 28 | # %% 29 | -------------------------------------------------------------------------------- /060_CNN_ImageClassification/LayerCalculations_start.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | from typing import OrderedDict 3 | import torch 4 | import torch.nn as nn 5 | 6 | #%% sample input data of certain shape 7 | 8 | # %% 9 | model = nn.Sequential(OrderedDict([ 10 | ('conv1', nn.Conv2d(3, 8, 3)), # out: (BS, 8, 30, 30) 11 | ('relu1', nn.ReLU()), 12 | ('pool', nn.MaxPool2d(2, 2)), # out: (BS, 8, 15, 15) 13 | ('conv2', nn.Conv2d(8, 16, 3)), # out: (BS, 16, 13, 13) 14 | ('relu2', nn.ReLU()), 15 | ('pool2', nn.MaxPool2d(2, 2)), # out: (BS, 16, 6, 6) 16 | ('flatten', nn.Flatten()), # shape: (3, 16*6*6) 17 | ('fc1', nn.Linear(16 * 6 * 6, 127)), 18 | ('relu3', nn.ReLU()), 19 | ('fc2', nn.Linear(128, 64)), 20 | ('relu4', nn.ReLU()), 21 | ('fc3', nn.Linear(64, 1)), 22 | ('sigmoid', nn.Sigmoid()) 23 | ])) 24 | 25 | # %% test the model setup 26 | -------------------------------------------------------------------------------- /060_CNN_ImageClassification/kiki.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/060_CNN_ImageClassification/kiki.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_001.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_002.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_003.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_004.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_005.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_006.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_007.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_008.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/images/image_part_009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/images/image_part_009.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_001.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_002.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_003.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_004.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_005.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_006.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_007.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_008.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 1/masks/image_part_009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 1/masks/image_part_009.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_001.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_002.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_003.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_004.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_005.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_006.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_007.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_008.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/images/image_part_009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/images/image_part_009.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_001.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_002.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_003.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_004.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_005.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_006.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_007.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_008.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 2/masks/image_part_009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 2/masks/image_part_009.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_001.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_002.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_003.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_004.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_005.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_006.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_007.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_008.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/images/image_part_009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/images/image_part_009.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_001.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_002.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_003.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_004.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_005.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_006.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_007.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_008.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 3/masks/image_part_009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 3/masks/image_part_009.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_001.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_002.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_003.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_004.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_005.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_006.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_007.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_008.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/images/image_part_009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/images/image_part_009.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_001.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_002.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_003.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_004.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_005.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_006.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_007.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_008.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 4/masks/image_part_009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 4/masks/image_part_009.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_001.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_002.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_003.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_004.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_005.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_006.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_007.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_008.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/images/image_part_009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/images/image_part_009.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_001.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_002.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_003.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_004.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_005.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_006.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_007.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_008.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 5/masks/image_part_009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 5/masks/image_part_009.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_001.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_002.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_003.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_004.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_005.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_006.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_007.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_008.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/images/image_part_009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/images/image_part_009.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_001.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_002.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_003.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_004.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_005.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_006.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_007.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_008.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 6/masks/image_part_009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 6/masks/image_part_009.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_001.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_002.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_003.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_004.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_005.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_006.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_007.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_008.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/images/image_part_009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/images/image_part_009.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_001.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_002.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_003.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_004.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_005.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_006.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_007.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_008.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 7/masks/image_part_009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 7/masks/image_part_009.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_001.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_002.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_003.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_004.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_005.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_006.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_007.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_008.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/images/image_part_009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/images/image_part_009.jpg -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_001.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_002.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_003.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_004.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_004.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_005.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_005.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_006.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_006.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_007.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_008.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/Tile 8/masks/image_part_009.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/100_SemanticSegmentation/data/Tile 8/masks/image_part_009.png -------------------------------------------------------------------------------- /100_SemanticSegmentation/data/classes.json: -------------------------------------------------------------------------------- 1 | {"classes": [{"title": "Water", "shape": "polygon", "color": "#50E3C2", "geometry_config": {}}, {"title": "Land (unpaved area)", "shape": "polygon", "color": "#F5A623", "geometry_config": {}}, {"title": "Road", "shape": "polygon", "color": "#DE597F", "geometry_config": {}}, {"title": "Building", "shape": "polygon", "color": "#D0021B", "geometry_config": {}}, {"title": "Vegetation", "shape": "polygon", "color": "#417505", "geometry_config": {}}, {"title": "Unlabeled", "shape": "polygon", "color": "#9B9B9B", "geometry_config": {}}], "tags": []} -------------------------------------------------------------------------------- /100_SemanticSegmentation/dataprep_final.py: -------------------------------------------------------------------------------- 1 | # %% Purpose: 2 | # 1. Prepare images and masks folder within train, val, test folder 3 | # 2. copy all images into these folders 4 | # 3. create patches of images 5 | #%% Packages 6 | import os 7 | import re 8 | from pathlib import Path 9 | import numpy as np 10 | from patchify import patchify 11 | from PIL import Image 12 | #%% Create empty folders if necessary 13 | def create_folders(): 14 | FOLDERS = ['train', 'val', 'test'] 15 | for folder in FOLDERS: 16 | if not os.path.exists(folder): 17 | folder_imgs = f"{folder}/images" 18 | folder_msks = f"{folder}/masks" 19 | os.makedirs(folder_imgs) if not os.path.exists(folder_imgs) else print('folder already exists') 20 | os.makedirs(folder_msks) if not os.path.exists(folder_msks) else print('folder already exists') 21 | 22 | create_folders() 23 | 24 | #%% create patches 25 | # %% PATCHES 26 | def create_patches(src, dest_path): 27 | path_split = os.path.split(src) 28 | tile_num = re.findall(r'\d+', path_split[0])[0] 29 | 30 | image = Image.open(src) 31 | image = np.asarray(image) 32 | if len(image.shape) > 2: # only if color channel exists as well 33 | patches = patchify(image, (320, 320, 3), step=300) 34 | file_name_wo_ext = Path(src).stem 35 | for i in range(patches.shape[0]): 36 | for j in range(patches.shape[1]): 37 | patch = patches[i, j, 0] 38 | patch = Image.fromarray(patch) 39 | num = i * patches.shape[1] + j 40 | patch.save(f"{dest_path}/{file_name_wo_ext}_tile_{tile_num}_patch_{num}.png") 41 | 42 | #%% copy all files 43 | for path_name, _, file_name in os.walk('data'): 44 | for f in file_name: 45 | print(f) 46 | if f != 'classes.json': 47 | 48 | path_split = os.path.split(path_name) 49 | tile_num = re.findall(r'\d+', path_split[0])[0] 50 | 51 | img_type =path_split[1] # either 'masks' or 'images' 52 | 53 | # leave out tile 2, issues with color dim 54 | if tile_num == '3': 55 | target_folder_imgs = 'val' 56 | target_folder_masks = 'val' 57 | elif tile_num == '1': 58 | target_folder_imgs = 'test' 59 | target_folder_masks = 'test' 60 | elif tile_num in ['4', '5', '6', '7', '8']: 61 | target_folder_imgs = 'train' 62 | target_folder_masks = 'train' 63 | 64 | # copy all images 65 | src = os.path.join(path_name, f) 66 | file_name_wo_ext = Path(src).stem 67 | # check if file exists in images and masks 68 | img_file = f"{path_split[0]}/images/{file_name_wo_ext}.jpg" 69 | mask_file = f"{path_split[0]}/masks/{file_name_wo_ext}.png" 70 | if os.path.exists(img_file) and os.path.exists(mask_file): 71 | if img_type == 'images': 72 | dest = os.path.join(target_folder_imgs, img_type) 73 | create_patches(src=src, dest_path=dest) 74 | 75 | 76 | 77 | # copy all masks 78 | if img_type == 'masks': 79 | dest = os.path.join(target_folder_masks, img_type) 80 | create_patches(src=src, dest_path=dest) 81 | 82 | # %% 83 | -------------------------------------------------------------------------------- /100_SemanticSegmentation/model_inference_final.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import torch 5 | from torch.utils.data import DataLoader 6 | from sem_seg_dataset import SegmentationDataset 7 | import segmentation_models_pytorch as smp 8 | import torchmetrics 9 | 10 | #%% Dataset and Dataloader 11 | test_ds = SegmentationDataset(path_name='test') 12 | test_dataloader = DataLoader(test_ds, batch_size=1, shuffle=True) 13 | 14 | #%% 15 | 16 | DEVICE = "cuda" if torch.cuda.is_available() else "cpu" 17 | #%% Model setup 18 | model = smp.FPN( 19 | encoder_name='se_resnext50_32x4d', 20 | encoder_weights='imagenet', 21 | classes=6, 22 | activation='sigmoid', 23 | ) 24 | model.to(DEVICE) 25 | 26 | #%% load weights 27 | model.load_state_dict(torch.load('models/FPN_epochs_50_CEloss_statedict.pth')) 28 | 29 | #%% Model Evaluation 30 | pixel_accuracies = [] 31 | intersection_over_unions = [] 32 | metric_iou = torchmetrics.JaccardIndex(num_classes=6, task='multiclass').to(DEVICE) 33 | 34 | with torch.no_grad(): 35 | for data in test_dataloader: 36 | inputs, outputs = data 37 | true = outputs.to(torch.float32) 38 | pred = model(inputs.to(DEVICE).float()) 39 | _, predicted = torch.max(pred, 1) 40 | true = true.to(DEVICE) 41 | correct_pixels = (true == predicted).sum().item() 42 | total_pixels = true.size(1) * true.size(2) 43 | pixel_accuracies.append(correct_pixels / total_pixels) 44 | iou = metric_iou(predicted.float(), true).item() 45 | intersection_over_unions.append(iou) 46 | 47 | #%% Median Accuracy 48 | print(f"Median Pixel Accuracy: {np.median(pixel_accuracies) * 100 }") 49 | print(f"Median IoU: {np.median(intersection_over_unions) * 100 }") 50 | 51 | #%% Pick a test image and show it 52 | image_test, mask = next(iter(test_dataloader)) 53 | plt.imshow(np.transpose(image_test[0, :, :, :].cpu().numpy(), (1, 2, 0))) 54 | 55 | #%% EVALUATE MODEL 56 | # create preds 57 | with torch.no_grad(): 58 | image_test = image_test.float().to(DEVICE) 59 | output = model(image_test) 60 | 61 | #%% 62 | output_cpu = output.cpu().squeeze().numpy() 63 | output_cpu = output_cpu.transpose((1, 2, 0)) 64 | output_cpu = output_cpu.argmax(axis=2) 65 | output_cpu.shape 66 | #%% trick to cover all classes 67 | # use at least one pixel for each class for both images 68 | required_range = list(range(6)) 69 | output_cpu[0, 0] = 0 70 | output_cpu[0, 1] = 1 71 | output_cpu[0, 2] = 2 72 | output_cpu[0, 3] = 3 73 | output_cpu[0, 4] = 4 74 | output_cpu[0, 5] = 5 75 | 76 | mask[0, 0, 0] = 0 77 | mask[0, 0, 1] = 1 78 | mask[0, 0, 2] = 2 79 | mask[0, 0, 3] = 3 80 | mask[0, 0, 4] = 4 81 | mask[0, 0, 5] = 5 82 | #%% 83 | fig, axs = plt.subplots(nrows=1, ncols=2) 84 | fig.suptitle('True and Predicted Mask') 85 | axs[0].imshow(mask[0, :, :]) 86 | axs[1].imshow(output_cpu) 87 | axs[0].set_title("True Mask") 88 | axs[1].set_title("Predicted Mask") 89 | plt.show() 90 | # %% 91 | -------------------------------------------------------------------------------- /100_SemanticSegmentation/model_inference_start.py: -------------------------------------------------------------------------------- 1 | #%% packages 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import torch 5 | from torch.utils.data import DataLoader 6 | from sem_seg_dataset import SegmentationDataset 7 | import segmentation_models_pytorch as smp 8 | import torchmetrics 9 | # %% Dataset and Dataloader 10 | -------------------------------------------------------------------------------- /100_SemanticSegmentation/modeling_final.py: -------------------------------------------------------------------------------- 1 | #%% 2 | import numpy as np 3 | import torch 4 | import torch.nn as nn 5 | from torch.utils.data import DataLoader 6 | from sem_seg_dataset import SegmentationDataset 7 | import segmentation_models_pytorch as smp 8 | import seaborn as sns 9 | import matplotlib.pyplot as plt 10 | 11 | #%% Constants 12 | DEVICE = "cuda" if torch.cuda.is_available() else "cpu" 13 | 14 | #%% Hyperparameters 15 | EPOCHS = 5 16 | BS = 4 17 | 18 | #%% Instantiate Dataset and Dataloader 19 | train_ds = SegmentationDataset(path_name='train') 20 | train_dataloader = DataLoader(train_ds, batch_size=BS, shuffle=True) 21 | val_ds = SegmentationDataset(path_name='val') 22 | val_dataloader = DataLoader(val_ds, batch_size=BS, shuffle=True) 23 | 24 | #%% HYPERPARAMETERS 25 | 26 | model = smp.FPN( 27 | encoder_name='se_resnext50_32x4d', 28 | encoder_weights='imagenet', 29 | classes=6, 30 | activation='sigmoid', 31 | ) 32 | model.to(DEVICE) 33 | 34 | optimizer = torch.optim.Adam([ 35 | dict(params=model.parameters(), lr=0.0001), 36 | ]) 37 | 38 | #%% TRAIN MODEL 39 | criterion = nn.CrossEntropyLoss() 40 | # criterion = smp.losses.DiceLoss(mode='multiclass') 41 | train_losses, val_losses = [], [] 42 | 43 | #%% 44 | for e in range(EPOCHS): 45 | model.train() 46 | running_train_loss, running_val_loss = 0, 0 47 | for i, data in enumerate(train_dataloader): 48 | #training phase 49 | image_i, mask_i = data 50 | image = image_i.to(DEVICE) 51 | mask = mask_i.to(DEVICE) 52 | 53 | # reset gradients 54 | optimizer.zero_grad() 55 | #forward 56 | output = model(image.float()) 57 | 58 | # calc losses 59 | train_loss = criterion(output.float(), mask.long()) 60 | 61 | # back propagation 62 | train_loss.backward() 63 | optimizer.step() #update weight 64 | 65 | running_train_loss += train_loss.item() 66 | train_losses.append(running_train_loss) 67 | 68 | # validation 69 | model.eval() 70 | with torch.no_grad(): 71 | for i, data in enumerate(val_dataloader): 72 | image_i, mask_i = data 73 | image = image_i.to(DEVICE) 74 | mask = mask_i.to(DEVICE) 75 | #forward 76 | output = model(image.float()) 77 | # calc losses 78 | val_loss = criterion(output.float(), mask.long()) 79 | running_val_loss += val_loss.item() 80 | val_losses.append(running_val_loss) 81 | 82 | 83 | print(f"Epoch: {e}: Train Loss: {np.median(running_train_loss)}, Val Loss: {np.median(running_val_loss)}") 84 | 85 | #%% TRAIN LOSS 86 | sns.lineplot(x = range(len(train_losses)), y= train_losses).set(title='Train Loss') 87 | plt.show() 88 | sns.lineplot(x = range(len(train_losses)), y= val_losses).set(title='Validation Loss') 89 | plt.show() 90 | 91 | # %% save model 92 | torch.save(model.state_dict(), f'models/FPN_epochs_{EPOCHS}_crossentropy_state_dict.pth') -------------------------------------------------------------------------------- /100_SemanticSegmentation/modeling_start.py: -------------------------------------------------------------------------------- 1 | #%% 2 | import numpy as np 3 | import torch 4 | import torch.nn as nn 5 | from torch.utils.data import DataLoader 6 | from sem_seg_dataset import SegmentationDataset 7 | import segmentation_models_pytorch as smp 8 | import seaborn as sns 9 | import matplotlib.pyplot as plt 10 | -------------------------------------------------------------------------------- /100_SemanticSegmentation/sem_seg_dataset.py: -------------------------------------------------------------------------------- 1 | from torch.utils.data import Dataset 2 | import os 3 | from pathlib import Path 4 | import cv2 5 | 6 | class SegmentationDataset(Dataset): 7 | """Create Semantic Segmentation Dataset. Read images, apply augmentations, and process transformations 8 | 9 | Args: 10 | Dataset (image): Aerial Drone Images 11 | """ 12 | # CLASSES = {'building': 44, 'land': 91, 'road':172, 'vegetation':212, 'water':171, 'unlabeled':155} 13 | # CLASSES_KEYS = list(CLASSES.keys()) 14 | 15 | def __init__(self, path_name) -> None: 16 | super().__init__() 17 | self.image_names = os.listdir(f"{path_name}/images") 18 | self.image_paths = [f"{path_name}/images/{i}" for i in self.image_names] 19 | self.masks_names = os.listdir(f"{path_name}/masks") 20 | self.masks_paths = [f"{path_name}/masks/{i}" for i in self.masks_names] 21 | 22 | # filter all images that do not exist in both folders 23 | self.img_stem = [Path(i).stem for i in self.image_paths] 24 | self.msk_stem = [Path(i).stem for i in self.masks_paths] 25 | self.img_msk_stem = set(self.img_stem) & set(self.msk_stem) 26 | self.image_paths = [i for i in self.image_paths if (Path(i).stem in self.img_msk_stem)] 27 | 28 | 29 | def convert_mask(self, mask): 30 | mask[mask == 155] = 0 # unlabeled 31 | mask[mask == 44] = 1 # building 32 | mask[mask == 91] = 2 # land 33 | mask[mask == 171] = 3 # water 34 | mask[mask == 172] = 4 # road 35 | mask[mask == 212] = 5 # vegetation 36 | return mask 37 | 38 | def __len__(self): 39 | return len(self.img_msk_stem) 40 | 41 | def __getitem__(self, index): 42 | image = cv2.imread(self.image_paths[index]) 43 | image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 44 | image = image.transpose((2, 0, 1)) #structure: BS, C, H, W 45 | mask = cv2.imread(self.masks_paths[index], 0) 46 | mask = self.convert_mask(mask) 47 | return image, mask 48 | -------------------------------------------------------------------------------- /10_SemanticSegmentation_101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/10_SemanticSegmentation_101.pdf -------------------------------------------------------------------------------- /15_CourseStructure.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/15_CourseStructure.pdf -------------------------------------------------------------------------------- /20_Architectures.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/20_Architectures.pdf -------------------------------------------------------------------------------- /30_Upsampling.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/30_Upsampling.pdf -------------------------------------------------------------------------------- /40_LossFunctions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/40_LossFunctions.pdf -------------------------------------------------------------------------------- /50_SemanticSegmentation_Lab.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/50_SemanticSegmentation_Lab.pdf -------------------------------------------------------------------------------- /60_Evaluation Metrics.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/60_Evaluation Metrics.pdf -------------------------------------------------------------------------------- /60_SemanticSegmentation_DataPrep.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/60_SemanticSegmentation_DataPrep.pdf -------------------------------------------------------------------------------- /Model HyperparameterTuning.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataScienceHamburg/ImageSegmentationCourseMaterial/5a0a2cde354de68dff1fea4d6fe0f3b5367c85ce/Model HyperparameterTuning.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ImageSegmentationCourseMaterial 2 | This repo holds the code for the Udemy course: "Mastering Image Segmentation with PyTorch" 3 | --------------------------------------------------------------------------------