def getImage(sampSize, newSize, imPath, _=''): ''' randomly samples an image ''' f1 = int(np.sign(np.random.rand() - .5)) f2 = int(np.sign(np.random.rand() - .5)) im = image.load_img(imPath).resize((newSize, newSize)) r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) im = image.random_rotation(im, 5) im = image.img_to_array(im)[:, ::-f1, ::-f2] im = (im - im.min()) / (im.max() - im.min()) return im
def getOne(batchSize, imPath): allIms = np.zeros((batchSize, 3, sampSize, sampSize)) for i in xrange(batchSize): f1 = int(np.sign(np.random.rand() - .5)) f2 = int(np.sign(np.random.rand() - .5)) im = image.load_img(imPath).resize((newSize, newSize)) r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) im = image.random_rotation(im, 5) allIms[i, :, :, :] = image.img_to_array(im) allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2] / 255. return allIms
def getBatch(batchSize, path): allPaths = image.list_pictures(path) allIms = np.zeros((batchSize, 3, sampSize, sampSize)) for i in xrange(batchSize): f1 = int(np.sign(np.random.rand() - .5)) f2 = int(np.sign(np.random.rand() - .5)) im = image.load_img(allPaths[int(np.random.rand() * len(allPaths))]).resize((newSize, newSize)) r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) im = image.random_rotation(im, 5) allIms[i, :, :, :] = image.img_to_array(im) allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2] return allIms/255.
def getBatch(batchSize, allPaths, numClass): allIms = np.zeros((batchSize, 3, sampSize, sampSize)) Y = np.zeros((batchSize, 1)).astype('int') rIms = np.zeros((batchSize, 1)).astype('str') for i in xrange(batchSize): f1 = int(np.sign(np.random.rand() - .5)) f2 = int(np.sign(np.random.rand() - .5)) rIm = allPaths[int(np.random.rand() * len(allPaths))] rIms[i] = rIm im = image.load_img(rIm).resize((newSize, newSize)) Y[i] = labels[rIm.split('/')[-1]] r = int(np.random.rand() * (newSize - sampSize)) c = int(np.random.rand() * (newSize - sampSize)) im = im.crop((r, c, r + sampSize, c + sampSize)) im = image.random_rotation(im, 5) allIms[i, :, :, :] = image.img_to_array(im) allIms[i, :, :, :] = allIms[i, :, ::-f1, ::-f2] if numClass > 2: Y2 = np.zeros((batchSize, numClass)).astype('int') for i in xrange(batchSize): Y2[i, Y[i][0]] = 1 Y = Y2 return allIms, Y, rIms