Skip to content

Instantly share code, notes, and snippets.

@fantods
Created February 11, 2019 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fantods/ce1719559c7175cc22906585f738feb6 to your computer and use it in GitHub Desktop.
Save fantods/ce1719559c7175cc22906585f738feb6 to your computer and use it in GitHub Desktop.
from keras.utils.data_utils import Sequence
import requests
import io
class BatchSequence(Sequence):
def __init__(self, x_set, y_set, batch_size=32, image_size=228):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
self.image_size = image_size
def __len__(self):
return int(np.ceil(len(self.x) / float(self.batch_size)))
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
images = [self.url_to_image(file_name) for file_name in batch_x]
return np.array(images), np.array(batch_y)
def url_to_image(self, url):
try:
response = requests.get(url)
image = Image.open(io.BytesIO(response.content))
image.thumbnail((self.image_size, self.image_size))
image = np.array(image)
except Exception as e:
output = [1]*(self.image_size*self.image_size*3)
output = np.array(output).reshape(self.image_size,self.image_size,3).astype('uint8')
image = Image.fromarray(output).convert('RGB')
return image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment