Skip to content

Instantly share code, notes, and snippets.

@timseed
Created March 23, 2018 12:57
Show Gist options
  • Save timseed/fcbf1050d54fb295d4bfddeca33f4a2e to your computer and use it in GitHub Desktop.
Save timseed/fcbf1050d54fb295d4bfddeca33f4a2e to your computer and use it in GitHub Desktop.
Load Imges for Keras Multi Class Classification
from glob import glob
from PIL import Image
import numpy as np
def img_data(class_id,filename):
'''
Return an np array of
CLASS_NUMBER
'''
im=np.array(Image.open(filename))
label = [class_id]
r = im[:,:,0].flatten()
g = im[:,:,1].flatten()
b = im[:,:,2].flatten()
return np.array(list(label) +list(r) + list(g) + list(b),np.uint8)
#
#Read our Test/Train data sets into to np
#So we then can use multi class learning on them
#
#
FILES=3
train=[]
for n in range(6):
for f in glob("./train/L{}/*".format(n))[:FILES]:
train.append(img_data(n,f))
print("train Shape is {}".format(len(train)))
train_data["files"].append(f)
train_data["class"].append("L{}".format(n))
test=[]
for n in range(6):
for f in glob("./validation/L{}/*".format(n))[:FILES]:
test.append(img_data(n,f))
print("test Shape is {}".format(len(test)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment