Ejemplo n.º 1
0
def decafImages(src_path, socketid, output_path, result_path):
    # Establishing connection to send results and write messages
    print "The worker is working properly"
    from skimage import io
    from cloudcv17 import config

    import redis
    import json
    import caffe
    import numpy as np
    import os
    import glob
    import time
    import scipy.io as sio
    import traceback

    rs = redis.StrictRedis(host=config.REDIS_HOST, port=6379)

    try:
        # Needed to fix error https://github.com/BVLC/caffe/issues/438
        io.use_plugin('matplotlib')

        # Caffe Initialisations
        CAFFE_DIR = os.path.normpath(os.path.join(os.path.dirname(caffe.__file__), "..", ".."))
        MODEL_FILE = os.path.join(CAFFE_DIR, 'models/bvlc_reference_caffenet/deploy.prototxt')
        PRETRAINED = os.path.join(CAFFE_DIR, 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel')

        # Set CPU mode
        caffe.set_mode_cpu()

        # Make classifier.
        classifier = caffe.Classifier(MODEL_FILE, PRETRAINED)

        # Find decaf features and send Results
        if os.path.isdir(src_path):
            for input_file in glob.glob(os.path.join(src_path, '*')):
                if os.path.isfile(input_file):
                    rs.publish('chat', json.dumps({'message': 'Processing ' +
                                                   os.path.basename(input_file), 'socketid': str(socketid)}))

                    # Loading Image
                    input_image = caffe.io.load_image(input_file)

                    # Finding decaf features
                    start = time.time()
                    classifier.predict([input_image])
                    blobs = classifier.blobs.items()
                    features = blobs[-3][1].data[:, :, 0, 0]
                    features_center = blobs[-3][1].data[4, :, 0, 0]
                    features_center = np.resize(features_center, (1, 4096))
                    timeMsg = "Completed in %.2f s." % (time.time() - start)
                    rs.publish('chat', json.dumps({'message': timeMsg, 'socketid': str(socketid)}))

                    # Saving decaf features
                    matfile = {}
                    matfile['decaf'] = features
                    matfile['decaf_center'] = features_center
                    out_file = os.path.join(output_path, os.path.basename(input_file) + '.mat')
                    publish_file = os.path.join(result_path, os.path.basename(input_file) + '.mat')
                    sio.savemat(out_file, matfile)
                    rs.publish('chat', json.dumps({'web_result': publish_file, 'socketid': str(socketid)}))
        else:
            input_file = src_path

            rs.publish('chat', json.dumps({'message': 'Processing ' +
                                           os.path.basename(input_file), 'socketid': str(socketid)}))

            # Loading Image
            input_image = caffe.io.load_image(input_file)

            # Finding decaf features
            start = time.time()
            classifier.predict([input_image])
            blobs = classifier.blobs.items()
            features = blobs[-3][1].data[:, :, 0, 0]
            features_center = blobs[-3][1].data[4, :, 0, 0]
            features_center = np.resize(features_center, (1, 4096))
            timeMsg = "Completed in %.2f s." % (time.time() - start)
            rs.publish('chat', json.dumps({'message': timeMsg, 'socketid': str(socketid)}))

            # Saving decaf features
            matfile = {}
            matfile['decaf'] = features
            matfile['decaf_center'] = features_center
            out_file = os.path.join(result_path, os.path.basename(input_file) + '.mat')
            out_file = os.path.join(output_path, os.path.basename(input_file) + '.mat')
            publish_file = os.path.join(result_path, os.path.basename(input_file) + '.mat')
            sio.savemat(out_file, matfile)
            rs.publish('chat', json.dumps({'web_result': publish_file, 'socketid': str(socketid)}))

        rs.publish('chat', json.dumps({'message': 'Thank you for using CloudCV', 'socketid': str(socketid)}))

    except:
        # In case of an error, send the whole error with traceback
        rs.publish('chat', json.dumps({'message': str(traceback.format_exc()), 'socketid': str(socketid)}))
Ejemplo n.º 2
0
import caffe
from caffe import layers as L, params as P

path = '/home/User/Documents/folder/'

#%%

#load net
model = path + 'real_deploy.prototxt'
weights = path + 'snap_iter_1000.caffemodel'

caffe.set_mode_cpu()
#caffe.set_device(0) #per gpu

net = caffe.Classifier(model, weights, raw_scale=1, image_dims=(128, 128))
#%%

#load test dataset from hdf5 file
with h5py.File(path + 'test.h5', 'r') as hf:
    dataset = hf.get('data')
    labelset = hf.get('label')

    data = np.array(dataset)
    label = np.array(labelset)

#compute predictions for every image in the test dataset
predictions = np.zeros(label.shape)

for i in range(data.shape[0]):
    input_image = np.expand_dims(data[i, 0], axis=3)
Ejemplo n.º 3
0
import skimage
import requests

MODLE_FILE = "/home/yonatan/trendi/yonatan/Alexnet_deploy.prototxt"
PRETRAINED = "/home/yonatan/alexnet_imdb_first_try/caffe_alexnet_train_faces_iter_10000.caffemodel"
caffe.set_mode_gpu()
image_dims = [115, 115]
mean, input_scale = np.array([120, 120, 120]), None
channel_swap = [2, 1, 0]
raw_scale = 255.0

# Make classifier.
classifier = caffe.Classifier(MODLE_FILE,
                              PRETRAINED,
                              image_dims=image_dims,
                              mean=mean,
                              input_scale=input_scale,
                              raw_scale=raw_scale,
                              channel_swap=channel_swap)

print "Done initializing!"


def cv2_image_to_caffe(image):
    return skimage.img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).astype(
        np.float32)


def url_to_image(url):
    # download the image, convert it to a NumPy array, and then read
    # it into OpenCV format
Ejemplo n.º 4
0
def classifynum(correctphone, bmpnum):
    MODEL_FILE = '/home/songqing/dl/caffe/examples/mnist/lenet.prototxt'
    PRETRAINED = '/home/songqing/dl/caffe/examples/mnist/zsq11_lenet_iter_10000.caffemodel'
    piccount = 0
    iternum = 0
    finallist = []
    poslist = []
    ratelist = []
    outfile = open('smallpic.txt', 'a+')
    outfile.write('phone ' + str(bmpnum) + '\n')
    outfile.write(str(correctphone) + '\n')
    for iternum in range(1, 140):
        #print iternum
        counttemp = piccount
        piccount = piccount + 1
        filename = '0'
        if counttemp < 10:
            filename = filename + '00' + str(counttemp)
        elif counttemp < 100:
            filename = filename + '0' + str(counttemp)
        else:
            filename = filename + str(counttemp)

        IMAGE_FILE = '/home/songqing/dl/dl/phone/phonenum_recognition/BMP7/testpic/pic' + filename + '.bmp'
        if (os.path.isfile(IMAGE_FILE) == False):
            continue
        caffe.set_phase_test()
        caffe.set_mode_cpu()
        net = caffe.Classifier(MODEL_FILE, PRETRAINED)
        #                       mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'),
        #							channel_swap=(2,1,0),
        #                      raw_scale=28,
        #                       image_dims=(28, 28))
        input_image = caffe.io.load_image(IMAGE_FILE, color=False)
        #plt.imshow(input_image)
        prediction = net.predict([input_image], oversample=False)
        nummax = prediction[0].argmax()
        #print 'prediction shape:', prediction[0].shape
        #plt.plot(prediction[0])
        #print 'predicted class:', prediction[0].argmax()
        #print 'predicted class:', prediction[0][nummax]
        #print 'predicted class:', prediction[0]

        # if rate is small, put 0 to it's value
        if (prediction[0][nummax] < 0.98 and nummax != 1):
            if (nummax == 9):
                if (prediction[0][nummax] < 0.9):
                    prediction[0][nummax] = 0
            else:
                prediction[0][nummax] = 0
        if (prediction[0][nummax] < 0.995 and nummax == 1):
            prediction[0][nummax] = 0
        if (prediction[0][nummax] == 0):
            outfile.write(
                str(nummax) + ' ' + str(prediction[0][nummax]) + '\n')
            continue
#in a range , get the max value
        if ((len(finallist) == 0)):
            if (nummax == 1):  # the first num is 1
                finallist.append(nummax)
                ratelist.append(prediction[0][nummax])
                poslist.append(iternum)
        elif (iternum - poslist[-1] <= 2):
            if (prediction[0][nummax] > ratelist[-1]):
                if (len(finallist) > 1):
                    if (iternum - poslist[-2] < 14):
                        if (nummax == 1):
                            if (prediction[0][nummax] > 0.999):
                                finallist[-1] = nummax
                                ratelist[-1] = prediction[0][nummax]
                                poslist[-1] = iternum
                        else:
                            finallist[-1] = nummax
                            ratelist[-1] = prediction[0][nummax]
                            poslist[-1] = iternum
                else:
                    finallist[-1] = nummax
                    ratelist[-1] = prediction[0][nummax]
                    poslist[-1] = iternum
