图像分割数据集制作,主要分为4步,用于语义分割还是实例分割,分不清楚,只是针对代码制作的自己的数据集。
数据整体差别不大,用复制加速了一下。
1、cmd:labelme,先画一个json作为模板
2、制作label
通过代码批量复制json,每个图片都复制同样的label,然后在此基础上修改能稍快一些
然后labelme手动调整
主要是json图片数据那块,需要转换一下
import os
import glob
import PIL.Image
from labelme.logger import logger
from labelme import PY2
from labelme import QT4
import io
import json
import os.path as osp
from scipy import ndimage
import base64
from labelme import utils
path =r"图像文件夹位置"
jsonpath = r"json文件夹位置"
jsonone = r"1370559239.json"#将被复制的模板
#这块很重要
def load_image_file(filename):
try:
image_pil = PIL.Image.open(filename)
except IOError:
logger.error('Failed opening image file: {}'.format(filename))
return
# apply orientation to image according to exif
image_pil = utils.apply_exif_orientation(image_pil)
with io.BytesIO() as f:
ext = osp.splitext(filename)[1].lower()
if PY2 and QT4:
format = 'PNG'
elif ext in ['.jpg', '.jpeg']:
format = 'JPEG'
else:
format = 'PNG'
image_pil.save(f, format=format)
f.seek(0)
return f.read()
def newjson(jsonone,path,jsonpath):
data = json.load(open(jsonone))
imagePath = data["imagePath"]
imageps = imagePath.split("\\")
imgpath =os.listdir(path)
for j in range(2,len(imgpath)):
imgp = imgpath[j]
print(imgp)
imageps[-1] = imgp
imagePathnew=imageps[0]
for i in range(1,len(imageps)):
imagePathnew = imagePathnew+"\\"+imageps[i]
data["imagePath"] = imagePathnew
#这块很重要
imageData = load_image_file(path+"\\"+imgp)
imageData = base64.b64encode(imageData).decode('utf-8')
data["imageData"] = imageData
image = PIL.Image.open(path+"\\"+imgp)
data["imageHeight"] = image.height
data["imageWidth"] = image.width
jsonnew = jsonpath + "\\" + imgp.split('.')[0] + '.json'
json.dump(data, open(jsonnew, 'w'))
3、将json转换成png图片
顺带也分了一下训练集和测试集
import base64
import json
import os
import os.path as osp
import PIL.Image
from labelme import utils
import random
def main():
jsonpath = r"json文件夹"#应该是从json里面直接读的图片数据
outpath = r"png文件夹"
ratio = 0.8
if not osp.exists(outpath):
os.mkdir(outpath)
namelist=[]
paths = os.listdir(jsonpath)
for onepath in paths:
name = onepath.split(".")[0]
json_file = jsonpath+"\\"+onepath
outfile = outpath+"\\"+name+".png"
namelist.append(name)
data = json.load(open(json_file))
imageData = data.get("imageData")
if not imageData:
imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
with open(imagePath, "rb") as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode("utf-8")
img = utils.img_b64_to_arr(imageData)
label_name_to_value = {"_background_": 0}
for shape in sorted(data["shapes"], key=lambda x: x["label"]):
label_name = shape["label"]
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
lbl, _ = utils.shapes_to_label(
img.shape, data["shapes"], label_name_to_value
)
utils.lblsave(outfile, lbl)
random.shuffle(namelist)
n_total = len(namelist)
offset = int(n_total * ratio)
train = namelist[:offset]
val = namelist[offset:]
with open(outpath+"\\train.txt","w") as f:
for i in range(len(train)):
f.write(str(train[i])+"\n")
f.close()
with open(outpath+"\\val.txt","w") as f:
for i in range(len(val)):
f.write(str(val[i])+"\n")
f.close()
if __name__ == "__main__":
main()
png结果图片
4、制作训练集
我其实训练的时候只用到了,原图和3生成的png,将数据分为训练和测试写入txt,就可以了
训练模型调别人的:GitHub - bubbliiiing/deeplabv3-plus-pytorch: 这是一个deeplabv3-plus-pytorch的源码,可以用于训练自己的模型。
5、测试c++
为了让c++调用,将pth转成带网络结构的pt文件
weight = torch.load(“*.pth”)
model.load_state_dict(weight, strict=True)
model = model.eval()
# 注意模型输入的尺寸
example = torch.rand(1, 3, 512, 512)
traced_script_module = torch.jit.trace(model, example)
name = lp.replace(".pth",".pt")
traced_script_module.save(name)
opencv调用,Python版
image = cv2.imread("img_path")
orininal_h,orininal_w,_ = image.shape
image=cv2.resize(image, (512, 512))
#image.convertTo(image,cv2.CV_32F, 1.0 / 255, 0)
image = image/255.0
image_data = np.expand_dims(np.transpose((np.array(image, np.float32)), (2, 0, 1)), 0)
img_tensor = torch.from_numpy(image_data)
pr = module.forward(img_tensor)[0]
pr = F.softmax(pr.permute(1, 2, 0), dim=-1).cpu().detach().numpy()
# cv2.imshow("pr",pr)
# cv2.waitKey(0)
pr = cv2.resize(pr, (orininal_w, orininal_h), interpolation=cv2.INTER_LINEAR)
pr = pr.argmax(axis=-1)
colors = [(0, 0, 0), (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128), (0, 128, 128),
(128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0), (192, 128, 0), (64, 0, 128), (192, 0, 128),
(64, 128, 128), (192, 128, 128), (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128),
(128, 64, 12)]
seg_img = np.reshape(np.array(colors, np.uint8)[np.reshape(pr, [-1])], [orininal_h, orininal_w, -1])
image = Image.fromarray(np.uint8(seg_img))
image.save("")