Python scipy.misc 模块,imresize() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用scipy.misc.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[lbl==255] = 0
lbl = lbl.astype(float)
lbl = m.imresize(lbl, 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
def transform(self, 1)
lbl = self.encode_segmap(lbl)
classes = np.unique(lbl)
lbl = lbl.astype(float)
lbl = m.imresize(lbl, 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.
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), axis=2)
else:
# 4 images
xt_list = []
for i in range(images.shape[0]):
x_t = imresize(images[i], 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 ###############################
def preprocess_images(images):
if images.shape[0] < 4:
# single image
x_t = images[0]
x_t = imresize(x_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,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,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_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, 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), 3)).reshape(len(x), 3)
L, A, B = mean(val2[:,0]), mean(val2[:,1]),2])
val2[:, 0] = np.clip(val2[:, 0] - L + val1[:,0], 100)
val2[:, 1] = np.clip(val2[:, 1] - A + val1[:,1], -127, 128)
val2[:, 2] = np.clip(val2[:, 2] - B + val1[:,2], 128)
im[x,y] = color.lab2rgb(val2.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)), 'constant', constant_values=(0))
elif data_mode == 'label':
img = np.pad(img, im_sz-img.shape[1])), 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,'L')/255.0
all_shape.append(trimap.shape)
rgb_batch.append(misc.imresize(rgb,[320,320,3])-g_mean)
trimap = misc.imresize(trimap,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,'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,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),0]
origin_shape = alpha.shape
rgb = np.expand_dims(misc.imresize(rgb.astype(np.uint8),3]).astype(np.float32)-g_mean,0)
trimap = np.expand_dims(np.expand_dims(misc.imresize(trimap.astype(np.uint8),interp = 'nearest').astype(np.float32),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 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), interp='bilinear'), -90))[:,:3], [0.299, 0.587, 0.114])).reshape((1,80,80))
output_state = np.append(graydisplay,input_state[:,:p.HISTORY-1,:], axis=1)
#############################################################################################################################################################################
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, 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,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, _ = 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, interp='bilinear')
imsave(imgPath,new_img)
#Download img
#Later we can do multi thread apply workers to do faster work
def resizeImg(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, 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
def transformData(self, max_)
else:
data = data - 127.0
if(self.opts.has_key('randomflip') and self.opts['randomflip']):
if(np.random.rand() <= self.opts['randomflip_prob']):
data = np.flipud(data)
self.dataflip_state = True
return data
def predict():
# get data from drawing canvas and save as image
parseImage(request.get_data())
# read parsed image back in 8-bit,black and white mode (L)
x = imread('output.png', mode='L')
x = np.invert(x)
x = imresize(x,(28,28))
# reshape image data for use in neural network
x = x.reshape(1,28,1)
with graph.as_default():
out = model.predict(x)
print(out)
print(np.argmax(out, axis=1))
response = np.array_str(np.argmax(out, axis=1))
return response
def process_mot(path):
'''
1920 x 1080 -> 384 x 216
640 x 480 -> 320 x 240
'''
images = []
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if filename[-4:] == ".jpg" and "_ds" not in filename:
full_path = os.path.join(dirpath, filename)
img = misc.imread(full_path,mode='RGB')
if img.shape == LARGE_IMAGE_SIZE:
img = misc.imresize(img, size=LARGE_IMAGE_RESCALE)
img = pad_image(img, FINAL_IMAGE_SIZE)
elif img.shape == MEDIUM_IMAGE_SIZE:
img = misc.imresize(img, size=MEDIUM_IMAGE_RESCALE)
img = pad_image(img, FINAL_IMAGE_SIZE)
else:
print("Unexpected shape " + str(img.shape))
continue
output_filename = os.path.join(dirpath, filename[:-4] + "_ds.jpg")
misc.imsave(output_filename, img)
images.append(output_filename)
return images
def process_vot(path, min_height, min_width):
images = []
for dirpath, filenames in os.walk(path):
img_shape = None
pad_height = 0
pad_width = 0
for filename in filenames:
if filename[-4:] == ".jpg" and "_ds" not in filename:
full_path = os.path.join(dirpath,mode='RGB')
img_shape = img.shape
ratio = min(float(min_width)/img.shape[1], float(min_height)/img.shape[0])
img = misc.imresize(img, size=ratio)
img, pad_height, pad_width = pad_image(img, (min_height, min_width))
output_filename = os.path.join(dirpath, img)
images.append(output_filename)
if img_shape:
gt_path = os.path.join(dirpath, "groundtruth.txt")
preprocess_label(gt_path, ratio, img_shape, min_width, pad_width)
return images
def do_roc(self):
if self.gan_mode and self.dmodel2 is not None:
dmodel_cur = self.dmodel2
scale_factor = 2
elif self.dmodel is not None:
dmodel_cur = self.dmodel
scale_factor = self.scale_factor
else:
theApp.cur_hist_tex = theApp.standard_hist_tex
theApp.cur_roc_tex = theApp.standard_roc_tex
return
encoded_vector_source = self.get_encoded(dmodel_cur, self.cur_vector_source, scale_factor)
encoded_vector_dest = self.get_encoded(dmodel_cur, self.cur_vector_dest, scale_factor)
attribute_vector = encoded_vector_dest - encoded_vector_source
threshold = None
outfile = "{}/{}".format(roc_dir, get_date_str())
do_roc(attribute_vector, encoded, attribs, attribute_index, threshold, outfile)
hist_img = imread("{}_hist_both.png".format(outfile), mode='RGB')
roc_img = imread("{}_roc.png".format(outfile), mode='RGB')
hist_img = imresize(hist_img, roc_image_resize)
roc_img = imresize(roc_img, roc_image_resize)
theApp.cur_hist_tex = image_to_texture(hist_img)
theApp.cur_roc_tex = image_to_texture(roc_img)
def create_dictionary_dl(lmbd, K=100, N=10000, dir_mnist='save_exp/mnist'):
import os.path as osp
fname = osp.join(dir_mnist, "D_mnist_K{}_lmbd{}.npy".format(K, lmbd))
if osp.exists(fname):
D = np.load(fname)
else:
from sklearn.decomposition import DictionaryLearning
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
im = mnist.train.next_batch(N)[0]
im = im.reshape(N, 28, 28)
im = [imresize(a, (17, 17), interp='bilinear', mode='L')-.5
for a in im]
X = np.array(im).reshape(N, -1)
print(X.shape)
dl = DictionaryLearning(K, alpha=lmbd*N, fit_algorithm='cd',
n_jobs=-1, verbose=1)
dl.fit(X)
D = dl.components_.reshape(K, -1)
np.save(fname, D)
return D
def main(input_pic):
img = cv.imread(input_pic,cv.CV_LOAD_IMAGE_GRAYSCALE)
img=sp.gaussian_filter(img,sigma=3)
img= imresize(img,((len(img)/10),(len(img[0])/10)))
img_arr=np.asarray(img,dtype="int32")
LoG_arr=LoG_Filter(img_arr)
cv.imwrite('LoG_image.jpg',LoG_arr)
LoG_arr=cv.imread('LoG_image.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
Hist=genHistogram(LoG_arr)
#print(Hist)
for i in range(0,len(LoG_arr)):
for j in range(0,len(LoG_arr[0])):
if LoG_arr[i][j]<200:
LoG_arr[i][j]=0
else:
LoG_arr[i][j]=255
cv.imwrite('LoG_image.jpg',LoG_arr)
#img_new=cv.imread('LoG_image.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
def preprocess(self, screen, new_game=False):
"""Converts to grayscale,resizes and stacks input screen.
:param screen: array image in [0; 255] range with shape=[H,W,C]
:param new_game: if True - repeats passed screen `memlen` times
otherwise - stacks with prevIoUs screens"
:type screen: nd.array
:type new_game: bool
:return: image in [0.0; 1.0] stacked with last `memlen-1` screens;
shape=[1,h,memlen]
:rtype: nd.array"""
gray = screen.astype('float32').mean(2) # no need in true grayscale,just take mean
# convert values into [0.0; 1.0] range
s = imresize(gray, (self.W, self.H)).astype('float32') * (1. / 255)
s = s.reshape(1, s.shape[0], s.shape[1], 1)
if new_game or self.stacked_s is None:
self.stacked_s = np.repeat(s, self.memlen, axis=3)
else:
self.stacked_s = np.append(s, self.stacked_s[:, :self.memlen - 1], axis=3)
return self.stacked_s
def preprocess(self, new_game=False):
luminance = screen.astype('float32').mean(2) # no need in true grayscale,just take mean
# crop top/bottom Atari specific borders
if self.env.spec.id == 'SpaceInvaders-v0':
# crop only bottom in SpaceInvaders,due to flying object at the top of the screen
luminance = luminance[:-15, :]
else:
luminance = luminance[36:-15, :]
# convert into [0.0; 1.0]
s = imresize(luminance, axis=3)
return self.stacked_s
def resize_images(x, shape):
from scipy.misc import imresize
reszie_func = lambda x, shape: imresize(x, shape, interp='bilinear')
if x.ndim == 4:
def reszie_func(x, shape):
# x: 3D
# The color channel is the first dimension
tmp = []
for i in x:
tmp.append(imresize(i, shape).reshape((-1,) + shape))
return np.swapaxes(np.vstack(tmp).T, 1)
imgs = []
for i in x:
imgs.append(reszie_func(i, shape))
return imgs
def __caffe_predict(self, width, url):
# logger = logging.getLogger(__name__)
#
# logger.info("caffe_predict has been called")
input_layer = net.inputs[0]
output_layer = net.outputs[0]
r = requests.get(url, allow_redirects=False)
arr = numpy.asarray(bytearray(r.content), dtype=numpy.uint8)
img = cv2.imdecode(arr, -1)
resized_img = imresize(img, (height,width), 'bilinear')
transposed_resized_img = numpy.transpose(resized_img, (2,0,1))
reqd_shape = (1,) + transposed_resized_img.shape
#net.blobs["data_q"].reshape(*reqd_shape)
#net.blobs["data_q"].data[...] = transposed_resized_img
net.blobs[input_layer].reshape(*reqd_shape)
net.blobs[input_layer].data[...] = transposed_resized_img
net.forward()
#result = net.blobs['latent_q_encode'].data[0].tolist()
result = net.blobs[output_layer].data[0].tolist()
return result
def agent_start(self, observation):
# Preprocess
tmp = np.bitwise_and(np.asarray(observation.intArray[128:]).reshape([210, 160]), 0b0001111) # Get Intensity from the observation
obs_array = (spm.imresize(tmp, (110, 84)))[110-84-8:110-8, :] # Scaling
# Initialize State
self.state = np.zeros((4, 84, 84), dtype=np.uint8)
self.state[0] = obs_array
state_ = cuda.to_gpu(np.asanyarray(self.state.reshape(1, 4, dtype=np.float32))
# Generate an Action e-greedy
returnAction = Action()
action, Q_Now = self.DDQN.e_greedy(state_, self.epsilon)
returnAction.intArray = [action]
# Update for next step
self.lastAction = copy.deepcopy(returnAction)
self.last_state = self.state.copy()
self.last_observation = obs_array
return returnAction
def preprocess_faces(images, n_components='default', resize_shape='default'):
""" Notes: Images are all square,but not the same size. We resize all the
images to be the same sized square,thereby preserving the
aspect ratios.
"""
for img in images:
assert img.shape[0] == img.shape[1]
if resize_shape == 'default':
resize_shape = get_smallest_shape(images)
preprocessed_images = []
for img in images:
prepped_img = imresize(img, resize_shape).astype(float)
prepped_img = prepped_img.flatten()
preprocessed_images.append(prepped_img)
preprocessed = get_principal_components(preprocessed_images, n_components)
return preprocessed
def find_faces(self, image):
faces = []
bounding_Boxes, _ = align.detect_face.detect_face(image, self.minsize,
self.pnet, self.rnet, self.onet,
self.threshold, self.factor)
for bb in bounding_Boxes:
face = Face()
face.container_image = image
face.bounding_Box = np.zeros(4, dtype=np.int32)
img_size = np.asarray(image.shape)[0:2]
face.bounding_Box[0] = np.maximum(bb[0] - self.face_crop_margin / 2, 0)
face.bounding_Box[1] = np.maximum(bb[1] - self.face_crop_margin / 2, 0)
face.bounding_Box[2] = np.minimum(bb[2] + self.face_crop_margin / 2, img_size[1])
face.bounding_Box[3] = np.minimum(bb[3] + self.face_crop_margin / 2, img_size[0])
cropped = image[face.bounding_Box[1]:face.bounding_Box[3], face.bounding_Box[0]:face.bounding_Box[2], :]
face.image = misc.imresize(cropped, (self.face_crop_size, self.face_crop_size), interp='bilinear')
faces.append(face)
return faces
def resize_to_optimal(infile, scale_ratio, rect, outfile):
image_array = imread(infile, mode='RGB')
im_shape = image_array.shape
h, _ = im_shape
width = float(rect.right()-rect.left())
scale_amount = (optimal_extent * scale_ratio) / width
new_w = int(scale_amount * w)
new_h = int(scale_amount * h)
new_w = new_w - (new_w % 4)
new_h = new_h - (new_h % 4)
print("optimal resize of width {} and ratio {} went from {},{} to {},{}".format(width, h, new_w, new_h))
new_shape = (new_h, new_w)
image_array_resized = imresize(image_array, new_shape)
imsave(outfile, image_array_resized)
return new_shape
def input_wrapper(f):
image = misc.imread(f)
sx,sy = image.shape
diff = np.abs(sx-sy)
sx,sy = image.shape
image = np.pad(image,((sx//8,sx//8),(sy//8,sy//8)),'constant')
if sx > sy:
image = np.pad(image,((0,0),(diff//2,diff//2)),'constant')
else:
image = np.pad(image,((diff//2,diff//2),(0,'constant')
image = dilation(image,disk(max(sx,sy)/32))
image = misc.imresize(image,(32,32))
if np.max(image) > 1:
image = image/255.
return image
def input_wrapper(f):
image = misc.imread(f)
# image[image>50]=255
# image[image<=50]=0
sx,32))
if np.max(image) > 1:
image = image/255.
return image
def main():
x = tf.placeholder(tf.float32, [None, 224, 3])
network, probs = build_vgg(x)
# network2,probs2 = build_vgg(x)
sess = tf.InteractiveSession()
tl.layers.initialize_global_variables(sess)
network.print_params()
network.print_layers()
npz = np.load('vgg16_weights.npz')
params = []
for val in sorted( npz.items() ):
print(" Loading %s" % str(val[1].shape))
params.append(val[1])
tl.files.assign_params(sess, params, network)
img1 = imread('laska.png', mode='RGB')
img1 = imresize(img1, (224, 224))
prob = sess.run(probs, Feed_dict={x: [img1]})[0]
print(prob)
def _merge(self, images, size):
h, w = images.shape[1], images.shape[2]
h_ = int(h * self.resize_factor)
w_ = int(w * self.resize_factor)
img = np.zeros((h_ * size[0], w_ * size[1]))
for idx, image in enumerate(images):
i = int(idx % size[1])
j = int(idx / size[1])
image_ = imresize(image, size=(w_,h_), interp='bicubic')
img[j*h_:j*h_+h_, i*w_:i*w_+w_] = image_
return img
def _merge(self, h_), interp='bicubic')
img[j * h_:j * h_ + h_, i * w_:i * w_ + w_] = image_
return img
# borrowed from https://github.com/ykwon0407/variational_autoencoder/blob/master/variational_bayes.ipynb
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。