Tensorflow2——图像定位


给定一副图片,我们要输出四个数字(x,y,w,h),图像中某一个点的坐标(x,y),以及图像的宽度和高度,有了这四个数字,我们可以很容易的找到物体的边框。

1、单张图片图像定位

import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from lxml import etree
import glob
from matplotlib.patches import Rectangle

img=tf.io.read_file("./location/images/Abyssinian_1.jpg")
img=tf.image.decode_jpeg(img)
plt.imshow(img)

在这里插入图片描述
#读取xml文件

xml=open("./location/annotations/xmls/Abyssinian_1.xml").read()
#解析
sel=etree.HTML(xml) #建立好选择器
width=int(sel.xpath("//size/width/text()")[0])
height=int(sel.xpath("//size/height/text()")[0])
xmin=int(sel.xpath("//bndbox/xmin/text()")[0])
xmax=int(sel.xpath("//bndbox/xmax/text()")[0])
ymin=int(sel.xpath("//bndbox/ymin/text()")[0])
ymax=int(sel.xpath("//bndbox/ymax/text()")[0])
#根目录下的size里的width,取出text文本
#这样解析出来的是一个列表,列表里面放置的有文本
## width,height,xmin,xmax,ymin,ymax
#(600, 400, 333, 425, 72, 158)

plt.imshow(img)
rec=Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),fill=False,color="red")  #最下角的值就是xmin,ymin
ax=plt.gca()  #获取当前图像
ax.axes.add_patch(rec)

在这里插入图片描述

2、随意尺度图片定位

(代码紧接上)

img=tf.image.resize(img,(224,224))
img=img/255
plt.imshow(img)

在这里插入图片描述

xmin=(xmin/width)*224
xmax=(xmax/width)*224
ymin=(ymin/height)*224
ymax=(ymax/height)*224

plt.imshow(img)
rec=Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),fill=False,color="red")  #最下角的值就是xmin,ymin
ax=plt.gca()  #获取当前图像
ax.axes.add_patch(rec)

在这里插入图片描述

3、批量图片定位

创建输入管道
数据读取与预处理
获取图像的路径

images=glob.glob("./location/images/*.jpg")
#获取目标值
xmls=glob.glob("./location/annotations/xmls/*.xml")
#拿到这3686张图片(与xml文件对应的)
name = [x.split("\\")[-1].split(".xml")[0] for x in xmls]

如何取出这些名称中的images呢?

imgs_train=[img for img in images if (img.split("\\")[-1].split(".jpg")[0]) in name] 

数据集划分

test_count=int(len(imgs_train)*0.2)
train_count=len(imgs_train)-test_count
def to_labels(path):
    #读取路径
    xml=open("{}".format(path)).read()
    sel=etree.HTML(xml)
    width=int(sel.xpath("//size/width/text()")[0])
    height=int(sel.xpath("//size/height/text()")[0])
    xmin=int(sel.xpath("//bndbox/xmin/text()")[0])
    xmax=int(sel.xpath("//bndbox/xmax/text()")[0])
    ymin=int(sel.xpath("//bndbox/ymin/text()")[0])
    ymax=int(sel.xpath("//bndbox/ymax/text()")[0])
    return  [xmin/width,ymin/height,xmax/width,ymax/height]

labels=[to_labels(path) for path in xmls]  #每个label里面包含x的最小值,x的最大值,y的最小值,y的最大值

在这里插入图片描述

out_1,out_2,out_3,out_4=list(zip(*labels))  #把xmin,ymin,xmax,ymax分别弄在一起
out_1=np.array(out_1)
out_2=np.array(out_2)
out_3=np.array(out_3)
out_4=np.array(out_4)

标签数据集

label_datasets=tf.data.Dataset.from_tensor_slices((out_1,out_2,out_3,out_4))

在这里插入图片描述
载入图片

def load_image(path):
    image=tf.io.read_file(path)
    image=tf.image.decode_jpeg(image,channels=3)
    image=tf.image.resize(image,(224,224))
    image=image/255
    return image

图片数据集处理

image_dataset=tf.data.Dataset.from_tensor_slices(imgs_train)
image_dataset=image_dataset.map(load_image)

图片数据集与标签数据集整合

dataset=tf.data.Dataset.zip((image_dataset,label_datasets))

划分数据集

dataset_train=dataset.skip(test_count)
dataset_test=dataset.take(test_count)
BATCH_SIZE=8
BUFFER_SIZE=300
STEPS_PER_EPOCH=train_count//BATCH_SIZE
VALIDATION_STEPS=test_count//BATCH_SIZE

训练数据集与测试数据集的处理

