目标检测础知识(Object Detection)

Object Localization

在这里插入图片描述
定位一个物体,要求我们输入一个图片,输出一个物体对应的方位。

X

ConvNet

[

P

c

b

x

b

y

b

h

b

w

.

.

.

o

n

e

_

h

o

t

]

X\quad \underrightarrow {\text{ConvNet}} \quad \begin{bmatrix} P_c &\\ b_x &\\ b_y&\\ b_h&\\ b_w&\\ ...&\\ one\_hot&\\ \end{bmatrix}

X

ConvNetPcbxbybhbw...one_hot

p

c

b

x

,

b

y

,

b

w

,

b

h

,

O

n

e

_

H

o

t

p_c表示是否存在物体,b_x,b_y,b_w,b_h,分别表示坐标和对应的长和宽。\\后面是One\_Hot表示的物体种类。

pcbx,by,bw,bh,One_Hot
我们定义的loss:

L

o

s

s

(

y

,

y

^

)

=

{

i

=

0

n

(

y

i

y

i

^

)

2

,

y

p

c

=

1

(

y

p

c

y

p

c

^

)

2

,

y

p

c

=

0

ignore anything else

Loss(y,\hat{y}) = \left \{ \begin{aligned} -\sum_{i=0}^{n}(y_i-\hat{y_i})^2& ,\quad y_{p_c}=1\\ (y_{p_c}-\hat{y_{p_c}})^2 &,\quad y_{p_c} = 0 \quad \text{ignore anything else} \end{aligned} \right .

Loss(y,y^)=i=0n(yiyi^)2(ypcypc^)2,ypc=1,ypc=0ignore anything else
当然,不限于均方误差,我们也可以灵活采用其他误差。

Sliding Window Detection(Convolutional implementation)

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

  • 基于前面的CNN知识,我们很容易想到一种滑动窗口的方法,每次从一张图片里面截取一个固定大小的窗口,进行之前的CNN处理,输出

    y

    ^

    \hat{y}

    y^,但是这样的计算代价过于昂贵。

  • 我们可以知道,图片中每个像素点绝大部分之和它周围几个像素点具有联系,这也是ConvNet用于图像处理的关键。

于是,我们可以将最后几层FC层(全连接)改为Conv层,得到以下的网络。
在这里插入图片描述

一个需要注意的点是,我们并不需要人为每次截取窗口,只需要将整张图片输入进去,得到的自然使我们想要的。

  • 如下:
    在这里插入图片描述

YOLO(you only look once)

尽管我们用卷积操作代替了全连接层,但是网络的运行速度仍然不如人意。于是,YOLO应运而生。
在这里插入图片描述
YOLO算法将图片分割为

n

n

n*n

nn个小格,处理每一个小格,我们标记的时候,标记物体中心所在的小格。这个我们可以做到对图像中每一个像素点只进行一次计算即可,这也就是 You only look once 。

在YOLO中,

b

x

,

b

y

b_x,b_y

bx,by都是小于1的,因为中心点总是会在一个小格内,但是

b

w

,

b

h

b_w,b_h

bw,bh可以大于1,因为一个物体可以横跨多个小格。

Iou (intersection over union)

那么如果判断一个算法给定的box的好坏,我们需要一个评定标准,不然的话,算法就是给定一个很大的box,这样就可以很大可能包含一个物体,这样准确度就会上升,但是实际上我们肯定很少需要这样的算法,我们通常需要一个尽可能精确的box来圈定我们的物体。我们采用下面的标准:

I

o

u

=

O

b

j

e

c

t

 

s

i

z

e

B

o

x

 

s

i

z

e

B

o

x

 

s

i

z

e

O

b

j

e

c

t

 

s

i

z

e

Iou = \frac{Object \ size\cap Box\ size}{Box\ size \cup Object \ size}

Iou=Box sizeObject sizeObject sizeBox size

  • 在这里插入图片描述

    I

    o

    u

    =

    y

    e

    l

    l

    o

    w

     

    s

    i

    z

    e

    g

    r

    e

    e

    n

     

    s

    i

    z

    e

    Iou = \frac{yellow\ size}{green\ size}

    Iou=green sizeyellow size
    通常需要

    I

    o

    u

    0.5

    Iou \geq0.5

    Iou0.5才认定为一个比较好的识别。

Anchor Boxes

我们会发现,

y

^

\hat{y}

y^只会输出一个box,如果多个物体的中心都落在同一个小格内,那该如何输出呢?
答案就是再加一个box,

y

^

=

[

p

c

b

x

1

b

y

1

b

w

1

b

h

1

b

x

2

b

y

2

b

w

2

b

h

2

.

.

.

o

n

e

_

h

o

t

]

\hat{y} = \begin{bmatrix} p_c&\\ b_{x1}&\\ b_{y1}&\\ b_{w1}&\\ b_{h1}&\\ b_{x2}&\\ b_{y2}&\\ b_{w2}&\\ b_{h2}&\\ ...\\ one\_hot&\\ \end{bmatrix}

y^=pcbx1by1bw1bh1bx2by2bw2bh2...one_hot
这个时候

p

c

p_c

pc依然等于1,而不是2。(如果具有两个物体)
在这里插入图片描述

如果具有三个及以上物体怎么办,事实上,当我们的分割的时候,尽量分割较小可以避免很多在同一个小格的情况,三个及以上物体出现在同一个小格的情况很少,一般我们不会再添加一个Anchor Box,而是特殊处理一下这种情况。

Non-Max Supression

在目标检测中还容易出现一种问题就是:对于同一个物体,输出了多个对应的box,这也是我们不想要看到的。如何在这几个box选择最好的。
在这里插入图片描述
一种称为Non-supression的方法就是:

  1. 舍弃所有

    P

    c

    P_c

    Pc值小于

    t

    h

    r

    e

    s

    h

    h

    o

    l

    d

    threshhold

    threshhold的box

  2. 首先选择

    P

    c

    P_c

    Pc值最高的box

  3. 舍弃掉那些和之前box具有较高IOU的box
  4. 重复操作2一直到没有box达到较高的

    P

    c

    P_c

    Pc

我们只在相同物体进行Non-supression操作

Semantic Segmentation with U-Net

U-Net Paper
在这里插入图片描述
在这里插入图片描述
我们想要的输出类似下面一张图片:

在这里插入图片描述
相比于之前的CNN,如下:在这里插入图片描述

  • 随着层数加深,图像的长和宽都在缩小,并且深度在不断增加,最终输出一个我们想要的向量。
  • 但是在Semantic Segmentation中,我们需要的输出应当是一个长宽和输入对应的,深度为1的矩阵,因此如何减小深度,并且扩大长宽,需要一个叫做transpose convolution操作。
    在这里插入图片描述

Transpose Convolution

在这里插入图片描述
Transpose Convolution详解

简单叙述步骤:

  1. 计算新参数

    z

    p

    z和p'

    zp

  2. 输入的每一行和列之间,插入

    z

    z

    z个0。这将输入的大小增加到(2 * I-1)x(2 * I-1)

  3. 填充

    p

    p'

    p个0在第二部周围

  4. 在步骤3中执行从步骤3生成的图像的标准卷积,步幅长度为1

U-Net

在这里插入图片描述

  • 前面的操作就是传统的Conv和下采样操作
  • 后面就是Conv和transpose 操作
  • 其中的skip connnection是直接按照深度叠加(因为长和宽相等)
  • 最后进行1*1Conv操作得到

    h

    w

    n

    c

    h

    ,

    w

    n

    c

    n

    u

    m

    s

     

    o

    f

     

    c

    l

    a

    s

    s

    e

    s

    (h*w*nc)h,w为原始图片大小,nc为 nums\ of \ classes

    hwnch,wncnums of classes

import argparse
import os
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
import scipy.io
import scipy.misc
import numpy as np
import pandas as pd
import PIL
from PIL import ImageFont, ImageDraw, Image
import tensorflow as tf
from tensorflow.python.framework.ops import EagerTensor

from tensorflow.keras.models import load_model
from yad2k.models.keras_yolo import yolo_head
from yad2k.utils.utils import draw_boxes, get_colors_for_classes, scale_boxes, read_classes, read_anchors, preprocess_image

%matplotlib inline
  • 舍弃预测概率小于阈值的box
# UNQ_C1 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# GRADED FUNCTION: yolo_filter_boxes

def yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold = .6):
    """Filters YOLO boxes by thresholding on object and class confidence.
    
    Arguments:
        boxes -- tensor of shape (19, 19, 5, 4)
        box_confidence -- tensor of shape (19, 19, 5, 1)
        box_class_probs -- tensor of shape (19, 19, 5, 80)
        threshold -- real value, if [ highest class probability score < threshold],
                     then get rid of the corresponding box

    Returns:
        scores -- tensor of shape (None,), containing the class probability score for selected boxes
        boxes -- tensor of shape (None, 4), containing (b_x, b_y, b_h, b_w) coordinates of selected boxes
        classes -- tensor of shape (None,), containing the index of the class detected by the selected boxes

    Note: "None" is here because you don't know the exact number of selected boxes, as it depends on the threshold. 
    For example, the actual output size of scores would be (10,) if there are 10 boxes.
    """
    
    ### START CODE HERE
    # Step 1: Compute box scores
    ##(≈ 1 line)
    box_scores = box_confidence * box_class_probs
    # Step 2: Find the box_classes using the max box_scores, keep track of the corresponding score
    ##(≈ 2 lines)
    box_classes = tf.math.argmax(box_scores,axis=-1)
    box_class_scores = tf.math.reduce_max(box_scores,axis=-1)
    
    # Step 3: Create a filtering mask based on "box_class_scores" by using "threshold". The mask should have the
    # same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold)
    ## (≈ 1 line)
    filtering_mask = (box_class_scores>threshold)
    # print(filtering_mask)
    # Step 4: Apply the mask to box_class_scores, boxes and box_classes
    ## (≈ 3 lines)
    scores = tf.boolean_mask(box_class_scores,filtering_mask)
    boxes = tf.boolean_mask(boxes,filtering_mask)
    classes = tf.boolean_mask(box_classes,filtering_mask)
    ### END CODE HERE
    
    return scores, boxes, classes
  • 计算IOU
# UNQ_C2 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# GRADED FUNCTION: iou

def iou(box1, box2):
    """Implement the intersection over union (IoU) between box1 and box2
    
    Arguments:
    box1 -- first box, list object with coordinates (box1_x1, box1_y1, box1_x2, box_1_y2)
    box2 -- second box, list object with coordinates (box2_x1, box2_y1, box2_x2, box2_y2)
    """


    (box1_x1, box1_y1, box1_x2, box1_y2) = box1
    (box2_x1, box2_y1, box2_x2, box2_y2) = box2

    ### START CODE HERE
    # Calculate the (yi1, xi1, yi2, xi2) coordinates of the intersection of box1 and box2. Calculate its Area.
    ##(≈ 7 lines)
    xi1 = max(box1_x1,box2_x1)
    yi1 = max(box1_y1,box2_y1)
    xi2 = min(box1_x2,box2_x2)
    yi2 = min(box1_y2,box2_y2)
    inter_width = xi2 - xi1
    inter_height =  yi2 - yi1
    inter_area = max(inter_width,0)*max(inter_height,0)
    
    # Calculate the Union area by using Formula: Union(A,B) = A + B - Inter(A,B)
    ## (≈ 3 lines)
    box1_area = (box1_x2-box1_x1)*(box1_y2-box1_y1)
    box2_area = (box2_x2-box2_x1)*(box2_y2-box2_y1)
    union_area = (box1_area + box2_area - inter_area)
    
    # compute the IoU
    iou = inter_area / (box1_area + box2_area - inter_area)
    ### END CODE HERE
    
    return iou
  • Non-max suppression
# UNQ_C3 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# GRADED FUNCTION: yolo_non_max_suppression

def yolo_non_max_suppression(scores, boxes, classes, max_boxes = 10, iou_threshold = 0.5):
    """
    Applies Non-max suppression (NMS) to set of boxes
    
    Arguments:
    scores -- tensor of shape (None,), output of yolo_filter_boxes()
    boxes -- tensor of shape (None, 4), output of yolo_filter_boxes() that have been scaled to the image size (see later)
    classes -- tensor of shape (None,), output of yolo_filter_boxes()
    max_boxes -- integer, maximum number of predicted boxes you'd like
    iou_threshold -- real value, "intersection over union" threshold used for NMS filtering
    
    Returns:
    scores -- tensor of shape (, None), predicted score for each box
    boxes -- tensor of shape (4, None), predicted box coordinates
    classes -- tensor of shape (, None), predicted class for each box
    
    Note: The "None" dimension of the output tensors has obviously to be less than max_boxes. Note also that this
    function will transpose the shapes of scores, boxes, classes. This is made for convenience.
    """
    
    max_boxes_tensor = tf.Variable(max_boxes, dtype='int32')     # tensor to be used in tf.image.non_max_suppression()

    ### START CODE HERE
    # Use tf.image.non_max_suppression() to get the list of indices corresponding to boxes you keep
    ##(≈ 1 line)
    nms_indices = tf.image.non_max_suppression(boxes,scores,max_boxes_tensor,iou_threshold=iou_threshold)
    
    # Use tf.gather() to select only nms_indices from scores, boxes and classes
    ##(≈ 3 lines)
    scores = tf.gather(scores,nms_indices)
    boxes = tf.gather(boxes,nms_indices)
    classes = tf.gather(classes,nms_indices)
    ### END CODE HERE

    
    return scores, boxes, classes
  • box的转换
def yolo_boxes_to_corners(box_xy, box_wh):
    """Convert YOLO box predictions to bounding box corners."""
    box_mins = box_xy - (box_wh / 2.)
    box_maxes = box_xy + (box_wh / 2.)

    return tf.keras.backend.concatenate([
        box_mins[..., 1:2],  # y_min
        box_mins[..., 0:1],  # x_min
        box_maxes[..., 1:2],  # y_max
        box_maxes[..., 0:1]  # x_max
    ])

  • 综合
# UNQ_C4 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# GRADED FUNCTION: yolo_eval

def yolo_eval(yolo_outputs, image_shape = (720, 1280), max_boxes=10, score_threshold=.6, iou_threshold=.5):
    """
    Converts the output of YOLO encoding (a lot of boxes) to your predicted boxes along with their scores, box coordinates and classes.
    
    Arguments:
    yolo_outputs -- output of the encoding model (for image_shape of (608, 608, 3)), contains 4 tensors:
                    box_xy: tensor of shape (None, 19, 19, 5, 2)
                    box_wh: tensor of shape (None, 19, 19, 5, 2)
                    box_confidence: tensor of shape (None, 19, 19, 5, 1)
                    box_class_probs: tensor of shape (None, 19, 19, 5, 80)
    image_shape -- tensor of shape (2,) containing the input shape, in this notebook we use (608., 608.) (has to be float32 dtype)
    max_boxes -- integer, maximum number of predicted boxes you'd like
    score_threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box
    iou_threshold -- real value, "intersection over union" threshold used for NMS filtering
    
    Returns:
    scores -- tensor of shape (None, ), predicted score for each box
    boxes -- tensor of shape (None, 4), predicted box coordinates
    classes -- tensor of shape (None,), predicted class for each box
    """
    
    ### START CODE HERE
    # Retrieve outputs of the YOLO model (≈1 line)
    box_xy, box_wh, box_confidence, box_class_probs = yolo_outputs
    
    # Convert boxes to be ready for filtering functions (convert boxes box_xy and box_wh to corner coordinates)
    boxes = yolo_boxes_to_corners(box_xy,box_wh)
    
    # Use one of the functions you've implemented to perform Score-filtering with a threshold of score_threshold (≈1 line)
    scores, boxes, classes = yolo_filter_boxes(boxes,box_confidence,box_class_probs,score_threshold)
    
    # Scale boxes back to original image shape.
    boxes = scale_boxes(boxes,image_shape)
    
    # Use one of the functions you've implemented to perform Non-max suppression with 
    # maximum number of boxes set to max_boxes and a threshold of iou_threshold (≈1 line)
    scores, boxes, classes = yolo_non_max_suppression(scores,boxes,classes,max_boxes,iou_threshold)
    ### END CODE HERE
    
    return scores, boxes, classes
yolo_model = load_model("model_data/", compile=False)
def predict(image_file):
    """
    Runs the graph to predict boxes for "image_file". Prints and plots the predictions.
    
    Arguments:
    image_file -- name of an image stored in the "images" folder.
    
    Returns:
    out_scores -- tensor of shape (None, ), scores of the predicted boxes
    out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes
    out_classes -- tensor of shape (None, ), class index of the predicted boxes
    
    Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. 
    """

    # Preprocess your image
    image, image_data = preprocess_image("images/" + image_file, model_image_size = (608, 608))
    
    yolo_model_outputs = yolo_model(image_data)
    yolo_outputs = yolo_head(yolo_model_outputs, anchors, len(class_names))
    
    out_scores, out_boxes, out_classes = yolo_eval(yolo_outputs, [image.size[1],  image.size[0]], 10, 0.3, 0.5)

    # Print predictions info
    print('Found {} boxes for {}'.format(len(out_boxes), "images/" + image_file))
    # Generate colors for drawing bounding boxes.
    colors = get_colors_for_classes(len(class_names))
    # Draw bounding boxes on the image file
    #draw_boxes2(image, out_scores, out_boxes, out_classes, class_names, colors, image_shape)
    draw_boxes(image, out_boxes, out_classes, class_names, out_scores)
    # Save the predicted bounding box on the image
    image.save(os.path.join("out", image_file), quality=100)
    # Display the results in the notebook
    output_image = Image.open(os.path.join("out", image_file))
    imshow(output_image)

    return out_scores, out_boxes, out_classes

Image Segmentation with U-Net

import tensorflow as tf
import numpy as np

from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Dropout 
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras.layers import concatenate

from test_utils import summary, comparator
import os
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import imageio

import matplotlib.pyplot as plt
%matplotlib inline

path = ''
image_path = os.path.join(path, './data/CameraRGB/')
mask_path = os.path.join(path, './data/CameraMask/')
image_list = os.listdir(image_path)
mask_list = os.listdir(mask_path)
image_list = [image_path+i for i in image_list]
mask_list = [mask_path+i for i in mask_list]

N = 2
img = imageio.imread(image_list[N])
mask = imageio.imread(mask_list[N])
#mask = np.array([max(mask[i, j]) for i in range(mask.shape[0]) for j in range(mask.shape[1])]).reshape(img.shape[0], img.shape[1])
fig, arr = plt.subplots(1, 2, figsize=(14, 10))
arr[0].imshow(img)
arr[0].set_title('Image')
arr[1].imshow(mask[:, :, 0])
arr[1].set_title('Segmentation')
image_filenames = tf.constant(image_list)
masks_filenames = tf.constant(mask_list)

dataset = tf.data.Dataset.from_tensor_slices((image_filenames, masks_filenames))


  • 预处理
def process_path(image_path, mask_path):
    img = tf.io.read_file(image_path)
    img = tf.image.decode_png(img, channels=3)
    img = tf.image.convert_image_dtype(img, tf.float32)

    mask = tf.io.read_file(mask_path)
    mask = tf.image.decode_png(mask, channels=3)
    mask = tf.math.reduce_max(mask, axis=-1, keepdims=True)
    return img, mask

def preprocess(image, mask):
    input_image = tf.image.resize(image, (96, 128), method='nearest')
    input_mask = tf.image.resize(mask, (96, 128), method='nearest')

    return input_image, input_mask

image_ds = dataset.map(process_path)
processed_image_ds = image_ds.map(preprocess)
  • ConvNet
# UNQ_C1
# GRADED FUNCTION: conv_block
def conv_block(inputs=None, n_filters=32, dropout_prob=0, max_pooling=True):
    """
    Convolutional downsampling block
    
    Arguments:
        inputs -- Input tensor
        n_filters -- Number of filters for the convolutional layers
        dropout_prob -- Dropout probability
        max_pooling -- Use MaxPooling2D to reduce the spatial dimensions of the output volume
    Returns: 
        next_layer, skip_connection --  Next layer and skip connection outputs
    """

    ### START CODE HERE
    conv = Conv2D(n_filters, # Number of filters
                  3,   # Kernel size   
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(inputs)
    conv = Conv2D(n_filters, # Number of filters
                  3,   # Kernel size
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(conv)
    ### END CODE HERE
    
    # if dropout_prob > 0 add a dropout layer, with the variable dropout_prob as parameter
    if dropout_prob > 0:
         ### START CODE HERE
        conv = Dropout(dropout_prob)(conv)
         ### END CODE HERE
         
        
    # if max_pooling is True add a MaxPooling2D with 2x2 pool_size
    if max_pooling:
        ### START CODE HERE
        next_layer = MaxPooling2D(pool_size=(2,2))(conv)
        ### END CODE HERE
        
    else:
        next_layer = conv
        
    skip_connection = conv
    
    return next_layer, skip_connection
  • Transpose Conv
# UNQ_C2
# GRADED FUNCTION: upsampling_block
def upsampling_block(expansive_input, contractive_input, n_filters=32):
    """
    Convolutional upsampling block
    
    Arguments:
        expansive_input -- Input tensor from previous layer
        contractive_input -- Input tensor from previous skip layer
        n_filters -- Number of filters for the convolutional layers
    Returns: 
        conv -- Tensor output
    """
    
    ### START CODE HERE
    up = Conv2DTranspose(
                 n_filters,    # number of filters
                 (3,3),    # Kernel size
                 strides=2,
                 padding='same')(expansive_input)
    
    # Merge the previous output and the contractive_input
    merge = concatenate([up, contractive_input], axis=3)
    conv = Conv2D(n_filters,   # Number of filters
                 (3,3),     # Kernel size
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(merge)
    conv = Conv2D(n_filters,  # Number of filters
                 (3,3),   # Kernel size
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(conv)
    ### END CODE HERE
    
    return conv
  • 综合为U-Net

# UNQ_C3
# GRADED FUNCTION: unet_model
def unet_model(input_size=(96, 128, 3), n_filters=32, n_classes=23):
    """
    Unet model
    
    Arguments:
        input_size -- Input shape 
        n_filters -- Number of filters for the convolutional layers
        n_classes -- Number of output classes
    Returns: 
        model -- tf.keras.Model
    """
    inputs = Input(input_size)
    # Contracting Path (encoding)
    # Add a conv_block with the inputs of the unet_ model and n_filters
    ### START CODE HERE
    cblock1 = conv_block(inputs, n_filters)
    # Chain the first element of the output of each block to be the input of the next conv_block. 
    # Double the number of filters at each new step
    cblock2 = conv_block(cblock1[0], n_filters*2)
    cblock3 = conv_block(cblock2[0], n_filters*4)
    cblock4 = conv_block(cblock3[0], n_filters*8, dropout_prob=0.3) # Include a dropout_prob of 0.3 for this layer
    # Include a dropout_prob of 0.3 for this layer, and avoid the max_pooling layer
    cblock5 = conv_block(cblock4[0], n_filters*16, dropout_prob=0.3, max_pooling=False) 
    ### END CODE HERE
    
    # Expanding Path (decoding)
    # Add the first upsampling_block.
    # Use the cblock5[0] as expansive_input and cblock4[1] as contractive_input and n_filters * 8
    ### START CODE HERE
    ublock6 = upsampling_block(cblock5[0], cblock4[1],  n_filters*8)
    # Chain the output of the previous block as expansive_input and the corresponding contractive block output.
    # Note that you must use the second element of the contractive block i.e before the maxpooling layer. 
    # At each step, use half the number of filters of the previous block 
    ublock7 = upsampling_block(ublock6, cblock3[1],  n_filters*4)
    ublock8 = upsampling_block(ublock7, cblock2[1],  n_filters*2)
    ublock9 = upsampling_block(ublock8, cblock1[1],  n_filters)
    ### END CODE HERE

    conv9 = Conv2D(n_filters,
                 3,
                 activation='relu',
                 padding='same',
                 kernel_initializer='he_normal')(ublock9)

    # Add a Conv2D layer with n_classes filter, kernel size of 1 and a 'same' padding
    ### START CODE HERE
    conv10 = Conv2D(filters=23,kernel_size=(1,1) , padding='same')(conv9)
    ### END CODE HERE
    
    model = tf.keras.Model(inputs=inputs, outputs=conv10)

    return model
unet.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
def display(display_list):
    plt.figure(figsize=(15, 15))

    title = ['Input Image', 'True Mask', 'Predicted Mask']

    for i in range(len(display_list)):
        plt.subplot(1, len(display_list), i+1)
        plt.title(title[i])
        plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i]))
        plt.axis('off')
    plt.show()
EPOCHS = 40
VAL_SUBSPLITS = 5
BUFFER_SIZE = 500
BATCH_SIZE = 32
processed_image_ds.batch(BATCH_SIZE)
train_dataset = processed_image_ds.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
print(processed_image_ds.element_spec)
model_history = unet.fit(train_dataset, epochs=EPOCHS)

def show_predictions(dataset=None, num=1):
    """
    Displays the first image of each of the num batches
    """
    if dataset:
        for image, mask in dataset.take(num):
            pred_mask = unet.predict(image)
            display([image[0], mask[0], create_mask(pred_mask)])
    else:
        display([sample_image, sample_mask,
             create_mask(unet.predict(sample_image[tf.newaxis, ...]))])

版权声明:本文为CSDN博主「东风中的蒟蒻」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_50089378/article/details/122856793

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

从0开始实现目标检测——实践篇

根据上一篇《从0开始实现目标检测——原理篇》的讲述,我们选择了YOLOv3作为模型,那么本篇文章将继续接着上篇的内容,自己动手基于YOLOv3实现模型训练和mAP的计算。 在自己动手的这个过程中&#xf

YOLO-V3-SPP详细解析

YOLO-V3-SPP 继前两篇简单的YOLO博文 YOLO-V1 论文理解《You Only Look Once: Unified, Real-Time Object Detection》YOLO-V2论文理解《YOLO9000: Bet

目标检测部署(卡牌识别)

最近在折腾yolov5,训练了一个识别纸牌的模型,最后使用onnxruntime进行部署,感兴趣的可以上github上clone下来玩玩,模型的权重文件上传到了百度网盘,链接