#put into the list OR update, limit operation
        elif (iternum - poslist[-1] <= 5 and iternum - poslist[-1] >= 4):
            if (len(finallist) == 1):  #add not update
                if (nummax == 1):
                    if (prediction[0][nummax] > 0.999):
                        finallist.append(nummax)
                        ratelist.append(prediction[0][nummax])
                        poslist.append(iternum)
                elif (prediction[0][nummax] > 0.99):
                    finallist.append(nummax)
                    ratelist.append(prediction[0][nummax])
                    poslist.append(iternum)
            else:
                if (ratelist[-1] > 0.999
                        and len(finallist) < 11):  # last rate is too high, add
                    if (nummax == finallist[-1]
                            and finallist[-1] == finallist[-2]):
                        if (nummax != 1 and iternum - poslist[-2] > 14):
                            finallist.append(nummax)
                            ratelist.append(prediction[0][nummax])
                            poslist.append(iternum)
                    elif (nummax == 1):
                        if (finallist[-1] != 0):
                            if (prediction[0][nummax] > 0.9999
                                    and iternum - poslist[-2] > 14):
                                finallist.append(nummax)
                                ratelist.append(prediction[0][nummax])
                                poslist.append(iternum)
                    elif (prediction[0][nummax] > 0.99):
                        finallist.append(nummax)
                        ratelist.append(prediction[0][nummax])
                        poslist.append(iternum)
                elif (ratelist[-1] > 0.98 and nummax == 9
                      and len(finallist) < 11):  # 9 rate is lower, add
                    if (nummax == finallist[-1]
                            and finallist[-1] == finallist[-2]):
                        if (nummax != 1 and iternum - poslist[-2] > 14):
                            finallist.append(nummax)
                            ratelist.append(prediction[0][nummax])
                            poslist.append(iternum)
                    elif (nummax == 1 and iternum - poslist[-2] > 14):
                        if (prediction[0][nummax] > 0.9999):
                            finallist.append(nummax)
                            ratelist.append(prediction[0][nummax])
                            poslist.append(iternum)
                    elif (prediction[0][nummax] > 0.99):
                        finallist.append(nummax)
                        ratelist.append(prediction[0][nummax])
                        poslist.append(iternum)
                elif (prediction[0][nummax] > ratelist[-1]):  #update
                    if (iternum - poslist[-2] < 14):
                        if (nummax == 1):
                            if (finallist[-1] == 0):
                                if (prediction[0][nummax] > 0.9999):
                                    finallist[-1] = nummax
                                    ratelist[-1] = prediction[0][nummax]
                                    poslist[-1] = iternum
                            elif (prediction[0][nummax] > 0.999):
                                finallist[-1] = nummax
                                ratelist[-1] = prediction[0][nummax]
                                poslist[-1] = iternum
                        else:
                            finallist[-1] = nummax
                            ratelist[-1] = prediction[0][nummax]
                            poslist[-1] = iternum

#direct put into the list
        elif ((iternum - poslist[-1]) > 5 and len(finallist) < 11):
            if (len(finallist) <= 2):
                if (nummax == 1):
                    if (prediction[0][nummax] > 0.993):
                        finallist.append(nummax)
                        ratelist.append(prediction[0][nummax])
                        poslist.append(iternum)
                else:
                    finallist.append(nummax)
                    ratelist.append(prediction[0][nummax])
                    poslist.append(iternum)
            else:  # 2 same num might detect as 3 nums
                if (nummax == finallist[-1]
                        and finallist[-1] == finallist[-2]):
                    if (nummax != 1 and iternum - poslist[-2] > 14):
                        finallist.append(nummax)
                        ratelist.append(prediction[0][nummax])
                        poslist.append(iternum)
                else:
                    if (nummax == 1):
                        if (prediction[0][nummax] > 0.993):
                            finallist.append(nummax)
                            ratelist.append(prediction[0][nummax])
                            poslist.append(iternum)
                    else:
                        finallist.append(nummax)
                        ratelist.append(prediction[0][nummax])
                        poslist.append(iternum)

        outfile.write(str(nummax) + ' ' + str(prediction[0][nummax]) + '\n')

        #%timeit net.predict([input_image])

        # Resize the image to the standard (256, 256) and oversample net input sized crops.
        input_oversampled = caffe.io.oversample(
            [caffe.io.resize_image(input_image, net.image_dims)],
            net.crop_dims)
        # 'data' is the input blob name in the model definition, so we preprocess for that input.
        caffe_input = np.asarray(
            [net.preprocess('data', in_) for in_ in input_oversampled])
# forward() takes keyword args for the input blobs with preprocessed input arrays.
#%timeit net.forward(data=caffe_input)
#	print finallist
#	print ratelist
#	print poslist
#	outfile.close()
    outfile.write(str(finallist) + '\n')
    outfile.write(str(ratelist) + '\n')
    outfile.write(str(poslist) + '\n')
    outfile.close()
    str_out = ''
    n_for = 0
    while (n_for < len(finallist)):
        str_out += str(finallist[n_for])
        n_for = n_for + 1


