VGG:ValueError:解压缩的值太多(预期2)

时间:2017-07-06 08:34:05

标签: python tensorflow valueerror vgg-net

我定义了VGG net并完成了测试任务:def net获取VGG模型参数,因此我可以使用mnist数据进行测试任务。

以下是错误信息:

追踪(最近一次呼叫最后一次):

 File "D:\Users\2015randongmei\workspace\tensor\VGG.py", line 73, in <module>
    nets, mean_pixel, all_layers = net(VGG_PATH, image)
  File "D:\Users\2015randongmei\workspace\tensor\VGG.py", line 47, in net
    kernels, bias = weights[i][0][0][0][0]
ValueError: too many values to unpack (expected 2)

以下是代码:

# -*- coding:utf-8 -*- 
import os
import numpy as np
import scipy.io
import tensorflow as tf
from scipy.misc import imread
import matplotlib.pyplot as plt
#import ast
#source
def _conv_layer(input, weights, bias):
    #权重w转换为tensorlow支持的格式:
    tf.constant(weights)
    conv = tf.nn.conv2d(input, tf.constant(weights), strides=(1, 1, 1, 1), padding='SAME')
    return tf.nn.bias_add(conv, bias)

def _pool_layer(input):
        return tf.nn.max_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding='SAME')

def preprocess(image, mean_pixel):
     return image - mean_pixel

#VGG 19层网络,
#net完成所有层的前向传播,计算结果保存在net字典中
def net(data_path, input_image):
    layers = {
        'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',
        'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
        'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
        'relu3_3', 'conv3_4', 'relu3_4', 'pool3',
        'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
        'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
        'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
        'relu5_3', 'conv5_4', 'relu5_4'
        }
    data = scipy.io.loadmat(data_path)
    mean = data['normalization'][0][0][0]
    mean_pixel = np.mean(mean, axis=(0, 1))
    weights = data['layers'][0]
    #保存每一层的输出
    net = {}
    current = input_image
    for i, name in enumerate(layers):
        #neme的前四个字母
        kind = name[:4]
        if kind == 'conv':
            kernels, bias = weights[i][0][0][0][0]
            # matconvnet: weights are [width, height, in_channels, out_channels]
            # tensorflow: weights are [height, width, in_channels, out_channels]
            kernels = np.transpose(kernels, (1, 0, 2, 3))
            #bias只有一个维度
            bias = bias.reshape(-1)
            #对卷积层进行前向传播,current为当前输入
            current = _conv_layer(current, kernels, bias)
        elif kind == 'relu':
            current = tf.nn.relu(current)
        elif kind == 'pool':
            current = _pool_layer(current)
            net[name] = current
    assert len(net) == len(layers)
    return net, mean_pixel, layers
print ("Network for VGG ready")


#将网络结果进行可视化操作
cwd = os.getcwd()
VGG_PATH = cwd+"\\data\\imagenet-vgg-verydeep-19.mat"
IMG_PATH = cwd + "\\data\\cat.jpg"
input_image = imread(IMG_PATH)
shape =(1,input_image.shape[0],input_image.shape[1],input_image.shape[2])
with tf.Session() as sess:
    image = tf.placeholder('float', shape=shape)
    nets, mean_pixel, all_layers = net(VGG_PATH, image)
    input_image_pre = np.array([preprocess(input_image, mean_pixel)])
    layers = all_layers # For all layers 
    # layers = ('relu2_1', 'relu3_1', 'relu4_1')
    for i, layer in enumerate(layers):
        print ("[%d/%d] %s" % (i+1, len(layers), layer))
        features = nets[layer].eval(feed_dict={image: input_image_pre})
        print (" Type of 'features' is ", type(features))
        print (" Shape of 'features' is %s" % (features.shape,))
        # Plot response 
        if 1:
            plt.figure(i+1, figsize=(10, 5))
            plt.matshow(features[0, :, :, 0], cmap=plt.cm.gray, fignum=i+1)
            plt.title("" + layer)
            plt.colorbar()
            plt.show()

1 个答案:

答案 0 :(得分:0)

 kernels, bias = weights[i][0][0][2][0]