def gpu_task(prototxt, caffemodel, layer, path_images, out, gpu=0):

    num_images = len(path_images)
    h5f = h5py.File(out, 'w')

    # set gpu card
    caffe.set_device(gpu)
    caffe.set_mode_gpu()

    # init NN
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)
    net.forward()

    features = []
    image_names = []
    for i, path in enumerate(path_images):
        print "%d(%d), %s"%((i+1), num_images, os.path.basename(path))
        d = opencv_format_img_for_vgg(path, True)
        # extract feature
        feat = extract_raw_features(net, layer, d)
        #feat = extract_multi_raw_features(net, layer, d)
        # apply maxpooling
        #feat = apply_maxpooling_aggregation(feat)
        feat = apply_crow_aggregation(feat)
        features.append(feat)
        image_names.append(os.path.basename(path))
    features = np.array(features)
    h5f['feats'] = features
    h5f['names'] = image_names
    h5f.close()
    print "gpu %d task has finished..." % (int(gpu))
Exemple #2
0
def extract_crow_feature(img, cls_model):

    output = cls_model(img)
    output_cpu = output.cpu().data.numpy()
    X = output_cpu.squeeze()
    f_1 = crow.normalize(crow.apply_crow_aggregation(X)).flatten()

    return f_1
def extract_feat_3_yalers(img_path):
    model =load_model(dataPath+'/f1cn_model.49-1.613893.hdf5')
    layer_1 = K.function([model.layers[0].input], [model.layers[7].output])    
    img = image.load_img(img_path, target_size=(224,224))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)    
    f1 = layer_1([img])[0]
    feat_5=convert_kernel(f1[0].T)
    feature_crow_5=apply_crow_aggregation(feat_5)
    feature_norm_5=normalize(feature_crow_5)
    return np.sqrt(feature_norm_5)
    
    
    
    
    
    
    
        
def query_images(groundtruth_dir, image_dir, dataset, cropped=True):
    """
    Extract features from the Oxford or Paris dataset.
    :param str groundtruth_dir:
        the directory of the groundtruth files (which includes the query files)
    :param str image_dir:
        the directory of dataset images
    :param str dataset:
        the name of the dataset, either 'oxford' or 'paris'
    :param bool cropped:
        flag to optionally disable cropping
    :yields Image img:
        the Image object
    :yields str query_name:
        the name of the query
    """
    imgs = []
    query_names = []
    fake_query_names = []
    feats_crop = [] 
   
    modelDir = "/home/yuanyong/py/crow_retrieval/model"
    MODEL = "vgg.model"
    PROTO = "pool5.prototxt"
    caffemodel = os.path.join(modelDir, MODEL)
    prototxt = os.path.join(modelDir, PROTO)

    # set gpu card
    layer = 'pool5'
    caffe.set_device(6)
    caffe.set_mode_gpu()
    # init NN
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)
    net.forward()

    for f in glob.iglob(os.path.join(groundtruth_dir, '*_query.txt')):
        fake_query_name = os.path.splitext(os.path.basename(f))[0].replace('_query', '')
        fake_query_names.append(fake_query_name)

        query_name, x, y, w, h = open(f).read().strip().split(' ')

        if dataset == 'oxford':
            query_name = query_name.replace('oxc1_', '')
            query_names.append('%s.jpg' % query_name)
        img = cv2.imread(os.path.join(image_dir, '%s.jpg' % query_name), 1) # BGR

        if cropped:
            x, y, w, h = map(float, (x, y, w, h))
            x, y, w, h = map(lambda d: int(round(d)), (x, y, w, h))
        else:
            x, y, w, h = (0, 0, img.shape[1], img.shape[0])
        img = img[y:y+h, x:x+w]
        d = np.float32(img)
        # VggNet
        d -= np.array((104.00698793, 116.66876762, 122.67891434))
        d = d.transpose((2, 0, 1))
        feat = extract_raw_features(net, layer, d)
        #feat = extract_multi_raw_features(net, layer, d)
        feat = apply_crow_aggregation(feat)
        # L2-normalize feature
        feat = normalize(feat, copy=False)
        feats_crop.append(feat)
        imgs.append(img)
    return imgs, feats_crop, query_names, fake_query_names
Exemple #5
0
def squeeze_0(X):
    X = torch.Tensor(X)
    X.squeeze_(0)
    return X.numpy()