#	print str_out
    return str(str_out)
    def evaluate(self, metric='cosine'):
        '''
        @brief: 评测模型的性能
        @param:itera: 模型的迭代次数
        @param:metric: 度量的方法
        '''
        cap = cv2.VideoCapture(0)
        caffe.set_mode_gpu()

        net = caffe.Classifier(self.deploy,
                               self.model,
                               mean=np.load(self.imgmean_npy))

        Lists = os.listdir(
            '/home/kjin/caffe-master/examples/VGGNet/ContrastivePicNew/')
        Feature = [None] * len(Lists)
        i = 0
        for list in Lists:
            X1 = '/home/kjin/caffe-master/examples/VGGNet/ContrastivePicNew/' + list
            X = DeepIDTest.read_imagelist(X1)
            out = net.forward_all(data=X)
            Feature[i] = np.float64(out['fc7'])
            Feature[i] = np.reshape(Feature[i], 4096)
            i = i + 1

        prototxt = [
            '/home/kjin/caffe-master/examples/VGGNet/pycaffe-mtcnn-master/model/'
            + x + '.prototxt' for x in ['det1', 'det2', 'det3']
        ]
        binary = [
            '/home/kjin/caffe-master/examples/VGGNet/pycaffe-mtcnn-master/model/'
            + x + '.caffemodel' for x in ['det1', 'det2', 'det3']
        ]
        PNet = caffe.Net(prototxt[0], binary[0], caffe.TEST)
        RNet = caffe.Net(prototxt[1], binary[1], caffe.TEST)
        ONet = caffe.Net(prototxt[2], binary[2], caffe.TEST)

        Error = 70
        RecognitionNum = [None] * 5
        while True:
            while True:
                t1 = time.time()
                ret, im = cap.read()

                # Load image.
                # im = cv2.imread(frame)s
                # assert im is not None, 'Image is empty.'
                im_bk = im.copy()

                im = im.astype(np.float32)
                im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
                im = np.transpose(im, (1, 0, 2))  # Rotate image.

                image_height, image_width, num_channels = im.shape
                print('Image shape:', im.shape)

                # assert num_channels == 3, 'Error: only support RGB image.'

                MIN_FACE_SIZE = 24.  # Minimum face size.
                MIN_INPUT_SIZE = 12.  # Minimum input size.
                m = MIN_INPUT_SIZE / MIN_FACE_SIZE

                min_size = min(image_height, image_width)
                min_size = min_size * m

                scales = []
                counter = 0
                FACTOR = 0.709
                while min_size >= MIN_INPUT_SIZE:
                    scales.append(m * FACTOR**counter)
                    min_size = min_size * FACTOR
                    counter = counter + 1

                # Load models.

                # Threshold for each stage.
                # THRESHOLD = [0.6, 0.7, 0.7]
                THRESHOLD = [0.9, 0.9, 0.9]
                #t1 = time.time()

                # --------------------------------------------------------------
                # First stage.
                #
                total_boxes = []  # Bounding boxes of all scales.
                for scale in scales:
                    hs = int(math.ceil(image_height * scale))
                    ws = int(math.ceil(image_width * scale))

                    im_resized = cv2.resize(im, (ws, hs),
                                            interpolation=cv2.INTER_AREA)
                    print('Resize to:', im_resized.shape)

                    # H,W,C -> C,H,W
                    im_resized = np.transpose(im_resized, (2, 0, 1))

                    # Zero mean and normalization.
                    im_resized = (im_resized - 127.5) * 0.0078125

                    # Reshape input layer.
                    PNet.blobs['data'].reshape(1, 3, hs, ws)
                    PNet.blobs['data'].data[...] = im_resized
                    outputs = PNet.forward()

                    bboxes = get_pnet_boxes(outputs, scale, THRESHOLD[0])
                    bboxes, _ = non_max_suppression(bboxes, 0.5)

                    total_boxes.append(bboxes)

                total_boxes = np.vstack(total_boxes)

                bboxes, _ = non_max_suppression(total_boxes, 0.7)
                bboxes = bbox_regression(total_boxes)

                bboxes = bbox_to_square(bboxes)
                bboxes = padding(bboxes, image_height, image_width)

                print('After PNet bboxes shape: ', bboxes.shape)
                if bboxes.shape[0] == 0:
                    draw_and_show(im_bk, bboxes)
                    break

                # --------------------------------------------------------------
                # Second stage.
                #
                inputs = get_inputs_from_bboxes(im, bboxes, 24)
                N, C, H, W = inputs.shape

                RNet.blobs['data'].reshape(N, 3, H, W)
                RNet.blobs['data'].data[...] = inputs
                outputs = RNet.forward()

                bboxes = get_rnet_boxes(bboxes, outputs, THRESHOLD[1])

                bboxes, _ = non_max_suppression(bboxes, 0.7)
                bboxes = bbox_regression(bboxes)
                bboxes = bbox_to_square(bboxes)
                bboxes = padding(bboxes, image_height, image_width)

                print('After RNet bboxes shape: ', bboxes.shape)
                if bboxes.shape[0] == 0:
                    draw_and_show(im_bk, bboxes)
                    break

                # --------------------------------------------------------------
                # Third stage.
                #
                inputs = get_inputs_from_bboxes(im, bboxes, 48)
                N, C, H, W = inputs.shape

                ONet.blobs['data'].reshape(N, 3, H, W)
                ONet.blobs['data'].data[...] = inputs
                outputs = ONet.forward()

                bboxes, points = get_onet_boxes(bboxes, outputs, THRESHOLD[2])
                bboxes = bbox_regression(bboxes)

                bboxes, picked_indices = non_max_suppression(
                    bboxes, 0.7, 'min')
                points = points[picked_indices]
                bboxes = padding(bboxes, image_height, image_width)

                print('After ONet bboxes shape: ', bboxes.shape, '\n')
                if bboxes.shape[0] == 0:
                    draw_and_show(im_bk, bboxes, points)
                    break

            # t2 = time.time()
            #print('Total time: %.3fs\n' % (t2 - t1))

            #  draw_and_show(im_bk, bboxes, points)

                num_boxes = bboxes.shape[0]
                Boxnum = 0  #记录实际人脸个数
                CoordinateList = [None] * num_boxes  #记录每个人脸对应的坐标点
                ROIimage = [None] * num_boxes
                if num_boxes != 0:
                    for i in range(num_boxes):
                        box = bboxes[i]
                        x1 = int(box[0])
                        y1 = int(box[1])
                        x2 = int(box[2])
                        y2 = int(box[3])
                        if (x2 - x1 < 50) or (y2 - y1 < 50) or (y2 - y1 >
                                                                x2 - x1 - 30):
                            continue

                        cv2.rectangle(im_bk, (y1, x1), (y2, x2), (0, 255, 255),
                                      2)
                        CoordinateList[Boxnum] = (y1, x1)
                        ROIimage[Boxnum] = im_bk[x1:x2 + 1, y1:y2 + 1]
                        Boxnum = Boxnum + 1

                for i in range(Boxnum):
                    cv2.imwrite(
                        '/home/kjin/caffe-master/examples/VGGNet/' + str(i) +
                        '.jpg', ROIimage[i])

                feature2 = [None] * Boxnum
                for i in range(Boxnum):

                    X2 = '/home/kjin/caffe-master/examples/VGGNet/' + str(
                        i) + '.jpg'
                    #X2 = '/home/kjin/caffe-master/examples/VGGNet/pycaffe-mtcnn-master/Others/Repired/Test96.jpg'
                    X = DeepIDTest.read_imagelist(X2)
                    out = net.forward_all(data=X)
                    # print out
                    feature2[i] = np.float64(out['fc7'])
                min_predicts = 1
                for Num in range(Boxnum):
                    #t3=time.time()

                    # Calculate_Distance_1(Feature, feature2[Num], metric, min_predicts, Lists_Num)
                    # Calculate_Distance_2(Feature, feature2[Num], metric, min_predicts, Lists_Num)
                    Thread1 = threading.Thread(target=Calculate_Distance_1,
                                               args=(Feature, feature2[Num],
                                                     metric, min_predicts,
                                                     len(Lists)))
                    Thread1.start()
                    #主线程运行,加上上面的线程,共2线程
                    global ThreadingState1
                    global ThreadingState2
                    ThreadingState1 = 0
                    ThreadingState2 = 0
                    i = 1
                    if len(Lists) / 2 is not (len(Lists) + 1) / 2:  # 奇数
                        for sublist in range((len(Lists) - 1) / 2):
                            predicts2 = pw.pairwise_distances(Feature[i],
                                                              feature2[Num],
                                                              metric=metric)
                            i = i + 2
                            if predicts2[0][0] > 0.12:
                                if ThreadingState1 is 1:
                                    break
                                if predicts2[0][0] < min_predicts:
                                    min_predicts = predicts2[0][0]
                            else:
                                min_predicts = predicts2[0][0]
                                ThreadingState2 = 1
                                break
                    else:
                        for i in range(len(Lists) / 2):  # 偶数
                            predicts2 = pw.pairwise_distances(Feature[i],
                                                              feature2[Num],
                                                              metric=metric)
                            i = i + 2
                            if predicts2[0][0] > 0.12:
                                if ThreadingState1 is 1:
                                    break
                                if predicts2[0][0] < min_predicts:
                                    min_predicts = predicts2[0][0]
                            else:
                                min_predicts = predicts2[0][0]
                                ThreadingState2 = 1
                                break
                    #Thread2=threading.Thread(target=Calculate_Distance_2,args=(Feature, feature2[Num], metric, min_predicts, len(Lists)))

                    #Thread2.start()
                    Thread1.join()
                    # Thread2.join()
                    # while ( Thread1.isAlive()) or ( Thread2.isAlive()):
                    #     pass
                    #print ('Total time: %.3fs\n' % (t2 - t1))

                    #predicts = pw.pairwise_distances(Feature[i], feature2[Num], metric=metric)
                    # i=i+1
                    # predicts = mt
                    # if predicts[0][0] >0.12:
                    #  if predicts[0][0] < min_predicts:
                    #      min_predicts = predicts[0][0]
                    # if i==(len(Lists)-1):
                    # accuracy=1
                    if (ThreadingState1 == 0) and (ThreadingState2 == 0):

                        if RecognitionNum[Num] is None:
                            RecognitionNum[Num] = 1
                        else:
                            RecognitionNum[Num] = RecognitionNum[Num] + 1
                        if RecognitionNum[Num] > 0:
                            cv2.putText(im_bk, 'Unknows', CoordinateList[Num],
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL, 2,
                                        (0, 0, 255))
                            #accuracy=1
                            #cv2.imwrite('/home/kjin/caffe-master/examples/VGGNet/ContrastivePicNew/AddError_'+str(Error)+'.jpg', ROIimage[Num])
                            # Error=Error+1
                            RecognitionNum[Num] = 0
                        # t3=time.time()
                        # os.system('sh /home/kjin/caffe-master/examples/VGGNet/beep.sh')
                        # t4=time.time()
                        #print('Beep time: %.3fs\n' % (t4-t3))
                    elif ThreadingState1 or ThreadingState2:
                        RecognitionNum[Num] = 0
                        # min_predicts=predicts[0][0]
                        #accuracy=0
                        cv2.putText(im_bk, 'Me', CoordinateList[Num],
                                    cv2.FONT_HERSHEY_COMPLEX_SMALL, 2,
                                    (0, 255, 0))
                    # break                           #若是本人,立马break,不用再比较
                    cv2.imshow('result', im_bk)
                    t2 = time.time()
                    cv2.waitKey(8)
                    print('Total time: %.3fs\n' % (t2 - t1))
Ejemplo n.º 6
0
if SUBMIT:
    TEST_DB = "plankton_test_lmdb"
    FEATURES_FILE = "test_features.npz"
else:
    #    TEST_DB='plankton_val_lmdb'
    TEST_DB = 'plankton_train_lmdb'
    FEATURES_FILE = "val_features.npz"

ENCODE_FILE = "/home/shai/mocha/data/plankton/encode.txt"
MODEL_FILE = 'inet_deploy8.prototxt'
PRETRAINED = 'inet9.caffemodel'

print "Try to create net..."
net = caffe.Classifier(
    MODEL_FILE,
    PRETRAINED,
    #	mean=np.load('mean.npy'),
    image_dims=(tsize, tsize))
print "Created net"
net.set_phase_test()
net.set_mode_gpu()

env = lmdb.open(TEST_DB)
datum = caffe.proto.caffe_pb2.Datum()


def visualize(features):
    model = TSNE(n_components=2, random_state=0)
    X = model.fit_transform(features)
    print X.shape
    fig, ax = plt.subplots()
Ejemplo n.º 7
0
proto='./gdep.prototxt'
caffe.set_mode_gpu()

bsize=512
mean = np.zeros((3,int(bsize),int(bsize)))
mean[0,:,:]=104.00699
mean[1,:,:]=116.66877
mean[2,:,:]=122.67892
channel_swap = [2,1,0]
raw_scale=255.0;
center_only=False
input_scale=None
image_dims=[bsize,bsize]
ims=(int(bsize),int(bsize))
data = caffe.Classifier(proto,cm, image_dims=image_dims,mean=mean,
            input_scale=input_scale,
             raw_scale=raw_scale,
            channel_swap=channel_swap)

