#!/usr/bin/env python3
import pdb
import gym
import torch as th
import torch.nn as nn
import torchvision
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
[docs]class CustomResnet18CNN(BaseFeaturesExtractor):
"""
:param observation_space: (gym.Space)
:param features_dim: (int) Number of features extracted.
This corresponds to the number of unit for the last layer.
"""
def __init__(self, observation_space: gym.spaces.Box, features_dim: int = 256):
super(CustomResnet18CNN, self).__init__(observation_space, features_dim)
# We assume CxHxW images (channels first)
# Re-ordering will be done by pre-preprocessing or wrapper
## pretrain set false;
#self.cnn = torchvision.models.resnet18(weights=torchvision.models.ResNet18_Weights.DEFAULT)
n_input_channels = observation_space.shape[0]
print("N_input_channels", n_input_channels)
self.cnn = ResNet_18(n_input_channels, features_dim)
with th.no_grad():
n_flatten = self.cnn(
th.as_tensor(observation_space.sample()[None]).float()
).shape[1]
self.linear = nn.Sequential(nn.Linear(n_flatten, features_dim), nn.ReLU())
[docs] def forward(self, observations: th.Tensor) -> th.Tensor:
# Cut off image
# reshape to from vector to W*H
# gray to color transform
# application of ResNet
# Concat features to the rest of observation vector
# return
return self.linear(self.cnn(observations))
## reference - online
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels, identity_downsample=None, stride=1):
super(ResBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU()
self.identity_downsample = identity_downsample
def forward(self, x):
identity = x
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.conv2(x)
x = self.bn2(x)
if self.identity_downsample is not None:
identity = self.identity_downsample(identity)
x += identity
x = self.relu(x)
return x
class ResNet_18(nn.Module):
def __init__(self, image_channels, num_classes):
super(ResNet_18, self).__init__()
self.in_channels = 64
self.conv1 = nn.Conv2d(image_channels, 64, kernel_size=7, stride=2, padding=3)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
#resnet layers
self.layer1 = self.__make_layer(64, 64, stride=1)
self.layer2 = self.__make_layer(64, 128, stride=2)
self.layer3 = self.__make_layer(128, 256, stride=2)
self.layer4 = self.__make_layer(256, 512, stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, num_classes)
def __make_layer(self, in_channels, out_channels, stride):
identity_downsample = None
if stride != 1:
identity_downsample = self.identity_downsample(in_channels, out_channels)
return nn.Sequential(
ResBlock(in_channels, out_channels, identity_downsample=identity_downsample, stride=stride),
ResBlock(out_channels, out_channels)
)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.shape[0], -1)
x = self.fc(x)
return x
def identity_downsample(self, in_channels, out_channels):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(out_channels)
)