if __name__ == '__main__':
    net_features = '/data8T/ycf/project/IRS/feas/crow/features'
    query_features = '/data8T/ycf/project/IRS/feas/crow/query_features'
    norm_features = '/data8T/ycf/project/IRS/feas/crow/norm_features'
    query_norm_features = '/data8T/ycf/project/IRS/feas/crow/query_norm_features'

    features, N = [], []
    for X, n in load_features(net_features):
        X = squeeze_0(X)
        X = list(apply_crow_aggregation(X))
        features.append(X)
        N.append(n)

    features = np.array(features)
    features, paras = apply_process_normalize(features)
    infos = {'norm_features': features, 'paras': paras, 'images': N}
    pickle.dump(infos, open(os.path.join(norm_features, 'infos.plk'), 'wb'))

    features, N = [], []
    for X, n in load_features(query_features):
        X = squeeze_0(X)
        X = list(apply_crow_aggregation(X))
        features.append(X)
        N.append(n)
    features = np.array(features)
    def extract_feat(self, img_path):
        """
        img = image.load_img(img_path, target_size=(self.input_shape[0], self.input_shape[1]))
        img = image.img_to_array(img)
        """
        img = image.load_img(img_path)
        img = image.img_to_array(img)
        h, w, c = img.shape
        resize_h=h
        resize_w=w
        minlength=min(h,w)
        if minlength>224:
            beta=minlength/224
            resize_h=int(h/beta)
            resize_w=int(w/beta)           
        img=cv2.resize(img,(resize_h,resize_w))

        img = np.expand_dims(img, axis=0)
        img = preprocess_input(img)

        feat_0 = self.model_vgg_block1_conv2.predict(img)
        feat_0=convert_kernel(feat_0[0].T)
        feature_crow_0=apply_crow_aggregation(feat_0)
        feature_norm_0=normalize(feature_crow_0)
        feature_mean_norm_0=normalize(preprocessing.scale(feature_crow_0,axis=0, with_mean=True, with_std=False, copy=True))

        feat_1 = self.model_vgg_block2_conv2.predict(img)
        feat_1=convert_kernel(feat_1[0].T)
        feature_crow_1=apply_crow_aggregation(feat_1)
        feature_norm_1=normalize(feature_crow_1)
        feature_mean_norm_1=normalize(preprocessing.scale(feature_crow_1,axis=0, with_mean=True, with_std=False, copy=True))

        feat_2= self.model_vgg_block3_conv1.predict(img)
        feat_2=convert_kernel(feat_2[0].T)
        feature_crow_2=apply_crow_aggregation(feat_2)
        feature_norm_2=normalize(feature_crow_2)
        feature_mean_norm_2=normalize(preprocessing.scale(feature_crow_2,axis=0, with_mean=True, with_std=False, copy=True))

        feature_448=np.hstack((np.hstack((feature_crow_0.T,feature_crow_1.T)),feature_crow_2.T))
        feature_448_norm=np.hstack((np.hstack((feature_norm_0.T,feature_norm_1.T)),feature_norm_2.T))
        feature_448_mean_norm=np.hstack((np.hstack((feature_mean_norm_0.T,feature_mean_norm_1.T)),feature_mean_norm_2.T))
        
        feat_3 = self.model_vgg_block3_conv3.predict(img)
        feat_3=convert_kernel(feat_3[0].T)
        feature_crow_3=apply_crow_aggregation(feat_3)
        feature_norm_3=normalize(feature_crow_3)
        feature_mean_norm_3=normalize(preprocessing.scale(feature_crow_3,axis=0, with_mean=True, with_std=False, copy=True))

        feat_4 = self.model_vgg_block4_conv3.predict(img)
        feat_4=convert_kernel(feat_4[0].T)
        feature_crow_4=apply_crow_aggregation(feat_4)
        feature_norm_4=normalize(feature_crow_4)
        feature_mean_norm_4=normalize(preprocessing.scale(feature_crow_4,axis=0, with_mean=True, with_std=False, copy=True))

        feat_5= self.model_vgg_block5_conv3.predict(img)
        feat_5=convert_kernel(feat_5[0].T)
        feature_crow_5=apply_crow_aggregation(feat_5)
        feature_norm_5=normalize(feature_crow_5)
        feature_mean_norm_5=normalize(preprocessing.scale(feature_crow_5,axis=0, with_mean=True, with_std=False, copy=True))

        feature_1280=np.hstack((np.hstack((feature_crow_3.T,feature_crow_4.T)),feature_crow_5.T))
        feature_1280_norm=np.hstack((np.hstack((feature_norm_3.T,feature_norm_4.T)),feature_norm_5.T))
        feature_1280_mean_norm=np.hstack((np.hstack((feature_mean_norm_3.T,feature_mean_norm_4.T)),feature_mean_norm_5.T))
        #print(feature_norm.shape)
        #feature,pca_prams=run_feature_processing_pipeline(feature_norm)
        return np.hstack((feature_448.T,feature_1280.T)),np.hstack((feature_448_norm.T,feature_1280_norm.T)),np.hstack((feature_448_mean_norm.T,feature_1280_mean_norm.T))