Skip to content

Instantly share code, notes, and snippets.

@Amplify4177
Created November 9, 2023 13:53
Show Gist options
  • Select an option

  • Save Amplify4177/d5bb18350012efbdb44e5224bad87a27 to your computer and use it in GitHub Desktop.

Select an option

Save Amplify4177/d5bb18350012efbdb44e5224bad87a27 to your computer and use it in GitHub Desktop.
ESP32ML - MicroPython Wrapper for C Optimized Conv Layer
class convLayer(Module):
def __init__(self, in_channels, out_channels, kernel_size: tuple, stride):
self.kernels = [[None for _ in range(in_channels)] for _2
in range(out_channels)]# Start with no kernels
self.bias = [None for _ in range(out_channels)]# Start with no kernels
self.stride = stride
self.kernel_size = kernel_size
@micropython.native
def __call__(self, images):
conv_func = mlops.conv2d
output_data = []
for out_channel in range(len(self.kernels)):
channel_output = []
for in_channel, image in enumerate(images):
channel_output.append(conv_func(image, self.kernels[out_channel][in_channel],self.stride))
biased_channel_output = sumMatricesAddBias(channel_output, self.bias[out_channel])
output_data.append(biased_channel_output)
return output_data
def load_state_dict(self, weights, bias):
for i, in_channels in enumerate(weights):
for j, kernels in enumerate(in_channels):
kernel_data = []
for row in kernels:
kernel_data.append(row)
self.kernels[i][j] = kernel_data
for i, b in enumerate(bias):
self.bias[i] = b
@micropython.native
def sumMatricesAddBias(matrices, bias=0.0):
return mlops.sum_matrices(matrices, bias)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment