提问人:kot ucheniy 提问时间:7/27/2023 更新时间:7/27/2023 访问量:33
我需要 20x20 图像形式的 ai(感知器)数据,手动创建它会变慢。我如何训练它?
I need data for my ai (perceptron) in the form of 20x20 images.creating it by hand is to slow. how I train it?
问:
我在 python 中复制了第一个 perceptron(ai),它使用一个 20x20 矩阵,其中 1 和 0 作为输入(1=白色,0=黑色)。我为它制作了 4 个图像矩阵,但我花了半个小时才制作它们。 人工智能应该区分由图像矩阵上的 1 组成的矩形和圆形,因此仅仅做出随机选择是行不通的。
我试着用手制作一些矩阵。这是为了放慢速度。
答:
0赞
tintin98
7/27/2023
#1
我假设您正在尝试生成 20x20 的圆形和矩形图像。
为此,请使用 OpenCV 绘图工具。教程可以在这里找到。
我对此进行了编码,并检查了运行时间,用于生成每个圆和矩形的 50 个样本的最大 0.05 秒。
import cv2 as cv
import numpy as np
import time
h, w = 20, 20 #the image dimensions
start = time.time()
#code to generate circle
sample_count = 50 #give number of sample as per your requirement
circle_sample = [] #contains images of circles
circle_added = [] #contains circles added to the samples
while len(circle_sample) != sample_count:
#choose center
centerX = np.random.randint(0, h)
centerY = np.random.randint(0, w)
#calculate an upper limit for radius such that circle does not exceed the image dimensions
#take the upper limit as minimum space in all 4 directions of the center
radius_upper_limit = min([centerX, h-centerX, centerY, w-centerY])
#choose radius...I want more than 5 pixels radius
if radius_upper_limit > 5:
radius = np.random.randint(5, radius_upper_limit)
#define this cirle with centerX, centerY and radius and check whether this has already been included or not
circle = (centerX, centerY, radius)
if circle not in circle_added:
circle_added.append(circle)
img = np.zeros((h, w), dtype=np.uint8) #take a background image of all 1s
#use drawing tool to draw the circle on the image
circle_img = cv.circle(img, (centerX, centerY), radius, 1, cv.FILLED)
circle_sample.append(circle_img)
#code to generate squares
sample_count = 50 #give number of sample as per your requirement
rect_sample = [] #contains images of rectangles
rect_added = [] #contains rectangles added to the samples
while len(rect_sample) != sample_count:
#choose top-left corner of the rectangle
top = np.random.randint(0, h)
left = np.random.randint(0, w)
#calculate the upper limit for height (vertical direction) and width (horizontal direction)
height_upper_limit = min(top, h-top)
width_upper_limit = min(left, w-left)
#find bottom-right (opposite of top-left corner) corner....I want rect with more than 25 pixel area
if height_upper_limit>5 and width_upper_limit>5:
bottom = np.random.randint(5, height_upper_limit)
right = np.random.randint(5, width_upper_limit)
#define rectanlge by top-left and bottom-right corners and check whether this has already been added or not
rect = (left, top, right, bottom)
if rect not in rect_added:
rect_added.append(rect)
img = np.zeros((h, w), dtype=np.uint8)
#use drawing tool to draw rectangle on image
rect_img = cv.rectangle(img, (left, top), (right, bottom), 1, cv.FILLED)
rect_sample.append(rect_img)
end = time.time()
exec_time = end-start
print("Execution Time: {:.2f} seconds".format(exec_time))
我已经在其中的注释中记录了代码。
注意
- 我不会选择半径小于或等于 5 像素的圆和面积小于或等于 25 像素的矩形。
- 我正在设置一个上限,以便圆形或矩形对象不超过图像尺寸。
- 填充颜色为 1,这是您想要的。如果要可视化图像,则必须将图像矩阵标量乘以 255 或将图像填充颜色更改为 255。
- 你得到的是一个图像列表,即 2D in 和 of size .如果需要,您可以保存这些图像。
np.ndarray
circle_sample
rect_sample
20X20
- 值上的一个点 - 它本质上是将 -1 传递给 OpenCV 中绘图函数中的参数,这意味着“填充形状”。对于正值,它将绘制给定厚度的轮廓。
cv.FILLED
thickness
评论