Skip to content

Commit

Permalink
Trying to add resnet18 but 1D to model
Browse files Browse the repository at this point in the history
  • Loading branch information
anilsson committed Mar 1, 2023
1 parent 60d77db commit 2ab1cc8
Show file tree
Hide file tree
Showing 8 changed files with 48,366 additions and 0 deletions.
1 change: 1 addition & 0 deletions data
26 changes: 26 additions & 0 deletions experiments/eval_test_AN.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
#SBATCH --partition=csedu
#SBATCH --gres=gpu:1
#SBATCH --mem=10G
#SBATCH --cpus-per-task=6
#SBATCH --time=6:00:00
#SBATCH --output=./logs/slurm/%J.out
#SBATCH --error=./logs/slurm/%J.err
#only use this if you want to send the mail to another team member #SBATCH --mail-user=teammember
#SBATCH --mail-type=BEGIN,END,FAIL

# location of repository and data
project_dir=.
shard_folder=./data/tiny-voxceleb-shards/
val_trials_path=./data/tiny-voxceleb/val_trials.txt
dev_trials_path=./data/tiny-voxceleb/dev_trials.txt

# execute train CLI


source "$project_dir"/venv/bin/activate
./cli_evaluate.py \
logs/lightning_logs/version_2023_02_24___00_02_28___job_id_15379/checkpoints/epoch_0027.step_000001092.val-eer_0.1586.best.ckpt \
data/tiny-voxceleb-shards/dev/,data/tiny-voxceleb-shards/eval \
data/tiny-voxceleb/dev_trials.txt,data/tiny-voxceleb/eval_trials_no_gt.txt \
scores.txt
1 change: 1 addition & 0 deletions logs
47,611 changes: 47,611 additions & 0 deletions scores.txt

Large diffs are not rendered by default.

146 changes: 146 additions & 0 deletions skeleton/models/basicblock_AN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#source: https://pytorch.org/vision/main/_modules/torchvision/models/resnet.html#resnet18

from torch import nn
from typing import Any, Callable, List, Optional, Type, Union


def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> nn.Conv2d:
"""3x3 convolution with padding"""
return nn.Conv2d(
in_planes,
out_planes,
kernel_size=3,
stride=stride,
padding=dilation,
groups=groups,
bias=False,
dilation=dilation,
)

def conv1x1(in_planes: int, out_planes: int, stride: int = 1) -> nn.Conv2d:
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)


class BasicBlock(nn.Module):
expansion: int = 1

def __init__(
self,
inplanes: int,
planes: int,
stride: int = 1,
downsample: Optional[nn.Module] = None,
groups: int = 1,
base_width: int = 64,
dilation: int = 1,
norm_layer: Optional[Callable[..., nn.Module]] = None,
) -> None:
super().__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or base_width != 64:
raise ValueError("BasicBlock only supports groups=1 and base_width=64")
if dilation > 1:
raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
# Both self.conv1 and self.downsample layers downsample the input when stride != 1
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = norm_layer(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = norm_layer(planes)
self.downsample = downsample
self.stride = stride

def forward(self, x):
identity = x

out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)

out = self.conv2(out)
out = self.bn2(out)

if self.downsample is not None:
identity = self.downsample(x)
print(out.shape)
out += identity
out = self.relu(out)

return out


# class Bottleneck(nn.Module):
# # Bottleneck in torchvision places the stride for downsampling at 3x3 convolution(self.conv2)
# # while original implementation places the stride at the first 1x1 convolution(self.conv1)
# # according to "Deep residual learning for image recognition" https://arxiv.org/abs/1512.03385.
# # This variant is also known as ResNet V1.5 and improves accuracy according to
# # https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch.

# expansion: int = 4

# def __init__(
# self,
# inplanes: int,
# planes: int,
# stride: int = 1,
# downsample: Optional[nn.Module] = None,
# groups: int = 1,
# base_width: int = 64,
# dilation: int = 1,
# norm_layer: Optional[Callable[..., nn.Module]] = None,
# ) -> None:
# super().__init__()
# if norm_layer is None:
# norm_layer = nn.BatchNorm2d
# width = int(planes * (base_width / 64.0)) * groups
# # Both self.conv2 and self.downsample layers downsample the input when stride != 1
# self.conv1 = conv1x1(inplanes, width)
# self.bn1 = norm_layer(width)
# self.conv2 = conv3x3(width, width, stride, groups, dilation)
# self.bn2 = norm_layer(width)
# self.conv3 = conv1x1(width, planes * self.expansion)
# self.bn3 = norm_layer(planes * self.expansion)
# self.relu = nn.ReLU(inplace=True)
# self.downsample = downsample
# self.stride = stride

# def forward(self, x: Tensor) -> Tensor:
# identity = x

# out = self.conv1(x)
# out = self.bn1(out)
# out = self.relu(out)

