caffe icon indicating copy to clipboard operation
caffe copied to clipboard

AttributeError: 'module' object has no attribute 'CaffeNet'

Open wzq12138 opened this issue 11 years ago • 1 comments

Hello,everyone,I am doing the example of imagenet about Color recognition of vehicle body. I refered to the ./python/caffe/classisifer.py.this is my test.py

!/usr/bin/env python

"""wrapper.py implements an end-to-end wrapper that classifies an image read from disk, using the imagenet classifier. """

import numpy as np import os from skimage import io from skimage import transform

import _caffe

IMAGE_DIM = 256 CROPPED_DIM = 227

Load the imagenet mean file

IMAGENET_MEAN = np.load('./model/ilsvrc_2012_mean.npy')

def oversample(image, center_only=True): """ Oversamples an image. Currently the indices are hard coded to the 4 corners and the center of the image, as well as their flipped ones, a total of 10 images.

Input: image: an image of size (256 x 256 x 3) and has data type uint8. center_only: if True, only return the center image. Output: images: the output of size (10 x 3 x 224 x 224) """ image = image.swapaxes(1, 2).swapaxes(0, 1) indices = [0, IMAGE_DIM - CROPPED_DIM] center = int(indices[1] / 2) if center_only: return np.ascontiguousarray( image[np.newaxis, :, center:center + CROPPED_DIM, center:center + CROPPED_DIM], dtype=np.float32) else: images = np.empty((10, 3, CROPPED_DIM, CROPPED_DIM), dtype=np.float32) curr = 0 for i in indices: for j in indices: images[curr] = image[:, i:i + CROPPED_DIM, j:j + CROPPED_DIM] curr += 1 images[4] = image[:, center:center + CROPPED_DIM, center:center + CROPPED_DIM] # flipped version images[5:] = images[:5, :, :, ::-1] return images

def prepare_image(filename, center_only=True): img = io.imread(filename) if img.ndim == 2: img = np.tile(img[:, :, np.newaxis], (1, 1, 3)) elif img.shape[2] == 4: img = img[:, :, :3]

Resize and convert to BGR

#img_reshape = (transform.resize(img, (IMAGE_DIM,IMAGE_DIM)) * 255)[:, :, ::-1]

left, right, center

x = img.shape[0] y = img.shape[1] if x > y: img3 = img[(x-IMAGE_DIM)/2:(x-IMAGE_DIM)/2+IMAGE_DIM,:,:] else: img3 = img[:,(y-IMAGE_DIM)/2:(y-IMAGE_DIM)/2+IMAGE_DIM,:]

#io.imsave('1.JPEG', img3)

subtract main

#img_reshape -= IMAGENET_MEAN #crop_img1 = (img1.copy())[:, :, ::-1] - IMAGENET_MEAN #crop_img2 = (img2.copy())[:, :, ::-1] - IMAGENET_MEAN crop_img3 = (img3.copy())[:, :, ::-1] - IMAGENET_MEAN #img1_oversample = oversample(crop_img1, center_only) #img2_oversample = oversample(crop_img2, center_only) img3_oversample = oversample(crop_img3, center_only)

#return np.concatenate([img1_oversample, img2_oversample, img3_oversample], axis=0) return img3_oversample

class Classifier(object): """ Classifier extends Net for image class prediction by scaling, center cropping, or oversampling. """ def init(self, model_file, pretrained_file, image_dims=None, gpu=False, mean=None, input_scale=None, raw_scale=None, channel_swap=None,center_only=True,num_output=9): """ Take image_dims: dimensions to scale input for cropping/sampling. Default is to scale to net input size for whole-image crop. gpu, mean, input_scale, raw_scale, channel_swap: params for preprocessing options. """ if center_only: num = 1 else: num = 1 self.caffenet = _caffe.CaffeNet(model_def_file, pretrained_model) self._output_blobs = [np.empty((num, num_output, 1, 1), dtype=np.float32)] self._center_only = center_only self.caffenet =_caffe.CaffeNet(self,model_file, pretrained_file) self.set_phase_test()

    if gpu:
        self.set_mode_gpu()
    else:
        self.set_mode_cpu()

    if mean is not None:
        self.set_mean(self.inputs[0], mean)
    if input_scale is not None:
        self.set_input_scale(self.inputs[0], input_scale)
    if raw_scale is not None:
        self.set_raw_scale(self.inputs[0], raw_scale)
    if channel_swap is not None:
        self.set_channel_swap(self.inputs[0], channel_swap)

    self.crop_dims = np.array(self.blobs[self.inputs[0]].data.shape[2:])
    if not image_dims:
        image_dims = self.crop_dims
    self.image_dims = image_dims


def predict(self, filename):
    input_blob = [prepare_image(filename, self._center_only)]
    self.caffenet.Forward(input_blob, self._output_blobs)
    return self._output_blobs[0].mean(0).flatten()

MODEL_FILE = './model/imagenet_deploy.prototxt' PRETRAINED = './model/caffenet_train_iter_3500.caffemodel'

net = Classifier(MODEL_FILE, PRETRAINED) net.caffenet.set_phase_test() net.caffenet.set_mode_gpu() net.caffenet.set_device(0)

filenames = os.listdir('./test/') filenames.sort() output = open('./result.txt', 'a')

for i in range(len(filenames)): IMAGE_FILE = './test/' + filenames[i] prediction = net.predict(IMAGE_FILE) #print prediction sort_pre = sorted(enumerate(prediction) ,key=lambda z:z[1]) label = sort_pre[-1][0] s = './test/' + filenames[i] + ' ' + str(label) + '\n' output.write(s) if i%100==0: print i output.close()

the error is that
Traceback (most recent call last): File "teste.py", line 138, in net = Classifier(MODEL_FILE, PRETRAINED) File "teste.py", line 103, in init self.caffenet = _caffe.CaffeNet(model_def_file, pretrained_model) AttributeError: 'module' object has no attribute 'CaffeNet'

can u help me ,thanks

wzq12138 avatar Jan 13 '15 01:01 wzq12138

wrapper.py is from a very old version of Caffe and deprecated. Refer to the scripts in master or dev.

Please ask usage questions on the caffe-users mailing list. Thanks!

shelhamer avatar Jan 15 '15 07:01 shelhamer