Skip to content

Instantly share code, notes, and snippets.

View johnolafenwa's full-sized avatar
💭
Building the future!

John Olafenwa johnolafenwa

💭
Building the future!
View GitHub Profile
# A single resnet module consisting of 1 x 1 conv - 3 x 3 conv and 1 x 1 conv
def resnet_module(x, filters, pool=False):
res = x
stride = 1
if pool:
stride = 2
res = Conv2D(filters, kernel_size=1, strides=2, padding="same")(res)
res = BatchNormalization()(res)
x = Conv2D(int(filters / 4), kernel_size=1, strides=stride, padding="same")(x)
#A resnet block consisting of N number of resnet modules, first layer has is pooled.
def resnet_block(x,filters,num_layers,pool_first_layer=True):
for i in range(num_layers):
pool = False
if i == 0 and pool_first_layer: pool = True
x = resnet_module(x,filters=filters,pool=pool)
return x
import keras
from keras.layers import *
from keras.models import Model
# A single resnet module consisting of 1 x 1 conv - 3 x 3 conv and 1 x 1 conv
def resnet_module(x, filters, pool=False):
res = x
stride = 1
if pool:
stride = 2
# A single resnet module consisting of 1 x 1 conv - 3 x 3 conv and 1 x 1 conv
def resnet_identity_module(x, filters, pool=False):
res = x
stride = 1
if pool:
stride = 2
res = Conv2D(filters, kernel_size=1, strides=2, padding="same")(res)
x = BatchNormalization()(x)
x = Activation("relu")(x)
import keras
from keras.layers import *
from keras.models import Model
# A single resnet module consisting of 1 x 1 conv - 3 x 3 conv and 1 x 1 conv
def resnet_identity_module(x, filters, pool=False):
res = x
stride = 1
if pool:
stride = 2
# Import needed packages
import torch
import torch.nn as nn
class SimpleNet(nn.Module):
def __init__(self, num_classes=10):
super(SimpleNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=3, stride=1, padding=1)
class Unit(nn.Module):
def __init__(self, in_channels, out_channels):
super(Unit, self).__init__()
self.conv = nn.Conv2d(in_channels=in_channels, kernel_size=3, out_channels=out_channels, stride=1, padding=1)
self.bn = nn.BatchNorm2d(num_features=out_channels)
self.relu = nn.ReLU()
def forward(self, input):
output = self.conv(input)
class Unit(nn.Module):
def __init__(self,in_channels,out_channels):
super(Unit,self).__init__()
self.conv = nn.Conv2d(in_channels=in_channels,kernel_size=3,out_channels=out_channels,stride=1,padding=1)
self.bn = nn.BatchNorm2d(num_features=out_channels)
self.relu = nn.ReLU()
def forward(self,input):
#Define transformations for the training set, flip the images randomly, crop out and apply mean and std normalization
train_transformations = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32,padding=4),
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))
])
#Load the training set
train_set =CIFAR10(root="./data",train=True,transform=train_transformations,download=True)
# Define transformations for the test set
test_transformations = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
# Load the test set, note that train is set to False
test_set = CIFAR10(root="./data", train=False, transform=test_transformations, download=True)