im = [caffe.io.load_image('./img.jpg')]
im2=[caffe.io.resize_image(im[0], ims)]
im3 = np.zeros((1,ims[0],ims[1],im2[0].shape[2]),dtype=np.float32)
im3[0]=im2[0]
caffe_in = np.zeros(np.array(im3.shape)[[0, 3, 1, 2]],dtype=np.float32)
caffe_in[0]=data.transformer.preprocess('data', im3[0])
out = data.forward_all(**{'data': caffe_in})
map=data.blobs['dcsmn'].data
id=data.blobs['sortid'].data
rsize=512
zeronp=np.zeros((rsize,rsize),dtype=np.float32)
ctg=['plane','bicycle','bird','boat','bottle','bus','car','cat','chair','cow','dog','dtable','horse','moterbike','person','plant','sheep','sofa','train','tv']
for i in range(map.shape[0]):
Ejemplo n.º 8
0
def main():

  parser = argparse.ArgumentParser(description='Process some integers.')
  parser.add_argument('--unit', metavar='unit', type=int, help='an unit to visualize e.g. [0, 999]')
  parser.add_argument('--n_iters', metavar='iter', type=int, default=10, help='Number of iterations')
  parser.add_argument('--L2', metavar='w', type=float, default=1.0, nargs='?', help='L2 weight')
  parser.add_argument('--start_lr', metavar='lr', type=float, default=2.0, nargs='?', help='Learning rate')
  parser.add_argument('--end_lr', metavar='lr', type=float, default=-1.0, nargs='?', help='Ending Learning rate')
  parser.add_argument('--seed', metavar='n', type=int, default=0, nargs='?', help='Learning rate')
  parser.add_argument('--xy', metavar='n', type=int, default=0, nargs='?', help='Spatial position for conv units')
  parser.add_argument('--opt_layer', metavar='s', type=str, help='Layer at which we optimize a code')
  parser.add_argument('--act_layer', metavar='s', type=str, default="fc8", help='Layer at which we activate a neuron')
  parser.add_argument('--init_file', metavar='s', type=str, default="None", help='Init image')
  parser.add_argument('--debug', metavar='b', type=int, default=0, help='Print out the images or not')
  parser.add_argument('--clip', metavar='b', type=int, default=0, help='Clip out within a code range')
  parser.add_argument('--bound', metavar='b', type=str, default="", help='The file to an array that is the upper bound for activation range')
  parser.add_argument('--output_dir', metavar='b', type=str, default=".", help='Output directory for saving results')
  parser.add_argument('--net_weights', metavar='b', type=str, default=settings.net_weights, help='Weights of the net being visualized')
  parser.add_argument('--net_definition', metavar='b', type=str, default=settings.net_definition, help='Definition of the net being visualized')
  parser.add_argument('--outName' , metavar='s',type=str,help='name of the output file')
  parser.add_argument('--appendImages', action='append')
  parser.add_argument('--appendNames', action='append')

  args = parser.parse_args()

  # Default to constant learning rate
  if args.end_lr < 0:
    args.end_lr = args.start_lr

  # which neuron to visualize
  print "-------------"
  print " unit: %s  xy: %s" % (args.unit, args.xy)
  print " n_iters: %s" % args.n_iters
  print " L2: %s" % args.L2
  print " start learning rate: %s" % args.start_lr
  print " end learning rate: %s" % args.end_lr
  print " seed: %s" % args.seed
  print " opt_layer: %s" % args.opt_layer
  print " act_layer: %s" % args.act_layer
  print " init_file: %s" % args.init_file
  print " clip: %s" % args.clip
  print " bound: %s" % args.bound
  print "-------------"
  print " debug: %s" % args.debug
  print " output dir: %s" % args.output_dir
  print " net weights: %s" % args.net_weights
  print " net definition: %s" % args.net_definition
  print "-------------"

  params = [
    {
      'layer': args.act_layer,
      'iter_n': args.n_iters,
      'L2': args.L2,
      'start_step_size': args.start_lr,
      'end_step_size': args.end_lr
    }
  ]
  act_unit = args.unit
  caffe.set_mode_gpu()
  #caffe.set_mode_cpu()
  # networks
  generator = caffe.Net(settings.generator_definition, settings.generator_weights, caffe.TEST)
  net = caffe.Classifier(args.net_definition, args.net_weights,
               mean = mean, # ImageNet mean
               channel_swap = (2,1,0)) # the reference model has channels in BGR order instead of RGB

  # input / output layers in generator
  gen_in_layer = "feat"
  gen_out_layer = "deconv0"

  # shape of the code being optimized
  shape = generator.blobs[gen_in_layer].data.shape

  # Fix the seed
  np.random.seed(args.seed)

  

  
  image_list = args.appendImages
  #print image_list
  name_list = args.appendNames
  #print name_list
  # Optimize a code via gradient ascent
  for i in range(len(name_list)):
      start = time.time()
      if len(image_list) > 0:
          start_code, start_image = get_code(image_list[i], args.opt_layer)
          print "Loaded start code: ", start_code.shape
      else:
          start_code = np.random.normal(0, 1, shape)

      # Load the activation range
      upper_bound = lower_bound = None
      # Set up clipping bounds
      if args.bound != "":
          n_units = shape[1]
          upper_bound = np.loadtxt(args.bound, delimiter=' ', usecols=np.arange(0, n_units), unpack=True)
          upper_bound = upper_bound.reshape(start_code.shape)

      # Lower bound of 0 due to ReLU
      lower_bound = np.zeros(start_code.shape)

      print "Running file %d or %d , filename : %s"%(i , len(name_list) , image_list[i] )

      output_image , best_act_final = activation_maximization(net, generator, gen_in_layer, gen_out_layer, start_code, params, 
               clip=args.clip, unit=act_unit, xy=args.xy, debug=args.debug,
               upper_bound=upper_bound, lower_bound=lower_bound)
      print output_image.shape
      
      #Save image
      
      #filename = "%s/%s_%s_%s_%s_%s_%s__%s.jpg" % (
      #    args.output_dir,
      #    args.init_file.split('.')[0],
      #    args.act_layer, 
      #    str(args.unit).zfill(4), 
      #    str(args.n_iters).zfill(2), 
      #    args.L2, 
      #    args.start_lr,
      #    args.seed
      #  )
      end = time.time()
      print '%30s' % 'Executed DGN-AM in ', str((end - start)*1000), 'ms'
      start = time.time()
      filename = "%s/%s_%s_%s_.jpg" % (
              args.output_dir,
              name_list[i],
              str(args.unit).zfill(4),
              str(best_act_final)
      )
      # Save image
      save_image(output_image, filename)
      print "Saved to %s" % filename
      end = time.time()
      print '%30s' % 'Saved File in in ', str((end - start)*1000), 'ms'
Ejemplo n.º 9
0
def main(
    input,
    output,
    image_type,
    gpu,
    model_path,
    model_name,
    preview,
    octaves,
    octave_scale,
    iterations,
    jitter,
    zoom,
    stepsize,
    blend,
    layers,
    guide_image,
    start_frame,
    end_frame,
    verbose,
):

    make_sure_path_exists(input)
    make_sure_path_exists(output)

    # let max nr of frames

    nrframes = len([
        name for name in os.listdir(input)
        if os.path.isfile(os.path.join(input, name))
    ])
    if nrframes == 0:
        print('no frames to process found')
        sys.exit(0)

    if preview is None:
        preview = 0
    if octaves is None:
        octaves = 4
    if octave_scale is None:
        octave_scale = 1.5
    if iterations is None:
        iterations = 5
    if jitter is None:
        jitter = 32
    if zoom is None:
        zoom = 1
    if stepsize is None:
        stepsize = 1.5
    if blend is None:  # can be nr (constant), random, or loop
        blend = 0.5
    if verbose is None:
        verbose = 1
    if layers is None:  # ['inception_4c/output']
        layers = 'customloop'
    if start_frame is None:
        frame_i = 1
    else:
        frame_i = int(start_frame)
    if not end_frame is None:
        nrframes = int(end_frame) + 1
    else:
        nrframes = nrframes + 1

    # Load DNN

    net_fn = model_path + 'deploy.prototxt'
    param_fn = model_path + model_name  # 'bvlc_googlenet.caffemodel'

    if gpu is None:
        print("SHITTTTTTTTTTTTTT You're running CPU man =D")
    else:
        caffe.set_mode_gpu()
        caffe.set_device(int(args.gpu))
        print('GPU mode [device id: %s]' % args.gpu)
        print("using GPU, but you'd still better make a cup of coffee")

    # Patching model to be able to compute gradients.
    # Note that you can also manually add "force_backward: true" line to "deploy.prototxt".

    model = caffe.io.caffe_pb2.NetParameter()
    text_format.Merge(open(net_fn).read(), model)
    model.force_backward = True
    open('tmp.prototxt', 'w').write(str(model))

    net = caffe.Classifier(
        'tmp.prototxt',
        param_fn,
        mean=np.float32([104.0, 116.0, 122.0]),
        channel_swap=(2, 1, 0))  # ImageNet mean, training set dependent

    # the reference model has channels in BGR order instead of RGB

    if verbose == 3:
        from IPython.display import clear_output, Image, display
        print('display turned on')
    frame = np.float32(
        PIL.Image.open(input + '/%08d.%s' % (frame_i, image_type)))
    if preview is not 0:
        frame = np.float32(
            resizePicture(input + '/%08d.%s' % (frame_i, image_type), preview))

    now = time.time()
    totaltime = 0

    if blend == 'loop':
        blend_forward = True
        blend_at = 0.4
        blend_step = 0.1

    for i in range(frame_i, nrframes):
        print('Processing frame #{}'.format(frame_i))

        # Choosing Layer

        if layers == 'customloop':  # loop over layers as set in layersloop array
            endparam = layersloop[frame_i % len(layersloop)]
        else:

            # loop through layers one at a time until this specific layer

            endparam = layers[frame_i % len(layers)]

        # Choosing between normal dreaming, and guided dreaming

        if guide_image is None:
            frame = deepdream(
                net,
                frame,
                image_type=image_type,
                verbose=verbose,
                iter_n=iterations,
                step_size=stepsize,
                octave_n=octaves,
                octave_scale=octave_scale,
                jitter=jitter,
                end=endparam,
            )
        else:
            guide = np.float32(PIL.Image.open(guide_image))
            print('Setting up Guide with selected image')
            guide_features = prepare_guide(net,
                                           PIL.Image.open(guide_image),
                                           end=endparam)

            frame = deepdream_guided(
                net,
                frame,
                image_type=image_type,
                verbose=verbose,
                iter_n=iterations,
                step_size=stepsize,
                octave_n=octaves,
                octave_scale=octave_scale,
                jitter=jitter,
                end=endparam,
                objective_fn=objective_guide,
                guide_features=guide_features,
            )

        saveframe = output + '/%08d.%s' % (frame_i, image_type)

        later = time.time()
        difference = int(later - now)
        totaltime += difference
        avgtime = totaltime / i

        # Stats (stolen + adapted from Samim: https://github.com/samim23/DeepDreamAnim/blob/master/dreamer.py)

        print('***************************************')
        print('Saving Image As: ' + saveframe)
        print('Frame ' + str(i) + ' of ' + str(nrframes - 1))
        print('Frame Time: ' + str(difference) + 's')
        timeleft = avgtime * (nrframes - 1 - frame_i)
        (m, s) = divmod(timeleft, 60)
        (h, m) = divmod(m, 60)
        print('Estimated Total Time Remaining: ' + str(timeleft) \
            + 's (' + '%d:%02d:%02d' % (h, m, s) + ')')
        print('***************************************')

        PIL.Image.fromarray(np.uint8(frame)).save(saveframe)
        newframe = input + '/%08d.%s' % (frame_i, image_type)

        if blend == 0:
            newimg = PIL.Image.open(newframe)
            if preview is not 0:
                newimg = resizePicture(newframe, preview)
            frame = newimg
        else:

            if blend == 'random':
                blendval = randint(5, 10) / 10.
            elif blend == 'loop':
                if blend_at > 1 - blend_step:
                    blend_forward = False
                elif blend_at <= 0.5:
                    blend_forward = True
                if blend_forward:
                    blend_at += blend_step
                else:
                    blend_at -= blend_step
                blendval = blend_at
            else:
                blendval = float(blend)
            frame = morphPicture(saveframe, newframe, blendval, preview)

        frame = np.float32(frame)

        now = time.time()
        frame_i += 1
