### 亲自动手！

#### YOLO的输入

YOLO算法需要一些给定的输入 –

YOLO的示例输入

LABELS = ['Shirt', 'Trousers', 'Swimwear', 'Tie', 'Bus', 'Truck', 'Train', 'Motorcycle', 'Helmet', 'Shorts', 'Airplane',
'Sunglasses', 'Jacket', 'Dress', 'Human eye', 'Suit', 'Footwear', 'Woman', 'Human face', 'Man', 'Human arm',
'Human head','Human hand', 'Human leg', 'Human nose', 'Human mouth', 'Human ear', 'Human beard', 'Human foot', 'Car',
'Wheel', 'Boat', 'House', 'Bird', 'Guitar', 'Fast food', 'Hat', 'Dog', 'Laptop', 'Beer', 'Cat', 'Lantern', 'Fountain']
# Setting the input image size to 608 X 608
IMAGE_H, IMAGE_W = 608, 608
# We wil use 19X19 grids for our images. This will lead us to a total of 608/19 = 32 grids for an image
GRID_H,  GRID_W  = 19 , 19
BOX              = 5
# Getting the total number of classes/labels we will be predicting.
CLASS            = len(LABELS)
# Assigning 1's to all class labels
CLASS_WEIGHTS    = np.ones(CLASS, dtype='float32')
# Pr (object in class) * Pr (class of the object) < Obj_threshold, then it disregards this anchor box
OBJ_THRESHOLD    = 0.3#0.5
# If there are many overlapping boxes and IOU is > NMS_thereshold, then we will drop the one with a lower probability.
NMS_THRESHOLD    = 0.3#0.45
# Anchor Boxes Dimensions
ANCHORS          = [0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828]
NO_OBJECT_SCALE  = 1.0
OBJECT_SCALE     = 5.0
COORD_SCALE      = 1.0
CLASS_SCALE      = 1.0
BATCH_SIZE       = 16
WARM_UP_BATCHES  = 0
TRUE_BOX_BUFFER  = 50

#### YOLO v2架构

YOLO算法为每个定义的锚点框输出一个矩阵（如下所示） –

# Layer 20
x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_20', use_bias=False)(x)
x = BatchNormalization(name='norm_20')(x)
x = LeakyReLU(alpha=0.1)(x)
# Layer 21
skip_connection = Conv2D(64, (1,1), strides=(1,1), padding='same', name='conv_21', use_bias=False)(skip_connection)
skip_connection = BatchNormalization(name='norm_21')(skip_connection)
skip_connection = LeakyReLU(alpha=0.1)(skip_connection)
skip_connection = Lambda(space_to_depth_x2)(skip_connection)
x = concatenate([skip_connection, x])
# Layer 22
x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_22', use_bias=False)(x)
x = BatchNormalization(name='norm_22')(x)
x = LeakyReLU(alpha=0.1)(x)
# Layer 23
x = Conv2D(BOX * (4 + 1 + CLASS), (1,1), strides=(1,1), padding='same', name='conv_23')(x)
output = Reshape((GRID_H, GRID_W, BOX, 4 + 1 + CLASS))(x)
# small hack to allow true_boxes to be registered when Keras build the model
output = Lambda(lambda args: args[0])([output, true_boxes])
model = Model([input_image, true_boxes], output)

YOLO v2架构的最后几层（仅用于说明目的）

#### 迁移学习

# Taking the last convolutional layer
layer   = model.layers[-4]
weights = layer.get_weights()
# Randomly initializing the weights of the last layer
new_kernel = np.random.normal(size=weights[0].shape)/(GRID_H*GRID_W)
new_bias   = np.random.normal(size=weights[1].shape)/(GRID_H*GRID_W)
# Setting the weights of the last layer
layer.set_weights([new_kernel, new_bias])

#### 代价函数

# Optimization Functions
optimizer = Adam(lr=0.5e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
#optimizer = SGD(lr=1e-4, decay=0.0005, momentum=0.9)
#optimizer = RMSprop(lr=1e-4, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile(loss=custom_loss, optimizer=optimizer)
model.fit_generator(generator        = train_batch,
steps_per_epoch  = int(len(train_batch)/16),
epochs           = 100,
verbose          = 1,
validation_data  = valid_batch,
validation_steps = int(len(valid_batch)/16),
callbacks        = [early_stop, checkpoint,learning_rate_reduction],#, tensorboard],
max_queue_size   = 3)

### 输出精度 – 平均精确度（mAP分数）：

#### IoU

IoU测量两个区域之间有多少重叠，这等于并集区域上的重叠区域。这可以衡量预测（来自对象探测器）与真实情况（真实对象边界）的对比情况。总而言之，mAP分数是所有IoU阈值的平均AP。

### 未来的工作 – 持续或改进

https://github.com/bandiatindra/Object-Detection-Project