def load_model_weights(self): weight_reader = WeightReader(self.wt_path) #weight_reader.reset() nb_conv = 23 for i in range(1, nb_conv + 1): conv_layer = self.model.get_layer('conv_' + str(i)) if i < nb_conv: norm_layer = self.model.get_layer('norm_' + str(i)) size = np.prod(norm_layer.get_weights()[0].shape) beta = weight_reader.read_bytes(size) gamma = weight_reader.read_bytes(size) mean = weight_reader.read_bytes(size) var = weight_reader.read_bytes(size) weights = norm_layer.set_weights([gamma, beta, mean, var]) if len(conv_layer.get_weights()) > 1: bias = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[1].shape)) kernel = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[0].shape)) kernel = kernel.reshape( list(reversed(conv_layer.get_weights()[0].shape))) kernel = kernel.transpose([2, 3, 1, 0]) conv_layer.set_weights([kernel, bias]) else: kernel = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[0].shape)) kernel = kernel.reshape( list(reversed(conv_layer.get_weights()[0].shape))) kernel = kernel.transpose([2, 3, 1, 0]) conv_layer.set_weights([kernel]) return
# Layer 23 x = Conv2D(BOX * (4 + 1 + CLASS), (1, 1), strides=(1, 1), padding='same', name='conv_23')(x) output = Reshape((GRID_H, GRID_W, BOX, 4 + 1 + CLASS))(x) # small hack to allow true_boxes to be registered when Keras build the model # for more information: https://github.com/fchollet/keras/issues/2790 output = Lambda(lambda args: args[0])([output, true_boxes]) model = Model([input_image, true_boxes], output) model.summary() weight_reader = WeightReader(wt_path) weight_reader.reset() nb_conv = 23 for i in range(1, nb_conv + 1): conv_layer = model.get_layer('conv_' + str(i)) if i < nb_conv: norm_layer = model.get_layer('norm_' + str(i)) size = np.prod(norm_layer.get_weights()[0].shape) beta = weight_reader.read_bytes(size) gamma = weight_reader.read_bytes(size) mean = weight_reader.read_bytes(size)
import torch import YoloV3_model from dataset import YoloV3DataModule from collections import namedtuple from utils import WeightReader, load_conv_block import pytorch_lightning as pl conv_config = namedtuple("ConvConfig", ["kernel_size", "filters", "stride", "pad"]) maxpool_config = namedtuple("MaxPoolConfig", ["kernel_size", "stride"]) DummyBlock = namedtuple("DummyBlock", ["conv"]) model = YoloV3_model.YoloV3Model() data = YoloV3DataModule() # + weight_reader = WeightReader( "yolov3.weights", initial_offset=5, ) def apply_weights_on_seq(seq_block): for block in seq_block.children(): if "CNNBlock" in str(block.__class__): load_conv_block(block, weight_reader) elif "DoubleConvWithResidual" in str(block.__class__): load_conv_block(block.conv1, weight_reader) load_conv_block(block.conv2, weight_reader) elif "Conv2d" in str(block.__class__): load_conv_block(block, weight_reader, with_bn=False) apply_weights_on_seq(model.base_model.part1)
# small hack to allow true_boxes to be registered when Keras build the model # for more information: https://github.com/fchollet/keras/issues/2790 # output = Lambda(lambda args: args[0])([output, true_boxes]) model = Model([input_image, true_boxes], output) model.summary() yolo = Model([input_image, true_boxes], output) '''Load pre-trained weights''' # Load pretrained weights # **Load the weights originally provided by YOLO** print("**Load the weights originally provided by YOLO**") weight_reader = WeightReader(wt_path) weight_reader.reset() # don't worry! it doesn't delete the weights. nb_conv = 23 for i in range(1, nb_conv + 1): conv_layer = model.get_layer('conv_' + str(i)) if i < nb_conv: norm_layer = model.get_layer('norm_' + str(i)) size = np.prod(norm_layer.get_weights()[0].shape) beta = weight_reader.read_bytes(size) gamma = weight_reader.read_bytes(size) mean = weight_reader.read_bytes(size)
repeat_block([conv_config(1, 512, 1, 0), conv_config(3, 1024, 1, 1)], 2), conv_config(3, 1024, 1, 1), conv_config(3, 1024, 2, 1), conv_config(3, 1024, 1, 1), conv_config(3, 1024, 1, 1), ] DummyBlock = namedtuple("DummyBlock", ["conv"]) model = YoloV1_model.YoloV1Model( architecture=architecture_config, split_size=7, num_boxes=2, num_classes=20, ) weight_reader = WeightReader("./yolov1.weights", initial_offset=5) def apply_weights_on_seq(seq_block): for block in seq_block.children(): if "CNNBlock" in str(block.__class__): load_conv_block(block, weight_reader) apply_weights_on_seq(model.darknet) print("After Loading:", weight_reader.offset) for p in model.darknet.parameters(): p.requires_grad = False data = YoloV1DataModule()
conv_config(3, 1024, 1, 1), conv_config(1, 512, 1, 0), conv_config(3, 1024, 1, 1), conv_config(3, 1024, 1, 1), conv_config(3, 1024, 1, 1), ] DummyBlock = namedtuple("DummyBlock", ["conv"]) model = YoloV2_model.YoloV2Model( architecture=[architecture_config1, architecture_config2], split_size=13, num_boxes=5, num_classes=20, ) weight_reader = WeightReader("yolov2-voc.weights", initial_offset=5) def apply_weights_on_seq(seq_block): for block in seq_block.children(): if "CNNBlock" in str(block.__class__): load_conv_block(block, weight_reader) apply_weights_on_seq(model.darknet_before_skip) apply_weights_on_seq(model.darknet_after_skip) load_conv_block(model.middle, weight_reader) load_conv_block(model.conv_end1, weight_reader) load_conv_block(model.final_conv, weight_reader, with_bn=False) print("After Loading:", weight_reader.offset)
def initialize_weights(): weight_reader = WeightReader(wt_path) weight_reader.reset() nb_conv = 23 for i in range(1, nb_conv + 1): conv_layer = model.get_layer('conv_' + str(i)) if i < nb_conv: norm_layer = model.get_layer('norm_' + str(i)) size = np.prod(norm_layer.get_weights()[0].shape) beta = weight_reader.read_bytes(size) gamma = weight_reader.read_bytes(size) mean = weight_reader.read_bytes(size) var = weight_reader.read_bytes(size) weights = norm_layer.set_weights([gamma, beta, mean, var]) if len(conv_layer.get_weights()) > 1: bias = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[1].shape)) kernel = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[0].shape)) kernel = kernel.reshape( list(reversed(conv_layer.get_weights()[0].shape))) kernel = kernel.transpose([2, 3, 1, 0]) conv_layer.set_weights([kernel, bias]) else: kernel = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[0].shape)) kernel = kernel.reshape( list(reversed(conv_layer.get_weights()[0].shape))) kernel = kernel.transpose([2, 3, 1, 0]) conv_layer.set_weights([kernel]) layer = model.layers[-4] # the last convolutional layer weights = layer.get_weights() new_kernel = np.random.normal(size=weights[0].shape) / (GRID_H * GRID_W) new_bias = np.random.normal(size=weights[1].shape) / (GRID_H * GRID_W) layer.set_weights([new_kernel, new_bias])
def load_weights(): """ This method loads the pretrained weights from YOLO """ weight_reader = WeightReader(WEIGHT_PATH) weight_reader.reset() nb_conv = 23 for i in range(1, nb_conv + 1): conv_layer = model.get_layer('conv_' + str(i)) if i < nb_conv: norm_layer = model.get_layer('norm_' + str(i)) size = np.prod(norm_layer.get_weights()[0].shape) beta = weight_reader.read_bytes(size) gamma = weight_reader.read_bytes(size) mean = weight_reader.read_bytes(size) var = weight_reader.read_bytes(size) weights = norm_layer.set_weights([gamma, beta, mean, var]) if len(conv_layer.get_weights()) > 1: bias = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[1].shape)) kernel = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[0].shape)) kernel = kernel.reshape( list(reversed(conv_layer.get_weights()[0].shape))) kernel = kernel.transpose([2, 3, 1, 0]) conv_layer.set_weights([kernel, bias]) else: kernel = weight_reader.read_bytes( np.prod(conv_layer.get_weights()[0].shape)) kernel = kernel.reshape( list(reversed(conv_layer.get_weights()[0].shape))) kernel = kernel.transpose([2, 3, 1, 0]) conv_layer.set_weights([kernel]) # Randomize the weights of the last layer # We would only be training the last layer again layer = model.layers[-4] weights = layer.get_weights() new_kernel = np.random.normal(size=weights[0].shape) / (GRID_H * GRID_W) new_bias = np.random.normal(size=weights[1].shape) / (GRID_H * GRID_W) layer.set_weights([new_kernel, new_bias])