Ejemplo n.º 10
0
def main():
    inputfile = '/media/jwong/Transcend/VQADataset/imagePaths.txt'
    outputfile = '/media/jwong/Transcend/VQADataset/VQATrainOutput'
    jsonFile = '/media/jwong/Transcend/VQADataset/VQAImgFeatures_Train.json'
    errorLogFile = '/media/jwong/Transcend/VQADataset/ImgFeatures_TrainLog.txt'

    print 'Reading images from "', inputfile
    print 'Writing vectors to "', outputfile
    print 'Writing to json file "', jsonFile

    # Setting this to CPU, but feel free to use GPU if you have CUDA installed
    caffe.set_mode_cpu()
    # Loading the Caffe model, setting preprocessing parameters
    net = caffe.Classifier(model_prototxt,
                           model_trained,
                           mean=np.load(mean_path).mean(1).mean(1),
                           channel_swap=(2, 1, 0),
                           raw_scale=255,
                           image_dims=(480, 640))

    # Loading class labels
    with open(imagenet_labels) as f:
        labels = f.readlines()

    #Names & sizes of network layers
    #print [(k, v.data.shape) for k, v in net.blobs.items()]

    resultJSONData = {}
    errorMessages = []

    count = 0
    # Processing one image at a time, print predictions
    with open(inputfile, 'r') as reader:
        with open(outputfile, 'w') as writer:
            writer.truncate()
            for image_path in reader:
                image_path = image_path.strip()
                input_image = caffe.io.load_image(image_path)
                prediction = net.predict([input_image], oversample=False)
                print os.path.basename(image_path), ' : ', labels[
                    prediction[0].argmax()].strip(), ' (', prediction[0][
                        prediction[0].argmax()], ')'
                count = count + 1
                try:
                    #Get image ID
                    splitPath = image_path.split('/')
                    imgNameParts = splitPath[len(splitPath) - 1].split(
                        '_')  #COCO, train, XXX.jpg
                    suffix = imgNameParts[len(imgNameParts) - 1]  #XXX.jpg
                    img_id = int(suffix.split('.')[0])
                    print 'Reading img ', img_id
                    print 'Images processed : ', count

                    # filename, array data to be saved, format, delimiter
                    featureData = net.blobs[layer_name].data[0].reshape(
                        1, -1).tolist()
                    np.savetxt(writer, featureData, fmt='%.8g')
                    resultJSONData[img_id] = featureData

                except ValueError:
                    #Invalid image names
                    errorMessages.append(image_path)

    with open(jsonFile, 'w') as jsonOut:
        json.dump(resultJSONData, jsonOut)
    with open(errorLogFile, 'w') as logFile:
        for msg in errorMessages:
            logFile.write(msg)
 def InitializeDetecter(self):
     self._net = caffe.Classifier(self._NET_FILE, self._TRAINED_MODEL_FILE)
     if os.path.exists(self._outputDir) is False:
         os.system('mkdir ' + self._outputDir)
Ejemplo n.º 12
0
import pickle
import skimage
import redis
caffe_root = '/home/jiangwei/bin/caffe-master/'
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
import sklearn.metrics.pairwise as pw

#GPU加速
caffe.set_mode_gpu()


#加载caffe模型
global net
net=caffe.Classifier('vgg/deploy.prototxt','vgg/vgg_face.caffemodel',caffe.TEST)


#提取特征数组
def get_feature(path1):
    global net
    #加载图片
    X=read_image(path1)
    test_num=np.shape(X)[0]
    #X  作为 模型的输入
    out = net.forward_all(blobs=['pool5'],data = X)
    # print out.keys()


    feature1 = np.float64(out["pool5"])
#load our mean file and reshape it accordingly
a = caffe.io.caffe_pb2.BlobProto()
file = open('%s/DB_train_w32_%d.binaryproto' % (BASE, FOLD), 'rb')
data = file.read()
a.ParseFromString(data)
means = a.data
means = np.asarray(means)
means = means.reshape(3, 32, 32)

#make sure we use teh GPU otherwise things will take a very long time
caffe.set_mode_gpu()

#load the model
net = caffe.Classifier(MODEL_FILE,
                       PRETRAINED,
                       mean=means,
                       channel_swap=(2, 1, 0),
                       raw_scale=255,
                       image_dims=(32, 32))

#see which files we need to produce output for in this fold
#we look at the parent IDs in the test file and only compute those images
#as they've been "held out" from the training set
files = open('%s/test_w32_parent_%d.txt' % (BASE, FOLD), 'rb')

start_time = time.time()
start_time_iter = 0

#go into the image directory so we can use glob a bit easier
os.chdir(IMAGE_DIR)

for base_fname in files:
import numpy as np

import numpy as np
import os
import skimage
import sys
import caffe
import math
import sklearn.metrics.pairwise as pw
from fr_wuqianliang_sphereface import *
from matlab_cp2tform import get_similarity_transform_for_cv2

#  load Caffe model

global net
net = caffe.Classifier('sphereface_deploy.prototxt',
                       'sphereface_model_iter_28000.caffemodel')


def getface(im, boxes):
    constImgfile = "./tmp"
    for i in range(len(boxes)):
        cv2.rectangle(im, (int(boxes[i][0]), int(boxes[i][1])),
                      (int(boxes[i][2]), int(boxes[i][3])), (0, 255, 0), 1)
        cropped = im[int(boxes[i][1]):int(boxes[i][3]),
                     int(boxes[i][0]):int(boxes[i][2]), :]

        #cropped = cv2.resize(cropped, (96, 96), interpolation=cv2.INTER_CUBIC )
        img = cropped

    #cv2.imshow('src',img)
# File containing the class labels
imagenet_labels = caffe_root + 'data/ilsvrc12/synset_words.txt'

# Path to the mean image (used for input processing)
mean_path = caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'

# Name of the layer we want to extract
layer_name = 'pool5/7x7_s1'

sys.path.insert(0, caffe_root + 'python')
import caffe

caffe.set_mode_gpu()
net = caffe.Classifier(model_prototxt,
                       model_trained,
                       mean=np.load(mean_path).mean(1).mean(1),
                       channel_swap=(2, 1, 0),
                       raw_scale=255,
                       image_dims=(256, 256))
# print [(k, v.data.shape) for k, v in net.blobs.items()]

# Loading class labels
with open(imagenet_labels) as f:
    labels = f.readlines()


def forward_cnn(image_path):
    image_path = image_path.strip()
    input_image = caffe.io.load_image(image_path)
    prediction = net.predict([input_image], oversample=False)
    print os.path.basename(image_path), ' : ', labels[prediction[0].argmax(
    )].strip(), ' (', prediction[0][prediction[0].argmax()], ')'
Ejemplo n.º 16
0
import sys
import caffe
import matplotlib.pyplot as plt
import os

caffe_root = '../'
MODEL_FILE = 'mnist/lenet.prototxt'
PRETRAINED = 'mnist/lenet/lenet_iter_10000.caffemodel'
IMAGE_FILE = 'mnist/slike/test7-4.png'  #image path

input_image = caffe.io.load_image(IMAGE_FILE, color=False)

digits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for i in range(50):
    net = caffe.Classifier(
        MODEL_FILE, PRETRAINED, raw_scale=255,
        image_dims=(28,
                    28))  #, image_dims=(28, 28), raw_scale=0.00392156862745
    caffe.set_mode_cpu()
    score = net.predict([input_image], oversample=False)
    if np.argmax(score) == 0:
        digits[0] += 1
        continue
    elif np.argmax(score) == 1:
        digits[1] += 1
        continue
    elif np.argmax(score) == 2:
        digits[2] += 1
        continue
    elif np.argmax(score) == 3:
        digits[3] += 1
        continue
Ejemplo n.º 17
0
caffe_root = './caffe-master/'
sys.path.insert(0, caffe_root + 'python')
sys.path.append('/home/gong/Downloads/caffe-master/python')
import caffe

