def __init__(self, kernel_size, strides, expand_ratio, input_filters, output_filters, se_ratio): oup = expand_ratio * input_filters if expand_ratio != 1: self._expand_conv = Tensor.zeros(oup, input_filters, 1, 1) self._bn0 = BatchNorm2D(oup) else: self._expand_conv = None self.strides = strides if strides == (2, 2): self.pad = [(kernel_size - 1) // 2 - 1, (kernel_size - 1) // 2] * 2 else: self.pad = [(kernel_size - 1) // 2] * 4 self._depthwise_conv = Tensor.zeros(oup, 1, kernel_size, kernel_size) self._bn1 = BatchNorm2D(oup) num_squeezed_channels = max(1, int(input_filters * se_ratio)) self._se_reduce = Tensor.zeros(num_squeezed_channels, oup, 1, 1) self._se_reduce_bias = Tensor.zeros(num_squeezed_channels) self._se_expand = Tensor.zeros(oup, num_squeezed_channels, 1, 1) self._se_expand_bias = Tensor.zeros(oup) self._project_conv = Tensor.zeros(output_filters, oup, 1, 1) self._bn2 = BatchNorm2D(output_filters)
def __init__(self, number=0): self.number = number global_params = [ # width, depth (1.0, 1.0), # b0 (1.0, 1.1), # b1 (1.1, 1.2), # b2 (1.2, 1.4), # b3 (1.4, 1.8), # b4 (1.6, 2.2), # b5 (1.8, 2.6), # b6 (2.0, 3.1), # b7 (2.2, 3.6), # b8 (4.3, 5.3), # l2 ][number] def round_filters(filters): multiplier = global_params[0] divisor = 8 filters *= multiplier new_filters = max(divisor, int(filters + divisor / 2) // divisor * divisor) if new_filters < 0.9 * filters: # prevent rounding by more than 10% new_filters += divisor return int(new_filters) def round_repeats(repeats): return int(math.ceil(global_params[1] * repeats)) out_channels = round_filters(32) self._conv_stem = Tensor.zeros(out_channels, 3, 3, 3) self._bn0 = BatchNorm2D(out_channels) blocks_args = [ [1, 3, (1, 1), 1, 32, 16, 0.25], [2, 3, (2, 2), 6, 16, 24, 0.25], [2, 5, (2, 2), 6, 24, 40, 0.25], [3, 3, (2, 2), 6, 40, 80, 0.25], [3, 5, (1, 1), 6, 80, 112, 0.25], [4, 5, (2, 2), 6, 112, 192, 0.25], [1, 3, (1, 1), 6, 192, 320, 0.25], ] self._blocks = [] # num_repeats, kernel_size, strides, expand_ratio, input_filters, output_filters, se_ratio for b in blocks_args: args = b[1:] args[3] = round_filters(args[3]) args[4] = round_filters(args[4]) for n in range(round_repeats(b[0])): self._blocks.append(MBConvBlock(*args)) args[3] = args[4] args[1] = (1, 1) in_channels = round_filters(320) out_channels = round_filters(1280) self._conv_head = Tensor.zeros(out_channels, in_channels, 1, 1) self._bn1 = BatchNorm2D(out_channels) self._fc = Tensor.zeros(out_channels, 1000) self._fc_bias = Tensor.zeros(1000)
def __init__(self, h, w, inp, filters=128, conv=3): self.h, self.w = h, w self.inp = inp #init weights self.cweights = [Tensor.uniform(filters, inp if i==0 else filters, conv, conv) for i in range(3)] self.cbiases = [Tensor.uniform(1, filters, 1, 1) for i in range(3)] #init layers self._bn = BatchNorm2D(128, training=True) self._seb = SqueezeExciteBlock2D(filters)
def __init__(self): self.blocks = 3 self.block_convs = 3 # TODO: raise back to 128 when it's fast self.chans = 32 self.convs = [ Tensor.uniform(self.chans, self.chans if i > 0 else 1, 3, 3) for i in range(self.blocks * self.block_convs) ] self.cbias = [ Tensor.uniform(1, self.chans, 1, 1) for i in range(self.blocks * self.block_convs) ] self.bn = [BatchNorm2D(self.chans, training=True) for i in range(3)] self.fc1 = Tensor.uniform(self.chans, 10) self.fc2 = Tensor.uniform(self.chans, 10)
def create_modules(self, blocks): net_info = blocks[0] # Info about model hyperparameters prev_filters = 3 filters = None output_filters = [] module_list = [] ## module for index, x in enumerate(blocks[1:]): module_type = x["type"] module = [] if module_type == "convolutional": try: batch_normalize = int(x["batch_normalize"]) bias = False except: batch_normalize = 0 bias = True # layer activation = x["activation"] filters = int(x["filters"]) padding = int(x["pad"]) if padding: pad = (int(x["size"]) - 1) // 2 else: pad = 0 conv = Conv2d(prev_filters, filters, int(x["size"]), int(x["stride"]), pad, bias = bias) module.append(conv) # BatchNorm2d if batch_normalize: bn = BatchNorm2D(filters, eps=1e-05, training=True, track_running_stats=True) module.append(bn) # LeakyReLU activation if activation == "leaky": module.append(LeakyReLU(0.1)) # TODO: Add tiny model elif module_type == "maxpool": size = int(x["size"]) stride = int(x["stride"]) maxpool = MaxPool2d(size, stride) module.append(maxpool) elif module_type == "upsample": upsample = Upsample(scale_factor = 2, mode = "nearest") module.append(upsample) elif module_type == "route": x["layers"] = x["layers"].split(",") # Start of route start = int(x["layers"][0]) # End if it exists try: end = int(x["layers"][1]) except: end = 0 if start > 0: start = start - index if end > 0: end = end - index route = EmptyLayer() module.append(route) if end < 0: filters = output_filters[index + start] + output_filters[index + end] else: filters = output_filters[index + start] # Shortcut corresponds to skip connection elif module_type == "shortcut": module.append(EmptyLayer()) elif module_type == "yolo": mask = x["mask"].split(",") mask = [int(x) for x in mask] anchors = x["anchors"].split(",") anchors = [int(a) for a in anchors] anchors = [(anchors[i], anchors[i+1]) for i in range(0, len(anchors), 2)] anchors = [anchors[i] for i in mask] detection = DetectionLayer(anchors) module.append(detection) # Append to module_list module_list.append(module) if filters is not None: prev_filters = filters output_filters.append(filters) return (net_info, module_list)