如何解决caffe可视化中的不匹配输入大小?

时间:2016-10-14 08:00:41

标签: python caffe

我用AlexNet模型培训了我的网络,并且我有模型权重。现在我想在我的测试数据上测试它。这是我的代码的一部分:

import matplotlib.pyplot as plt
from PIL import Image
import cv2
import numpy as np
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
net = caffe.Net(model_def,      # defines the structure of the model
            model_weights,  # contains the trained weights
            caffe.TEST)
print 'net.blobs['data'].data.shape = ', net.blobs['data'].data.shape

# create transformer for the input called 'data'
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})

transformer.set_transpose('data', (2,0,1))  # move image channels to outermost dimension
#transformer.set_mean('data', mu)            # subtract the dataset-mean value in each channel
transformer.set_raw_scale('data', 255)      # rescale from [0, 1] to [0, 255]
#transformer.set_channel_swap('data', (2,1,0))  # swap channels from RGB to BGR

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# set the size of the input 
#net.blobs['data'].reshape(1,        # batch size
#                          1,        # 3-channel (BGR) images
#                       227, 227,  # image size is 227x227


image = cv2.imread('p225232.jpg')
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print 'original image: ', np.shape(image)
print 'convert to gray image: ', np.shape(image1)

image = caffe.io.load_image('p225232.jpg')
transformed_image = transformer.preprocess('data', image1)
print np.shape(image)
print np.shape(transformed_image)

# show net information
for layer_name, blob in net.blobs.iteritems():
    print layer_name + '\t' + str(blob.data.shape)

,此次运行的输出为:

net.blobs['data'].data.shape = (1, 1, 227, 227)
original image:  (228, 228, 3)
convert to gray image:  (228, 228)
caffe.io.load_image('p225232.jpg') = (228, 228, 3)
transformed_image size = (3, 227, 227)

data    (1, 227, 227)
conv1   (1, 96, 55, 55)
norm1   (1, 96, 55, 55)
pool1   (1, 96, 27, 27)
conv2   (1, 256, 27, 27)
norm2   (1, 256, 27, 27)
pool2   (1, 256, 13, 13)
conv3   (1, 384, 13, 13)
conv4   (1, 384, 13, 13)
conv5   (1, 256, 13, 13)
pool5   (1, 256, 6, 6)
fc6 (1, 4096)
fc7 (1, 4096)
fc8 (1, 4)
prob    (1, 4)
conv1   (96, 1, 11, 11) (96,)
conv2   (256, 48, 5, 5) (256,)
conv3   (384, 256, 3, 3) (384,)
conv4   (384, 192, 3, 3) (384,)
conv5   (256, 192, 3, 3) (256,)
fc6 (4096, 9216) (4096,)
fc7 (4096, 4096) (4096,)
fc8 (4, 4096) (4,)

但是当我跑步时:

net.blobs['data'].data[...] = transformed_image

我收到此错误:

ValueError: could not broadcast input array from shape (3,227,227) into shape (1,227,227)

我非常混淆为输入部分创建变压器! 1-首先,我没有那个.npy文件!我怎么才能得到它? 如果我不进行这种转换,为什么我无法继续? 3-如何将这些尺寸从输入数据与转换后的数据相匹配? (解决我遇到的错误) 4-这似乎是一个简单的问题但是当我在MATLAB中读取我的图像数据时它是288 * 288但是当我读取相同的图像时,我得到的图像大小为288 * 288 * 3。为什么呢?

你能不能帮我找到问题的解决方案?? !!

1 个答案:

答案 0 :(得分:0)

似乎您必须将图像转换为灰度。我真的不知道如何使用caffe.io.transformer对象来做(实际上我会说你做不到)。但是之后np.mean可以完成工作

image = caffe.io.load_image('p225232.jpg')
transformed_image = transformer.preprocess('data', image1)
transformed_image = np.mean(tranformed_image, axis=0)