caffe如何将图片数据写成lmdb格式

本人也才开始使用caffe 配好环境跑了mnist和另外一个例子,当时就蒙了   如果是图片的例子应该怎么导入,后来看了两天终于整明白了 给大家讲述一下具体过程很简单。我用minst数据库做个实验。

首先我们要得到两个txt一个是train.txt另一个是test.txt

内容如下:



前面代表是图像所在目录下的名字  第二个数字代表标签  test.txt   train.txt

下面是matlab代码

clc;
clear;
load('mnist_uint8.mat')
num=size(test_x,1);
fid = fopen('test_minst.txt','wt'); 
savepath='testImage/';
%train_y=interge(train_y);
for i=1:num
    image=reshape(test_x(i,:),[28 28]);
    label=find(test_y(i,:)~=0)-1;
    if i<10
    imageName=strcat('test_0000',num2str(i));
    end
    if i<100&&i>9
    imageName=strcat('test_000',num2str(i));
    end
    if i<1000&&i>99
    imageName=strcat('test_00',num2str(i));
    end
    if i<10000&&i>999
    imageName=strcat('test_0',num2str(i));
    end
    if i>9999
    imageName=strcat('test_',num2str(i));
    end
    imageName=strcat(imageName,'.jpg');
    imagepath=strcat(savepath,imageName);
    fprintf(fid,'%s\t',imagepath);
    fprintf(fid,'%s\n',num2str(label));
   imwrite(image,imagepath,'jpg');
    
end
fclose(fid);

这样我们得到



这样我们得到两个文件夹和两个txt

下一步在caffe example下新建一个目录 至于叫什么  你自己决定我的叫newmnist文件夹  之后我将测试图像集和训练图像集和两个txt导入caffe目录下的dat

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将LaSOT数据集转换为LMDB格式的步骤如下: 1. 首先,需要下载并解压缩LaSOT数据集。您可以从以下链接下载数据集:http://vision.cs.stonybrook.edu/~lasot/download.php 2. 然后,您需要安装LMDB库。您可以从以下链接下载并安装LMDB库:https://github.com/LMDB/lmdb 3. 接下来,您需要使用脚本将LaSOT数据集转换为LMDB格式。以下是一个示例脚本: ``` import lmdb import os import cv2 import numpy as np def prepare_lmdb_data(data_dir, lmdb_file): env = lmdb.Environment(lmdb_file, map_size=int(1e12)) txn = env.begin(write=True) for root, dirs, files in os.walk(data_dir): for file in files: if file.endswith(".jpg"): video_name = os.path.basename(root) frame_name = os.path.splitext(file)[0] img_path = os.path.join(root, file) img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 将图像转换为LMDB格式 datum = caffe.proto.caffe_pb2.Datum() datum.channels = 3 datum.height = img.shape[0] datum.width = img.shape[1] datum.data = img.tobytes() datum.label = -1 key = (video_name + "/" + frame_name).encode("ascii") txn.put(key, datum.SerializeToString()) txn.commit() env.close() if __name__ == "__main__": data_dir = "/path/to/LaSOT/dataset" lmdb_file = "/path/to/LaSOT/lmdb" prepare_lmdb_data(data_dir, lmdb_file) ``` 4. 运行脚本,将LaSOT数据集转换为LMDB格式。请注意,此过程需要一些时间和计算资源,具体取决于数据集的大小和您的计算机性能。 5. 转换完成后,您可以使用LMDB库读取和处理数据集。以下是一个示例代码片段: ``` import lmdb import caffe env = lmdb.open('/path/to/LaSOT/lmdb', readonly=True) with env.begin() as txn: cursor = txn.cursor() for key, value in cursor: datum = caffe.proto.caffe_pb2.Datum() datum.ParseFromString(value) img = np.fromstring(datum.data, dtype=np.uint8).reshape(datum.channels, datum.height, datum.width) img = img.transpose((1, 2, 0)) # 转换为RGB格式 label = datum.label # 处理图像和标签 env.close() ``` 希望这可以帮助您将LaSOT数据集转换为LMDB格式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值