import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
import torch
### 读入图像
img = cv.imread('C:/Users/Dell/Desktop/cv/horse.jpg')
### 对图像进行展示
plt.imshow(img)
plt.show()
### cv默认对图像的读取方式为BGR
print(type(img))
print(img.shape)
### 将格式转换为RGB
img_convert = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.imshow(img_convert)
### 图像的RGB矩阵表示
image_array=np.array(img)
print(image_array)
### 转换为列向量表示
image = image_array.flatten()
print (image)
kind = 10
rate = 1e-3
### L2正则化
def L2_Regularization () :
return rate * np.sum(weight ** 2)
### 采用多类支撑向量机损失
def loss_function (outputs , labels) :
num_labels = len(labels)
corrects = outputs[range(num_labels), labels].unsqueeze(0).T
# 最大间隔
margin = 1.0
margins = outputs - corrects + margin
loss = torch.sum(torch.max(margins, 1)[0]) / len(labels)
# # 正则化强度
loss += rate * L2_Regularization()
return loss
对图片RGB编码后的展示