(parameters)
return result
Conversion from tensorflow parameters
Conversion from tensorflow parameters
Conversion from tensorflow parameters
def state_dict_from_tf_parameters(parameters):
    '''
    Conversion from tensorflow parameters
    '''
    def torch_from_tf(data):
        return torch.from_numpy(data.eval())

    params = dict(parameters)
    result = {}
    sizes = []
    for i in itertools.count():
        resolution = 4 * (2 ** (i // 2))
        # Translate parameter names.  For example:
        # 4x4/Dense/weight -> layer1.conv.weight
        # 32x32/Conv0_up/weight -> layer7.conv.weight
        # 32x32/Conv1/weight -> layer8.conv.weight
        tf_layername = '%dx%d/%s' % (resolution, resolution,
                'Dense' if i == 0 else 'Conv' if i == 1 else
                'Conv0_up' if i % 2 == 0 else 'Conv1')
        pt_layername = 'layer%d' % (i + 1)
        # Stop looping when we run out of parameters.
        try:
            weight = torch_from_tf(params['%s/weight' % tf_layername])
        except KeyError:
            break
        # Transpose convolution weights into pytorch format.
        if i == 0:
            # Convert dense layer to 4x4 convolution
            weight = weight.view(weight.shape[0], weight.shape[1] // 16,
                   4, 4).permute(1, 0, 2, 3).flip(2, 3)
            sizes.append(weight.shape[0])
        elif i % 2 == 0:
            # Convert inverse convolution to convolution
            weight = weight.permute(2, 3, 0, 1).flip(2, 3)
        else:
            # Ordinary Conv2d conversion.
            weight = weight.permute(3, 2, 0, 1)
            sizes.append(weight.shape[1])
        result['%s.conv.weight' % (pt_layername)] = weight
        # Copy bias vector.
        bias = torch_from_tf(params['%s/bias' % tf_layername])
        result['%s.wscale.b' % (pt_layername)] = bias
    # Copy just finest-grained ToRGB output layers.  For example:
    # ToRGB_lod0/weight -> output.conv.weight
    i -= 1
    resolution = 4 * (2 ** (i // 2))
    tf_layername = 'ToRGB_lod0'
    pt_layername = 'output_%dx%d' % (resolution, resolution)
    result['%s.conv.weight' % pt_layername] = torch_from_tf(
            params['%s/weight' % tf_layername]).permute(3, 2, 0, 1)
    result['%s.wscale.b' % pt_layername] = torch_from_tf(
            params['%s/bias' % tf_layername])
    # Return parameters
    return result
def state_dict_from_tf_parameters ( parameters ) : def torch_from_tf ( data ) : return torch . from_numpy ( data . eval ( ) ) params = dict ( parameters ) result = { } sizes = [ ] for i in itertools . count ( ) : resolution = 4 * ( 2 ** ( i // 2 ) ) # Translate parameter names.  For example: # 4x4/Dense/weight -> layer1.conv.weight # 32x32/Conv0_up/weight -> layer7.conv.weight # 32x32/Conv1/weight -> layer8.conv.weight tf_layername = '%dx%d/%s' % ( resolution , resolution , 'Dense' if i == 0 else 'Conv' if i == 1 else 'Conv0_up' if i % 2 == 0 else 'Conv1' ) pt_layername = 'layer%d' % ( i + 1 ) # Stop looping when we run out of parameters. try : weight = torch_from_tf ( params [ '%s/weight' % tf_layername ] ) except KeyError : break # Transpose convolution weights into pytorch format. if i == 0 : # Convert dense layer to 4x4 convolution weight = weight . view ( weight . shape [ 0 ] , weight . shape [ 1 ] // 16 , 4 , 4 ) . permute ( 1 , 0 , 2 , 3 ) . flip ( 2 , 3 ) sizes . append ( weight . shape [ 0 ] ) elif i % 2 == 0 : # Convert inverse convolution to convolution weight = weight . permute ( 2 , 3 , 0 , 1 ) . flip ( 2 , 3 ) else : # Ordinary Conv2d conversion. weight = weight . permute ( 3 , 2 , 0 , 1 ) sizes . append ( weight . shape [ 1 ] ) result [ '%s.conv.weight' % ( pt_layername ) ] = weight # Copy bias vector. bias = torch_from_tf ( params [ '%s/bias' % tf_layername ] ) result [ '%s.wscale.b' % ( pt_layername ) ] = bias # Copy just finest-grained ToRGB output layers.  For example: # ToRGB_lod0/weight -> output.conv.weight i -= 1 resolution = 4 * ( 2 ** ( i // 2 ) ) tf_layername = 'ToRGB_lod0' pt_layername = 'output_%dx%d' % ( resolution , resolution ) result [ '%s.conv.weight' % pt_layername ] = torch_from_tf ( params [ '%s/weight' % tf_layername ] ) . permute ( 3 , 2 , 0 , 1 ) result [ '%s.wscale.b' % pt_layername ] = torch_from_tf ( params [ '%s/bias' % tf_layername ] ) # Return parameters return result