utilities/other_utils/HamlinBeachStatePark/unet/model.py
def load_weights_from_matfiles(dir_path):
    """
        Uses scipy.io to read .mat files and loads weights into torch model
    :param path_to_file: path to mat file to read
    :return: None, but saves the model dictionary!
    """
    import pickle
    model_file = 'Unet_pretrained_model.pkl'
    if os.path.exists(os.path.join(dir_path, model_file)):
        print('loading saved model dictionary...')
        with open(os.path.join(dir_path, model_file), 'rb') as handle:
            model_dict = pickle.load(handle)
        for i, layer in enumerate(model_dict.keys(), 1):
            print('{}.'.format(i), layer, model_dict[layer].shape)
    else:
        model_dict = {}
        for file in [x for x in os.listdir(dir_path) if x.endswith('.mat')]:
            layer, _ = os.path.splitext(file)
            try:
                read = sio.loadmat(os.path.join(dir_path, file))
            except:
                print(layer)
            print(layer, read[layer].shape)
            model_dict[layer] = read[layer]
        pass
        os.chdir('/home/annus/Desktop/trainedUnet/weightsforpython/')
        with open(model_file, 'wb') as handle:
            pickle.dump(model_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
        print('Saved model!!!')

    # test on pytorch layers now...
    conv = nn.Conv2d(256, 512, kernel_size=3, padding=1)
    conv.weight = torch.nn.Parameter(torch.Tensor(model_dict['e4c1']))
    conv.bias = torch.nn.Parameter(torch.Tensor(model_dict['e4c1_b']))

    mid_conv = nn.Conv2d(1024, 1024, kernel_size=3, padding=1)
    mid_conv.weight = torch.nn.Parameter(torch.Tensor(model_dict['mid_conv2']))
    mid_conv.bias = torch.nn.Parameter(torch.Tensor(model_dict['mid_conv2_b']))

    tr_conv = nn.ConvTranspose2d(512, 256, kernel_size=2, stride=2)
    tr_conv.weight = torch.nn.Parameter(torch.Tensor(model_dict['d2tc']))
    tr_conv.bias = torch.nn.Parameter(torch.Tensor(model_dict['d2tc_b']))

        Uses scipy.io to read .mat files and loads weights into torch model
    :param path_to_file: path to mat file to read
    :return: None, but saves the model dictionary!
    
Uses scipy.io to read .mat files and loads weights into torch model
Uses scipy . io to read . mat files and loads weights into torch model
def load_weights_from_matfiles(dir_path):
    import pickle
    model_file = 'Unet_pretrained_model.pkl'
    if os.path.exists(os.path.join(dir_path, model_file)):
        print('loading saved model dictionary...')
        with open(os.path.join(dir_path, model_file), 'rb') as handle:
            model_dict = pickle.load(handle)
        for i, layer in enumerate(model_dict.keys(), 1):
            print('{}.'.format(i), layer, model_dict[layer].shape)
    else:
        model_dict = {}
        for file in [x for x in os.listdir(dir_path) if x.endswith('.mat')]:
            layer, _ = os.path.splitext(file)
            try:
                read = sio.loadmat(os.path.join(dir_path, file))
            except:
                print(layer)
            print(layer, read[layer].shape)
            model_dict[layer] = read[layer]
        pass
        os.chdir('/home/annus/Desktop/trainedUnet/weightsforpython/')
        with open(model_file, 'wb') as handle:
            pickle.dump(model_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
        print('Saved model!!!')
    conv = nn.Conv2d(256, 512, kernel_size=3, padding=1)
    conv.weight = torch.nn.Parameter(torch.Tensor(model_dict['e4c1']))
    conv.bias = torch.nn.Parameter(torch.Tensor(model_dict['e4c1_b']))
    mid_conv = nn.Conv2d(1024, 1024, kernel_size=3, padding=1)
    mid_conv.weight = torch.nn.Parameter(torch.Tensor(model_dict['mid_conv2']))
    mid_conv.bias = torch.nn.Parameter(torch.Tensor(model_dict['mid_conv2_b']))
    tr_conv = nn.ConvTranspose2d(512, 256, kernel_size=2, stride=2)
    tr_conv.weight = torch.nn.Parameter(torch.Tensor(model_dict['d2tc']))
    tr_conv.bias = torch.nn.Parameter(torch.Tensor(model_dict['d2tc_b']))
def load_weights_from_matfiles ( dir_path ) : import pickle model_file = 'Unet_pretrained_model.pkl' if os . path . exists ( os . path . join ( dir_path , model_file ) ) : print ( 'loading saved model dictionary...' ) with open ( os . path . join ( dir_path , model_file ) , 'rb' ) as handle : model_dict = pickle . load ( handle ) for i , layer in enumerate ( model_dict . keys ( ) , 1 ) : print ( '{}.' . format ( i ) , layer , model_dict [ layer ] . shape ) else : model_dict = { } for file in [ x for x in os . listdir ( dir_path ) if x . endswith ( '.mat' ) ] : layer , _ = os . path . splitext ( file ) try : read = sio . loadmat ( os . path . join ( dir_path , file ) ) except : print ( layer ) print ( layer , read [ layer ] . shape ) model_dict [ layer ] = read [ layer ] pass os . chdir ( '/home/annus/Desktop/trainedUnet/weightsforpython/' ) with open ( model_file , 'wb' ) as handle : pickle . dump ( model_dict , handle , protocol = pickle . HIGHEST_PROTOCOL ) print ( 'Saved model!!!' ) conv = nn . Conv2d ( 256 , 512 , kernel_size = 3 , padding = 1 ) conv . weight = torch . nn . Parameter ( torch . Tensor ( model_dict [ 'e4c1' ] ) ) conv . bias = torch . nn . Parameter ( torch . Tensor ( model_dict [ 'e4c1_b' ] ) ) mid_conv = nn . Conv2d ( 1024 , 1024 , kernel_size = 3 , padding = 1 ) mid_conv . weight = torch . nn . Parameter ( torch . Tensor ( model_dict [ 'mid_conv2' ] ) ) mid_conv . bias = torch . nn . Parameter ( torch . Tensor ( model_dict [ 'mid_conv2_b' ] ) ) tr_conv = nn . ConvTranspose2d ( 512 , 256 , kernel_size = 2 , stride = 2 ) tr_conv . weight = torch . nn . Parameter ( torch . Tensor ( model_dict [ 'd2tc' ] ) ) tr_conv . bias = torch . nn . Parameter ( torch . Tensor ( model_dict [ 'd2tc_b' ] ) )
Uses scipy.io to read .mat files and loads weights into torch model
Uses scipy . io to read . mat files and loads weights into torch model
"""
        Uses scipy.io to read .mat files and loads weights into torch model
    :param path_to_file: path to mat file to read
    :return: None, but saves the model dictionary!
    """ # test on pytorch layers now...
None, but saves the model dictionary!
None but saves the model dictionary!
dir_path
path_to_file
path to mat file to read
path to mat file to read