dataset_train=dataset_train.shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()
dataset_train=dataset_train.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
dataset_test=dataset_test.batch(BATCH_SIZE)

图像定位

for img,label in dataset_train.take(1):   #这里的take(1)是取出一个batch出来,这里的img是一个batch
    #这里的img和label都是tensor
    plt.imshow(tf.keras.preprocessing.image.array_to_img(img[0]))
    out_1,out_2,out_3,out_4=label
    xmin,ymin,xmax,ymax=out_1[0].numpy()*224,out_2[0].numpy()*224,out_3[0].numpy()*224,out_4[0].numpy()*224
    rec=Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),fill=False,color="red")  #最下角的值就是xmin,ymin
    ax=plt.gca()  #获取当前图像
    ax.axes.add_patch(rec)

在这里插入图片描述创建模型
#创建图像定位的模型,使用预训练网络

xception=tf.keras.applications.Xception(weights="imagenet",include_top = False,input_shape=(224,224,3))
#函数式API
inputs=tf.keras.layers.Input(shape=(224,224,3))
x=xception(inputs)
x=tf.keras.layers.GlobalAveragePooling2D()(x)
x=tf.keras.layers.Dense(2048,activation="relu")(x)
x=tf.keras.layers.Dense(256,activation="relu")(x)
out_1=tf.keras.layers.Dense(1)(x)  #这里是做回归,不需要激活函数
out_2=tf.keras.layers.Dense(1)(x)
out_3=tf.keras.layers.Dense(1)(x)
out_4=tf.keras.layers.Dense(1)(x)
prediction=[out_1,out_2,out_3,out_4]

model=tf.keras.models.Model(inputs=inputs,outputs=prediction)

编译模型

model.compile(tf.keras.optimizers.Adam(lr=0.0001),loss="mse",metrics=["mae"])

训练模型

Epochs=50
history=model.fit(dataset_train,epochs=Epochs,steps_per_epoch=STEPS_PER_EPOCH,validation_steps=VALIDATION_STEPS,validation_data=dataset_test)

在这里插入图片描述
可视化

loss=history.history["loss"]
val_loss=history.history["val_loss"]
epochs=range(Epochs)
plt.figure()
plt.plot(epochs,loss,"r",label="Training loss")
plt.plot(epochs,val_loss,"bo",label="Validation loss")
plt.title("Training and validation Loss")
plt.xlabel("Epoch")
plt.ylim([0,1])
plt.legend()
plt.show()

在这里插入图片描述
模型保存

model.save("detect_v1.h5")

在这里插入图片描述
新模型载入训练好的权重

new_model=tf.keras.models.load_model("detect_v1.h5")

新模型预测

plt.figure(figsize=(8,24))
for img,_ in dataset_test.take(1):
    out_1,out_2,out_3,out_4 = new_model.predict(img)
    for i in range(3):
        plt.subplot(3,1,i+1)  #画三行一列的第一个图像
        plt.imshow(tf.keras.preprocessing.image.array_to_img(img[i]))
        xmin,ymin,xmax,ymax = out_1[i]*224,out_2[i]*224,out_3[i]*224,out_4[i]*224
        rect=Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),fill=False,color="red")
        ax=plt.gca()
        ax.axes.add_patch(rect)
    

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
效果还可以,嘻嘻

一级目录

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
TensorFlow是一个被广泛应用于机器学习和深度学习的开源框架,它提供了丰富的工具和库来构建和训练神经网络模型。其中,CNN(卷积神经网络)是一种特别适用于图像处理任务的神经网络结构,而VGGNet19是其中一个经典的CNN模型。 在进行图像风格转化任务时,我们可以利用VGGNet19来实现。图像风格转化是一种将一张图像的内容与另一张图像的风格相结合的技术,常用于艺术创作和图像处理领域。在这个实战项目中,我们可以使用Jupyter Notebook来编写并运行我们的代码。 首先,我们需要准备两张输入图片,一张作为内容图像,一张作为风格图像。然后,我们可以使用VGGNet19模型来提取内容图像和风格图像的特征表示。接着,我们需要定义一个损失函数,该损失函数可以度量内容图像与生成图像之间的内容差异,以及风格图像与生成图像之间的风格差异。最后,我们可以使用梯度下降等优化算法来最小化损失函数,从而生成新的图像,使得它既保留了内容图像的内容特征,又融合了风格图像的风格特征。 在Jupyter Notebook中,我们可以逐步编写和调试这些代码,并及时查看生成的图像效果。通过这个实战项目,我们不仅能够深入理解CNN模型和图像风格转化的原理,还能够掌握如何使用TensorFlow和Jupyter Notebook进行实际的深度学习任务。这将为我们在图像处理和艺术创作领域带来更多的应用和创新可能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值