mean_filename='./mean.binaryproto'
proto_data = open(mean_filename, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean  = caffe.io.blobproto_to_array(a)[0]


gender_net_pretrained='./gender_train_net.caffemodel'
gender_net_model_file='./deploy_gender.prototxt'
gender_net = caffe.Classifier(gender_net_model_file, gender_net_pretrained,
                       mean=mean,
                       channel_swap=(2,1,0),
                       raw_scale=127,
                       image_dims=(128, 128))

gender_list=['Male','Female']

## ===============read the images in folder './image' ==============##
#path = './image'
#images = os.listdir(path)
#n = 1;
#for file in images:
#    if not os.path.isdir(file):
#        example_image = open(path + "/" + file)
#        input_image = caffe.io.load_image(example_image)
 #       _ = plt.imshow(input_image)
        
Ejemplo n.º 18
0
def main():

    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('--units',
                        metavar='units',
                        type=str,
                        help='an unit to visualize e.g. [0, 999]')
    parser.add_argument('--n_iters',
                        metavar='iter',
                        type=int,
                        default=10,
                        help='Number of sampling steps per each unit')
    parser.add_argument(
        '--threshold',
        metavar='w',
        type=float,
        default=-1.0,
        nargs='?',
        help='The probability threshold to decide whether to keep an image')
    parser.add_argument(
        '--save_every',
        metavar='save_iter',
        type=int,
        default=1,
        help='Save a sample every N iterations. 0 to disable saving')
    parser.add_argument('--reset_every',
                        metavar='reset_iter',
                        type=int,
                        default=0,
                        help='Reset the code every N iterations')
    parser.add_argument('--lr',
                        metavar='lr',
                        type=float,
                        default=2.0,
                        nargs='?',
                        help='Learning rate')
    parser.add_argument('--lr_end',
                        metavar='lr',
                        type=float,
                        default=-1.0,
                        nargs='?',
                        help='Ending Learning rate')
    parser.add_argument('--epsilon2',
                        metavar='lr',
                        type=float,
                        default=1.0,
                        nargs='?',
                        help='Ending Learning rate')
    parser.add_argument('--epsilon1',
                        metavar='lr',
                        type=float,
                        default=1.0,
                        nargs='?',
                        help='Ending Learning rate')
    parser.add_argument('--epsilon3',
                        metavar='lr',
                        type=float,
                        default=1.0,
                        nargs='?',
                        help='Ending Learning rate')
    parser.add_argument('--seed',
                        metavar='n',
                        type=int,
                        default=0,
                        nargs='?',
                        help='Random seed')
    parser.add_argument('--xy',
                        metavar='n',
                        type=int,
                        default=0,
                        nargs='?',
                        help='Spatial position for conv units')
    parser.add_argument('--opt_layer',
                        metavar='s',
                        type=str,
                        help='Layer at which we optimize a code')
    parser.add_argument('--act_layer',
                        metavar='s',
                        type=str,
                        default="fc8",
                        help='Layer at which we activate a neuron')
    parser.add_argument('--init_file',
                        metavar='s',
                        type=str,
                        default="None",
                        help='Init image')
    parser.add_argument('--write_labels',
                        action='store_true',
                        default=False,
                        help='Write class labels to images')
    parser.add_argument('--output_dir',
                        metavar='b',
                        type=str,
                        default=".",
                        help='Output directory for saving results')
    parser.add_argument('--net_weights',
                        metavar='b',
                        type=str,
                        default=settings.encoder_weights,
                        help='Weights of the net being visualized')
    parser.add_argument('--net_definition',
                        metavar='b',
                        type=str,
                        default=settings.encoder_definition,
                        help='Definition of the net being visualized')

    args = parser.parse_args()

    # Default to constant learning rate
    if args.lr_end < 0:
        args.lr_end = args.lr

    # summary
    print "-------------"
    print " units: %s    xy: %s" % (args.units, args.xy)
    print " n_iters: %s" % args.n_iters
    print " reset_every: %s" % args.reset_every
    print " save_every: %s" % args.save_every
    print " threshold: %s" % args.threshold

    print " epsilon1: %s" % args.epsilon1
    print " epsilon2: %s" % args.epsilon2
    print " epsilon3: %s" % args.epsilon3

    print " start learning rate: %s" % args.lr
    print " end learning rate: %s" % args.lr_end
    print " seed: %s" % args.seed
    print " opt_layer: %s" % args.opt_layer
    print " act_layer: %s" % args.act_layer
    print " init_file: %s" % args.init_file
    print "-------------"
    print " output dir: %s" % args.output_dir
    print " net weights: %s" % args.net_weights
    print " net definition: %s" % args.net_definition
    print "-------------"

    # encoder and generator for images
    encoder = caffe.Net(settings.encoder_definition, settings.encoder_weights,
                        caffe.TEST)
    generator = caffe.Net(settings.generator_definition,
                          settings.generator_weights, caffe.TEST)

    # condition network, here an image classification net
    net = caffe.Classifier(
        args.net_definition,
        args.net_weights,
        mean=np.float32([104.0, 117.0, 123.0]),  # ImageNet mean
        channel_swap=(
            2, 1,
            0))  # the reference model has channels in BGR order instead of RGB

    # Fix the seed
    np.random.seed(args.seed)

    if args.init_file != "None":
        start_code, start_image = get_code(encoder=encoder,
                                           path=args.init_file,
                                           layer=args.opt_layer)

        print "Loaded start code: ", start_code.shape
    else:
        # shape of the code being optimized
        shape = generator.blobs[settings.generator_in_layer].data.shape
        start_code = np.random.normal(0, 1, shape)
        print ">>", np.min(start_code), np.max(start_code)

    # Separate the dash-separated list of units into numbers
    conditions = [{
        "unit": int(u),
        "xy": args.xy
    } for u in args.units.split("_")]

    # Optimize a code via gradient ascent
    sampler = ClassConditionalSampler()
    output_image, list_samples = sampler.sampling(
        condition_net=net,
        image_encoder=encoder,
        image_generator=generator,
        gen_in_layer=settings.generator_in_layer,
        gen_out_layer=settings.generator_out_layer,
        start_code=start_code,
        n_iters=args.n_iters,
        lr=args.lr,
        lr_end=args.lr_end,
        threshold=args.threshold,
        layer=args.act_layer,
        conditions=conditions,
        epsilon1=args.epsilon1,
        epsilon2=args.epsilon2,
        epsilon3=args.epsilon3,
        output_dir=args.output_dir,
        reset_every=args.reset_every,
        save_every=args.save_every)

    # Output image
    filename = "%s/%s_%04d_%04d_%s_h_%s_%s_%s__%s.jpg" % (
        args.output_dir, args.act_layer, conditions[0]["unit"], args.n_iters,
        args.lr, str(args.epsilon1), str(args.epsilon2), str(
            args.epsilon3), args.seed)

    # Save the final image
    util.save_image(output_image, filename)
    print "%s/%s" % (os.getcwd(), filename)

    # Write labels to images
    print "Saving images..."
    for p in list_samples:
        img, name, label = p
        util.save_image(img, name)
        if args.write_labels:
            util.write_label_to_img(name, label)
Ejemplo n.º 19
0
import pandas as pd
import numpy as np
import caffe
import os
import glob
import re

net = caffe.Classifier('/home/seiji/caffe/swagnet_deploy.prototxt',
                       '/home/seiji/Desktop/swaga_iter_50000.caffemodel',
                       mean=np.load('/home/seiji/out.npy'),
                       channel_swap=(2, 1, 0),
                       raw_scale=255,
                       image_dims=(512, 512))
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
results = pd.DataFrame(columns=['filename', 'prediction'])
net.blobs['data'].reshape(1, 3, 512, 512)
n = 0
path = '/home/seiji/Desktop/Data/predictions/test/'
listing1 = os.listdir(path)
listing = sorted(listing1, key=lambda x: (int(re.sub('\D', '', x)), x))

for infile in listing:
    input_image = caffe.io.load_image(
        '/home/seiji/Desktop/Data/predictions/test/' + infile)
    prediction = net.predict([input_image], oversample=False)
    print 'predicted class:', prediction
    pred = prediction.argmax()
    results.loc[n] = np.array([infile, pred])
    n += 1
Ejemplo n.º 20
0
def get_codes_and_labels(params):
    device_id = int(params["gpu"])
    caffe.set_device(device_id)
    caffe.set_mode_gpu()
    model_file = params['model_file']
    pretrained_model = params['pretrained_model']
    dims = params['image_dims']
    scale = params['scale']
    database = params['database']
    validation = params['validation']
    batch_size = params['batch_size']

    net = caffe.Classifier(model_file,
                           pretrained_model,
                           channel_swap=(2, 1, 0),
                           image_dims=dims,
                           mean=np.array([103.939, 116.779, 123.68]),
                           raw_scale=scale)

    database_code = []
    validation_code = []
    database_labels = []
    validation_labels = []
    cur_pos = 0

    while 1:
        lines = database[cur_pos:cur_pos + batch_size]
        if len(lines) == 0:
            break
        cur_pos = cur_pos + len(lines)
        images = [
            caffe.io.load_image(line.strip().split(" ")[0]) for line in lines
        ]
        labels = [[int(i) for i in line.strip().split(" ")[1:]]
                  for line in lines]
        codes = net.predict(images, oversample=False)
        #codes = net.predict(images)
        [database_code.append(c) for c in codes]
        [database_labels.append(l) for l in labels]

        print str(cur_pos) + "/" + str(len(database))
        if len(lines) < batch_size:
            break

    cur_pos = 0
    while 1:
        lines = validation[cur_pos:cur_pos + batch_size]
        if len(lines) == 0:
            break
        cur_pos = cur_pos + len(lines)
        images = [
            caffe.io.load_image(line.strip().split(" ")[0]) for line in lines
        ]
        labels = [[int(i) for i in line.strip().split(" ")[1:]]
                  for line in lines]

        codes = net.predict(images, oversample=False)
        #codes = net.predict(images)
        [validation_code.append(c) for c in codes]
        [validation_labels.append(l) for l in labels]

        print str(cur_pos) + "/" + str(len(validation))
        if len(lines) < batch_size:
            break

    return dict(database_code=np.sign(np.array(database_code)),
                database_labels=np.array(database_labels),
                validation_code=np.sign(np.array(validation_code)),
                validation_labels=np.array(validation_labels))
# PRETRAINED = '../examples/kaggle2/googlenet/bvlc_googlenet_lr_1e-3_layer_2_iter_125000.caffemodel'
# PRETRAINED = '../examples/kaggle2/googlenet/bvlc_googlenet_txt_lr_1e-2_1020_iter_155000.caffemodel'
# PRETRAINED = '../examples/kaggle2/googlenet/bvlc_googlenet_txt_lr_1e-3_1020_iter_95000.caffemodel'
# PRETRAINED = '../examples/kaggle2/googlenet/bvlc_googlenet_lr_1e-3_layer_2_iter_155000.caffemodel'
# PRETRAINED = '../examples/kaggle2/googlenet/bvlc_googlenet_txt_lr_1e-2_1020_iter_160000.caffemodel'
# PRETRAINED = '../examples/kaggle2/googlenet/bvlc_googlenet_lr_1e-3_layer_2_iter_190000.caffemodel'
# PRETRAINED = '../examples/kaggle2/googlenet/bvlc_googlenet_txt_lr_1e-2_1020_iter_200000.caffemodel'
PRETRAINED = '/media/VSlab/coldmanck/fast-rcnn/caffe-fast-rcnn/examples/kaggle2/googlenet/bvlc_googlenet_txt_lr_1e-2_resize_rotated_iter_15000.caffemodel'

caffe.set_device(6)
caffe.set_mode_gpu()

net = caffe.Classifier(
    MODEL_FILE,
    PRETRAINED,
    mean=np.load('/home/coldmanck/kaggle/kaggle_mean.npy').mean(1).mean(1),
    # mean=np.load('../python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1),
    channel_swap=(2, 1, 0),
    raw_scale=255,
    image_dims=(224, 224))

total_num = 0
correct = 0
correct5 = 0
'''
net = caffe.Net(MODEL_FILE, PRETRAINED, caffe.TEST)

# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load('/home/coldmanck/kaggle/kaggle_mean.npy').mean(1).mean(1)) # mean pixel
transformer.set_raw_scale('data', 255)  # the reference model operates on images in [0,255] range instead of [0,1]
Ejemplo n.º 22
0
# -*- coding: utf-8 -*-

import os
import sys
import numpy as np
import matplotlib.pyplot as plt

caffe_root = os.environ.get('CAFFE_ROOT')

sys.path.insert(0, caffe_root + '/python')  #把pycaffe所在路径添加到环境变量
import caffe

#指定网络结构 与 lenet_train_test.prototxt不同
MODEL_FILE = 'lenet.prototxt'
PRETRAINED = 'quick_draw_lenet_iter_10000.caffemodel'
#图片已经处理成 lenet.prototxt的输入要求(尺寸28x28)且已经二值化为黑白色
IMAGE_FILE = '../data/quickdraw_val_image/full_Fnumpy_bitmap_Fcat_6501.jpg'

# print os.path.exists(MODEL_FILE)
# print os.path.exists(PRETRAINED)
# print os.path.exists(IMAGE_FILE)

input_image = caffe.io.load_image(IMAGE_FILE, color=False)
net = caffe.Classifier(MODEL_FILE, PRETRAINED)
prediction = net.predict([input_image], oversample=False)
caffe.set_mode_cpu()
print 'predicted class:', prediction
Ejemplo n.º 23
0
import sys

sys.path.insert(0, caffe_root + 'python')
import caffe

# DEFN
curdir, _ = os.path.split(__file__)
MODEL_FILE = os.path.join(curdir, '../deploy_lb.prototxt')
PRETRAINED = os.path.join(curdir, '../models/lb65_iter_100000.caffemodel')
MEAN_FILE = os.path.join(curdir, '../data/64x64/ndsb_mean_test.npy')
TEST_FILE = os.path.join(curdir, '../data/test_final.txt')
N = 20000  # batch size for our pythno script to feed to caffe to avoid memory issues

net = caffe.Classifier(MODEL_FILE,
                       PRETRAINED,
                       mean=np.load(MEAN_FILE),
                       raw_scale=255,
                       image_dims=(57, 57),
                       gpu=True)
caffe.set_mode_gpu()
caffe.set_phase_test()

# Loading test paths
test_doc = np.genfromtxt(TEST_FILE, dtype='str')
test_files = test_doc


# Partition test_files for memory sake
def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    #for i in xrange(0, len(l), n):
Ejemplo n.º 24
0
def main(argv):
    pycaffe_dir = os.path.dirname(__file__)

    parser = argparse.ArgumentParser()
    print "here"
    # Required arguments: input and output files.
    parser.add_argument("input_file", help="Input image, directory, or npy.")
    parser.add_argument("output_file", help="Output npy filename.")
    # Optional arguments.
    parser.add_argument(
        "--model_def",
        default=os.path.join(
            pycaffe_dir, "../models/bvlc_reference_caffenet/deploy.prototxt"),
        help="Model definition file.")
    parser.add_argument(
        "--pretrained_model",
        default=os.path.join(
            pycaffe_dir,
            "../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"
        ),
        help="Trained model weights file.")
    parser.add_argument("--gpu",
                        action='store_true',
                        help="Switch for gpu computation.")
    parser.add_argument(
        "--center_only",
        action='store_true',
        help="Switch for prediction from center crop alone instead of " +
        "averaging predictions across crops (default).")
    parser.add_argument(
        "--images_dim",
        default='256,256',
        help="Canonical 'height,width' dimensions of input images.")
    parser.add_argument(
        "--mean_file",
        default=os.path.join(pycaffe_dir,
                             'caffe/imagenet/ilsvrc_2012_mean.npy'),
        help="Data set image mean of [Channels x Height x Width] dimensions " +
        "(numpy array). Set to '' for no mean subtraction.")
    parser.add_argument(
        "--input_scale",
        type=float,
        help="Multiply input features by this scale to finish preprocessing.")
    parser.add_argument(
        "--raw_scale",
        type=float,
        default=255.0,
        help="Multiply raw input by this scale before preprocessing.")
    parser.add_argument(
        "--channel_swap",
        default='2,1,0',
        help="Order to permute input channels. The default converts " +
        "RGB -> BGR since BGR is the Caffe default by way of OpenCV.")
    parser.add_argument(
        "--ext",
        default='jpg',
        help="Image file extension to take as input when a directory " +
        "is given as the input file.")
    args = parser.parse_args()

    image_dims = [int(s) for s in args.images_dim.split(',')]

    mean, channel_swap = None, None
    if args.mean_file:
        mean = np.load(args.mean_file)
    if args.channel_swap:
        channel_swap = [int(s) for s in args.channel_swap.split(',')]

    if args.gpu:
        caffe.set_mode_gpu()
        print("GPU mode")
    else:
        caffe.set_mode_cpu()
        print("CPU mode")

    # Make classifier.
    classifier = caffe.Classifier(args.model_def,
                                  args.pretrained_model,
                                  image_dims=image_dims,
                                  mean=mean,
                                  input_scale=args.input_scale,
                                  raw_scale=args.raw_scale,
                                  channel_swap=channel_swap)

    # Load numpy array (.npy), directory glob (*.jpg), or image file.
    args.input_file = os.path.expanduser(args.input_file)
    if args.input_file.endswith('npy'):
        print("Loading file: %s" % args.input_file)
        inputs = np.load(args.input_file)
    elif os.path.isdir(args.input_file):
        print("Loading folder: %s" % args.input_file)
        inputs = [
            caffe.io.load_image(im_f)
            for im_f in glob.glob(args.input_file + '/*.' + args.ext)
        ]
    else:
        print("Loading file: %s" % args.input_file)
        inputs = [caffe.io.load_image(args.input_file)]

    print("Classifying %d inputs." % len(inputs))

    # Classify.
    start = time.time()
    predictions = classifier.predict(inputs, not args.center_only)
    print("Done in %.2f s." % (time.time() - start))

    # Save
    print("Saving results into %s" % args.output_file)
    np.save(args.output_file, predictions)
Ejemplo n.º 25
0
# load the trained net
#

MODEL = '/u/amo-d0/grad/rbalten/research/deeptransient/src/caffemodels/deploy.prototxt'
PRETRAINED = '/u/amo-d0/grad/rbalten/research/deeptransient/src/caffemodels/transientneth.caffemodel'
MEAN = '../mean/transient_mean.binaryproto'

# load the mean image
blob = caffe.io.caffe_pb2.BlobProto()
file = open(MEAN, 'rb')
blob.ParseFromString(file.read())
means = caffe.io.blobproto_to_array(blob)
means = means[0]

caffe.set_mode_cpu()
net = caffe.Classifier(MODEL, PRETRAINED, caffe.TEST)

#
# process
#
ix = 0
db = lmdb.open(db_name)

# get all keys
with db.begin(write=False) as db_txn:
    for (key, value) in db_txn.cursor():
        im_datum = caffe.io.caffe_pb2.Datum()
        im_datum.ParseFromString(value)
        im = caffe.io.datum_to_array(im_datum)

        # subtract mean & resize
Ejemplo n.º 26
0
    f = StringIO()
    PIL.Image.fromarray(a).save(f, fmt)
    display(Image(data=f.getvalue()))


# Patching model to be able to compute gradients.
# Note that you can also manually add "force_backward: true" line to "deploy.prototxt".
model = caffe.io.caffe_pb2.NetParameter()
text_format.Merge(open(net_fn).read(), model)
model.force_backward = True
open('tmp.prototxt', 'w').write(str(model))

net = caffe.Classifier(
    'tmp.prototxt',
    param_fn,
    mean=np.float32([104.0, 116.0,
                     122.0]),  # ImageNet mean, training set dependent
    channel_swap=(
        2, 1,
        0))  # the reference model has channels in BGR order instead of RGB


# a couple of utility functions for converting to and from Caffe's input image layout
def preprocess(net, img):
    return np.float32(np.rollaxis(img, 2)[::-1]) - net.transformer.mean['data']


def deprocess(net, img):
    return np.dstack((img + net.transformer.mean['data'])[::-1])


def objective_L2(dst):
    print "Usage: python create_kaggle_submission_probability.py deploy.prototxt model.caffemodel mean.npy <test_data_dir>"
    sys.exit()

DEPLOY_PROTOTXT = sys.argv[1]  #deploy.prototxt
CAFFE_MODEL = sys.argv[2]  #model.caffemodel
MEAN_FILE = sys.argv[3]  #mean.npy
TEST_DATA_DIR = sys.argv[4]
MODEL_POSTFIX = CAFFE_MODEL.split(os.sep)[-2]  # not safe way
BATCH_SIZE = 1  # edit it to fit your GPU memory

print "MODEL_POSTFIX", MODEL_POSTFIX

caffe.set_mode_gpu()
net = caffe.Classifier(DEPLOY_PROTOTXT,
                       CAFFE_MODEL,
                       mean=np.load(MEAN_FILE).mean(1).mean(1),
                       channel_swap=(2, 1, 0),
                       raw_scale=255,
                       image_dims=(256, 256))


def predict_proba(images_batch):

    predictions = net.predict(images_batch)

    #taking dog class predictions
    batch_predictions = predictions[:, 1]
    batch_predictions = batch_predictions[..., np.newaxis]

    return batch_predictions  #probability that the image is a dog (1 = dog, 0 = cat)

Ejemplo n.º 28
0
def main(argv):
    pycaffe_dir = os.path.dirname(__file__)

    parser = argparse.ArgumentParser()
    # Required arguments: input and output files.
    parser.add_argument("input_file", help="Input image, directory, or npy.")
    parser.add_argument("output_file", help="Output npy filename.")
    # Optional arguments.
    parser.add_argument("--model_def",
                        default=os.path.join(pycaffe_dir,
                                             "python/lenet.prototxt"),
                        help="Model definition file.")
    parser.add_argument("--pretrained_model",
                        default=os.path.join(
                            pycaffe_dir, "python/lenet_iter_5000.caffemodel"),
                        help="Trained model weights file.")
    parser.add_argument("--gpu",
                        action='store_true',
                        help="Switch for gpu computation.")
    parser.add_argument(
        "--center_only",
        action='store_true',
        help="Switch for prediction from center crop alone instead of " +
        "averaging predictions across crops (default).")
    parser.add_argument(
        "--images_dim",
        default='28,28',
        help="Canonical 'height,width' dimensions of input images.")
    parser.add_argument(
        "--mean_file",
        default=os.path.join(pycaffe_dir,
                             'caffe/imagenet/ilsvrc_2012_mean.npy'),
        help="Data set image mean of H x W x K dimensions (numpy array). " +
        "Set to '' for no mean subtraction.")
    parser.add_argument(
        "--input_scale",
        type=float,
        help="Multiply input features by this scale before input to net")
    parser.add_argument(
        "--channel_swap",
        default='2,1,0',
        help="Order to permute input channels. The default converts " +
        "RGB -> BGR since BGR is the Caffe default by way of OpenCV.")
    parser.add_argument(
        "--ext",
        default='jpg',
        help="Image file extension to take as input when a directory " +
        "is given as the input file.")
    parser.add_argument("--labels_file",
                        default=os.path.join(pycaffe_dir, "mnist_words.txt"),
                        help="Readable label definition file.")
    parser.add_argument(
        "--print_results",
        action='store_true',
        help="Write output text to stdout rather than serializing to a file.")
    parser.add_argument(
        "--force_grayscale",
        action='store_true',
        help="Converts RGB images down to single-channel grayscale versions," +
        "useful for single-channel networks like MNIST.")
    args = parser.parse_args()

    image_dims = [int(s) for s in args.images_dim.split(',')]
    if args.force_grayscale:
        channel_swap = None
        mean = None
    else:
        channel_swap = [int(s) for s in args.channel_swap.split(',')]
        mean = np.load(args.mean_file)

    # Make classifier.
    classifier = caffe.Classifier(
        args.model_def,
        args.pretrained_model,
        image_dims=image_dims,
        mean=mean,
        input_scale=args.input_scale)  #, channel_swap=channel_swap

    if args.gpu:
        print 'GPU mode'
    print 'Get in while'
    total_counter = 1
    total_file_number = 100
    sub_counter = 0
    sub_file_number = 0
    my_list_prob = []
    my_list_label = []
    file = open("prediction\\prediction_file_no_sub.txt", "r+")
    while total_counter <= total_file_number:
        my_list_prob = []
        my_list_label = []
        sub_counter = 0
        while sub_counter <= sub_file_number:
            print 'total_count: ' + '%d' % total_counter
            # Load numpy array (.npy), directory glob (*.jpg), or image file.
            FilePath = args.input_file + "test (%d)_%d.jpg" % (total_counter,
                                                               sub_counter)
            print FilePath
            FilePath = os.path.expanduser(FilePath)
            if FilePath.endswith('npy'):
                inputs = np.load(FilePath)
            elif os.path.isdir(FilePath):
                inputs = [
                    caffe.io.load_image(im_f)
                    for im_f in glob.glob(FilePath + '/*.' + args.ext)
                ]
            else:
                inputs = [caffe.io.load_image(FilePath)]

            if args.force_grayscale:
                inputs = [rgb2gray(input) for input in inputs]
                #print inputs
                inputs = np.asarray(inputs)
                inputs = np.reshape(inputs, inputs.shape + (1, ))
                print inputs.shape
            print "Classifying %d inputs." % len(inputs)
            print 'Get in Classify'
            # Classify.
            start = time.time()
            scores = classifier.predict(inputs, not args.center_only).flatten()
            print "Done in %.2f s." % (time.time() - start)

            if args.print_results:
                with open(args.labels_file) as f:
                    labels_df = pd.DataFrame([{
                        'synset_id':
                        l.strip().split(' ')[0],
                        'name':
                        ' '.join(l.strip().split(' ')[1:]).split(',')[0]
                    } for l in f.readlines()])
                labels = labels_df.sort('synset_id')['name'].values

                indices = (-scores).argsort()[:5]
                predictions = labels[indices]

                meta = [(p, '%.5f' % scores[i])
                        for i, p in zip(indices, predictions)]

                print meta

            # Save
            #np.save(args.output_file, predictions[0])
            if float(meta[0][1]) > 0.4:
                my_list_prob.append(meta[0][1])
                my_list_label.append(meta[0][0])
            #if (sub_counter!=0) and (sub_counter % sub_file_number) == 0:
            if my_list_prob:
                Max_num_index = int(my_list_prob.index(max(my_list_prob)))
            else:
                my_list_prob.append(meta[0][1])
                my_list_label.append(meta[0][0])
                Max_num_index = int(my_list_prob.index(max(my_list_prob)))
            print my_list_label[Max_num_index]
            save_str = "%d\t" % total_counter
            file.write(save_str + str(my_list_label[Max_num_index]) + "\r\n")
            sub_counter += 1
        total_counter += 1
        print total_counter
    file.close()
Ejemplo n.º 29
0
# Setting this to CPU, but feel free to use GPU if you have CUDA installed
caffe.set_mode_cpu()
# Loading the Caffe model, setting preprocessing parameters
net = caffe.Net(model_prototxt, model_trained, caffe.TEST)

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_mean('data', np.load(mean_path).mean(1).mean(1))
transformer.set_transpose('data', (2, 0, 1))
transformer.set_raw_scale('data', 255.0)
transformer.set_channel_swap('data', (2, 1, 0))  # RGB -> BGR

data_blob_shape = net.blobs['data'].data.shape
data_blob_shape = list(data_blob_shape)
#net.blobs['data'].reshape(batchsize, data_blob_shape[1], data_blob_shape[2], data_blob_shape[3])
"""
caffe.Classifier(model_prototxt, model_trained,
                       mean=np.load(mean_path).mean(1).mean(1),
                       channel_swap=(2,1,0),
                       raw_scale=255,
                       image_dims=(256, 256))
"""


def extract_feature(list_image_file):
    start = datetime.now()
    batchsize = len(list_image_file)
    net.blobs['data'].reshape(batchsize, data_blob_shape[1],
                              data_blob_shape[2], data_blob_shape[3])
    net.blobs['data'].data[...] = map(
        lambda x: transformer.preprocess('data', caffe.io.load_image(x)),
Ejemplo n.º 30
0
import caffe
import argparse
import numpy as np

caffe.set_mode_gpu()
caffe.set_device(0)

parser = argparse.ArgumentParser()
parser.add_argument('source', nargs='+', help='list of image(s)')
parser.add_argument('model', help='model')
parser.add_argument('weights', help='pre-trained weights')
parser.add_argument('output', help='output')
args = parser.parse_args()

classifier = caffe.Classifier(args.model, args.weights)

inputs = [caffe.io.load_image(input) for input in args.source]

predictions = classifier.predict(inputs)

with open(args.output, 'w') as f:
    for file, prediction in zip(args.source, predictions):
        top_3_labels = np.argsort(prediction)[-1::-1][:3]
        top_3_confidence = prediction[top_3_labels]
        s = '{},{},{},{},{},{},{}\n'.format(
            file, top_3_labels[0], top_3_confidence[0], top_3_labels[1],
            top_3_confidence[1], top_3_labels[2], top_3_confidence[2])

        f.write(s)