def transform(self, img, lbl):
img = img[:, :, ::-1]
img = img.astype(np.float64)
img -= self.mean
img = m.imresize(img, (self.img_size[0], self.img_size[1]))
# Resize scales images from 0 to 255, thus we need
# to divide by 255.0
img = img.astype(float) / 255.0
# NHWC -> NCWH
img = img.transpose(2, 0, 1)
lbl[lbl==255] = 0
lbl = lbl.astype(float)
lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), 'nearest', mode='F')
lbl = lbl.astype(int)
img = torch.from_numpy(img).float()
lbl = torch.from_numpy(lbl).long()
return img, lbl
python类imresize()的实例源码
def transform(self, img, lbl):
img = img[:, :, ::-1]
img = img.astype(np.float64)
img -= self.mean
img = m.imresize(img, (self.img_size[0], self.img_size[1]))
# Resize scales images from 0 to 255, thus we need
# to divide by 255.0
img = img.astype(float) / 255.0
# NHWC -> NCWH
img = img.transpose(2, 0, 1)
lbl = self.encode_segmap(lbl)
classes = np.unique(lbl)
lbl = lbl.astype(float)
lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), 'nearest', mode='F')
lbl = lbl.astype(int)
assert(np.all(classes == np.unique(lbl)))
img = torch.from_numpy(img).float()
lbl = torch.from_numpy(lbl).long()
return img, lbl
def get_image(filepath, image_target, image_size):
img = imread(filepath).astype(np.float)
h_origin, w_origin = img.shape[:2]
if image_target > h_origin or image_target > w_origin:
image_target = min(h_origin, w_origin)
h_drop = int((h_origin - image_target)/2)
w_drop = int((w_origin - image_target)/2)
if img.ndim == 2:
img = np.tile(img.reshape(h_origin, w_origin, 1), (1,1,3))
img_crop = img[h_drop:h_drop+image_target, w_drop:w_drop+image_target, :]
img_resize = imresize(img_crop, [image_size, image_size])
return np.array(img_resize)/127.5 - 1.
rl-network-test.py 文件源码
项目:Deep-Learning-with-Keras
作者: PacktPublishing
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def preprocess_images(images):
if images.shape[0] < 4:
# single image
x_t = images[0]
x_t = imresize(x_t, (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
s_t = np.stack((x_t, x_t, x_t, x_t), axis=2)
else:
# 4 images
xt_list = []
for i in range(images.shape[0]):
x_t = imresize(images[i], (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
xt_list.append(x_t)
s_t = np.stack((xt_list[0], xt_list[1], xt_list[2], xt_list[3]),
axis=2)
s_t = np.expand_dims(s_t, axis=0)
return s_t
############################# main ###############################
rl-network-train.py 文件源码
项目:Deep-Learning-with-Keras
作者: PacktPublishing
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def preprocess_images(images):
if images.shape[0] < 4:
# single image
x_t = images[0]
x_t = imresize(x_t, (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
s_t = np.stack((x_t, x_t, x_t, x_t), axis=2)
else:
# 4 images
xt_list = []
for i in range(images.shape[0]):
x_t = imresize(images[i], (80, 80))
x_t = x_t.astype("float")
x_t /= 255.0
xt_list.append(x_t)
s_t = np.stack((xt_list[0], xt_list[1], xt_list[2], xt_list[3]),
axis=2)
s_t = np.expand_dims(s_t, axis=0)
return s_t
def post_sub_one(inx):
w,h = 1918,1280
path,out,threshold = inx
data = np.load(path).item()
imgs,pred = data['name'], data['pred']
#print(pred.shape)
fo = open(out,'w')
#masks = pred>threshold
for name,mask in zip(imgs,np.squeeze(pred)):
mask = imresize(mask,[h,w])
mask = mask>threshold
code = rle_encode(mask)
code = [str(i) for i in code]
code = " ".join(code)
fo.write("%s,%s\n"%(name,code))
fo.close()
return 0
def show_one_img_mask(data):
w,h = 1918,1280
a = randint(0,31)
path = "../input/test"
data = np.load(data).item()
name,masks = data['name'][a],data['pred']
img = Image.open("%s/%s"%(path,name))
#img.show()
plt.imshow(img)
plt.show()
mask = np.squeeze(masks[a])
mask = imresize(mask,[h,w]).astype(np.float32)
print(mask.shape,mask[0])
img = Image.fromarray(mask*256)#.resize([w,h])
plt.imshow(img)
plt.show()
def get_image(filepath, image_target, image_size):
img = imread(filepath).astype(np.float)
h_origin, w_origin = img.shape[:2]
if image_target > h_origin or image_target > w_origin:
image_target = min(h_origin, w_origin)
h_drop = int((h_origin - image_target)/2)
w_drop = int((w_origin - image_target)/2)
if img.ndim == 2:
img = np.tile(img.reshape(h_origin, w_origin, 1), (1,1,3))
img_crop = img[h_drop:h_drop+image_target, w_drop:w_drop+image_target, :]
img_resize = imresize(img_crop, [image_size, image_size])
return np.array(img_resize)/127.5 - 1.
def PrepareDataList(BASE, length):
List = []
for M in range(0,min(length,len(BASE))):
img, text = BASE[M]
image = misc.imread(img,mode='RGB')
#image = misc.imresize(image, [227, 227])
r1 = []
if isfile(text):
f = open(text, 'r')
s = f.readline()
st = s.split(' ')
for i in range(0,2):
r1.append(int(st[i]))
f.close()
else: #If there are no txt file - "no bird situation"
r1.append(0);
r1.append(0);
List.append([image,r1])
return List
# Random test and train list
def predict_multi_scale(full_image, net, scales, sliding_evaluation, flip_evaluation):
"""Predict an image by looking at it with different scales."""
classes = net.model.outputs[0].shape[3]
full_probs = np.zeros((full_image.shape[0], full_image.shape[1], classes))
h_ori, w_ori = full_image.shape[:2]
for scale in scales:
print("Predicting image scaled by %f" % scale)
scaled_img = misc.imresize(full_image, size=scale, interp="bilinear")
if sliding_evaluation:
scaled_probs = predict_sliding(scaled_img, net, flip_evaluation)
else:
scaled_probs = net.predict(scaled_img, flip_evaluation)
# scale probs up to full size
h, w = scaled_probs.shape[:2]
probs = ndimage.zoom(scaled_probs, (1.*h_ori/h, 1.*w_ori/w, 1.),
order=1, prefilter=False)
# visualize_prediction(probs)
# integrate probs over all scales
full_probs += probs
full_probs /= len(scales)
return full_probs
def prepare_image(image, input_height=299, input_width=299):
""" Prepare an image to be passed through a network.
Arguments:
image (numpy.ndarray): An uint8 RGB image
Returns:
list: the image resized, centered and raveled
"""
# We assume an uint8 RGB image
assert image.dtype == np.uint8
assert image.ndim == 3
assert image.shape[2] == 3
resized_image = imresize(image, (input_height, input_width, 3))
float_image = resized_image.astype(np.float32)
centered_image = ((float_image / 255.) - 0.5) * 2.0
return centered_image.ravel().tolist()
def resize_images(prms):
seqNum = range(11)
rawStr = ['rawLeftImFile', 'rawRightImFile']
imStr = ['leftImFile', 'rightImFile']
num = ku.get_num_images()
for raw, new in zip(rawStr, imStr):
for seq in seqNum:
N = num[seq]
print seq, N, raw, new
rawNames = [prms['paths'][raw] % (seq,i) for i in range(N)]
newNames = [prms['paths'][new] % (seq,i) for i in range(N)]
dirName = os.path.dirname(newNames[0])
if not os.path.exists(dirName):
os.makedirs(dirName)
for rawIm, newIm in zip(rawNames, newNames):
im = scm.imread(rawIm)
im = scm.imresize(im, [256, 256])
scm.imsave(newIm, im)
##
# Save images as jpgs.
def applyTexture(x, y, texture = texture_input):
text = imread(texture_input)
height,width = text.shape[:2]
xmin, ymin = amin(x),amin(y)
xmax, ymax = amax(x),amax(y)
scale = max(((xmax - xmin + 2)/height),((ymax - ymin + 2)/width))
text = imresize(text, scale)
# print text.shape[:2]
# print xmax - xmin +2, ymax - ymin+2
X = (x-xmin).astype(int)
Y = (y-ymin).astype(int)
val1 = color.rgb2lab((text[X, Y]/255.).reshape(len(X), 1, 3)).reshape(len(X), 3)
val2 = color.rgb2lab((im[x, y]/255.).reshape(len(x), 1, 3)).reshape(len(x), 3)
L, A, B = mean(val2[:,0]), mean(val2[:,1]), mean(val2[:,2])
val2[:, 0] = np.clip(val2[:, 0] - L + val1[:,0], 0, 100)
val2[:, 1] = np.clip(val2[:, 1] - A + val1[:,1], -127, 128)
val2[:, 2] = np.clip(val2[:, 2] - B + val1[:,2], -127, 128)
im[x,y] = color.lab2rgb(val2.reshape(len(x), 1, 3)).reshape(len(x), 3)*255
# points = np.loadtxt('nailpoint_5')
def preprocess_data(img, preprocess_mode, im_sz, data_mode):
if preprocess_mode == 'pad':
if data_mode == 'image':
img = np.pad(img, ((0, im_sz-img.shape[0]), (0, im_sz-img.shape[1]), (0,0)), 'constant', constant_values=(0))
elif data_mode == 'label':
img = np.pad(img, ((0, im_sz-img.shape[0]), (0, im_sz-img.shape[1])), 'constant', constant_values=(0))
else:
print('Invalid data mode.', file=sys.stderr)
elif preprocess_mode == 'res':
img = imresize(img, (im_sz, im_sz), interp='bilinear')
else:
print('Invalid preprocess mode.', file=sys.stderr)
return img
def load_alphamatting_data(test_alpha):
rgb_path = os.path.join(test_alpha,'rgb')
trimap_path = os.path.join(test_alpha,'trimap')
alpha_path = os.path.join(test_alpha,'alpha')
images = os.listdir(trimap_path)
test_num = len(images)
all_shape = []
rgb_batch = []
tri_batch = []
alp_batch = []
for i in range(test_num):
rgb = misc.imread(os.path.join(rgb_path,images[i]))
trimap = misc.imread(os.path.join(trimap_path,images[i]),'L')
alpha = misc.imread(os.path.join(alpha_path,images[i]),'L')/255.0
all_shape.append(trimap.shape)
rgb_batch.append(misc.imresize(rgb,[320,320,3])-g_mean)
trimap = misc.imresize(trimap,[320,320],interp = 'nearest').astype(np.float32)
tri_batch.append(np.expand_dims(trimap,2))
alp_batch.append(alpha)
return np.array(rgb_batch),np.array(tri_batch),np.array(alp_batch),all_shape,images
def load_validation_data(vali_root):
alpha_dir = os.path.join(vali_root,'alpha')
RGB_dir = os.path.join(vali_root,'RGB')
images = os.listdir(alpha_dir)
test_num = len(images)
all_shape = []
rgb_batch = []
tri_batch = []
alp_batch = []
for i in range(test_num):
rgb = misc.imread(os.path.join(RGB_dir,images[i]))
alpha = misc.imread(os.path.join(alpha_dir,images[i]),'L')
trimap = generate_trimap(np.expand_dims(np.copy(alpha),2),np.expand_dims(alpha,2))[:,:,0]
alpha = alpha / 255.0
all_shape.append(trimap.shape)
rgb_batch.append(misc.imresize(rgb,[320,320,3])-g_mean)
trimap = misc.imresize(trimap,[320,320],interp = 'nearest').astype(np.float32)
tri_batch.append(np.expand_dims(trimap,2))
alp_batch.append(alpha)
return np.array(rgb_batch),np.array(tri_batch),np.array(alp_batch),all_shape,images
def main(args):
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction = args.gpu_fraction)
with tf.Session(config=tf.ConfigProto(gpu_options = gpu_options)) as sess:
saver = tf.train.import_meta_graph('./meta_graph/my-model.meta')
saver.restore(sess,tf.train.latest_checkpoint('./model'))
image_batch = tf.get_collection('image_batch')[0]
GT_trimap = tf.get_collection('GT_trimap')[0]
pred_mattes = tf.get_collection('pred_mattes')[0]
rgb = misc.imread(args.rgb)
alpha = misc.imread(args.alpha,'L')
trimap = generate_trimap(np.expand_dims(np.copy(alpha),2),np.expand_dims(alpha,2))[:,:,0]
origin_shape = alpha.shape
rgb = np.expand_dims(misc.imresize(rgb.astype(np.uint8),[320,320,3]).astype(np.float32)-g_mean,0)
trimap = np.expand_dims(np.expand_dims(misc.imresize(trimap.astype(np.uint8),[320,320],interp = 'nearest').astype(np.float32),2),0)
feed_dict = {image_batch:rgb,GT_trimap:trimap}
pred_alpha = sess.run(pred_mattes,feed_dict = feed_dict)
final_alpha = misc.imresize(np.squeeze(pred_alpha),origin_shape)
# misc.imshow(final_alpha)
misc.imsave('./alpha.png',final_alpha)
def _transform(self, filename, flag = False):
if flag:
image = np.array(Image.open(filename), dtype=np.uint8)
image[image == 255] = 21
else:
image = misc.imread(filename)
if self.__channels and len(image.shape) < 3: # make sure images are of shape(h,w,3)
image = np.array([image for i in range(3)])
if self.image_options.get("resize", False) and self.image_options["resize"]:
resize_size = int(self.image_options["resize_size"])
resize_image = misc.imresize(image,
[resize_size, resize_size], interp='nearest')
else:
resize_image = image
return np.array(resize_image)
def test_graphcut():
import matplotlib.pyplot as plt
from scipy.misc import lena
im = lena()
# Any bigger and my weak laptop gets memory errors
bounds = (50, 50)
im = imresize(im, bounds, interp="bicubic")
all_matches, all_splits = graphcut(im, split_type="mean")
to_plot = all_splits[-1]
f, axarr = plt.subplots(2, len(to_plot) // 2)
for n in range(len(to_plot)):
axarr.ravel()[n].imshow(to_plot[n], cmap="gray")
axarr.ravel()[n].set_xticks([])
axarr.ravel()[n].set_yticks([])
plt.show()
def segment(self,img,box,landmarks):
left,top,right,bottom = box
avglr = int((left + right) / 2)
avgtb = int((top + bottom) / 2)
r_cir = int(max((right - left),(bottom - top) )/ 2)
if isinstance(self.margin,int):
margin = self.margin
elif isinstance(self.margin,float):
margin = r_cir * self.margin
bb = np.zeros(4, dtype=np.int32)
bb[0] = np.maximum(left-margin/2, 0)
bb[1] = np.maximum(top-margin/2, 0)
bb[2] = np.minimum(right+margin/2, img.shape[1])
bb[3] = np.minimum(bottom+margin/2, img.shape[0])
cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]
scaled = misc.imresize(cropped, (self.img_size, self.img_size), interp='bilinear')
return [('expand-align',scaled)]
def segment(self,img,box,landmarks):
left,top,right,bottom = box
avglr = int((left + right) / 2)
avgtb = int((top + bottom) / 2)
r_cir = int(max((right - left),(bottom - top) )/ 2)
if isinstance(self.margin,int):
margin = self.margin
elif isinstance(self.margin,float):
margin = r_cir * self.margin
bb = np.zeros(4, dtype=np.int32)
bb[0] = np.maximum(left-margin/2, 0)
bb[1] = np.maximum(top-margin/2, 0)
bb[2] = np.minimum(right+margin/2, img.shape[1])
bb[3] = np.minimum(bottom+margin/2, img.shape[0])
cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]
scaled = misc.imresize(cropped, (self.img_size, self.img_size), interp='bilinear')
return [('expand-align',scaled)]
def run_pretrained(input_state,model,action_states,gameState):
print '\n\nLoading pretrained weights onto model...'
model.load_weights(p.PRETRAINED_PATH)
epsilon=1
while True:
print 'Running pretrained model (no exploration) with weights at ', p.PRETRAINED_PATH
nn_out = model.predict(input_state,batch_size=1,verbose=0)
nn_action = [[0,0]]
nn_action[0][np.argmax(nn_out)] =1
action,rand_flag = select_action(nn_action+action_states,prob=[epsilon,(1-epsilon)*1/7,(1-epsilon)*6/7])
rgbDisplay, reward, tState = gameState.frame_step(action)
grayDisplay = (np.dot(np.fliplr(imrotate(imresize(rgbDisplay, (80,80), interp='bilinear'), -90))[:,:,:3], [0.299, 0.587, 0.114])).reshape((1,1,80,80))
output_state = np.append(grayDisplay,input_state[:,:p.HISTORY-1,:,:], axis=1)
#############################################################################################################################################################################
ilsvrc_cls_multithread_scipy.py 文件源码
项目:tensorflow_yolo2
作者: wenxichen
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def image_read(self, imname):
image = misc.imread(imname, mode='RGB').astype(np.float)
r,c,ch = image.shape
if r < 299 or c < 299:
# TODO: check too small images
# print "##too small!!"
image = misc.imresize(image, (299, 299, 3))
elif r > 299 or c > 299:
image = image[(r-299)/2 : (r-299)/2 + 299, (c-299)/2 : (c-299)/2 + 299, :]
# print r, c, image.shape
assert image.shape == (299, 299, 3)
image = (image / 255.0) * 2.0 - 1.0
if self.random_noise:
add_noise = bool(random.getrandbits(1))
if add_noise:
eps = random.choice([4.0, 8.0, 12.0, 16.0]) / 255.0 * 2.0
noise_image = image + eps * np.random.choice([-1, 1], (299,299,3))
image = np.clip(noise_image, -1.0, 1.0)
return image
def resizeImg(imgPath,img_size):
try:
img = imread(imgPath)
h, w, _ = img.shape
scale = 1
if w >= h:
new_w = img_size
if w >= new_w:
scale = float(new_w) / w
new_h = int(h * scale)
else:
new_h = img_size
if h >= new_h:
scale = float(new_h) / h
new_w = int(w * scale)
new_img = imresize(img, (new_h, new_w), interp='bilinear')
imsave(imgPath,new_img)
print('Img Resized as {}'.format(img_size))
except Exception as e:
print(e)
def resizeImg(imgPath,img_size):
img = imread(imgPath)
h, w, _ = img.shape
scale = 1
if w >= h:
new_w = img_size
if w >= new_w:
scale = float(new_w) / w
new_h = int(h * scale)
else:
new_h = img_size
if h >= new_h:
scale = float(new_h) / h
new_w = int(w * scale)
new_img = imresize(img, (new_h, new_w), interp='bilinear')
imsave(imgPath,new_img)
#Download img
#Later we can do multi thread apply workers to do faster work
def resizeImg(imgPath,img_size):
img = imread(imgPath)
h, w, _ = img.shape
scale = 1
if w >= h:
new_w = img_size
if w >= new_w:
scale = float(new_w) / w
new_h = int(h * scale)
else:
new_h = img_size
if h >= new_h:
scale = float(new_h) / h
new_w = int(w * scale)
new_img = imresize(img, (new_h, new_w), interp='bilinear')
imsave(imgPath,new_img)
print('Img Resized as {}'.format(img_size))
def sliceImages(inputImage, targetImage):
inputSlices = []
targetSlices = []
sliceSize = 32
for y in range(0,inputImage.shape[1]//sliceSize):
for x in range(0,inputImage.shape[0]//sliceSize):
inputSlice = inputImage[x*sliceSize:(x+1)*sliceSize,y*sliceSize:(y+1)*sliceSize]
targetSlice = targetImage[x*sliceSize//2:(x+1)*sliceSize//2,y*sliceSize//2:(y+1)*sliceSize//2]
# only add slices if they're not just empty space
# if (np.any(targetSlice)):
# Reweight smaller sizes
# for i in range(0,max(1,128//inputImage.shape[1])**2):
inputSlices.append(inputSlice)
targetSlices.append(targetSlice)
# inputSlices.append(np.fliplr(inputSlice))
# targetSlices.append(np.fliplr(targetSlice))
# inputSlices.append(np.flipud(inputSlice))
# targetSlices.append(np.flipud(targetSlice))
# naiveSlice = imresize(inputSlice, 0.5)
# deltaSlice = targetSlice - naiveSlice
# targetSlices.append(deltaSlice)
# return two arrays of images in a tuple
return (inputSlices, targetSlices)
def load_mask(mask_path, shape):
mask = imread(mask_path, mode="L") # Grayscale mask load
width, height, _ = shape
mask = imresize(mask, (width, height), interp='bicubic').astype('float32')
# Perform binarization of mask
mask[mask <= 127] = 0
mask[mask > 128] = 255
max = np.amax(mask)
mask /= max
return mask
# util function to apply mask to generated image
def preprocess_image(image_path, load_dims=False, style_image=False):
global img_WIDTH, img_HEIGHT, aspect_ratio, b_scale_ratio_height, b_scale_ratio_width
img = imread(image_path, mode="RGB") # Prevents crashes due to PNG images (ARGB)
if load_dims:
img_WIDTH = img.shape[0]
img_HEIGHT = img.shape[1]
aspect_ratio = img_HEIGHT / img_WIDTH
if style_image:
b_scale_ratio_width = float(img.shape[0]) / img_WIDTH
b_scale_ratio_height = float(img.shape[1]) / img_HEIGHT
img = imresize(img, (img_width, img_height))
img = img.transpose((2, 0, 1)).astype('float64')
img = np.expand_dims(img, axis=0)
return img
# util function to convert a tensor into a valid image
def transformData(self, data):
if(self.opts.has_key('newdims')):
(H, W) = self.opts['newdims']
data = misc.imresize(data, (H, W), interp='bilinear')
if(self.opts.has_key('zeromean') and self.opts['zeromean']):
mean = self.opts['dataset_mean'] # provided by bmanager
data = data - mean
if(self.opts.has_key('rangescale') and self.opts['rangescale']):
min_ = self.opts['dataset_min'] # provided by bmanager
min_ = np.abs(min_.min())
max_ = self.opts['dataset_max'] # provided by bmanager
max_ = np.abs(max_.max())
data = 127 * data / max(min_, max_)
else:
data = data - 127.0
return data