How to write the test.py about the imagenet
I have trained the date,and I need to classify the picture. I have a test.py with cuda5.5,it can classiy the picture.but then it is cuda6.0.there is some error. can u help me. this is the 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 ImageNetClassifier(object): """ The ImageNetClassifier is a wrapper class to perform easier deployment of models trained on imagenet. """ def init(self, model_def_file, pretrained_model, center_only=True, num_output=9): 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
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/caffe_gaoxin_train_iter_3500'
net = ImageNetClassifier(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 "test.py", line 110, in
Duplicate of #1712.