def create_cnn(data, arch, pretrained=False, is_mono_input=True, **kwargs): meta = cnn_config(arch) body = create_body(arch, pretrained) # sum up the weights of in_channels axis, to reduce to single input channel # Suggestion by David Gutman # https://forums.fast.ai/t/black-and-white-images-on-vgg16/2479/2 if is_mono_input: first_conv_layer = body[0] first_conv_weights = first_conv_layer.state_dict()['weight'] assert first_conv_weights.size(1) == 3 # RGB channels dim summed_weights = torch.sum(first_conv_weights, dim=1, keepdim=True) first_conv_layer.weight.data = summed_weights first_conv_layer.in_channels = 1 else: # In this case, the input is a stereo first_conv_layer = body[0] first_conv_weights = first_conv_layer.state_dict()['weight'] assert first_conv_weights.size(1) == 3 # RGB channels dim summed_weights = torch.sum(first_conv_weights, dim=1, keepdim=True) first_conv_layer.weight.data = first_conv_weights[:, : 2, :, :] # Keep only 2 channels for the weights first_conv_layer.in_channels = 2 nf = num_features_model(body) * 2 head = create_head(nf, data.c, None, 0.5) model = nn.Sequential(body, head) learn = Learner(data, model, **kwargs) learn.split(meta['split']) if pretrained: learn.freeze() apply_init(model[1], nn.init.kaiming_normal_) return learn
def __init__(self, bu, instructor, td_c=1, bu_c=0, lateral=laterals.ConvAddLateral, td_out_lateral=None, embedding=fv.embedding, img_size: Tuple[int, int] = (256, 256)): super().__init__() self.ifn, self.bu_body, self.td, self.td_head, self.laterals, channels = create_bu_td( bu, td_head=td_c, lateral=lateral, img_size=img_size) if td_out_lateral: self.laterls[-1].remove() hm_lat = td_out_lateral(self.td[-1], self.bu_body[0], td_c, channels[0]) self.laterls[-1] = hm_lat self.emb = embedding(instructor.n_inst, channels[-1]) if embedding else None self.bu_head = fv.create_head(channels[-1] * 2, bu_c) if bu_c else None self.instructor = instructor self.instructor.on_init_end(self)
def __init__(self, encoder=models.resnet18, s_out=512): # TODO warn is s_out is to large super().__init__() self.body = create_body(encoder, cut=-2) self.head = create_head(1024, 1, [s_out])[:5]
def resnet(classes): base_model = models.resnet50(pretrained=True) body = nn.Sequential(*list(base_model.children())[:-2]) nf = num_features_model(body) * 2 head = create_head(nf, classes, None, ps=0.5, bn_final=False) return nn.Sequential(body, head, nn.LogSoftmax())