Machine learning, especially deep learning, often uses image classification as a subject. However, the material used there seems to be fixed, so I wrote a script to automatically generate a large number of images of koalas and bears in order to provide new material.
Below is the code.
from PIL import Image, ImageDraw
from IPython.display import HTML
import random
def koala_or_bear(bear=False, rotate=False, resize=False, gray=True, black=True, others=False):
r = random.randint(100, 255)
g = random.randint(100, 255)
b = random.randint(100, 255)
if gray:
r = g = b
im = Image.new('RGB', (500, 500), (r, g, b))
draw = ImageDraw.Draw(im)
if others:
for i in range(100):
r = random.randint(100, 255)
g = random.randint(100, 255)
b = random.randint(100, 255)
if gray:
r = g = b
x1 = random.randint(0, 500)
y1 = random.randint(0, 500)
x2 = random.randint(0, 500)
y2 = random.randint(0, 500)
draw.ellipse((x1, x2, y1, y2), fill=(r, g, b))
r = random.randint(0, 200)
g = random.randint(0, 200)
b = random.randint(0, 200)
if black:
r = g = b = 1
dx1 = random.randint(-10, 0)
dx2 = random.randint(0, 10)
dy1 = random.randint(-10, 0)
dy2 = random.randint(0, 10)
if bear:
draw.ellipse((200 + dx1, 200 + dy1, 300 + dx2, 300 + dy2), fill=(r, g, b))
else:
draw.ellipse((210 + dx1, 210 + dy1, 290 + dx2, 290 + dy2), fill=(r, g, b))
dx1 = random.randint(-5, 10)
dx2 = random.randint(-10, 5)
dy1 = random.randint(-5, 10)
dy2 = random.randint(-10, 5)
cx1 = random.randint(160, 180)
cx2 = random.randint(230, 250)
if bear:
#draw.ellipse((160 + dx1, 160 + dy1, 230 + dx2, 230 + dy2), fill=(r, g, b))
draw.ellipse((160 + dx1, cx1 + dy1, 230 + dx2, cx2 + dy2), fill=(r, g, b))
else:
#draw.ellipse((160 + dx1, 190 + dy1, 230 + dx2, 260 + dy2), fill=(r, g, b))
draw.ellipse((160 + dx1, 210 + dy1, 230 + dx2, 280 + dy2), fill=(r, g, b))
dx1 = random.randint(-5, 10)
dx2 = random.randint(-10, 5)
dy1 = random.randint(-5, 10)
dy2 = random.randint(-10, 5)
if bear:
#draw.ellipse((270 + dx1, 160 + dy1, 340 + dx2, 230 + dy2), fill=(r, g, b))
draw.ellipse((270 + dx1, cx1 + dy1, 340 + dx2, cx2 + dy2), fill=(r, g, b))
else:
#draw.ellipse((270 + dx1, 190 + dy1, 340 + dx2, 260 + dy2), fill=(r, g, b))
draw.ellipse((270 + dx1, 210 + dy1, 340 + dx2, 280 + dy2), fill=(r, g, b))
if rotate:
angle = random.randint(0, 360)
im = im.rotate(angle)
if resize:
h = random.randint(100, 200)
center = random.randint(220, 280)
size = 384
if type(resize) == int:
size = resize
im = im.resize(size=(size, size), resample=Image.LANCZOS, box=(max(0, center - h), max(0, center - h),
min(500, center + h), min(500, center + h)))
return im
im = koala_or_bear()
im.save('image.jpg', quality=95)
HTML('<img src="image.jpg ">')
!mkdir koala_or_bear
num_data = 16
for i in range(num_data):
im = koala_or_bear(bear=False)
im.save("koala_or_bear/koala_{}.jpg ".format(i), quality=95)
for i in range(num_data):
im = koala_or_bear(bear=True)
im.save("koala_or_bear/bear_{}.jpg ".format(i), quality=95)
from PIL import Image
koalas = []
for i in range(num_data):
koala = Image.open("koala_or_bear/koala_{}.jpg ".format(i))
koalas.append(koala)
bears = []
for i in range(num_data):
bear = Image.open("koala_or_bear/bear_{}.jpg ".format(i))
bears.append(bear)
%matplotlib inline
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,10))
for i in range(num_data):
ax = fig.add_subplot(4, 4, i+1)
ax.axis('off')
ax.set_title('koala_{}'.format(i))
ax.imshow(koalas[i],cmap=plt.cm.gray, interpolation='none')
plt.show()
%matplotlib inline
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,10))
for i in range(num_data):
ax = fig.add_subplot(4, 4, i+1)
ax.axis('off')
ax.set_title('bear_{}'.format(i))
ax.imshow(bears[i],cmap=plt.cm.gray, interpolation='none')
plt.show()
for i in range(num_data):
im = koala_or_bear(bear=False, rotate=True, resize=True)
im.save("koala_or_bear/koala_{}.jpg ".format(i), quality=95)
bears = []
for i in range(num_data):
bear = Image.open("koala_or_bear/koala_{}.jpg ".format(i))
bears.append(bear)
fig = plt.figure(figsize=(10,10))
for i in range(num_data):
ax = fig.add_subplot(4, 4, i+1)
ax.axis('off')
ax.set_title('koala_{}'.format(i))
ax.imshow(bears[i],cmap=plt.cm.gray, interpolation='none')
plt.show()
for i in range(num_data):
im = koala_or_bear(bear=False, rotate=True, resize=True, gray=False, black=False)
im.save("koala_or_bear/koala_{}.jpg ".format(i), quality=95)
bears = []
for i in range(num_data):
bear = Image.open("koala_or_bear/koala_{}.jpg ".format(i))
bears.append(bear)
fig = plt.figure(figsize=(10,10))
for i in range(num_data):
ax = fig.add_subplot(4, 4, i+1)
ax.axis('off')
ax.set_title('koala_{}'.format(i))
ax.imshow(bears[i],cmap=plt.cm.gray, interpolation='none')
plt.show()
for i in range(num_data):
im = koala_or_bear(bear=True, rotate=True, resize=True, gray=False, black=False, others=True)
im.save("koala_or_bear/bear_{}.jpg ".format(i), quality=95)
bears = []
for i in range(num_data):
bear = Image.open("koala_or_bear/bear_{}.jpg ".format(i))
bears.append(bear)
fig = plt.figure(figsize=(10,10))
for i in range(num_data):
ax = fig.add_subplot(4, 4, i+1)
ax.axis('off')
ax.set_title('bear_{}'.format(i))
ax.imshow(bears[i],cmap=plt.cm.gray, interpolation='none')
plt.show()
We have created a tool that automatically generates koala images and bear images that can be used as image classification by machine learning or deep learning. I think you can make various variations and adjust the difficulty level. It's also good for studying semantic segmentation.
Recommended Posts