以前没发过优快云,这里跟大家主要分享一下我自己做深度学习的
四法五步架构方法。
四法:也就是四个必要的方法
即 数据通道模块、网络架构模块、预测模块、学习模块
五步:也就是从架构模型到训练结果的五个步骤
即 数据收集、数据强化、数据池构建、数据池更新、数据训练
我所有的网络架构都是这么搞的,逻辑很清晰。当你想添加一些其他的数据处理方法,例如维度变换、语义上下文分析,或者把深度学习架到其他算法中,你会发现我的这种架构很好用,还方便。
之前经常来回调包。用人家的东西,这次正好学校有这个机会,网络又比较简单,就自己用tf1里面最基础的运算码了一下午,搞出来了这个AlexNet。
勤动手很重要的,改错、调整的过程,都是很宝贵的学习过程,就这样。
import numpy as np
import cv2
'''
实现一个数据管道类,从文件中读取图像流数据
参数:
self.category:数据通道所属类别,指定了文件路径,对于每一种类的图片,设置不同路径。
self.RF_pool:强化池最大数量。
方法:
A.数据强化:指定图像路径,加载图像,对图像进行加噪点、翻转等强化,放入强化池。
B.取数据:从强化池中取定量数据。可以规定取数据的数量。
'''
class Data_Channel():
def __init__(self, category, pool_size):
self.CG = category
self.CG_path_list = {"cloudy":"./IEEE-weather_classification/cloudy/", "haze":"./IEEE-weather_classification/haze/",
"rainy":"./IEEE-weather_classification/rainy/", "snow":"./IEEE-weather_classification/snow/",
"sunny":"./IEEE-weather_classification/sunny/", "thunder":"./IEEE-weather_classification/thunder/"}
self.PS = pool_size
self.PATH = self.CG_path_list[self.CG]
self.RF_pool = np.empty(shape=[self.PS, 227, 227, 3])
def Renew_dataset(self):
for m in range(self.PS):
index = np.random.randint(1,9999)
path_now = self.PATH + self.CG + "_" + str(index).zfill(5) + ".jpg"
img = cv2.imread(path_now)
#RFImge = self.Img_reinforce(img)
ResizeImg = cv2.resize(img, (227, 227))
self.RF_pool[m] = ResizeImg
def Img_reinforce(self, img):
change_odd = np.random.randint(0, 8)
if (change_odd <= 2):
Last_img = self.random_light(img)
elif (change_odd <= 4):
Last_img = self.rotate(img)
elif (change_odd <= 6):
Last_img = self.add_noise(img)
else:
Last_img = self.flip_image(img)
return Last_img
def flip_image(self, img):
flipped_img = np.fliplr(img)
return (flipped_img)
def add_noise(self, img):
sp = img.shape
HEIGHT = sp[0]
WIDTH = sp[1]
for i in range(100):
x = np.random.randint(0, HEIGHT)
y = np.random.randint(0, WIDTH)
img[x, y, :] = 255
return (img)
def rotate(self, image):
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
angle = np.random.randint(-60, 60)
scale = np.random.randint(150, 250)
scale /= 100
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
def random_light(self, img):
lgt_opp = np.random.randint(-50, 10)
lgt_opp /= 100
sp = img.shape
HEIGHT = sp[0]
WIDTH = sp[1]
for xi in range(0, WIDTH):
for xj in range(0, HEIGHT):
img[xj, xi, 0] = int(img[xj, xi, 0] * (1 + lgt_opp))
img[xj, xi, 1] = int(img[xj, xi, 0] * (1 + lgt_opp))
img[xj, xi, 2] = int(img[xj, xi, 0] * (1 + lgt_opp))
return (img)
if __name__ == "__main__":
DC1 = Data_Channel(category="snow", pool_size=10)
DC1.Renew_dataset()
print(DC1.RF_pool)
在这里插入代码片