Skip to content

Instantly share code, notes, and snippets.

@framp
Last active October 27, 2020 17:20
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 framp/71b2e6138d3b4740782bda3aa9d9f4ae to your computer and use it in GitHub Desktop.
Save framp/71b2e6138d3b4740782bda3aa9d9f4ae to your computer and use it in GitHub Desktop.
from __future__ import division
import math
import numpy as np
import tensorflow as tf
from PIL import Image
net_model = tf.keras.models.load_model('NIvsCG_sgd_lr-1e-5_WITH_REDUCE-LR.28-0.29.h5')
## images here
images = []
for image_path in images:
img = Image.open(image_path)
img.load()
#load full image
data = np.asarray( img, dtype="int32" )
(width,height,rgb) = data.shape
patch_size = min(width, height, 233)
(patch_width, patch_height) = (patch_size, patch_size)
#print(image_path, width, height)
#split in patches
len = 0
sum = 0
for x in range(math.ceil(width / patch_width)):
for y in range(math.ceil(width / patch_height)):
len=len+1
xstart = x * patch_width
xend = (x+1) * patch_width
if xend > width:
xstart = width - patch_width
xend = width
ystart = y * patch_height
yend = (y+1) * patch_height
if yend > height:
ystart = height - patch_height
yend = height
sliced = data[xstart:xend,ystart:yend,:]
#print(image_path, xstart, xend, ystart, yend, data.shape)
image = sliced.reshape((1,) + sliced.shape)
#compute prediction
output = net_model.predict([image], batch_size = 1)
sum = sum + output[0].argmax()
#print(image_path, x, y, output)
print(image_path, sum/len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment