I need data for my ai (perceptron) in the form of 20x20 images.creating it by hand is to slow. how I train it?

44 views Asked by At

I made a copy of the first perceptron(ai) in python which use a 20x20 matrix with ones and zeros as input(1=white,0=black). I made 4 image-matrix for it, but it took me half an hour to make them. the ai should distinguish rectangles and circles made from 1s on the image-matrix, so just making random choices won't work.

I tried to make some matrix by hand. it is to slow.

1

There are 1 answers

0
tintin98 On

I am assuming that you are trying to generate 20x20 images of circles and rectangles.

Use the OpenCV drawing tools for this purpose. The tutorial can be found here.

I coded this and checked the runtime to be 0.05 s at max for generating 50 samples of each circle and rectangle.

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))

I have documented the code in the comments within it.

NOTE

  1. I am not picking circles that have a radius less than or equal to 5 pixels and rectangles that are of area less than or equal to 25 pixels.
  2. I am setting an upper limit so that the circle or rectangle object does not exceed the image dimensions.
  3. The fill color is given as 1 which is what you wanted. If you want to visualize the images then you have to scalar multiply the image matrix by 255 or change the image fill color to 255.
  4. What you get is a list of images i.e. 2D np.ndarray in circle_sample and rect_sample of size 20X20. You can save these images if you want to.
  5. A point on the cv.FILLED value - it is essentially passing -1 to the thickness parameter in the drawing function in OpenCV which means "fill the shape". For positive values it will draw a contour of given thickness.