# out = self.conv2(out)
# out = self.bn2(out)
# out = self.relu(out)

# out = self.conv3(out)
# out = self.bn3(out)

# if self.downsample is not None:
# identity = self.downsample(x)
# print(out.shape)
# out += identity
# out = self.relu(out)

# return out

if __name__ == "__main__":
import torch
import numpy as np

#norm_layer(planes * block.expansion) #rm

# downsampling so input and output are same shape.
downsample = nn.Sequential(
conv1x1(3, 15 * 1, 1),
)

testblock=BasicBlock(3,15, downsample=downsample)
#testinp=torch.tensor(np.random.randn((5,5,5))) # 3 dim tensor from np array, can also be done with torch
testinp=torch.randn((1,3,5,5)) # (batch) channels width height

testblock.forward(testinp)
148 changes: 148 additions & 0 deletions skeleton/models/basicblock_AN_1D.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#source: https://pytorch.org/vision/main/_modules/torchvision/models/resnet.html#resnet18

from torch import nn
from typing import Any, Callable, List, Optional, Type, Union


# change to returns 1d instead. then modify code to use 1 D.
# doc for conv 1d
def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> nn.Conv2d:
"""3x3 convolution with padding"""
return nn.Conv2d(
in_planes,
out_planes,
kernel_size=3,
stride=stride,
padding=dilation,
groups=groups,
bias=False,
dilation=dilation,
)

def conv1x1(in_planes: int, out_planes: int, stride: int = 1) -> nn.Conv2d:
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)


class BasicBlock(nn.Module):
expansion: int = 1

def __init__(
self,
inplanes: int,
planes: int,
stride: int = 1,
downsample: Optional[nn.Module] = None,
groups: int = 1,
base_width: int = 64,
dilation: int = 1,
norm_layer: Optional[Callable[..., nn.Module]] = None,
) -> None:
super().__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or base_width != 64:
raise ValueError("BasicBlock only supports groups=1 and base_width=64")
if dilation > 1:
raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
# Both self.conv1 and self.downsample layers downsample the input when stride != 1
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = norm_layer(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = norm_layer(planes)
self.downsample = downsample
self.stride = stride

def forward(self, x):
identity = x

out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)

out = self.conv2(out)
out = self.bn2(out)

if self.downsample is not None:
identity = self.downsample(x)
print(out.shape)
out += identity
out = self.relu(out)

return out


# class Bottleneck(nn.Module):
# # Bottleneck in torchvision places the stride for downsampling at 3x3 convolution(self.conv2)
# # while original implementation places the stride at the first 1x1 convolution(self.conv1)
# # according to "Deep residual learning for image recognition" https://arxiv.org/abs/1512.03385.
# # This variant is also known as ResNet V1.5 and improves accuracy according to
# # https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch.

# expansion: int = 4

# def __init__(
# self,
# inplanes: int,
# planes: int,
# stride: int = 1,
# downsample: Optional[nn.Module] = None,
# groups: int = 1,
# base_width: int = 64,
# dilation: int = 1,
# norm_layer: Optional[Callable[..., nn.Module]] = None,
# ) -> None:
# super().__init__()
# if norm_layer is None:
# norm_layer = nn.BatchNorm2d
# width = int(planes * (base_width / 64.0)) * groups
# # Both self.conv2 and self.downsample layers downsample the input when stride != 1
# self.conv1 = conv1x1(inplanes, width)
# self.bn1 = norm_layer(width)
# self.conv2 = conv3x3(width, width, stride, groups, dilation)
# self.bn2 = norm_layer(width)
# self.conv3 = conv1x1(width, planes * self.expansion)
# self.bn3 = norm_layer(planes * self.expansion)
# self.relu = nn.ReLU(inplace=True)
# self.downsample = downsample
# self.stride = stride

# def forward(self, x: Tensor) -> Tensor:
# identity = x

# out = self.conv1(x)
# out = self.bn1(out)
# out = self.relu(out)

# out = self.conv2(out)
# out = self.bn2(out)
# out = self.relu(out)

# out = self.conv3(out)
# out = self.bn3(out)

# if self.downsample is not None:
# identity = self.downsample(x)
# print(out.shape)
# out += identity
# out = self.relu(out)

# return out

if __name__ == "__main__":
import torch
import numpy as np

#norm_layer(planes * block.expansion) #rm

# downsampling so input and output are same shape.
downsample = nn.Sequential(
conv1x1(3, 15 * 1, 1),
)

testblock=BasicBlock(3,15, downsample=downsample)
#testinp=torch.tensor(np.random.randn((5,5,5))) # 3 dim tensor from np array, can also be done with torch
testinp=torch.randn((1,3,5,5)) # (batch) channels width height

testblock.forward(testinp)
Loading

0 comments on commit 2ab1cc8

Please sign in to comment.